I'm refactoring a cmake file to use modern CMAKE practices for an personal project. However since I don't believe the operating system comes as a generalized variable, in order to test the operating system in CMAKE I have to do the following (based on this answer):
if(UNIX AND NOT APPLE)
set(LINUX TRUE)
endif()
#...
if(LINUX)
set(MY_PROJECT_LINK_ARGUMENTS ...)
elseif(WIN32)
set(MY_PROJECT_LINK_ARGUMENTS ...)
else()
message( FATAL_ERROR "Unsupported operating system")
endif()
add_library(xxx STATIC)
#...
target_link_libraries(xxx INTERFACE ${MY_PROJECT_LINK_LIBRARIES} ...)
add_library(yyy STATIC)
#...
target_link_libraries(yyy INTERFACE ${MY_PROJECT_LINK_LIBRARIES} ...)
add_library(zzz STATIC)
#...
target_link_libraries(zzz INTERFACE ${MY_PROJECT_LINK_LIBRARIES} ...)
What I'm confused about is how to accomplish this in idiomatic modern target based CMAKE. Since I don't know of a CMAKE variable that exists that generally encapsulates the operating system, I created an OS variable.
if(UNIX AND NOT APPLE)
set(LINUX TRUE)
set(PROJECT_OS LINUX)
elseif(APPLE)
set(PROJECT_OS APPLE)
elseif(WIN32)
set(PROJECT_OS WIN32)
endif()
And then I create a generator expression
target_link_libraries(xxx INTERFACE
$<$<BOOL:$<PROJECT_OS:LINUX>>: ...>
$<$<BOOL:$<PROJECT_OS:WIN32>>: ...>
...)
But now I've already created a variable and if I were to pass the same arguments to each target (xxx
, yyy
, zzz
) I would have duplicated the same thing several times.
The only way I see to solve this is with a variable, but modern CMAKE guidelines apparently say we should stay far away from variables (example A, B, and C), so how am I supposed to accomplish this task with out them?