External Issue: Unable to build Chapel 1.25.0 under Gentoo Linux with CHPL_LLVM=system

18589, "preney", "Unable to build Chapel 1.25.0 under Gentoo Linux with CHPL_LLVM=system", "2021-10-16T22:09:15Z"

Summary of Problem

When using these steps to compile Chapel v1.25.0 everything compiles correctly:

    wget https://github.com/chapel-lang/chapel/releases/download/1.25.0/chapel-1.25.0.tar.gz
    tar xvf chapel-1.25.0.tar.gz
    cd chapel-1.25.0
    export CHPL_LLVM=bundled
    source util/setchplenv.bash
    make

however when using these steps to use the system-installed LLVM/Clang:

    wget https://github.com/chapel-lang/chapel/releases/download/1.25.0/chapel-1.25.0.tar.gz
    tar xvf chapel-1.25.0.tar.gz
    cd chapel-1.25.0
    export CHPL_LLVM=system
    export CHPL_LLVM_CONFIG=/usr/lib/llvm/11/bin/llvm-config
    export CHPL_HOST_COMPILER=llvm
    export CHPL_TARGET_COMPILER=llvm
    source util/setchplenv.bash
    make

compilation fails with these errors:

    ld.lld: error: unable to find library -lclangFrontend
    ld.lld: error: unable to find library -lclangSerialization
    ld.lld: error: unable to find library -lclangDriver
    ld.lld: error: unable to find library -lclangCodeGen
    ld.lld: error: unable to find library -lclangParse
    ld.lld: error: unable to find library -lclangSema
    ld.lld: error: unable to find library -lclangAnalysis
    ld.lld: error: unable to find library -lclangEdit
    ld.lld: error: unable to find library -lclangASTMatchers
    ld.lld: error: unable to find library -lclangAST
    ld.lld: error: unable to find library -lclangLex
    ld.lld: error: unable to find library -lclangBasic
    clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

Since these libclang libraries are not on my system I submitted a bug report to Gentoo Linux here:

and the response was to use -lclang-cpp. (Per [gentoo-dev] Packaging changes in LLVM 10 - gentoo-dev - Gentoo Mailing List Archives Gentoo as of Clang v10 was no longer using BUILD_SHARED_LIBS=ON when building Clang and instead uses the 'dylib' model).

I grep'd the chapel source code for these libraries and they are mentioned in third-party/llvm/Makefile.include-system so I changed the file so that:

    LLVM_CLANG_LIBS=-lclang-cpp

was set and I still get linker errors. After experimentation, starting with:

    export CHPL_LLVM=system
    export CHPL_LLVM_CONFIG=/usr/lib/llvm/11/bin/llvm-config
    export CHPL_HOST_COMPILER=llvm
    export CHPL_TARGET_COMPILER=llvm
    export CHPL_LLVM_DYNAMIC=1
    export CHPL_LIB_PIC=pic

and then editing third-party/llvm/Makefile.include-system so the following are set:

    LLVM_CLANG_LIBS=-lclang-cpp
    # Add -fPIC...
    LLVM_CXXFLAGS=-fPIC $(LLVM_CONFIG_CXXFLAGS) $(LLVM_MY_CXXFLAGS) -DHAVE_LLVM
    LLVM_CFLAGS=-fPIC $(LLVM_CONFIG_CFLAGS) -DHAVE_LLVM
    # Add -dynamic...
    LLVM_LIBS=-dynamic -shared -L$(LLVM_CONFIG_LIB_DIR) $(LLVM_CLANG_LIBS) $(LLVM_LLVM_LIBS)

and then running source util/setchplenv.bash followed by make yields a successful compile --but when chpl is run (including with no arguments) it immediately segfaults, e.g.,

    $ chpl
    Segmentation fault (core dumped)

What behavior did you observe when encountering this issue?

  • See above,

What behavior did you expect to observe?

  • A successful compilation using the system LLVM/Clang.

Is this a blocking issue with no known work-arounds?

  • A work-around is to use CHPL_LLVM=bundle.

There are no instructions given in Chapel concerning this, but, my system is running LLVM/Clang is v11.1.0 --not v11.0.1 as stated in third-party/llvm/README. The Chapel docs say LLVM/Clang v11 --which I am running.

I suppose it is possible that when system LLVM is being used, the Chapel build might be incorrectly using source code from the bundled code --not the system (e.g., via header files). If so then this could be causing issues. Since the organization of code in third-party/llvm doesn't appear to match pristine LLVM/Clang code, I did not replace any LLVM/Clang v11.0.1 code with the same from v11.1.0. (Additionally, there is no documentation concerning how to graft such in or how to remove the all bundled LLVM/Clang 11.0.1 code (since it shouldn't be used at all) from third-party/llvm --so I left such alone.)

In summary, the problem appears to be that the build code for Chapel assume the ability to statically link to LLVM/Clang libraries when using a system-installed LLVM/Clang. On its own CHPL_LLVM_DYNAMIC doesn't fix any issues: it fails to link (even if the -lclang-cpp edits are made as detailed above). One has to use -fPIC and -dynamic -shared as discussed above to get things to compile and link (but it segfaults when run). How might this be fixed so Chapel can be built using the system's LLVM/Clang v11? Thanks.

(ASIDE: All Chapel prerequisites are installed in the system.)