blast: fix and enable strictDeps (#366620)
[NixPkgs.git] / pkgs / by-name / mo / moltenvk / package.nix
blobd758e29157738fa7c55a82705b1dbe0df9ed9c15
2   lib,
3   stdenv,
4   fetchFromGitHub,
5   fetchpatch2,
6   gitUpdater,
7   apple-sdk_15,
8   cereal,
9   libcxx,
10   glslang,
11   spirv-cross,
12   spirv-headers,
13   spirv-tools,
14   vulkan-headers,
15   xcbuildHook,
16   enableStatic ? stdenv.hostPlatform.isStatic,
17   # MoltenVK supports using private APIs to implement some Vulkan functionality.
18   # Applications that use private APIs can’t be distributed on the App Store,
19   # but that’s not really a concern for nixpkgs, so use them by default.
20   # See: https://github.com/KhronosGroup/MoltenVK/blob/main/README.md#metal_private_api
21   enablePrivateAPIUsage ? true,
24 stdenv.mkDerivation (finalAttrs: {
25   pname = "MoltenVK";
26   version = "1.2.11";
28   strictDeps = true;
30   buildInputs = [
31     apple-sdk_15
32     cereal
33     glslang
34     spirv-cross
35     spirv-headers
36     spirv-tools
37     vulkan-headers
38   ];
40   nativeBuildInputs = [ xcbuildHook ];
42   outputs = [
43     "out"
44     "bin"
45     "dev"
46   ];
48   src = fetchFromGitHub {
49     owner = "KhronosGroup";
50     repo = "MoltenVK";
51     rev = "v${finalAttrs.version}";
52     hash = "sha256-24qQnJ0RnJP2M4zSlSlQ4c4dVZtHutNiCvjrsCDw6wY=";
53   };
55   patches = [
56     # Cherry-pick patch to fix build failure due to a hardcoded SPIRV-Cross namespace.
57     # This can be dropped for MoltenVK 1.2.12.
58     (fetchpatch2 {
59       url = "https://github.com/KhronosGroup/MoltenVK/commit/856c8237ac3b32178caae3408effc35bedfdffa1.patch?full_index=1";
60       hash = "sha256-dVTop8sU19Swdb3ajbI+6S715NaxTqd7d0yQ/FDqxqY=";
61     })
62   ];
64   postPatch = ''
65     # Move `mvkGitRevDerived.h` to a stable location
66     substituteInPlace Scripts/gen_moltenvk_rev_hdr.sh \
67       --replace-fail '$'''{BUILT_PRODUCTS_DIR}' "$NIX_BUILD_TOP/$sourceRoot/build/include" \
68       --replace-fail '$(git rev-parse HEAD)' ${finalAttrs.src.rev}
70     # Modify MoltenVK Xcode projects to build with xcbuild and dependencies from nixpkgs.
71     for proj in MoltenVK MoltenVKShaderConverter; do
72       # Remove xcframework dependencies from the Xcode projects. The basic format is:
73       #     (children|files) = (
74       #         DCFD7F822A45BDA0007BBBF7 /* SPIRVCross.xcframework in Frameworks */,
75       #         etc
76       #     )
77       # This regex will only remove lines matching `xcframework` that are in these blocks
78       # to avoid accidentally corrupting the project.
79       sed -E -e '/(children|files) = /,/;/{/xcframework/d}' \
80         -i "$proj/$proj.xcodeproj/project.pbxproj"
81       # Ensure the namespace used is consistent with the spirv-cross package in nixpkgs.
82       substituteInPlace "$proj/$proj.xcodeproj/project.pbxproj" \
83         --replace-fail SPIRV_CROSS_NAMESPACE_OVERRIDE=MVK_spirv_cross SPIRV_CROSS_NAMESPACE_OVERRIDE=spirv_cross
84     done
85     substituteInPlace MoltenVKShaderConverter/MoltenVKShaderConverter.xcodeproj/project.pbxproj \
86       --replace-fail MetalGLShaderConverterTool MoltenVKShaderConverter \
87       --replace-fail MetalGLShaderConverter-macOS MoltenVKShaderConverter
89     # Don’t try to build `xcframework`s because `xcbuild` can’t build them.
90     sed -e '/xcframework/d' -i Scripts/package_all.sh
92     # Remove vendored dependency links.
93     find . -lname '*/External/*' -delete
95     # The library will be linked in the install phase regardless of version,
96     # so truncate it if it exists to avoid link failures.
97     test -f Scripts/create_dylib.sh && truncate --size 0 Scripts/create_dylib.sh
99     # Link glslang source because MoltenVK needs non-public headers to build.
100     mkdir -p build/include
101     ln -s "${glslang.src}" "build/include/glslang"
102   '';
104   env.NIX_CFLAGS_COMPILE = toString (
105     [
106       "-isystem ${lib.getDev libcxx}/include/c++/v1"
107       "-I${lib.getDev spirv-cross}/include/spirv_cross"
108       "-I${lib.getDev spirv-headers}/include/spirv/unified1"
110       # MoltenVK prints a lot of verbose output to the console out of
111       # the box; we adjust this to match Homebrew’s default log level.
112       "-DMVK_CONFIG_LOG_LEVEL=MVK_CONFIG_LOG_LEVEL_NONE"
113     ]
114     ++ lib.optional enablePrivateAPIUsage "-DMVK_USE_METAL_PRIVATE_API=1"
115   );
117   env.NIX_LDFLAGS = toString [
118     "-lglslang"
119     "-lSPIRV"
120     "-lSPIRV-Tools"
121     "-lSPIRV-Tools-opt"
122     "-lspirv-cross-msl"
123     "-lspirv-cross-core"
124     "-lspirv-cross-glsl"
125     "-lspirv-cross-reflect"
126   ];
128   preBuild = ''
129     NIX_CFLAGS_COMPILE+=" \
130       -I$NIX_BUILD_TOP/$sourceRoot/build/include \
131       -I$NIX_BUILD_TOP/$sourceRoot/Common"
132   '';
134   xcbuildFlags = [
135     "-configuration"
136     "Release"
137     "-project"
138     "MoltenVKPackaging.xcodeproj"
139     "-scheme"
140     "MoltenVK Package (macOS only)"
141     "-destination"
142     "generic/platform=macOS"
143     "-arch"
144     stdenv.hostPlatform.darwinArch
145   ];
147   postBuild =
148     if enableStatic then
149       ''
150         mkdir -p Package/Release/MoltenVK/static
151         cp Products/Release/libMoltenVK.a Package/Release/MoltenVK/static
152       ''
153     else
154       ''
155         # MoltenVK’s Xcode project builds the dylib, but it doesn’t seem to work with
156         # xcbuild. This is based on the script versions prior to 1.2.8 used.
157         mkdir -p Package/Release/MoltenVK/dynamic/dylib
158         clang++ -Wl,-all_load -Wl,-w \
159           -dynamiclib \
160           -compatibility_version 1.0.0 -current_version 1.0.0 \
161           -LProducts/Release \
162           -framework AppKit \
163           -framework CoreGraphics \
164           -framework Foundation \
165           -framework IOKit \
166           -framework IOSurface \
167           -framework Metal \
168           -framework QuartzCore \
169           -lobjc \
170           -lMoltenVKShaderConverter \
171           -lspirv-cross-reflect \
172           -install_name "$out/lib/libMoltenVK.dylib" \
173           -o Package/Release/MoltenVK/dynamic/dylib/libMoltenVK.dylib \
174           -force_load Products/Release/libMoltenVK.a
175       '';
177   installPhase = ''
178     runHook preInstall
180     libraryExtension=${if enableStatic then ".a" else ".dylib"}
181     packagePath=${if enableStatic then "static" else "dynamic/dylib"}
183     mkdir -p "$out/lib" "$out/share/vulkan/icd.d" "$bin/bin" "$dev"
185     cp Package/Release/MoltenVKShaderConverter/Tools/MoltenVKShaderConverter "$bin/bin"
186     cp -r Package/Release/MoltenVK/include "$dev"
187     cp Package/Release/MoltenVK/$packagePath/libMoltenVK$libraryExtension "$out/lib"
189     # Install ICD definition for use with vulkan-loader.
190     install -m644 MoltenVK/icd/MoltenVK_icd.json \
191       "$out/share/vulkan/icd.d/MoltenVK_icd.json"
192     substituteInPlace "$out/share/vulkan/icd.d/MoltenVK_icd.json" \
193       --replace-fail ./libMoltenVK.dylib "$out/lib/libMoltenVK.dylib"
195     runHook postInstall
196   '';
198   __structuredAttrs = true;
200   passthru.updateScript = gitUpdater {
201     rev-prefix = "v";
202     ignoredVersions = ".*-(beta|rc).*";
203   };
205   meta = {
206     description = "Vulkan Portability implementation built on top of Apple’s Metal API";
207     homepage = "https://github.com/KhronosGroup/MoltenVK";
208     changelog = "https://github.com/KhronosGroup/MoltenVK/releases";
209     maintainers = [ lib.maintainers.reckenrode ];
210     license = lib.licenses.asl20;
211     platforms = lib.platforms.darwin;
212   };