[Nitpicking] Making generator expression evaluates to null/nil/nothing/nada/zilch rather than empty/blank/""?

Hi,

Thanks to this great post and SO answer, I have CMake writes FooConfig.cmake file whose content varies depending on target type (STATIC, SHARED) evaluated by generator expression.

To this end, FooConfig.cmake.in reads as:

include(CMakeFindDependencyMacro)

$<$<STREQUAL:$<TARGET_PROPERTY:Foo,TYPE>,STATIC_LIBRARY>:find_dependency(Bar REQUIRED)>

And project Foo’s CMakeLists.txt reads as:

include(CMakePackageConfigHelpers)

# Generate <package>Config.cmake
configure_package_config_file(
	${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
	${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
	INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)

# Evaluate generator expression in generated <package>Config.cmake
file(
	GENERATE
	OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
	INPUT ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
)

When Foo target is created as a static library, the resulting FooConfig.cmake file reads as expected as:

include(CMakeFindDependencyMacro)

find_dependency(Bar REQUIRED)

Great! And when Foo target is generated as shared library, rather than evaluating to nothing in place of find_dependency(Bar REQUIRED), an empty/blank/new line is inserted to the file. Is there a way not to insert this empty line, as if the generator expression evaluates to nothing rather than empty?

In the advent multiple dependencies would be required, I would like to prevent multiple empty lines being inserted in the generated FooConfig.cmake file.

I know, just nitpicking…

The newline comes from the newline after the closing >. You could move this inside of the genex, but then you have a file without a newline at the end which isn’t always acceptable (Git marks it as a “whitespace problem” at least).

Thanks for the explanation, I’ll try your suggestion. There are other lines following the genex in fact, so the file will nevertheless ends with a new line.