From 78afb20efbe1923fce513a750b9888c2daa26d8d Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 24 Jun 2024 08:07:42 +0200 Subject: [PATCH] fix cross build --- CMakeLists.txt | 4 +++ flake.nix | 11 ++++++- nix/packages/test/package.nix | 60 +++++++++++++++++++++++++++++----- nix/packages/vcpkg/package.nix | 7 ++++ 4 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 nix/packages/vcpkg/package.nix diff --git a/CMakeLists.txt b/CMakeLists.txt index 13db4c5..037669e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/flake.nix b/flake.nix index dc1e6d7..879cb3e 100644 --- a/flake.nix +++ b/flake.nix @@ -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: { diff --git a/nix/packages/test/package.nix b/nix/packages/test/package.nix index 522a7c3..8d6f34b 100644 --- a/nix/packages/test/package.nix +++ b/nix/packages/test/package.nix @@ -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 diff --git a/nix/packages/vcpkg/package.nix b/nix/packages/vcpkg/package.nix new file mode 100644 index 0000000..69ce662 --- /dev/null +++ b/nix/packages/vcpkg/package.nix @@ -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 + ''; +})