From c25f7f894fa6d9018a4b79607dfadef6b4cb7fae Mon Sep 17 00:00:00 2001 From: Artturin Date: Mon, 28 Aug 2023 01:19:58 +0300 Subject: [PATCH] stdenv: Add hack to fix cmake canExecute cross-compilation Fixes `pkgsCross.musl64.llvmPackages_16.clang.cc` on `x86_64-linux`, which used to fail with `/bin/sh: clang-tblgen: not found`. Same hack is used in other projects: https://github.com/search?q=%2FCMAKE_CROSSCOMPILING_EMULATOR.%2B%5C%2Fusr%5C%2Fbin%5C%2Fenv%2F+NOT+is%3Afork&type=code Comment from https://github.com/vsrinivas/fuchsia/blob/30435a9d0f0b67c94e3c70760b522c9f7fbbd6be/build/cmake/HostLinuxToolchain.cmake#L64 > Required to run host Linux executables during the build itself. > An example would be https://gitub.com/KhronosGroup/Vulkan-Loader and > its "asm_offset" program. > > NOTE: Alternatives have been tried unsuccessfully, i.e.: > > With $(set CMAKE_CROSSCOMPILING_EMULATOR), the build fails because > the CMake ninja/Make script tries to find the executable in the current > path, as in: > > [3/16] Generating gen_defines.asm > FAILED: loader/gen_defines.asm > cd /tmp/cc/build-Vulkan-Loader/loader && asm_offset GAS > /bin/sh: asm_offset: command not found > ninja: build stopped: subcommand failed. > > With $(set CMAKE_CROSSCOMPILING_EMULATOR ""), the build fails because > the shell cannot find the "" program as in: > > [3/16] Generating gen_defines.asm > FAILED: loader/gen_defines.asm > cd /tmp/cc/build-Vulkan-Loader/loader && "" /tmp/cc/build-Vulkan-Loader/loader/asm_offset GAS > /bin/sh: : command not found > ninja: build stopped: subcommand failed. > > It seems that the root of the problem comes from how the CMake function > cmCustomCommandGenerator::GetArgc0Location() computes the target > executable's location. At this point it's unclear whether this is a CMake > bug or a feature. Risicle discovered this hack. Co-authored-by: Robert Scott --- pkgs/stdenv/generic/make-derivation.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index 7a083660e449d..beba687e788ac 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -377,6 +377,8 @@ else let "-DCMAKE_HOST_SYSTEM_PROCESSOR=${stdenv.buildPlatform.uname.processor}" ] ++ lib.optionals (stdenv.buildPlatform.uname.release != null) [ "-DCMAKE_HOST_SYSTEM_VERSION=${stdenv.buildPlatform.uname.release}" + ] ++ lib.optionals (stdenv.buildPlatform.canExecute stdenv.hostPlatform) [ + "-DCMAKE_CROSSCOMPILING_EMULATOR=env" ]); mesonFlags =