Skip to content

[LLVM][Cygwin] Enable dynamic linking of libLLVM #146440

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mati865
Copy link
Contributor

@mati865 mati865 commented Jul 1, 2025

These changes allow to link everything* to shared LLVM library with MSYS2 "Cygwin" toolchain.

  • c-index-test fails with ld.bfd with this error:
/usr/lib/gcc/x86_64-pc-cygwin/15.1.0/../../../../x86_64-pc-cygwin/bin/ld: lib/libclangSerialization.a(PCHContainerOperations.cpp.o):PCHContainerOperations.cpp:(.text+0xc0): multiple definition of `clang::PCHContainerOperations::PCHContainerOperations()'; lib/libclang.dll.a(msys_clang_21_0git_dll_d008967.o):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status

Haven't tried with LLD.

These changes allow to link everything* to shared LLVM library with MSYS2 "Cygwin" toolchain.

* `c-index-test` fails with ld.bfd with this error:
```
/usr/lib/gcc/x86_64-pc-cygwin/15.1.0/../../../../x86_64-pc-cygwin/bin/ld: lib/libclangSerialization.a(PCHContainerOperations.cpp.o):PCHContainerOperations.cpp:(.text+0xc0): multiple definition of `clang::PCHContainerOperations::PCHContainerOperations()'; lib/libclang.dll.a(msys_clang_21_0git_dll_d008967.o):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
```
Haven't tried with LLD.
@llvmbot llvmbot added cmake Build system in general and CMake in particular llvm:support labels Jul 1, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 1, 2025

@llvm/pr-subscribers-llvm-support

Author: Mateusz Mikuła (mati865)

Changes

These changes allow to link everything* to shared LLVM library with MSYS2 "Cygwin" toolchain.

  • c-index-test fails with ld.bfd with this error:
/usr/lib/gcc/x86_64-pc-cygwin/15.1.0/../../../../x86_64-pc-cygwin/bin/ld: lib/libclangSerialization.a(PCHContainerOperations.cpp.o):PCHContainerOperations.cpp:(.text+0xc0): multiple definition of `clang::PCHContainerOperations::PCHContainerOperations()'; lib/libclang.dll.a(msys_clang_21_0git_dll_d008967.o):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status

Haven't tried with LLD.


Full diff: https://github.com/llvm/llvm-project/pull/146440.diff

4 Files Affected:

  • (modified) llvm/cmake/modules/AddLLVM.cmake (+3-3)
  • (modified) llvm/cmake/modules/HandleLLVMOptions.cmake (+1-1)
  • (modified) llvm/include/llvm/Support/Compiler.h (+1-1)
  • (modified) llvm/lib/Target/CMakeLists.txt (+1-1)
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index 8d8a94d1cddc4..83772ed8d2b13 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -137,7 +137,7 @@ function(add_llvm_symbol_exports target_name export_file)
       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
                    LINK_FLAGS "  -Wl,--version-script,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"")
     endif()
-  elseif(WIN32)
+  elseif(WIN32 OR CYGWIN)
     set(native_export_file "${target_name}.def")
 
     add_custom_command(OUTPUT ${native_export_file}
@@ -153,7 +153,7 @@ function(add_llvm_symbol_exports target_name export_file)
     elseif(CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
       # clang in msvc mode, calling a link.exe/lld-link style linker
       set(export_file_linker_flag "-Wl,/DEF:${export_file_linker_flag}")
-    elseif(MINGW)
+    elseif(MINGW OR CYGWIN)
       # ${export_file_linker_flag}, which is the plain file name, works as is
       # when passed to the compiler driver, which then passes it on to the
       # linker as an input file.
@@ -666,7 +666,7 @@ function(llvm_add_library name)
     # When building shared objects for each target there are some internal APIs
     # that are used across shared objects which we can't hide.
     if (LLVM_BUILD_LLVM_DYLIB_VIS AND NOT BUILD_SHARED_LIBS AND NOT APPLE AND
-        (NOT (WIN32 OR CYGWIN) OR (MINGW AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")) AND
+        (NOT (WIN32 OR CYGWIN) OR ((MINGW OR CYGWIN) AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")) AND
         NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX") AND
         NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET)
 
diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake
index 5aa047a33ba6a..c126b0d073322 100644
--- a/llvm/cmake/modules/HandleLLVMOptions.cmake
+++ b/llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -465,7 +465,7 @@ if( LLVM_ENABLE_PIC )
 endif()
 
 if((NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX")) AND
-   (NOT (WIN32 OR CYGWIN) OR (MINGW AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")))
+   (NOT (WIN32 OR CYGWIN) OR ((MINGW OR CYGWIN) AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")))
   # GCC for MinGW does nothing about -fvisibility-inlines-hidden, but warns
   # about use of the attributes. As long as we don't use the attributes (to
   # override the default) we shouldn't set the command line options either.
diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h
index f46a7aeccd0aa..f6d0d3591c33b 100644
--- a/llvm/include/llvm/Support/Compiler.h
+++ b/llvm/include/llvm/Support/Compiler.h
@@ -133,7 +133,7 @@
 #endif
 
 #if (!(defined(_WIN32) || defined(__CYGWIN__)) ||                              \
-     (defined(__MINGW32__) && defined(__clang__)))
+     ((defined(__MINGW32__) || defined(__CYGWIN__)) && defined(__clang__)))
 #define LLVM_LIBRARY_VISIBILITY LLVM_ATTRIBUTE_VISIBILITY_HIDDEN
 // Clang compilers older then 15 do not support gnu style attributes on
 // namespaces.
diff --git a/llvm/lib/Target/CMakeLists.txt b/llvm/lib/Target/CMakeLists.txt
index 9472288229cac..f5f6f5386f899 100644
--- a/llvm/lib/Target/CMakeLists.txt
+++ b/llvm/lib/Target/CMakeLists.txt
@@ -22,7 +22,7 @@ add_llvm_component_library(LLVMTarget
 # When building shared objects for each target there are some internal APIs
 # that are used across shared objects which we can't hide.
 if (NOT BUILD_SHARED_LIBS AND NOT APPLE AND
-    (NOT (WIN32 OR CYGWIN) OR (MINGW AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")) AND
+    (NOT (WIN32 OR CYGWIN) OR ((MINGW OR CYGWIN) AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")) AND
     NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX") AND
     NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET)
   # Set default visibility to hidden, so we don't export all the Target classes

@mati865
Copy link
Contributor Author

mati865 commented Jul 1, 2025

cc @jeremyd2019, this pulls the remaining changes from #134458

FYI I used:

$ mkdir build && cd build

$ CC=clang CXX=clang++ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DLLVM_LINK_LLVM_DYLIB=ON -DLLVM_ENABLE_PROJECTS='clang;lld' -DLLVM_INSTALL_TOOLCHAIN_ONLY=ON ../llvm

$ ninja

After multiple retries of ninja it gets to the last binary:

$ ninja
[1/1] Linking CXX executable bin/c-index-test.exe
FAILED: [code=1] bin/c-index-test.exe
: && /usr/bin/clang++.exe -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 -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -fno-common -Woverloaded-virtual -Wno-nested-anon-types -O3 -DNDEBUG -Wl,--enable-auto-import -Wl,--stack,16777216  -Wl,-rpath-link,/h/projects/llvm-project/build-cygwin/./lib  -Wl,--gc-sections tools/clang/tools/c-index-test/CMakeFiles/c-index-test.dir/c-index-test.c.o tools/clang/tools/c-index-test/CMakeFiles/c-index-test.dir/core_main.cpp.o -o bin/c-index-test.exe -Wl,--out-implib,lib/libc-index-test.dll.a -Wl,--major-image-version,0,--minor-image-version,0  lib/libclang.dll.a  lib/libclangAST.a  lib/libclangBasic.a  lib/libclangFrontend.a  lib/libclangIndex.a  lib/libclangSerialization.a  lib/libclangFrontend.a  lib/libclangDriver.a  lib/libclangParse.a  lib/libclangSerialization.a  lib/libclangSema.a  lib/libclangAPINotes.a  lib/libclangEdit.a  lib/libclangAnalysis.a  lib/libclangASTMatchers.a  lib/libclangAST.a  lib/libclangSupport.a  lib/libclangFormat.a  lib/libclangToolingInclusions.a  lib/libclangToolingCore.a  lib/libclangRewrite.a  lib/libclangLex.a  lib/libclangBasic.a  lib/libLLVM-21git.dll.a && :
/usr/lib/gcc/x86_64-pc-cygwin/15.1.0/../../../../x86_64-pc-cygwin/bin/ld: lib/libclangSerialization.a(PCHContainerOperations.cpp.o):PCHContainerOperations.cpp:(.text+0xc0): multiple definition of `clang::PCHContainerOperations::PCHContainerOperations()'; lib/libclang.dll.a(msys_clang_21_0git_dll_d008967.o):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
clang++: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

@jeremyd2019
Copy link
Contributor

yeah, I know about the multiple definitions error with bfd. I believe it did not happen with lld, but I also gave up on trying to get lld working for Cygwin target before I had everything working right because it was not possible to tell the difference between a Cygwin and MinGW target.

Copy link
Contributor

@jeremyd2019 jeremyd2019 left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmake Build system in general and CMake in particular llvm:support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants