Skip to content

Commit

Permalink
[build] Migrate cmake targets from OBJECT to STATIC for libtaichi_c_a…
Browse files Browse the repository at this point in the history
…pi.so (#6831)

Issue: #6793

### Brief Summary
  • Loading branch information
jim19930609 committed Dec 9, 2022
1 parent cf3678a commit a87b98f
Showing 1 changed file with 21 additions and 7 deletions.
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

0 comments on commit a87b98f

Please sign in to comment.