I just discovered by accident that target_sources()
does not complain if you add the same BASE_DIR
to a file set more than once. It appears this works fine with no side effects, as far as I could tell. It turns out this is quite useful, since it allows the following pattern to be used in both in-source and out-of-source builds without any special handling:
include(GenerateExportHeader)
generate_export_header(Something
EXPORT_FILE_NAME include/Something/something_export.h
)
target_sources(Something
PUBLIC
FILE_SET HEADERS
BASE_DIRS
include
${CMAKE_CURRENT_BINARY_DIR}/include
FILES
${CMAKE_CURRENT_BINARY_DIR}/include/Something/something_export.h
)
In practice, a project might move the generate_export_header()
parts down into a subdirectory, but I’ve put it all in the one file above just to more easily demonstrate the main relevant aspects of the pattern.
Given that it is a useful behavior to allow a base directory to be added more than once and that it is already in a release, should we formally document it as something projects can rely on? Seems we might as well.
CC: @kyle.edwards