# Directory scoping for set\_property(SOURCE ...) or set\_source\_files\_properties

**URL:** <https://discourse.cmake.org/t/directory-scoping-for-set-property-source-or-set-source-files-properties/7493>\
**Category:** Code\
**Created:** [February 18, 2023, 2:01am UTC](https://discourse.cmake.org/t/directory-scoping-for-set-property-source-or-set-source-files-properties/7493 "2023-02-18T02:01:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoeM](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/0ea827/32.png) [@JoeM](https://discourse.cmake.org/u/JoeM)\
**Post date:** [February 18, 2023, 2:01am UTC](https://discourse.cmake.org/t/directory-scoping-for-set-property-source-or-set-source-files-properties/7493/1 "2023-02-18T02:01:50Z")

</div>

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](https://stackoverflow.com/a/51826822) that talks about doing it through the use of a glob. Is it possible without globbing?

---

<div class="post-metadata">

**Author:** ![JoeM](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/0ea827/32.png) [@JoeM](https://discourse.cmake.org/u/JoeM)\
**Post date:** [February 21, 2023, 5:30pm UTC](https://discourse.cmake.org/t/directory-scoping-for-set-property-source-or-set-source-files-properties/7493/2 "2023-02-21T17:30:40Z")

</div>

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

```auto
  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.

---

<div class="post-metadata">

**Author:** ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)\
**Post date:** [February 21, 2023, 7:33pm UTC](https://discourse.cmake.org/t/directory-scoping-for-set-property-source-or-set-source-files-properties/7493/3 "2023-02-21T19:33:23Z")

</div>

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...>)`

---

<div class="post-metadata">

**Author:** ![JoeM](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/0ea827/32.png) [@JoeM](https://discourse.cmake.org/u/JoeM)\
**Post date:** [February 27, 2023, 7:54pm UTC](https://discourse.cmake.org/t/directory-scoping-for-set-property-source-or-set-source-files-properties/7493/4 "2023-02-27T19:54:04Z")

</div>

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

This is not what I observe. Example:

```auto
% 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.

---

<div class="post-metadata">

**Author:** ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)\
**Post date:** [February 27, 2023, 8:28pm UTC](https://discourse.cmake.org/t/directory-scoping-for-set-property-source-or-set-source-files-properties/7493/5 "2023-02-27T20:28:14Z")

</div>

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