1+ # CMake toolchain for the Microchip XC16 compiler
2+ # http://www.microchip.com/mplab/compilers
3+ #
4+ # Copyright 2016 Sam Hanes
5+ #
6+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7+ # of this software and associated documentation files (the "Software"), to deal
8+ # in the Software without restriction, including without limitation the rights
9+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ # copies of the Software, and to permit persons to whom the Software is
11+ # furnished to do so, subject to the following conditions:
12+ #
13+ # The above copyright notice and this permission notice shall be included in
14+ # all copies or substantial portions of the Software.
15+ #
16+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ # SOFTWARE.
123
24+ # set the CMake platform
25+ # Generic is correct for embedded systems without an OS
226set (CMAKE_SYSTEM_NAME Generic)
3- set (CMAKE_SYSTEM_PROCESSOR PIC24FJ256GB106)
427
5- set (XC16_PATH /opt/microchip/xc16/v1.25)
6- set (CMAKE_C_COMPILER ${XC16_PATH} /bin/xc16-gcc)
7- set (XC16_BIN2HEX ${XC16_PATH} /bin/xc16-bin2hex)
28+ # set the (default) MCU model
29+ set (CMAKE_SYSTEM_PROCESSOR PIC24FJ32GB002
30+ CACHE STRING "the part number of the target Microchip MCU"
31+ )
32+
33+ # set the (default) path to XC16
34+ set (XC16_PATH "/opt/microchip/xc16/v1.25"
35+ CACHE PATH "the path at which Microchip XC16 is installed"
36+ )
37+
38+ # validate the XC16 path
39+ if (NOT EXISTS ${XC16_PATH} )
40+ message (FATAL_ERROR
41+ "XC16 path '${XC16_PATH} ' does not exist"
42+ )
43+ endif ()
844
945# ensure that only the cross toolchain is searched for
1046# libraries, include files, and other similar things
@@ -13,3 +49,92 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH)
1349set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
1450set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
1551set (CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
52+
53+ # set up the toolchain executables
54+ find_program (CMAKE_C_COMPILER xc16-gcc)
55+ find_program (CMAKE_AR xc16-ar)
56+ find_program (CMAKE_LINKER xc16-ld)
57+ find_program (CMAKE_NM xc16-nm)
58+ find_program (CMAKE_OBJDUMP xc16-objdump)
59+ find_program (CMAKE_RANLIB xc16-ranlib)
60+ find_program (CMAKE_STRIP xc16-strip)
61+ find_program (XC16_BIN2HEX xc16-bin2hex)
62+
63+ # verify the comiler was found
64+ if (NOT CMAKE_C_COMPILER)
65+ message (FATAL_ERROR
66+ "XC16 path '${XC16_PATH} '"
67+ " does not contain an XC16 compiler"
68+ )
69+ endif ()
70+
71+ # parse the CPU option and set relevant options
72+ if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(dsPIC|PIC)([0-9]+[A-Z])([A-Z0-9]+)$" )
73+ set (XC16_CPU_FAMILY "${CMAKE_MATCH_1}${CMAKE_MATCH_2} " )
74+ set (XC16_CPU_MODEL "${CMAKE_MATCH_2}${CMAKE_MATCH_3} " )
75+ set (XC16_SUPPORT_DIR "${XC16_PATH} /support/${XC16_CPU_FAMILY} " )
76+ set (XC16_LINKER_SCRIPT "p${XC16_CPU_MODEL} .gld" )
77+ set (XC16_GLD_PATH "${XC16_SUPPORT_DIR} /gld/${XC16_LINKER_SCRIPT} " )
78+
79+ if (NOT EXISTS ${XC16_GLD_PATH} )
80+ message (SEND_ERROR
81+ "CPU '${CMAKE_SYSTEM_PROCESSOR} ' is not supported: "
82+ "linker script '${XC16_GLD_PATH} ' does not exist."
83+ )
84+ endif ()
85+
86+ add_compile_options (
87+ "-mcpu=${XC16_CPU_MODEL} "
88+ )
89+
90+ set (CMAKE_C_LINK_FLAGS
91+ "-mcpu=${XC16_CPU_MODEL} -Wl,--script,${XC16_LINKER_SCRIPT} "
92+ )
93+ else ()
94+ message (SEND_ERROR
95+ "Invalid CMAKE_SYSTEM_PROCESSOR value '${CMAKE_SYSTEM_PROCESSOR} '."
96+ )
97+ endif ()
98+
99+
100+ # adds an Intel HEX conversion to the given target
101+ function (bin2hex target )
102+ function (get_target_property_fallback var target )
103+ set (result NOTFOUND )
104+ foreach (property ${ARGN} )
105+ get_target_property (result ${target} ${property} )
106+ if (result)
107+ break ()
108+ endif ()
109+ endforeach ()
110+ set (${var} ${result} PARENT_SCOPE)
111+ endfunction ()
112+
113+ get_target_property_fallback(in_f ${target}
114+ RUNTIME_OUTPUT_NAME
115+ OUTPUT_NAME
116+ NAME
117+ )
118+
119+ get_target_property_fallback(dir ${target}
120+ RUNTIME_OUTPUT_DIRECTORY
121+ BINARY_DIR
122+ )
123+
124+ get_filename_component (out_f ${in_f} NAME_WE )
125+ set (out_f "${out_f} .hex" )
126+
127+ add_custom_command (
128+ TARGET ${target} POST_BUILD
129+ WORKING_DIRECTORY ${dir}
130+ COMMAND ${XC16_BIN2HEX} ${in_f}
131+ COMMENT "Creating HEX for ${target} "
132+ BYPRODUCTS ${dir} /${out_f}
133+ VERBATIM
134+ )
135+
136+ set_property (DIRECTORY APPEND
137+ PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
138+ ${dir} /${out_f}
139+ )
140+ endfunction ()
0 commit comments