moltenvk: refactor to build without Xcode

Co-authored-by: Sandro <sandro.jaeckel@gmail.com>

- Use the 11.0 SDK instead of the 10.12 one on x86_64-darwin;
- Use `NIX_CFLAGS_COMPILE` and `NIX_LDFLAGS` to pass flags to the
  compiler instead of patching the Xcode project files; and
- Use xcbuild to build the project.
This commit is contained in:
Randy Eckenrode 2022-06-06 09:49:32 -04:00
parent 9bde9353e9
commit 9d2b95eef3
No known key found for this signature in database
GPG Key ID: 64C1CD4EC2A600D9
6 changed files with 79 additions and 263 deletions

View File

@ -86,61 +86,3 @@ index c23afce4..12ac12f4 100644
/* Begin XCBuildConfiguration section */
2FEA0AB824902F9F00EEF3AD /* Debug */ = {
isa = XCBuildConfiguration;
@@ -1586,11 +1544,12 @@
GCC_WARN_UNUSED_VARIABLE = YES;
GENERATE_MASTER_OBJECT_FILE = YES;
HEADER_SEARCH_PATHS = (
- "\"$(SRCROOT)/include\"",
- "\"$(SRCROOT)/../MoltenVKShaderConverter\"",
- "\"$(SRCROOT)/../MoltenVKShaderConverter/SPIRV-Cross\"",
- "\"$(SRCROOT)/../External/cereal/include\"",
- "\"${BUILT_PRODUCTS_DIR}\"",
+ "@@sourceRoot@@/outputs/include",
+ "@@sourceRoot@@/outputs/include/simd_workaround",
+ "@@sourceRoot@@/MoltenVKShaderConverter",
+ "@@cereal@@/include",
+ "@@spirv-cross@@/include/spirv_cross",
+ "@@vulkan-headers@@/include",
);
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MACH_O_TYPE = staticlib;
@@ -1600,6 +1559,10 @@
MVK_SKIP_DYLIB = "";
"MVK_SKIP_DYLIB[sdk=appletvsimulator*]" = YES;
"MVK_SKIP_DYLIB[sdk=iphonesimulator*]" = YES;
+ OTHER_CFLAGS = (
+ "-isystem",
+ "@@libcxx@@/include/c++/v1",
+ );
PRELINK_LIBS = "${CONFIGURATION_BUILD_DIR}/libMoltenVKShaderConverter.a";
PRODUCT_NAME = MoltenVK;
SKIP_INSTALL = YES;
@@ -1658,11 +1621,12 @@
GCC_WARN_UNUSED_VARIABLE = YES;
GENERATE_MASTER_OBJECT_FILE = YES;
HEADER_SEARCH_PATHS = (
- "\"$(SRCROOT)/include\"",
- "\"$(SRCROOT)/../MoltenVKShaderConverter\"",
- "\"$(SRCROOT)/../MoltenVKShaderConverter/SPIRV-Cross\"",
- "\"$(SRCROOT)/../External/cereal/include\"",
- "\"${BUILT_PRODUCTS_DIR}\"",
+ "@@sourceRoot@@/outputs/include",
+ "@@sourceRoot@@/outputs/include/simd_workaround",
+ "@@sourceRoot@@/MoltenVKShaderConverter",
+ "@@cereal@@/include",
+ "@@spirv-cross@@/include/spirv_cross",
+ "@@vulkan-headers@@/include",
);
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MACH_O_TYPE = staticlib;
@@ -1672,6 +1636,10 @@
MVK_SKIP_DYLIB = "";
"MVK_SKIP_DYLIB[sdk=appletvsimulator*]" = YES;
"MVK_SKIP_DYLIB[sdk=iphonesimulator*]" = YES;
+ OTHER_CFLAGS = (
+ "-isystem",
+ "@@libcxx@@/include/c++/v1",
+ );
PRELINK_LIBS = "${CONFIGURATION_BUILD_DIR}/libMoltenVKShaderConverter.a";
PRODUCT_NAME = MoltenVK;
SKIP_INSTALL = YES;

View File

@ -82,99 +82,3 @@ index c7842b63..d55f73ed 100644
productReference = A93903C71C57E9ED00FE90DC /* libMoltenVKShaderConverter.a */;
productType = "com.apple.product-type.library.static";
};
@@ -536,6 +518,17 @@
OTHER_LDFLAGS = (
"-ObjC",
"-w",
+ "-lMachineIndependent",
+ "-lGenericCodeGen",
+ "-lOGLCompiler",
+ "-lglslang",
+ "-lOSDependent",
+ "-lSPIRV",
+ "-lSPIRV-Tools",
+ "-lSPIRV-Tools-opt",
+ "-lspirv-cross-msl",
+ "-lspirv-cross-core",
+ "-lspirv-cross-glsl",
);
SDKROOT = macosx;
};
@@ -548,6 +541,17 @@
OTHER_LDFLAGS = (
"-ObjC",
"-w",
+ "-lMachineIndependent",
+ "-lGenericCodeGen",
+ "-lOGLCompiler",
+ "-lglslang",
+ "-lOSDependent",
+ "-lSPIRV",
+ "-lSPIRV-Tools",
+ "-lSPIRV-Tools-opt",
+ "-lspirv-cross-msl",
+ "-lspirv-cross-core",
+ "-lspirv-cross-glsl",
);
SDKROOT = macosx;
};
@@ -624,15 +628,24 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
HEADER_SEARCH_PATHS = (
- "$(inherited)",
- "\"$(SRCROOT)\"",
- "\"$(SRCROOT)/SPIRV-Cross\"",
- "\"$(SRCROOT)/glslang\"",
- "\"$(SRCROOT)/glslang/External/spirv-tools/include\"",
+ "@@sourceRoot@@/Common",
+ "@@glslang@@/include",
+ "@@spirv-cross@@/include/spirv_cross",
+ "@@spirv-tools@@/include",
+ "@@spirv-headers@@/include/spirv/unified1/",
);
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
+ LIBRARY_SEARCH_PATHS = (
+ "@@glslang@@/lib",
+ "@@spirv-cross@@/lib",
+ "@@spirv-tools@@/lib",
+ );
MACH_O_TYPE = staticlib;
MACOSX_DEPLOYMENT_TARGET = 10.11;
+ OTHER_CFLAGS = (
+ "-isystem",
+ "@@libcxx@@/include/c++/v1",
+ );
PRODUCT_NAME = MoltenVKShaderConverter;
SKIP_INSTALL = YES;
TVOS_DEPLOYMENT_TARGET = 9.0;
@@ -683,15 +696,24 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
HEADER_SEARCH_PATHS = (
- "$(inherited)",
- "\"$(SRCROOT)\"",
- "\"$(SRCROOT)/SPIRV-Cross\"",
- "\"$(SRCROOT)/glslang\"",
- "\"$(SRCROOT)/glslang/External/spirv-tools/include\"",
+ "@@sourceRoot@@/Common",
+ "@@glslang@@/include",
+ "@@spirv-cross@@/include/spirv_cross",
+ "@@spirv-tools@@/include",
+ "@@spirv-headers@@/include/spirv/unified1/",
);
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
+ LIBRARY_SEARCH_PATHS = (
+ "@@glslang@@/lib",
+ "@@spirv-cross@@/lib",
+ "@@spirv-tools@@/lib",
+ );
MACH_O_TYPE = staticlib;
MACOSX_DEPLOYMENT_TARGET = 10.11;
+ OTHER_CFLAGS = (
+ "-isystem",
+ "@@libcxx@@/include/c++/v1",
+ );
PRODUCT_NAME = MoltenVKShaderConverter;
SKIP_INSTALL = YES;
TVOS_DEPLOYMENT_TARGET = 9.0;

View File

@ -1,13 +0,0 @@
diff --git a/Scripts/create_dylib.sh b/Scripts/create_dylib.sh
index 5ba75f67..a0405067 100755
--- a/Scripts/create_dylib.sh
+++ b/Scripts/create_dylib.sh
@@ -57,6 +57,8 @@ ${MVK_SAN} \
-framework Metal ${MVK_IOSURFACE_FWK} -framework ${MVK_UX_FWK} -framework QuartzCore -framework CoreGraphics ${MVK_IOKIT_FWK} -framework Foundation \
--library-directory ${MVK_USR_LIB_DIR} \
-o "${MVK_BUILT_PROD_DIR}/dynamic/${MVK_DYLIB_NAME}" \
+-L@@sourceRoot@@/outputs/lib -L@@glslang@@/lib -L@@spirv-tools@@/lib -L@@spirv-cross@@/lib \
+-lobjc -lMoltenVKShaderConverter -lGenericCodeGen -lMachineIndependent -lOGLCompiler -lOSDependent -lSPIRV -lSPIRV-Tools -lSPIRV-Tools-opt -lspirv-cross-core -lspirv-cross-glsl -lspirv-cross-msl -lspirv-cross-reflect \
-force_load "${MVK_BUILT_PROD_DIR}/lib${PRODUCT_NAME}.a"
if test "$CONFIGURATION" = Debug; then

View File

@ -1,42 +1,34 @@
{ lib
, overrideCC
, stdenv
, stdenvNoCC
, fetchurl
, fetchFromGitHub
, cctools
, sigtool
, cereal
, libcxx
, glslang
, spirv-cross
, spirv-headers
, spirv-tools
, vulkan-headers
, xcbuild
, AppKit
, Foundation
, Libsystem
, MacOSX-SDK
, Metal
, QuartzCore
}:
# Even though the derivation is currently impure, it is written to build successfully using
# `xcbuild`. Once the SDK on x86_64-darwin is updated, it should be possible to switch from being
# an impure derivation.
#
# The `sandboxProfile` was copied from the iTerm2 derivation. In order to build you at least need
# the `sandbox` option set to `relaxed` or `false`. Xcode should be available in the default
# location.
let
libcxx.dev = "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr";
in
stdenvNoCC.mkDerivation (finalAttrs: {
stdenv.mkDerivation (finalAttrs: {
pname = "MoltenVK";
version = "1.1.9";
buildInputs = [
AppKit
Foundation
Metal
QuartzCore
];
buildInputs = [ AppKit Foundation Metal QuartzCore cereal ]
++ lib.attrValues finalAttrs.passthru;
nativeBuildInputs = [ cctools sigtool xcbuild ];
outputs = [ "out" "bin" "dev" ];
@ -50,13 +42,10 @@ stdenvNoCC.mkDerivation (finalAttrs: {
rev = "9bb8cfffb0eed010e07132282c41d73064a7a609";
hash = "sha256-YLn/Mxuk6mXPGtBBgfwky5Nl1TCAW6i2g+AZLzqVz+A=";
};
})).override {
inherit (finalAttrs.passthru) spirv-headers spirv-tools;
};
})).override { inherit (finalAttrs.passthru) spirv-headers spirv-tools; };
spirv-cross = spirv-cross.overrideAttrs (old: {
cmakeFlags = (old.cmakeFlags or [ ]) ++ [
"-DSPIRV_CROSS_NAMESPACE_OVERRIDE=MVK_spirv_cross"
];
cmakeFlags = (old.cmakeFlags or [ ])
++ [ "-DSPIRV_CROSS_NAMESPACE_OVERRIDE=MVK_spirv_cross" ];
src = fetchFromGitHub {
owner = "KhronosGroup";
repo = "SPIRV-Cross";
@ -79,9 +68,7 @@ stdenvNoCC.mkDerivation (finalAttrs: {
rev = "eed5c76a57bb965f2e1b56d1dc40b50910b5ec1d";
hash = "sha256-2Mr3HbhRslLpRfwHascl7e/UoPijhrij9Bjg3aCiqBM=";
};
})).override {
inherit (finalAttrs.passthru) spirv-headers;
};
})).override { inherit (finalAttrs.passthru) spirv-headers; };
vulkan-headers = vulkan-headers.overrideAttrs (old: {
src = fetchFromGitHub {
owner = "KhronosGroup";
@ -100,88 +87,95 @@ stdenvNoCC.mkDerivation (finalAttrs: {
};
patches = [
# Specify the libraries to link directly since XCFrameworks are not being used.
./createDylib.patch
# Move `mvkGitRevDerived.h` to a stable location
./gitRevHeaderStability.patch
# Fix the Xcode projects to play nicely with `xcbuild`.
./MoltenVKShaderConverter.xcodeproj.patch
./MoltenVK.xcodeproj.patch
];
postPatch = ''
substituteInPlace MoltenVKShaderConverter/MoltenVKShaderConverter.xcodeproj/project.pbxproj \
--replace @@sourceRoot@@ $(pwd) \
--replace @@libcxx@@ "${libcxx.dev}" \
--replace @@glslang@@ "${finalAttrs.passthru.glslang}" \
--replace @@spirv-cross@@ "${finalAttrs.passthru.spirv-cross}" \
--replace @@spirv-tools@@ "${finalAttrs.passthru.glslang.spirv-tools}" \
--replace @@spirv-headers@@ "${finalAttrs.passthru.glslang.spirv-headers}"
substituteInPlace MoltenVK/MoltenVK.xcodeproj/project.pbxproj \
--replace @@sourceRoot@@ $(pwd) \
--replace @@libcxx@@ "${libcxx.dev}" \
--replace @@cereal@@ "${cereal}" \
--replace @@spirv-cross@@ "${finalAttrs.passthru.spirv-cross}" \
--replace @@vulkan-headers@@ "${finalAttrs.passthru.vulkan-headers}"
substituteInPlace Scripts/create_dylib.sh \
--replace @@sourceRoot@@ $(pwd) \
--replace @@glslang@@ "${finalAttrs.passthru.glslang}" \
--replace @@spirv-tools@@ "${finalAttrs.passthru.glslang.spirv-tools}" \
--replace @@spirv-cross@@ "${finalAttrs.passthru.spirv-cross}"
# Move `mvkGitRevDerived.h` to a stable location
substituteInPlace Scripts/gen_moltenvk_rev_hdr.sh \
--replace @@sourceRoot@@ $(pwd) \
--replace '$'''{BUILT_PRODUCTS_DIR}' "$NIX_BUILD_TOP/$sourceRoot/build/include" \
--replace '$(git rev-parse HEAD)' ${finalAttrs.src.rev}
# Adding all of `usr/include` from the SDK results in header conflicts with `libcxx.dev`.
# Work around it by symlinking just the SIMD stuff needed by MoltenVK.
mkdir -p build/include
ln -s "${MacOSX-SDK}/usr/include/simd" "build/include"
'';
dontConfigure = true;
NIX_CFLAGS_COMPILE = [
"-isystem ${lib.getDev libcxx}/include/c++/v1"
"-I${finalAttrs.passthru.spirv-cross}/include/spirv_cross"
"-I${finalAttrs.passthru.spirv-headers}/include/spirv/unified1/"
];
buildPhase = ''
NIX_CFLAGS_COMPILE+=" \
-I$NIX_BUILD_TOP/$sourceRoot/build/include \
-I$NIX_BUILD_TOP/$sourceRoot/Common"
NIX_LDFLAGS+=" -L$NIX_BUILD_TOP/$sourceRoot/build/lib"
# Build each project on its own because `xcbuild` fails to build `MoltenVKPackaging.xcodeproj`.
derived_data_path=$(pwd)/DerivedData
build=$NIX_BUILD_TOP/$sourceRoot/build
mkdir -p "$build/bin" "$build/lib"
NIX_LDFLAGS+=" \
-lMachineIndependent \
-lGenericCodeGen \
-lOGLCompiler \
-lglslang \
-lOSDependent \
-lSPIRV \
-lSPIRV-Tools \
-lSPIRV-Tools-opt \
-lspirv-cross-msl \
-lspirv-cross-core \
-lspirv-cross-glsl"
pushd MoltenVKShaderConverter
/usr/bin/xcodebuild build \
-jobs $NIX_BUILD_CORES \
-derivedDataPath "$derived_data_path" \
-configuration Release \
-project MoltenVKShaderConverter.xcodeproj \
-scheme MoltenVKShaderConverter \
-arch ${stdenv.targetPlatform.darwinArch}
popd
mkdir -p outputs/bin outputs/lib
declare -A outputs=( [MoltenVKShaderConverter]=bin [libMoltenVKShaderConverter.a]=lib )
for output in "''${!outputs[@]}"; do
cp DerivedData/Build/Products/Release/$output "outputs/''${outputs[$output]}/$output"
xcodebuild build \
-jobs $NIX_BUILD_CORES \
-configuration Release \
-project MoltenVKShaderConverter.xcodeproj \
-scheme MoltenVKShaderConverter \
-arch ${stdenv.targetPlatform.darwinArch}
declare -A products=( [MoltenVKShaderConverter]=bin [libMoltenVKShaderConverter.a]=lib )
for product in "''${!products[@]}"; do
cp MoltenVKShaderConverter-*/Build/Products/Release/$product "$build/''${products[$product]}/$product"
done
popd
NIX_LDFLAGS+=" \
-lobjc \
-lMoltenVKShaderConverter \
-lspirv-cross-reflect"
pushd MoltenVK
/usr/bin/xcodebuild build \
-jobs $NIX_BUILD_CORES \
-derivedDataPath "$derived_data_path" \
-configuration Release \
-project MoltenVK.xcodeproj \
-scheme MoltenVK-macOS \
-arch ${stdenv.targetPlatform.darwinArch}
xcodebuild build \
-jobs $NIX_BUILD_CORES \
-configuration Release \
-project MoltenVK.xcodeproj \
-scheme MoltenVK-macOS \
-arch ${stdenv.targetPlatform.darwinArch}
cp MoltenVK-*/Build/Products/Release/dynamic/libMoltenVK.dylib "$build/lib/libMoltenVK.dylib"
popd
cp DerivedData/Build/Products/Release/dynamic/libMoltenVK.dylib outputs/lib/libMoltenVK.dylib
'';
installPhase = ''
mkdir -p "$out/lib" "$out/share/vulkan/icd.d" "$bin/bin" "$dev/include/MoltenVK"
cp outputs/bin/MoltenVKShaderConverter "$bin/bin/"
cp outputs/lib/libMoltenVK.dylib "$out/lib/"
cp build/bin/MoltenVKShaderConverter "$bin/bin/"
cp build/lib/libMoltenVK.dylib "$out/lib/"
cp MoltenVK/MoltenVK/API/* "$dev/include/MoltenVK"
${cctools}/bin/install_name_tool -id "$out/lib/libMoltenVK.dylib" "$out/lib/libMoltenVK.dylib"
# FIXME: https://github.com/NixOS/nixpkgs/issues/148189
/usr/bin/codesign -s - -f "$out/lib/libMoltenVK.dylib"
install -m644 MoltenVK/icd/MoltenVK_icd.json "$out/share/vulkan/icd.d/MoltenVK_icd.json"
substituteInPlace $out/share/vulkan/icd.d/MoltenVK_icd.json \
--replace ./libMoltenVK.dylib "$out/lib/libMoltenVK.dylib"
'';
sandboxProfile = ''
(allow file-read* file-write* process-exec mach-lookup)
; block homebrew dependencies
(deny file-read* file-write* process-exec mach-lookup (subpath "/usr/local") (with no-log))
postFixup = ''
install_name_tool -id "$out/lib/libMoltenVK.dylib" "$out/lib/libMoltenVK.dylib"
codesign -s - -f "$out/lib/libMoltenVK.dylib"
'';
meta = {
@ -189,7 +183,6 @@ stdenvNoCC.mkDerivation (finalAttrs: {
homepage = "https://github.com/KhronosGroup/MoltenVK";
changelog = "https://github.com/KhronosGroup/MoltenVK/releases";
maintainers = [ lib.maintainers.reckenrode ];
hydraPlatforms = [ ]; # Prevent building on Hydra until MoltenVK no longer requires Xcode.
license = lib.licenses.asl20;
platforms = lib.platforms.darwin;
};

View File

@ -1,14 +0,0 @@
diff --git a/Scripts/gen_moltenvk_rev_hdr.sh b/Scripts/gen_moltenvk_rev_hdr.sh
index c49ea4e6..9b7d13d1 100755
--- a/Scripts/gen_moltenvk_rev_hdr.sh
+++ b/Scripts/gen_moltenvk_rev_hdr.sh
@@ -2,7 +2,7 @@
# Record the MoltenVK GIT revision as a derived header file suitable for including in a build
MVK_GIT_REV=$(git rev-parse HEAD)
-MVK_HDR_FILE="${BUILT_PRODUCTS_DIR}/mvkGitRevDerived.h"
+MVK_HDR_FILE="@@sourceRoot@@/outputs/include/mvkGitRevDerived.h"
+mkdir -p $(dirname ${MVK_HDR_FILE})
echo "// Auto-generated by MoltenVK" > "${MVK_HDR_FILE}"
echo "static const char* mvkRevString = \"${MVK_GIT_REV}\";" >> "${MVK_HDR_FILE}"
-

View File

@ -148,7 +148,11 @@ impure-cmds // appleSourcePackages // chooseLibs // {
lsusb = callPackage ../os-specific/darwin/lsusb { };
moltenvk = callPackage ../os-specific/darwin/moltenvk { };
moltenvk = pkgs.darwin.apple_sdk_11_0.callPackage ../os-specific/darwin/moltenvk {
inherit (apple_sdk_11_0.frameworks) AppKit Foundation Metal QuartzCore;
inherit (apple_sdk_11_0) MacOSX-SDK Libsystem;
inherit (pkgs.darwin) cctools sigtool;
};
opencflite = callPackage ../os-specific/darwin/opencflite { };