fix cross build

This commit is contained in:
Oystein Kristoffer Tveit 2024-06-24 08:07:42 +02:00 committed by Øystein Tveit
parent be8e7ecd69
commit 78afb20efb
4 changed files with 72 additions and 10 deletions

View File

@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.16)
project(TestProject LANGUAGES CXX)
include("${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
find_package(fmt CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
@ -15,3 +17,5 @@ target_link_libraries(
fmt::fmt
nlohmann_json::nlohmann_json
)
install(TARGETS test DESTINATION bin)

View File

@ -15,8 +15,9 @@
pkgs = import nixpkgs {
inherit system;
overlays = [
(prev: final: {
(final: prev: {
vcpkg-tool = final.callPackage ./nix/packages/vcpkg-tool/package.nix { };
vcpkg = import ./nix/packages/vcpkg/package.nix { inherit (prev) vcpkg; };
})
];
};
@ -29,6 +30,14 @@
test = pkgs.callPackage ./nix/packages/test/package.nix {
src = ./.;
};
test-aarch64 = pkgs.pkgsCross.aarch64-multiplatform.callPackage ./nix/packages/test/package.nix {
src = ./.;
};
test-armv7 = pkgs.pkgsCross.armv7l-hf-multiplatform.callPackage ./nix/packages/test/package.nix {
src = ./.;
};
});
devShells = forAllSystems (system: pkgs: {

View File

@ -14,24 +14,52 @@
, zip
}:
let
nixTripletToVcpkgTripletTable = {
i686-linux = "x86-linux";
x86_64-linux = "x64-linux";
armv7l-linux = "arm-linux";
aarch64-linux = "arm64-linux";
};
nixTripletToVcpkgArchitectureTable = {
i686-linux = "x86";
x86_64-linux = "x64";
armv7l-linux = "arm";
aarch64-linux = "arm64";
};
vcpkgHostTriplet = nixTripletToVcpkgTripletTable.${stdenv.buildPlatform.system}
+ lib.optionalString stdenv.buildPlatform.isStatic "-static";
vcpkgTargetTriplet = nixTripletToVcpkgTripletTable.${stdenv.hostPlatform.system}
+ lib.optionalString stdenv.hostPlatform.isStatic "-static";
vcpkgHostArch = nixTripletToVcpkgArchitectureTable.${stdenv.buildPlatform.system};
vcpkgTargetArch = nixTripletToVcpkgArchitectureTable.${stdenv.hostPlatform.system};
deps = stdenv.mkDerivation {
pname = "test-deps";
version = "0.0.1";
inherit src;
# NOTE: for this small example, these hashes seem to match,
# but I have a suspicion that this might not be the case
# for larger projects, where deps depend on their architecture.
outputHashAlgo = "sha256";
outputHashMode = "recursive";
outputHash = {
"x86_64-linux" = "sha256-nFIbhVp32y+TlFLhuLc0XhlGJZmADgKBo4pd78H3mg8=";
# "x86_64-linux" = "";
"aarch64-linux" = "";
"armv7l-linux" = null;
"aarch64-linux" = "sha256-nFIbhVp32y+TlFLhuLc0XhlGJZmADgKBo4pd78H3mg8=";
"armv7l-linux" = "sha256-nFIbhVp32y+TlFLhuLc0XhlGJZmADgKBo4pd78H3mg8=";
}.${stdenv.hostPlatform.system};
dontConfigure = true;
env = {
VCPKG_ROOT = "${vcpkg}/share/vcpkg";
VCPKG_DEFAULT_TRIPLET = vcpkgTargetTriplet;
VCPKG_DEFAULT_HOST_TRIPLET = vcpkgHostTriplet;
VCPKG_BINARY_SOURCES = "clear";
VCPKG_FORCE_SYSTEM_BINARIES = "1";
};
nativeBuildInputs = [
@ -86,36 +114,50 @@ stdenv.mkDerivation {
cacert
ninja
zip
pkg-config
];
dontConfigure = true;
env = {
VCPKG_ROOT = "${vcpkg}/share/vcpkg";
VCPKG_DEFAULT_TRIPLET = vcpkgTargetTriplet;
VCPKG_DEFAULT_HOST_TRIPLET = vcpkgHostTriplet;
VCPKG_BINARY_SOURCES = "clear";
VCPKG_FORCE_SYSTEM_BINARIES = "1";
};
buildPhase = let
vcpkg-install-opts = lib.concatStringsSep " " [
''--x-buildtrees-root=$TMP/vcpkg_buildtrees''
''--x-packages-root=$TMP/vcpkg_packages''
''--downloads-root=${deps}''
''--downloads-root=$TMP/vcpkg_downloads''
''--x-manifest-root=.''
''--x-install-root=$TMP/vcpkg_installed''
];
cmake-opts = lib.concatStringsSep " " [
(lib.cmakeFeature "VCPKG_INSTALLED_DIR" "$TMP/vcpkg_installed")
(lib.cmakeFeature "VCPKG_TRACE_FIND_PACKAGE" "ON")
# (lib.cmakeFeature "VCPKG_TRACE_FIND_PACKAGE" "ON")
(lib.cmakeFeature "VCPKG_MANIFEST_INSTALL" "OFF")
(lib.cmakeFeature "VCPKG_TARGET_TRIPLET" vcpkgTargetTriplet)
(lib.cmakeFeature "VCPKG_HOST_TRIPLET" vcpkgHostTriplet)
(lib.cmakeFeature "VCPKG_ROOT" "${vcpkg}/share/vcpkg")
(lib.cmakeFeature "CMAKE_BUILD_TYPE" "Release")
(lib.cmakeFeature "CMAKE_TOOLCHAIN_FILE" "${vcpkg}/share/vcpkg/scripts/buildsystems/vcpkg.cmake")
# (lib.cmakeFeature "CMAKE_CXX_COMPILER" "g++")
# (lib.cmakeFeature "CMAKE_C_COMPILER" "gcc")
# -DVCPKG_INSTALL_OPTIONS="${vcpkg-install-opts}"
# NOTE: this does not work for cross compilation, because
# you already specify a toolchain. We have to somehow
# inject the library path or an `include` instead?
# source: https://stackoverflow.com/a/64143507
# (lib.cmakeFeature "CMAKE_TOOLCHAIN_FILE" "${vcpkg}/share/vcpkg/scripts/buildsystems/vcpkg.cmake")
];
in ''
runHook preBuild
cp -r "${deps}/vcpkg_downloads" "$TMP/"
vcpkg install ${vcpkg-install-opts}
cmake ${cmake-opts} -S . -B build -G Ninja

View File

@ -0,0 +1,7 @@
{ vcpkg }:
vcpkg.overrideAttrs (prevAttrs: {
postPatch = ''
sed -i -E -e 's/aarch64-linux-gnu-(as|gcc|g++)/aarch64-unknown-linux-gnu-\1/' scripts/toolchains/linux.cmake
sed -i -E -e 's/arm-linux-gnueabihf-(as|gcc|g++)/armv7l-unknown-linux-gnueabihf-\1/' scripts/toolchains/linux.cmake
'';
})