|
| 1 | +# Append current directory to CMAKE_MODULE_PATH for making device specific cmake modules visible |
| 2 | +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}) |
| 3 | + |
| 4 | +# Target definition |
| 5 | +set(CMAKE_SYSTEM_NAME Generic) |
| 6 | +set(CMAKE_SYSTEM_PROCESSOR arm) |
| 7 | + |
| 8 | +#--------------------------------------------------------------------------------------- |
| 9 | +# Set toolchain paths |
| 10 | +#--------------------------------------------------------------------------------------- |
| 11 | +set(TOOLCHAIN arm-none-eabi) |
| 12 | + |
| 13 | +if(NOT DEFINED TOOLCHAIN_PREFIX) |
| 14 | + if(CMAKE_HOST_SYSTEM_NAME STREQUAL Linux) |
| 15 | + set(TOOLCHAIN_PREFIX "/usr") |
| 16 | + elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin) |
| 17 | + set(TOOLCHAIN_PREFIX "/usr/local") |
| 18 | + elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows) |
| 19 | + if($ENV{MINGW_PREFIX} MATCHES "^.*/(mingw|clang)64$") |
| 20 | + set(TOOLCHAIN_PREFIX $ENV{MINGW_PREFIX}) |
| 21 | + # STRING(REGEX REPLACE "^.*/(mingw|clang)64$" "/\\164" TOOLCHAIN_PREFIX $ENV{MINGW_PREFIX}) |
| 22 | + else() |
| 23 | + message(STATUS "Please specify the TOOLCHAIN_PREFIX !\n For example: -DTOOLCHAIN_PREFIX=\"C:/Program Files/GNU Tools ARM Embedded\" ") |
| 24 | + endif() |
| 25 | + else() |
| 26 | + set(TOOLCHAIN_PREFIX "/usr") |
| 27 | + message(STATUS "No TOOLCHAIN_PREFIX specified, using default: " ${TOOLCHAIN_PREFIX}) |
| 28 | + endif() |
| 29 | +endif() |
| 30 | +set(TOOLCHAIN_BIN_DIR ${TOOLCHAIN_PREFIX}/bin) |
| 31 | +set(TOOLCHAIN_INC_DIR ${TOOLCHAIN_PREFIX}/${TOOLCHAIN}/include) |
| 32 | +set(TOOLCHAIN_LIB_DIR ${TOOLCHAIN_PREFIX}/${TOOLCHAIN}/lib) |
| 33 | + |
| 34 | +# Set system depended extensions |
| 35 | +if(WIN32) |
| 36 | + set(TOOLCHAIN_EXT ".exe" ) |
| 37 | +else() |
| 38 | + set(TOOLCHAIN_EXT "" ) |
| 39 | +endif() |
| 40 | + |
| 41 | +# Perform compiler test with static library |
| 42 | +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) |
| 43 | + |
| 44 | +#--------------------------------------------------------------------------------------- |
| 45 | +# Set compiler/linker flags |
| 46 | +#--------------------------------------------------------------------------------------- |
| 47 | + |
| 48 | +# Object build options |
| 49 | +# -O0 No optimizations, reduce compilation time and make debugging produce the expected results. |
| 50 | +# -mthumb Generat thumb instructions. |
| 51 | +# -fno-builtin Do not use built-in functions provided by GCC. |
| 52 | +# -Wall Print only standard warnings, for all use Wextra |
| 53 | +# -ffunction-sections Place each function item into its own section in the output file. |
| 54 | +# -fdata-sections Place each data item into its own section in the output file. |
| 55 | +# -fomit-frame-pointer Omit the frame pointer in functions that don’t need one. |
| 56 | +# -mabi=aapcs Defines enums to be a variable sized type. |
| 57 | +set(OBJECT_GEN_FLAGS "-mthumb -fno-builtin -Wall -ffunction-sections -fdata-sections -fomit-frame-pointer -fmessage-length=0") |
| 58 | + |
| 59 | +set(CMAKE_C_FLAGS "${OBJECT_GEN_FLAGS} -std=gnu17 " CACHE INTERNAL "C Compiler options") |
| 60 | +set(CMAKE_CXX_FLAGS "${OBJECT_GEN_FLAGS} -std=gnu++17 " CACHE INTERNAL "C++ Compiler options") |
| 61 | +set(CMAKE_ASM_FLAGS "${OBJECT_GEN_FLAGS} -x assembler-with-cpp " CACHE INTERNAL "ASM Compiler options") |
| 62 | + |
| 63 | + |
| 64 | +# -Wl,--gc-sections Perform the dead code elimination. |
| 65 | +# --specs=nano.specs Link with newlib-nano. |
| 66 | +# --specs=nosys.specs No syscalls, provide empty implementations for the POSIX system calls. |
| 67 | +# set(MAPFILE "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_PROJECT_NAME}.map" CACHE INTERNAL "Linker Map File") |
| 68 | +set(CMAKE_EXE_LINKER_FLAGS "-Wl,--gc-sections,--cref --specs=nano.specs --specs=nosys.specs -mthumb -nostartfiles -static" CACHE INTERNAL "Linker options") |
| 69 | + |
| 70 | +#--------------------------------------------------------------------------------------- |
| 71 | +# Set debug/release build configuration Options |
| 72 | +#--------------------------------------------------------------------------------------- |
| 73 | + |
| 74 | +# Options for DEBUG build |
| 75 | +# -Og Enables optimizations that do not interfere with debugging. |
| 76 | +# -g Produce debugging information in the operating system’s native format. |
| 77 | +set(CMAKE_C_FLAGS_DEBUG "-Og -g3" CACHE INTERNAL "C Compiler options for debug build type") |
| 78 | +set(CMAKE_CXX_FLAGS_DEBUG "-Og -g3" CACHE INTERNAL "C++ Compiler options for debug build type") |
| 79 | +set(CMAKE_ASM_FLAGS_DEBUG "-g3" CACHE INTERNAL "ASM Compiler options for debug build type") |
| 80 | +set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g3" CACHE INTERNAL "Linker options for debug build type") |
| 81 | + |
| 82 | +# Options for SPY build are identical to DEBUG build |
| 83 | +set(CMAKE_C_FLAGS_SPY "${CMAKE_C_FLAGS_DEBUG}" CACHE INTERNAL "C Compiler options for spy build type") |
| 84 | +set(CMAKE_CXX_FLAGS_SPY "${CMAKE_CXX_FLAGS_DEBUG}" CACHE INTERNAL "C++ Compiler options for spy build type") |
| 85 | +set(CMAKE_ASM_FLAGS_SPY "${CMAKE_ASM_FLAGS_DEBUG}" CACHE INTERNAL "ASM Compiler options for spy build type") |
| 86 | +set(CMAKE_EXE_LINKER_FLAGS_SPY "${CMAKE_EXE_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Linker options for spy build type") |
| 87 | + |
| 88 | +# Options for RELEASE build |
| 89 | +# -Os Optimize for size. -Os enables all -O2 optimizations. |
| 90 | +# -flto Runs the standard link-time optimizer. |
| 91 | +set(CMAKE_C_FLAGS_RELEASE "-Os -g0" CACHE INTERNAL "C Compiler options for release build type") |
| 92 | +set(CMAKE_CXX_FLAGS_RELEASE "-Os -g0" CACHE INTERNAL "C++ Compiler options for release build type") |
| 93 | +set(CMAKE_ASM_FLAGS_RELEASE "" CACHE INTERNAL "ASM Compiler options for release build type") |
| 94 | +set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-g0" CACHE INTERNAL "Linker options for release build type") |
| 95 | +# set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-g3 -flto" CACHE INTERNAL "Linker options for release build type") |
| 96 | + |
| 97 | +#--------------------------------------------------------------------------------------- |
| 98 | +# Set compilers |
| 99 | +#--------------------------------------------------------------------------------------- |
| 100 | +set(CMAKE_C_COMPILER ${TOOLCHAIN_BIN_DIR}/${TOOLCHAIN}-gcc${TOOLCHAIN_EXT} CACHE INTERNAL "C Compiler") |
| 101 | +set(CMAKE_CXX_COMPILER ${TOOLCHAIN_BIN_DIR}/${TOOLCHAIN}-g++${TOOLCHAIN_EXT} CACHE INTERNAL "C++ Compiler") |
| 102 | +set(CMAKE_ASM_COMPILER ${TOOLCHAIN_BIN_DIR}/${TOOLCHAIN}-gcc${TOOLCHAIN_EXT} CACHE INTERNAL "ASM Compiler") |
| 103 | + |
| 104 | +set(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN_PREFIX}/${${TOOLCHAIN}} ${CMAKE_PREFIX_PATH}) |
| 105 | +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) |
| 106 | +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH) |
| 107 | +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH) |
| 108 | +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) |
0 commit comments