Merge pull request #10092 from fschuindt/patch-1
[inav.git] / cmake / sitl.cmake
blobee43aa9a93a30199a893bee80989e3791ad08e5c
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     string(TOLOWER ${PROJECT_NAME} lowercase_project_name)
103     set(binary_name ${lowercase_project_name}_${FIRMWARE_VERSION}_${name})
104     if(DEFINED BUILD_SUFFIX AND NOT "" STREQUAL "${BUILD_SUFFIX}")
105         set(binary_name "${binary_name}_${BUILD_SUFFIX}")
106     endif()
108     list(APPEND target_definitions ${SITL_DEFINITIONS})
109     set(exe_target ${name}.elf)
110     add_executable(${exe_target})
111     target_sources(${exe_target} PRIVATE ${target_sources} ${COMMON_SRC})
112     target_include_directories(${exe_target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
113     target_compile_definitions(${exe_target} PRIVATE ${target_definitions})
116     if(WARNINGS_AS_ERRORS)
117         target_compile_options(${exe_target} PRIVATE -Werror)
118     endif()
120     target_compile_options(${exe_target} PRIVATE ${SITL_COMPILE_OPTIONS})
122     target_link_libraries(${exe_target} PRIVATE ${SITL_LINK_LIBRARIS})
123     target_link_options(${exe_target} PRIVATE ${SITL_LINK_OPTIONS})
125     set(script_path ${MAIN_SRC_DIR}/target/link/sitl.ld)
126     if(NOT EXISTS ${script_path})
127         message(FATAL_ERROR "linker script ${script_path} doesn't exist")
128     endif()
129     set_target_properties(${exe_target} PROPERTIES LINK_DEPENDS ${script_path})
130     if(NOT MACOSX)
131         target_link_options(${exe_target} PRIVATE -T${script_path})
132     endif()
134     if(${CYGWIN})
135         set(exe_filename ${CMAKE_BINARY_DIR}/${binary_name}.exe)
136     else()
137         set(exe_filename ${CMAKE_BINARY_DIR}/${binary_name})
138     endif()
140     add_custom_target(${name} ALL
141         cmake -E copy $<TARGET_FILE:${exe_target}> ${exe_filename}
142     )
144     setup_firmware_target(${exe_target} ${name} ${ARGN})
145     #clean_<target>
146     set(generator_cmd "")
147     if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
148         set(generator_cmd "make")
149     elseif(CMAKE_GENERATOR STREQUAL "Ninja")
150         set(generator_cmd "ninja")
151     endif()
152     if (NOT generator_cmd STREQUAL "")
153         set(clean_target "clean_${name}")
154         add_custom_target(${clean_target}
155             WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
156             COMMAND ${generator_cmd} clean
157             COMMENT "Removing intermediate files for ${name}")
158         set_property(TARGET ${clean_target} PROPERTY
159             EXCLUDE_FROM_ALL 1
160             EXCLUDE_FROM_DEFAULT_BUILD 1)
161     endif()
162 endfunction()