From 816bf7c4936b4916223f7bd4a702daebbde09a5c Mon Sep 17 00:00:00 2001 From: Gabriel Huau Date: Tue, 15 Mar 2022 09:46:21 -0700 Subject: [PATCH] [build] Default symbol visibility to hidden for all targets Taichi can be used by external applications that has conflicting dependencies such as ImGui or Vulkan. In order to avoid any conflicts or nasty bugs, enforce the visibility to 'hidden' by default so only the public API is exported. This moves the config to the global property instead of the per target property --- cmake/TaichiCore.cmake | 15 ++++++++------- misc/linker.map | 13 ------------- 2 files changed, 8 insertions(+), 20 deletions(-) delete mode 100644 misc/linker.map diff --git a/cmake/TaichiCore.cmake b/cmake/TaichiCore.cmake index 3545dc68b0fb2..3e5bbad9043fd 100644 --- a/cmake/TaichiCore.cmake +++ b/cmake/TaichiCore.cmake @@ -8,7 +8,14 @@ option(TI_WITH_CC "Build with the C backend" ON) option(TI_WITH_VULKAN "Build with the Vulkan backend" OFF) option(TI_WITH_DX11 "Build with the DX11 backend" OFF) option(TI_EMSCRIPTENED "Build using emscripten" OFF) -set(_TI_SYMBOL_VISIBILITY default) + +# Force symbols to be 'hidden' by default so nothing is exported from the Taichi +# library including the third-party dependencies. +# As Taichi can be used by external projects, some of the internal dependencies +# such as Vulkan, ImGui, etc. could be in conflict with the dependencies of those +# projects. +set(CMAKE_CXX_VISIBILITY_PRESET hidden) +set(CMAKE_VISIBILITY_INLINES_HIDDEN ON) if(TI_EMSCRIPTENED) set(TI_WITH_LLVM OFF) @@ -225,7 +232,6 @@ endif() # everywhere in python. set(CORE_LIBRARY_NAME taichi_isolated_core) add_library(${CORE_LIBRARY_NAME} OBJECT ${TAICHI_CORE_SOURCE}) -set_target_properties(${CORE_LIBRARY_NAME} PROPERTIES CXX_VISIBILITY_PRESET ${_TI_SYMBOL_VISIBILITY}) if (APPLE) # Ask OS X to minic Linux dynamic linking behavior @@ -390,10 +396,6 @@ if (NOT WIN32) # Linux target_link_libraries(${CORE_LIBRARY_NAME} stdc++fs X11) target_link_libraries(${CORE_LIBRARY_NAME} -static-libgcc -static-libstdc++) - if ((NOT TI_EXPORT_CORE) AND (NOT ${_TI_SYMBOL_VISIBILITY} STREQUAL hidden)) # expose api for CHI IR Builder - message(WARNING "Using linker.map to hide symbols!") - target_link_libraries(${CORE_LIBRARY_NAME} -Wl,--version-script,${CMAKE_CURRENT_SOURCE_DIR}/misc/linker.map) - endif () # Avoid glibc dependencies if (TI_WITH_VULKAN) target_link_libraries(${CORE_LIBRARY_NAME} -Wl,--wrap=log2f) @@ -426,7 +428,6 @@ if(NOT TI_EMSCRIPTENED) add_library(${CORE_WITH_PYBIND_LIBRARY_NAME} SHARED) endif () - set_target_properties(${CORE_WITH_PYBIND_LIBRARY_NAME} PROPERTIES CXX_VISIBILITY_PRESET ${_TI_SYMBOL_VISIBILITY}) # Remove symbols from static libs: https://stackoverflow.com/a/14863432/12003165 if (LINUX) target_link_options(${CORE_WITH_PYBIND_LIBRARY_NAME} PUBLIC -Wl,--exclude-libs=ALL) diff --git a/misc/linker.map b/misc/linker.map deleted file mode 100644 index 8172240c474b8..0000000000000 --- a/misc/linker.map +++ /dev/null @@ -1,13 +0,0 @@ -{ -global: - extern "C++" { - taichi::*; - }; - extern "C" { - Py*; - }; -local: - extern "C" { - *; - }; -};