Skip to content

Instantly share code, notes, and snippets.

@mathomp4
Last active February 24, 2021 21:55
Show Gist options
  • Save mathomp4/e7a4082f1b0b31ba187693a4b6563592 to your computer and use it in GitHub Desktop.
Save mathomp4/e7a4082f1b0b31ba187693a4b6563592 to your computer and use it in GitHub Desktop.
Automatically ignore CMake build and install directories

This bit of CMake is based off of code from Michael Hirsch (aka @scivision)

# https://www.scivision.dev/cmake-auto-gitignore-build-dir/
# --- auto-ignore build directory
if(NOT EXISTS ${PROJECT_BINARY_DIR}/.gitignore)
  file(WRITE ${PROJECT_BINARY_DIR}/.gitignore "*")
endif()

# Piggyback that file into install
install(
   FILES ${PROJECT_BINARY_DIR}/.gitignore
   DESTINATION ${CMAKE_INSTALL_PREFIX}
   )
@mathomp4
Copy link
Author

I might ping @scivision here and ask his opinion on the second step. I shamelessly "borrowed" his cool trick for automatically ignoring build directories, but I usually install parallel to build inside the git repo I'm building. Thus, I'd like to ignore that directory no matter the name. The above second step works...but it just seems so inelegant.

Thus, I climb the mountain and ask the guru @scivision if he has a more elegant way to do this...

@scivision
Copy link

I think that's an effective way to ignore the installed directory hierarchy, at cmake --install time

@mathomp4
Copy link
Author

I think that's an effective way to ignore the installed directory hierarchy, at cmake --install time

@scivision, Thanks! And I really need to learn my cmake --install etc. I still do old school make install or ninja install.

@scivision
Copy link

It's just about abstracting away details so that scripts and docs have no need to prefer a particular build system over another. CMake 3.20 significantly enhances the preset functionality and I will be heavily using presets for configure build and test.

@mathomp4
Copy link
Author

I had honestly not heard about presets, but now I am quite intrigued by them. They look quite useful!

@scivision
Copy link

presets aren't a cure-all, but they can help avoid the need to use scripts to drive CMake, or excessively long command lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment