I want to use the new file sets feature of cmake 3.28 since it seems to simplify a lot of steps (installing, exporting). However, I can’t really wrap my head around what exactly is going on behind the scenes. Here’s my project cmake file:
lib_file_loader/CMakeList.txt:
cmake_minimum_required(VERSION 3.28)
project(lib_file_loader VERSION 0.1.0 LANGUAGES CXX)
add_library(lib_file_loader src/file_loader.cpp)
add_library(file_loader::lib_file_loader ALIAS lib_file_loader)
target_sources(lib_file_loader
PRIVATE
"src/file_loader.cpp"
PUBLIC FILE_SET HEADERS BASE_DIRS "src/include" FILES
"src/include/lib_file_loader/file_loader.h"
)
target_include_directories(lib_file_loader
PRIVATE FILE_SET HEADERS
PUBLIC FILE_SET INTERFACE_HEADERS
)
message(STATUS "Interface headers: ${lib_file_loader_INTERFACE_HEADERS}")
This generates ok, however the output of the interface headers is empty:
-- Interface headers: <--- i expected this to be filled
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/myuser/../file_loader/build
The way I thought this would work was for cmake to detect automatically that I want to create a file set of type HEADERS for PUBLIC headers and it would choose the INTERFACE_HEADERS set for me to populate the files into. This however doesn’t seem to be the case.
So I thought ok maybe I need to specify the fileset name explicitely, so I changed the ?target_sources
line to this:
PUBLIC FILE_SET INTERFACE_HEADERS TYPE HEADERS BASE_DIRS "src/include" FILES
But this gives me an error saying that:
CMake Error at lib_file_loader/CMakeLists.txt:9 (target_sources):
target_sources Non-default file set name must contain only letters,
numbers, and underscores, and must not start with a capital letter or
underscore
I’m a bit lost on how to populate the interface header set to retrieve it later for target_include_directories. Things I also hoped for (but seem to be mistaken) are:
target_include_directories
will automatically know for file sets of HEADERS type to go into either PRIVATE or PUBLIC category without respecifying.BASE_DIRS
(what is it for anyway? I can’t find anything in the documentation) to allow header files to automatically be globbed up in the said directory so I don’t even have to specify them.
I like to keep my cmake minimal and thought file sets look good enough, but I don’t seem to understand them properly. Can someone shed some light on the mechanics on how to use them?