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

181 lines
4.6 KiB
Nix

{
lib
, src
, stdenv
, cmake
, vcpkg
, vcpkg-tool
, git
, curl
, cacert
, ninja
, pkg-config
, 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=";
"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 = [
# 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
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=$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_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")
# 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
cmake --build ./build --config Release
runHook postBuild
'';
# dontInstall = true;
installPhase = ''
runHook preInstall
mkdir "$out"
cmake --install ./build --prefix "$out"
runHook postInstall
'';
passthru.deps = deps;
}