diff --git a/doc/redirects.json b/doc/redirects.json index 879eb37bd369..2acb189f2b17 100644 --- a/doc/redirects.json +++ b/doc/redirects.json @@ -222,6 +222,9 @@ "sec-building-packages-with-llvm-using-clang-stdenv": [ "index.html#sec-building-packages-with-llvm-using-clang-stdenv" ], + "sec-darwin-availability-checks": [ + "index.html#sec-darwin-availability-checks" + ], "sec-darwin-libcxx-deployment-targets": [ "index.html#sec-darwin-libcxx-deployment-targets" ], diff --git a/doc/stdenv/platform-notes.chapter.md b/doc/stdenv/platform-notes.chapter.md index 06662bbc76fa..a6174b3e2008 100644 --- a/doc/stdenv/platform-notes.chapter.md +++ b/doc/stdenv/platform-notes.chapter.md @@ -47,6 +47,17 @@ See below for how to use a newer deployment target. For example, `std::print` depends on features that are only available on macOS 13.3 or newer. To make them available, set the deployment target to 13.3 using `darwinMinVersionHook`. +#### Package fails to build due to missing API availability checks {#sec-darwin-availability-checks} + +This is normally a bug in the package or a misconfigured deployment target. +* If it is using an API from a newer release (e.g., from macOS 26.0 while targeting macOS 14.0), it needs to use an availability check. + The code should be patched to use [`__builtin_available`](https://clang.llvm.org/docs/LanguageExtensions.html#objective-c-available). + Note that while the linked documentation is for Objective-C, it is applicable to C and C++ except that you use `__builtin_available` in place of `@available`. +* If the package intends to require the newer platform (i.e., it does not support running on older versions with reduced functionality), use `darwinMinVersionHook` to set the deployment target to the required version. + See below for how to use a newer deployment target. +* If the package actually handles this through some other mechanism (e.g., MoltenVK relies on the running platform’s MSL version), the error can be suppressed. + To suppress the error, add `-Wno-error=unguarded-availability` to `env.NIX_CFLAGS_COMPILE`. + #### Package requires a non-default SDK or fails to build due to missing frameworks or symbols {#sec-darwin-troubleshooting-using-sdks} In some cases, you may have to use a non-default SDK. diff --git a/pkgs/build-support/cc-wrapper/add-clang-cc-cflags-before.sh b/pkgs/build-support/cc-wrapper/add-clang-cc-cflags-before.sh index 2b7cd00783a5..865e0d054820 100644 --- a/pkgs/build-support/cc-wrapper/add-clang-cc-cflags-before.sh +++ b/pkgs/build-support/cc-wrapper/add-clang-cc-cflags-before.sh @@ -35,3 +35,7 @@ elif [[ $0 != *cpp ]]; then extraBefore+=(-mabi=@explicitAbiValue@) fi fi + +if [[ "@darwinMinVersion@" ]]; then + extraBefore+=(-Werror=unguarded-availability) +fi diff --git a/pkgs/by-name/mo/moltenvk/package.nix b/pkgs/by-name/mo/moltenvk/package.nix index 23680a517df9..635d6ceab848 100644 --- a/pkgs/by-name/mo/moltenvk/package.nix +++ b/pkgs/by-name/mo/moltenvk/package.nix @@ -51,6 +51,13 @@ stdenv.mkDerivation (finalAttrs: { }; postPatch = '' + # Update the deployment target for the minimum target used by nixpkgs. + while IFS= read -d "" proj; do + echo "Updating deployment target to ${stdenv.hostPlatform.darwinMinVersion}: $proj" + substituteInPlace "$proj" \ + --replace-fail 'MACOSX_DEPLOYMENT_TARGET = 10.15' "MACOSX_DEPLOYMENT_TARGET = $MACOSX_DEPLOYMENT_TARGET" + done < <(grep -Z -rl --include=project.pbxproj MACOSX_DEPLOYMENT_TARGET) + # Move `mvkGitRevDerived.h` to a stable location substituteInPlace Scripts/gen_moltenvk_rev_hdr.sh \ --replace-fail '$'''{BUILT_PRODUCTS_DIR}' "$NIX_BUILD_TOP/$sourceRoot/build/include" \ @@ -91,7 +98,11 @@ stdenv.mkDerivation (finalAttrs: { ''; env.NIX_CFLAGS_COMPILE = toString ( - lib.optional (stdenv.cc.libcxx != null) "-isystem ${lib.getInclude stdenv.cc.libcxx}/include/c++/v1" + # MoltenVK does its own checks for availability by probing the version at runtime and checking the MSL version. + [ "-Wno-error=unguarded-availability" ] + ++ lib.optional ( + stdenv.cc.libcxx != null + ) "-isystem ${lib.getInclude stdenv.cc.libcxx}/include/c++/v1" ++ [ "-I${lib.getDev spirv-cross}/include/spirv_cross" "-I${lib.getDev spirv-headers}/include/spirv/unified1"