├── COPYING.txt ├── Modules ├── Compiler │ ├── XC8-C.cmake │ └── XC8CC-C.cmake ├── FindMStack.cmake ├── MicrochipBin2Hex.cmake ├── MicrochipPathSearch.cmake └── Platform │ ├── MicrochipMCU-C-XC16.cmake │ ├── MicrochipMCU-C-XC32.cmake │ ├── MicrochipMCU-C-XC8.cmake │ ├── MicrochipMCU-C.cmake │ ├── MicrochipMCU-GNU-C.cmake │ ├── MicrochipMCU-Initialize.cmake │ └── MicrochipMCU.cmake ├── README.rst ├── docs └── xc8.md └── toolchain.cmake /COPYING.txt: -------------------------------------------------------------------------------- 1 | CMake-Microchip - CMake support for the Microchip embedded toolchain 2 | Copyright 2016 Sam Hanes 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 14 | 3. Neither the name of the copyright holder nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | This software is provided by the copyright holders and contributors "as 19 | is" and any express or implied warranties, including, but not limited 20 | to, the implied warranties of merchantability and fitness for a 21 | particular purpose are disclaimed. In no event shall the copyright 22 | holder or contributors be liable for any direct, indirect, incidental, 23 | special, exemplary, or consequential damages (including, but not limited 24 | to, procurement of substitute goods or services; loss of use, data, or 25 | profits; or business interruption) however caused and on any theory of 26 | liability, whether in contract, strict liability, or tort (including 27 | negligence or otherwise) arising in any way out of the use of this 28 | software, even if advised of the possibility of such damage. 29 | -------------------------------------------------------------------------------- /Modules/Compiler/XC8-C.cmake: -------------------------------------------------------------------------------- 1 | #============================================================================= 2 | # Copyright 2018 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 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 18 | 19 | 20 | set(MICROCHIP_XC8_MODE "free" 21 | CACHE STRING "the license mode for XC8 (pro, std, free)" 22 | ) 23 | 24 | string(APPEND CMAKE_C_FLAGS_INIT 25 | # don't output the copyright notice on every invocation 26 | "-Q" 27 | # use the configured license mode and fail if it's not available 28 | " --mode=${MICROCHIP_XC8_MODE} --nofallback" 29 | # build for the configured MCU model 30 | " --chip=${MICROCHIP_MCU_MODEL}" 31 | ) 32 | 33 | 34 | set(CMAKE_C_OUTPUT_EXTENSION ".p1") 35 | set(CMAKE_STATIC_LIBRARY_SUFFIX_C ".lpp") 36 | 37 | 38 | set(CMAKE_C_COMPILE_OBJECT) 39 | string(APPEND CMAKE_C_COMPILE_OBJECT 40 | " " 41 | " -o --pass1 " 42 | ) 43 | 44 | set(CMAKE_C_LINK_EXECUTABLE) 45 | string(APPEND CMAKE_C_LINK_EXECUTABLE 46 | " " 47 | " " 48 | " -o" 49 | ) 50 | 51 | set(CMAKE_C_CREATE_STATIC_LIBRARY) 52 | string(APPEND CMAKE_C_CREATE_STATIC_LIBRARY 53 | " " 54 | " " 55 | " --output=lpp -o" 56 | ) 57 | -------------------------------------------------------------------------------- /Modules/Compiler/XC8CC-C.cmake: -------------------------------------------------------------------------------- 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 | set(CMAKE_EXECUTABLE_SUFFIX ".elf") 28 | 29 | set(CMAKE_C_COMPILE_OBJECT) 30 | string(APPEND CMAKE_C_COMPILE_OBJECT 31 | " " 32 | " -o -c " 33 | ) 34 | 35 | set(CMAKE_C_LINK_EXECUTABLE) 36 | string(APPEND CMAKE_C_LINK_EXECUTABLE 37 | " " 38 | " " 39 | " -o " 40 | ) 41 | 42 | set(CMAKE_C_CREATE_STATIC_LIBRARY) 43 | string(APPEND CMAKE_C_CREATE_STATIC_LIBRARY 44 | " -r " 45 | " " 46 | ) 47 | -------------------------------------------------------------------------------- /Modules/FindMStack.cmake: -------------------------------------------------------------------------------- 1 | #.rst: 2 | # FindMStack 3 | # ----------- 4 | # 5 | # Finds the M-Stack USB stack for Microchip PIC MCUs. 6 | # 7 | # ########## COPYRIGHT NOTICE ########## 8 | # CMake-Microchip - CMake support for the Microchip embedded toolchain 9 | # Copyright 2016 Sam Hanes 10 | # 11 | # Redistribution and use in source and binary forms, with or without 12 | # modification, are permitted provided that the following conditions 13 | # are met: 14 | # 15 | # 1. Redistributions of source code must retain the above copyright 16 | # notice, this list of conditions and the following disclaimer. 17 | # 18 | # 2. Redistributions in binary form must reproduce the above copyright 19 | # notice, this list of conditions and the following disclaimer in the 20 | # documentation and/or other materials provided with the distribution. 21 | # 22 | # 3. Neither the name of the copyright holder nor the names of its 23 | # contributors may be used to endorse or promote products derived from 24 | # this software without specific prior written permission. 25 | # 26 | # This software is provided by the copyright holders and contributors 27 | # "as is" and any express or implied warranties, including, but not 28 | # limited to, the implied warranties of merchantability and fitness for 29 | # a particular purpose are disclaimed. In no event shall the copyright 30 | # holder or contributors be liable for any direct, indirect, incidental, 31 | # special, exemplary, or consequential damages (including, but not 32 | # limited to, procurement of substitute goods or services; loss of use, 33 | # data, or profits; or business interruption) however caused and on any 34 | # theory of liability, whether in contract, strict liability, or tort 35 | # (including negligence or otherwise) arising in any way out of the use 36 | # of this software, even if advised of the possibility of such damage. 37 | # ########## END COPYRIGHT NOTICE ########## 38 | 39 | 40 | set(MStack_ROOT external/m-stack 41 | CACHE PATH "the root of the M-Stack source checkout" 42 | ) 43 | get_filename_component(MStack_ROOT "${MStack_ROOT}" ABSOLUTE) 44 | 45 | set(MStack_USB_ROOT ${MStack_ROOT}/usb) 46 | 47 | include(FindPackageHandleStandardArgs) 48 | find_package_handle_standard_args(MStack 49 | REQUIRED_VARS 50 | MStack_ROOT 51 | ) 52 | 53 | if(NOT MStack_FOUND) 54 | return() 55 | endif() 56 | 57 | 58 | add_library(MStack INTERFACE) 59 | target_sources(MStack INTERFACE 60 | ${MStack_USB_ROOT}/src/usb.c 61 | ) 62 | target_include_directories(MStack INTERFACE 63 | ${MStack_ROOT}/usb/include 64 | ) 65 | 66 | 67 | add_library(MStack_HID INTERFACE) 68 | target_sources(MStack_HID INTERFACE 69 | ${MStack_USB_ROOT}/src/usb_hid.c 70 | ) 71 | target_link_libraries(MStack_HID 72 | INTERFACE MStack 73 | ) 74 | 75 | 76 | add_library(MStack_CDC INTERFACE) 77 | target_sources(MStack_CDC INTERFACE 78 | ${MStack_USB_ROOT}/src/usb_cdc.c 79 | ) 80 | target_link_libraries(MStack_CDC 81 | INTERFACE MStack 82 | ) 83 | -------------------------------------------------------------------------------- /Modules/MicrochipBin2Hex.cmake: -------------------------------------------------------------------------------- 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 | 15 | function(bin2hex target) 16 | find_program(MICROCHIP_BIN2HEX 17 | NAMES ${_CMAKE_TOOLCHAIN_PREFIX}bin2hex bin2hex 18 | HINTS ${_CMAKE_TOOLCHAIN_LOCATION} 19 | ) 20 | 21 | if(NOT MICROCHIP_BIN2HEX) 22 | message(SEND_ERROR "No bin2hex program was found") 23 | endif() 24 | 25 | function(get_target_property_fallback var target) 26 | set(result NOTFOUND) 27 | foreach(property ${ARGN}) 28 | get_target_property(result ${target} ${property}) 29 | if(result) 30 | break() 31 | endif() 32 | endforeach() 33 | set(${var} ${result} PARENT_SCOPE) 34 | endfunction() 35 | 36 | get_target_property_fallback(in_f ${target} 37 | RUNTIME_OUTPUT_NAME 38 | OUTPUT_NAME 39 | NAME 40 | ) 41 | 42 | get_target_property_fallback(dir ${target} 43 | RUNTIME_OUTPUT_DIRECTORY 44 | BINARY_DIR 45 | ) 46 | 47 | get_filename_component(out_f ${in_f} NAME_WE) 48 | set(out_f "${out_f}.hex") 49 | 50 | add_custom_command( 51 | TARGET ${target} POST_BUILD 52 | WORKING_DIRECTORY ${dir} 53 | COMMAND "${MICROCHIP_BIN2HEX}" "${in_f}" 54 | BYPRODUCTS ${dir}/${out_f} 55 | VERBATIM 56 | ) 57 | 58 | set_property(DIRECTORY APPEND 59 | PROPERTY ADDITIONAL_MAKE_CLEAN_FILES 60 | ${dir}/${out_f} 61 | ) 62 | endfunction() 63 | -------------------------------------------------------------------------------- /Modules/MicrochipPathSearch.cmake: -------------------------------------------------------------------------------- 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 | function(MICROCHIP_PATH_SEARCH outvar target) 15 | set(options) 16 | list(APPEND oneValueArgs CACHE STORE_VERSION) 17 | set(multiValueArgs BAD_VERSIONS) 18 | cmake_parse_arguments(SEARCH 19 | "${options}" "${oneValueArgs}" "${multiValueArgs}" 20 | ${ARGN} 21 | ) 22 | 23 | if(SEARCH_CACHE) 24 | set(${outvar} "" CACHE PATH "${SEARCH_CACHE}") 25 | if(${outvar}) 26 | if(EXISTS "${${outvar}}") 27 | set(${outvar} "${${outvar}}" PARENT_SCOPE) 28 | else() 29 | message(FATAL_ERROR 30 | "given path '${${outvar}}' does not exist" 31 | " in ${outvar} (${SEARCH_CACHE})" 32 | ) 33 | set(${outvar} "${outvar}-NOTFOUND" PARENT_SCOPE) 34 | endif() 35 | return() 36 | endif() 37 | endif() 38 | 39 | set(MICROCHIP_SEARCH_PATH "" 40 | CACHE STRING "the search path for Microchip tool installations" 41 | ) 42 | 43 | if(CMAKE_HOST_SYSTEM MATCHES "Linux") 44 | list(APPEND MICROCHIP_SEARCH_PATH /opt/microchip) 45 | elseif(CMAKE_HOST_SYSTEM MATCHES "Windows") 46 | list(APPEND MICROCHIP_SEARCH_PATH 47 | "C:/Program Files/Microchip" 48 | "C:/Program Files (x86)/Microchip" 49 | ) 50 | endif() 51 | 52 | set(candidate_paths) 53 | foreach(path ${MICROCHIP_SEARCH_PATH}) 54 | if(IS_DIRECTORY "${path}/${target}") 55 | list(APPEND candidate_paths "${path}/${target}") 56 | endif() 57 | endforeach() 58 | 59 | set(best_good_path) 60 | set(best_good_version) 61 | set(best_bad_path) 62 | set(best_bad_version) 63 | 64 | set(msg "\nSearching for Microchip tool '${target}' (${outvar}):") 65 | 66 | foreach(path ${candidate_paths}) 67 | file(GLOB versions RELATIVE "${path}" "${path}/v*") 68 | foreach(version_path ${versions}) 69 | string(REGEX REPLACE "^v" "" version "${version_path}") 70 | 71 | set(type good) 72 | if(${version} IN_LIST SEARCH_BAD_VERSIONS) 73 | set(type bad) 74 | endif() 75 | 76 | string(APPEND msg 77 | "\n ${type} ${version} = ${path}/${version_path}" 78 | ) 79 | 80 | if(NOT best_${type}_version 81 | OR version VERSION_GREATER best_${type}_version) 82 | set(best_${type}_version "${version}") 83 | set(best_${type}_path "${path}/${version_path}") 84 | endif() 85 | endforeach() 86 | endforeach() 87 | 88 | if(best_good_path) 89 | set(result "${best_good_path}") 90 | set(result_version "${best_good_version}") 91 | elseif(best_bad_path) 92 | set(result "${best_bad_path}") 93 | set(result_version "${best_good_version}") 94 | 95 | message(WARNING 96 | "Version ${best_bad_version} of ${target} is known" 97 | " to be problematic. If you encounter issues, set" 98 | " ${outvar} to a different version." 99 | ) 100 | else() 101 | set(result "${outvar}-NOTFOUND") 102 | set(result_version "${SEARCH_STORE_VERSION}-NOTFOUND") 103 | endif() 104 | 105 | string(APPEND msg 106 | "\n Best good version: ${best_good_version}" 107 | "\n Best bad version: ${best_bad_version}" 108 | "\n Chose: ${result}" 109 | ) 110 | file(APPEND 111 | "${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CMakeOutput.log" 112 | "${msg}\n\n" 113 | ) 114 | 115 | if(SEARCH_CACHE) 116 | set(${outvar} "${result}" 117 | CACHE PATH "${SEARCH_CACHE}" FORCE 118 | ) 119 | endif() 120 | set(${outvar} "${result}" PARENT_SCOPE) 121 | 122 | if(SEARCH_STORE_VERSION) 123 | set(${SEARCH_STORE_VERSION} "${result_version}" PARENT_SCOPE) 124 | endif() 125 | endfunction() 126 | -------------------------------------------------------------------------------- /Modules/Platform/MicrochipMCU-C-XC16.cmake: -------------------------------------------------------------------------------- 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 | include(MicrochipPathSearch) 18 | MICROCHIP_PATH_SEARCH(MICROCHIP_XC16_PATH xc16 19 | CACHE "the path to a Microchip XC16 installation" 20 | BAD_VERSIONS 1.26 21 | ) 22 | 23 | if(NOT MICROCHIP_XC16_PATH) 24 | message(FATAL_ERROR 25 | "No Microchip XC16 compiler was found. Please provide the path" 26 | " to an XC16 installation on the command line, for example:\n" 27 | "cmake -DMICROCHIP_XC16_PATH=/opt/microchip/xc16/v1.25 ." 28 | ) 29 | endif() 30 | 31 | set(CMAKE_FIND_ROOT_PATH ${MICROCHIP_XC16_PATH}) 32 | 33 | 34 | 35 | # Unfortunately the normal CMake compiler detection process doesn't work 36 | # with XC16. It functions by compiling a file which uses preprocessor 37 | # conditionals to determine the compiler type and version and puts that 38 | # information in string literals, then running `strings` on the output 39 | # file and parsing out the detected values. That fails for XC16 because 40 | # string literals are not packed contiguously and therefore `strings` 41 | # can't find them. 42 | # 43 | # In intermediate object files, XC16 handles character literals as 44 | # 16-bit integers and string literals as arrays of character literals. 45 | # The strings therefore appear in the file with a zero byte after each 46 | # character. See the "MPLAB XC16 C Compiler User's Guide" (DS50002071E) 47 | # section 8.9 "Literal Constant Types and Formats". 48 | # 49 | # In the final executable file that issue is resolved as by that point 50 | # the compiler has optimized the 16-bit character literals down to 8-bit 51 | # values. `strings` still doesn't work, however. Program memory on the 52 | # 16-bit MCUs uses 24-bit words but is addressed on 16-bit boundaries. 53 | # Each program word therefore has an extra addressable byte which 54 | # doesn't actually exist. In the executable file that byte is included 55 | # and always zero, so string literals are written as groups of three 56 | # characters separated by zero bytes. 57 | # 58 | # 59 | # We therefore have to implement compiler version detection ourselves. 60 | # Fortunately that's quite easy as we know we're dealing with XC16 and 61 | # it has a `--version` switch that produces both the GCC anc XC16 62 | # version numbers. We still allow CMake's feature detection and test 63 | # routines to run as they still find some useful information. 64 | 65 | 66 | find_program(CMAKE_C_COMPILER "xc16-gcc") 67 | 68 | 69 | # bypass CMake compiler detection 70 | set(CMAKE_C_COMPILER_ID_RUN 1) 71 | 72 | # set the compiler ID manually 73 | set(CMAKE_C_COMPILER_ID GNU) 74 | set(MICROCHIP_C_COMPILER_ID XC16) 75 | set(CMAKE_COMPILER_IS_GNUCC 1) 76 | 77 | # call the compiler to check its version 78 | function(_xc16_get_version) 79 | execute_process( 80 | COMMAND "${CMAKE_C_COMPILER}" "--version" 81 | OUTPUT_VARIABLE output 82 | ERROR_VARIABLE output 83 | RESULT_VARIABLE result 84 | ) 85 | 86 | if(result) 87 | message(FATAL_ERROR 88 | "Calling '${CMAKE_C_COMPILER} --version' failed." 89 | ) 90 | endif() 91 | 92 | if(output MATCHES "([0-9]+[.0-9]+).*XC16, Microchip v([0-9]+\.[0-9]+)") 93 | set(gnu_version ${CMAKE_MATCH_1}) 94 | set(xc16_version ${CMAKE_MATCH_2}) 95 | else() 96 | message(FATAL_ERROR 97 | "Failed to parse output of '${CMAKE_C_COMPILER} --version'." 98 | ) 99 | endif() 100 | 101 | string(REPLACE "_" "." gnu_version ${gnu_version}) 102 | string(REPLACE "_" "." xc16_version ${xc16_version}) 103 | 104 | set(CMAKE_C_COMPILER_VERSION ${gnu_version} PARENT_SCOPE) 105 | set(MICROCHIP_C_COMPILER_VERSION ${xc16_version} PARENT_SCOPE) 106 | endfunction() 107 | _xc16_get_version() 108 | 109 | # set the default C standard manually 110 | # this is required by `Compiler/Gnu-C` 111 | set(CMAKE_C_STANDARD_COMPUTED_DEFAULT 90) 112 | 113 | 114 | add_compile_options( 115 | "-mcpu=${MICROCHIP_MCU_MODEL}" 116 | ) 117 | string(APPEND CMAKE_C_LINK_FLAGS 118 | " -mcpu=${MICROCHIP_MCU_MODEL}" 119 | " -Wl,--script,p${MICROCHIP_MCU_MODEL}.gld" 120 | ) 121 | -------------------------------------------------------------------------------- /Modules/Platform/MicrochipMCU-C-XC32.cmake: -------------------------------------------------------------------------------- 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 XC32 compiler 16 | 17 | include(MicrochipPathSearch) 18 | MICROCHIP_PATH_SEARCH(MICROCHIP_XC32_PATH xc32 19 | CACHE "the path to a Microchip XC32 installation" 20 | STORE_VERSION MICROCHIP_C_COMPILER_VERSION 21 | ) 22 | 23 | if(NOT MICROCHIP_XC32_PATH) 24 | message(FATAL_ERROR 25 | "No Microchip XC32 compiler was found. Please provide the path" 26 | " to an XC32 installation on the command line, for example:\n" 27 | "cmake -DMICROCHIP_XC32_PATH=/opt/microchip/xc32/v1.42 ." 28 | ) 29 | endif() 30 | 31 | set(CMAKE_FIND_ROOT_PATH ${MICROCHIP_XC32_PATH}) 32 | 33 | set(CMAKE_C_COMPILER xc32-gcc) 34 | set(MICROCHIP_C_COMPILER_ID XC32) 35 | 36 | add_compile_options( 37 | "-mprocessor=${MICROCHIP_MCU_MODEL}" 38 | ) 39 | string(APPEND CMAKE_C_LINK_FLAGS 40 | " -mprocessor=${MICROCHIP_MCU_MODEL}" 41 | ) 42 | -------------------------------------------------------------------------------- /Modules/Platform/MicrochipMCU-C-XC8.cmake: -------------------------------------------------------------------------------- 1 | #============================================================================= 2 | # Copyright 2018 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 XC8 compiler 16 | 17 | include(MicrochipPathSearch) 18 | MICROCHIP_PATH_SEARCH(MICROCHIP_XC8_PATH xc8 19 | CACHE "the path to a Microchip XC8 installation" 20 | ) 21 | 22 | if(NOT MICROCHIP_XC8_PATH) 23 | message(FATAL_ERROR 24 | "No Microchip XC8 compiler was found. Please provide the path" 25 | " to an XC8 installation on the command line, for example:\n" 26 | " cmake -DMICROCHIP_XC8_PATH=/opt/microchip/xc8/v2.00 .." 27 | ) 28 | endif() 29 | 30 | set(CMAKE_FIND_ROOT_PATH "${MICROCHIP_XC8_PATH}") 31 | 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')" 39 | ) 40 | 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 | find_program(CMAKE_AR "xc8-ar" 48 | PATHS "${MICROCHIP_XC8_PATH}" 49 | PATH_SUFFIXES "bin" 50 | ) 51 | set(_xc8_version_flag "--version") 52 | set(CMAKE_C_COMPILER_ID "XC8CC") 53 | elseif(MICROCHIP_XC8_CLI STREQUAL "xc8") 54 | find_program(CMAKE_C_COMPILER "xc8" 55 | PATHS "${MICROCHIP_XC8_PATH}" 56 | PATH_SUFFIXES "bin" 57 | ) 58 | set(_xc8_version_flag "--ver") 59 | set(CMAKE_C_COMPILER_ID "XC8") 60 | else() 61 | message(FATAL_ERROR 62 | "Invalid choice '${MICROCHIP_XC8_CLI}' for MICROCHIP_XC8_CLI." 63 | " Please choose either 'xc8-cc' (recommended) or 'xc8'." 64 | " See docs/xc8.md in your cmake-microchip installation for" 65 | " details on this option." 66 | ) 67 | endif() 68 | 69 | 70 | if(NOT CMAKE_C_COMPILER) 71 | if(_xc8_cli_default) 72 | message(WARNING 73 | "The XC8 command-line driver was not explicitly selected," 74 | " so the newer 'xc8-cc' driver is being used. This requires" 75 | " XC8 version 2.00 or newer. If you want to use older versions" 76 | " of XC8, or if you want to use the legacy 'xc8' driver in XC8" 77 | " 2.00 or newer, add this line to your CMakeLists.txt before" 78 | " the 'project' command:\n" 79 | " set(MICROCHIP_XC8_CLI xc8)\n" 80 | "To suppress this message when XC8 is not found but continue" 81 | " using the newer 'xc8-cc' driver, add this line to your" 82 | " CMakeLists.txt before the 'project' command:\n" 83 | " set(MICROCHIP_XC8_CLI xc8-cc)\n" 84 | "For more information on selecting a command-line driver" 85 | " see docs/xc8.md in your cmake-microchip installation." 86 | ) 87 | endif() 88 | 89 | message(FATAL_ERROR 90 | "The XC8 compiler executable ${MICROCHIP_XC8_CLI} was not found," 91 | " but what looks like an XC8 installation was found at:\n" 92 | " ${MICROCHIP_XC8_PATH}\n" 93 | "Please provide the path to a working XC8 installation on the" 94 | " command line, for example:\n" 95 | " cmake -DMICROCHIP_XC8_PATH=/opt/microchip/xc8/v2.00 .." 96 | ) 97 | endif() 98 | 99 | # skip compiler ID since XC8 isn't supported by CMake's test file 100 | set(CMAKE_C_COMPILER_ID_RUN 1) 101 | 102 | # call the compiler to check its version 103 | function(_xc8_get_version) 104 | execute_process( 105 | COMMAND "${CMAKE_C_COMPILER}" "${_xc8_version_flag}" 106 | OUTPUT_VARIABLE output 107 | ERROR_VARIABLE output 108 | RESULT_VARIABLE result 109 | ) 110 | 111 | if(result) 112 | message(FATAL_ERROR 113 | "Calling '${CMAKE_C_COMPILER} ${_xc8_version_flag}' failed." 114 | ) 115 | endif() 116 | 117 | if(output MATCHES "XC8 C Compiler V([0-9]+\.[0-9]+)") 118 | set(CMAKE_C_COMPILER_VERSION ${CMAKE_MATCH_1} PARENT_SCOPE) 119 | else() 120 | message(FATAL_ERROR 121 | "Failed to parse output of '${CMAKE_C_COMPILER} ${_xc8_version_flag}'." 122 | ) 123 | endif() 124 | endfunction() 125 | _xc8_get_version() 126 | -------------------------------------------------------------------------------- /Modules/Platform/MicrochipMCU-C.cmake: -------------------------------------------------------------------------------- 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_8") 18 | include(Platform/MicrochipMCU-C-XC8) 19 | elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "PIC_16") 20 | include(Platform/MicrochipMCU-C-XC16) 21 | elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "PIC_32") 22 | include(Platform/MicrochipMCU-C-XC32) 23 | else() 24 | message(FATAL_ERROR 25 | "No C compiler for '${CMAKE_SYSTEM_PROCESSOR}'" 26 | " is supported yet." 27 | ) 28 | endif() 29 | 30 | if(MICROCHIP_C_COMPILER_ID) 31 | message(STATUS 32 | "Using Microchip C compiler ${MICROCHIP_C_COMPILER_ID}" 33 | " ${MICROCHIP_C_COMPILER_VERSION}" 34 | ) 35 | endif() 36 | -------------------------------------------------------------------------------- /Modules/Platform/MicrochipMCU-GNU-C.cmake: -------------------------------------------------------------------------------- 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 after the compiler has been determined 15 | # to set up information specific to Microchip GNU C compilers 16 | 17 | # for XC16, inject properties that may have been missed 18 | # see `Platform/MicrochipMCU-C-XC16` for explanation 19 | if(MICROCHIP_C_COMPILER_ID STREQUAL "XC16") 20 | if(NOT CMAKE_C_COMPILE_FEATURES) 21 | set(CMAKE_C_COMPILE_FEATURES "c_function_prototypes;c_restrict;c_variadic_macros") 22 | set(CMAKE_C90_COMPILE_FEATURES "c_function_prototypes") 23 | set(CMAKE_C99_COMPILE_FEATURES "c_restrict;c_variadic_macros") 24 | set(CMAKE_C11_COMPILE_FEATURES "") 25 | endif() 26 | 27 | if(NOT CMAKE_C_SIZEOF_DATA_PTR) 28 | set(CMAKE_C_SIZEOF_DATA_PTR 2) 29 | endif() 30 | 31 | if(NOT CMAKE_C_COMPILER_ABI) 32 | set(CMAKE_C_COMPILER_ABI ELF) 33 | endif() 34 | endif() 35 | -------------------------------------------------------------------------------- /Modules/Platform/MicrochipMCU-Initialize.cmake: -------------------------------------------------------------------------------- 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. 16 | 17 | # ensure that only the cross toolchain is searched for 18 | # tools, libraries, include files, and other similar things 19 | set(CMAKE_FIND_ROOT_PATH "") 20 | set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH) 21 | set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 22 | set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 23 | set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) 24 | -------------------------------------------------------------------------------- /Modules/Platform/MicrochipMCU.cmake: -------------------------------------------------------------------------------- 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) 24 | 25 | include(MicrochipBin2Hex) 26 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ################################# 2 | CMake for the Microchip Toolchain 3 | ################################# 4 | 5 | This project provides toolchains and other support modules to enable 6 | using `CMake`_ with the `Microchip compilers`_, although presently only 7 | XC16 is supported. 8 | 9 | .. _CMake: https://cmake.org/ 10 | .. _Microchip compilers: http://www.microchip.com/mplab/compilers 11 | 12 | Usage 13 | ===== 14 | 15 | First, you need to somehow get a copy of this project as a subdirectory 16 | of your project named ``external/cmake-microchip``. If you use git, the 17 | easiest way is to add a submodule:: 18 | 19 | git submodule add git://github.com/Elemecca/cmake-microchip.git external/cmake-microchip 20 | 21 | Then add this snippet at the very top of your ``CMakeLists.txt``:: 22 | 23 | # set up the Microchip cross toolchain 24 | set(CMAKE_TOOLCHAIN_FILE external/cmake-microchip/toolchain.cmake) 25 | 26 | # set the default MCU model 27 | set(MICROCHIP_MCU PIC24FJ32GB002) 28 | 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:: 31 | 32 | cmake -DMICROCHIP_MCU=PIC24FJ256GB004 . 33 | 34 | Copying 35 | ======= 36 | 37 | This project is provided under the same BSD 3-Clause license as 38 | CMake itself. See ``COPYING.txt`` for details. 39 | -------------------------------------------------------------------------------- /docs/xc8.md: -------------------------------------------------------------------------------- 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. 57 | -------------------------------------------------------------------------------- /toolchain.cmake: -------------------------------------------------------------------------------- 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 | # `CMAKE_TOOLCHAIN_FILE` should be set to the path to this file in 15 | # CMakeLists.txt before the `project` command, for example: 16 | # set(CMAKE_TOOLCHAIN_FILE external/cmake-microchip/toolchain.cmake) 17 | # 18 | # doing so will trigger CMake's cross-compiling support 19 | # this module will be loaded during the system detection process 20 | # to provide information about the target platform 21 | 22 | 23 | # CMP0057 (IN_LIST operator) since 3.3 24 | cmake_minimum_required(VERSION 3.3) 25 | 26 | 27 | # record the directory containing this script 28 | # it will be used as the base for finding our other files 29 | set(MICROCHIP_ROOT ${CMAKE_CURRENT_LIST_DIR}) 30 | 31 | # add our modules to the search path 32 | list(APPEND CMAKE_MODULE_PATH "${MICROCHIP_ROOT}/Modules") 33 | 34 | 35 | # set the target platform 36 | # this is what causes CMake to call our modules during setup 37 | set(CMAKE_SYSTEM_NAME "MicrochipMCU") 38 | 39 | # set the default MCU model 40 | # handling the default this way produces this priority order: 41 | # 42 | # 1. the value already set in the cache, if one exists 43 | # e.g. from "-DMICROCHIP_MCU=..." on the command line 44 | # 45 | # 2. a non-cache value already set 46 | # e.g. with "set(MICROCHIP_MCU ...)" in CMakeLists.txt 47 | # 48 | # 3. the default value 49 | # 50 | # that makes it possible to set a per-project default model in 51 | # CMakeLists.txt that can still be overridden on the command line 52 | if(NOT MICROCHIP_MCU) 53 | set(MICROCHIP_MCU "generic16") 54 | endif() 55 | set(MICROCHIP_MCU "${MICROCHIP_MCU}" 56 | CACHE STRING "full model number of the target Microchip MCU" 57 | ) 58 | 59 | 60 | # known 8-bit MCU families 61 | list(APPEND MICROCHIP_FAMILIES_8 62 | PIC12F 63 | PIC16F 64 | PIC18F 65 | ) 66 | 67 | # known 16-bit MCU families 68 | list(APPEND MICROCHIP_FAMILIES_16 69 | dsPIC30F 70 | dsPIC33E 71 | dsPIC33F 72 | PIC24E 73 | PIC24F 74 | PIC24H 75 | ) 76 | 77 | # known 32-bit MCU families 78 | list(APPEND MICROCHIP_FAMILIES_32 79 | PIC32MX 80 | PIC32MZ 81 | ) 82 | 83 | 84 | # parse the MCU model 85 | if(MICROCHIP_MCU STREQUAL "generic8") 86 | set(MICROCHIP_MCU_FAMILY "generic") 87 | set(MICROCHIP_MCU_MODEL "generic") 88 | set(CMAKE_SYSTEM_PROCESSOR "PIC_8") 89 | 90 | elseif(MICROCHIP_MCU STREQUAL "generic16") 91 | set(MICROCHIP_MCU_FAMILY "generic") 92 | set(MICROCHIP_MCU_MODEL "p30sim") 93 | set(CMAKE_SYSTEM_PROCESSOR "PIC_16") 94 | 95 | elseif(MICROCHIP_MCU STREQUAL "generic32") 96 | set(MICROCHIP_MCU_FAMILY "generic") 97 | set(MICROCHIP_MCU_MODEL "generic") 98 | set(CMAKE_SYSTEM_PROCESSOR "PIC_32") 99 | 100 | elseif(MICROCHIP_MCU MATCHES "^(dsPIC|PIC)(32M[XZ]|[0-9]+[A-Z])([A-Z0-9]+)$") 101 | set(MICROCHIP_MCU_FAMILY "${CMAKE_MATCH_1}${CMAKE_MATCH_2}") 102 | set(MICROCHIP_MCU_MODEL "${CMAKE_MATCH_2}${CMAKE_MATCH_3}") 103 | 104 | if(MICROCHIP_MCU_FAMILY IN_LIST MICROCHIP_FAMILIES_8) 105 | set(CMAKE_SYSTEM_PROCESSOR "PIC_8") 106 | elseif(MICROCHIP_MCU_FAMILY IN_LIST MICROCHIP_FAMILIES_16) 107 | set(CMAKE_SYSTEM_PROCESSOR "PIC_16") 108 | elseif(MICROCHIP_MCU_FAMILY IN_LIST MICROCHIP_FAMILIES_32) 109 | set(CMAKE_SYSTEM_PROCESSOR "PIC_32") 110 | else() 111 | message(FATAL_ERROR 112 | "Unsupported MCU family '${MICROCHIP_MCU_FAMILY}'." 113 | ) 114 | endif() 115 | 116 | else() 117 | message(FATAL_ERROR 118 | "Invalid MICROCHIP_MCU value '${MICROCHIP_MCU}'." 119 | ) 120 | endif() 121 | --------------------------------------------------------------------------------