Keil MDK是非常常用的单片机开发集成环境,Keil公司2005年由ARM公司收购,现在是ARM主要的嵌入系统开发平台(ARM的另一个开发环境ds-5早在九年前就停止更新了)。 Keil虽然是个集成开发环境,但Keil本身其实是由μVision IDE和arm编译器构成。cmake虽然目前不支持生成μVision的工程文件,但cmake完全可以使用MDK中提供的arm编译器来实现独立于μVision的交叉编译(说到底μVision只是一个为开发者提供易用的GUI界面,真正干活儿的还是编译器)。 cmake实现交叉编译最重要的就是正确的定义编译工具链(toolchain),本文以Nationstech.N32G45X(国民技术)平台为例说明如何在定义cmake交叉编译工具链来实现使用MDK的armcc编译器执行单片系统的交叉编译。
N32G45X交叉编译
要求安装Arm Keil编译器(mdk525.rar)及N32G45X SDK(N32G452xx.rar)
并如下设置环境变量:
n32g45x_env.bat
@rem ------------------------------------------------------------
@rem set environment variable for N32G45X cross compile
@rem ------------------------------------------------------------
@set PATH=C:\Keil_v5\ARM\ARMCC\bin;%PATH%
@set N32G45X_SDK_PREFIX=E:\mdk\Nationstech.N32G45x_Library.1.1.1
- N32G452xx使用ARM Keil编译器,C:\Keil_v5为Keil 编译器的安装位置
- N32G45X_SDK_PREFIX为 N32G45X SDK安装位置,这个很重要,因为后面的交叉编译脚本中(cmake/n32g45x.toolchain.cmake)会用到这个环境变量
cmake的交叉编译工具链文件定义如下: n32g45x.toolchain.cmake
# toolchain for Nationstech.N32G45X
INCLUDE(CMakeForceCompiler)
# This one is important
SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_PROCESSOR arm)
# Keil armcc编译器安装默认位置
set(_compiler_prefix "C:/Keil_v5/ARM/ARMCC")
if(NOT EXISTS ${_compiler_prefix})
if(NOT $ENV{CROSS_COMPILER_PREFIX} STREQUAL "")
set(_compiler_prefix $ENV{CROSS_COMPILER_PREFIX})
elseif(CROSS_COMPILER_PREFIX)
set(_compiler_prefix ${CROSS_COMPILER_PREFIX})
else()
find_program(_armcc_path armcc)
if(NOT _armcc_path)
message(FATAL_ERROR "NOT FOUND compiler armcc in system path")
endif()
get_filename_component(_bin ${_armcc_path} DIRECTORY )
get_filename_component(_compiler_prefix ${_bin} DIRECTORY )
endif()
endif()
set(_suffix)
if(WIN32)
set(_suffix .exe)
endif()
# 要求环境变量定义 N32G45X_SDK_PREFIX 以指定 N32G45X SDK的安装位置
# 或由cmake变量N32G45X_SDK_PREFIX 定义
if(NOT $ENV{N32G45X_SDK_PREFIX} STREQUAL "")
set(N32G45X_SDK_PREFIX $ENV{N32G45X_SDK_PREFIX})
endif()
if(NOT N32G45X_SDK_PREFIX)
message(FATAL_ERROR "NOT DEFINE N32G45X SDK PREFIX FOR CROSS COMIPILER")
endif()
# Specify the cross compiler
# 定义编译器名字
SET(CMAKE_C_COMPILER "${_compiler_prefix}/bin/armcc${_suffix}")
SET(CMAKE_CXX_COMPILER "${_compiler_prefix}/bin/armcc${_suffix}")
SET(CMAKE_AR "${_compiler_prefix}/bin/armar${_suffix}" CACHE FILEPATH "Archiver")
# 参考N32G45X SDK 工程文件(.uvprojx)中的编译选项定义CMAKE_C_FLAGS_INIT,CMAKE_CXX_FLAGS_INIT
UNSET(CMAKE_C_FLAGS_INIT CACHE)
SET(CMAKE_C_FLAGS_INIT "--c99 --cpu=Cortex-M4.fp --apcs=interwork --split_sections -DN32G45X -DUSE_STDPERIPH_DRIVER" CACHE STRING "" FORCE)
UNSET(CMAKE_CXX_FLAGS_INIT CACHE)
SET(CMAKE_CXX_FLAGS_INIT "--cpu=Cortex-M4.fp --apcs=interwork --split_sections -DN32G45X -DUSE_STDPERIPH_DRIVER" CACHE STRING "" FORCE)
UNSET(CMAKE_EXE_LINKER_FLAGS_INIT CACHE)
SET(CMAKE_EXE_LINKER_FLAGS_INIT "" CACHE STRING "" FORCE)
UNSET(CMAKE_AR_FLAGS CACHE)
SET(CMAKE_AR_FLAGS "-p -armcc,-Ospace" CACHE STRING "" FORCE)
# set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> cr <TARGET> <LINK_FLAGS> <OBJECTS>")
SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> ${CMAKE_AR_FLAGS} -o <TARGET> <OBJECTS>" CACHE STRING "C Archive Create")
# set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> cr <TARGET> <LINK_FLAGS> <OBJECTS>")
SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> ${CMAKE_AR_FLAGS} -o <TARGET> <OBJECTS>" CACHE STRING "CXX Archive Create")
# Where is the target environment
SET(CMAKE_FIND_ROOT_PATH "${_compiler_prefix}")
# 将 N32G45X SDK 的header文件夹添加到include搜索路径
include_directories(
"${N32G45X_SDK_PREFIX}/firmware/CMSIS/core/"
"${N32G45X_SDK_PREFIX}/firmware/CMSIS/device"
"${N32G45X_SDK_PREFIX}/firmware/n32g45x_algo_lib/inc"
"${N32G45X_SDK_PREFIX}/firmware/n32g45x_std_periph_driver/inc"
"${N32G45X_SDK_PREFIX}/firmware/n32g45x_usbfs_driver/inc"
"${N32G45X_SDK_PREFIX}/projects/n32g45x_EVAL/bsp/inc"
)
# Search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# For libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
unset(_compiler_prefix)
**NOTE:**在我的项目中只是用这个工具链文件生成静态库提供给客户调用,这个工具链文件中编译选项部分是可以正常执行的,链接选项并没有验证是否正确。
|