Skip to content

[staging] llvmPackages*.libllvm: drop outputs.patch for llvm-config.patch - #152944

Merged
sternenseemann merged 2 commits into
NixOS:stagingfrom
sternenseemann:llvm-config-no-outputs.patch
Jan 4, 2022
Merged

sternenseemann merged 2 commits into
NixOS:stagingfrom
sternenseemann:llvm-config-no-outputs.patch

Conversation

@sternenseemann

@sternenseemann sternenseemann commented Dec 31, 2021 •

Copy link
Copy Markdown
Member

Due to gnu-install-dirs.patch llvm-config will return correct results
for --link-shared as well as --link-static even without this patch.

I am currently verifying this using the following script:

#!/bin/sh

versions=("5" "6" "7" "8" "9" "10" "11" "12" "13") # "git")

flags=(
  "--version"
  "--prefix"
  "--src-root"
  "--obj-root"
  "--bindir"
  "--includedir"
  "--libdir --link-shared"
  "--libdir --link-static"
  "--cmakedir"
  "--cppflags"
  "--cflags"
  "--cxxflags"
  "--ldflags"
  "--system-libs --link-shared"
  "--system-libs --link-static"
  "--libs --link-shared"
  "--libs --link-static"
  "--libnames --link-shared"
  "--libnames --link-static"
  "--libfiles --link-shared"
  "--libfiles --link-static"
  "--components"
  "--targets-built"
  "--host-target"
  "--build-mode"
  "--assertion-mode"
  "--build-system"
  "--has-rtti"
  "--has-global-isel"
  "--shared-mode"
)

outputs=("out" "dev" "lib")

for version in "${versions[@]}"; do
    echo "=== llvmPackages_$version ==="
    llvmConfig="$(nix-build -A llvmPackages_$version.libllvm.dev)/bin/llvm-config"

    transformOutputs=()

    for output in "${outputs[@]}"; do
        transformOutputs+=(
            "-e" "s|$(nix-build -A llvmPackages_$version.libllvm.$output)|$output|g"
        )
    done

    for flag in "${flags[@]}"; do
        "$llvmConfig" $flag | sed "${transformOutputs[@]}"
    done
done

Will take some build time, currently checked:

  • LLVM 5
  • LLVM 6
  • LLVM 7
  • LLVM 8
  • LLVM 9
  • LLVM 10
  • LLVM 11
  • LLVM 12
  • LLVM 13
  • LLVM HEAD
Motivation for this change
Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 22.05 Release Notes (or backporting 21.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@sternenseemann

sternenseemann commented Dec 31, 2021 •

Copy link
Copy Markdown
Member Author

Note that this will cause a regression for cross compilation (see also #148367), which can be (better) fixed along the lines of 46240c3.

Basically outputs.patch will set ActiveLibDir to a hard coded $lib/lib value (previously only if --link-shared, since #148367 in both cases). The problem appears only in the first place because llvm-config-native is produced by running cmake configure again with a different host platform. For this, basically all flags passed to cmake originally are dropped, so we need to pass CMAKE_INSTALL_* again explicitly to the native configure step in order for llvm-config-native to produce correct results.

cc @r-burns

I'll port the cross fix commit to all other LLVM versions and push it here later.

@ofborg ofborg Bot added the 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin and must target a staging branch. label Dec 31, 2021
@ofborg ofborg Bot added 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. labels Dec 31, 2021
@sternenseemann

Copy link
Copy Markdown
Member Author

The script pasted above produces the same output for normal llvm-config with and without this patch, which is great. I'll fix the cross situation tomorrow.

@sternenseemann
sternenseemann changed the base branch from master to staging January 1, 2022 10:54
@sternenseemann
sternenseemann force-pushed the llvm-config-no-outputs.patch branch from d29b0cf to b374593 Compare January 1, 2022 10:57
Due to gnu-install-dirs.patch llvm-config will return correct results
for --link-shared as well as --link-static even without this patch.
@sternenseemann
sternenseemann force-pushed the llvm-config-no-outputs.patch branch from b374593 to 9a0a6d3 Compare January 1, 2022 11:01

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will aim to make this one not necessary to fix soon :)

@sternenseemann
sternenseemann force-pushed the llvm-config-no-outputs.patch branch from 9a0a6d3 to 28147cc Compare January 1, 2022 21:27
@sternenseemann

Copy link
Copy Markdown
Member Author

Okay, so now --link-shared is broken for cross, but --link-static works… I'll need to investigate what's going on.

@Ericson2314
Ericson2314 marked this pull request as draft January 2, 2022 01:56
LLVM's build system creates NATIVE/bin/llvm-config by reexecuting cmake
with entirely new flags. Problematically, the `CMAKE_INSTALL_*` flags
are not inherited, causing llvm-config-native to return wrong
installation paths, e. g. CMAKE_INSTALL_LIBDIR would default to `lib64`
on x86_64-linux. Previously this was masked by outputs.patch which
replaced ActiveLibDir with a string passed in from Nix, however
`--cmakedir` for example would turn out to be wrong always, breaking
cross-compilation of e. g. lld.

Additionally LLVM_ENABLE_RTTI needs to be repassed, as it is used to
determine if RTTI is available. Passing LLVM_LINK_LLVM_DYLIB is crucial
if we are building LLVM non-statically: It influences the --shared-mode
flag (which should indicate that -lLLVM is enough to link all
components) and makes --link-shared work in the first place,
i. e. llvm-config-native believes the built shared libs don't exist
unless we repass this flag.

Passing LLVM_LINK_LLVM_DYLIB=ON, however, makes the native build produce
a full libLLVM.so which is something we don't want, so we introduce a
patch which forces llvm-config to link statically against the LLVM
components it needs.
@sternenseemann
sternenseemann force-pushed the llvm-config-no-outputs.patch branch from 28147cc to e10edcc Compare January 3, 2022 13:38
@sternenseemann

Copy link
Copy Markdown
Member Author

Okay, I think I've finally untangled it. As it turns out llvm-config uses LLVM_ENABLE_DYLIB and LLVM_LINK_DYLIB to determine whether a) libLLVM.so is available and b) whether it should be used by default (--shared-mode uses this for example).

Both these values are influenced by LLVM_LINK_LLVM_DYLIB=ON in our case, so we need to pass this via CROSS_TOOLCHAIN_FLAGS_NATIVE as well — except that does not work because building the full (!) libLLVM.so as part of NATIVE just fails (and is also undesireable). To work around this we need to patch llvm-config to link statically against the relevant LLVM components.

As you can see for LLVM 11, the BuildVariables.inc used by llvm-config is now practically equivalent (I'll leave the system libs part as an exercise for later…). What's left now is to check all the builds (again), i. e. I don't know yet if the patch applies everywhere.

--- build/NATIVE/tools/llvm-config/BuildVariables.inc	2022-01-03 13:06:13.482576812 +0100
+++ build/tools/llvm-config/BuildVariables.inc	2022-01-03 13:05:36.917470071 +0100
@@ -16,19 +16,19 @@
 //===----------------------------------------------------------------------===//
 
 #define LLVM_SRC_ROOT "/home/lukas/src/nix/nixpkgs/llvm-11.1.0.src"
-#define LLVM_OBJ_ROOT "/home/lukas/src/nix/nixpkgs/llvm-11.1.0.src/build/NATIVE"
+#define LLVM_OBJ_ROOT "/home/lukas/src/nix/nixpkgs/llvm-11.1.0.src/build"
 #define LLVM_CPPFLAGS "-D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS"
 #define LLVM_CFLAGS " -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS"
 #define LLVM_LDFLAGS ""
 #define LLVM_CXXFLAGS "-std=c++14   -fno-exceptions -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS"
 #define LLVM_BUILDMODE "Release"
 #define LLVM_LIBDIR_SUFFIX ""
-#define LLVM_INSTALL_BINDIR "/1rz4g4znpzjwh1xymhjpm42vipw92pr73vdgl6xs1hycac8kf2n9/bin"
-#define LLVM_INSTALL_LIBDIR "/0sra2y18lr3h6j58qjm0w46yv36h1wjmilb09n8aimdpivdymscx/lib"
-#define LLVM_INSTALL_INCLUDEDIR "/02qcpld1y6xhs5gz9bchpxaw0xdhmsp5dv88lh25r2ss44kh8dxz/include"
+#define LLVM_INSTALL_BINDIR "/nix/store/f8w4zhgy974b3hsvfif3b6fj4wfb5506-llvm-aarch64-unknown-linux-gnu-11.1.0/bin"
+#define LLVM_INSTALL_LIBDIR "/nix/store/609l10l12vdlgq5vc72sfi4iaiqnv5bh-llvm-aarch64-unknown-linux-gnu-11.1.0-lib/lib"
+#define LLVM_INSTALL_INCLUDEDIR "/nix/store/2ajndk42qqh609ml69lih3liwigvwc43-llvm-aarch64-unknown-linux-gnu-11.1.0-dev/include"
 #define LLVM_INSTALL_CMAKEDIR "/02qcpld1y6xhs5gz9bchpxaw0xdhmsp5dv88lh25r2ss44kh8dxz/lib/cmake/llvm/"
 #define LLVM_TARGETS_BUILT "AArch64 AMDGPU ARM AVR BPF Hexagon Lanai Mips MSP430 NVPTX PowerPC RISCV Sparc SystemZ WebAssembly X86 XCore"
-#define LLVM_SYSTEM_LIBS "-lrt -ldl -lpthread -lm -lxml2"
+#define LLVM_SYSTEM_LIBS "-lz -lrt -ldl -ltinfo -lpthread -lm -lxml2"
 #define LLVM_BUILD_SYSTEM "cmake"
 #define LLVM_HAS_RTTI 1
 #define LLVM_ENABLE_DYLIB 1
@@ -37,4 +37,4 @@
 #define LLVM_DYLIB_COMPONENTS "all"
 #define LLVM_DYLIB_VERSION "11"
 #define LLVM_HAS_GLOBAL_ISEL 1
-#define LLVM_TOOLS_INSTALL_DIR "/1rz4g4znpzjwh1xymhjpm42vipw92pr73vdgl6xs1hycac8kf2n9/bin"
+#define LLVM_TOOLS_INSTALL_DIR "/nix/store/f8w4zhgy974b3hsvfif3b6fj4wfb5506-llvm-aarch64-unknown-linux-gnu-11.1.0/bin"

@sternenseemann sternenseemann changed the title llvmPackages*.libllvm: drop outputs.patch for llvm-config.patch [staging] llvmPackages*.libllvm: drop outputs.patch for llvm-config.patch Jan 3, 2022
@sternenseemann
sternenseemann marked this pull request as ready for review January 3, 2022 16:05

@Ericson2314 Ericson2314 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@sternenseemann

Copy link
Copy Markdown
Member Author

Okay, I've tested all LLVM versions again: llvmPackages_*.llvm llvm-config's output hasn't changed to before this PR, pkgsCross.*.llvmPackages_*.llvm's llvm-config-native's output is identical to the former, except for the host LLVM triple (naturally) and the list of system libs. Since the missing system lib is always -ltinfo, I suspect this is related to the ncurses cross issue we have been working around and could be addressed with that in the future.

LLVM 5 has discrepancies in the output, listing less components than the native build, but I suppose this is a genuine LLVM cross compilation issue only present in the oldest LLVM version we ship, so I don't think it's worth fixing for now.

@sternenseemann
sternenseemann merged commit c70dca8 into NixOS:staging Jan 4, 2022
@github-actions

github-actions Bot commented Jan 7, 2022

Copy link
Copy Markdown
Contributor

Successfully created backport PR #153868 for staging-21.11.

sternenseemann added a commit that referenced this pull request Jun 17, 2022
Reverts #162607 / 1748887.

Reason for revert: This change caused llvm-config{,-native} to be unable
to find static archives bundled with LLVM, as has been [reported]. Ever
since #152944 using moveToOutput in LLVM is _evil_ because llvm-config
obtains it knowledge about the installation locations from the CMake
configure step.

Consequently a change like #162607 will need to be implemented by making
LLVM itself install the static archives to the correct location or by
adding yet another patch which updates llvm-config's knowledge of the
location. The latter is not desireable in my opinion, though, since it
is just asking for this sort of trouble: Before #152944 we had an
outputs.patch that did this sort of things which broke spectacularly in
edge cases.

Fixes #148117.

[reported]: #148117 (comment)
sternenseemann added a commit that referenced this pull request Jul 5, 2022
Reverts #162607 / 1748887.

Reason for revert: This change caused llvm-config{,-native} to be unable
to find static archives bundled with LLVM, as has been [reported]. Ever
since #152944 using moveToOutput in LLVM is _evil_ because llvm-config
obtains it knowledge about the installation locations from the CMake
configure step.

Consequently a change like #162607 will need to be implemented by making
LLVM itself install the static archives to the correct location or by
adding yet another patch which updates llvm-config's knowledge of the
location. The latter is not desireable in my opinion, though, since it
is just asking for this sort of trouble: Before #152944 we had an
outputs.patch that did this sort of things which broke spectacularly in
edge cases.

Fixes #148117.

[reported]: #148117 (comment)
github-actions Bot pushed a commit that referenced this pull request Jul 5, 2022
Reverts #162607 / 1748887.

Reason for revert: This change caused llvm-config{,-native} to be unable
to find static archives bundled with LLVM, as has been [reported]. Ever
since #152944 using moveToOutput in LLVM is _evil_ because llvm-config
obtains it knowledge about the installation locations from the CMake
configure step.

Consequently a change like #162607 will need to be implemented by making
LLVM itself install the static archives to the correct location or by
adding yet another patch which updates llvm-config's knowledge of the
location. The latter is not desireable in my opinion, though, since it
is just asking for this sort of trouble: Before #152944 we had an
outputs.patch that did this sort of things which broke spectacularly in
edge cases.

Fixes #148117.

[reported]: #148117 (comment)

(cherry picked from commit a2ed5b2)
sternenseemann added a commit that referenced this pull request Jul 5, 2022
Reverts #162607 / 1748887.

Reason for revert: This change caused llvm-config{,-native} to be unable
to find static archives bundled with LLVM, as has been [reported]. Ever
since #152944 using moveToOutput in LLVM is _evil_ because llvm-config
obtains it knowledge about the installation locations from the CMake
configure step.

Consequently a change like #162607 will need to be implemented by making
LLVM itself install the static archives to the correct location or by
adding yet another patch which updates llvm-config's knowledge of the
location. The latter is not desireable in my opinion, though, since it
is just asking for this sort of trouble: Before #152944 we had an
outputs.patch that did this sort of things which broke spectacularly in
edge cases.

Fixes #148117.

[reported]: #148117 (comment)

(cherry picked from commit a2ed5b2)
@rrbutani rrbutani added the 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related label May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin and must target a staging branch. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

llvm-config-native --libs --link-shared fails because it can't find libLLVM-9.so

3 participants