Skip to content

Commit

Permalink
supported linux cross build with docker container
Browse files Browse the repository at this point in the history
  • Loading branch information
valord577 committed Sep 9, 2024
1 parent 5007714 commit 775b668
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
7 changes: 6 additions & 1 deletion cross/toolchain-cmake-template
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ set(CROSS_TARGET_TRIPLE "__TARGET_TRIPLE__")
set(CMAKE_C_COMPILER "/usr/bin/clang" "--target=${CROSS_TARGET_TRIPLE}" "--gcc-toolchain=${CROSS_ROOT}")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++" "--target=${CROSS_TARGET_TRIPLE}" "--gcc-toolchain=${CROSS_ROOT}")

set(CMAKE_EXE_LINKER_FLAGS_INIT "-fuse-ld=/usr/bin/ld.lld")
set(CMAKE_MODULE_LINKER_FLAGS_INIT "-fuse-ld=/usr/bin/ld.lld")
set(CMAKE_SHARED_LINKER_FLAGS_INIT "-fuse-ld=/usr/bin/ld.lld")
#set(CMAKE_STATIC_LINKER_FLAGS_INIT "-fuse-ld=/usr/bin/ld.lld")

set(CMAKE_SYSROOT "${CROSS_ROOT}/${CROSS_TARGET_TRIPLE}/sysroot")
set(CMAKE_FIND_ROOT_PATH "${CMAKE_SYSROOT}" )
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

set(PKG_CONFIG_EXECUTABLE "${CMAKE_CURRENT_LIST_DIR}/pkgconf-wrapper.${CROSS_TARGET_TRIPLE}" CACHE FILEPATH "pkgconf executable")
set(PKG_CONFIG_EXECUTABLE "${CROSS_ROOT}/pkgconf-wrapper.${CROSS_TARGET_TRIPLE}" CACHE FILEPATH "pkgconf executable")
22 changes: 15 additions & 7 deletions env-linux-cross.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,38 @@ case ${TARGET_ARCH} in
exit 1
;;
esac
BUILTIN_CROSS_TOOLCHAIN_FILE="${PROJ_ROOT}/cross/toolchain-cmake-template.${__TARGET_TRIPLE__}"
BUILTIN_CROSS_TOOLCHAIN_FILE_CMAKE="${PROJ_ROOT}/cross/toolchain-cmake-template.${__TARGET_TRIPLE__}"
cat ${PROJ_ROOT}/cross/toolchain-cmake-template \
| sed "s@__TARGET_ARCH__@${__TARGET_ARCH__}@g" \
| sed "s@__TARGET_TRIPLE__@${__TARGET_TRIPLE__}@g" \
> ${BUILTIN_CROSS_TOOLCHAIN_FILE}
> ${BUILTIN_CROSS_TOOLCHAIN_FILE_CMAKE}
if [ -n "${CROSS_TOOLCHAIN_FILE_PREFIX_CMAKE}" ]; then
CROSS_TOOLCHAIN_FILE_CMAKE="${CROSS_TOOLCHAIN_FILE_PREFIX_CMAKE}.${__TARGET_TRIPLE__}"
else
CROSS_TOOLCHAIN_FILE_CMAKE="${BUILTIN_CROSS_TOOLCHAIN_FILE_CMAKE}"
fi

pushd ${PROJ_ROOT}/cross; { ln -sfn "pkgconf-wrapper" "pkgconf-wrapper.${__TARGET_TRIPLE__}"; }; popd
export CROSS_TOOLCHAIN_PKGCONF="${PROJ_ROOT}/cross/pkgconf-wrapper.${__TARGET_TRIPLE__}"

BUILTIN_CROSS_TOOLCHAIN_PKGCONF="${PROJ_ROOT}/cross/pkgconf-wrapper.${__TARGET_TRIPLE__}"
if [ -n "${CROSS_TOOLCHAIN_PKGCONF_PREFIX}" ]; then
export CROSS_TOOLCHAIN_PKGCONF="${CROSS_TOOLCHAIN_PKGCONF_PREFIX}.${__TARGET_TRIPLE__}"
else
export CROSS_TOOLCHAIN_PKGCONF="${BUILTIN_CROSS_TOOLCHAIN_PKGCONF}"
fi

CROSS_TOOLCHAIN_ROOT=${CROSS_TOOLCHAIN_ROOT:-""}
if [ -z "${CROSS_TOOLCHAIN_ROOT}" ]; then
printf "\e[1m\e[31m%s\e[0m\n" "Blank CROSS_TOOLCHAIN_ROOT: '${CROSS_TOOLCHAIN_ROOT}'."
exit 1
fi
CROSS_TOOLCHAIN_FILE=${CROSS_TOOLCHAIN_FILE:-${BUILTIN_CROSS_TOOLCHAIN_FILE}}
export SYSROOT="${CROSS_TOOLCHAIN_ROOT}/${__TARGET_TRIPLE__}/sysroot"

export PARALLEL_JOBS="$(nproc)"
export CMAKE_EXTRA_ARGS="${CMAKE_EXTRA_ARGS} -D CMAKE_TOOLCHAIN_FILE=${CROSS_TOOLCHAIN_FILE}"
export CMAKE_EXTRA_ARGS="${CMAKE_EXTRA_ARGS} -D CMAKE_TOOLCHAIN_FILE=${CROSS_TOOLCHAIN_FILE_CMAKE}"

# for cross-compiling, cmake sets compiler vars by toolchain file, so keep CC/CXX.
export CROSS_FLAGS="--target=${__TARGET_TRIPLE__} --gcc-toolchain=${CROSS_TOOLCHAIN_ROOT}"
export CC="/usr/bin/clang ${CROSS_FLAGS}"; export CXX="/usr/bin/clang++ ${CROSS_FLAGS}";
export CC="/usr/bin/clang ${CROSS_FLAGS}"; export CXX="/usr/bin/clang++ ${CROSS_FLAGS}"; export HOSTCC="/usr/bin/clang"
export LD="/usr/bin/ld.lld"; export NM="/usr/bin/llvm-nm"; export AR="/usr/bin/llvm-ar"; export AS="/usr/bin/llvm-as";
export RANLIB="/usr/bin/llvm-ranlib"; export STRIP="/usr/bin/llvm-strip";
if command -v ccache >/dev/null 2>&1 ; then
Expand Down
12 changes: 12 additions & 0 deletions patches/mbedtls/01_fix_cross_shared_build.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5f5afb212..362488c51 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -239,6 +239,7 @@ endif(CMAKE_COMPILER_IS_GNU)

if(CMAKE_COMPILER_IS_CLANG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings -Wmissing-prototypes -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-fuse-ld-path")
set(CMAKE_C_FLAGS_RELEASE "-O2")
set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
2 changes: 1 addition & 1 deletion scripts/ffmpeg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ case ${PKG_PLATFORM} in
"linux")
if [ "${CROSS_BUILD_ENABLED}" == "1" ]; then
CONFIGURE_COMMAND="${CONFIGURE_COMMAND} \
--enable-cross-compile --sysroot='${SYSROOT}' --target-os=linux --arch=${PKG_ARCH} \
--enable-cross-compile --sysroot='${SYSROOT}' --target-os=linux --arch=${PKG_ARCH} --host-cc='${HOSTCC}' \
--extra-ldflags='-fuse-ld=${LD}' --nm='${NM}' --ar='${AR}' --ranlib='${RANLIB}' --strip='${STRIP}'"
fi
;;
Expand Down

0 comments on commit 775b668

Please sign in to comment.