1 ############################################################################ 2 # 3 # Copyright (c) 2015 PX4 Development Team. All rights reserved. 4 # 5 # Redistribution and use in source and binary forms, with or without 6 # modification, are permitted provided that the following conditions 7 # are met: 8 # 9 # 1. Redistributions of source code must retain the above copyright 10 # notice, this list of conditions and the following disclaimer. 11 # 2. Redistributions in binary form must reproduce the above copyright 12 # notice, this list of conditions and the following disclaimer in 13 # the documentation and/or other materials provided with the 14 # distribution. 15 # 3. Neither the name PX4 nor the names of its contributors may be 16 # used to endorse or promote products derived from this software 17 # without specific prior written permission. 18 # 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 24 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 25 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 26 # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 27 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 # POSSIBILITY OF SUCH DAMAGE. 31 # 32 ############################################################################ 33 34 #============================================================================= 35 # 36 # Defined functions in this file 37 # 38 # OS Specific Functions 39 # 40 # * px4_posix_add_firmware 41 # * px4_posix_generate_builtin_commands 42 # * px4_posix_add_export 43 # * px4_posix_generate_romfs 44 # 45 # Required OS Interface Functions 46 # 47 # * px4_os_add_flags 48 # * px4_os_prebuild_targets 49 # 50 51 include(common/px4_base) 52 list(APPEND CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/cmake/posix) 53 54 #============================================================================= 55 # 56 # px4_posix_generate_builtin_commands 57 # 58 # This function generates the builtin_commands.c src for posix 59 # 60 # Usage: 61 # px4_posix_generate_builtin_commands( 62 # MODULE_LIST <in-list> 63 # OUT <file>) 64 # 65 # Input: 66 # MODULE_LIST : list of modules 67 # 68 # Output: 69 # OUT : stem of generated apps.cpp/apps.h ("apps") 70 # 71 # Example: 72 # px4_posix_generate_builtin_commands( 73 # OUT <generated-src> MODULE_LIST px4_simple_app) 74 # 75 function(px4_posix_generate_builtin_commands) 76 px4_parse_function_args( 77 NAME px4_posix_generate_builtin_commands 78 ONE_VALUE OUT 79 MULTI_VALUE MODULE_LIST 80 REQUIRED MODULE_LIST OUT 81 ARGN ${ARGN}) 82 83 set(builtin_apps_string) 84 set(builtin_apps_decl_string) 85 set(command_count 0) 86 foreach(module ${MODULE_LIST}) 87 # default 88 set(MAIN_DEFAULT MAIN-NOTFOUND) 89 set(STACK_DEFAULT 1024) 90 set(PRIORITY_DEFAULT SCHED_PRIORITY_DEFAULT) 91 foreach(property MAIN STACK PRIORITY) 92 get_target_property(${property} ${module} ${property}) 93 if(NOT ${property}) 94 set(${property} ${${property}_DEFAULT}) 95 endif() 96 endforeach() 97 if (MAIN) 98 set(builtin_apps_string 99 "${builtin_apps_string} apps["${MAIN}"] = ${MAIN}_main; ") 100 set(builtin_apps_decl_string 101 "${builtin_apps_decl_string}int ${MAIN}_main(int argc, char *argv[]); ") 102 math(EXPR command_count "${command_count}+1") 103 endif() 104 endforeach() 105 configure_file(${PX4_SOURCE_DIR}/src/platforms/apps.cpp.in ${OUT}.cpp) 106 configure_file(${PX4_SOURCE_DIR}/src/platforms/apps.h.in ${OUT}.h) 107 endfunction() 108 109 #============================================================================= 110 # 111 # px4_os_add_flags 112 # 113 # Set the posix build flags. 114 # 115 # Usage: 116 # px4_os_add_flags( 117 # C_FLAGS <inout-variable> 118 # CXX_FLAGS <inout-variable> 119 # OPTIMIZATION_FLAGS <inout-variable> 120 # EXE_LINKER_FLAGS <inout-variable> 121 # INCLUDE_DIRS <inout-variable> 122 # LINK_DIRS <inout-variable> 123 # DEFINITIONS <inout-variable>) 124 # 125 # Input: 126 # BOARD : flags depend on board/posix config 127 # 128 # Input/Output: (appends to existing variable) 129 # C_FLAGS : c compile flags variable 130 # CXX_FLAGS : c++ compile flags variable 131 # OPTIMIZATION_FLAGS : optimization compile flags variable 132 # EXE_LINKER_FLAGS : executable linker flags variable 133 # INCLUDE_DIRS : include directories 134 # LINK_DIRS : link directories 135 # DEFINITIONS : definitions 136 # 137 # Note that EXE_LINKER_FLAGS is not suitable for adding libraries because 138 # these flags are added before any of the object files and static libraries. 139 # Add libraries in src/firmware/posix/CMakeLists.txt. 140 # 141 # Example: 142 # px4_os_add_flags( 143 # C_FLAGS CMAKE_C_FLAGS 144 # CXX_FLAGS CMAKE_CXX_FLAGS 145 # OPTIMIZATION_FLAGS optimization_flags 146 # EXE_LINKER_FLAG CMAKE_EXE_LINKER_FLAGS 147 # INCLUDES <list>) 148 # 149 function(px4_os_add_flags) 150 151 set(inout_vars 152 C_FLAGS CXX_FLAGS OPTIMIZATION_FLAGS EXE_LINKER_FLAGS INCLUDE_DIRS LINK_DIRS DEFINITIONS) 153 154 px4_parse_function_args( 155 NAME px4_os_add_flags 156 ONE_VALUE ${inout_vars} BOARD 157 REQUIRED ${inout_vars} BOARD 158 ARGN ${ARGN}) 159 160 px4_add_common_flags( 161 BOARD ${BOARD} 162 C_FLAGS ${C_FLAGS} 163 CXX_FLAGS ${CXX_FLAGS} 164 OPTIMIZATION_FLAGS ${OPTIMIZATION_FLAGS} 165 EXE_LINKER_FLAGS ${EXE_LINKER_FLAGS} 166 INCLUDE_DIRS ${INCLUDE_DIRS} 167 LINK_DIRS ${LINK_DIRS} 168 DEFINITIONS ${DEFINITIONS}) 169 170 set(added_include_dirs 171 src/platforms/posix/include 172 ) 173 174 # This block sets added_definitions and added_cxx_flags. 175 if(UNIX AND APPLE) 176 set(added_definitions 177 -D__PX4_POSIX 178 -D__PX4_DARWIN 179 -D__DF_DARWIN 180 -Dnoreturn_function=__attribute__((noreturn)) 181 ) 182 183 set(added_cxx_flags) 184 185 if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0) 186 message(FATAL_ERROR "PX4 Firmware requires XCode 8 or newer on Mac OS. Version installed on this system: ${CMAKE_CXX_COMPILER_VERSION}") 187 endif() 188 189 EXEC_PROGRAM(uname ARGS -v OUTPUT_VARIABLE DARWIN_VERSION) 190 STRING(REGEX MATCH "[0-9]+" DARWIN_VERSION ${DARWIN_VERSION}) 191 # message(STATUS "PX4 Darwin Version: ${DARWIN_VERSION}") 192 if (DARWIN_VERSION LESS 16) 193 list(APPEND added_definitions 194 -DCLOCK_MONOTONIC=1 195 -DCLOCK_REALTIME=0 196 -D__PX4_APPLE_LEGACY 197 ) 198 endif() 199 200 else() 201 202 set(added_definitions 203 -D__PX4_POSIX 204 -D__PX4_LINUX 205 -D__DF_LINUX 206 -Dnoreturn_function=__attribute__((noreturn)) 207 ) 208 209 # Use -pthread For linux/g++. 210 set(added_cxx_flags 211 -pthread 212 ) 213 214 endif() 215 216 set(added_exe_linker_flags) 217 218 # This block sets added_c_flags (appends to others). 219 if ("${BOARD}" STREQUAL "eagle") 220 221 if ("$ENV{HEXAGON_ARM_SYSROOT}" STREQUAL "") 222 message(FATAL_ERROR "HEXAGON_ARM_SYSROOT not set") 223 else() 224 set(HEXAGON_ARM_SYSROOT $ENV{HEXAGON_ARM_SYSROOT}) 225 endif() 226 227 # Add the toolchain specific flags 228 set(added_cflags ${POSIX_CMAKE_C_FLAGS} --sysroot=${HEXAGON_ARM_SYSROOT}) 229 230 list(APPEND added_cxx_flags 231 ${POSIX_CMAKE_CXX_FLAGS} 232 --sysroot=${HEXAGON_ARM_SYSROOT} 233 ) 234 235 list(APPEND added_exe_linker_flags 236 -Wl,-rpath-link,${HEXAGON_ARM_SYSROOT}/usr/lib 237 -Wl,-rpath-link,${HEXAGON_ARM_SYSROOT}/lib 238 --sysroot=${HEXAGON_ARM_SYSROOT} 239 ) 240 # This block sets added_c_flags (appends to others). 241 elseif ("${BOARD}" STREQUAL "excelsior") 242 243 if ("$ENV{HEXAGON_ARM_SYSROOT}" STREQUAL "") 244 message(FATAL_ERROR "HEXAGON_ARM_SYSROOT not set") 245 else() 246 set(HEXAGON_ARM_SYSROOT $ENV{HEXAGON_ARM_SYSROOT}) 247 endif() 248 249 # Add the toolchain specific flags 250 251 set(added_cflags ${POSIX_CMAKE_C_FLAGS} --sysroot=${HEXAGON_ARM_SYSROOT}/lib32-apq8096 -mfloat-abi=softfp -mfpu=neon -mthumb-interwork) 252 253 list(APPEND added_cxx_flags 254 ${POSIX_CMAKE_CXX_FLAGS} 255 --sysroot=${HEXAGON_ARM_SYSROOT}/lib32-apq8096 -mfloat-abi=softfp -mfpu=neon -mthumb-interwork 256 257 ) 258 259 list(APPEND added_exe_linker_flags 260 -Wl,-rpath-link,${HEXAGON_ARM_SYSROOT}/lib32-apq8096/usr/lib 261 -Wl,-rpath-link,${HEXAGON_ARM_SYSROOT}/lib32-apq8096/lib 262 263 --sysroot=${HEXAGON_ARM_SYSROOT}/lib32-apq8096 -mfloat-abi=softfp -mfpu=neon -mthumb-interwork 264 265 ) 266 elseif ("${BOARD}" STREQUAL "rpi") 267 SET(RPI_COMPILE_FLAGS 268 -mcpu=cortex-a53 269 -mfpu=neon 270 -mfloat-abi=hard 271 ) 272 LIST(APPEND added_c_flags ${RPI_COMPILE_FLAGS}) 273 LIST(APPEND added_cxx_flags ${RPI_COMPILE_FLAGS}) 274 275 FIND_PROGRAM(CXX_COMPILER_PATH ${CMAKE_CXX_COMPILER}) 276 277 GET_FILENAME_COMPONENT(CXX_COMPILER_PATH ${CXX_COMPILER_PATH} DIRECTORY) 278 GET_FILENAME_COMPONENT(CXX_COMPILER_PATH "${CXX_COMPILER_PATH}/../" ABSOLUTE) 279 280 IF ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") 281 set(CLANG_COMPILE_FLAGS 282 --target=arm-pc-linux-gnueabihf 283 -ccc-gcc-name arm-linux-gnueabihf-gcc 284 --sysroot=${CXX_COMPILER_PATH}/arm-linux-gnueabihf/libc 285 -I${CXX_COMPILER_PATH}/arm-linux-gnueabihf/libc/usr/include/ 286 ) 287 288 set(added_c_flags ${POSIX_CMAKE_C_FLAGS} ${CLANG_COMPILE_FLAGS}) 289 list(APPEND added_cxx_flags ${POSIX_CMAKE_CXX_FLAGS} ${CLANG_COMPILE_FLAGS}) 290 list(APPEND added_exe_linker_flags ${POSIX_CMAKE_EXE_LINKER_FLAGS} ${CLANG_COMPILE_FLAGS} 291 -B${CXX_COMPILER_PATH}/arm-linux-gnueabihf/libc/usr/lib 292 -L${CXX_COMPILER_PATH}/arm-linux-gnueabihf/libc/usr/lib 293 ) 294 ENDIF() 295 endif() 296 297 # output 298 foreach(var ${inout_vars}) 299 string(TOLOWER ${var} lower_var) 300 #message(STATUS "posix: set(${${var}} ${${${var}}} ${added_${lower_var}} PARENT_SCOPE)") 301 set(${${var}} ${${${var}}} ${added_${lower_var}} PARENT_SCOPE) 302 endforeach() 303 304 endfunction() 305 306 #============================================================================= 307 # 308 # px4_os_prebuild_targets 309 # 310 # This function generates os dependent targets 311 # 312 # Usage: 313 # px4_os_prebuild_targets( 314 # OUT <out-list_of_targets> 315 # BOARD <in-string> 316 # ) 317 # 318 # Input: 319 # BOARD : board 320 # THREADS : number of threads for building 321 # 322 # Output: 323 # OUT : the target list 324 # 325 # Example: 326 # px4_os_prebuild_targets(OUT target_list BOARD px4fmu-v2) 327 # 328 function(px4_os_prebuild_targets) 329 px4_parse_function_args( 330 NAME px4_os_prebuild_targets 331 ONE_VALUE OUT BOARD THREADS 332 REQUIRED OUT BOARD 333 ARGN ${ARGN}) 334 add_custom_target(${OUT}) 335 endfunction()