- Install cross compile packages on debian
sudo apt install gcc-x86-64-linux-gnu sudo apt install g++-x86-64-linux-gnu
- Create cross compile
cmake
toolchain namedlinux-x86-64-toolchain.cmake
# the name of the target operating system
set(CMAKE_SYSTEM_NAME Linux)
# which compilers to use for C and C++
set(CMAKE_C_COMPILER x86_64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER x86_64-linux-gnu-g++)
set(CMAKE_FIND_ROOT_PATH /home/debian/debian_x86_64)
# adjust the default behavior of the FIND_XXX() commands:
# search programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# search headers and libraries in the target environment
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
- Created rootfs for linux because we wanted compiled version of
qt
which is not easy to build- Installed needed packages
- Installed needed packages
sudo apt install debootstrap qemu-user-static
2. Bootstrap the needed system first phase
```Shell
mkdir -p debian_x86_64
sudo debootstrap --arch=amd64 --foreign bullseye debian_x86_64
3. Chroot to the rootfs and do the second phase
```Shell
sudo chroot debian_x86_64 export LANG=C /debootstrap/debootstrap --second-stage
4. Install the needed packages
```Shell
# edit /etc/apt/sources.list
apt update
apt install qt qtbase5-dev libqt5svg5 libqt5svg5-dev libqt5x11extras5 libqt5x11extras5-dev qttools5-dev zlib1g-dev
- Compile the dependencies and rename the folder, this is a dirty trick
./libs/build_deps_all_cross.sh mv ./libs/deps/built ./libs/deps/built-host
- Cross compile dependencies. I changed `libs/build_deps_all.sh` into following which alows us to cross compile. Look at how I used `CMAKE_TOOLCHAIN_FILE`
```Shell
#!/bin/bash
set -e
cd libs
# 参数
if [ -z $cmake ]; then
cmake="cmake"
fi
if [ -z $deps ]; then
deps="deps"
fi
TOOLCHAIN_FILE=/home/debian/cmake_targets/linux-x86-64-toolchain.cmake
# libs/deps/...
mkdir -p $deps
cd $deps
if [ -z $NKR_PACKAGE ]; then
INSTALL_PREFIX=$PWD/built
else
INSTALL_PREFIX=$PWD/package
fi
rm -rf $INSTALL_PREFIX
mkdir -p $INSTALL_PREFIX
#### clean ####
clean() {
rm -rf dl.zip yaml-* zxing-* protobuf
}
#### ZXing v2.0.0 ####
curl -L -o dl.zip https://github.com/nu-book/zxing-cpp/archive/refs/tags/v2.0.0.zip
unzip dl.zip
cd zxing-*
mkdir -p build
cd build
$cmake .. -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN_FILE -GNinja -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release -DBUILD_EXAMPLES=OFF -DBUILD_BLACKBOX_TESTS=OFF -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX
ninja && ninja install
cd ../..
#### yaml-cpp ####
curl -L -o dl.zip https://github.com/jbeder/yaml-cpp/archive/refs/tags/yaml-cpp-0.7.0.zip
unzip dl.zip
cd yaml-*
mkdir -p build
cd build
$cmake .. -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN_FILE -GNinja -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX
ninja && ninja install
cd ../..
#### protobuf ####
git clone --recurse-submodules -b v21.4 --depth 1 --shallow-submodules https://github.com/protocolbuffers/protobuf
#备注:交叉编译要在 host 也安装 protobuf 并且版本一致,编译安装,同参数,安装到 /usr/local
mkdir -p protobuf/build
cd protobuf/build
$cmake .. -GNinja \
-DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN_FILE \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-Dprotobuf_MSVC_STATIC_RUNTIME=OFF \
-Dprotobuf_BUILD_TESTS=OFF \
-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX
ninja && ninja install
cd ../..
####
clean
- Do the dirty trick for binary results
mv ./libs/deps/built/bin ./libs/deps/built/bin-x
cp -r ./libs/deps/built-host/bin/ ./libs/deps/built
- Cross compile the nekoray
- Patch the cmake file. this can be done in more neat ways. dirty trick
- Patch the cmake file. this can be done in more neat ways. dirty trick
diff --git a/CMakeLists.txt b/CMakeLists.txt index 142d2a5..3f3577d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,12 @@ if (NOT QT_VERSION_MAJOR) endif () find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Network Svg LinguistTools)
+set_property(TARGET Qt5::moc PROPERTY IMPORTED_LOCATION /usr/bin/moc) +set_property(TARGET Qt5::uic PROPERTY IMPORTED_LOCATION /usr/bin/uic) +set_property(TARGET Qt5::rcc PROPERTY IMPORTED_LOCATION /usr/bin/rcc) +set_property(TARGET Qt5::lrelease PROPERTY IMPORTED_LOCATION /usr/bin/lrelease) +set_property(TARGET Qt5::lupdate PROPERTY IMPORTED_LOCATION /usr/bin/lupdate) + if (NKR_CROSS) set_property(TARGET Qt5::moc PROPERTY IMPORTED_LOCATION /usr/bin/moc) set_property(TARGET Qt5::uic PROPERTY IMPORTED_LOCATION /usr/bin/uic)
- Cross compile
```Shell
mkdir build
cmake .. -DCMAKE_TOOLCHAIN_FILE=~/cmake_targets/linux-x86-64-toolchain.cmake -GNinja
ninja
- Cross compile golang binaries
- clone the https://github.com/MatsuriDayo/v2ray-core outside
nekoray
- make sure that you have go 1.20 or newer
- run following commands to cross compile
- clone the https://github.com/MatsuriDayo/v2ray-core outside
cd go/cmd/nekobox_core/ GOOS=linux GOARCH=amd64 go build cd ../nekobox_core GOOS=linux GOARCH=amd64 go build
With doing all this you will have all the binaries needed to cross compile nekoray
# Cross compile in nix
Good to read
- https://nix.dev/tutorials/cross-compilation
- https://nix.dev/tutorials/declarative-and-reproducible-developer-environments#declarative-reproducible-envs
## reproduceable env