Skip to content

Commit d95718f

Browse files
committed
Refactor XC16 toolchain into platform modules.
1 parent ef7fb27 commit d95718f

File tree

7 files changed

+312
-163
lines changed

7 files changed

+312
-163
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 loaded during the search for a C compiler
15+
# to provide the information necessary to find one.
16+
17+
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "PIC_16")
18+
include(Platform/MicrochipMCU-C-XC16)
19+
else()
20+
message(FATAL_ERROR
21+
"No C compiler for '${CMAKE_SYSTEM_PROCESSOR}'"
22+
" is supported yet."
23+
)
24+
endif()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 loaded prior to the compiler search to set up enough of
15+
# the platform configuration that an appropriate compiler can be found.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 loaded after the compiler has been determined
15+
# to set up the rest of the platform configuration.
16+
17+
# without a filesystem, MCUs definitely don't support shared libraries
18+
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS FALSE)
19+
20+
# set search paths to work relative to CMAKE_FIND_ROOT_PATH
21+
set(CMAKE_SYSTEM_INCLUDE_PATH /include)
22+
set(CMAKE_SYSTEM_LIBRARY_PATH /lib)
23+
set(CMAKE_SYSTEM_PROGRAM_PATH /bin)

README.rst

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,16 @@ easiest way is to add a submodule::
2020

2121
Then add this snippet at the very top of your ``CMakeLists.txt``::
2222

23-
# make the Microchip support modules available
24-
list(APPEND CMAKE_MODULE_PATH
25-
"${CMAKE_CURRENT_SOURCE_DIR}/external/cmake-microchip/Modules"
26-
)
27-
2823
# set up the Microchip cross toolchain
29-
set(CMAKE_TOOLCHAIN_FILE
30-
external/cmake-microchip/Toolchains/xc16.cmake
31-
)
24+
set(CMAKE_TOOLCHAIN_FILE external/cmake-microchip/toolchain.cmake)
25+
26+
# set the default MCU model
27+
set(MICROCHIP_MCU PIC24FJ32GB002)
3228

33-
The target MCU is set by the ``CMAKE_SYSTEM_PROCESSOR`` variable. It can
34-
be set on the CMake command line like so::
29+
The target MCU is set by the ``MICROCHIP_MCU`` variable. It can be set
30+
in ``CMakeLists.txt`` as above or on the CMake command line like so::
3531

36-
cmake -DCMAKE_SYSTEM_PROCESSOR=PIC24FJ256GB004 .
32+
cmake -DMICROCHIP_MCU=PIC24FJ256GB004 .
3733

3834
Copying
3935
=======

Toolchains/xc16.cmake

Lines changed: 0 additions & 152 deletions
This file was deleted.

0 commit comments

Comments
 (0)