Skip to content

Commit

Permalink
[build] Default symbol visibility to hidden for all targets (#4545)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ghuau-innopeak authored Mar 16, 2022
1 parent be2357e commit 3a3415b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 20 deletions.
15 changes: 8 additions & 7 deletions cmake/TaichiCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 0 additions & 13 deletions misc/linker.map

This file was deleted.

0 comments on commit 3a3415b

Please sign in to comment.