Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake: Improve usage of llvm-config (esp. for consistent static/shared linking) #12136

Merged
merged 4 commits into from Jul 26, 2022

Conversation

topolarity
Copy link
Contributor

@topolarity topolarity commented Jul 16, 2022

This commit reworks the LLVM/Clang/LLD discovery process for CMake.

llvm-config really wraps two sets of libraries that exist in parallel in an installation: shared and static. Building LLVM according to the wiki gives you only static libs. However, building with -DLLVM_BUILD_LLVM_DYLIB=ON -DLLVM_LINK_LLVM_DYLIB=ON means that your LLVM/Clang installation with have both sets of libraries (which is usually the case for system-provided LLVM/Clang).

In that case, llvm-config reports entirely different libraries and dependencies for --link-static versus --link-shared

This include several changes to handle this better:

  1. Search for llvm-config from most preferred directory to least, skipping any candidate that is the wrong version or doesn't support the requested link mode ("static" or "shared")
  2. -DZIG_PREFER_CLANG_CPP_DYLIB has been renamed to -DZIG_SHARED_LLVM, to better align with -DZIG_STATIC_LLVM.
  3. Only search for Clang in the same directory where LLVM was located (or CMAKE_LIBRARY_PATH)
  4. Use llvm-config to find include directories
  5. LLVM's link mode is forwarded to Clang, so that they are each linked either statically or dynamically
  6. Use "--link-static" when querying "--system-libs" from llvm-config, so that this will include libz and other dependencies for statically linking LLD

@topolarity
Copy link
Contributor Author

IIUC, -DZIG_PREFER_CLANG_CPP_DYLIB is used for Homebrew, so let me know if we need to keep the old name to avoid thrash for users

@topolarity
Copy link
Contributor Author

Any extra steps I need to take to make sure this doesn't break zig-bootstrap? Or will the CI already cover that?

@BratishkaErik
Copy link
Contributor

BratishkaErik commented Jul 16, 2022

I have only shared libraries, and before bc1c5e2 it worked fine.
cmake -DZIG_SHARED_LLVM=true ..
Output:

-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring zig version 0.10.0-dev.3013+e867127a6
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBinaryFormat.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBitstreamReader.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRemarks.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMCore.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBitReader.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDebugInfoCodeView.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMC.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMCParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMTextAPI.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMObject.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDebugInfoDWARF.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMProfileData.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAnalysis.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBitWriter.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMTransformUtils.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAggressiveInstCombine.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMInstCombine.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMScalarOpts.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMTarget.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMFuzzMutate.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMFileCheck.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMInterfaceStub.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMIRReader.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSelectionDAG.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDebugInfoMSF.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAsmPrinter.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMIRParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMGlobalISel.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDWARFLinker.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMExtensions.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMFrontendOpenACC.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMFrontendOpenMP.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMInstrumentation.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLinker.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMVectorize.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMipo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMObjCARCOpts.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMCoroutines.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMCFGuard.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMPasses.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLTO.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMCDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMCA.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMObjectYAML.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMOption.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDebugInfoGSYM.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDebugInfoPDB.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSymbolize.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDWP.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMOrcShared.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMOrcTargetProcess.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRuntimeDyld.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMExecutionEngine.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMInterpreter.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMJITLink.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMCJIT.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMOrcJIT.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMXCoreInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMXCoreDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMXCoreCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMXCoreDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAVRInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAVRDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAVRCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAVRAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAVRDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMWebAssemblyInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMWebAssemblyUtils.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMWebAssemblyDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMWebAssemblyCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMWebAssemblyAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMWebAssemblyDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSystemZInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSystemZDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSystemZCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSystemZAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSystemZDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRISCVInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRISCVDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRISCVCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRISCVAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRISCVDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSparcInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSparcDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSparcCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSparcAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSparcDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAArch64Info.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAArch64Utils.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAArch64Desc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAArch64CodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAArch64AsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAArch64Disassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMSP430Info.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMSP430Desc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMSP430CodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMSP430AsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMSP430Disassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMHexagonInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMHexagonDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMHexagonAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMHexagonCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMHexagonDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLanaiInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLanaiDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLanaiAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLanaiCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLanaiDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMVEInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMVEDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMVECodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMVEAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMVEDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMipsInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMipsDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMipsCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMipsAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMipsDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMNVPTXInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMNVPTXDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMNVPTXCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMPowerPCInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMPowerPCDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMPowerPCCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMPowerPCAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMPowerPCDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBPFInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBPFDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBPFCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBPFAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBPFDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMX86Info.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMX86Desc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMX86CodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMX86AsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMX86Disassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMX86TargetMCA.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMARMInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMARMUtils.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMARMDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMARMCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMARMAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMARMDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAMDGPUInfo.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAMDGPUUtils.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAMDGPUDesc.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAMDGPUCodeGen.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAMDGPUAsmParser.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAMDGPUDisassembler.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAMDGPUTargetMCA.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLineEditor.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMCoverage.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDlltoolDriver.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLibDriver.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMXRay.a
llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMWindowsManifest.a
-- Found llvm: /usr/lib/llvm/14/lib64/libLLVM-14.so (Required is at least version "14") 
-- Found clang: /usr/lib/llvm/14/lib64/libclang-cpp.so.14 (Required is at least version "14") 
-- Could NOT find lld (missing: LLD_LIBRARIES LLD_INCLUDE_DIRS) (Required is at least version "14")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Configuring done
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
LLD_INCLUDE_DIRS (ADVANCED)
   used as include directory in directory /home/bratishkaerik/zig
   used as include directory in directory /home/bratishkaerik/zig
   used as include directory in directory /home/bratishkaerik/zig
   used as include directory in directory /home/bratishkaerik/zig
   used as include directory in directory /home/bratishkaerik/zig
   used as include directory in directory /home/bratishkaerik/zig
   used as include directory in directory /home/bratishkaerik/zig
   used as include directory in directory /home/bratishkaerik/zig
   used as include directory in directory /home/bratishkaerik/zig
   used as include directory in directory /home/bratishkaerik/zig
   used as include directory in directory /home/bratishkaerik/zig
   used as include directory in directory /home/bratishkaerik/zig

CMake Error in CMakeLists.txt:
  Found relative path while evaluating include directories of
  "embedded_softfloat":

    "LLD_INCLUDE_DIRS-NOTFOUND"



CMake Error in CMakeLists.txt:
  Found relative path while evaluating include directories of "zigcpp":

    "LLD_INCLUDE_DIRS-NOTFOUND"



CMake Error in CMakeLists.txt:
  Found relative path while evaluating include directories of "opt_c_util":

    "LLD_INCLUDE_DIRS-NOTFOUND"



CMake Error in CMakeLists.txt:
  Found relative path while evaluating include directories of "zigstage1":

    "LLD_INCLUDE_DIRS-NOTFOUND"



CMake Error in CMakeLists.txt:
  Found relative path while evaluating include directories of "zig0":

    "LLD_INCLUDE_DIRS-NOTFOUND"



CMake Error in CMakeLists.txt:
  Found relative path while evaluating include directories of "zig":

    "LLD_INCLUDE_DIRS-NOTFOUND"



-- Generating done
CMake Generate step failed.  Build files cannot be regenerated correctly.

@topolarity
Copy link
Contributor Author

Thanks for the report. I'll look into it.

Can you also share the output before this change, for comparison?

@BratishkaErik
Copy link
Contributor

BratishkaErik commented Jul 16, 2022

Thanks for the report. I'll look into it.

Can you also share the output before this change, for comparison?

cmake -DZIG_PREFER_CLANG_CPP_DYLIB=true ..
Output:

-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring zig version 0.10.0-dev.3013+e867127a6
-- Found llvm: /usr/lib/llvm/14/lib64/libLLVM-14.so (Required is at least version "14")
-- Found clang: /usr/lib/llvm/14/lib64/libclang-cpp.so (Required is at least version "14")
-- Found lld: /usr/lib64/liblldMinGW.so;/usr/lib64/liblldELF.so;/usr/lib64/liblldCOFF.so;/usr/lib64/liblldWasm.so;/usr/lib64/liblldMachO.so;/usr/lib64/liblldCommon.so (Required is at least version "14")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/bratishkaerik/zig/build

@kamidev
Copy link

kamidev commented Jul 16, 2022

I tried this on M1 Mac and successfully built stage1 and stage2 without the zlib-workaround. Behaviour tests worked as well.

However, building stage3 currently crashes. This started to happen on master too, today. So it may be unrelated.

@BratishkaErik
Copy link
Contributor

I tried this on M1 Mac and successfully built stage1 and stage2 without the zlib-workaround. Behaviour tests worked as well.

However, building stage3 currently crashes. This started to happen on master too, today. So it may be unrelated.

Do you mean #12069? IDK how but I can build stage1 without this workaround (both static and shared zlib installed) (IIUC llvm-config can't choose between static and shared form on affected systems?)

@kamidev
Copy link

kamidev commented Jul 16, 2022

I tried this on M1 Mac and successfully built stage1 and stage2 without the zlib-workaround. Behaviour tests worked as well.
However, building stage3 currently crashes. This started to happen on master too, today. So it may be unrelated.

Do you mean #12069? IDK how but I can build stage1 without this workaround (both static and shared zlib installed) (IIUC llvm-config can't choose between static and shared form on affected systems?)

Yes, #12069. Interesting! I had this problem on ARM Mac and Intel Mac, other people reported it on Arch Linux and NixOS. What are you running?

@BratishkaErik
Copy link
Contributor

BratishkaErik commented Jul 16, 2022

I tried this on M1 Mac and successfully built stage1 and stage2 without the zlib-workaround. Behaviour tests worked as well.
However, building stage3 currently crashes. This started to happen on master too, today. So it may be unrelated.

Do you mean #12069? IDK how but I can build stage1 without this workaround (both static and shared zlib installed) (IIUC llvm-config can't choose between static and shared form on affected systems?)

Yes, #12069. Interesting! I had this problem on ARM Mac and Intel Mac, other people reported it on Arch Linux and NixOS. What are you running?

Gentoo Linux, tried https://packages.gentoo.org/packages/sys-libs/zlib (shared library) with static-libs flag disabled and enabled (i.e without and with static library), in second case (both shared and static) — with and without workaround:
cmake -DZIG_PREFER_CLANG_CPP_DYLIB -DZIG_STATIC_ZLIB=On ..
cmake -DZIG_PREFER_CLANG_CPP_DYLIB ..

@topolarity
Copy link
Contributor Author

I tried this on M1 Mac and successfully built stage1 and stage2 without the zlib-workaround. Behaviour tests worked as well.

However, building stage3 currently crashes. This started to happen on master too, today. So it may be unrelated.

The stage 3 problems may be related to #12133. I have to rework that PR a bit, but it'd be helpful to know if it makes any difference for you as-is

@BratishkaErik
Copy link
Contributor

BratishkaErik commented Jul 16, 2022

cmake ..: works and builds fine
cmake -DZIG_SHARED_LLVM=On ..: works and builds fine
cmake -DZIG_STATIC_LLVM=On ..:

-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring zig version 0.10.0-dev.3024+47c58cba5
CMake Error at cmake/Findllvm.cmake:25 (message):
  LLVM 14.x found at /usr/lib/llvm/14/bin/llvm-config does not support
  linking as shared library
Call Stack (most recent call first):
  CMakeLists.txt:111 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/bratishkaerik/zig/build/CMakeFiles/CMakeOutput.log".

Notice:

LLVM 14.x found at /usr/lib/llvm/14/bin/llvm-config does not support
linking as shared library

But it surely does. It's a bit confusing. Besides this, thank you so much!

@topolarity
Copy link
Contributor Author

Thanks for the helpful reports!

Can you share the output of /usr/lib/llvm/14/bin/llvm-config --link-shared?

@BratishkaErik
Copy link
Contributor

BratishkaErik commented Jul 16, 2022

Thanks for the helpful reports!

Can you share the output of /usr/lib/llvm/14/bin/llvm-config --link-shared?

Empty line. Maybe /usr/lib/llvm/14/bin/llvm-config --libs --link-shared?

-lLLVM-14

@topolarity
Copy link
Contributor Author

topolarity commented Jul 16, 2022

LLVM 14.x found at /usr/lib/llvm/14/bin/llvm-config does not support
linking as shared library

But it surely does. It's a bit confusing. Besides this, thank you so much!

The commit just pushed should report the error received from llvm-config. Would you mind trying it once more?

@BratishkaErik
Copy link
Contributor

LLVM 14.x found at /usr/lib/llvm/14/bin/llvm-config does not support
linking as shared library

But it surely does. It's a bit confusing. Besides this, thank you so much!

The commit just pushed should report the error received from llvm-config. Would you mind trying it once more?

cmake ..: works and builds fine
cmake -DZIG_SHARED_LLVM=On ..: works and builds fine
cmake -DZIG_STATIC_LLVM=On ..:

-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring zig version 0.10.0-dev.3024+47c58cba5
CMake Error at cmake/Findllvm.cmake:25 (message):
  LLVM 14.x found at /usr/lib/llvm/14/bin/llvm-config does not support
  linking as shared library ("llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMBinaryFormat.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMBitstreamReader.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRemarks.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMCore.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBitReader.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMDebugInfoCodeView.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMC.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMCParser.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMTextAPI.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMObject.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDebugInfoDWARF.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMProfileData.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAnalysis.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBitWriter.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMTransformUtils.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMAggressiveInstCombine.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMInstCombine.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMScalarOpts.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMTarget.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMFuzzMutate.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMFileCheck.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMInterfaceStub.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAsmParser.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMIRReader.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMCodeGen.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSelectionDAG.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDebugInfoMSF.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAsmPrinter.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMIRParser.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMGlobalISel.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDWARFLinker.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMExtensions.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMFrontendOpenACC.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMFrontendOpenMP.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMInstrumentation.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLinker.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMVectorize.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMipo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMObjCARCOpts.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMCoroutines.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMCFGuard.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMPasses.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLTO.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMCDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMCA.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMObjectYAML.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMOption.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDebugInfoGSYM.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDebugInfoPDB.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSymbolize.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDWP.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMOrcShared.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMOrcTargetProcess.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRuntimeDyld.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMExecutionEngine.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMInterpreter.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMJITLink.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMCJIT.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMOrcJIT.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMXCoreInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMXCoreDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMXCoreCodeGen.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMXCoreDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAVRInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAVRDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAVRCodeGen.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAVRAsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMAVRDisassembler.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMWebAssemblyInfo.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMWebAssemblyUtils.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMWebAssemblyDesc.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMWebAssemblyCodeGen.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMWebAssemblyAsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMWebAssemblyDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSystemZInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSystemZDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSystemZCodeGen.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMSystemZAsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMSystemZDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRISCVInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRISCVDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRISCVCodeGen.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMRISCVAsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMRISCVDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSparcInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSparcDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSparcCodeGen.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMSparcAsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMSparcDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAArch64Info.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAArch64Utils.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAArch64Desc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAArch64CodeGen.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMAArch64AsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMAArch64Disassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMSP430Info.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMSP430Desc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMSP430CodeGen.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMMSP430AsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMMSP430Disassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMHexagonInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMHexagonDesc.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMHexagonAsmParser.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMHexagonCodeGen.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMHexagonDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLanaiInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLanaiDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLanaiAsmParser.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLanaiCodeGen.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMLanaiDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMVEInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMVEDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMVECodeGen.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMVEAsmParser.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMVEDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMipsInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMipsDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMipsCodeGen.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMMipsAsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMMipsDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMNVPTXInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMNVPTXDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMNVPTXCodeGen.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMPowerPCInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMPowerPCDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMPowerPCCodeGen.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMPowerPCAsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMPowerPCDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBPFInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBPFDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBPFCodeGen.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMBPFAsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMBPFDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMX86Info.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMX86Desc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMX86CodeGen.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMX86AsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMX86Disassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMX86TargetMCA.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMARMInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMARMUtils.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMARMDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMARMCodeGen.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMARMAsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMARMDisassembler.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAMDGPUInfo.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAMDGPUUtils.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAMDGPUDesc.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMAMDGPUCodeGen.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMAMDGPUAsmParser.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMAMDGPUDisassembler.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMAMDGPUTargetMCA.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLineEditor.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMCoverage.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMDlltoolDriver.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMLibDriver.a

  llvm-config: error: missing: /usr/lib/llvm/14/lib64/libLLVMXRay.a

  llvm-config: error: missing:
  /usr/lib/llvm/14/lib64/libLLVMWindowsManifest.a")
Call Stack (most recent call first):
  CMakeLists.txt:111 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/bratishkaerik/zig/build/CMakeFiles/CMakeOutput.log".

@topolarity
Copy link
Contributor Author

That error message should have said "LLVM 14.x found at /usr/lib/llvm/14/bin/llvm-config does not support linking as a static library". Fixed now

Regarding the failure itself: Are the files reported missing present elsewhere in /usr/lib/llvm/14/, and llvm-config is just failing to find them? Were you able to build Zig with statically-linked LLVM before?

If not, then CMake might just be correctly informing you that there is no static version of LLVM that it can find to link against.

@BratishkaErik
Copy link
Contributor

That error message should have said "LLVM 14.x found at /usr/lib/llvm/14/bin/llvm-config does not support linking as a static library". Fixed now

Oh! Now I can sleep in peace :)

Regarding the failure itself: Are the files reported missing present elsewhere in /usr/lib/llvm/14/, and llvm-config is just failing to find them? Were you able to build Zig with statically-linked LLVM before?

Only shared. #12136 (comment)

@topolarity
Copy link
Contributor Author

Pushed a change to build libzigcpp as a shared library, based on a discussion with @kubkon

This is the first time we'll be building and installing a shared library as part of the Zig build process, so hopefully I got all of the details right (especially rpath). If anyone is able to give things another quick test, I'd greatly appreciate it

@BratishkaErik
Copy link
Contributor

BratishkaErik commented Jul 17, 2022

Pushed a change to build libzigcpp as a shared library, based on a discussion with @kubkon

This is the first time we'll be building and installing a shared library as part of the Zig build process, so hopefully I got all of the details right (especially rpath). If anyone is able to give things another quick test, I'd greatly appreciate it

cmake -DZIG_SHARED_LLVM=On .. && make -j8 -l7: builds and workds fine
./zig build -p stage2 -Dstatic-llvm=false -Denable-llvm=true: fails, see patch below

warning: FileNotFound: /home/bratishkaerik/zig/build/zigcpp/libzigcpp.a
thread 25859 panic: attempt to unwrap error: FileNotFound
/home/bratishkaerik/zig/lib/std/process.zig:373:46: 0x5654b62bed41 in std.process.getEnvVarOwned (zig1)
        const result = os.getenv(key) orelse return error.EnvironmentVariableNotFound;
                                             ^
/home/bratishkaerik/zig/lib/std/process.zig:373:46: 0x5654b62bed41 in std.process.getEnvVarOwned (zig1)
        const result = os.getenv(key) orelse return error.EnvironmentVariableNotFound;
                                             ^
/home/bratishkaerik/zig/lib/std/process.zig:373:46: 0x5654b62bed41 in std.process.getEnvVarOwned (zig1)
        const result = os.getenv(key) orelse return error.EnvironmentVariableNotFound;
                                             ^
/home/bratishkaerik/zig/lib/std/os.zig:3068:19: 0x5654b62d5019 in std.os.readlinkZ (zig1)
        .INVAL => return error.NotLink,
                  ^
/home/bratishkaerik/zig/lib/std/zig/system/NativeTargetInfo.zig:419:26: 0x5654b655d5d8 in std.zig.system.NativeTargetInfo.glibcVerFromSO (zig1)
        error.NotLink => return error.GnuLibCVersionUnavailable,
                         ^
/home/bratishkaerik/zig/lib/std/os.zig:2749:19: 0x5654b62463c0 in std.os.mkdiratZ (zig1)
        .EXIST => return error.PathAlreadyExists,
                  ^
/home/bratishkaerik/zig/lib/std/os.zig:2709:9: 0x5654b6246276 in std.os.mkdirat (zig1)
        return mkdiratZ(dir_fd, &sub_dir_path_c, mode);
        ^
/home/bratishkaerik/zig/lib/std/fs.zig:1327:9: 0x5654b6246186 in std.fs.Dir.makeDir (zig1)
        try os.mkdirat(self.fd, sub_path, default_new_dir_mode);
        ^
/home/bratishkaerik/zig/lib/std/os.zig:2749:19: 0x5654b62463c0 in std.os.mkdiratZ (zig1)
        .EXIST => return error.PathAlreadyExists,
                  ^
/home/bratishkaerik/zig/lib/std/os.zig:2709:9: 0x5654b6246276 in std.os.mkdirat (zig1)
        return mkdiratZ(dir_fd, &sub_dir_path_c, mode);
        ^
/home/bratishkaerik/zig/lib/std/fs.zig:1327:9: 0x5654b6246186 in std.fs.Dir.makeDir (zig1)
        try os.mkdirat(self.fd, sub_path, default_new_dir_mode);
        ^
/home/bratishkaerik/zig/lib/std/os.zig:2749:19: 0x5654b62463c0 in std.os.mkdiratZ (zig1)
        .EXIST => return error.PathAlreadyExists,
                  ^
/home/bratishkaerik/zig/lib/std/os.zig:2709:9: 0x5654b6246276 in std.os.mkdirat (zig1)
        return mkdiratZ(dir_fd, &sub_dir_path_c, mode);
        ^
/home/bratishkaerik/zig/lib/std/fs.zig:1327:9: 0x5654b6246186 in std.fs.Dir.makeDir (zig1)
        try os.mkdirat(self.fd, sub_path, default_new_dir_mode);
        ^
/home/bratishkaerik/zig/lib/std/os.zig:2749:19: 0x5654b62463c0 in std.os.mkdiratZ (zig1)
        .EXIST => return error.PathAlreadyExists,
                  ^
/home/bratishkaerik/zig/lib/std/os.zig:2709:9: 0x5654b6246276 in std.os.mkdirat (zig1)
        return mkdiratZ(dir_fd, &sub_dir_path_c, mode);
        ^
/home/bratishkaerik/zig/lib/std/fs.zig:1327:9: 0x5654b6246186 in std.fs.Dir.makeDir (zig1)
        try os.mkdirat(self.fd, sub_path, default_new_dir_mode);
        ^
/home/bratishkaerik/zig/lib/std/os.zig:2749:19: 0x5654b62463c0 in std.os.mkdiratZ (zig1)
        .EXIST => return error.PathAlreadyExists,
                  ^
/home/bratishkaerik/zig/lib/std/os.zig:2709:9: 0x5654b6246276 in std.os.mkdirat (zig1)
        return mkdiratZ(dir_fd, &sub_dir_path_c, mode);
        ^
/home/bratishkaerik/zig/lib/std/fs.zig:1327:9: 0x5654b6246186 in std.fs.Dir.makeDir (zig1)
        try os.mkdirat(self.fd, sub_path, default_new_dir_mode);
        ^
/home/bratishkaerik/zig/lib/std/os.zig:1680:23: 0x5654b6535810 in std.os.openatZ (zig1)
            .NOENT => return error.FileNotFound,
                      ^
/home/bratishkaerik/zig/lib/std/fs.zig:1076:13: 0x5654b623f8b6 in std.fs.Dir.openFileZ (zig1)
            try os.openatZ(self.fd, sub_path, os_flags, 0);
            ^
/home/bratishkaerik/zig/lib/std/fs.zig:1003:9: 0x5654b623bd0e in std.fs.Dir.openFile (zig1)
        return self.openFileZ(&path_c, flags);
        ^
/home/bratishkaerik/zig/src/Cache.zig:539:22: 0x5654b623d6da in Cache.Manifest.populateFileHash (zig1)
        const file = try fs.cwd().openFile(ch_file.path.?, .{});
                     ^
/home/bratishkaerik/zig/src/Cache.zig:476:21: 0x5654b622940d in Cache.Manifest.hit (zig1)
                    return err;
                    ^
/home/bratishkaerik/zig/src/Compilation.zig:2130:13: 0x5654b6321549 in Compilation.update (zig1)
            return err;
            ^
/home/bratishkaerik/zig/src/main.zig:3294:5: 0x5654b62b13ed in main.updateModule (zig1)
    try comp.update();
    ^
/home/bratishkaerik/zig/src/main.zig:2985:21: 0x5654b620c84b in main.buildOutputType (zig1)
        else => |e| return e,
                    ^
/home/bratishkaerik/zig/src/main.zig:219:9: 0x5654b61b8e23 in main.mainArgs (zig1)
        return buildOutputType(gpa, arena, args, .{ .build = .Exe });
        ^
???:?:?: 0x5654b61ba7ab in ??? (/home/bratishkaerik/zig/src/stage1/stage1.cpp)
/home/bratishkaerik/zig/src/stage1.zig:48:43: 0x5654b61b8938 in main (zig1)
        stage2.mainArgs(gpa, arena, args) catch unreachable;
                                          ^
error: zig...
error: The following command terminated unexpectedly:
/home/bratishkaerik/zig/build/zig build-exe /home/bratishkaerik/zig/src/main.zig -lc /home/bratishkaerik/zig/build/zigcpp/libzigcpp.a /usr/lib/llvm/14/lib64/libclang-cpp.so.14 /usr/lib64/liblldMinGW.so /usr/lib64/liblldELF.so /usr/lib64/liblldCOFF.so /usr/lib64/liblldWasm.so /usr/lib64/liblldMachO.so /usr/lib64/liblldCommon.so /usr/lib/llvm/14/lib64/libLLVM-14.so /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.a -lunwind --cache-dir /home/bratishkaerik/zig/zig-cache --global-cache-dir /home/bratishkaerik/.cache/zig --name zig --pkg-begin build_options /home/bratishkaerik/zig/zig-cache/options/0ghZFRJv5yPSIQOmN7jArYzqIPaxipxNPztZSGsd95d9FUsuBk1sFGWFiIl23teH --pkg-end -I /usr/include -fno-build-id --enable-cache
error: the following build command failed with exit code 6:
/home/bratishkaerik/zig/zig-cache/o/9e99760733e35b737bbb023007fc4485/build /home/bratishkaerik/zig/build/zig /home/bratishkaerik/zig /home/bratishkaerik/zig/zig-cache /home/bratishkaerik/.cache/zig -p stage2 -Dstatic-llvm=false -Denable-llvm=true

@BratishkaErik
Copy link
Contributor

BratishkaErik commented Jul 17, 2022

diff --git a/build.zig b/build.zig
index 31347f8e7..27c3931ac 100644
--- a/build.zig
+++ b/build.zig
@@ -59,6 +59,7 @@ pub fn build(b: *Builder) !void {
     const skip_run_translated_c = b.option(bool, "skip-run-translated-c", "Main test suite skips run-translated-c tests") orelse false;
     const skip_stage2_tests = b.option(bool, "skip-stage2-tests", "Main test suite skips self-hosted compiler tests") orelse false;
     const skip_install_lib_files = b.option(bool, "skip-install-lib-files", "Do not copy lib/ files to installation prefix") orelse false;
+    const skip_rpath = b.option(bool, "skip-rpath", "Do not set DT_RUNPATH (useful for package maintainers)") orelse false;

     const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false;

@@ -331,8 +332,23 @@ pub fn build(b: *Builder) !void {
                 b.addSearchPrefix(cfg.cmake_prefix_path);
             }

+            const zig_cpp_lib_name = b.fmt("{s}zigcpp{s}", .{ exe.target.libPrefix(), exe.target.dynamicLibSuffix() });
+
+            const zig_cpp_lib_location = fs.path.join(b.allocator, &[_][]const u8{ cfg.cmake_binary_dir, "zigcpp", zig_cpp_lib_name }) catch unreachable;
+
+            exe.addObjectFile(zig_cpp_lib_location);
+            test_cases.addObjectFile(zig_cpp_lib_location);
+
+            if (!skip_rpath) {
+                switch (exe.target.getOsTag()) {
+                    .macos => exe.addRPath("@loader_path/../lib"),
+                    else => exe.addRPath("$ORIGIN/../lib"),
+                }
+            }
+
             try addCmakeCfgOptionsToExe(b, cfg, exe, use_zig_libcxx);
             try addCmakeCfgOptionsToExe(b, cfg, test_cases, use_zig_libcxx);
+            b.installLibFile(zig_cpp_lib_location, zig_cpp_lib_name);
         } else {
             // Here we are -Denable-llvm but no cmake integration.
             try addStaticLlvmOptionsToExe(exe);
@@ -534,11 +550,6 @@ fn addCmakeCfgOptionsToExe(
     exe: *std.build.LibExeObjStep,
     use_zig_libcxx: bool,
 ) !void {
-    exe.addObjectFile(fs.path.join(b.allocator, &[_][]const u8{
-        cfg.cmake_binary_dir,
-        "zigcpp",
-        b.fmt("{s}{s}{s}", .{ exe.target.libPrefix(), "zigcpp", exe.target.staticLibSuffix() }),
-    }) catch unreachable);
     assert(cfg.lld_include_dir.len != 0);
     exe.addIncludePath(cfg.lld_include_dir);
     addCMakeLibraryList(exe, cfg.clang_libraries);

You can pass -DCMAKE_SKIP_INSTALL_RPATH=On for stage1 and -Dskip-rpath=true for stage2/stage3 if you don't need rpath (useful for package maintainers).

@BratishkaErik
Copy link
Contributor

BratishkaErik commented Jul 17, 2022

Everything from https://github.com/ziglang/zig/wiki/Building-Zig-From-Source builds and works fine:

stage1

Option B

Remember! For Option B, these libraries must be produced by zig cc / zig c++ - not by your system C/C++ compiler. If you are annoyed by this, welcome to the club.

Well, I can't reproduce this, it builds and works fine Segmentation fault... (Only in release mode, unrelated to this pull request, also happens on master)

$ stage1/bin/zig build -p stage1-from-prebuilt -Dstage1 -Domit-stage2 --zig-lib-dir /home/bratishkaerik/zig/lib/ --search-prefix /usr/lib/llvm/14/ -Dstatic-llvm=false -Dskip-rpath=true -Drelease=true && strip stage1-from-prebuilt/bin/zig

LLD Link... warning(link): unexpected LLD stderr:
ld.lld: warning: linking module flags 'Dwarf Version': IDs have conflicting values ('i32 5' from /home/bratishkaerik/zig/zig-cache/o/310992a639b0dffe65c963fb2461b00e/analyze.o with 'i32 4' from ld-temp.o)

$ stage1-from-prebuilt/bin/zig version
0.10.0-dev.3024+47c58cba5

$ stage1-from-prebuilt/bin/zig build --help
[1]    11387 segmentation fault  stage1-from-prebuilt/bin/zig build --help

Comparing sizes (all in release modes, without rpath and stripped)

bloaty /usr/lib64/libzigcpp.so

    FILE SIZE        VM SIZE
 --------------  --------------
  43.0%   130Ki  42.9%   130Ki    .text
  18.0%  54.6Ki  18.0%  54.6Ki    .dynstr
   9.4%  28.4Ki   9.3%  28.4Ki    .dynsym
   7.2%  21.7Ki   7.1%  21.7Ki    .eh_frame
   5.7%  17.4Ki   5.7%  17.4Ki    .rela.plt
   3.8%  11.6Ki   3.8%  11.6Ki    .plt
   3.1%  9.46Ki   3.1%  9.46Ki    .hash
   2.2%  6.76Ki   2.2%  6.76Ki    .rodata
   1.9%  5.83Ki   1.9%  5.83Ki    .got.plt
   1.5%  4.62Ki   1.5%  4.62Ki    .eh_frame_hdr
   1.1%  3.26Ki   1.1%  3.26Ki    .gnu.hash
   0.8%  2.53Ki   0.8%  2.53Ki    .rela.dyn
   0.0%       0   0.8%  2.41Ki    .bss
   0.8%  2.36Ki   0.8%  2.36Ki    .gnu.version
   0.5%  1.62Ki   0.0%       0    [ELF Section Headers]
   0.2%     608   0.2%     608    .data.rel.ro
   0.2%     608   0.2%     608    .dynamic
   0.2%     504   0.2%     504    [ELF Program Headers]
   0.1%     429   0.1%     388    [8 Others]
   0.1%     360   0.1%     360    .got
   0.1%     215   0.0%       0    .shstrtab
 100.0%   303Ki 100.0%   303Ki    TOTAL

stage1

Option A

bloaty pr/build/stage1/bin/zig -- master/build/stage1/bin/zig
    FILE SIZE        VM SIZE
 --------------  --------------
  +2.5%     +16  +2.5%     +16    .dynamic
 -66.7%     -16 -12.5%      -5    [2 Others]
  [ = ]       0  [DEL]     -32    [LOAD #5 [RW]]
 -37.5%     -48 -37.5%     -48    .data
 -18.3%     -80 -18.3%     -80    .gnu.version_r
 -78.1%    -200 -78.1%    -200    .got
  -0.1%    -608  -0.1%    -619    .data.rel.ro
 -29.6% -1.46Ki -29.6% -1.46Ki    .gnu.version
  -0.4% -1.99Ki  -0.4% -1.99Ki    .rela.dyn
 -25.5% -2.05Ki -25.4% -2.04Ki    .got.plt
  [ = ]       0 -35.5% -2.33Ki    .bss
 -25.5% -4.09Ki -25.5% -4.09Ki    .plt
 -37.8% -4.23Ki -37.8% -4.23Ki    .gnu.hash
 -25.8% -4.59Ki -25.8% -4.59Ki    .eh_frame_hdr
 -29.6% -5.84Ki -29.6% -5.84Ki    .hash
 -25.6% -6.14Ki -25.6% -6.14Ki    .rela.plt
  -0.9% -6.79Ki  -0.9% -6.79Ki    .rodata
 -29.6% -17.5Ki -29.6% -17.5Ki    .dynsym
 -16.7% -21.6Ki -16.7% -21.6Ki    .eh_frame
 -47.7% -41.0Ki -47.7% -41.0Ki    .dynstr
  -1.8%  -130Ki  -1.8%  -130Ki    .text
  -2.7%  -248Ki  -2.7%  -250Ki    TOTAL

Option B

bloaty pr/build/stage1-from-prebuilt/bin/zig -- master/build/stage1-from-prebuilt/bin/zig

    FILE SIZE        VM SIZE
 --------------  --------------
  +8.6%     +48  +8.6%     +48    .dynamic
  -2.1%     -10  -4.8%     -24    [4 Others]
  [DEL]     -30  [ = ]       0    [LOAD #4 [RW]]
 -75.0%     -24  [DEL]     -32    .init_array
 -85.2%    -184 -85.2%    -184    .got
 -33.3%    -248 -33.3%    -248    .gnu.hash
 -27.3%    -596 -27.3%    -596    .gnu.version
 -86.2%    -600 -86.2%    -600    .rela.dyn
 -80.6%    -864 -80.6%    -864    .gcc_except_table
 -21.7% -1.02Ki -22.2% -1.03Ki    .data.rel.ro
 -25.5% -1.95Ki -25.5% -1.95Ki    .got.plt
 -27.3% -2.33Ki -27.3% -2.33Ki    .hash
  [ = ]       0 -50.5% -2.39Ki    .bss
 -25.6% -3.91Ki -25.6% -3.91Ki    .plt
 -13.4% -5.05Ki -13.4% -5.05Ki    .eh_frame_hdr
 -25.6% -5.88Ki -25.6% -5.88Ki    .rela.plt
 -27.3% -6.98Ki -27.3% -6.98Ki    .dynsym
  -0.6% -7.09Ki  -0.6% -7.09Ki    .rodata
  -8.5% -23.7Ki  -8.5% -23.7Ki    .eh_frame
 -58.6% -25.3Ki -58.6% -25.3Ki    .dynstr
  -3.0%  -136Ki  -3.0%  -136Ki    .text
  -3.6%  -222Ki  -3.6%  -224Ki    TOTAL

stage2

bloaty pr/build/stage2/bin/zig -- master/build/stage2/bin/zig

    FILE SIZE        VM SIZE
 --------------  --------------
 -11.1%     -36 -11.1%     -36    .gnu.version_r
 -60.0%     -72 -60.0%     -72    .data
 -29.8%    -112 -70.6%     -96    [6 Others]
 -23.0%    -128 -23.0%    -128    .gnu.hash
  -6.9%    -128  [ = ]       0    [ELF Section Headers]
 -88.9%    -192 -88.9%    -192    .got
 -89.7%    -624 -89.7%    -624    .rela.dyn
 -31.9%    -676 -31.9%    -676    .gnu.version
  [DEL] -1.03Ki  [DEL] -1.03Ki    .gcc_except_table
 -30.3% -2.24Ki -30.3% -2.24Ki    .got.plt
 -31.9% -2.64Ki -31.9% -2.64Ki    .hash
 -30.4% -4.50Ki -30.3% -4.48Ki    .plt
  [ = ]       0 -96.5% -4.55Ki    .bss
  -1.1% -4.80Ki  -1.1% -4.80Ki    .data.rel.ro
 -13.6% -6.55Ki -13.6% -6.55Ki    .eh_frame_hdr
 -30.4% -6.73Ki -30.4% -6.73Ki    .rela.plt
 -31.9% -7.92Ki -31.9% -7.92Ki    .dynsym
  -2.3% -15.8Ki  -2.3% -15.8Ki    .rodata
 -60.6% -26.0Ki -60.6% -26.0Ki    .dynstr
  -8.6% -33.1Ki  -8.6% -33.2Ki    .eh_frame
  -3.2%  -199Ki  -3.2%  -199Ki    .text
  -4.0%  -313Ki  -4.1%  -317Ki    TOTAL

stage3

bloaty pr/build/stage3/bin/zig -- master/build/stage3/bin/zig

    FILE SIZE        VM SIZE
 --------------  --------------
 -11.1%     -36 -11.1%     -36    .gnu.version_r
 -24.3%     -72 -26.3%     -80    .data
 -29.2%    -112 -75.0%     -72    [5 Others]
 -23.0%    -128 -23.0%    -128    .gnu.hash
  -6.7%    -128  [ = ]       0    [ELF Section Headers]
 -88.9%    -192 -88.9%    -192    .got
 -89.7%    -624 -89.7%    -624    .rela.dyn
 -31.9%    -676 -31.9%    -676    .gnu.version
  [DEL] -1.03Ki  [DEL] -1.03Ki    .gcc_except_table
 -30.3% -2.24Ki -30.3% -2.24Ki    .got.plt
 -31.9% -2.64Ki -31.9% -2.64Ki    .hash
 -30.3% -4.48Ki -30.3% -4.48Ki    .plt
  [ = ]       0 -96.5% -4.55Ki    .bss
  -1.3% -4.81Ki  -1.3% -4.80Ki    .data.rel.ro
 -13.6% -6.55Ki -13.6% -6.55Ki    .eh_frame_hdr
 -30.4% -6.73Ki -30.4% -6.73Ki    .rela.plt
 -31.9% -7.92Ki -31.9% -7.92Ki    .dynsym
  -1.9% -15.7Ki  -1.9% -15.7Ki    .rodata
 -60.6% -26.0Ki -60.6% -26.0Ki    .dynstr
  -7.8% -33.2Ki  -7.8% -33.2Ki    .eh_frame
  -2.6%  -199Ki  -2.6%  -199Ki    .text
  -3.3%  -313Ki  -3.4%  -317Ki    TOTAL

Comparing sizes (all in debug modes, without rpath and not stripped)

bloaty /usr/lib64/libzigcpp.so

    FILE SIZE        VM SIZE
 --------------  --------------
  40.6%  2.51Mi   0.0%       0    .debug_str
  18.1%  1.12Mi   0.0%       0    .debug_info
  10.5%   666Ki   0.0%       0    .strtab
   7.2%   457Ki  41.1%   457Ki    .text
   5.3%   335Ki   0.0%       0    .debug_line
   3.4%   214Ki  19.3%   214Ki    .eh_frame
   3.1%   197Ki   0.0%       0    .debug_str_offsets
   2.9%   181Ki   0.0%       0    .symtab
   2.4%   153Ki  13.8%   153Ki    .dynstr
   1.0%  65.8Ki   0.0%       0    .debug_addr
   0.9%  58.5Ki   5.3%  58.5Ki    .rodata
   0.8%  53.6Ki   4.8%  53.6Ki    .eh_frame_hdr
   0.8%  51.8Ki   4.7%  51.8Ki    .dynsym
   0.6%  40.4Ki   3.6%  40.4Ki    .rela.plt
   0.5%  31.4Ki   0.0%       0    .debug_rnglists
   0.4%  27.0Ki   2.4%  27.0Ki    .plt
   0.3%  22.0Ki   0.0%       0    .debug_abbrev
   0.3%  21.7Ki   1.2%  13.8Ki    [19 Others]
   0.3%  17.3Ki   1.6%  17.3Ki    .hash
   0.2%  13.5Ki   1.2%  13.5Ki    .got.plt
   0.2%  10.9Ki   1.0%  10.9Ki    .gnu.hash
 100.0%  6.20Mi 100.0%  1.09Mi    TOTAL

stage1

Option A

bloaty pr/build/stage1/bin/zig -- master/build/stage1/bin/zig

    FILE SIZE        VM SIZE
 --------------  --------------
 -28.9% -4.88Ki -28.9% -4.88Ki    .plt
 -26.9% -5.88Ki  [ = ]       0    .debug_line_str
 -28.9% -7.31Ki -28.9% -7.31Ki    .rela.plt
 -55.8% -8.82Ki -55.8% -8.82Ki    .gnu.hash
  -0.2% -11.6Ki  -0.6% -12.8Ki    [14 Others]
 -49.3% -13.6Ki -49.3% -13.6Ki    .hash
 -32.3% -21.8Ki  [ = ]       0    .debug_abbrev
 -67.3% -31.4Ki  [ = ]       0    .debug_rnglists
 -49.3% -40.9Ki -49.3% -40.9Ki    .dynsym
 -57.9% -53.6Ki -57.9% -53.6Ki    .eh_frame_hdr
  -4.5% -58.7Ki  -4.5% -58.7Ki    .rodata
 -45.1% -65.8Ki  [ = ]       0    .debug_addr
 -75.6%  -139Ki -75.6%  -139Ki    .dynstr
  -5.9%  -170Ki  [ = ]       0    .symtab
 -55.2%  -196Ki  [ = ]       0    .debug_str_offsets
 -57.4%  -214Ki -57.4%  -214Ki    .eh_frame
  -4.5%  -336Ki  [ = ]       0    .debug_line
  -2.2%  -457Ki  -2.2%  -457Ki    .text
 -16.0%  -653Ki  [ = ]       0    .strtab
 -17.7% -1.12Mi  [ = ]       0    .debug_info
 -54.0% -2.49Mi  [ = ]       0    .debug_str
 -10.4% -6.05Mi  -4.0% -1012Ki    TOTAL

Option B

bloaty pr/build/stage1-from-prebuilt/bin/zig -- master/build/stage1-from-prebuilt/bin/zig

    FILE SIZE        VM SIZE
 --------------  --------------
  +211% +5.53Ki  +211% +5.53Ki    .rela.dyn
 -29.4% -2.59Ki -29.4% -2.59Ki    .hash
 -26.7% -4.12Ki -26.7% -4.12Ki    .plt
 -26.7% -6.19Ki -26.7% -6.19Ki    .rela.plt
 -29.4% -7.76Ki -29.4% -7.76Ki    .dynsym
  -0.6% -7.57Ki  -0.9% -8.00Ki    [17 Others]
 -28.5% -17.7Ki  [ = ]       0    .debug_line_str
 -34.1% -21.9Ki  [ = ]       0    .debug_abbrev
 -60.6% -27.6Ki -60.6% -27.6Ki    .dynstr
 -65.4% -31.4Ki  [ = ]       0    .debug_rnglists
 -21.6% -54.2Ki -21.6% -54.2Ki    .eh_frame_hdr
 -45.1% -65.8Ki  [ = ]       0    .debug_addr
  -5.7% -71.5Ki  -5.7% -71.5Ki    .rodata
  -9.0%  -171Ki  [ = ]       0    .symtab
 -55.8%  -196Ki  [ = ]       0    .debug_str_offsets
 -15.3%  -217Ki -15.3%  -217Ki    .eh_frame
  -6.2%  -336Ki  [ = ]       0    .debug_line
  -2.9%  -464Ki  -2.9%  -464Ki    .text
 -22.2%  -655Ki  [ = ]       0    .strtab
 -27.0% -1.12Mi  [ = ]       0    .debug_info
 -57.7% -2.96Mi  [ = ]       0    .debug_str
 -15.1% -6.38Mi  -4.3%  -857Ki    TOTAL

stage2

bloaty pr/build/stage2/bin/zig -- master/build/stage2/bin/zig

    FILE SIZE        VM SIZE
 --------------  --------------
  +125% +6.80Ki  +125% +6.80Ki    .rela.dyn
 -31.2% -4.69Ki -31.2% -4.69Ki    .plt
  -0.5% -5.28Ki  -0.5% -5.28Ki    .data.rel.ro
 -31.2% -7.03Ki -31.2% -7.03Ki    .rela.plt
 -33.8% -8.67Ki -33.8% -8.67Ki    .dynsym
 -40.2% -11.0Ki -13.6% -12.2Ki    [16 Others]
  [DEL] -16.6Ki  [ = ]       0    .debug_line_str
 -92.6% -21.8Ki  [ = ]       0    .debug_abbrev
 -62.4% -28.2Ki -62.4% -28.2Ki    .dynstr
  [DEL] -31.4Ki  [ = ]       0    .debug_rnglists
 -15.0% -55.7Ki -15.0% -55.7Ki    .eh_frame_hdr
  [DEL] -65.8Ki  [ = ]       0    .debug_addr
  -5.6% -80.7Ki  -5.6% -80.7Ki    .rodata
  -6.4%  -178Ki  [ = ]       0    .symtab
  [DEL]  -196Ki  [ = ]       0    .debug_str_offsets
  -9.7%  -226Ki  -9.7%  -226Ki    .eh_frame
  -5.2%  -336Ki  [ = ]       0    .debug_line
  -2.7%  -528Ki  -2.7%  -528Ki    .text
 -17.7%  -665Ki  [ = ]       0    .strtab
 -20.9% -1.12Mi  [ = ]       0    .debug_info
 -65.5% -2.96Mi  [ = ]       0    .debug_str
 -12.3% -6.49Mi  -3.8%  -950Ki    TOTAL

stage3

bloaty pr/build/stage3/bin/zig -- master/build/stage3/bin/zig

    FILE SIZE        VM SIZE
 --------------  --------------
  [ = ]       0  -6.6% -4.52Ki    .bss
 -31.3% -4.70Ki -31.3% -4.70Ki    .plt
  -0.4% -5.30Ki  -0.4% -5.29Ki    .data.rel.ro
 -31.2% -7.05Ki -31.2% -7.05Ki    .rela.plt
 -33.9% -8.70Ki -33.9% -8.70Ki    .dynsym
  -0.3% -11.7Ki  -9.4% -8.60Ki    [18 Others]
  [DEL] -16.6Ki  [ = ]       0    .debug_line_str
 -95.0% -21.8Ki  [ = ]       0    .debug_abbrev
 -62.4% -28.2Ki -62.4% -28.2Ki    .dynstr
  [DEL] -31.4Ki  [ = ]       0    .debug_rnglists
 -16.4% -55.7Ki -16.4% -55.7Ki    .eh_frame_hdr
  [DEL] -65.8Ki  [ = ]       0    .debug_addr
  -3.6% -81.0Ki  -3.6% -81.0Ki    .rodata
  -5.8%  -178Ki  [ = ]       0    .symtab
  [DEL]  -196Ki  [ = ]       0    .debug_str_offsets
 -11.3%  -226Ki -11.3%  -226Ki    .eh_frame
  -7.8%  -336Ki  [ = ]       0    .debug_line
  -3.0%  -528Ki  -3.0%  -528Ki    .text
  -9.2%  -665Ki  [ = ]       0    .strtab
 -14.3% -1.12Mi  [ = ]       0    .debug_info
 -22.3% -2.96Mi  [ = ]       0    .debug_str
  -9.3% -6.50Mi  -4.1%  -958Ki    TOTAL

Oh, I also noticed that with this pull request we can build stage2/stage3 without patch (IIUC only when using new shared library) from #11137 (but bug still persist)

@topolarity
Copy link
Contributor Author

topolarity commented Jul 17, 2022

@BratishkaErik Thanks for all of the thorough testing, I really appreciate it.

Pushed support for stage2 (+3)

Most important difference w.r.t. your change is that I did not make RPATH configurable. libzigcpp.so exposes no public API whatsoever, so I don't think we should support moving this into system library directories. Besides, Zig already has hard-coded relative path(s) to look up its stdlib. Willing to hear arguments to the contrary, though.

@BratishkaErik
Copy link
Contributor

scanelf: rpath_security_checks(): Security problem NULL DT_RUNPATH in zigcpp/libzigcpp.so

I guess libzigcpp.so doesn't need RPATH?

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2ae8e3d18..6dcd76d6c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -907,6 +907,7 @@ endif()

 set_target_properties(zigcpp PROPERTIES
     COMPILE_FLAGS ${EXE_CFLAGS}
+    SKIP_BUILD_RPATH On
 )

 target_link_libraries(zigcpp LINK_PUBLIC

@topolarity
Copy link
Contributor Author

I guess libzigcpp.so doesn't need RPATH?

Good catch! Updated now, thank you.

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this. What a complete mess.

I think the root problem here is that dynamically linked LLVM+Clang does not mix with statically linked LLD.

I don't really see a way to link properly when the system wants us to link dynamically against LLVM and Clang but then only provides static LLD libraries. This just seems like we're hitting C++ undefined behavior.

Pushed a change to build libzigcpp as a shared library, based on a discussion with @kubkon

I'd like to avoid this; this code has no reason to be a dynamic library and making it such will only cause problems. What problem is this solving?

@topolarity
Copy link
Contributor Author

I think the root problem here is that dynamically linked LLVM+Clang does not mix with statically linked LLD.

I don't really see a way to link properly when the system wants us to link dynamically against LLVM and Clang but then only provides static LLD libraries. This just seems like we're hitting C++ undefined behavior.

I haven't quite managed to grok the common recommendation not to "mix" static/shared libraries, to be honest - At the end of the day, static libraries are just (collections of) object files, and saying not to mix them with shared libraries would suggest it's also wrong to mix object files with shared libraries - But that's a linker's entire job.

The concrete problem I've seen is not C++ UB - It's that this part of the C standard (http://port70.net/~nsz/c/c11/n1570.html#6.2.2p2) no longer holds up when statically linking the same objects (e.g. libstdc++.a) into multiple shared libraries, since the dynamic linker is not capable of noticing/fixing the duplication.

I'm also no expert in C++ or linkers. There's a high chance there's more complexity I'm missing here, so I'm all ears if any of the above is wrong.

What a complete mess.

Tell me about it 😰

@topolarity
Copy link
Contributor Author

I'd like to avoid this; this code has no reason to be a dynamic library and making it such will only cause problems. What problem is this solving?

Same as the LLD situation basically - There are concerns about mixing shared/static stepping on C++ UB, but I don't personally have a specific bug/standard I can point to that says why this would have to be shared

Happy to revert, if you'd prefer. My priority is that we don't statically link libstdc++ into multiple shared libraries and we don't mix library versions/ABIs

@andrewrk
Copy link
Member

The concrete problem I've seen is not C++ UB - It's that this part of the C standard (http://port70.net/~nsz/c/c11/n1570.html#6.2.2p2) no longer holds up when statically linking the same objects (e.g. libstdc++.a) into multiple shared libraries, since the dynamic linker is not capable of noticing/fixing the duplication.

This is indeed the concrete problem that I am thinking of (which you have addressed in #12085), and that is my mistake for confusing things by saying "C++ UB". I think that I have gotten this problem mixed up with the Static Initialization Order Fiasco.

Same as the LLD situation basically - There are concerns about mixing shared/static stepping on C++ UB, but I don't personally have a specific bug/standard I can point to that says why this would have to be shared. Happy to revert, if you'd prefer.

I am firmly convinced that making these files dynamically linked is the wrong way to go, and would indeed request a revert on that particular change.

My priority is that we don't statically link libstdc++ into multiple shared libraries and we don't mix library versions/ABIs

I share this priority with you.

@topolarity
Copy link
Contributor Author

Great, sounds like a plan. Thanks for the input - I needed a Core Team POV on this one.

This commit reworks the LLVM/Clang/LLD discovery process for CMake. The
biggest changes are that:
  1. We search for LLVM from most preferred directory to least, skipping
     any `llvm-config` that is the wrong version, or that doesn't
     support the requested link mode ("static" or "shared").
  2. `ZIG_PREFER_CLANG_CPP_DYLIB` has been renamed to `ZIG_SHARED_LLVM`,
     to better align with `ZIG_STATIC_LLVM`.
  3. We only search for LLVM in the same directory alongside LLVM.
  4. LLVM's link mode is forwarded to Clang, so that we can look for the
     appropriate shared/static libraries.
  5. We use `--link-static` when querying `--system-libs` from llvm-config,
     so that this will include libz and other dependencies for
     statically linking LLD
This change relaxes the restriction added in the prior commit that LLD
should be alongside LLVM.

This also leaves unresolved the issue of making sure the link mode
(static or shared) of LLD matches that of LLVM/Clang. That would be an
unfortunate restriction, since LLD seems to be provided only as a static
lib on some distros.
This is currently unused, but it will be needed soon so that we can
avoid linking static libc++ if LLVM/Clang is configured as a shared
library.
@voroskoi
Copy link
Sponsor Contributor

Sorry for coming late to the party, but unlike master this branch does not work with guix, does not find the clang include dir and libs. They have different hash than llvm include dirs, so I have no idea how to handle this. Does it work with nixos?

… CMAKE_FRAMEWORK_PATH

On some systems (esp. systems that use unique hashed file-paths for
library-versions like Nix), we can't expect LLVM and Clang to share
lib/bin directories.

The best we can do is find the matching clang libraries in the
CMAKE_LIBRARY_PATH provided by the environment
@topolarity
Copy link
Contributor Author

topolarity commented Jul 25, 2022

Sorry for coming late to the party, but unlike master this branch does not work with guix, does not find the clang include dir and libs. They have different hash than llvm include dirs, so I have no idea how to handle this. Does it work with nixos?

Thanks for the heads up - I've relaxed the way we look for Clang so that it respects CMAKE_LIBRARY_PATH (as well as CMAKE_PREFIX_PATH), even if this isn't where we found LLVM.

I think this is enough to get things operational on Nix-like OS's again (tested on a local install of NixOS)

@voroskoi
Copy link
Sponsor Contributor

This one works indeed, thanks. Only #12153 left, but as far as I understand Andrew that stays this way for a while.

@andrewrk andrewrk merged commit 7a09bce into ziglang:master Jul 26, 2022
@andrewrk
Copy link
Member

Thank you @topolarity, this looks like it was an ordeal in order to implement. I think this is the best we can do with what we've got. That while (1) in cmake is terrifying but seems like the right call 🤷

Really appreciate your diligence on this one.

@topolarity topolarity deleted the llvm-config-rework branch July 26, 2022 18:45
BratishkaErik added a commit to BratishkaErik/gentoo that referenced this pull request Jul 26, 2022
-DZIG_SHARED_LLVM

See ziglang/zig#12136

Signed-off-by: BratishkaErik <bratishkaerik@getgoogleoff.me>
BratishkaErik added a commit to BratishkaErik/gentoo that referenced this pull request Jul 26, 2022
…stage2, minor fix

stage1 installation (cmake_src_install) already does this

-DZIG_PREFER_CLANG_CPP_DYLIB has been renamed
to -DZIG_SHARED_LLVM, see ziglang/zig#12136
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
BratishkaErik added a commit to BratishkaErik/gentoo that referenced this pull request Jul 27, 2022
…stage2, minor fix

stage1 installation (cmake_src_install) already does this

-DZIG_PREFER_CLANG_CPP_DYLIB has been renamed
to -DZIG_SHARED_LLVM, see ziglang/zig#12136
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
BratishkaErik added a commit to BratishkaErik/gentoo that referenced this pull request Aug 9, 2022
…stage2, critical fix

stage1 installation (cmake_src_install) already does this

-DZIG_PREFER_CLANG_CPP_DYLIB has been renamed
to -DZIG_SHARED_LLVM, see ziglang/zig#12136

Closes: https://bugs.gentoo.org/864247
Co-authored-by: Rahim Usubov <the.guard@mail.ru>
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
BratishkaErik added a commit to BratishkaErik/gentoo that referenced this pull request Aug 16, 2022
…stage2, critical fix

stage1 installation (cmake_src_install) already does this

-DZIG_PREFER_CLANG_CPP_DYLIB has been renamed
to -DZIG_SHARED_LLVM, see ziglang/zig#12136

Closes: https://bugs.gentoo.org/864247
Co-authored-by: Rahim Usubov <the.guard@mail.ru>
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
BratishkaErik added a commit to BratishkaErik/gentoo that referenced this pull request Aug 16, 2022
cmake_src_install already installs lib/, so we should skip this in
stage2

-DZIG_PREFER_CLANG_CPP_DYLIB has been renamed
to -DZIG_SHARED_LLVM, see ziglang/zig#12136

Closes: https://bugs.gentoo.org/864247
Closes: gentoo#26779
Co-authored-by: Rahim Usubov <the.guard@mail.ru>
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
BratishkaErik added a commit to BratishkaErik/gentoo that referenced this pull request Aug 17, 2022
cmake_src_install already installs lib/, so we should skip this in
stage2

-DZIG_PREFER_CLANG_CPP_DYLIB has been renamed
to -DZIG_SHARED_LLVM, see ziglang/zig#12136

Closes: https://bugs.gentoo.org/864247
Closes: gentoo#26779
Co-authored-by: Rahim Usubov <the.guard@mail.ru>
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
gentoo-bot pushed a commit to gentoo/gentoo that referenced this pull request Aug 22, 2022
cmake_src_install already installs lib/, so we should skip this in
stage2

-DZIG_PREFER_CLANG_CPP_DYLIB has been renamed
to -DZIG_SHARED_LLVM, see ziglang/zig#12136

Closes: https://bugs.gentoo.org/864247
Closes: #26779
Co-authored-by: Rahim Usubov <the.guard@mail.ru>
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
Signed-off-by: Jakov Smolić <jsmolic@gentoo.org>
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.

None yet

5 participants