Skip to content

cmake: rework LLVM/Clang cmake#3149

Closed
MaskRay wants to merge 1 commit intoziglang:masterfrom
MaskRay:cmake
Closed

cmake: rework LLVM/Clang cmake#3149
MaskRay wants to merge 1 commit intoziglang:masterfrom
MaskRay:cmake

Conversation

@MaskRay
Copy link

@MaskRay MaskRay commented Sep 1, 2019

  1. Use {LLVM,CLANG}_INCLUDE_DIRS from {LLVM,Clang}Config.cmake
    This makes it possible to use an LLVM/Clang source tree.

  2. Delete ZIG_STATIC_LLVM cmake: allow user to select static vs dynamic LLVM #2858

Building both libLLVM*.a and libLLVM*.so is unsupported in LLVM. The
LLVM libraries can be configured in 3 ways:

  • -DLLVM_LINK_LLVM_DYLIB=ON => link against libLLVM.so
  • -DLLVM_LINK_LLVM_DYLIB=OFF -DBUILD_SHARED_LIBS=OFF => link against libLLVM*.a
  • -DLLVM_LINK_LLVM_DYLIB=OFF -DBUILD_SHARED_LIBS=ON => link against libLLVM*.so

If libLLVM*.a are unavailable, ZIG_STATIC_LLVM will not work.
If libLLVM*.a are available, linking against libLLVM*.a is automatic.
So ZIG_STATIC_LLVM is not useful and can only be confusing.

  1. If CLANG_LINK_CLANG_DYLIB (available since clang 9) is ON,
    libclang-cpp.so instead of libclang*.so will be linked. It currently
    does not work because zig_cpp/libembedded_lld_lib.a has global
    constructors that conflict with libclang-cpp.so

Succeeded to build zig with

  • releases.llvm.org clang+llvm 8 prebuilt archive -DBUILD_SHARED_LIBS=off
  • prereleases.llvm.org/ clang+llvm 9 sources -DBUILD_SHARED_LIBS=off
  • prereleases.llvm.org/ clang+llvm 9 sources -DBUILD_SHARED_LIBS=off -DLLVM_LINK_LLVM_DYLIB=on -DCLANG_LINK_CLANG_DYLIB=on
    Succeeded to link zig0 but hit the conflicting global constructor issue when running zig0.
  • clang+llvm git master -DBUILD_SHARED_LIBS=on if I remove version requirement

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this option is supported by clang < 9. Perhaps target this PR at the zig llvm9 branch?

Copy link
Author

@MaskRay MaskRay Sep 1, 2019

Choose a reason for hiding this comment

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

No. Users have to specify -DCLANG_LINK_CLANG_DYLIB explicitly to enable this code path. The code is here so that:

  • after the global constructor conflict caused by the embedded lld is fixed
  • after the upstream exports CLANG_LINK_CLANG_DYLIB as cmake variable

This configuration can work automatically.

@MaskRay
Copy link
Author

MaskRay commented Sep 1, 2019

https://dev.azure.com/ziglang/zig/_build/results?buildId=3140

CMake Error at /usr/lib/llvm-6.0/lib/cmake/clang/ClangTargets.cmake:490 (message):

The build configuration needs something like -DCMAKE_PREFIX_PATH=/usr/lib/llvm-8.0

Or probably uninstall libclang-6-dev

cmake/Findlld.cmake is not touched in this patch. The upstream does not provide LLDConfig.cmake.

In llvm/MC/MCTargetOptionsCommandFlags.inc:

static cl::opt<bool> RelaxAll("mc-relax-all",
                       cl::desc("When used with filetype=obj, "
                                "relax all fixups in the emitted object file"));

Both libclang-cpp.so and libembedded_lld_lib.a have this global constructor.

% readelf -s zig_cpp/libembedded_lld_lib.a | grep _ZL8RelaxAll
   150: 0000000000000000   160 OBJECT  LOCAL  DEFAULT 1615 _ZL8RelaxAll
% readelf -s ~/Dev/llvm-9.0.0rc2/Dylib/lib/libclang-cpp.so | grep _ZL8RelaxAll
 23221: 00000000025e7120   160 OBJECT  LOCAL  DEFAULT   24 _ZL8RelaxAll
% ./zig0 
: CommandLine Error: Option 'mc-relax-all' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options

The problem is that embedded_lld_lib is built as a static archive:

add_library(embedded_lld_lib STATIC ${EMBEDDED_LLD_LIB_SOURCES})

@andrewrk
Copy link
Member

andrewrk commented Sep 1, 2019

What problem are you trying to solve?

CMakeLists.txt Outdated
Copy link
Member

Choose a reason for hiding this comment

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

The main point of this cmake file and the stage1 source code is to bootstrap from C++. So supporting a wide range of C++ compiler versions and cmake versions is part what this accomplishes. Requiring a significantly newer cmake is a big problem.

1. Use {LLVM,CLANG}_INCLUDE_DIRS from {LLVM,Clang}Config.cmake
  This makes it possible to use an LLVM/Clang source tree.

2. Delete ZIG_STATIC_LLVM

Building both libLLVM*.a and libLLVM*.so is unsupported in LLVM. The
LLVM libraries can be configured in 3 ways:

* -DLLVM_LINK_LLVM_DYLIB=ON => link against libLLVM.so
* -DLLVM_LINK_LLVM_DYLIB=OFF -DBUILD_SHARED_LIBS=OFF => link against libLLVM*.a
* -DLLVM_LINK_LLVM_DYLIB=OFF -DBUILD_SHARED_LIBS=ON => link against libLLVM*.so

If libLLVM*.a are unavailable, ZIG_STATIC_LLVM will not work.
If libLLVM*.a are available, linking against libLLVM*.a is automatic.
So ZIG_STATIC_LLVM is not useful and can only be confusing.

3. If CLANG_LINK_CLANG_DYLIB (available since clang 9) is ON,
libclang-cpp.so instead of libclang*.so will be linked. It currently
does not work because zig_cpp/libembedded_lld_lib.a has global
constructors that conflict with libclang-cpp.so
@MaskRay
Copy link
Author

MaskRay commented Sep 1, 2019

What problem are you trying to solve?

This makes it possible to use an LLVM/Clang source tree.

Zig cannot be built by using a LLVM/Clang source tree.

  1. Delete ZIG_STATIC_LLVM which does not make lots of sense.

@andrewrk
Copy link
Member

andrewrk commented Sep 1, 2019

What is a LLVM/clang source tree?

@MaskRay
Copy link
Author

MaskRay commented Sep 2, 2019

What is a LLVM/clang source tree?

Point -DCMAKE_PREFIX_PATH to a LLVM/Clang source tree where ninja install has not been executed.

% cd ~/Dev/llvm-9.0.0rc2
# Unpack cfe-9.0.0rc2.src.tar.xz at tools/clang
% cmake -GNinja -H. -BRelease -DCMAKE_BUILD_TYPE=Release
% ninja -C Release all
# In Release/lib/cmake/clang/ClangConfig.cmake, set(CLANG_INCLUDE_DIRS "/home/user/Dev/llvm-9.0.0rc2/tools/clang/include;/home/user/Dev/llvm-9.0.0rc2/Release/tools/clang/include")
% ~/Dev/llvm-9.0.0rc2/Release/bin/llvm-config --includedir
/home/user/Dev/llvm-9.0.0rc2/include
# llvm-config --includedir output does not include Release/tools/clang/include, so it is not suitable for use


% cd ~/Dev/zig
% LLVM=~/Dev/llvm-9.0.0rc2
% cmake -H. -BRelease -G Ninja -DCMAKE_PREFIX_PATH="$LLVM/Release;$LLVM/Release/tools/clang;$LLVM;$LLVM/tools/clang"
...
fatal error: llvm/Config/llvm-config.h: No such file or directory
#include "llvm/Config/llvm-config.h"

I know you'll call it an "unsupported" configuration, but this basically makes it very difficult to debug if you suspect there are bugs in LLVM/Clang.

@andrewrk andrewrk added this to the 0.6.0 milestone Sep 3, 2019
@shawnl
Copy link
Contributor

shawnl commented Sep 17, 2019

@andrewrk

What is a LLVM/clang source tree?

https://github.com/llvm/llvm-project

What problem are you trying to solve?

With this it becomes easier to bisect problems with llvm (or less likely the use of libclang). This would mainly only be useful on the llvm-9/truck branches.

@XVilka
Copy link

XVilka commented Sep 24, 2019

What about adding these two different types of the builds in the CI? So if any will be broken the problem will be immediately caught.

@shawnl
Copy link
Contributor

shawnl commented Sep 24, 2019 via email

@andrewrk andrewrk removed this from the 0.6.0 milestone Oct 1, 2019
@shawnl
Copy link
Contributor

shawnl commented Nov 7, 2019

I am still interested in this. turns out zig built against llvm 9 can't link against llvm 9.0.1

undefined symbol: _ZTIN4llvm17DiagnosticHandlerE, version LLVM_9

@andrewrk
Copy link
Member

Sorry, I'm not willing to support this use case. For this use case (working on LLVM itself, testing using the Zig frontend), I suggest a third-party, community-maintained project which is a fork of Zig with modified build system to tightly couple with LLVM's build system. But I am not willing to maintain such a project in the ziglang/zig repository.

@andrewrk andrewrk closed this Nov 26, 2019
@MaskRay
Copy link
Author

MaskRay commented Nov 26, 2019

One would not say this if they looked at the clean-up of the build system. Though, it was your choice, and I respect that. Bye.

@MaskRay MaskRay deleted the cmake branch November 26, 2019 04:50
@andrewrk
Copy link
Member

I certainly looked at it. It does look cleaner, but supports less system environments than status quo, mainly because it introduces a cmake dependency on a Clang cmake file, which is not always available. That's also not a viable way for the self-hosted compiler to depend on libclang.

But the bottom line is that the thing you're trying to do, is too much of a burden for me and other zig team members to maintain in this repository. I humbly ask for this maintenance work to be done externally.

If your goal was simply to fix the build for a particular system, in which the build was failing, I would be of course be happy to merge such changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants