nix-vcpkg-cross-experiment/nix/packages/test/package.nix

181 lines
4.6 KiB
Nix
Raw Normal View History

2024-06-22 16:38:19 +02:00
{
lib
, src
, stdenv
, cmake
, vcpkg
, vcpkg-tool
, git
, curl
, cacert
, ninja
, pkg-config
, zip
}:
let
2024-06-24 08:07:42 +02:00
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};
2024-06-22 16:38:19 +02:00
deps = stdenv.mkDerivation {
pname = "test-deps";
version = "0.0.1";
inherit src;
2024-06-24 08:07:42 +02:00
# 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.
2024-06-22 16:38:19 +02:00
outputHashAlgo = "sha256";
outputHashMode = "recursive";
outputHash = {
"x86_64-linux" = "sha256-nFIbhVp32y+TlFLhuLc0XhlGJZmADgKBo4pd78H3mg8=";
2024-06-24 08:07:42 +02:00
"aarch64-linux" = "sha256-nFIbhVp32y+TlFLhuLc0XhlGJZmADgKBo4pd78H3mg8=";
"armv7l-linux" = "sha256-nFIbhVp32y+TlFLhuLc0XhlGJZmADgKBo4pd78H3mg8=";
2024-06-22 16:38:19 +02:00
}.${stdenv.hostPlatform.system};
dontConfigure = true;
env = {
VCPKG_ROOT = "${vcpkg}/share/vcpkg";
2024-06-24 08:07:42 +02:00
VCPKG_DEFAULT_TRIPLET = vcpkgTargetTriplet;
VCPKG_DEFAULT_HOST_TRIPLET = vcpkgHostTriplet;
VCPKG_BINARY_SOURCES = "clear";
VCPKG_FORCE_SYSTEM_BINARIES = "1";
2024-06-22 16:38:19 +02:00
};
nativeBuildInputs = [
# TODO: is this a direct $PATH dependency of `vcpkg install`?
cmake
# TODO: fix overriding the envvars in build script
# vcpkg
# TODO: wrap these in the $PATH of vcpkg-tool
vcpkg-tool
git
curl
cacert
ninja
zip
# Only for fmt?
pkg-config
];
buildPhase = ''
runHook preBuild
mkdir -p "$out"
vcpkg install \
--x-buildtrees-root="$TMP/vcpkg_buildtrees" \
--x-packages-root="$TMP/vcpkg_packages" \
--only-downloads \
--downloads-root="$out/vcpkg_downloads"
runHook postBuild
'';
# --triplet="$VCPKG_DEFAULT_TRIPLET" \
# --host-triplet="$VCPKG_DEFAULT_HOST_TRIPLET"
dontInstall = true;
};
in
stdenv.mkDerivation {
pname = "test";
version = "0.0.1";
inherit src;
nativeBuildInputs = [
cmake
vcpkg-tool
git
curl
cacert
ninja
zip
2024-06-24 08:07:42 +02:00
pkg-config
2024-06-22 16:38:19 +02:00
];
dontConfigure = true;
env = {
VCPKG_ROOT = "${vcpkg}/share/vcpkg";
2024-06-24 08:07:42 +02:00
VCPKG_DEFAULT_TRIPLET = vcpkgTargetTriplet;
VCPKG_DEFAULT_HOST_TRIPLET = vcpkgHostTriplet;
VCPKG_BINARY_SOURCES = "clear";
VCPKG_FORCE_SYSTEM_BINARIES = "1";
2024-06-22 16:38:19 +02:00
};
buildPhase = let
vcpkg-install-opts = lib.concatStringsSep " " [
''--x-buildtrees-root=$TMP/vcpkg_buildtrees''
''--x-packages-root=$TMP/vcpkg_packages''
2024-06-24 08:07:42 +02:00
''--downloads-root=$TMP/vcpkg_downloads''
2024-06-22 16:38:19 +02:00
''--x-manifest-root=.''
''--x-install-root=$TMP/vcpkg_installed''
];
cmake-opts = lib.concatStringsSep " " [
(lib.cmakeFeature "VCPKG_INSTALLED_DIR" "$TMP/vcpkg_installed")
2024-06-24 08:07:42 +02:00
# (lib.cmakeFeature "VCPKG_TRACE_FIND_PACKAGE" "ON")
2024-06-22 16:38:19 +02:00
(lib.cmakeFeature "VCPKG_MANIFEST_INSTALL" "OFF")
2024-06-24 08:07:42 +02:00
(lib.cmakeFeature "VCPKG_TARGET_TRIPLET" vcpkgTargetTriplet)
(lib.cmakeFeature "VCPKG_HOST_TRIPLET" vcpkgHostTriplet)
(lib.cmakeFeature "VCPKG_ROOT" "${vcpkg}/share/vcpkg")
2024-06-22 16:38:19 +02:00
(lib.cmakeFeature "CMAKE_BUILD_TYPE" "Release")
2024-06-24 08:07:42 +02:00
# 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")
2024-06-22 16:38:19 +02:00
];
in ''
runHook preBuild
2024-06-24 08:07:42 +02:00
cp -r "${deps}/vcpkg_downloads" "$TMP/"
2024-06-22 16:38:19 +02:00
vcpkg install ${vcpkg-install-opts}
cmake ${cmake-opts} -S . -B build -G Ninja
cmake --build ./build --config Release
runHook postBuild
'';
# dontInstall = true;
installPhase = ''
runHook preInstall
mkdir "$out"
cmake --install ./build --prefix "$out"
runHook postInstall
'';
passthru.deps = deps;
}