0

Windows 10, CMake 3.19.1

Im trying to compile test project with XC8 compiler, CMake and custom toolchain (third party). Link: Toolchain repository

Toolchains quick guide says to only add two strings at top of my CMakeLists.txt. So ive got the next CMakeLists.txt:

   project(Test)

   # set up the Microchip cross toolchain
   set(CMAKE_TOOLCHAIN_FILE ./external/cmake-microchip/toolchain.cmake)

   # set the default MCU model
   set(MICROCHIP_MCU PIC18F97J60)

   add_executable(main main.c)

But occasionaly, every time CMake generation output starts with:

-- Building for: Visual Studio 16 2019
-- The C compiler identification is MSVC 19.28.29333.0
-- The CXX compiler identification is MSVC 19.28.29333.0
..... more

And i havent any Makefile in the output folder. Also im try to run CMake with -G "Unix makefiles". And makefile been generated, with wrong output and any trace of custom toolchain use. Output been:

-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Detecting C compiler ABI info

Every CMake generation try, im cleanup the output folder. Why custom toolchain doesnt starts?

  • Two things you can try. One is change file path to absolute path instead of relative path. Another one is remove that line and instead pass it in on the command line with `cmake -D CMAKE_TOOLCHAIN_FILE=/abs/path/to/toolchain/file .....`. See if either works. – TerryTsao Nov 28 '20 at 04:39
  • 1
    Not sure, if specifying the toolchain file in the `CMakeLists.txt` file works, but it's certainly too late, if you're doing it after the first `project` command encountered since the compiler is chosen at that time and any dependent variables are initialized... – fabian Nov 28 '20 at 07:23
  • @fabian I think you are onto sth. That toolchain file is absolute a frigging mess. For God's sake, who would put `cmake_minimum_required` inside a toolchain file??!! – TerryTsao Nov 28 '20 at 09:27

1 Answers1

2

The variable CMAKE_TOOLCHAIN_FILE should be set before the first project() call:

cmake_minimum_required(VERSION <..>)

# set up the Microchip cross toolchain
set(CMAKE_TOOLCHAIN_FILE ./external/cmake-microchip/toolchain.cmake)

project(Test)

# set the default MCU model
set(MICROCHIP_MCU PIC18F97J60)

add_executable(main main.c)

When first project() call is processed, CMake automatically calls the script specified in CMAKE_TOOLCHAIN_FILE.

Note, that preferred way is to not hardcode path to the toolchain in the CMakeLists.txt but pass -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain option to the cmake.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153