Skip to content

Commit 61ec0b8

Browse files
authored
libCppInterOp: fix symbol visibility issues on Windows (#10250)
1 parent 47e568a commit 61ec0b8

File tree

2 files changed

+308
-23
lines changed

2 files changed

+308
-23
lines changed

L/libCppInterOp/bundled/patches/cmake.patch

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
From c78c1f75e135e714874159bf383b99c5ca7d72de Mon Sep 17 00:00:00 2001
1+
From 743c35943a73e461d5946683c1b095020f610c0c Mon Sep 17 00:00:00 2001
22
From: Gnimuc <[email protected]>
3-
Date: Sat, 11 Jan 2025 20:11:20 +0900
3+
Date: Sat, 11 Jan 2025 20:47:32 +0900
44
Subject: [PATCH] Rewrite CMake build scripts
55

66
---
7-
CMakeLists.txt | 567 +++--------------------
7+
CMakeLists.txt | 574 +++--------------------
88
include/CMakeLists.txt | 1 +
99
include/clang-c/CMakeLists.txt | 1 +
1010
include/clang/CMakeLists.txt | 1 +
1111
include/clang/Interpreter/CMakeLists.txt | 1 +
1212
lib/Interpreter/CMakeLists.txt | 139 +-----
13-
6 files changed, 74 insertions(+), 636 deletions(-)
13+
6 files changed, 76 insertions(+), 641 deletions(-)
1414
create mode 100644 include/CMakeLists.txt
1515
create mode 100644 include/clang-c/CMakeLists.txt
1616
create mode 100644 include/clang/CMakeLists.txt
1717
create mode 100644 include/clang/Interpreter/CMakeLists.txt
1818

1919
diff --git a/CMakeLists.txt b/CMakeLists.txt
20-
index 883885d..de19512 100644
20+
index 883885d..61e01b0 100644
2121
--- a/CMakeLists.txt
2222
+++ b/CMakeLists.txt
23-
@@ -1,520 +1,75 @@
23+
@@ -1,520 +1,72 @@
2424
-cmake_minimum_required(VERSION 3.13)
2525
+cmake_minimum_required(VERSION 3.21)
2626

@@ -43,7 +43,7 @@ index 883885d..de19512 100644
4343
-# project, using LLVM as an external library:
4444
-if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
4545
- project(CppInterOp)
46-
+set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
46+
+# set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
4747

4848
- # LLVM/Clang/Cling default paths
4949
- if (DEFINED LLVM_DIR)
@@ -529,10 +529,7 @@ index 883885d..de19512 100644
529529
- if(MSVC_VERSION LESS 1914)
530530
- set(MSVC_EXPORTLIST ${MSVC_EXPORTLIST} ??3@YAXPAX0@Z ??_V@YAXPAX0@Z)
531531
- endif()
532-
+add_library(CppInterOp SHARED)
533-
+add_subdirectory(lib)
534-
+add_subdirectory(include)
535-
532+
-
536533
- if(MSVC_VERSION GREATER_EQUAL 1936)
537534
- set(MSVC_EXPORTLIST ${MSVC_EXPORTLIST}
538535
- __std_find_trivial_1
@@ -541,26 +538,31 @@ index 883885d..de19512 100644
541538
- __std_find_trivial_8
542539
- )
543540
- endif()
541+
-
542+
-foreach(sym ${MSVC_EXPORTLIST})
543+
- set(MSVC_EXPORTS "${MSVC_EXPORTS} /EXPORT:${sym}")
544+
-endforeach(sym ${MSVC_EXPORTLIST})
545+
-
546+
-endif()
547+
-
548+
-if (CPPINTEROP_INCLUDE_DOCS)
549+
- add_subdirectory(docs)
550+
+add_library(CppInterOp SHARED)
551+
+add_subdirectory(lib)
552+
+add_subdirectory(include)
553+
+
544554
+target_include_directories(CppInterOp PUBLIC
545555
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
546556
+ $<BUILD_INTERFACE:${LLVM_INCLUDE_DIRS}>
547557
+ $<BUILD_INTERFACE:${Clang_INCLUDE_DIRS}>
548558
+ $<INSTALL_INTERFACE:include>)
549559
+
550560
+target_compile_features(CppInterOp PRIVATE cxx_std_17)
551-
552-
-foreach(sym ${MSVC_EXPORTLIST})
553-
- set(MSVC_EXPORTS "${MSVC_EXPORTS} /EXPORT:${sym}")
554-
-endforeach(sym ${MSVC_EXPORTLIST})
561+
+
555562
+target_compile_definitions(CppInterOp PUBLIC "USE_REPL")
556563
+target_compile_definitions(CppInterOp PUBLIC "__STDC_FORMAT_MACROS") # see https://github.com/tensorflow/tensorflow/issues/12998
557-
558-
+if(WIN32)
559-
+ target_compile_definitions(CppInterOp PRIVATE "__STDC_WANT_LIB_EXT1__=1") # for `_s` secure functions
560-
endif()
561-
562-
-if (CPPINTEROP_INCLUDE_DOCS)
563-
- add_subdirectory(docs)
564+
+target_compile_definitions(CppInterOp PRIVATE "_CINDEX_LIB_")
565+
+
564566
+file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" CPPINTEROP_VERSION)
565567
+string(REGEX MATCH "([0-9]*)\.([0-9]*)\.([0-9]*)" CPPINTEROP_VERSION_ONLY "${CPPINTEROP_VERSION}")
566568
+set(CPPINTEROP_VERSION_MAJOR "${CMAKE_MATCH_1}")

L/libCppInterOp/bundled/patches/windows.patch

Lines changed: 284 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 21805e1bdbdda55f36f8e7d76a99032e86b0c750 Mon Sep 17 00:00:00 2001
22
From: Gnimuc <[email protected]>
33
Date: Sat, 11 Jan 2025 20:56:57 +0900
4-
Subject: [PATCH] Restrict the use of `_s` (Annex K) functions to MSVC
4+
Subject: [PATCH 1/2] Restrict the use of `_s` (Annex K) functions to MSVC
55

66
These `_s` secure functions are introduced in C11 as optional bounds-checking interfaces, there is no guarantee that they are implemented/enabled by other compilers.
77
---
@@ -47,3 +47,286 @@ index e25ee9a..6064952 100644
4747
--
4848
2.39.5 (Apple Git-154)
4949

50+
51+
From ce769c6e91730c753e30dfd072a3d991d87e4688 Mon Sep 17 00:00:00 2001
52+
From: Gnimuc <[email protected]>
53+
Date: Sun, 12 Jan 2025 19:01:50 +0900
54+
Subject: [PATCH 2/2] Fix symbol visibility issues for C API
55+
56+
---
57+
include/clang-c/CXCppInterOp.h | 85 ++++++++++++++++++----------------
58+
1 file changed, 46 insertions(+), 39 deletions(-)
59+
60+
diff --git a/include/clang-c/CXCppInterOp.h b/include/clang-c/CXCppInterOp.h
61+
index e5b057d..f885e19 100644
62+
--- a/include/clang-c/CXCppInterOp.h
63+
+++ b/include/clang-c/CXCppInterOp.h
64+
@@ -34,7 +34,8 @@ typedef struct CXInterpreterImpl* CXInterpreter;
65+
*
66+
* \returns a \c CXInterpreter.
67+
*/
68+
-CXInterpreter clang_createInterpreter(const char* const* argv, int argc);
69+
+CINDEX_LINKAGE CXInterpreter clang_createInterpreter(const char* const* argv,
70+
+ int argc);
71+
72+
typedef void* TInterp_t;
73+
74+
@@ -43,27 +44,29 @@ typedef void* TInterp_t;
75+
*
76+
* \returns a \c CXInterpreter.
77+
*/
78+
-CXInterpreter clang_createInterpreterFromRawPtr(TInterp_t I);
79+
+CINDEX_LINKAGE CXInterpreter clang_createInterpreterFromRawPtr(TInterp_t I);
80+
81+
/**
82+
* Returns a pointer to the underlying interpreter.
83+
*/
84+
-void* clang_Interpreter_getClangInterpreter(CXInterpreter I);
85+
+CINDEX_LINKAGE void* clang_Interpreter_getClangInterpreter(CXInterpreter I);
86+
87+
/**
88+
* Returns a \c TInterp_t and takes the ownership.
89+
*/
90+
-TInterp_t clang_Interpreter_takeInterpreterAsPtr(CXInterpreter I);
91+
+CINDEX_LINKAGE TInterp_t
92+
+clang_Interpreter_takeInterpreterAsPtr(CXInterpreter I);
93+
94+
/**
95+
* Undo N previous incremental inputs.
96+
*/
97+
-enum CXErrorCode clang_Interpreter_undo(CXInterpreter I, unsigned int N);
98+
+CINDEX_LINKAGE enum CXErrorCode clang_Interpreter_undo(CXInterpreter I,
99+
+ unsigned int N);
100+
101+
/**
102+
* Dispose of the given interpreter context.
103+
*/
104+
-void clang_Interpreter_dispose(CXInterpreter I);
105+
+CINDEX_LINKAGE void clang_Interpreter_dispose(CXInterpreter I);
106+
107+
/**
108+
* Describes the return result of the different routines that do the incremental
109+
@@ -95,8 +98,9 @@ typedef enum {
110+
*
111+
* \param prepend Whether to prepend the directory to the search path.
112+
*/
113+
-void clang_Interpreter_addSearchPath(CXInterpreter I, const char* dir,
114+
- bool isUser, bool prepend);
115+
+CINDEX_LINKAGE void clang_Interpreter_addSearchPath(CXInterpreter I,
116+
+ const char* dir,
117+
+ bool isUser, bool prepend);
118+
119+
/**
120+
* Add an include path.
121+
@@ -105,7 +109,8 @@ void clang_Interpreter_addSearchPath(CXInterpreter I, const char* dir,
122+
*
123+
* \param dir The directory to add.
124+
*/
125+
-void clang_Interpreter_addIncludePath(CXInterpreter I, const char* dir);
126+
+CINDEX_LINKAGE void clang_Interpreter_addIncludePath(CXInterpreter I,
127+
+ const char* dir);
128+
129+
/**
130+
* Declares a code snippet in \c code and does not execute it.
131+
@@ -118,8 +123,8 @@ void clang_Interpreter_addIncludePath(CXInterpreter I, const char* dir);
132+
*
133+
* \returns a \c CXErrorCode.
134+
*/
135+
-enum CXErrorCode clang_Interpreter_declare(CXInterpreter I, const char* code,
136+
- bool silent);
137+
+CINDEX_LINKAGE enum CXErrorCode
138+
+clang_Interpreter_declare(CXInterpreter I, const char* code, bool silent);
139+
140+
/**
141+
* Declares and executes a code snippet in \c code.
142+
@@ -130,7 +135,8 @@ enum CXErrorCode clang_Interpreter_declare(CXInterpreter I, const char* code,
143+
*
144+
* \returns a \c CXErrorCode.
145+
*/
146+
-enum CXErrorCode clang_Interpreter_process(CXInterpreter I, const char* code);
147+
+CINDEX_LINKAGE enum CXErrorCode clang_Interpreter_process(CXInterpreter I,
148+
+ const char* code);
149+
150+
/**
151+
* An opaque pointer representing a lightweight struct that is used for carrying
152+
@@ -143,14 +149,14 @@ typedef void* CXValue;
153+
*
154+
* \returns a \c CXValue.
155+
*/
156+
-CXValue clang_createValue(void);
157+
+CINDEX_LINKAGE CXValue clang_createValue(void);
158+
159+
/**
160+
* Dispose of the given CXValue.
161+
*
162+
* \param V The CXValue to dispose.
163+
*/
164+
-void clang_Value_dispose(CXValue V);
165+
+CINDEX_LINKAGE void clang_Value_dispose(CXValue V);
166+
167+
/**
168+
* Declares, executes and stores the execution result to \c V.
169+
@@ -163,8 +169,8 @@ void clang_Value_dispose(CXValue V);
170+
*
171+
* \returns a \c CXErrorCode.
172+
*/
173+
-enum CXErrorCode clang_Interpreter_evaluate(CXInterpreter I, const char* code,
174+
- CXValue V);
175+
+CINDEX_LINKAGE enum CXErrorCode
176+
+clang_Interpreter_evaluate(CXInterpreter I, const char* code, CXValue V);
177+
178+
/**
179+
* Looks up the library if access is enabled.
180+
@@ -175,7 +181,8 @@ enum CXErrorCode clang_Interpreter_evaluate(CXInterpreter I, const char* code,
181+
*
182+
* \returns the path to the library.
183+
*/
184+
-CXString clang_Interpreter_lookupLibrary(CXInterpreter I, const char* lib_name);
185+
+CINDEX_LINKAGE CXString clang_Interpreter_lookupLibrary(CXInterpreter I,
186+
+ const char* lib_name);
187+
188+
/**
189+
* Finds \c lib_stem considering the list of search paths and loads it by
190+
@@ -189,9 +196,8 @@ CXString clang_Interpreter_lookupLibrary(CXInterpreter I, const char* lib_name);
191+
*
192+
* \returns a \c CXInterpreter_CompilationResult.
193+
*/
194+
-CXInterpreter_CompilationResult
195+
-clang_Interpreter_loadLibrary(CXInterpreter I, const char* lib_stem,
196+
- bool lookup);
197+
+CINDEX_LINKAGE CXInterpreter_CompilationResult clang_Interpreter_loadLibrary(
198+
+ CXInterpreter I, const char* lib_stem, bool lookup);
199+
200+
/**
201+
* Finds \c lib_stem considering the list of search paths and unloads it by
202+
@@ -201,7 +207,8 @@ clang_Interpreter_loadLibrary(CXInterpreter I, const char* lib_stem,
203+
*
204+
* \param lib_stem The stem of the library to unload.
205+
*/
206+
-void clang_Interpreter_unloadLibrary(CXInterpreter I, const char* lib_stem);
207+
+CINDEX_LINKAGE void clang_Interpreter_unloadLibrary(CXInterpreter I,
208+
+ const char* lib_stem);
209+
210+
/**
211+
* @}
212+
@@ -226,40 +233,41 @@ typedef struct {
213+
} CXScope;
214+
215+
// for debugging purposes
216+
-void clang_scope_dump(CXScope S);
217+
+CINDEX_LINKAGE void clang_scope_dump(CXScope S);
218+
219+
/**
220+
* Checks if a class has a default constructor.
221+
*/
222+
-bool clang_hasDefaultConstructor(CXScope S);
223+
+CINDEX_LINKAGE bool clang_hasDefaultConstructor(CXScope S);
224+
225+
/**
226+
* Returns the default constructor of a class, if any.
227+
*/
228+
-CXScope clang_getDefaultConstructor(CXScope S);
229+
+CINDEX_LINKAGE CXScope clang_getDefaultConstructor(CXScope S);
230+
231+
/**
232+
* Returns the class destructor, if any.
233+
*/
234+
-CXScope clang_getDestructor(CXScope S);
235+
+CINDEX_LINKAGE CXScope clang_getDestructor(CXScope S);
236+
237+
/**
238+
* Returns a stringified version of a given function signature in the form:
239+
* void N::f(int i, double d, long l = 0, char ch = 'a').
240+
*/
241+
-CXString clang_getFunctionSignature(CXScope func);
242+
+CINDEX_LINKAGE CXString clang_getFunctionSignature(CXScope func);
243+
244+
/**
245+
* Checks if a function is a templated function.
246+
*/
247+
-bool clang_isTemplatedFunction(CXScope func);
248+
+CINDEX_LINKAGE bool clang_isTemplatedFunction(CXScope func);
249+
250+
/**
251+
* This function performs a lookup to check if there is a templated function of
252+
* that type. \c parent is mandatory, the global scope should be used as the
253+
* default value.
254+
*/
255+
-bool clang_existsFunctionTemplate(const char* name, CXScope parent);
256+
+CINDEX_LINKAGE bool clang_existsFunctionTemplate(const char* name,
257+
+ CXScope parent);
258+
259+
typedef struct {
260+
void* Type;
261+
@@ -282,9 +290,8 @@ typedef struct {
262+
* \returns a \c CXScope representing the instantiated templated
263+
* class/function/variable.
264+
*/
265+
-CXScope clang_instantiateTemplate(CXScope tmpl,
266+
- CXTemplateArgInfo* template_args,
267+
- size_t template_args_size);
268+
+CINDEX_LINKAGE CXScope clang_instantiateTemplate(
269+
+ CXScope tmpl, CXTemplateArgInfo* template_args, size_t template_args_size);
270+
271+
/**
272+
* A fake CXType for working with the interpreter.
273+
@@ -299,12 +306,12 @@ typedef struct {
274+
/**
275+
* Gets the string of the type that is passed as a parameter.
276+
*/
277+
-CXString clang_getTypeAsString(CXQualType type);
278+
+CINDEX_LINKAGE CXString clang_getTypeAsString(CXQualType type);
279+
280+
/**
281+
* Returns the complex of the provided type.
282+
*/
283+
-CXQualType clang_getComplexType(CXQualType eltype);
284+
+CINDEX_LINKAGE CXQualType clang_getComplexType(CXQualType eltype);
285+
286+
/**
287+
* An opaque pointer representing the object of a given type (\c CXScope).
288+
@@ -314,18 +321,18 @@ typedef void* CXObject;
289+
/**
290+
* Allocates memory for the given type.
291+
*/
292+
-CXObject clang_allocate(unsigned int n);
293+
+CINDEX_LINKAGE CXObject clang_allocate(unsigned int n);
294+
295+
/**
296+
* Deallocates memory for a given class.
297+
*/
298+
-void clang_deallocate(CXObject address);
299+
+CINDEX_LINKAGE void clang_deallocate(CXObject address);
300+
301+
/**
302+
* Creates an object of class \c scope and calls its default constructor. If \c
303+
* arena is set it uses placement new.
304+
*/
305+
-CXObject clang_construct(CXScope scope, void* arena);
306+
+CINDEX_LINKAGE CXObject clang_construct(CXScope scope, void* arena);
307+
308+
/**
309+
* Creates a trampoline function and makes a call to a generic function or
310+
@@ -341,8 +348,8 @@ CXObject clang_construct(CXScope scope, void* arena);
311+
*
312+
* \param self The 'this pointer' of the object.
313+
*/
314+
-void clang_invoke(CXScope func, void* result, void** args, size_t n,
315+
- void* self);
316+
+CINDEX_LINKAGE void clang_invoke(CXScope func, void* result, void** args,
317+
+ size_t n, void* self);
318+
319+
/**
320+
* Calls the destructor of object of type \c type. When withFree is true it
321+
@@ -354,7 +361,7 @@ void clang_invoke(CXScope func, void* result, void** args, size_t n,
322+
*
323+
* \param withFree Whether to call operator delete/free or not.
324+
*/
325+
-void clang_destruct(CXObject This, CXScope S, bool withFree);
326+
+CINDEX_LINKAGE void clang_destruct(CXObject This, CXScope S, bool withFree);
327+
328+
/**
329+
* @}
330+
--
331+
2.39.5 (Apple Git-154)
332+

0 commit comments

Comments
 (0)