This is an archive of the discontinued LLVM Phabricator instance.

libclang: Add static build support for Windows
ClosedPublic

Authored by cristian.adam on Feb 24 2020, 11:12 AM.

Details

Summary

This patch is a squash for:

https://reviews.llvm.org/D74564
https://reviews.llvm.org/D74907

And a fix for the issue that got both changes reverted. The case on Windows when both STATIC and SHARED options were generated, which on on Windows is not possible.

Diff Detail

Event Timeline

cristian.adam created this revision.Feb 24 2020, 11:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 24 2020, 11:12 AM

I've tested this patch with:

-D LIBCLANG_BUILD_STATIC=OFF
-D LLVM_ENABLE_PIC=ON

All green https://github.com/cristianadam/llvm-project/actions/runs/44839699

Then:

-D LIBCLANG_BUILD_STATIC=ON
-D LLVM_ENABLE_PIC=OFF

All green https://github.com/cristianadam/llvm-project/actions/runs/44755935

And lastly:

-D LIBCLANG_BUILD_STATIC=OFF
-D LLVM_ENABLE_PIC=OFF

Which builds on Windows and macOS, and fails on Linux as expected
https://github.com/cristianadam/llvm-project/actions/runs/44423211

The expected Linux error is:

/usr/bin/ld: lib/libclangBasic.a(Stack.cpp.o): relocation R_X86_64_TPOFF32 against `_ZL13BottomOfStack' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: lib/libLLVMSupport.a(PrettyStackTrace.cpp.o): relocation R_X86_64_TPOFF32 against `_ZL20PrettyStackTraceHead' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: lib/libLLVMSupport.a(TimeProfiler.cpp.o): relocation R_X86_64_TPOFF32 against `_ZN12_GLOBAL__N_125TimeTraceProfilerInstanceE' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CIndex.cpp.o: relocation R_X86_64_PC32 against symbol `clang_getCursorLocation' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value

From my point of view it passes all configurations. What do you think?

This comment was removed by cristian.adam.
cristian.adam added a reviewer: Restricted Project.Apr 21 2020, 6:19 AM
rnk accepted this revision.Apr 23 2020, 11:09 AM

lgtm, assuming my understanding is correct.

clang/tools/libclang/CMakeLists.txt
88

Essentially, this is the fix: don't try to build both libclang.lib for static linking and libclang.dll (and implicitly libclang.lib) on Windows. Is that accurate?

This revision is now accepted and ready to land.Apr 23 2020, 11:09 AM
cristian.adam marked an inline comment as done.Apr 23 2020, 11:26 AM

Since I do not have commit rights, I require help to get this merged.

clang/tools/libclang/CMakeLists.txt
88

Correct.

cristian.adam marked an inline comment as done.Apr 24 2020, 2:38 AM

@mstorsjo can you merge (again) this on my behalf? Thank you in advance!

This revision was automatically updated to reflect the committed changes.

This breaks building libclang on Fedora 32 with clang 10.0 and lld 10.0. Do you mind if I revert this tomorrow if a quick fix cannot be found? Here is the relevant diagnostics:

FAILED: lib64/libclang.so.11
: && /usr/bin/clang++ -fPIC -Werror=switch -stdlib=libc++ -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wstring-conversion -fdiagnostics-color -ffunction-sections -fdata-sections -fno-common -Woverloaded-virtual -Wno-nested-anon-types -O2 -DNDEBUG -stdlib=libc++ -Wl,-z,defs -Wl,-z,nodelete -fuse-ld=lld -Wl,--color-diagnostics -Wl,-O3 -Wl,--gc-sections -Wl,--version-script,"/tmp/_update_lc/r/tools/clang/tools/libclang/libclang.exports" -shared -Wl,-soname,libclang.so.11git -o lib64/libclang.so.11 tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/BuildSystem.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CIndex.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CIndexCXX.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CIndexCodeCompletion.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CIndexDiagnostic.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CIndexHigh.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CIndexInclusionStack.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CIndexUSRs.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CIndexer.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CXComment.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CXCursor.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CXIndexDataConsumer.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CXCompilationDatabase.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CXLoadedDiagnostic.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CXSourceLocation.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CXStoredDiagnostic.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CXString.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/CXType.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/Indexing.cpp.o tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/FatalErrorHandler.cpp.o -Wl,-rpath,"\$ORIGIN/../lib64" lib64/libclangAST.a lib64/libclangBasic.a lib64/libclangDriver.a lib64/libclangFrontend.a lib64/libclangIndex.a lib64/libclangLex.a lib64/libclangSema.a lib64/libclangSerialization.a lib64/libclangTooling.a lib64/libLLVMSupport.a lib64/libclangARCMigrate.a -ldl lib64/libLLVMX86CodeGen.a lib64/libLLVMX86AsmParser.a lib64/libLLVMX86Desc.a lib64/libLLVMX86Disassembler.a lib64/libLLVMX86Info.a lib64/libLLVMAArch64CodeGen.a lib64/libLLVMAArch64AsmParser.a lib64/libLLVMAArch64Desc.a lib64/libLLVMAArch64Disassembler.a lib64/libLLVMAArch64Info.a lib64/libLLVMAArch64Utils.a lib64/libLLVMPowerPCCodeGen.a lib64/libLLVMPowerPCAsmParser.a lib64/libLLVMPowerPCDesc.a lib64/libLLVMPowerPCDisassembler.a lib64/libLLVMPowerPCInfo.a lib64/libLLVMRISCVCodeGen.a lib64/libLLVMRISCVAsmParser.a lib64/libLLVMRISCVDesc.a lib64/libLLVMRISCVDisassembler.a lib64/libLLVMRISCVInfo.a lib64/libLLVMRISCVUtils.a lib64/libLLVMCore.a lib64/libLLVMSupport.a lib64/libclangFormat.a lib64/libclangToolingInclusions.a lib64/libclangToolingCore.a lib64/libclangFrontend.a lib64/libclangDriver.a lib64/libLLVMOption.a lib64/libclangParse.a lib64/libclangSerialization.a lib64/libclangSema.a lib64/libclangEdit.a lib64/libclangRewrite.a lib64/libclangAnalysis.a lib64/libclangASTMatchers.a lib64/libclangAST.a lib64/libclangLex.a lib64/libclangBasic.a lib64/libLLVMFrontendOpenMP.a lib64/libLLVMCFGuard.a lib64/libLLVMAArch64Desc.a lib64/libLLVMAArch64Info.a lib64/libLLVMAArch64Utils.a lib64/libLLVMAsmPrinter.a lib64/libLLVMDebugInfoDWARF.a lib64/libLLVMGlobalISel.a lib64/libLLVMSelectionDAG.a lib64/libLLVMCodeGen.a lib64/libLLVMScalarOpts.a lib64/libLLVMAggressiveInstCombine.a lib64/libLLVMInstCombine.a lib64/libLLVMBitWriter.a lib64/libLLVMTransformUtils.a lib64/libLLVMTarget.a lib64/libLLVMAnalysis.a lib64/libLLVMProfileData.a lib64/libLLVMObject.a lib64/libLLVMBitReader.a lib64/libLLVMCore.a lib64/libLLVMRemarks.a lib64/libLLVMBitstreamReader.a lib64/libLLVMTextAPI.a lib64/libLLVMMCParser.a lib64/libLLVMMCDisassembler.a lib64/libLLVMMC.a lib64/libLLVMBinaryFormat.a lib64/libLLVMDebugInfoCodeView.a lib64/libLLVMDebugInfoMSF.a lib64/libLLVMSupport.a -lz -lrt -ldl -ltinfo -lpthread -lm /usr/lib64/libz3.so lib64/libLLVMDemangle.a && :
ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappings)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappings)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappings)

ld.lld: error: can't create dynamic relocation R_X86_64_32S against symbol: vtable for clang::TextDiagnosticBuffer in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in lib64/libclangFrontend.a(TextDiagnosticBuffer.cpp.o)
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappings)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappings)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappings)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappings)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappings)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappings)

ld.lld: error: can't create dynamic relocation R_X86_64_32S against symbol: vtable for clang::TextDiagnosticBuffer in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in lib64/libclangFrontend.a(TextDiagnosticBuffer.cpp.o)
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang::TextDiagnosticBuffer::~TextDiagnosticBuffer())

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappingsFromFileList)

ld.lld: error: can't create dynamic relocation R_X86_64_32S against symbol: vtable for clang::TextDiagnosticBuffer in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in lib64/libclangFrontend.a(TextDiagnosticBuffer.cpp.o)
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappingsFromFileList)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappingsFromFileList)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappingsFromFileList)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o
referenced by ARCMigrate.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/ARCMigrate.cpp.o:(clang_getRemappingsFromFileList)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/BuildSystem.cpp.o
referenced by BuildSystem.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/BuildSystem.cpp.o:(clang_VirtualFileOverlay_addFileMapping)

ld.lld: error: can't create dynamic relocation R_X86_64_32S against symbol: vtable for llvm::raw_svector_ostream in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in lib64/libLLVMSupport.a(raw_ostream.cpp.o)
referenced by BuildSystem.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/BuildSystem.cpp.o:(clang_VirtualFileOverlay_writeToBuffer)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/BuildSystem.cpp.o
referenced by BuildSystem.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/BuildSystem.cpp.o:(clang_VirtualFileOverlay_writeToBuffer)

ld.lld: error: can't create dynamic relocation R_X86_64_32S against symbol: vtable for llvm::raw_svector_ostream in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in lib64/libLLVMSupport.a(raw_ostream.cpp.o)
referenced by BuildSystem.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/BuildSystem.cpp.o:(clang_ModuleMapDescriptor_writeToBuffer)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

defined in tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/BuildSystem.cpp.o
referenced by BuildSystem.cpp

tools/clang/tools/libclang/CMakeFiles/obj.libclang.dir/BuildSystem.cpp.o:(clang_ModuleMapDescriptor_writeToBuffer)

ld.lld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)

Rather than wholly reverting this, I committed a fix: 665471907a5c072c6653a38c35f35e5d54cef220

The problem is that "NOT LIBCLANG_BUILD_STATIC" does not imply PIC, so there is no point in trying to build the SHARED version. Please let me know if I somehow missed something. Thanks!

Rather than wholly reverting this, I committed a fix: 665471907a5c072c6653a38c35f35e5d54cef220

The problem is that "NOT LIBCLANG_BUILD_STATIC" does not imply PIC, so there is no point in trying to build the SHARED version. Please let me know if I somehow missed something. Thanks!

@thakis has a use case at https://reviews.llvm.org/D74907 building a SHARED libclang with LLVM_ENABLE_PIC set to OFF.

Would the variant if(LLVM_ENABLE_PIC OR (WIN32 AND NOT LIBCLANG_BUILD_STATIC)) work for the Fedora case?

Rather than wholly reverting this, I committed a fix: 665471907a5c072c6653a38c35f35e5d54cef220

The problem is that "NOT LIBCLANG_BUILD_STATIC" does not imply PIC, so there is no point in trying to build the SHARED version. Please let me know if I somehow missed something. Thanks!

@thakis has a use case at https://reviews.llvm.org/D74907 building a SHARED libclang with LLVM_ENABLE_PIC set to OFF.

Would the variant if(LLVM_ENABLE_PIC OR (WIN32 AND NOT LIBCLANG_BUILD_STATIC)) work for the Fedora case?

Fedora is not WIN32, so sure.