|
| 1 | +#============================================================================= |
| 2 | +# Copyright 2016 Sam Hanes |
| 3 | +# |
| 4 | +# Distributed under the OSI-approved BSD License (the "License"); |
| 5 | +# see accompanying file COPYING.txt for details. |
| 6 | +# |
| 7 | +# This software is distributed WITHOUT ANY WARRANTY; without even the |
| 8 | +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 9 | +# See the License for more information. |
| 10 | +#============================================================================= |
| 11 | +# (To distribute this file outside of CMake-Microchip, |
| 12 | +# substitute the full License text for the above reference.) |
| 13 | + |
| 14 | +# this module is called by `Platform/MicrochipMCU-C` |
| 15 | +# to provide information specific to the XC16 compiler |
| 16 | + |
| 17 | + |
| 18 | +# set the (default) path to XC16 |
| 19 | +set(MICROCHIP_XC16_PATH "/opt/microchip/xc16/v1.25" |
| 20 | + CACHE PATH "the path at which Microchip XC16 is installed" |
| 21 | +) |
| 22 | + |
| 23 | +# validate the XC16 path |
| 24 | +if(NOT EXISTS ${MICROCHIP_XC16_PATH}) |
| 25 | + message(FATAL_ERROR |
| 26 | + "XC16 path '${XC16_PATH}' does not exist" |
| 27 | + ) |
| 28 | +endif() |
| 29 | + |
| 30 | +# ensure that only the cross toolchain is searched for |
| 31 | +# tools, libraries, include files, and other similar things |
| 32 | +set(CMAKE_FIND_ROOT_PATH ${MICROCHIP_XC16_PATH}) |
| 33 | +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH) |
| 34 | +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) |
| 35 | +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) |
| 36 | +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) |
| 37 | + |
| 38 | +# set up the toolchain executables |
| 39 | +find_program(CMAKE_C_COMPILER xc16-gcc) |
| 40 | +find_program(CMAKE_AR xc16-ar) |
| 41 | +find_program(CMAKE_LINKER xc16-ld) |
| 42 | +find_program(CMAKE_NM xc16-nm) |
| 43 | +find_program(CMAKE_OBJDUMP xc16-objdump) |
| 44 | +find_program(CMAKE_RANLIB xc16-ranlib) |
| 45 | +find_program(CMAKE_STRIP xc16-strip) |
| 46 | +find_program(XC16_BIN2HEX xc16-bin2hex) |
| 47 | + |
| 48 | +# verify the comiler was found |
| 49 | +if(NOT CMAKE_C_COMPILER) |
| 50 | + message(FATAL_ERROR |
| 51 | + "XC16 path '${XC16_PATH}'" |
| 52 | + " does not contain an XC16 compiler" |
| 53 | + ) |
| 54 | +endif() |
| 55 | + |
| 56 | + |
| 57 | +# verify that the MCU is supported |
| 58 | +set(MICROCHIP_XC16_LINKER_SCRIPT "p${MICROCHIP_MCU_MODEL}.gld") |
| 59 | +set(MICROCHIP_XC16_SUPPORT_DIR |
| 60 | + "${MICROCHIP_XC16_PATH}/support/${MICROCHIP_MCU_FAMILY}" |
| 61 | +) |
| 62 | +set(MICROCHIP_XC16_GLD_PATH |
| 63 | + "${MICROCHIP_XC16_SUPPORT_DIR}/gld/${MICROCHIP_XC16_LINKER_SCRIPT}" |
| 64 | +) |
| 65 | +if(NOT EXISTS ${MICROCHIP_XC16_GLD_PATH}) |
| 66 | + message(SEND_ERROR |
| 67 | + "MCU '${MICROCHIP_MCU}' is not supported: linker script" |
| 68 | + " '${MICROCHIP_XC16_GLD_PATH}' does not exist." |
| 69 | + ) |
| 70 | +endif() |
| 71 | + |
| 72 | + |
| 73 | +add_compile_options( |
| 74 | + "-mcpu=${MICROCHIP_MCU_MODEL}" |
| 75 | +) |
| 76 | + |
| 77 | +string(APPEND CMAKE_C_LINK_FLAGS |
| 78 | + " -mcpu=${MICROCHIP_MCU_MODEL}" |
| 79 | + " -Wl,--script,${MICROCHIP_XC16_LINKER_SCRIPT}" |
| 80 | +) |
| 81 | + |
| 82 | + |
| 83 | +# adds an Intel HEX conversion to the given target |
| 84 | +function(bin2hex target) |
| 85 | + function(get_target_property_fallback var target) |
| 86 | + set(result NOTFOUND) |
| 87 | + foreach(property ${ARGN}) |
| 88 | + get_target_property(result ${target} ${property}) |
| 89 | + if(result) |
| 90 | + break() |
| 91 | + endif() |
| 92 | + endforeach() |
| 93 | + set(${var} ${result} PARENT_SCOPE) |
| 94 | + endfunction() |
| 95 | + |
| 96 | + get_target_property_fallback(in_f ${target} |
| 97 | + RUNTIME_OUTPUT_NAME |
| 98 | + OUTPUT_NAME |
| 99 | + NAME |
| 100 | + ) |
| 101 | + |
| 102 | + get_target_property_fallback(dir ${target} |
| 103 | + RUNTIME_OUTPUT_DIRECTORY |
| 104 | + BINARY_DIR |
| 105 | + ) |
| 106 | + |
| 107 | + get_filename_component(out_f ${in_f} NAME_WE) |
| 108 | + set(out_f "${out_f}.hex") |
| 109 | + |
| 110 | + add_custom_command( |
| 111 | + TARGET ${target} POST_BUILD |
| 112 | + WORKING_DIRECTORY ${dir} |
| 113 | + COMMAND ${XC16_BIN2HEX} ${in_f} |
| 114 | + COMMENT "Creating HEX for ${target}" |
| 115 | + BYPRODUCTS ${dir}/${out_f} |
| 116 | + VERBATIM |
| 117 | + ) |
| 118 | + |
| 119 | + set_property(DIRECTORY APPEND |
| 120 | + PROPERTY ADDITIONAL_MAKE_CLEAN_FILES |
| 121 | + ${dir}/${out_f} |
| 122 | + ) |
| 123 | +endfunction() |
0 commit comments