CMake .gitignore out-of-source build tree

CMake can place a .gitignore file in the top-level build directory to allow making build directories with arbitrary names not cause Git clutter. Rather than adding arbitrary directory names to the project-wide .gitignore, be programmatic by having CMake create a .gitignore in the out-of-source build directory.

In general, it is recommended to use out-of-source builds with CMake as well as other build systems in general. Meson build system does not allow in-source builds.

Put this line of code in the top-level CMakeLists.txt to auto-ignore out-of-source build directory tree.

if(NOT PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
  # Git auto-ignore out-of-source build directory
  file(GENERATE OUTPUT .gitignore CONTENT "*")
endif()