Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2017-2018, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | #This file contains settings to specify how GNUARM shall be used |
| 9 | |
| 10 | function(check_gnuarm_input_vars MY_VERSION) |
| 11 | #Specify where gnuarm is |
| 12 | if (NOT DEFINED GNUARM_PATH) |
| 13 | message(FATAL_ERROR "Please set GNUARM_PATH to the root directory of the gnuarm installation. e.g. set(GNUARM_PATH \"C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q1-update/\"") |
| 14 | endif() |
| 15 | |
| 16 | STRING(REGEX REPLACE "([0-9]+).([0-9]+).*" "\\1.\\2" _MY_MAJOR_MINOR "${MY_VERSION}") |
| 17 | STRING(REGEX REPLACE "([0-9]+).([0-9]+).*" "\\1.\\2" _GNUARM_MAJOR_MINOR "${GNUARM_VER}") |
| 18 | |
| 19 | #Check gnuarm version. |
| 20 | if (NOT "${_MY_MAJOR_MINOR}" VERSION_EQUAL "${_GNUARM_MAJOR_MINOR}") |
| 21 | message(FATAL_ERROR "GNUARM version (GNUARM_VER=${GNUARM_VER}) does not match ${MY_VERSION}") |
| 22 | endif() |
| 23 | |
| 24 | if (NOT DEFINED ARM_CPU_ARHITECTURE AND NOT DEFINED ARM_CPU_TYPE) |
| 25 | message(FATAL_ERROR "ARM_CPU_TYPE and ARM_CPU_ARHITECTURE is not defined! Please include the CPU specific config file before this one.") |
| 26 | endif() |
| 27 | |
| 28 | endfunction() |
| 29 | |
| 30 | message(STATUS "Using gcc compiler package v${GNUARM_VER} from ${GNUARM_PATH}") |
| 31 | |
| 32 | |
| 33 | #Tell cmake which compiler we use |
| 34 | if (EXISTS "c:/") |
| 35 | set (CMAKE_C_COMPILER "${GNUARM_PATH}/bin/arm-none-eabi-gcc.exe") |
| 36 | set (CMAKE_CXX_COMPILER "${GNUARM_PATH}/bin/arm-none-eabi-g++.exe") |
| 37 | set (CMAKE_ASM_COMPILER "${GNUARM_PATH}/bin/arm-none-eabi-gcc.exe") |
| 38 | else() |
| 39 | set (CMAKE_C_COMPILER "${GNUARM_PATH}/bin/arm-none-eabi-gcc") |
| 40 | set (CMAKE_CXX_COMPILER "${GNUARM_PATH}/bin/arm-none-eabi-g++") |
| 41 | set (CMAKE_ASM_COMPILER "${GNUARM_PATH}/bin/arm-none-eabi-gcc") |
| 42 | endif() |
| 43 | |
| 44 | if("CXX" IN_LIST languages) |
| 45 | set(CMAKE_CXX_COMPILER_ID "GNUARM" CACHE INTERNAL "CXX compiler ID" FORCE) |
| 46 | include(Compiler/GNUARM-CXX) |
| 47 | endif() |
| 48 | |
| 49 | if("C" IN_LIST languages) |
| 50 | set(CMAKE_C_COMPILER_ID "GNUARM" CACHE INTERNAL "C compiler ID" FORCE) |
| 51 | include(Compiler/GNUARM-C) |
| 52 | endif() |
| 53 | |
| 54 | if("ASM" IN_LIST languages) |
| 55 | set(CMAKE_C_COMPILER_ID "GNUARM" CACHE INTERNAL "ASM compiler ID" FORCE) |
| 56 | include(Compiler/GNUARM-ASM) |
| 57 | endif() |
| 58 | |
| 59 | function(compiler_set_linkercmdfile) |
| 60 | set( _OPTIONS_ARGS ) #Option (on/off) arguments. |
| 61 | set( _ONE_VALUE_ARGS TARGET PATH) #Single option arguments. |
| 62 | set( _MULTI_VALUE_ARGS DEFINES INCLUDES) #List arguments |
| 63 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 64 | |
| 65 | #Check passed parameters |
| 66 | if(NOT _MY_PARAMS_TARGET) |
| 67 | message(FATAL_ERROR "compiler_set_linkercmdfile: mandatory parameter 'TARGET' is missing.") |
| 68 | endif() |
| 69 | if (NOT TARGET ${_MY_PARAMS_TARGET}) |
| 70 | message(FATAL_ERROR "compiler_set_linkercmdfile: value of parameter 'TARGET' is invalid.") |
| 71 | endif() |
| 72 | |
| 73 | if(NOT _MY_PARAMS_PATH) |
| 74 | message(FATAL_ERROR "compiler_set_linkercmdfile: mandatory parameter 'PATH' is missing.") |
| 75 | endif() |
| 76 | set(_FILE_PATH ${_MY_PARAMS_PATH}) |
| 77 | |
| 78 | #Compose additional command line switches from macro definitions. |
| 79 | set(_FLAGS "") |
| 80 | if (_MY_PARAMS_DEFINES) |
| 81 | foreach(_DEFINE IN LISTS _MY_PARAMS_DEFINES) |
Gabor Kertesz | fffaafb | 2018-06-27 17:13:13 +0200 | [diff] [blame] | 82 | list(APPEND _FLAGS "-D${_DEFINE}") |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 83 | endforeach() |
| 84 | endif() |
| 85 | #Compose additional command line switches from include paths. |
| 86 | if (_MY_PARAMS_INCLUDES) |
| 87 | foreach(_INCLUDE_P IN LISTS _MY_PARAMS_INCLUDES) |
Gabor Kertesz | fffaafb | 2018-06-27 17:13:13 +0200 | [diff] [blame] | 88 | list(APPEND _FLAGS "-I${_INCLUDE_P}") |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 89 | endforeach() |
| 90 | endif() |
| 91 | |
| 92 | #Create additional target if linker script needs to be pre-processed. |
| 93 | if (_MY_PARAMS_DEFINES OR _MY_PARAMS_INCLUDES) |
| 94 | #Name of pre-processed linker script file. |
Tamas Ban | 6032c3b | 2018-07-17 22:11:20 +0100 | [diff] [blame] | 95 | set(FINAL_LD_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_MY_PARAMS_TARGET}.ld.i") |
Mate Toth-Pal | 7686726 | 2018-03-09 13:15:36 +0100 | [diff] [blame] | 96 | #Name of the target doing the pre-processing |
| 97 | set(LD_PP_TARGET_NAME "${_MY_PARAMS_TARGET}_ldpp") |
| 98 | #The target definition. |
| 99 | add_custom_target(${LD_PP_TARGET_NAME} |
| 100 | COMMENT "Pre-processing linker command file ${_MY_PARAMS_PATH}..." |
| 101 | COMMAND ${CMAKE_C_COMPILER} -E -P -xc ${_FLAGS} -o ${FINAL_LD_FILE_NAME} ${_MY_PARAMS_PATH} |
| 102 | DEPENDS ${_MY_PARAMS_PATH} |
| 103 | BYPRODUCTS ${FINAL_LD_FILE_NAME} |
| 104 | ) |
| 105 | #Make the original target depend on the new one. |
| 106 | add_dependencies(${_MY_PARAMS_TARGET} ${LD_PP_TARGET_NAME}) |
| 107 | #Tell cmake to delete the intermediate linker script when the clean rule |
| 108 | #is executed. |
| 109 | get_directory_property(_ADDITIONAL_MAKE_CLEAN_FILES DIRECTORY "./" ADDITIONAL_MAKE_CLEAN_FILES) |
| 110 | set_directory_properties(PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${_ADDITIONAL_MAKE_CLEAN_FILES} ${FINAL_LD_FILE_NAME}") |
| 111 | #Set the path to linker script point to the intermediate file. |
| 112 | set(_FILE_PATH ${FINAL_LD_FILE_NAME}) |
| 113 | endif() |
| 114 | |
| 115 | #Note: the space before the option is important! |
| 116 | set_property(TARGET ${_MY_PARAMS_TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -T${_FILE_PATH}") |
| 117 | set_property(TARGET ${_MY_PARAMS_TARGET} APPEND PROPERTY LINK_DEPENDS ${_FILE_PATH}) |
| 118 | #Tell cmake .map files shall be removed when project is cleaned (make clean) |
| 119 | get_filename_component(_TARGET_BASE_NAME ${_MY_PARAMS_TARGET} NAME_WE) |
| 120 | get_directory_property(_ADDITIONAL_MAKE_CLEAN_FILES DIRECTORY "./" ADDITIONAL_MAKE_CLEAN_FILES) |
| 121 | set_directory_properties(PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${_ADDITIONAL_MAKE_CLEAN_FILES} ${_TARGET_BASE_NAME}.map") |
| 122 | endfunction() |
| 123 | |
| 124 | function(compiler_set_cmse_output TARGET FILE_PATH) |
| 125 | #Note: the space before the option is important! |
| 126 | set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--cmse-implib,--out-implib=${FILE_PATH}") |
| 127 | #Tell cmake cmse output is a generated object file. |
| 128 | SET_SOURCE_FILES_PROPERTIES("${FILE_PATH}" PROPERTIES EXTERNAL_OBJECT true GENERATED true) |
| 129 | #Tell cmake cmse output shall be removed by clean target. |
| 130 | get_directory_property(_ADDITIONAL_MAKE_CLEAN_FILES DIRECTORY "./" ADDITIONAL_MAKE_CLEAN_FILES) |
| 131 | set_directory_properties(PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${_ADDITIONAL_MAKE_CLEAN_FILES} ${FILE_PATH}") |
| 132 | endfunction() |
| 133 | |
| 134 | function(compiler_merge_library) |
| 135 | set( _OPTIONS_ARGS ) #Option (on/off) arguments. |
| 136 | set( _ONE_VALUE_ARGS DEST) #Single option arguments. |
| 137 | set( _MULTI_VALUE_ARGS LIBS) #List arguments |
| 138 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 139 | |
| 140 | #Check passed parameters |
| 141 | if(NOT _MY_PARAMS_DEST) |
| 142 | message(FATAL_ERROR "compiler_merge_library: no destination target specified. Please see the DEST parameter.") |
| 143 | endif() |
| 144 | #Check if destination is a library |
| 145 | get_target_property(_tmp ${_MY_PARAMS_DEST} TYPE) |
| 146 | if(NOT "${_tmp}" STREQUAL "STATIC_LIBRARY") |
| 147 | message(FATAL_ERROR "compiler_merge_library: parameter DEST must be a static library target.") |
| 148 | endif() |
| 149 | set(_DEST ${_MY_PARAMS_DEST}) |
| 150 | |
| 151 | if(NOT _MY_PARAMS_LIBS) |
| 152 | message(FATAL_ERROR "compiler_merge_library: no source libraries specified. Please see the LIBS parameter.") |
| 153 | endif() |
| 154 | set(_LIBS ${_MY_PARAMS_LIBS}) |
| 155 | |
| 156 | ##Find the cmake script doing the merge. |
| 157 | find_file(_MERGE_SCRIPT "GNUArMerge.cmake" PATHS ${CMAKE_MODULE_PATH} PATH_SUFFIXES Common NO_DEFAULT_PATH) |
| 158 | |
| 159 | #Now add a custom command for each source library to our custom target to |
| 160 | #merge into the destination. |
| 161 | foreach(SRC_LIB ${_LIBS}) |
| 162 | get_filename_component(_SRC_LIB_NAME "${SRC_LIB}" NAME) |
| 163 | add_custom_command(TARGET ${_DEST} POST_BUILD |
| 164 | COMMAND ${CMAKE_COMMAND} -DCMAKE_AR=${CMAKE_AR} -DSOURCE=${SRC_LIB} -DDESTINATION=$<TARGET_FILE:${_DEST}> -P ${_MERGE_SCRIPT} |
| 165 | COMMENT "\t\tmerging objects from ${_SRC_LIB_NAME}") |
| 166 | endforeach() |
| 167 | endfunction() |
| 168 | |
| 169 | function(compiler_generate_binary_output TARGET) |
| 170 | add_custom_command(TARGET ${TARGET} POST_BUILD COMMAND ${CMAKE_GNUARM_OBJCOPY} ARGS -O binary $<TARGET_FILE:${TARGET}> $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.bin) |
| 171 | endfunction() |