ziglang / zig-bootstrap

take off every zig
368 stars 86 forks source link

(Solved) x86_64-linux-musl cannot compile because of wrong c++ std lib #152

Closed zd08135 closed 3 months ago

zd08135 commented 1 year ago

env:
centos 8 + gcc 12 / clang 14(with no libc++ installed)

build cmd
(use build.log because the error message is too large to scroll on my terminal/screen)

env CMAKE_BUILD_PARALLEL_LEVEL=24 ./build x86_64-linux-musl baseline > build.log 2>&1

the building of LLVM libs is ok but after that it complained:
(rootdir is /root/work/zig-build/zig-bootstrap)

[ 10%] Built target zig-wasm2c
Consolidate compiler generated dependencies of target zigcpp
Consolidate compiler generated dependencies of target zig1
[ 30%] Built target zig1
[ 70%] Built target zigcpp
Consolidate compiler generated dependencies of target zig2
[ 95%] Built target zig2
[100%] Building stage3
zig build-exe zig ReleaseFast native: error: the following command failed with 141 compilation errors:
/root/work/zig-build/zig-bootstrap/out/build-zig-host/zig2 build-exe --stack 33554432 /root/work/zig-build/zig-bootstrap/zig/src/main.zig /root/work/zig-build/zig-bootstrap/out/build-zig-host/zigcpp/libzigcpp.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangFrontendTool.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangCodeGen.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangFrontend.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangDriver.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangSerialization.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangSema.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangStaticAnalyzerFrontend.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangStaticAnalyzerCheckers.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangStaticAnalyzerCore.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangAnalysis.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangASTMatchers.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangAST.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangParse.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangSema.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangBasic.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangEdit.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangLex.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangARCMigrate.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangRewriteFrontend.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangRewrite.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangCrossTU.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangIndex.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangToolingCore.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangExtractAPI.a /root/work/zig-build/zig-bootstrap/out/host/lib/libclangSupport.a /root/work/zig-build/zig-bootstrap/out/host/lib/liblldMinGW.a /root/work/zig-build/zig-bootstrap/out/host/lib/liblldELF.a /root/work/zig-build/zig-bootstrap/out/host/lib/liblldCOFF.a /root/work/zig-build/zig-bootstrap/out/host/lib/liblldWasm.a /root/work/zig-build/zig-bootstrap/out/host/lib/liblldMachO.a /root/work/zig-build/zig-bootstrap/out/host/lib/liblldCommon.a -lLLVMWindowsManifest -lLLVMXRay -lLLVMLibDriver -lLLVMDlltoolDriver -lLLVMCoverage -lLLVMLineEditor -lLLVMXCoreDisassembler -lLLVMXCoreCodeGen -lLLVMXCoreDesc -lLLVMXCoreInfo -lLLVMX86TargetMCA -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMX86Desc -lLLVMX86Info -lLLVMWebAssemblyDisassembler -lLLVMWebAssemblyAsmParser -lLLVMWebAssemblyCodeGen -lLLVMWebAssemblyDesc -lLLVMWebAssemblyUtils -lLLVMWebAssemblyInfo -lLLVMVEDisassembler -lLLVMVEAsmParser -lLLVMVECodeGen -lLLVMVEDesc -lLLVMVEInfo -lLLVMSystemZDisassembler -lLLVMSystemZAsmParser -lLLVMSystemZCodeGen -lLLVMSystemZDesc -lLLVMSystemZInfo -lLLVMSparcDisassembler -lLLVMSparcAsmParser -lLLVMSparcCodeGen -lLLVMSparcDesc -lLLVMSparcInfo -lLLVMRISCVTargetMCA -lLLVMRISCVDisassembler -lLLVMRISCVAsmParser -lLLVMRISCVCodeGen -lLLVMRISCVDesc -lLLVMRISCVInfo -lLLVMPowerPCDisassembler -lLLVMPowerPCAsmParser -lLLVMPowerPCCodeGen -lLLVMPowerPCDesc -lLLVMPowerPCInfo -lLLVMNVPTXCodeGen -lLLVMNVPTXDesc -lLLVMNVPTXInfo -lLLVMMSP430Disassembler -lLLVMMSP430AsmParser -lLLVMMSP430CodeGen -lLLVMMSP430Desc -lLLVMMSP430Info -lLLVMMipsDisassembler -lLLVMMipsAsmParser -lLLVMMipsCodeGen -lLLVMMipsDesc -lLLVMMipsInfo -lLLVMLoongArchDisassembler -lLLVMLoongArchAsmParser -lLLVMLoongArchCodeGen -lLLVMLoongArchDesc -lLLVMLoongArchInfo -lLLVMLanaiDisassembler -lLLVMLanaiCodeGen -lLLVMLanaiAsmParser -lLLVMLanaiDesc -lLLVMLanaiInfo -lLLVMHexagonDisassembler -lLLVMHexagonCodeGen -lLLVMHexagonAsmParser -lLLVMHexagonDesc -lLLVMHexagonInfo -lLLVMBPFDisassembler -lLLVMBPFAsmParser -lLLVMBPFCodeGen -lLLVMBPFDesc -lLLVMBPFInfo -lLLVMAVRDisassembler -lLLVMAVRAsmParser -lLLVMAVRCodeGen -lLLVMAVRDesc -lLLVMAVRInfo -lLLVMARMDisassembler -lLLVMARMAsmParser -lLLVMARMCodeGen -lLLVMARMDesc -lLLVMARMUtils -lLLVMARMInfo -lLLVMAMDGPUTargetMCA -lLLVMAMDGPUDisassembler -lLLVMAMDGPUAsmParser -lLLVMAMDGPUCodeGen -lLLVMAMDGPUDesc -lLLVMAMDGPUUtils -lLLVMAMDGPUInfo -lLLVMAArch64Disassembler -lLLVMAArch64AsmParser -lLLVMAArch64CodeGen -lLLVMAArch64Desc -lLLVMAArch64Utils -lLLVMAArch64Info -lLLVMOrcJIT -lLLVMWindowsDriver -lLLVMMCJIT -lLLVMJITLink -lLLVMInterpreter -lLLVMExecutionEngine -lLLVMRuntimeDyld -lLLVMOrcTargetProcess -lLLVMOrcShared -lLLVMDWP -lLLVMDebugInfoLogicalView -lLLVMDebugInfoGSYM -lLLVMOption -lLLVMObjectYAML -lLLVMObjCopy -lLLVMMCA -lLLVMMCDisassembler -lLLVMLTO -lLLVMPasses -lLLVMCFGuard -lLLVMCoroutines -lLLVMipo -lLLVMVectorize -lLLVMLinker -lLLVMInstrumentation -lLLVMFrontendOpenMP -lLLVMFrontendOpenACC -lLLVMFrontendHLSL -lLLVMExtensions -lLLVMDWARFLinkerParallel -lLLVMDWARFLinker -lLLVMGlobalISel -lLLVMMIRParser -lLLVMAsmPrinter -lLLVMSelectionDAG -lLLVMCodeGen -lLLVMObjCARCOpts -lLLVMIRPrinter -lLLVMInterfaceStub -lLLVMFileCheck -lLLVMFuzzMutate -lLLVMTarget -lLLVMScalarOpts -lLLVMInstCombine -lLLVMAggressiveInstCombine -lLLVMTransformUtils -lLLVMBitWriter -lLLVMAnalysis -lLLVMProfileData -lLLVMSymbolize -lLLVMDebugInfoPDB -lLLVMDebugInfoMSF -lLLVMDebugInfoDWARF -lLLVMObject -lLLVMTextAPI -lLLVMMCParser -lLLVMIRReader -lLLVMAsmParser -lLLVMMC -lLLVMDebugInfoCodeView -lLLVMBitReader -lLLVMFuzzerCLI -lLLVMCore -lLLVMRemarks -lLLVMBitstreamReader -lLLVMBinaryFormat -lLLVMTargetParser -lLLVMTableGen -lLLVMSupport -lLLVMDemangle -lunwind -lc++ -lc -fstrip -OReleaseFast --cache-dir /root/work/zig-build/zig-bootstrap/zig/zig-cache --global-cache-dir /root/.cache/zig --name zig --mod build_options::/root/work/zig-build/zig-bootstrap/zig/zig-cache/c/e8807410929eeb2eda993300d590e527/options.zig --deps build_options -I /root/work/zig-build/zig-bootstrap/out/host/include -I /root/work/zig-build/zig-bootstrap/out/host/include -L /root/work/zig-build/zig-bootstrap/out/host/lib -L /root/work/zig-build/zig-bootstrap/out/host/lib -I /root/work/zig-build/zig-bootstrap/out/host/include -fno-build-id --zig-lib-dir /root/work/zig-build/zig-bootstrap/zig/lib --listen=- 
Build Summary: 1/3 steps succeeded; 1 failed (disable with -fno-summary)
compile transitive failure
+- zig build-exe zig ReleaseFast native 141 errors
   +- options cached
error: ld.lld: undefined symbol: std::_V2::system_category()
    note: referenced by SourceManager.cpp
    note:               SourceManager.cpp.o:(clang::SrcMgr::ContentCache::getBufferOrNone(clang::DiagnosticsEngine&, clang::FileManager&, clang::SourceLocation) const) in archive /root/work/zig-build/zig-bootstrap/out/host/lib/libclangBasic.a
    note: referenced by zig_clang.cpp
    note:               zig_clang.cpp.o:(ZigClangIsLLVMUsingSeparateLibcxx) in archive /root/work/zig-build/zig-bootstrap/out/build-zig-host/zigcpp/libzigcpp.a
    note: referenced by MemoryBuffer.cpp
    note:               MemoryBuffer.cpp.o:(llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>> getOpenFileImpl<llvm::MemoryBuffer>(int, llvm::Twine const&, unsigned long, unsigned long, long, bool, bool, std::optional<llvm::Align>)) in archive /root/work/zig-build/zig-bootstrap/out/host/lib/libLLVMSupport.a
    note: referenced 249 more times
error: ld.lld: undefined symbol: std::__throw_length_error(char const*)
    note: referenced by SourceManager.cpp
    note:               SourceManager.cpp.o:(clang::LineTableInfo::getLineTableFilenameID(llvm::StringRef)) in archive /root/work/zig-build/zig-bootstrap/out/host/lib/libclangBasic.a
    note: referenced by SourceManager.cpp
    note:               SourceManager.cpp.o:(clang::LineTableInfo::AddLineNote(clang::FileID, unsigned int, unsigned int, int, unsigned int, clang::SrcMgr::CharacteristicKind)) in archive /root/work/zig-build/zig-bootstrap/out/host/lib/libclangBasic.a
    note: referenced by SourceManager.cpp
    note:               SourceManager.cpp.o:(clang::SourceManager::createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>)) in archive /root/work/zig-build/zig-bootstrap/out/host/lib/libclangBasic.a
    note: referenced 4101 more times
error: ld.lld: undefined symbol: std::_Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)
    note: referenced by SourceManager.cpp
    note:               SourceManager.cpp.o:(std::map<clang::FileID, std::vector<clang::LineEntry, std::allocator<clang::LineEntry>>, std::less<clang::FileID>, std::allocator<std::pair<clang::FileID const, std::vector<clang::LineEntry, std::allocator<clang::LineEntry>>>>>::operator[](clang::FileID const&)) in archive /root/work/zig-build/zig-bootstrap/out/host/lib/libclangBasic.a
    note: referenced by YAMLParser.cpp
    note:               YAMLParser.cpp.o:(std::_Rb_tree_iterator<std::pair<llvm::StringRef const, llvm::StringRef>> std::_Rb_tree<llvm::StringRef, std::pair<llvm::StringRef const, llvm::StringRef>, std::_Select1st<std::pair<llvm::StringRef const, llvm::StringRef>>, std::less<llvm::StringRef>, std::allocator<std::pair<llvm::StringRef const, llvm::StringRef>>>::_M_emplace_hint_unique<std::piecewise_construct_t const&, std::tuple<llvm::StringRef&&>, std::tuple<>>(std::_Rb_tree_const_iterator<std::pair<llvm::StringRef const, llvm::StringRef>>, std::piecewise_construct_t const&, std::tuple<llvm::StringRef&&>&&, std::tuple<>&&)) in archive /root/work/zig-build/zig-bootstrap/out/host/lib/libLLVMSupport.a
    note: referenced by YAMLParser.cpp
    note:               YAMLParser.cpp.o:(std::_Rb_tree_iterator<std::pair<llvm::StringRef const, llvm::StringRef>> std::_Rb_tree<llvm::StringRef, std::pair<llvm::StringRef const, llvm::StringRef>, std::_Select1st<std::pair<llvm::StringRef const, llvm::StringRef>>, std::less<llvm::StringRef>, std::allocator<std::pair<llvm::StringRef const, llvm::StringRef>>>::_M_emplace_hint_unique<std::piecewise_construct_t const&, std::tuple<llvm::StringRef const&>, std::tuple<>>(std::_Rb_tree_const_iterator<std::pair<llvm::StringRef const, llvm::StringRef>>, std::piecewise_construct_t const&, std::tuple<llvm::StringRef const&>&&, std::tuple<>&&)) in archive /root/work/zig-build/zig-bootstrap/out/host/lib/libLLVMSupport.a
    note: referenced 1050 more times
error: ld.lld: undefined symbol: std::__throw_bad_array_new_length()

As I see, all the undefined symbol is gcc internal defined in libstdc++ while clang/zig's libc++ doesn't have. Seems that the LLVM libs are compiled using libstdc++ headers but will be linked by libc++ in 'Building stage3'

When I try to force all to use libstdc++ ( I know it is not recommended) I saw zig force using libc++ in the code. So I doubt whether the build script ever worked. If true, please give some details for how to build the dep env(I guess a custom clang with libc++ is used). If not, can anyone help to figure out how to make it work?

zd08135 commented 1 year ago

Hello guys, after a long time of hard work. I found a way to build zig successfully.

Using ubuntu 23.04 for building may work. if your machine is not, please use docker for compile as I did.

Steps:

  1. start a docker instance of ubuntu 23.04.
    If your machine is it, go to step 2.

    docker pull ubuntu:23.04
    docker run --itd --name ziglang-dev -v ${path_in_machine}:${path_in_docker} -w ${path_in_docker} ubuntu:23.04 /bin/bash
    docker exec -it ziglang-dev /bin/bash
  2. install dependency tools

    apt update
    apt install -y git gcc g++ cmake
    apt install -y vim tar xz wget

    your gcc --version maybe like this:

    root@02f2988b2401:~/work# gcc --version
    gcc (Ubuntu 12.2.0-17ubuntu1) 12.2.0
    Copyright (C) 2022 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Installing llvm & clang is not recommended, as zig-bootstrap contains the src itself clang & llvm libs installed in your os may conflict with the ones built, and cause some unexcepted problems

  1. download zig-bootstrap to your working directory either git clone this repo or download from https://ziglang.org/download/ is OK

    wget https://ziglang.org/builds/zig-bootstrap-0.11.0-dev.3334+cd1417dbd.tar.xz
    tar -xvf zig-bootstrap-0.11.0-dev.3334+cd1417dbd.tar.xz
    mv zig-bootstrap-0.11.0-dev.3334+cd1417dbd zig-bootstrap-0.11.0-dev.3334
    cd zig-bootstrap-0.11.0-dev.3334

    The next steps require your working directory changed to zig-bootstrap-0.11.0-dev.3334

  2. build now

    env CMAKE_BUILD_PARALLEL_LEVEL=24 ./build x86_64-linux-musl baseline > build.log 2>&1

    I recommend to redirect all the outputs and errors to a log file, because the whole building job may cost several hours depending on your hardware, you may go to drink coffee and back a moment later. If the building failed somewhere, your terminal will display tons of errors which makes debugging hard.

zd08135 commented 1 year ago

Although my issue solved, I prefer to keep this issue open until official docs come out the build steps in detail.

I added Solved in the title so anyone who quering may see this.

matu3ba commented 11 months ago

Issue #17018 as Zig bootstrapping bug should be fixed with https://github.com/ziglang/zig/commit/3cf71580c460a30bb52da6e2cbc4482a67bd2930. I had the same problem on ubuntu at work.

Suggestion to close or provide further information.

zd08135 commented 3 months ago

Sorry to reply too late, since it's solved, I will try it later, thks Let me close it now.