Merge remote-tracking branch 'upstream/master' into abo_emerg_flight_rearm
[inav.git] / cmake / main.cmake
blobd8e2dddf3ece31c26249ce68bc51b858c2a0b153
1 set(MAIN_INCLUDE_DIRS
2     "${MAIN_LIB_DIR}"
3     "${MAIN_SRC_DIR}"
4     "${MAIN_LIB_DIR}/main/MAVLink"
7 set(MAIN_DEFINITIONS
8     __FORKNAME__=inav
9     __REVISION__="${GIT_REV}"
13 # Can't check for OSX yet at this point
14 if(SITL)
15     set(MAIN_COMPILE_OPTIONS
16         -Wall
17         -Wextra
18         -Wdouble-promotion
19         -Wstrict-prototypes
20         -Werror=switch
21         #-Wno-unknown-warning-option
22     )
23 else()
24     set(MAIN_COMPILE_OPTIONS
25         -Wall
26         -Wextra
27         -Wunsafe-loop-optimizations
28         -Wdouble-promotion
29         -Wstrict-prototypes
30         -Werror=switch
31     )
32 endif()
34 macro(main_sources var) # list-var src-1...src-n
35     set(${var} ${ARGN})
36     list(TRANSFORM ${var} PREPEND "${MAIN_SRC_DIR}/")
37 endmacro()
39 function(exclude var excludes)
40     set(filtered "")
41     foreach(item ${${var}})
42         if (NOT ${item} IN_LIST excludes)
43             list(APPEND filtered ${item})
44         endif()
45     endforeach()
46     set(${var} ${filtered} PARENT_SCOPE)
47 endfunction()
49 function(exclude_basenames var excludes)
50     set(filtered "")
51     foreach(item ${${var}})
52         get_filename_component(basename ${item} NAME)
53         if (NOT ${basename} IN_LIST excludes)
54             list(APPEND filtered ${item})
55         endif()
56     endforeach()
57     set(${var} ${filtered} PARENT_SCOPE)
58 endfunction()
60 function(glob_except var pattern excludes)
61     file(GLOB results ${pattern})
62     list(LENGTH results count)
63     if(count EQUAL 0)
64         message(FATAL_ERROR "glob with pattern '${pattern}' returned no results")
65     endif()
66     exclude_basenames(results "${excludes}")
67     set(${var} ${results} PARENT_SCOPE)
68 endfunction()
70 function(get_generated_files_dir output target_name)
71     set(${output} ${CMAKE_CURRENT_BINARY_DIR}/${target_name} PARENT_SCOPE)
72 endfunction()
74 function(setup_executable exe name)
75     get_generated_files_dir(generated_dir ${name})
76     target_compile_options(${exe} PRIVATE ${MAIN_COMPILE_OPTIONS})
77     target_include_directories(${exe} PRIVATE ${generated_dir} ${MAIN_INCLUDE_DIRS})
78     target_compile_definitions(${exe} PRIVATE ${MAIN_DEFINITIONS} __TARGET__="${name}" ${name})
79     # XXX: Don't make SETTINGS_GENERATED_C part of the build,
80     # since it's compiled via #include in settings.c. This will
81     # change once we move off PGs
82     target_sources(${exe} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/${name}/${SETTINGS_GENERATED_H}")
83     set_target_properties(${exe} PROPERTIES
84         RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
85     )
86     if(IS_RELEASE_BUILD AND NOT (CMAKE_HOST_APPLE AND SITL))
87         set_target_properties(${exe} PROPERTIES
88             INTERPROCEDURAL_OPTIMIZATION ON
89         )
90     endif()
91 endfunction()
93 function(setup_firmware_target exe name)
94     setup_executable(${exe} ${name})
95     enable_settings(${exe} ${name})
96     get_property(targets GLOBAL PROPERTY VALID_TARGETS)
97     list(APPEND targets ${name})
98     set_property(GLOBAL PROPERTY VALID_TARGETS "${targets}")
99     if(NOT SITL)
100         setup_openocd(${exe} ${name})
101         setup_svd(${exe} ${name})
102     endif()
104     cmake_parse_arguments(args "SKIP_RELEASES" "" "" ${ARGN})
105     if(args_SKIP_RELEASES)
106         set_target_properties(${exe} ${name} PROPERTIES SKIP_RELEASES ON)
107     endif()
108 endfunction()
110 function(exclude_from_all target)
111     set_property(TARGET ${target} PROPERTY
112         TARGET_MESSAGES OFF
113         EXCLUDE_FROM_ALL 1
114         EXCLUDE_FROM_DEFAULT_BUILD 1)
115 endfunction()
117 function(collect_targets)
118     get_property(targets GLOBAL PROPERTY VALID_TARGETS)
119     list(SORT targets)
120     set(release_targets)
121     foreach(target ${targets})
122         get_target_property(skip_releases ${target} SKIP_RELEASES)
123         if(NOT skip_releases)
124             list(APPEND release_targets ${target})
125         endif()
126     endforeach()
128     list(JOIN targets " " target_names)
129     list(JOIN release_targets " " release_targets_names)
130     set_property(GLOBAL PROPERTY RELEASE_TARGETS ${release_targets})
132     set(list_target_name "targets")
133     add_custom_target(${list_target_name}
134         COMMAND ${CMAKE_COMMAND} -E echo "Valid targets: ${target_names}"
135         COMMAND ${CMAKE_COMMAND} -E echo "Release targets: ${release_targets_names}"
136     )
137     exclude_from_all(${list_target_name})
138     set(release_target_name "release")
139     add_custom_target(${release_target_name}
140         ${CMAKE_COMMAND} -E true
141         DEPENDS ${release_targets}
142     )
143     list(LENGTH targets target_count)
144     list(LENGTH release_targets release_target_count)
145     message("-- ${target_count} targets (${release_target_count} for release) found for toolchain ${TOOLCHAIN}")
146 endfunction()