Skip to content

Commit 7f0ab8b

Browse files
committed
ffi/CMakeL*.txt: check support of GNUC vis ctrls
Check if the compiler supports compiler switch `--fvisibility=hidden` and the function annotation `__attribute__((visibility("default")))`. If so, add the compiler definition `HAVE_ATTRIBUTE_VISIBILITY` to the llvmlite target's compiler definitions. Further set the target's default visibility to hidden when compiling CXX files. NOTE: Changes have no impact on `cmake_minimum_required(VERSION ...)` as module `CheckCXXSourceCompiles` and `check_cxx_source_compiles` are coming with CMake versions < 3.13.
1 parent 5f77715 commit 7f0ab8b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

ffi/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ project(llvmlite_ffi)
66
include(CheckIncludeFiles)
77
include(CheckLibraryExists)
88
include(CheckCXXCompilerFlag)
9+
include(CheckCXXSourceCompiles)
910
include(CMakePushCheckState)
1011
# Work around llvm/llvm-project#83802 - LLVM's Findzstd.cmake uses variables
1112
# that require including `GNUInstallDirs`, but it does not include it itself.
@@ -18,6 +19,20 @@ find_package(LLVM REQUIRED CONFIG)
1819
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
1920
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
2021

22+
# This function checks if the CXX compiler supports
23+
# compiler flag `--fvisibility=hidden` and the
24+
# function annotation `__attribute__((visibility("default")))`.
25+
function(check_cxx_compiler_supports_fvisibility_and_attribute_visibility)
26+
set(CMAKE_REQUIRED_FLAGS "-fvisibility=hidden")
27+
set(SOURCE_CODE [=[
28+
__attribute__((visibility("default"))) int api_function() { return 0; }
29+
int main() { return 0; }
30+
]=])
31+
check_cxx_source_compiles("${SOURCE_CODE}" CXX_COMPILER_SUPPORTS_FVISIBILITY_AND_ATTRIBUTE_VISIBILITY)
32+
# propagate result to caller
33+
set(CXX_COMPILER_SUPPORTS_FVISIBILITY_AND_ATTRIBUTE_VISIBILITY ${CXX_COMPILER_SUPPORTS_FVISIBILITY_AND_ATTRIBUTE_VISIBILITY} PARENT_SCOPE)
34+
endfunction()
35+
2136
# NOTE: Keep this in sync with the version that llvmlite is declared to support
2237
set(LLVMLITE_SUPPORTED_LLVM_VERSION_DEFAULT 20)
2338

@@ -316,6 +331,11 @@ endif()
316331
message(STATUS "LLVM target link libraries: ${llvm_libs}")
317332
target_link_libraries(llvmlite ${llvm_libs})
318333

334+
check_cxx_compiler_supports_fvisibility_and_attribute_visibility()
335+
if(CXX_COMPILER_SUPPORTS_FVISIBILITY_AND_ATTRIBUTE_VISIBILITY)
336+
target_compile_definitions(llvmlite PRIVATE HAVE_ATTRIBUTE_VISIBILITY)
337+
set_target_properties(llvmlite PROPERTIES CXX_VISIBILITY_PRESET hidden)
338+
endif()
319339

320340
# -flto and --exclude-libs allow removal of unused parts of LLVM
321341
# TODO: these options are just set, they should really be tested for

0 commit comments

Comments
 (0)