terralang / terra

Terra is a low-level system programming language that is embedded in and meta-programmed by the Lua programming language.
terralang.org
Other
2.73k stars 205 forks source link

CommandLine Error under LLVM 18 (Ubuntu 22.04) #678

Open ligurio opened 3 weeks ago

ligurio commented 3 weeks ago

Compiled using CMake:

$ cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
<snipped>
$ cmake --build build/ --parallel
<snipped>
$ ./build/bin/terra -h
: CommandLine Error: Option 'o' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options
Aborted (core dumped)

Backtrace:

(gdb) run 
Starting program: /home/sergeyb/sources/cache/terra/build/bin/terra 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
: CommandLine Error: Option 'o' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options

Program received signal SIGABRT, Aborted.
__pthread_kill_implementation (no_tid=0, signo=6, threadid=140737325451776) at ./nptl/pthread_kill.c:44
44      ./nptl/pthread_kill.c: No such file or directory.
(gdb) bt
#0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=140737325451776) at ./nptl/pthread_kill.c:44
#1  __pthread_kill_internal (signo=6, threadid=140737325451776) at ./nptl/pthread_kill.c:78
#2  __GI___pthread_kill (threadid=140737325451776, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
#3  0x00007ffff7042476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4  0x00007ffff70287f3 in __GI_abort () at ./stdlib/abort.c:79
#5  0x0000555556c47a15 in llvm::report_fatal_error(llvm::Twine const&, bool) ()
#6  0x0000555556c47856 in llvm::report_fatal_error(char const*, bool) ()
#7  0x0000555556c334bd in (anonymous namespace)::CommandLineParser::addOption(llvm::cl::Option*, llvm::cl::SubCommand*) ()
#8  0x0000555556c24b8c in llvm::cl::Option::addArgument() ()
#9  0x0000555556a19aea in _GLOBAL__sub_I_CommandLineOpts.cpp ()
#10 0x00007ffff7029ebb in call_init (env=<optimized out>, argv=0x7fffffffdd68, argc=1) at ../csu/libc-start.c:145
#11 __libc_start_main_impl (main=0x555556a221c9 <main(int, char**)>, argc=1, argv=0x7fffffffdd68, init=<optimized out>, 
    fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffdd58) at ../csu/libc-start.c:379
#12 0x0000555556a21f05 in _start ()
(gdb) 

Distributor ID: Ubuntu Description: Ubuntu 22.04.4 LTS Release: 22.04 Codename: jammy

Similar issue: https://github.com/terralang/terra/issues/357

elliottslaughter commented 3 weeks ago

Like it says in #357, this is a packaging issue. If you follow the instructions in the README to build LLVM from source you will not see this.

ligurio commented 3 weeks ago

I've followed instructions in a README.md and built LLVM 13 from source. Building terra with LLVM 13 produces a huge number of linkage errors:

<snipped>
[ 99%] Building CXX object src/CMakeFiles/TerraExecutable.dir/linenoise.cpp.o                                                           
[ 99%] Building CXX object src/CMakeFiles/TerraExecutable.dir/main.cpp.o                                                                
[100%] Linking CXX executable ../bin/terra                                                                                              
/bin/ld: ../lib/libterra_s.a(ASTUnit.cpp.o): in function `(anonymousnamespace)::ASTInfoCollector::ReadHeaderSearchOptions(clang::Header
SearchOptions const&, llvm::StringRef, bool)':                                                                                          
ASTUnit.cpp(.text._ZN12_GLOBAL__N_116ASTInfoCollector23ReadHeaderSearchOptionsERKN5clang19HeaderSearchOptionsEN4llvm9StringRefEb+0xdc4): undefined reference to `llvm::SmallVectorBase<unsigned int>::mallocForGrow(unsigned long, unsigned long, unsigned long&)'             
/bin/ld: ../lib/libterra_s.a(ASTUnit.cpp.o): in function `llvm::SmallVectorTemplateBase<clang::FixItHint, false>::grow(unsigned long)': 
ASTUnit.cpp:(.text._ZN4llvm23SmallVectorTemplateBaseIN5clang9FixItHintELb0EE4growEm[_ZN4llvm23SmallVectorTemplateBaseIN5clang9FixItHintE
Lb0EE4growEm]+0x2a): undefined reference to `llvm::SmallVectorBase<unsigned int>::mallocForGrow(unsigned long, unsigned long, unsigned long&)'                                                                                                                                  
/bin/ld: ../lib/libterra_s.a(ASTUnit.cpp.o): in function `clang::ASTUnit::Save(llvm::StringRef)':                                       
ASTUnit.cpp:(.text._ZN5clang7ASTUnit4SaveEN4llvm9StringRefE+0x119): undefined reference to `llvm::writeFileAtomically(llvm::StringRef, llvm::StringRef, std::function<llvm::Error (llvm::raw_ostream&)>)'                                                                       
/bin/ld: ../lib/libterra_s.a(ASTUnit.cpp.o): in function `llvm::SmallVectorTemplateBase<clang::StoredDiagnostic, false>::grow(unsigned long)':                                                                                                                                  
ASTUnit.cpp:(.text._ZN4llvm23SmallVectorTemplateBaseIN5clang16StoredDiagnosticELb0EE4growEm[_ZN4llvm23SmallVectorTemplateBaseIN5clang16StoredDiagnosticELb0EE4growEm]+0x28): undefined reference to `llvm::SmallVectorBase<unsigned int>::mallocForGrow(unsigned long, unsigned long, unsigned long&)'                                                                                                                  
/bin/ld: ../lib/libterra_s.a(ASTUnit.cpp.o): in function`llvm::SmallVectorTemplateBase<clang::ASTUnit::StandaloneDiagnostic, false>::grow(unsigned long)':         
<snipped>                                        

Then I've followed instructions in a README.md and built LLVM 18 (llvm-18.1.3) from source. Instruction in a README missed several steps important for successful LLVM build. Building terra with LLVM 18 is failed on a linkage due to error:

[100%] Linking CXX executable ../bin/terra                                                                                              
[100%] Built target TerraLibraryShared                                                                                                  
[100%] Generating ../lib/terra.so                                                                                                       
[100%] Built target TerraLibrarySymlink                                                                                                 
/bin/ld: ../lib/libterra_s.a(tcwrapper.cpp.o):(.data.rel.ro._ZTI12CodeGenProxy[_ZTI12CodeGenProxy]+0x10): undefined reference to `typein
fo for clang::ASTConsumer'                                                                                                              
collect2: error: ld returned 1 exit status                                                                                              
gmake[2]: *** [src/CMakeFiles/TerraExecutable.dir/build.make:199: bin/terra] Error 1                                                    
gmake[1]: *** [CMakeFiles/Makefile2:403: src/CMakeFiles/TerraExecutable.dir/all] Error 2                                                
gmake: *** [Makefile:146: all] Error 2 

According to advice in ^1 I've rebuilt terra with CXXFLAGS=-fno-rtti and terra builds successfully. But on start:

: CommandLine Error: Option 'o' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options
Aborted (core dumped)

Please help.

terra: release-1.2.0-4-g7faa246

elliottslaughter commented 3 weeks ago

I don't have Ubuntu 22.04 personally, so I am using Docker to build. But this should give you a basis for debugging—if it works here and not on your machine, maybe you have some difference in your system configuration that is causing problems.

Note that the LLVM 18 source build is slightly different because LLVM changed the source distribution layout, but that other than the combined tarball and one additional flag -DLLVM_ENABLE_PROJECTS='clang', everything should be basically the same.

One other thing I'll mention: link errors like what you show me are relatively unusual. I actually cannot think of the last time I've seen something like that. I wonder if it's possible that you're not actually using your source builds of LLVM. If you accidentally mixed builds or used the wrong one, that would be a classic way to get the errors you indicated.

Configuration for Ubuntu 22.04 and LLVM 13

Dockerfile.ubuntu22-llvm13:

FROM ubuntu:22.04

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update -qq && \
    apt-get install -qq build-essential cmake git wget libpython3-dev python3-pip && \
    apt-get clean

RUN wget https://github.com/llvm/llvm-project/releases/download/llvmorg-13.0.0/llvm-13.0.0.src.tar.xz && \
    wget https://github.com/llvm/llvm-project/releases/download/llvmorg-13.0.0/clang-13.0.0.src.tar.xz && \
    tar xf llvm-13.0.0.src.tar.xz && \
    tar xf clang-13.0.0.src.tar.xz && \
    mv clang-13.0.0.src llvm-13.0.0.src/tools/clang && \
    mkdir build install && \
    cd build && \
    cmake ../llvm-13.0.0.src -DCMAKE_INSTALL_PREFIX=$PWD/../install -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_TERMINFO=OFF -DLLVM_ENABLE_LIBEDIT=OFF -DLLVM_ENABLE_ZLIB=OFF -DLLVM_ENABLE_LIBXML2=OFF -DLLVM_ENABLE_ASSERTIONS=OFF && \
    make install -j20 && \
    rm -rf build llvm-13.0.0.src *.tar.xz

RUN git clone https://github.com/terralang/terra.git && \
    cd terra/build && \
    cmake .. -DCMAKE_INSTALL_PREFIX=$PWD/../install -DCMAKE_PREFIX_PATH=$PWD/../../install && \
    make install -j20 && \
    ctest -j20

Build with:

docker build -f Dockerfile.ubuntu22-llvm13 .

Note that you may want to adjust the -j parameters to fit your machine.

Configuration for Ubuntu 22.04 and LLVM 18

Dockerfile.ubuntu22-llvm18:

FROM ubuntu:22.04

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update -qq && \
    apt-get install -qq build-essential cmake git wget libpython3-dev python3-pip && \
    apt-get clean

RUN wget https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/llvm-project-18.1.8.src.tar.xz && \
    tar xf llvm-project-18.1.8.src.tar.xz && \
    mkdir build install && \
    cd build && \
    cmake ../llvm-project-18.1.8.src/llvm -DCMAKE_INSTALL_PREFIX=$PWD/../install -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_TERMINFO=OFF -DLLVM_ENABLE_LIBEDIT=OFF -DLLVM_ENABLE_ZLIB=OFF -DLLVM_ENABLE_LIBXML2=OFF -DLLVM_ENABLE_ASSERTIONS=OFF -DLLVM_ENABLE_PROJECTS='clang' && \
    make install -j20 && \
    rm -rf build llvm-project-18.1.8.src *.tar.xz

RUN git clone https://github.com/terralang/terra.git && \
    cd terra/build && \
    cmake .. -DCMAKE_INSTALL_PREFIX=$PWD/../install -DCMAKE_PREFIX_PATH=$PWD/../../install && \
    make install -j20 && \
    ctest -j20

Build with:

docker build -f Dockerfile.ubuntu22-llvm18 .
ligurio commented 3 weeks ago

Many thanks! Finally, I can build it with LLVM 18.