Skip to content
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

[build] Migrate cmake targets from OBJECT to STATIC for libtaichi_c_api.so #6831

Merged
merged 7 commits into from
Dec 9, 2022
28 changes: 21 additions & 7 deletions cmake/TaichiCAPI.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
cmake_minimum_required(VERSION 3.0)

# This function creates a static target from OBJECT_TARGET, then link TARGET with the static target
#
# For now, we have to keep this hack because:
# 1. Existence of circular dependencies in Taichi repo (https://github.com/taichi-dev/taichi/issues/6838)
# 2. Link order restriction from `ld` linker on Linux (https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc), which has zero-tolerance w.r.t circular dependencies.
function(target_link_static_library TARGET OBJECT_TARGET)

set(STATIC_TARGET "${OBJECT_TARGET}_static")
add_library(${STATIC_TARGET})
target_link_libraries(${STATIC_TARGET} PUBLIC ${OBJECT_TARGET})
if(LINUX)
get_target_property(LINK_LIBS ${OBJECT_TARGET} LINK_LIBRARIES)
target_link_libraries(${TARGET} PRIVATE "-Wl,--start-group" "${STATIC_TARGET}" "${LINK_LIBS}" "-Wl,--end-group")
else()
target_link_libraries(${TARGET} PRIVATE "${STATIC_TARGET}")
endif()

endfunction()


set(TAICHI_C_API_NAME taichi_c_api)

file(GLOB_RECURSE C_API_SOURCE "c_api/src/taichi_core_impl.cpp")
Expand Down Expand Up @@ -28,13 +48,7 @@ if(TI_BUILD_TESTS)
endif()

add_library(${TAICHI_C_API_NAME} SHARED ${C_API_SOURCE})
target_link_libraries(${TAICHI_C_API_NAME} PRIVATE taichi_core)

# [TODO] Remove the following two linkages after rewriting AOT Demos with Device APIS
if(TI_WITH_GGUI)
target_link_libraries(${TAICHI_C_API_NAME} PRIVATE taichi_ui_vulkan)
target_link_libraries(${TAICHI_C_API_NAME} PRIVATE taichi_ui)
endif()
target_link_static_library(${TAICHI_C_API_NAME} taichi_core)

# Avoid exporting third party symbols from libtaichi_c_api.so
# Note that on Windows, external symbols will be excluded from .dll automatically, by default.
Expand Down