I'm writing a CMakeLists.txt
for a project of mine, and I need to add (under certain conditions which don't matter here) a compiler flag to $CMAKE_CXX_FLAGS
(and it doesn't matter that it's C++, it could C, or Fortran or what-not). Now, I can do it this way:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAG_HERE}")
or, to be more modern:
string(APPEND CMAKE_CXX_FLAGS " ${EXTRA_FLAG_HERE}")
but if the flag is already in there, I don't want to add it twice. Now, I could do
if(NOT string(FIND CMAKE_CXX_FLAGS "${EXTRA_FLAG_HERE}")
string(APPEND CMAKE_CXX_FLAGS " ${EXTRA_FLAG_HERE}")
endif()
Edit: but as @Fred points out, this won't actually avoid duplicates if that flag is introduced by CMake otherwise than through $CMAKE_CXX_FLAGS
.
I was hoping there's something "built-in" which adds a flag while assuring no duplication (including being more robust against occurrences of ${EXTRA_FLAG}
which within the argument of another flag, e.g. in a -D
flag).
So, what do I do?