| 1 | # CMake in FlightGear overview |
| 2 | |
| 3 | CMake has evolved considerably in the past decade; if you're reading external tutorials abput it, ensure |
| 4 | they mention 'modern CMake', or the information will be incorrect. |
| 5 | |
| 6 | The top-level `CMakeLists.txt` handles configuration options, finding dependencies, and scanning |
| 7 | the rest of the source tree. Across the source tree we add executables, including the main FGFS |
| 8 | binary but also other helpers and utilities. We also define targets for helper libraries, for |
| 9 | various reasons; for example to build some code with different include paths or flags. |
| 10 | |
| 11 | Due to the historical code structure, we use some helper functions to collect most of the |
| 12 | application sources into two global CMake variables, which are then read and added to the |
| 13 | main executable target, in `src/Main/CMakeList.txt`. Therefore, many subdirectories have |
| 14 | a trivial `CMakeLists.txt` which simply calls the helper functions to add its sources: |
| 15 | |
| 16 | ``` |
| 17 | include(FlightGearComponent) |
| 18 | |
| 19 | set(SOURCES |
| 20 | foo.cxx |
| 21 | bar.cxx |
| 22 | ) |
| 23 | |
| 24 | set(HEADERS |
| 25 | foo.hxx |
| 26 | bar.hxx |
| 27 | ) |
| 28 | |
| 29 | flightgear_component(MyComp "${SOURCES}" "${HEADERS}") |
| 30 | ``` |
| 31 | |
| 32 | The global properties used are `FG_SOURCES` and `FG_HEADERS`. |
| 33 | |
| 34 | ## Configurations |
| 35 | |
| 36 | Official release builds are built with `RelWithDebInfo`; this is also the most useful configuration for |
| 37 | development, since on Windows, `Debug` is unusably slow. If trying to optimize performance, |
| 38 | keep in mind that compiler flags must be manually set for `RelWithDebInfo`; they are _not_ |
| 39 | automatically inherited from `Release`. |
| 40 | |
| 41 | Adding additional configurations is possible: for example for profiling. CMake also picks up |
| 42 | the `CXXFLAGS` environment variable to pass ad-hoc compiler options, without modifying the |
| 43 | build systen. |
| 44 | |
| 45 | ## Dependencies |
| 46 | |
| 47 | All dependencies should be handled via an `IMPORTED` target: this ensures that include paths, |
| 48 | link options, etc specific to the dependency are handled correctly across different platforms. |
| 49 | |
| 50 | For some dependencies, there may be a zFoo-Config.cmakez which defines such a target for |
| 51 | you automatically. Or there may be an existing `FindFoo.cmake` which does the same. If neither |
| 52 | of these situations exist, create a custom finder file in `CMakeModules`, following the existing |
| 53 | examples. |
| 54 | |
| 55 | CMake tracks transitive dependencies precisely, so for example if your new dependency is used |
| 56 | in SimGear, it will automatically be added to the include / link paths for FlightGear based on |
| 57 | the SimGear build type. |
| 58 | |
| 59 | If you encounter a case where a downstream target is missing an include path or flag for a |
| 60 | dependency, it typically indicates a bug in your dependency graph. Do _not_ fix it by manually |
| 61 | modifying the downstream target's include path or flags. Rather, fix your dependency graph |
| 62 | and/or `INTERFACE` exports from your dependency, so that CMake can see the required transitive |
| 63 | dependencies correctly. |
| 64 |