CMake Cookbook
上QQ阅读APP看书,第一时间看更新

How it works

The foreach-endforeach syntax can be used to express the repetition of certain tasks over a list of variables. In our case, we used it to manipulate, set, and get the compiler flags of specific files in the project. This CMake snippet introduced two additional new commands:

  • set_source_files_properties(file PROPERTIES property value), which sets the property to the passed value for the given file. Much like targets, files also have properties in CMake. This allows for extremely fine-grained control over the build system generation. The list of available properties for source files can be found here: https://cmake.org/cmake/help/v3.5/manual/cmake-properties.7.html#source-file-properties.
  • get_source_file_property(VAR file property), which retrieves the value of the desired property for the given file and stores it in the CMake VAR variable.
In CMake, lists are semicolon-separated groups of strings. A list c an be created either by the  list  or the  set  commands.  For example, both set(var a b c d e) and list(APPEND a b c d e) create the list a;b;c;d;e .
To lower optimization for a set of files, it would probably be cleaner to collect them into a separate target (library) and set the optimization level explicitly for this target instead of appending a flag, but in this recipe our focus was on foreach-endforeach.