I have a long, complex, quoted generator expression that I’d like to make more readable with whitespace, as well as splitting across several lines. For example:
set(MYPROJ_LINK_TO_JSON_ENABLED
"$<AND:\
$<NOT: $<BOOL:${MYPROJ_KNOB_NO_JSON}> >,\
$<TARGET_EXISTS:SomeJsonLib::SomeJsonLib>\
>")
target_link_libraries(mylib
PRIVATE
"$< $<BOOL:${MYPROJ_LINK_TO_JSON_ENABLED}>: SomeJsonLib::SomeJsonLib >")
where MYPROJ_KNOB_NO_JSON
is a cache variable that can be set by the user to disable the JSON feature of my library.
Compare to the same without whitespace in the generator expressions:
set(MYPROJ_LINK_TO_JSON_ENABLED
"$<AND:$<NOT:$<BOOL:${MYPROJ_KNOB_NO_JSON}>>,$<TARGET_EXISTS:SomeJsonLib::SomeJsonLib>>")
target_link_libraries(mylib
PRIVATE
"$<$<BOOL:${MYPROJ_LINK_TO_JSON_ENABLED}>:SomeJsonLib::SomeJsonLib>")
I’d like to know if the first version with the whitespace is acceptable. The manual doesn’t mention anything about whitespace within generator expressions, and web searches have not come up with any answers (only problems related to forgetting to quote the expression).
BTW, the reason why I skip linking RapidJSON::rapidjson
if it doesn’t exist (due to a prior find_package(RapidJSON)
call failing), is that I want the configuration stage to continue running to completion so that the user may change the configuration via cmake-gui or their IDE. This also allows IDEs to list source files even when the dependencies are not found, thus allowing someone to peruse the source code in an IDE without having to first install the dependencies.
If there’s a cleaner way to achieve what I’m looking for, I’m open to suggestions.
UPDATE: Sorry, I thought that attempting to link to a non-existent SomeJsonLib::SomeJsonLib
target results in configuration failure, but I was mistaken. My main question about whitespace within generator expressions still stands, though.