1. cmake

1.1 CMakeList.txt

# 指定cmake最低版本版本
cmake_minimum_required(VERSION 2.8.0 FATAL_ERROR)
# 项目名称
PROJECT (gree)
# OPTION(PLATFORM "select platform" OFF)
# https://www.cnblogs.com/549294286/p/6583189.html
# cmake手册详解
ADD_DEFINITIONS(-DPLATFORM)
# 设置编译选项
SET(CMAKE_CXX_FLAGS "-std=c++11")
# 头文件路径
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/communication_layer/include/)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/dev_app_layer/include/)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib/include/)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib/include/googletest/)
# 源文件路径
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/communication_layer/src/ COMMUNICATION_LAYER)
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/dev_app_layer/src/ DEV_APP_LAYER)
SET(MAIN ${PROJECT_SOURCE_DIR}/start/main_local.c)
SET(MAIN_GTEST ${PROJECT_SOURCE_DIR}/start/main_gtest_local.cpp)
# 添加第三方库
if(PLATFORM STREQUAL "LOCAL")
    LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib/local)
elseif(PLATFORM STREQUAL "NOT_LOCAL")
    LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib/target)
endif()
SET(LIB_GREE 
    libsqlite3.a libcJSON.a libmbedtls.a 
    libmbedcrypto.a -lm -lpthread -ldl
)
SET(LIB_GTEST 
    libgtest.a libgtest_main.a 
    libsqlite3.a libcJSON.a libmbedtls.a 
    libmbedcrypto.a -lm -ldl -pthread
)
# 可运行文件存放于../bin
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ../bin/
    CACHE PATH "Single directory for all executable.")

# 添加可执行文件
ADD_EXECUTABLE(gree ${COMMUNICATION_LAYER} ${DEV_APP_LAYER} ${MAIN})  
ADD_EXECUTABLE(unit_test ${COMMUNICATION_LAYER} ${DEV_APP_LAYER} ${MAIN_GTEST})
#下面的 libxxx 是 第三方库 的名字:
TARGET_LINK_LIBRARIES(gree ${LIB_GREE})
TARGET_LINK_LIBRARIES(unit_test  ${LIB_GTEST})

1.2 make.sh

#! /bin/bash
curpath=`pwd`
buildpath=$curpath/build
lib_path=$curpath/lib
toolchain_local=/usr/bin
toolchain_target=/opt/toolchain/linkpad/bin
CC_LOCAL=$toolchain_local/gcc
CXX_LOCAL=$toolchain_local/g++
CC_TARGET=$CC_LOCAL
CXX_TARGET=$CXX_LOCAL
# CC_TARGET=$toolchain_target/aarch64-rockchip-linux-gnu-gcc
# CXX_TARGET=$toolchain_target/aarch64-rockchip-linux-gnu-g++
lib_a_path_local=$lib_path/local
lib_a_path_target=$lib_path/target
lib_a_path_include=$lib_path/include

if [[ ! -x "$buildpath" ]]; then
        mkdir "$buildpath"
fi
if [[ ! -x "$lib_path" ]]; then
    mkdir "$lib_path"
    mkdir "$lib_a_path_local"
    mkdir "$lib_a_path_target"
    mkdir "$lib_a_path_include"
fi

# 编译第三方库的静态库
# mbedtls 的静态库编译
build_mbedtls()
{
    mbedtls_path=$curpath/third_party/mbedtls-2.16.6
    if [[ ! -x "$lib_a_path_include/mbedtls" ]]; then
        mkdir "$lib_a_path_include/mbedtls"
    fi
    cp -r $mbedtls_path/include/mbedtls/* $lib_a_path_include/mbedtls
    mkdir $mbedtls_path/build
    cd $mbedtls_path/build

    cmake -D CMAKE_C_COMPILER=$CC_LOCAL ../
    make
    cp library/*.a $lib_a_path_local
    rm -rf $mbedtls_path/build/*

    cmake -D CMAKE_C_COMPILER=$CC_TARGET ../
    make
    cp library/*.a $lib_a_path_target
    rm -rf $mbedtls_path/build
}
# googletest 的静态库编译
build_googletest()
{
    lib_name=googletest
    lib_real_name=googletest
    lib_real_path=$curpath/third_party/$lib_real_name
    lib_cp_include=$lib_a_path_include/$lib_name

    cd $lib_real_path
    if [[ ! -x $lib_cp_include ]]; then
        mkdir $lib_cp_include
        mkdir $lib_cp_include/gtest
        mkdir $lib_cp_include/gmock
    fi
    cp -r $lib_real_path/googletest/include/gtest/* $lib_cp_include/gtest
    cp -r $lib_real_path/googlemock/include/gmock/* $lib_cp_include/gmock
    if [[ -x $lib_cp_include ]]; then
        rm -rf $lib_real_path/build/
    fi
    mkdir $lib_real_path/build && cd $lib_real_path/build

    cmake -D CMAKE_CXX_COMPILER=$CXX_LOCAL ../
    make
    cp lib/*.a $lib_a_path_local
    rm -rf $lib_real_path/build/*

    cmake -D CMAKE_CXX_COMPILER=$CXX_TARGET ../
    make
    cp lib/*.a $lib_a_path_target
    rm -rf $lib_real_path/build    
}
# cjson 的静态库编译
build_cjson()
{
    lib_name=cJSON
    lib_real_name=cJSON
    lib_real_path=$curpath/third_party/$lib_real_name
    lib_cp_include=$lib_a_path_include/$lib_name

    cd $lib_real_path
    if [[ ! -x $lib_cp_include ]]; then
        mkdir $lib_cp_include
    fi
    cp *.h $lib_cp_include
    mkdir $lib_real_path/build && cd $lib_real_path/build

    $CC_LOCAL ../*.c -c -I ../
    ar rcs lib${lib_name}.a *.o
    cp *.a $lib_a_path_local
    rm -rf $lib_real_path/build/*

    $CC_TARGET ../*.c -c -I ../
    ar rcs lib${lib_name}.a *.o
    cp *.a $lib_a_path_target
    rm -rf $lib_real_path/build/
}
# sqlite3 的静态库编译
build_sqlite3()
{
    lib_name=sqlite3
    lib_real_name=sqlite-amalgamation-3310100
    lib_real_path=$curpath/third_party/$lib_real_name
    lib_cp_include=$lib_a_path_include/$lib_name

    cd $lib_real_path
    if [[ ! -x $lib_cp_include ]]; then
        mkdir $lib_cp_include
    fi
    cp *.h $lib_cp_include
    mkdir $lib_real_path/build && cd $lib_real_path/build

    $CC_LOCAL ../*.c -c -I ../
    ar rcs lib${lib_name}.a *.o
    cp *.a $lib_a_path_local
    rm -rf $lib_real_path/build/*

    $CC_TARGET ../*.c -c -I ../
    ar rcs lib${lib_name}.a *.o
    cp *.a $lib_a_path_target
    rm -rf $lib_real_path/build/    
}
if [[ $1 == lib && -z "$2" ]]; then
    echo "build all static libs ..."
    build_mbedtls
    build_cjson
    build_sqlite3
    build_googletest
elif [[ $2 == googletest ]]; then
    echo "build googletest static lib ..."
    build_googletest
elif [[ $2 == mbedtls ]]; then
    echo "build mbedtls static lib ..."
    build_mbedtls
elif [[ $2 == cjson ]]; then
    echo "build cjson static lib ..."
    build_cjson
elif [[ $2 == sqlite3 ]]; then
    echo "build sqlite3 static lib ..."
    build_sqlite3
fi

#编译项目目录
cd $curpath
if [[ -z "$1" ]]; then
    echo "<default> build for local system ..."
    cd $buildpath
    cmake ../ \
    -DCMAKE_CXX_COMPILER:FILEPATH=$CXX_LOCAL \
    -DCMAKE_C_COMPILER:FILEPATH=$CC_LOCAL \
    -DPLATFORM=LOCAL \
#    -DCMAKE_BUILD_TYPE:STRING=Release
    make
elif [[ $1 == target ]]; then
    echo "build for target system ..."
    cd $buildpath
    cmake ../ \
    -DCMAKE_CXX_COMPILER:FILEPATH=$CXX_TARGET \
    -DCMAKE_C_COMPILER:FILEPATH=$CC_TARGET \
    -DCMAKE_BUILD_TYPE:STRING=Release \
    -DPLATFORM=NOT_LOCAL
    make
elif [[ $1 == clean && -z "$2" ]]; then
    echo "rm build/ bin/ ..."
    rm -rf $buildpath/
    rm -rf $curpath/bin/
elif [[ $2 == bin ]]; then
    echo "rm bin/ ..."
    rm -rf $curpath/bin/
elif [[ $2 == lib ]]; then
    echo "rm lib/ ..."
    rm -rf $curpath/lib/
elif [[ $2 == build ]]; then
    echo "rm build/ ..."
    rm -rf $buildpath/
fi

results matching ""

    No results matching ""