Directory scoping for set_property(SOURCE ...) or set_source_files_properties

I don’t understand what the DIRECTORY scoping does to these commands. That’s sort of the preamble question.

The main question is if it’s possible to change/append the COMPILE_OPTIONS for all the source files in a given directory, without using a glob.

My ideal of “how it should work” would be
set_property(SOURCE DIRECTORY SomeSubdir APPEND PROPERTY COMPILE_OPTIONS -Wblah)

to enable warning blah on all the files that came in through SomeSubdir, with bonus points if it also applied to SomeSubdir/AddtlSubdir files too.

Also, there’s just one target in play here. All the files from all the subdirs get added to this one target. But my wish is to change how some of them get compiled. There’s a SO answer here that talks about doing it through the use of a glob. Is it possible without globbing?

1 Like

I think I figured out the DIRECTORY param though experimentation. As the docs state, the properties on source files can only be set from the directory in which the target consuming that source file is defined. So if you want to set a source file property from another directory you need to specify the DIRECTORY that defined the target. And I guess TARGET_DIRECTORY is a helper that allows you to refer to the target, and not hardcode a path in DIRECTORY.

Coming back to my example, if I had SomeSubdir/foo.c, I could change the source properties from directory SomeSubdir/CMakeLists.txt if I had

  set_property(SOURCE foo.c DIRECTORY .. PROPERTY COMPILE_OPTIONS "-Wblah")

assuming the target was defined in the parent dir.

Still wondering about the main question though. Is there a CMake command that would operate on all files in SomeSubdir without enumerating each one like I did with foo.c, above?

There’s set_source_files_properties but that looks like syntactic sugar for the set_property command. It still requires the files to be enumerated.

1 Like

Your assessment of the DIRECTORY keyword to set_property and set_source_files_properties is correct.

The directory-scoped command available is add_compile_options. This command adds compile options to all source files in the current directory and below. This command is syntactic sugar for set_property (DIRECTORY PROPERTY COMPILE_DEFINITIONS <opts...>)

This command adds compile options to all source files in the current directory and below.

This is not what I observe. Example:

% find . ! -type d
./foo.cpp
./CMakeLists.txt
./bar/CMakeLists.txt
./bar/bar.cpp

% echo ----- | cat CMakeLists.txt - bar/CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(foo LANGUAGES CXX)

add_executable(foo foo.cpp)
add_subdirectory("bar")
-----
add_compile_options("-Wbitfield-width")
target_sources(foo PRIVATE bar.cpp)

% cmake -B build -G Ninja
...

-Wbitfield-width is NOT in the compile flags for bar.cpp. If I understood your 2nd paragraph comment correctly, it should have been added to any source files in bar/.

I believe that the docs are stating that add_compile_options changes the DIRECTORY properties, but (!) those properties only affect newly defined targets at that directly and below.

ah, perhaps it would only be applied to any targets created in the bar directory.