157 lines
5.0 KiB
Bash
157 lines
5.0 KiB
Bash
|
|
#!/bin/bash
|
||
|
|
|
||
|
|
cd "$(dirname "$(readlink -f "$0")")"
|
||
|
|
|
||
|
|
start_time=$(date +%s)
|
||
|
|
trap 'echo "$(print_execution_time $?)"' EXIT
|
||
|
|
|
||
|
|
function print_execution_time {
|
||
|
|
end_time=$(date +%s)
|
||
|
|
execution_time=$((end_time - start_time))
|
||
|
|
execution_time_formatted=$(date -u -d @$execution_time +"%T")
|
||
|
|
if [ $1 -eq 0 ]; then
|
||
|
|
SET_COLOR="\e[32m "
|
||
|
|
else
|
||
|
|
SET_COLOR="\e[31m error at $(pwd) \n\e[1m"
|
||
|
|
fi
|
||
|
|
mydir_name="$(d=$(pwd); echo ${d##*/})"
|
||
|
|
echo -e "${SET_COLOR} [$1] 执行时间:$execution_time_formatted ( $mydir_name )\e[0m "
|
||
|
|
}
|
||
|
|
|
||
|
|
current_project_path=$(pwd)
|
||
|
|
current_project_name=${current_project_path##*/}
|
||
|
|
echo "current_project_path = ${current_project_path}"
|
||
|
|
|
||
|
|
echo "====================================================build shell start [ ${current_project_name} ]===================================================="
|
||
|
|
|
||
|
|
pd_complier_type=${TARGET_PRODUCT}
|
||
|
|
|
||
|
|
if [ -z "${TARGET_PRODUCT}" ]; then
|
||
|
|
PROJECT_ROOT=$(while [ ! -f "build/envsetup.sh" ]; do
|
||
|
|
[ "$(pwd)" = "/" ] && exit 1
|
||
|
|
cd ..
|
||
|
|
done
|
||
|
|
pwd)
|
||
|
|
|
||
|
|
if [ -n "${PROJECT_ROOT}" ]; then
|
||
|
|
source "${PROJECT_ROOT}/build/envsetup.sh" && lunch LAST
|
||
|
|
if [ $? -eq 255 ]; then
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
pd_complier_type=${TARGET_PRODUCT}
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo -e " ready to build\e[34;1m ${pd_complier_type} \e[0m target [\e[34;1m ${current_project_name}\e[0m ] ......"
|
||
|
|
|
||
|
|
unset LD_LIBRARY_PATH
|
||
|
|
|
||
|
|
# 如果外部传了 TOOLCHAIN,就优先 source
|
||
|
|
if [[ -n "${TOOLCHAIN}" ]] && [[ -f "${TOOLCHAIN}" ]]; then
|
||
|
|
echo -e "\e[32m source TOOLCHAIN=${TOOLCHAIN} \e[0m"
|
||
|
|
source "${TOOLCHAIN}"
|
||
|
|
fi
|
||
|
|
|
||
|
|
base_folder="$(echo ${PROJECT_ROOT} | tr -s '/')"
|
||
|
|
|
||
|
|
if [[ ${current_project_path} =~ "${base_folder}" ]] && [ -n "${base_folder}" ]; then
|
||
|
|
echo ""
|
||
|
|
else
|
||
|
|
echo "base_folder = ${base_folder}"
|
||
|
|
echo "current_project_path = ${current_project_path}"
|
||
|
|
echo -e "\e[31m Error: 环境配置不匹配,在当前项目下请重新配置:\n\e[35m\t source build/envsetup.sh \e[0m"
|
||
|
|
exit -2
|
||
|
|
fi
|
||
|
|
|
||
|
|
build_path=$(echo "${current_project_path}" | sed "s|${base_folder}|${base_folder}/out/build/${pd_complier_type}/|g")
|
||
|
|
echo "build_path = ${build_path}"
|
||
|
|
|
||
|
|
# 清理逻辑
|
||
|
|
if [[ "$1" == "clean" ]] || [[ "$1" == "-c" ]]; then
|
||
|
|
rm -rf build_arm
|
||
|
|
echo " clean done."
|
||
|
|
echo "====================================================build shell end [lib${current_project_name}]===================================================="
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
|
||
|
|
if [[ "$1" == "all" ]]; then
|
||
|
|
rm -rf build_arm
|
||
|
|
echo " full rebuild mode."
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 结果目录
|
||
|
|
RESULT_DIR="$(pwd)/result_dir"
|
||
|
|
|
||
|
|
RESULT_SSL="${RESULT_DIR}/../../openssl-OpenSSL_1_1_1g/ssl_result"
|
||
|
|
RESULT_MQTT_C="${RESULT_DIR}/paho.mqtt.c_result"
|
||
|
|
RESULT_MQTT_CPP="${RESULT_DIR}/paho.mqtt.cpp_result"
|
||
|
|
|
||
|
|
mkdir -p "${RESULT_DIR}"
|
||
|
|
|
||
|
|
# 动态选择 CXX
|
||
|
|
if [[ -z "${CXX}" ]]; then
|
||
|
|
if [[ "${pd_complier_type}" =~ imx* ]]; then
|
||
|
|
export CXX="aarch64-poky-linux-g++"
|
||
|
|
else
|
||
|
|
export CXX="x86_64-linux-gnu-g++"
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 把 CXX 拆成“编译器本体 + 额外参数”
|
||
|
|
RAW_CXX="${CXX}"
|
||
|
|
REAL_CXX="$(printf '%s\n' "${RAW_CXX}" | awk '{print $1}')"
|
||
|
|
EXTRA_CXXFLAGS="$(printf '%s\n' "${RAW_CXX}" | cut -d' ' -f2-)"
|
||
|
|
|
||
|
|
MERGED_CXXFLAGS="${EXTRA_CXXFLAGS} ${CXXFLAGS} -std=gnu++11"
|
||
|
|
|
||
|
|
echo "----------------cmake start----------------"
|
||
|
|
echo "RAW_CXX = ${RAW_CXX}"
|
||
|
|
echo "REAL_CXX = ${REAL_CXX}"
|
||
|
|
echo "EXTRA_CXXFLAGS = ${EXTRA_CXXFLAGS}"
|
||
|
|
echo "MERGED_CXXFLAGS = ${MERGED_CXXFLAGS}"
|
||
|
|
echo "SYSROOT = ${SYSROOT}"
|
||
|
|
echo "RESULT_MQTT_C = ${RESULT_MQTT_C}"
|
||
|
|
echo "RESULT_MQTT_CPP = ${RESULT_MQTT_CPP}"
|
||
|
|
echo "RESULT_SSL = ${RESULT_SSL}"
|
||
|
|
|
||
|
|
# rm -rf build_arm
|
||
|
|
mkdir -p build_arm
|
||
|
|
cd build_arm || exit 1
|
||
|
|
|
||
|
|
cmake .. \
|
||
|
|
-DCMAKE_CXX_COMPILER="${REAL_CXX}" \
|
||
|
|
-DCMAKE_INSTALL_PREFIX="${RESULT_MQTT_CPP}" \
|
||
|
|
-DPAHO_MQTT_C_LIBRARIES="${RESULT_MQTT_C}/lib/libpaho-mqtt3as.so" \
|
||
|
|
-DPAHO_MQTT_C_INCLUDE_DIRS="${RESULT_MQTT_C}/include" \
|
||
|
|
-DOPENSSL_SSL_LIBRARY="${RESULT_SSL}/lib/libssl.so" \
|
||
|
|
-DOPENSSL_INCLUDE_DIR="${RESULT_SSL}/include" \
|
||
|
|
-DOPENSSL_CRYPTO_LIBRARY="${RESULT_SSL}/lib/libcrypto.so" \
|
||
|
|
-DPAHO_WITH_SSL=TRUE \
|
||
|
|
-DCMAKE_CXX_FLAGS="${MERGED_CXXFLAGS}"
|
||
|
|
|
||
|
|
make -j4 || exit $?
|
||
|
|
make install || exit $?
|
||
|
|
|
||
|
|
echo "build dir = $(pwd)"
|
||
|
|
|
||
|
|
mkdir -p "${PROJECT_ROOT}/include/"
|
||
|
|
mkdir -p "${PROJECT_ROOT}/out/${TARGET_PRODUCT}/lib/lib_so/"
|
||
|
|
mkdir -p "${PROJECT_ROOT}/board_dir/lib/"
|
||
|
|
|
||
|
|
target_dir="${RESULT_MQTT_CPP}/lib/cmake"
|
||
|
|
|
||
|
|
if [[ -d "${target_dir}" && "$(basename "${target_dir}")" == "cmake" ]]; then
|
||
|
|
rm -rf "${target_dir}"
|
||
|
|
echo "已删除目录: ${target_dir}"
|
||
|
|
else
|
||
|
|
echo "未删除,目标不存在或目录名不是 cmake: ${target_dir}"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
cp -ravf "${RESULT_MQTT_CPP}/include/mqtt/"* "${PROJECT_ROOT}/include/mqtt/" || exit $?
|
||
|
|
cp -ravf "${RESULT_MQTT_CPP}/lib/"* "${PROJECT_ROOT}/out/${TARGET_PRODUCT}/lib/lib_so/" || exit $?
|
||
|
|
cp -ravf "${RESULT_MQTT_CPP}/lib/"* "${PROJECT_ROOT}/board_dir/lib/" || exit $?
|
||
|
|
|
||
|
|
cd - >/dev/null || exit 1
|
||
|
|
|
||
|
|
echo "===============================build shell end [lib${current_project_name}] [${pd_complier_type}]===================================================="
|