Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Clang][Cygwin] attempt to fix building shared libclang. #138351

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

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

Conversation

jeremyd2019
Copy link
Contributor

@mstorsjo I don't understand why this isn't working. We know it works for MinGW right?

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:as-a-library libclang and C++ API labels May 2, 2025
@llvmbot
Copy link
Member

llvmbot commented May 2, 2025

@llvm/pr-subscribers-clang

Author: None (jeremyd2019)

Changes

@mstorsjo I don't understand why this isn't working. We know it works for MinGW right?


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

2 Files Affected:

  • (modified) clang/include/clang-c/Platform.h (+1-1)
  • (modified) clang/tools/libclang/CMakeLists.txt (+1-1)
diff --git a/clang/include/clang-c/Platform.h b/clang/include/clang-c/Platform.h
index 67c1fff8ff783..8d341ddd6f8eb 100644
--- a/clang/include/clang-c/Platform.h
+++ b/clang/include/clang-c/Platform.h
@@ -22,7 +22,7 @@ LLVM_CLANG_C_EXTERN_C_BEGIN
 #ifndef CINDEX_NO_EXPORTS
   #define CINDEX_EXPORTS
 #endif
-#ifdef _WIN32
+#if defined(_WIN32) || defined(__CYGWIN__)
   #ifdef CINDEX_EXPORTS
     #ifdef _CINDEX_LIB_
       #define CINDEX_LINKAGE __declspec(dllexport)
diff --git a/clang/tools/libclang/CMakeLists.txt b/clang/tools/libclang/CMakeLists.txt
index 37a939ffcada7..454dfead0bcf5 100644
--- a/clang/tools/libclang/CMakeLists.txt
+++ b/clang/tools/libclang/CMakeLists.txt
@@ -158,7 +158,7 @@ if(ENABLE_STATIC)
 endif()
 
 if(ENABLE_SHARED)
-  if(WIN32)
+  if(WIN32 OR CYGWIN)
     set_target_properties(libclang
       PROPERTIES
       VERSION ${LIBCLANG_LIBRARY_VERSION}

Copy link

github-actions bot commented May 2, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions h -- clang/include/clang-c/Platform.h
View the diff from clang-format here.
diff --git a/clang/include/clang-c/Platform.h b/clang/include/clang-c/Platform.h
index 8d341ddd6..1c28ccf81 100644
--- a/clang/include/clang-c/Platform.h
+++ b/clang/include/clang-c/Platform.h
@@ -23,13 +23,13 @@ LLVM_CLANG_C_EXTERN_C_BEGIN
   #define CINDEX_EXPORTS
 #endif
 #if defined(_WIN32) || defined(__CYGWIN__)
-  #ifdef CINDEX_EXPORTS
-    #ifdef _CINDEX_LIB_
-      #define CINDEX_LINKAGE __declspec(dllexport)
-    #else
-      #define CINDEX_LINKAGE __declspec(dllimport)
-    #endif
-  #endif
+#ifdef CINDEX_EXPORTS
+#ifdef _CINDEX_LIB_
+#define CINDEX_LINKAGE __declspec(dllexport)
+#else
+#define CINDEX_LINKAGE __declspec(dllimport)
+#endif
+#endif
 #elif defined(CINDEX_EXPORTS) && defined(__GNUC__)
   #define CINDEX_LINKAGE __attribute__((visibility("default")))
 #endif

@mati865
Copy link
Contributor

mati865 commented May 3, 2025

I think it works when you build with Clang rather than GCC. Clang will not export symbols with hidden visibility.

@jeremyd2019
Copy link
Contributor Author

But people build mingw clang with gcc, right? They presumably don't set LLVM_ENABLE_PIC=OFF. It seems like having explicit dllexport annotations as libclang does should turn off auto-export. I didn't see --export-all-symbols in the command line anyway.

@mati865
Copy link
Contributor

mati865 commented May 3, 2025

But people build mingw clang with gcc, right?

Dunno.

It seems like having explicit dllexport annotations as libclang does should turn off auto-export. I didn't see --export-all-symbols in the command line anyway.

Yes, linker exports all the symbols by default, but explicit export disable that.

@mstorsjo
Copy link
Member

mstorsjo commented May 3, 2025

Offhand, I don't quite know here. Which configurations is this about? For mingw, the only cases I'm familiar with is building with LLVM_LINK_LLVM_DYLIB=ON, which iirc implicitly enables a corresponding one for Clang too. Not sure which of the local variables here that maps to without trying and tracing it.

Building that kind of dylib used to work with GCC, but these days it probably hits the limit on the number of exported symbols (initially in libLLVM-*.dll), unless you reduce the number of enabled targets a lot.

@jeremyd2019
Copy link
Contributor Author

-DLLVM_BUILD_LLVM_DYLIB=OFF -DLLVM_LINK_LLVM_DYLIB=OFF -DLIBCLANG_BUILD_STATIC=ON

Now we know that adding -DLLVM_ENABLE_PIC=OFF will turn off building libclang.dll, but otherwise it will be built regardless.

@jeremyd2019
Copy link
Contributor Author

Surprisingly, it looks like Cygwin (@tyan0) is working around the export limit on gcc by setting -DCMAKE_SHARED_LINKER_FLAGS=-fvisibility=hidden. I was surprised that worked on gcc - maybe it can be generalized here instead of fighting to build only static with gcc to then use that clang to build dynamic libraries?

@tyan0
Copy link

tyan0 commented May 5, 2025

Surprisingly, it looks like Cygwin (@tyan0) is working around the export limit on gcc by setting -DCMAKE_SHARED_LINKER_FLAGS=-fvisibility=hidden.

This was my misunderstanding. Building successfully was due to -DLLVM_TARGETS_TO_BUILD=X86 which restrict target architecture only to x86. -fvisibility=hidden does not seem to take effect.

I wonder why your msys2 build scripts does not fail to build even with -DLLVM_TARGETS_TO_BUILD="AArch64;ARM;X86" and gcc/g++.

@mstorsjo
Copy link
Member

mstorsjo commented May 5, 2025

Surprisingly, it looks like Cygwin (@tyan0) is working around the export limit on gcc by setting -DCMAKE_SHARED_LINKER_FLAGS=-fvisibility=hidden.

This was my misunderstanding. Building successfully was due to -DLLVM_TARGETS_TO_BUILD=X86 which restrict target architecture only to x86. -fvisibility=hidden does not seem to take effect.

Thanks for clarifying this!

Indeed, -fvisibility=hidden (or __attribute__((visibility("hidden"))) or -fvisibility-inlines-hidden etc) only have an effect with Clang, but the embedded attributes for omitting symbols from autoexport that it produces are honored by both ld.bfd and LLD.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:as-a-library libclang and C++ API clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants