I'm trying to write my own CMake build system for the STM32F407 on Windows. Below is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(test_1 LANGUAGES C)
#Set the path if arm-none-eabi's bin folder is not added in the SYSTEM path environment variable
set(CMAKE_TOOLCHAIN_FILE "toolchain_stm32f407.cmake")
set_property(SOURCE startup_stm32f407xx.s PROPERTY LANGUAGE C)
set(INCLUDE_DIRECTORIES ./inc)
include_directories(${INCLUDE_DIRECTORIES})
set(SOURCE_FILES ./src/main.c)
add_executable(${CMAKE_PROJECT_NAME}.elf ${SOURCE_FILES})
add_custom_target(TARGET ${CMAKE_PROJECT_NAME}.elf POST_BUILD
COMMAND ${CMAKE_SIZE} --format=berkeley "${CMAKE_PROJECT_NAME}.elf")
I have 2 toolchain files. One works properly and another one gives me an error. Erroneous toolchain file:
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR ARM)
option(LINUX OFF)
#Set the path if arm-none-eabi's bin folder is not added in the SYSTEM path environment variable
if(LINUX)
set(ARM_TOOLCHAIN_PATH "./gcc-arm-none-eabi-9-2019-q4-major/bin")
else(LINUX)
set(ARM_TOOLCHAIN_PATH "D:/gcc-arm-none-eabi-7-2018-q2-update/bin")
set(EXE ".exe")
endif(LINUX)
set(LINKER_PATH "./STM32F407VGTx_FLASH.ld")
option(DEBUG ON)
set(CMAKE_C_COMPILER_WORKS ON)
set(CMAKE_CXX_COMPILER_WORKS ON)
set(CMAKE_C_COMPILER ${ARM_TOOLCHAIN_PATH}/arm-none-eabi-gcc${EXE})
set(CMAKE_CXX_COMPILER ${ARM_TOOLCHAIN_PATH}/arm-none-eabi-g++.exe${EXE})
set(CMAKE_ASM_COMPILER ${ARM_TOOLCHAIN_PATH}/arm-none-eabi-as.exe${EXE})
set(CMAKE_OBJCOPY ${ARM_TOOLCHAIN_PATH}/arm-none-eabi-objcopy${EXE})
if(DEBUG)
set(DEBUG_FLAG "-g")
endif(DEBUG)
set(COMMON_FLAGS "-mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mthumb-interwork ${DEBUG_FLAG} \
-mcpu=cortex-m4 -march=armv7e-m \
-Wall -fdata-sections -ffunction-section")
#Should these flags be added?: -Wall -Wextra -Wimplicit-function-declaration -Wredundant-decls -Wstrict-prototypes
#-Wundef -Wshadow
set(CMAKE_C_FLAGS "${COMMON_FLAGS} -std=gnu99 --specs=nosys.specs")
set(CMAKE_CXX_FLAGS "${COMMON_FLAGS} -std=c++11 --specs=nosys.specs")
set(CMAKE_EXE_LINKER_FLAGS "${COMMON_FLAGS} -Wl,-map=linker.map -Wl,--gc-sections" CACHE INTERNAL "")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
Error:
>cmake -H. -B_build -DCMAKE_TOOLCHAIN_FILE:PATH="toolchain_stm3
2f407.cmake"
-- The C compiler identification is unknown
CMake Error at CMakeLists.txt:3 (project):
The CMAKE_C_COMPILER:
D:/gcc-arm-none-eabi-7-2018-q2-update/bin/arm-none-eabi-gcc.exe
is not a full path and was not found in the PATH.
Tell CMake where to find the compiler by setting either the environment
variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
Properly working toolchain:(Got the toolchain file from this question:Link)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER "arm-none-eabi-gcc.exe")
set(CMAKE_CXX_COMPILER "arm-none-eabi-g++.exe")
set(CMAKE_EXE_LINKER_FLAGS "--specs=nosys.specs" CACHE INTERNAL "")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
My questions are:
- I'm unable to understand what is lacking in the erroneous toolchain file which is causing that error.
- In CMakeLists.txt, I have set CMAKE_TOOLCHAIN_FILE to the toolchain file. But the toolchain file is not detected unless I pass it as a CLI option using "-DCMAKE_TOOLCHAIN_FILE:PATH=". What is causing this?
- Why does only CMAKE_EXE_LINKER_FLAGS need to be cached?
Thanks