This seems to put the files into the correct place, but if I then use this with find_package(myproj) and link against myproj::foo, I get ${PREFIX}/include/myproj in the include directories rather than simply ${PREFIX}/include.
How can I install header files (using FILE_SET) where I need to use ${CMAKE_CURRENT_SOURCE_DIR} itself as part of the installed path?
(For reference, I am trying to adapt an existing project where libraries were set up such that you could simply do a git clone directly into /usr/local/include and get the headers in the “right” place.)
EDIT: It seems as though the include path when imported is defined by FILE_SET DESTINATION.
Yes. The idea is that if myproj/ is part of the path to be included, it would be under the “BASE_DIRS” rather than straddling it. I think you want BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/.." FILES foo.h (with myproj coming from the name of the current directory) and then a DESTINATION include.
You shouldn’t use the name of the top level directory for your BASE_DIRS. That is not a directory name your project controls, the user could use a different name. If you’re using file sets, you pretty much MUST put your headers in a subdirectory.
Note also that you should avoid using your top level source directory as a BASE_DIRS too. It is quite common for CI systems and often CMake presets too to specify a build directory that is a subdirectory of the top level source directory. If you ever need to add a part of the build directory as a BASE_DIRS to a file set (which you would if you were using something like generate_export_header() from the GenerateExportHeader module), you won’t be able to if the top level source directory is already a base directory of the file set. File sets cannot have overlapping BASE_DIRS.
I don’t disagree, but this is a slow migration to CMake from a custom build system which made heavy use of Git submodules. It also made a point of not separating headers from sources, but instead grouped headers and sources into “libraries”. That is, a typical structure would be:
When one of the “libraries” (like foo or bar) got too big or needed to be used somewhere else, it would just get pulled out into its own repository and added to project/libraries/ as a (Git) submodule. Of course, that meant that now all of the headers and sources needed to be at top level of the new Git repository, since it had just been a subdirectory of a larger project.
EDIT: And, of course, it didn’t have a separate build tree, so object files would get scattered across all of those directories!
EDIT2: Even further, the old build system didn’t even have installing headers as an option – of course you would always build everything from scratch every time!