Skip to content

Commit fec2397

Browse files
committed
Add support for the xc8-cc CLI from XC8 2.00.
1 parent c7462ae commit fec2397

File tree

4 files changed

+152
-11
lines changed

4 files changed

+152
-11
lines changed

Modules/Compiler/XC8-C.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
# called by `CMakeCInformation`
1515
# to configure the XC8 compiler interface for C files
16+
# this supports the `xc8` CLI driver in XC8 1.x
17+
# and the equivalent legacy CLI driver in XC8 2.x
1618

1719

1820
set(MICROCHIP_XC8_MODE "free"

Modules/Compiler/XC8CC-C.cmake

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#=============================================================================
2+
# Copyright 2019 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+
# called by `CMakeCInformation`
15+
# to configure the XC8CC compiler interface for C files
16+
# this supports the xc8-cc CLI driver from XC8 v2.x
17+
18+
19+
string(APPEND CMAKE_C_FLAGS_INIT
20+
# build for the configured MCU model
21+
" -mcpu=${MICROCHIP_MCU_MODEL}"
22+
# fail if the requested optimization level is forbidden by the license
23+
" --nofallback"
24+
)
25+
26+
set(CMAKE_C_OUTPUT_EXTENSION ".p1")
27+
28+
set(CMAKE_C_COMPILE_OBJECT)
29+
string(APPEND CMAKE_C_COMPILE_OBJECT
30+
"<CMAKE_C_COMPILER> <FLAGS> <DEFINES> <INCLUDES>"
31+
" -o <OBJECT> -c <SOURCE>"
32+
)
33+
34+
set(CMAKE_C_LINK_EXECUTABLE)
35+
string(APPEND CMAKE_C_LINK_EXECUTABLE
36+
"<CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS>"
37+
" <OBJECTS> <LINK_LIBRARIES>"
38+
" -o <TARGET>"
39+
)

Modules/Platform/MicrochipMCU-C-XC8.cmake

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,64 @@ if(NOT MICROCHIP_XC8_PATH)
2727
)
2828
endif()
2929

30-
3130
set(CMAKE_FIND_ROOT_PATH "${MICROCHIP_XC8_PATH}")
3231

33-
# skip compiler search and just use XC8
34-
find_program(CMAKE_C_COMPILER "xc8"
35-
PATHS "${MICROCHIP_XC8_PATH}"
36-
PATH_SUFFIXES "bin"
32+
33+
if(NOT MICROCHIP_XC8_CLI)
34+
set(MICROCHIP_XC8_CLI "xc8-cc")
35+
set(_xc8_cli_default TRUE CACHE INTERNAL "" FORCE)
36+
endif()
37+
set(MICROCHIP_XC8_CLI "${MICROCHIP_XC8_CLI}"
38+
CACHE STRING "the XC8 CLI driver to use ('xc8-cc' or 'xc8')"
3739
)
3840

41+
42+
if(MICROCHIP_XC8_CLI STREQUAL "xc8-cc")
43+
find_program(CMAKE_C_COMPILER "xc8-cc"
44+
PATHS "${MICROCHIP_XC8_PATH}"
45+
PATH_SUFFIXES "bin"
46+
)
47+
set(_xc8_version_flag "--version")
48+
set(CMAKE_C_COMPILER_ID "XC8CC")
49+
elseif(MICROCHIP_XC8_CLI STREQUAL "xc8")
50+
find_program(CMAKE_C_COMPILER "xc8"
51+
PATHS "${MICROCHIP_XC8_PATH}"
52+
PATH_SUFFIXES "bin"
53+
)
54+
set(_xc8_version_flag "--ver")
55+
set(CMAKE_C_COMPILER_ID "XC8")
56+
else()
57+
message(FATAL_ERROR
58+
"Invalid choice '${MICROCHIP_XC8_CLI}' for MICROCHIP_XC8_CLI."
59+
" Please choose either 'xc8-cc' (recommended) or 'xc8'."
60+
" See docs/xc8.md in your cmake-microchip installation for"
61+
" details on this option."
62+
)
63+
endif()
64+
65+
3966
if(NOT CMAKE_C_COMPILER)
67+
if(_xc8_cli_default)
68+
message(WARNING
69+
"The XC8 command-line driver was not explicitly selected,"
70+
" so the newer 'xc8-cc' driver is being used. This requires"
71+
" XC8 version 2.00 or newer. If you want to use older versions"
72+
" of XC8, or if you want to use the legacy 'xc8' driver in XC8"
73+
" 2.00 or newer, add this line to your CMakeLists.txt before"
74+
" the 'project' command:\n"
75+
" set(MICROCHIP_XC8_CLI xc8)\n"
76+
"To suppress this message when XC8 is not found but continue"
77+
" using the newer 'xc8-cc' driver, add this line to your"
78+
" CMakeLists.txt before the 'project' command:\n"
79+
" set(MICROCHIP_XC8_CLI xc8-cc)\n"
80+
"For more information on selecting a command-line driver"
81+
" see docs/xc8.md in your cmake-microchip installation."
82+
)
83+
endif()
84+
4085
message(FATAL_ERROR
41-
"The XC8 compiler executable was not found, but what looks"
42-
" like an XC8 installation was found at:\n"
86+
"The XC8 compiler executable ${MICROCHIP_XC8_CLI} was not found,"
87+
" but what looks like an XC8 installation was found at:\n"
4388
" ${MICROCHIP_XC8_PATH}\n"
4489
"Please provide the path to a working XC8 installation on the"
4590
" command line, for example:\n"
@@ -49,28 +94,27 @@ endif()
4994

5095
# skip compiler ID since XC8 isn't supported by CMake's test file
5196
set(CMAKE_C_COMPILER_ID_RUN 1)
52-
set(CMAKE_C_COMPILER_ID "XC8")
5397

5498
# call the compiler to check its version
5599
function(_xc8_get_version)
56100
execute_process(
57-
COMMAND "${CMAKE_C_COMPILER}" "--ver"
101+
COMMAND "${CMAKE_C_COMPILER}" "${_xc8_version_flag}"
58102
OUTPUT_VARIABLE output
59103
ERROR_VARIABLE output
60104
RESULT_VARIABLE result
61105
)
62106

63107
if(result)
64108
message(FATAL_ERROR
65-
"Calling '${CMAKE_C_COMPILER} --ver' failed."
109+
"Calling '${CMAKE_C_COMPILER} ${_xc8_version_flag}' failed."
66110
)
67111
endif()
68112

69113
if(output MATCHES "XC8 C Compiler V([0-9]+\.[0-9]+)")
70114
set(CMAKE_C_COMPILER_VERSION ${CMAKE_MATCH_1} PARENT_SCOPE)
71115
else()
72116
message(FATAL_ERROR
73-
"Failed to parse output of '${CMAKE_C_COMPILER} --ver'."
117+
"Failed to parse output of '${CMAKE_C_COMPILER} ${_xc8_version_flag}'."
74118
)
75119
endif()
76120
endfunction()

docs/xc8.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
## Versions
3+
4+
In XC8 version 2.00, Microchip switched from their proprietary HI-TECH C
5+
frontend to Clang in order to support C99. Rather than implement PIC
6+
code generation in LLVM, they translate LLVM's IR to p-code (the HI-TECH
7+
IR), which is then run through the same code generator used with the old
8+
HI-TECH frontend. Although they use Clang by default, current versions
9+
of XC8 still include the HI-TECH C frontend to support older projects.
10+
They've also bundled in AVR-GCC to support AVR parts.
11+
12+
### Command-line Driver
13+
14+
Since they now need to support multiple compilers, Microchip also
15+
introduced a new command-line driver, `xc8-cc`, in XC8 2.00. `xc8-cc`
16+
is completely custom, but it uses GCC-style options for compatibility
17+
with XC16 and XC32. The old command-line driver `xc8` was retained for
18+
backwards compatibility.
19+
20+
You can select which command-line driver to use by setting
21+
`MICROCHIP_XC8_CLI` to either `xc8-cc` or `xc8` in your `CMakeLists.txt`
22+
before calling the `project` command (which is when compiler resolution
23+
occurs). For example:
24+
25+
```cmake
26+
# set up the Microchip cross toolchain
27+
set(CMAKE_TOOLCHAIN_FILE external/cmake-microchip/toolchain.cmake)
28+
29+
# set the default MCU model
30+
set(MICROCHIP_MCU PIC18F87J50)
31+
32+
# use the new command-line driver
33+
set(MICROCHIP_XC8_CLI xc8-cc)
34+
35+
36+
project(example C)
37+
```
38+
39+
`MICROCHIP_XC8_CLI` may also be set on the command line as a cache
40+
variable, although doing so may break your project if you use
41+
command-line flags in your configuration that are specific to one of the
42+
drivers. For example:
43+
44+
```plain
45+
cmake -DMICROCHIP_XC8_CLI=xc8 -DMICROCHIP_XC8_PATH=/opt/microchip/xc8/v1.45 .
46+
```
47+
48+
For new projects it is recommended to use `xc8-cc`, which is the default
49+
in cmake-microchip as of version 0.3. Using `xc8-cc` is required if you
50+
want to use Clang (for C99) or AVR-GCC.
51+
52+
Using the legacy `xc8` command-line driver is only recommended if you
53+
need to support versions of XC8 before 2.00, or if you have a significant
54+
number of command-line flags for `xc8` already and don't want to port
55+
them. It is *not* necessary to use `xc8` in order to use the old HI-TECH
56+
C frontend.

0 commit comments

Comments
 (0)