fix cross build
This commit is contained in:
parent
be8e7ecd69
commit
78afb20efb
|
@ -1,6 +1,8 @@
|
||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
project(TestProject LANGUAGES CXX)
|
project(TestProject LANGUAGES CXX)
|
||||||
|
|
||||||
|
include("${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
|
||||||
|
|
||||||
find_package(fmt CONFIG REQUIRED)
|
find_package(fmt CONFIG REQUIRED)
|
||||||
find_package(nlohmann_json CONFIG REQUIRED)
|
find_package(nlohmann_json CONFIG REQUIRED)
|
||||||
|
|
||||||
|
@ -15,3 +17,5 @@ target_link_libraries(
|
||||||
fmt::fmt
|
fmt::fmt
|
||||||
nlohmann_json::nlohmann_json
|
nlohmann_json::nlohmann_json
|
||||||
)
|
)
|
||||||
|
|
||||||
|
install(TARGETS test DESTINATION bin)
|
||||||
|
|
11
flake.nix
11
flake.nix
|
@ -15,8 +15,9 @@
|
||||||
pkgs = import nixpkgs {
|
pkgs = import nixpkgs {
|
||||||
inherit system;
|
inherit system;
|
||||||
overlays = [
|
overlays = [
|
||||||
(prev: final: {
|
(final: prev: {
|
||||||
vcpkg-tool = final.callPackage ./nix/packages/vcpkg-tool/package.nix { };
|
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 {
|
test = pkgs.callPackage ./nix/packages/test/package.nix {
|
||||||
src = ./.;
|
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: {
|
devShells = forAllSystems (system: pkgs: {
|
||||||
|
|
|
@ -14,24 +14,52 @@
|
||||||
, zip
|
, zip
|
||||||
}:
|
}:
|
||||||
let
|
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 {
|
deps = stdenv.mkDerivation {
|
||||||
pname = "test-deps";
|
pname = "test-deps";
|
||||||
version = "0.0.1";
|
version = "0.0.1";
|
||||||
inherit src;
|
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";
|
outputHashAlgo = "sha256";
|
||||||
outputHashMode = "recursive";
|
outputHashMode = "recursive";
|
||||||
outputHash = {
|
outputHash = {
|
||||||
"x86_64-linux" = "sha256-nFIbhVp32y+TlFLhuLc0XhlGJZmADgKBo4pd78H3mg8=";
|
"x86_64-linux" = "sha256-nFIbhVp32y+TlFLhuLc0XhlGJZmADgKBo4pd78H3mg8=";
|
||||||
# "x86_64-linux" = "";
|
"aarch64-linux" = "sha256-nFIbhVp32y+TlFLhuLc0XhlGJZmADgKBo4pd78H3mg8=";
|
||||||
"aarch64-linux" = "";
|
"armv7l-linux" = "sha256-nFIbhVp32y+TlFLhuLc0XhlGJZmADgKBo4pd78H3mg8=";
|
||||||
"armv7l-linux" = null;
|
|
||||||
}.${stdenv.hostPlatform.system};
|
}.${stdenv.hostPlatform.system};
|
||||||
|
|
||||||
dontConfigure = true;
|
dontConfigure = true;
|
||||||
|
|
||||||
env = {
|
env = {
|
||||||
VCPKG_ROOT = "${vcpkg}/share/vcpkg";
|
VCPKG_ROOT = "${vcpkg}/share/vcpkg";
|
||||||
|
VCPKG_DEFAULT_TRIPLET = vcpkgTargetTriplet;
|
||||||
|
VCPKG_DEFAULT_HOST_TRIPLET = vcpkgHostTriplet;
|
||||||
|
VCPKG_BINARY_SOURCES = "clear";
|
||||||
|
VCPKG_FORCE_SYSTEM_BINARIES = "1";
|
||||||
};
|
};
|
||||||
|
|
||||||
nativeBuildInputs = [
|
nativeBuildInputs = [
|
||||||
|
@ -86,36 +114,50 @@ stdenv.mkDerivation {
|
||||||
cacert
|
cacert
|
||||||
ninja
|
ninja
|
||||||
zip
|
zip
|
||||||
|
pkg-config
|
||||||
];
|
];
|
||||||
|
|
||||||
dontConfigure = true;
|
dontConfigure = true;
|
||||||
|
|
||||||
env = {
|
env = {
|
||||||
VCPKG_ROOT = "${vcpkg}/share/vcpkg";
|
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
|
buildPhase = let
|
||||||
vcpkg-install-opts = lib.concatStringsSep " " [
|
vcpkg-install-opts = lib.concatStringsSep " " [
|
||||||
''--x-buildtrees-root=$TMP/vcpkg_buildtrees''
|
''--x-buildtrees-root=$TMP/vcpkg_buildtrees''
|
||||||
''--x-packages-root=$TMP/vcpkg_packages''
|
''--x-packages-root=$TMP/vcpkg_packages''
|
||||||
''--downloads-root=${deps}''
|
''--downloads-root=$TMP/vcpkg_downloads''
|
||||||
''--x-manifest-root=.''
|
''--x-manifest-root=.''
|
||||||
''--x-install-root=$TMP/vcpkg_installed''
|
''--x-install-root=$TMP/vcpkg_installed''
|
||||||
];
|
];
|
||||||
|
|
||||||
cmake-opts = lib.concatStringsSep " " [
|
cmake-opts = lib.concatStringsSep " " [
|
||||||
(lib.cmakeFeature "VCPKG_INSTALLED_DIR" "$TMP/vcpkg_installed")
|
(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_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_BUILD_TYPE" "Release")
|
||||||
(lib.cmakeFeature "CMAKE_TOOLCHAIN_FILE" "${vcpkg}/share/vcpkg/scripts/buildsystems/vcpkg.cmake")
|
|
||||||
# (lib.cmakeFeature "CMAKE_CXX_COMPILER" "g++")
|
# NOTE: this does not work for cross compilation, because
|
||||||
# (lib.cmakeFeature "CMAKE_C_COMPILER" "gcc")
|
# you already specify a toolchain. We have to somehow
|
||||||
# -DVCPKG_INSTALL_OPTIONS="${vcpkg-install-opts}"
|
# 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 ''
|
in ''
|
||||||
runHook preBuild
|
runHook preBuild
|
||||||
|
|
||||||
|
cp -r "${deps}/vcpkg_downloads" "$TMP/"
|
||||||
|
|
||||||
vcpkg install ${vcpkg-install-opts}
|
vcpkg install ${vcpkg-install-opts}
|
||||||
|
|
||||||
cmake ${cmake-opts} -S . -B build -G Ninja
|
cmake ${cmake-opts} -S . -B build -G Ninja
|
||||||
|
|
|
@ -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
|
||||||
|
'';
|
||||||
|
})
|
Loading…
Reference in New Issue