首先是系统编译工具链环境的搭建
cmake/function.cmake
function(add_object_lib target include_dirs compile_definitions compile_options sources)
add_library(${target} OBJECT ${sources})
target_include_directories(${target} PRIVATE ${include_dirs})
target_compile_definitions(${target} PRIVATE ${compile_definitions})
target_compile_options(${target} PRIVATE ${compile_options})
endfunction()
function(add_object_lib_CXX target include_dirs CXX_compile_definitions CXX_compile_options sources)
add_library(${target} OBJECT ${sources})
target_include_directories(${target} PRIVATE ${include_dirs})
target_compile_definitions(${target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:${CXX_compile_definitions}>)
target_compile_options(${target} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:${CXX_compile_options}>)
endfunction()
function(set_shared_lib target link_options output_dir)
set_target_properties(${target} PROPERTIES LINK_FLAGS ${link_options})
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${output_dir})
set_target_properties(${target} PROPERTIES DEFINE_SYMBOL "")
set_target_properties(${target} PROPERTIES NO_SONAME 1)
endfunction()
function(set_static_lib target link_options output_dir)
set_target_properties(${target} PROPERTIES LINK_FLAGS ${link_options})
set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${output_dir})
#set_target_properties(${target} PROPERTIES DEFINE_SYMBOL "")
#set_target_properties(${target} PROPERTIES NO_SONAME 1)
endfunction()
cmake/includedir.cmake
set(include_dirs
#包含工程所需的头文件
)
主工程CMakeLists.txt
cmake_minimum_required(VERSION 3.12.1)
project(xxxx)
#set the path
set(SDK_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(SO_OUTPUT_DIR ${SDK_DIR}/output)
message("==== source_dir:${CMAKE_CURRENT_SOURCE_DIR}")
#=============include the function defined=================#
include(cmake/function.cmake)
#====================seleted which chip is compiled=================#
if(${CHIP_TYPE} STREQUAL xxxxx)
#printf the debug info
message("======the chip selected is ${target_xxxx}======")
#set the including header file
include(cmake/xxxx.cmake)
set(output_dir ${SO_OUTPUT_DIR}/${CHIP_TYPE})
if(${RTOS_VERSION} STREQUAL xxxx)
set(target_xxxx xxxxx)
message("======the RTOS selected is xxxx======")
elseif(${RTOS_VERSION} STREQUAL xxxx)
set(target xxxxx)
message("======the RTOS selected is xxxx======")
elseif(${RTOS_VERSION} STREQUAL xxxxx)
set(target xxxxxx)
message("======the RTOS selected is xxxxxx======")
endif()
#set the compile options
set(compile_definitions -Dxxxxx)
if(${SO_TYPE} STREQUAL static)
set(compile_options xxxxx)
set(link_options xxxx)
add_library(${target_xxx} STATIC $<TARGET_OBJECTS:xxx-sdk>)
add_subdirectory(cmake)
set_static_lib(${target_xx} "${link_options}" "${output_dir}")
else()
set(compile_options xxxx)
set(link_options xxxx)
add_library(${target_xxx} SHARED $<TARGET_OBJECTS:xxx-sdk>)
add_subdirectory(cmake)
set_shared_lib(${target_xxx} "${link_options}" "${output_dir}")
endif()
endif()
cmake文件夹下的CMakeLists.txt
add_subdirectory(xxx)
objlib的cmake/xxx/CMakeLists.txt
set(xxxx_sources
xxx.c
xxx.c
)
add_object_lib(xxx-sdk "${include_dirs}" "${compile_definitions}" "${compile_options}" "${xxxx_sources}")
gcc_tool_chain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER arm-linux-gnueabi-gcc)
set(CMAKE_CXX_COMPILER arm-linux-gnueabi-g++)
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
cmake指令
cmake -DCMAKE_TOOLCHAIN_FILE="$BUILD_DIR"/../cmake/gcc_tool_chain.cmake ../.. \
-DCHIP_TYPE="$1" \
-DRTOS_VERSION="$2" \
-DSO_TYPE="$4" \
-Dcdk_include="$cdk_include"
最后就是make执行编译