Merge branch 'release_6.1.0' into SITL
[inav.git] / cmake / sitl.cmake
blobe66c7a9e5df891b077136c9276483cd6e14124e6
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
28 set(SITL_LINK_OPTIONS
29     -lrt
30     -Wl,-L${STM32_LINKER_DIR}
31     -Wl,--cref
32     -static-libgcc # Required for windows build under cygwin
35 set(SITL_LINK_LIBRARIS
36     -lpthread
37     -lm
38     -lc
41 set(SITL_COMPILE_OPTIONS
42     -Wno-format #Fixme: Compile for 32bit, but settings.rb has to be adjusted
43     -Wno-return-local-addr
44     -fsingle-precision-constant
45     -funsigned-char
48 set(SITL_DEFINITIONS
49     SITL_BUILD
52 function(generate_map_file target)
53     if(CMAKE_VERSION VERSION_LESS 3.15)
54         set(map "$<TARGET_FILE:${target}>.map")
55     else()
56         set(map "$<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.map")
57     endif()
58     target_link_options(${target} PRIVATE "-Wl,-gc-sections,-Map,${map}")
59 endfunction()
61 function (target_sitl name)
63     if(NOT host STREQUAL TOOLCHAIN)
64         return()
65     endif()
66     
67     exclude(COMMON_SRC "${SITL_COMMON_SRC_EXCLUDES}")
69     set(target_sources)
70     list(APPEND target_sources ${SITL_SRC})
71     file(GLOB target_c_sources "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
72     file(GLOB target_h_sources "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
73     list(APPEND target_sources ${target_c_sources} ${target_h_sources})
74     
75     set(target_definitions ${COMMON_COMPILE_DEFINITIONS})
76     
77     set(hse_mhz ${STM32_DEFAULT_HSE_MHZ})
78     math(EXPR hse_value "${hse_mhz} * 1000000")
79     list(APPEND target_definitions "HSE_VALUE=${hse_value}")
81     string(TOLOWER ${PROJECT_NAME} lowercase_project_name)
82     set(binary_name ${lowercase_project_name}_${FIRMWARE_VERSION}_${name})
83     if(DEFINED BUILD_SUFFIX AND NOT "" STREQUAL "${BUILD_SUFFIX}")
84         set(binary_name "${binary_name}_${BUILD_SUFFIX}")
85     endif()
87     list(APPEND target_definitions ${SITL_DEFINITIONS})
88     set(exe_target ${name}.elf)
89     add_executable(${exe_target})
90     target_sources(${exe_target} PRIVATE ${target_sources} ${COMMON_SRC})
91     target_include_directories(${exe_target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
92     target_compile_definitions(${exe_target} PRIVATE ${target_definitions})
94     
95     if(WARNINGS_AS_ERRORS)
96         target_compile_options(${exe_target} PRIVATE -Werror)
97     endif()
98     
99     target_compile_options(${exe_target} PRIVATE ${SITL_COMPILE_OPTIONS})
100   
101     target_link_libraries(${exe_target} PRIVATE ${SITL_LINK_LIBRARIS})
102     target_link_options(${exe_target} PRIVATE ${SITL_LINK_OPTIONS})
103     
104     generate_map_file(${exe_target})
105     
106     set(script_path ${MAIN_SRC_DIR}/target/link/sitl.ld)
107     if(NOT EXISTS ${script_path})
108         message(FATAL_ERROR "linker script ${script_path} doesn't exist")
109     endif()
110     set_target_properties(${exe_target} PROPERTIES LINK_DEPENDS ${script_path})
111     target_link_options(${exe_target} PRIVATE -T${script_path})
113     if(${WIN32} OR ${CYGWIN})
114         set(exe_filename ${CMAKE_BINARY_DIR}/${binary_name}.exe)
115     else()
116         set(exe_filename ${CMAKE_BINARY_DIR}/${binary_name})
117     endif()
118     
119     add_custom_target(${name} ALL
120         cmake -E env PATH="$ENV{PATH}"
121         ${CMAKE_OBJCOPY} $<TARGET_FILE:${exe_target}> ${exe_filename}
122         BYPRODUCTS ${hex}
123     )
125     setup_firmware_target(${exe_target} ${name} ${ARGN})
126     #clean_<target>
127     set(generator_cmd "")
128     if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
129         set(generator_cmd "make")
130     elseif(CMAKE_GENERATOR STREQUAL "Ninja")
131         set(generator_cmd "ninja")
132     endif()
133     if (NOT generator_cmd STREQUAL "")
134         set(clean_target "clean_${name}")
135         add_custom_target(${clean_target}
136             WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
137             COMMAND ${generator_cmd} clean
138             COMMENT "Removing intermediate files for ${name}")
139         set_property(TARGET ${clean_target} PROPERTY
140             EXCLUDE_FROM_ALL 1
141             EXCLUDE_FROM_DEFAULT_BUILD 1)
142     endif()
143 endfunction()