Simplify and fix altitude processing for BF43COMPAT
[inav.git] / cmake / main.cmake
blob0367e797bd9742d76580cc7c3dc26544cfd42c1c
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}"
12 set(MAIN_COMPILE_OPTIONS
13     -Wall
14     -Wextra
15     -Wunsafe-loop-optimizations
16     -Wdouble-promotion
17     -Wstrict-prototypes
18     -Werror=switch
21 macro(main_sources var) # list-var src-1...src-n
22     set(${var} ${ARGN})
23     list(TRANSFORM ${var} PREPEND "${MAIN_SRC_DIR}/")
24 endmacro()
26 function(exclude_basenames var excludes)
27     set(filtered "")
28     foreach(item ${${var}})
29         get_filename_component(basename ${item} NAME)
30         if (NOT ${basename} IN_LIST excludes)
31             list(APPEND filtered ${item})
32         endif()
33     endforeach()
34     set(${var} ${filtered} PARENT_SCOPE)
35 endfunction()
37 function(glob_except var pattern excludes)
38     file(GLOB results ${pattern})
39     list(LENGTH results count)
40     if(count EQUAL 0)
41         message(FATAL_ERROR "glob with pattern '${pattern}' returned no results")
42     endif()
43     exclude_basenames(results "${excludes}")
44     set(${var} ${results} PARENT_SCOPE)
45 endfunction()
47 function(get_generated_files_dir output target_name)
48     set(${output} ${CMAKE_CURRENT_BINARY_DIR}/${target_name} PARENT_SCOPE)
49 endfunction()
51 function(setup_executable exe name)
52     get_generated_files_dir(generated_dir ${name})
53     target_compile_options(${exe} PRIVATE ${MAIN_COMPILE_OPTIONS})
54     target_include_directories(${exe} PRIVATE ${generated_dir} ${MAIN_INCLUDE_DIRS})
55     target_compile_definitions(${exe} PRIVATE ${MAIN_DEFINITIONS} __TARGET__="${name}" ${name})
56     # XXX: Don't make SETTINGS_GENERATED_C part of the build,
57     # since it's compiled via #include in settings.c. This will
58     # change once we move off PGs
59     target_sources(${exe} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/${name}/${SETTINGS_GENERATED_H}")
60     set_target_properties(${exe} PROPERTIES
61         RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
62     )
63     if(IS_RELEASE_BUILD)
64         set_target_properties(${exe} PROPERTIES
65             INTERPROCEDURAL_OPTIMIZATION ON
66         )
67     endif()
68 endfunction()
70 function(setup_firmware_target exe name)
71     setup_executable(${exe} ${name})
72     enable_settings(${exe} ${name})
73     get_property(targets GLOBAL PROPERTY VALID_TARGETS)
74     list(APPEND targets ${name})
75     set_property(GLOBAL PROPERTY VALID_TARGETS "${targets}")
76     setup_openocd(${exe} ${name})
77     setup_svd(${exe} ${name})
79     cmake_parse_arguments(args "SKIP_RELEASES" "" "" ${ARGN})
80     if(args_SKIP_RELEASES)
81         set_target_properties(${exe} ${name} PROPERTIES SKIP_RELEASES ON)
82     endif()
83 endfunction()
85 function(exclude_from_all target)
86     set_property(TARGET ${target} PROPERTY
87         TARGET_MESSAGES OFF
88         EXCLUDE_FROM_ALL 1
89         EXCLUDE_FROM_DEFAULT_BUILD 1)
90 endfunction()
92 function(collect_targets)
93     get_property(targets GLOBAL PROPERTY VALID_TARGETS)
94     list(SORT targets)
95     set(release_targets)
96     foreach(target ${targets})
97         get_target_property(skip_releases ${target} SKIP_RELEASES)
98         if(NOT skip_releases)
99             list(APPEND release_targets ${target})
100         endif()
101     endforeach()
103     list(JOIN targets " " target_names)
104     list(JOIN release_targets " " release_targets_names)
105     set_property(GLOBAL PROPERTY RELEASE_TARGETS ${release_targets})
107     set(list_target_name "targets")
108     add_custom_target(${list_target_name}
109         COMMAND ${CMAKE_COMMAND} -E echo "Valid targets: ${target_names}"
110         COMMAND ${CMAKE_COMMAND} -E echo "Release targets: ${release_targets_names}"
111     )
112     exclude_from_all(${list_target_name})
113     set(release_target_name "release")
114     add_custom_target(${release_target_name}
115         ${CMAKE_COMMAND} -E true
116         DEPENDS ${release_targets}
117     )
118     list(LENGTH targets target_count)
119     list(LENGTH release_targets release_target_count)
120     message("-- ${target_count} targets (${release_target_count} for release) found for toolchain ${TOOLCHAIN}")
121 endfunction()