Update dev-builds.yml
[inav.git] / cmake / sitl.cmake
blob39e6456830ac15ac1c12ca1e350247b691cd0bb4
2 main_sources(SITL_COMMON_SRC_EXCLUDES
3     build/atomic.h
4     drivers/system.c
5     drivers/time.c
6     drivers/timer.c
7     drivers/rcc.c
8     drivers/persistent.c
9     drivers/accgyro/accgyro_mpu.c
10     drivers/display_ug2864hsweg01.c
11     io/displayport_oled.c
14 main_sources(SITL_SRC
15     config/config_streamer_file.c
16     drivers/serial_tcp.c
17     drivers/serial_tcp.h
18     target/SITL/sim/realFlight.c
19     target/SITL/sim/realFlight.h
20     target/SITL/sim/simHelper.c
21     target/SITL/sim/simHelper.h
22     target/SITL/sim/simple_soap_client.c
23     target/SITL/sim/simple_soap_client.h
24     target/SITL/sim/xplane.c
25     target/SITL/sim/xplane.h
29 if(CMAKE_HOST_APPLE)
30   set(MACOSX ON)
31 endif()
33 set(SITL_LINK_OPTIONS
34     -Wl,-L${STM32_LINKER_DIR}
37 if(${CYGWIN})
38     set(SITL_LINK_OPTIONS ${SITL_LINK_OPTIONS} "-static-libgcc")
39 endif()
41 set(SITL_LINK_LIBRARIS
42     -lpthread
43     -lm
44     -lc
47 if(NOT MACOSX)
48     set(SITL_LINK_LIBRARIS ${SITL_LINK_LIBRARIS} -lrt)
49 endif()
51 set(SITL_COMPILE_OPTIONS
52     -Wno-format #Fixme: Compile for 32bit, but settings.rb has to be adjusted
53     -funsigned-char
56 if(DEBUG)
57     message(STATUS "Debug mode enabled. Adding -g to SITL_COMPILE_OPTIONS.")
58     list(APPEND SITL_COMPILE_OPTIONS -g)
59 endif()
61 if(NOT MACOSX)
62     set(SITL_COMPILE_OPTIONS ${SITL_COMPILE_OPTIONS}
63         -Wno-return-local-addr
64         -Wno-error=maybe-uninitialized
65         -fsingle-precision-constant
66     )
67     if (CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 12.0)
68         set(SITL_LINK_OPTIONS ${SITL_LINK_OPTIONS} "-Wl,--no-warn-rwx-segments")
69     endif()
70 else()
71     set(SITL_COMPILE_OPTIONS ${SITL_COMPILE_OPTIONS}
72     )
73 endif()
75 set(SITL_DEFINITIONS
76     SITL_BUILD
79 function (target_sitl name)
80     if(CMAKE_VERSION VERSION_GREATER 3.22)
81         set(CMAKE_C_STANDARD 17)
82     endif()
84     if(NOT host STREQUAL TOOLCHAIN)
85         return()
86     endif()
88     exclude(COMMON_SRC "${SITL_COMMON_SRC_EXCLUDES}")
90     set(target_sources)
91     list(APPEND target_sources ${SITL_SRC})
92     file(GLOB target_c_sources "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
93     file(GLOB target_h_sources "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
94     list(APPEND target_sources ${target_c_sources} ${target_h_sources})
96     set(target_definitions ${COMMON_COMPILE_DEFINITIONS})
98     set(hse_mhz ${STM32_DEFAULT_HSE_MHZ})
99     math(EXPR hse_value "${hse_mhz} * 1000000")
100     list(APPEND target_definitions "HSE_VALUE=${hse_value}")
102     if (MSP_UART) 
103         list(APPEND target_definitions "MSP_UART=${MSP_UART}")
104     endif()
106     string(TOLOWER ${PROJECT_NAME} lowercase_project_name)
107     set(binary_name ${lowercase_project_name}_${FIRMWARE_VERSION}_${name})
108     if(DEFINED BUILD_SUFFIX AND NOT "" STREQUAL "${BUILD_SUFFIX}")
109         set(binary_name "${binary_name}_${BUILD_SUFFIX}")
110     endif()
112     list(APPEND target_definitions ${SITL_DEFINITIONS})
113     set(exe_target ${name}.elf)
114     add_executable(${exe_target})
115     target_sources(${exe_target} PRIVATE ${target_sources} ${COMMON_SRC})
116     target_include_directories(${exe_target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
117     target_compile_definitions(${exe_target} PRIVATE ${target_definitions})
120     if(WARNINGS_AS_ERRORS)
121         target_compile_options(${exe_target} PRIVATE -Werror)
122     endif()
124     target_compile_options(${exe_target} PRIVATE ${SITL_COMPILE_OPTIONS})
126     target_link_libraries(${exe_target} PRIVATE ${SITL_LINK_LIBRARIS})
127     target_link_options(${exe_target} PRIVATE ${SITL_LINK_OPTIONS})
129     set(script_path ${MAIN_SRC_DIR}/target/link/sitl.ld)
130     if(NOT EXISTS ${script_path})
131         message(FATAL_ERROR "linker script ${script_path} doesn't exist")
132     endif()
133     set_target_properties(${exe_target} PROPERTIES LINK_DEPENDS ${script_path})
134     if(NOT MACOSX)
135         target_link_options(${exe_target} PRIVATE -T${script_path})
136     endif()
138     if(${CYGWIN})
139         set(exe_filename ${CMAKE_BINARY_DIR}/${binary_name}.exe)
140     else()
141         set(exe_filename ${CMAKE_BINARY_DIR}/${binary_name})
142     endif()
144     add_custom_target(${name} ALL
145         cmake -E copy $<TARGET_FILE:${exe_target}> ${exe_filename}
146     )
148     setup_firmware_target(${exe_target} ${name} ${ARGN})
149     #clean_<target>
150     set(generator_cmd "")
151     if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
152         set(generator_cmd "make")
153     elseif(CMAKE_GENERATOR STREQUAL "Ninja")
154         set(generator_cmd "ninja")
155     endif()
156     if (NOT generator_cmd STREQUAL "")
157         set(clean_target "clean_${name}")
158         add_custom_target(${clean_target}
159             WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
160             COMMAND ${generator_cmd} clean
161             COMMENT "Removing intermediate files for ${name}")
162         set_property(TARGET ${clean_target} PROPERTY
163             EXCLUDE_FROM_ALL 1
164             EXCLUDE_FROM_DEFAULT_BUILD 1)
165     endif()
166 endfunction()