Msys2构建CMake工程

更新时间:
2024-02-01
下载文档

Msys2构建CMake工程

准备工作

  • 下载zlib开源工程
  • 安装MSYS2
  • 安装CMake

配置CMake

CMake提供了CMAKE_TOOLCHAIN_FILE变量,通过编写CMAKE_TOOLCHAIN_FILE文件,可以达到交叉编译的目的。接下来还是以zlib为例,讲解如何使用CMake构建编译SylixOS工程。

具体操作步骤如下所示。

  1. 打开MSYS2终端,使用 cd /e /appdowmload/zlib-1.2.13 命令进入zlib源码目录下。
  2. 在该路径下创建一个名为 sylixos.toolchain.cmake 的文件,并将以下代码内容全部复制到该文件中保存。
#*********************************************************************************************************
#
#                                    中国软件开源组织
#
#                                   嵌入式实时操作系统
#
#                                SylixOS(TM)  LW : long wing
#
#                               Copyright All Rights Reserved
#
#--------------文件信息--------------------------------------------------------------------------------
#
# 文   件   名: sylixos.toolchain.cmake
#
# 创   建   人: lijiaming(李佳明)
#
# 文件创建日期: 2022 年 06 月 03 日
#
# 描        述: SylixOS cmake toolchain file
#*********************************************************************************************************

set(CMAKE_SYSTEM_NAME SylixOS)
set(SYLIXOS ON)
set(ENV{SYLIXOS} ON)

#*********************************************************************************************************
# config.mk
#*********************************************************************************************************
if(CMAKE_SYLIXOS_BASE_PATH)
string(REPLACE "\\" "/" result "${CMAKE_SYLIXOS_BASE_PATH}")
set(CMAKE_SYLIXOS_BASE_PATH ${result})
set(ENV{SYLIXOS_BASE_PATH} "${CMAKE_SYLIXOS_BASE_PATH}")
else()
return()
endif()

set(SYLIXOS_BASE_PATH $ENV{SYLIXOS_BASE_PATH})
message(STATUS "SylixOS Base Path: ${SYLIXOS_BASE_PATH}")

if(NOT EXISTS ${SYLIXOS_BASE_PATH})
message(FATAL_ERROR "${SYLIXOS_BASE_PATH} not found.")
endif()

if(SYLIXOS_BASE_PATH)
file(STRINGS "${SYLIXOS_BASE_PATH}/config.mk" MK_STRINGS REGEX "^MULTI_PLATFORM_BUILD := .*")
string(REPLACE "MULTI_PLATFORM_BUILD := " "" MULTI_PLATFORM_BUILD "${MK_STRINGS}")

message(STATUS "MULTI_PLATFORM_BUILD := ${MULTI_PLATFORM_BUILD}")
if (MULTI_PLATFORM_BUILD STREQUAL "yes")
  message(FATAL_ERROR "${SYLIXOS_BASE_PATH} is multi platform build. CMake not support.")
else ()
  file(STRINGS "${SYLIXOS_BASE_PATH}/config.mk" MK_STRINGS REGEX "^TOOLCHAIN_PREFIX := .*")
  string(REPLACE "TOOLCHAIN_PREFIX := " "" TOOLCHAIN_PREFIX "${MK_STRINGS}")

  file(STRINGS "${SYLIXOS_BASE_PATH}/config.mk" MK_STRINGS REGEX "^FPU_TYPE := .*")
  string(REPLACE "FPU_TYPE := " "" FPU_TYPE "${MK_STRINGS}")

  file(STRINGS "${SYLIXOS_BASE_PATH}/config.mk" MK_STRINGS REGEX "^CPU_TYPE := .*")
  string(REPLACE "CPU_TYPE := " "" CPU_TYPE "${MK_STRINGS}")

  file(STRINGS "${SYLIXOS_BASE_PATH}/config.mk" MK_STRINGS REGEX "^DEBUG_LEVEL := .*")
  string(REPLACE "DEBUG_LEVEL := " "" DEBUG_LEVEL "${MK_STRINGS}")
  endif()
endif()

message(STATUS "TOOLCHAIN_PREFIX := ${TOOLCHAIN_PREFIX}")
message(STATUS "FPU_TYPE := ${FPU_TYPE}")
message(STATUS "CPU_TYPE := ${CPU_TYPE}")

#*********************************************************************************************************
# target.mk
#*********************************************************************************************************
set(LOCAL_INC_PATH         "")
set(LOCAL_DSYMBOL          "")
set(LOCAL_CFLAGS           "")
set(LOCAL_CXXFLAGS         "")
set(LOCAL_LINKFLAGS        "")
set(LOCAL_DEPEND_LIB       "")
set(LOCAL_DEPEND_LIB_PATH  "")
set(LOCAL_USE_CXX          "yes")
set(LOCAL_USE_CXX_EXCEPT   "no")
set(LOCAL_USE_GCOV         "no")
set(LOCAL_USE_OMP          "no")
set(LOCAL_NO_UNDEF_SYM     "no")

if(FPU_TYPE AND NOT(FPU_TYPE MATCHES "disable") AND NOT(FPU_TYPE MATCHES "default"))
  if((FPU_TYPE MATCHES "hard-float") OR (FPU_TYPE MATCHES "soft-float") OR (FPU_TYPE MATCHES "double-float") OR (FPU_TYPE MATCHES "float-abi=soft") OR (FPU_TYPE MATCHES "float-abi=hard"))
    set(ARCH_FPUFLAGS       "-m${FPU_TYPE}")
  elseif(FPU_TYPE MATCHES "softfp")
    set(ARCH_FPUFLAGS "-mfloat-abi=softfp")
  else()
    set(ARCH_FPUFLAGS "-mfloat-abi=softfp -mfpu=${FPU_TYPE}")
  endif()
endif()

#*********************************************************************************************************
# arch.mk
#*********************************************************************************************************

#*********************************************************************************************************
# i386
#*********************************************************************************************************
if(TOOLCHAIN_PREFIX STREQUAL i386-sylixos-elf-)
set(CMAKE_SYSTEM_PROCESSOR "x86")
set(ARCH_COMMONFLAGS    "-mlong-double-64 -fno-omit-frame-pointer -fno-strict-aliasing")

set(ARCH_PIC_ASFLAGS    "")
set(ARCH_PIC_CFLAGS     "-fPIC")
set(ARCH_PIC_LDFLAGS    "-Wl,-shared -fPIC -shared")

set(ARCH_KO_CFLAGS      "")
set(ARCH_KO_LDFLAGS     "-nostdlib -r")

set(ARCH_KLIB_CFLAGS    "")

set(ARCH_KERNEL_CFLAGS  "")
set(ARCH_KERNEL_LDFLAGS "")

set(ARCH_CPUFLAGS       "-march=${CPU_TYPE} ${ARCH_FPUFLAGS}")
endif()

#*********************************************************************************************************
# x86-64
#*********************************************************************************************************
if(TOOLCHAIN_PREFIX STREQUAL x86_64-sylixos-elf-)
set(CMAKE_SYSTEM_PROCESSOR "x86_64")

set(ARCH_COMMONFLAGS    "-mlong-double-64 -mno-red-zone -fno-omit-frame-pointer -fno-strict-aliasing")

set(ARCH_PIC_ASFLAGS    "")
set(ARCH_PIC_CFLAGS     "-fPIC")
set(ARCH_PIC_LDFLAGS    "-Wl,-shared -fPIC -shared")

set(ARCH_KO_CFLAGS      "-mcmodel=large")
set(ARCH_KO_LDFLAGS     "-nostdlib -r")

set(ARCH_KLIB_CFLAGS    "-mcmodel=large")

set(ARCH_KERNEL_CFLAGS  "-mcmodel=kernel")
set(ARCH_KERNEL_LDFLAGS "-z max-page-size=4096")

if(CPU_TYPE AND NOT(FPU_TYPE MATCHES "generic"))
set(ARCH_CPUFLAGS       "-march=${CPU_TYPE} -m64 ${ARCH_FPUFLAGS}")
else()
set(ARCH_CPUFLAGS       "-m64 ${ARCH_FPUFLAGS}")
endif()

endif()

#*********************************************************************************************************
# ARM
#*********************************************************************************************************
if(TOOLCHAIN_PREFIX STREQUAL arm-sylixos-eabi-)
set(CMAKE_SYSTEM_PROCESSOR "arm")

set(ARCH_COMMONFLAGS    "-mno-unaligned-access -fno-strict-aliasing")

set(ARCH_PIC_ASFLAGS    "")
set(ARCH_PIC_CFLAGS     "-fPIC")
set(ARCH_PIC_LDFLAGS    "-nostdlib -Wl,-shared -fPIC -shared")

set(ARCH_KO_CFLAGS      "")
set(ARCH_KO_LDFLAGS     "-nostdlib -r")

set(ARCH_KLIB_CFLAGS    "")

set(ARCH_KERNEL_CFLAGS  "")
set(ARCH_KERNEL_LDFLAGS "")
set(ARCH_CPUFLAGS       "-mcpu=${CPU_TYPE} ${ARCH_FPUFLAGS}")
endif()

#*********************************************************************************************************
# aarch64
#*********************************************************************************************************
if(TOOLCHAIN_PREFIX STREQUAL aarch64-sylixos-elf-)
set(CMAKE_SYSTEM_PROCESSOR "aarch64")

set(ARCH_COMMONFLAGS    "-fno-omit-frame-pointer -mstrict-align -ffixed-x18 -fno-strict-aliasing")

set(ARCH_PIC_ASFLAGS    "")
set(ARCH_PIC_CFLAGS     "-fPIC")
set(ARCH_PIC_LDFLAGS    "-Wl,-shared -fPIC -shared")

set(ARCH_KO_CFLAGS      "-mcmodel=large -mgeneral-regs-only")
set(ARCH_KO_LDFLAGS     "-nostdlib -r")

set(ARCH_KLIB_CFLAGS    "")

set(ARCH_KERNEL_CFLAGS  "-mgeneral-regs-only")
set(ARCH_KERNEL_LDFLAGS "-Wl,--build-id=none")
set(ARCH_CPUFLAGS       "-mcpu=${CPU_TYPE} ${ARCH_FPUFLAGS}")

endif()

#*********************************************************************************************************
# MIPS
#*********************************************************************************************************
if(TOOLCHAIN_PREFIX STREQUAL mips-sylixos-elf-)
set(CMAKE_SYSTEM_PROCESSOR "mips")

set(ARCH_COMMONFLAGS    "-fno-strict-aliasing")

set(ARCH_PIC_ASFLAGS    "")
set(ARCH_PIC_CFLAGS     "-fPIC -mabicalls")
set(ARCH_PIC_LDFLAGS    "-mabicalls -Wl,-shared -fPIC -shared")

set(ARCH_KO_CFLAGS      "-mlong-calls")
set(ARCH_KO_LDFLAGS     "-nostdlib -r")

set(ARCH_KLIB_CFLAGS    "-mlong-calls")

set(ARCH_KERNEL_CFLAGS  "")
set(ARCH_KERNEL_LDFLAGS "")

set(ARCH_CPUFLAGS       "-march=${CPU_TYPE} -EL -G 0 -mno-branch-likely ${ARCH_FPUFLAGS}")
endif()

#*********************************************************************************************************
# MIPS64
#*********************************************************************************************************
if(TOOLCHAIN_PREFIX STREQUAL mips64-sylixos-elf-)
set(CMAKE_SYSTEM_PROCESSOR "mips64")

set(ARCH_COMMONFLAGS    "-fno-strict-aliasing")

set(ARCH_PIC_ASFLAGS    "")
set(ARCH_PIC_CFLAGS     "-fPIC -mabicalls")
set(ARCH_PIC_LDFLAGS    "-mabicalls -Wl,-shared -fPIC -shared")

set(ARCH_KO_CFLAGS      "-mlong-calls")
set(ARCH_KO_LDFLAGS     "-nostdlib -r")

set(ARCH_KLIB_CFLAGS    "-mlong-calls")

set(ARCH_KERNEL_CFLAGS  "")
set(ARCH_KERNEL_LDFLAGS "")

set(ARCH_CPUFLAGS       "-march=${CPU_TYPE} -EL -G 0 -mno-branch-likely -mabi=64 ${ARCH_FPUFLAGS}")
endif()

#*********************************************************************************************************
# PowerPC
#*********************************************************************************************************
if(TOOLCHAIN_PREFIX STREQUAL ppc-sylixos-eabi-)
set(CMAKE_SYSTEM_PROCESSOR "powerpc")

set(ARCH_COMMONFLAGS    "-G 0 -mstrict-align -fno-strict-aliasing")

set(ARCH_PIC_ASFLAGS    "")
set(ARCH_PIC_CFLAGS     "-fPIC")
set(ARCH_PIC_LDFLAGS    "-Wl,-shared -fPIC -shared")

set(ARCH_KO_CFLAGS      "")
set(ARCH_KO_LDFLAGS     "-nostdlib -r")

set(ARCH_KLIB_CFLAGS    "")

set(ARCH_KERNEL_CFLAGS  "")
set(ARCH_KERNEL_LDFLAGS "")
set(ARCH_CPUFLAGS       "-mcpu=${CPU_TYPE} ${ARCH_FPUFLAGS}")
endif()

#*********************************************************************************************************
# SPARC
#*********************************************************************************************************
if(TOOLCHAIN_PREFIX STREQUAL sparc-sylixos-elf-)
set(CMAKE_SYSTEM_PROCESSOR "sparc")

set(ARCH_COMMONFLAGS    "-fno-strict-aliasing")

set(ARCH_PIC_ASFLAGS    "")
set(ARCH_PIC_CFLAGS     "-fPIC")
set(ARCH_PIC_LDFLAGS    "-Wl,-shared -fPIC -shared")

set(ARCH_KO_CFLAGS      "")
set(ARCH_KO_LDFLAGS     "-nostdlib -r")

set(ARCH_KLIB_CFLAGS    "")

set(ARCH_KERNEL_CFLAGS  "")
set(ARCH_KERNEL_LDFLAGS "")
set(ARCH_CPUFLAGS       "-mcpu=${CPU_TYPE} ${ARCH_FPUFLAGS}")
endif()

#*********************************************************************************************************
# RISC-V
#*********************************************************************************************************
if(TOOLCHAIN_PREFIX STREQUAL riscv-sylixos-elf-)
set(CMAKE_SYSTEM_PROCESSOR "riscv")

set(ARCH_COMMONFLAGS    "-mstrict-align -mcmodel=medany -mno-save-restore -fno-strict-aliasing")

set(ARCH_PIC_ASFLAGS    "")
set(ARCH_PIC_CFLAGS     "-fPIC")
set(ARCH_PIC_LDFLAGS    "-Wl,-shared -fPIC -shared")

set(ARCH_KO_CFLAGS      "-fPIC")
set(ARCH_KO_LDFLAGS     "-r -fPIC")

set(ARCH_KLIB_CFLAGS    "")

set(ARCH_KERNEL_CFLAGS  "")
set(ARCH_KERNEL_LDFLAGS "")
set(ARCH_CPUFLAGS       "-march=${CPU_TYPE} -mabi=${FPU_TYPE}")
endif()

#*********************************************************************************************************
# C-SKY
#*********************************************************************************************************
if(TOOLCHAIN_PREFIX STREQUAL csky-sylixos-elfabiv2-)
set(CMAKE_SYSTEM_PROCESSOR "csky")

set(ARCH_COMMONFLAGS    "-fno-omit-frame-pointer -fno-strict-aliasing")

set(ARCH_PIC_ASFLAGS    "")
set(ARCH_PIC_CFLAGS     "-fPIC")
set(ARCH_PIC_LDFLAGS    "-Wl,-shared -fPIC -Wl,-z,max-page-size=65536")

set(ARCH_KO_CFLAGS      "-Wa,-mno-force2bsr")
set(ARCH_KO_LDFLAGS     "-nostdlib -r -Wl,-z,max-page-size=65536")

set(ARCH_KLIB_CFLAGS    "-Wa,-mno-force2bsr")

set(ARCH_KERNEL_CFLAGS  "")
set(ARCH_KERNEL_LDFLAGS "-Wl,-z,max-page-size=65536")
set(ARCH_CPUFLAGS       "-march=${CPU_TYPE} ${ARCH_FPUFLAGS}")
endif()

#*********************************************************************************************************
# LoongARCH64
#*********************************************************************************************************
if(TOOLCHAIN_PREFIX STREQUAL loongarch64-sylixos-elf-)
set(CMAKE_SYSTEM_PROCESSOR "loongarch64")

set(ARCH_COMMONFLAGS    "-mstrict-align")

set(ARCH_PIC_ASFLAGS    "")
set(ARCH_PIC_CFLAGS     "-fPIC")
set(ARCH_PIC_LDFLAGS    "-Wl,-shared -fPIC -shared -Wl,-z,max-page-size=65536")

set(ARCH_KO_CFLAGS      "-Wa,-mla-global-with-abs,-mla-local-with-abs")
set(ARCH_KO_LDFLAGS     "-nostdlib -r -Wl,-z,max-page-size=65536")

set(ARCH_KLIB_CFLAGS    "")

set(ARCH_KERNEL_CFLAGS  "-Wa,-mla-global-with-pcrel")
set(ARCH_KERNEL_LDFLAGS "-Wl,-z,max-page-size=65536")
set(ARCH_CPUFLAGS       "-march=${CPU_TYPE} -G 0 -mabi=lp64d ${ARCH_FPUFLAGS}")
endif()

#*********************************************************************************************************
# gcc.mk
#*********************************************************************************************************

#*********************************************************************************************************
# specify cross compilers and tools
#*********************************************************************************************************
set(CMAKE_C_COMPILER_WORKS   1)
set(CMAKE_CXX_COMPILER_WORKS 1)

set(CMAKE_C_COMPILER    ${TOOLCHAIN_PREFIX}gcc CACHE PATH "SylixOS Base Path")
set(CMAKE_CXX_COMPILER  ${TOOLCHAIN_PREFIX}g++ CACHE PATH "SylixOS Base Path")
set(CMAKE_ASM_COMPILER  ${TOOLCHAIN_PREFIX}gcc CACHE PATH "SylixOS Base Path")
set(CMAKE_LINKER        ${TOOLCHAIN_PREFIX}g++ CACHE PATH "SylixOS Base Path")
set(CMAKE_OBJCOPY       ${TOOLCHAIN_PREFIX}objcopy CACHE PATH "SylixOS Base Path")
set(CMAKE_OBJDUMP       ${TOOLCHAIN_PREFIX}objdump CACHE PATH "SylixOS Base Path")
set(CMAKE_AR            ${TOOLCHAIN_PREFIX}ar CACHE PATH "SylixOS Base Path")
set(CMAKE_SIZE          ${TOOLCHAIN_PREFIX}size CACHE PATH "SylixOS Base Path")
set(CMAKE_RANLIB        ${TOOLCHAIN_PREFIX}ranlib CACHE PATH "SylixOS Base Path")

#*********************************************************************************************************
# Compiler optimize flag
#*********************************************************************************************************
set(TOOLCHAIN_OPTIMIZE_DEBUG   "-O0 -g3 -gdwarf-2")
set(TOOLCHAIN_OPTIMIZE_RELEASE "-O2 -g3 -gdwarf-2")

#*********************************************************************************************************
# Toolchain flag
#*********************************************************************************************************
set(TOOLCHAIN_CXX_CFLAGS           "")
set(TOOLCHAIN_CXX_EXCEPT_CFLAGS    "${TOOLCHAIN_CXX_CFLAGS} -fexceptions -frtti")
set(TOOLCHAIN_NO_CXX_EXCEPT_CFLAGS "${TOOLCHAIN_CXX_CFLAGS} -fno-exceptions -fno-rtti")
set(TOOLCHAIN_GCOV_CFLAGS          "-fprofile-arcs -ftest-coverage")
set(TOOLCHAIN_OMP_FLAGS            "-fopenmp")
set(TOOLCHAIN_COMMONFLAGS          "-Wall -fmessage-length=0 -fsigned-char -fno-short-enums")
set(TOOLCHAIN_ASFLAGS              "-x assembler-with-cpp")
set(TOOLCHAIN_NO_UNDEF_SYM_FLAGS   "@${SYLIXOS_BASE_PATH}/libsylixos/${CMAKE_BUILD_TYPE}/symbol.ld")
set(TOOLCHAIN_AR_FLAGS             "-r")
set(TOOLCHAIN_STRIP_FLAGS          "")
set(TOOLCHAIN_STRIP_KO_FLAGS       "--strip-unneeded")

#*********************************************************************************************************
# Application.mk
#*********************************************************************************************************

#*********************************************************************************************************
# Include paths
#*********************************************************************************************************
set(TARGET_INC_PATH           "${LOCAL_INC_PATH} -I${SYLIXOS_BASE_PATH}/libsylixos/SylixOS -I${SYLIXOS_BASE_PATH}/libsylixos/SylixOS/include -I${SYLIXOS_BASE_PATH}/libsylixos/SylixOS/include/network")

#*********************************************************************************************************
# Compiler preprocess
#*********************************************************************************************************
set(TARGET_DSYMBOL            "-DSYLIXOS ${LOCAL_DSYMBOL}")

#*********************************************************************************************************
# Compiler flags
#*********************************************************************************************************
set(TARGET_CFLAGS             "${LOCAL_CFLAGS}")
set(TARGET_CXXFLAGS           "${LOCAL_CXXFLAGS}")
set(TARGET_LINKFLAGS          "${SYLIXOS_BASE_PATH}/libsylixos/Release;${SYLIXOS_BASE_PATH}/libsylixos/Debug;${SYLIXOS_BASE_PATH}/libcextern/Release;${SYLIXOS_BASE_PATH}/libcextern/Debug")

if(LOCAL_USE_CXX STREQUAL yes)
set(TARGET_LIBRARIES            "cextern;vpmpdm;stdc++;dsohandle;m;gcc")
else()
set(TARGET_LIBRARIES            "cextern;vpmpdm;m;gcc")
endif()

#*********************************************************************************************************
# Depend and compiler parameter (cplusplus in kernel MUST NOT use exceptions and rtti)
#*********************************************************************************************************
if(LOCAL_USE_GCOV STREQUAL yes)
set(TARGET_GCOV_FLAGS         "${TOOLCHAIN_GCOV_CFLAGS}")
else()
set(TARGET_GCOV_FLAGS         "")
endif()

if(LOCAL_USE_OMP STREQUAL yes)
set(TARGET_OMP_FLAGS          "${TOOLCHAIN_OMP_FLAGS}")
else()
set(TARGET_OMP_FLAGS          "")
endif()

if(LOCAL_NO_UNDEF_SYM STREQUAL yes)
set(TARGET_NO_UNDEF_SYM_FLAGS "${TOOLCHAIN_NO_UNDEF_SYM_FLAGS}")
else()
set(TARGET_NO_UNDEF_SYM_FLAGS "")
endif()

if(LOCAL_USE_CXX_EXCEPT STREQUAL yes)
set(TARGET_CXX_EXCEPT ${TOOLCHAIN_CXX_EXCEPT_CFLAGS})
else()
set(TARGET_CXX_EXCEPT ${TOOLCHAIN_NO_CXX_EXCEPT_CFLAGS})
endif()

set(TARGET_CPUFLAGS           "${ARCH_CPUFLAGS}")
set(TARGET_COMMONFLAGS        "${TARGET_CPUFLAGS} ${ARCH_COMMONFLAGS} ${TOOLCHAIN_COMMONFLAGS} ${TARGET_GCOV_FLAGS} ${TARGET_OMP_FLAGS}")

set(CMAKE_THREAD_LIBS_INIT "")
set(CMAKE_DL_LIBS "")
set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,")
set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP ":")
set(CMAKE_SHARED_LIBRARY_RPATH_ORIGIN_TOKEN "\$ORIGIN")
set(CMAKE_SHARED_LIBRARY_RPATH_LINK_C_FLAG "-Wl,-rpath-link,")
set(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG "-Wl,-soname,")
set(CMAKE_EXE_EXPORTS_C_FLAG "-Wl,--export-dynamic")

set(CMAKE_ASM_FLAGS           "${TARGET_INC_PATH} ${TARGET_COMMONFLAGS} ${ARCH_PIC_ASFLAGS} ${TOOLCHAIN_ASFLAGS} ${TARGET_DSYMBOL}" CACHE INTERNAL "ASM Compiler options")
set(CMAKE_C_FLAGS             "${TARGET_INC_PATH} ${TARGET_COMMONFLAGS} ${ARCH_PIC_CFLAGS} ${TARGET_DSYMBOL} ${TARGET_CFLAGS}" CACHE INTERNAL "C Compiler options")
set(CMAKE_CXX_FLAGS           "${TARGET_INC_PATH} ${TARGET_COMMONFLAGS} ${ARCH_PIC_CFLAGS} ${TARGET_DSYMBOL} ${TARGET_CXX_EXCEPT} ${TARGET_CXXFLAGS}" CACHE INTERNAL "C++ Compiler options")
set(CMAKE_EXE_LINKER_FLAGS    "${ARCH_PIC_LDFLAGS} ${TARGET_NO_UNDEF_SYM_FLAGS} " CACHE INTERNAL "Linker options")
set(CMAKE_SHARED_LINKER_FLAGS "${ARCH_PIC_LDFLAGS} " CACHE INTERNAL "Shared library Linker options")
set(CMAKE_MODULE_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS})

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(CMAKE_C_FLAGS_DEBUG                "${TOOLCHAIN_OPTIMIZE_DEBUG}")
set(CMAKE_C_FLAGS_RELEASE              "${TOOLCHAIN_OPTIMIZE_RELEASE}")
set(CMAKE_C_FLAGS_MINSIZEREL "")
set(CMAKE_C_FLAGS_RELEWITHDEBINFO "")
set(CMAKE_CXX_FLAGS_DEBUG              "${TOOLCHAIN_OPTIMIZE_DEBUG}")
set(CMAKE_CXX_FLAGS_RELEASE            "${TOOLCHAIN_OPTIMIZE_RELEASE}")
set(CMAKE_CXX_FLAGS_MINSIZEREL "")
set(CMAKE_CXX_FLAGS_RELEWITHDEBINFO "")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG       "${TOOLCHAIN_OPTIMIZE_DEBUG}")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE     "${TOOLCHAIN_OPTIMIZE_RELEASE}")
set(CMAKE_EXE_LINKER_MINSIZEREL "")
set(CMAKE_EXE_LINKER_RELEWITHDEBINFO "")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG    "${TOOLCHAIN_OPTIMIZE_DEBUG}")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE  "${TOOLCHAIN_OPTIMIZE_RELEASE}")
set(CMAKE_SHARED_LINKER_MINSIZEREL "")
set(CMAKE_SHARED_LINKER_RELEWITHDEBINFO "")

set(CMAKE_MODULE_LINKER_FLAGS_DEBUG    "${TOOLCHAIN_OPTIMIZE_DEBUG}")
set(CMAKE_MODULE_LINKER_FLAGS_RELEASE  "${TOOLCHAIN_OPTIMIZE_RELEASE}")
set(CMAKE_MODULE_LINKER_MINSIZEREL "")
set(CMAKE_MODULE_LINKER_RELEWITHDEBINFO "")

link_directories(${TARGET_LINKFLAGS})
link_libraries(${TARGET_LIBRARIES})

#*********************************************************************************************************
# End
#*********************************************************************************************************

构建CMake

在MSYS2 终端输入 mkdir build_sylixos && cd build_sylixos 命令,创建文件夹build_sylixos并进入。

执行cmake命令构建工程。

注意:
将下面命令中的路径修改为自己base工程对应的路径。

cmake -G "MSYS Makefiles" -DCMAKE_MAKE_PROGRAM="make" -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_SYLIXOS_BASE_PATH="D:/SylixOS_WS/test_driver/Base_ARM_A9_R_253/" -DCMAKE_INSTALL_PREFIX="_install" \
-DCMAKE_TOOLCHAIN_FILE="../sylixos.toolchain.cmake" -Wno-dev ../

其中,参数说明如下:

  • -G "MSYS Makefiles": 生成MSYS Makefiles格式,具体说明使用cmake --help查看。
  • -DCMAKE_MAKE_PROGRAM: 设置make工具,这里可以设置绝对路径,或者相对路径。
  • -DCMAKE_VERBOSE_MAKEFILE:设置是否打印编译信息,ON or OFF。
  • -DCMAKE_BUILD_TYPE: 设置编译版本,Release or Debug。
  • -DCMAKE_SYLIXOS_BASE_PATH:设置SylixOS Base路径。(构建前,先将base编译)。
  • -DCMAKE_INSTALL_PREFIX=: 设置安装编译后安装目录。
  • -DDCMAKE_TOOLCHAIN_FILE: 设置交叉编译文件路径。绝对路径 or 相对路径。
  • -Wno-dev: 设置不打印警告信息。
  • ../: CMakeLists.txt路径。

编译工程

创建完成之后,按顺序执行以下命令。

make
make install

安装成功后,在_install目录下看到include、lib文件夹,include文件夹存放了头文件,lib目录下存放了libz的库。

文档内容是否对您有所帮助?
有帮助
没帮助