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

[aot] Clean up exported symbols for libtaichi_c_api.so #6140

Merged
merged 21 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/scripts/aot-demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,21 @@ function build-and-test-headless-demo-desktop {
python3 ci/run_tests.py -l $TAICHI_C_API_INSTALL_DIR
}

function check-c-api-export-symbols {
cd taichi
TAICHI_REPO_DIR=$(pwd)
TAICHI_C_API_DIR=$(find $TAICHI_REPO_DIR -name libtaichi_c_api.* | head -n 1)

EXPORT_SYM=" T \| B \| D "
CAPI_SYM=" _\?ti_"
jim19930609 marked this conversation as resolved.
Show resolved Hide resolved
CAPI_UTILS_SYM=" capi::utils::"

NUM_LEAK_SYM=$(nm -C --extern-only ${TAICHI_C_API_DIR} | grep "${EXPORT_SYM}" | grep -v "${CAPI_SYM}" | grep -v "${CAPI_UTILS_SYM}" | wc -l)
if [ ${NUM_LEAK_SYM} -gt 0 ]; then
echo "Following symbols leaked from libtaichi_c_api: "
nm -C --extern-only ${TAICHI_C_API_DIR} | grep "${EXPORT_SYM}" | grep -v "${CAPI_SYM}" | grep -v "${CAPI_UTILS_SYM}"
exit 1
fi
}

$1
16 changes: 16 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ jobs:
-DTI_BUILD_TESTS:BOOL=ON
-DTI_WITH_C_API=ON

- name: Check C-API Export Symbols
run: |
[[ ${{needs.check_files.outputs.run_job}} == false ]] && exit 0
. .github/workflows/scripts/common-utils.sh

ci-docker-run-gpu --name taichi-test-check-c-api-export-symbols \
registry.taichigraphics.com/taichidev-ubuntu18.04:v0.3.4 \
/home/dev/taichi/.github/workflows/scripts/aot-demo.sh check-c-api-export-symbols

- name: Test
id: test
run: |
Expand Down Expand Up @@ -359,6 +368,13 @@ jobs:
-DTI_WITH_BACKTRACE:BOOL=ON
-DTI_WITH_C_API=ON

- name: Check C-API Export Symbols
run: |
[[ ${{needs.check_files.outputs.run_job}} == false ]] && exit 0
. .github/workflows/scripts/common-utils.sh

.github/workflows/scripts/aot-demo.sh check-c-api-export-symbols

- name: Test
id: test
run: |
Expand Down
8 changes: 8 additions & 0 deletions c_api/version_scripts/export_symbols_linux.lds
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
LIB_C_API_1.0 {
global:
ti_*;
_ZN4capi5utils*;

local:
*;
};
3 changes: 3 additions & 0 deletions c_api/version_scripts/export_symbols_mac.lds
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# MacOS
_ti_*
jim19930609 marked this conversation as resolved.
Show resolved Hide resolved
__ZN4capi5utils*
9 changes: 9 additions & 0 deletions cmake/TaichiCAPI.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ target_link_libraries(${TAICHI_C_API_NAME} PRIVATE taichi_ui_vulkan)
target_link_libraries(${TAICHI_C_API_NAME} PRIVATE taichi_ui)
endif()

# Avoid exporting third party symbols from libtaichi_c_api.so
# Note that on Windows, external symbols will be excluded from .dll automatically, by default.
if(LINUX)
target_link_options(${TAICHI_C_API_NAME} PRIVATE -Wl,--version-script,${CMAKE_CURRENT_SOURCE_DIR}/c_api/version_scripts/export_symbols_linux.lds)
elseif(APPLE)
# Unfortunately, ld on MacOS does not support --exclude-libs and we have to manually specify the exported symbols
target_link_options(${TAICHI_C_API_NAME} PRIVATE -Wl,-exported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/c_api/version_scripts/export_symbols_mac.lds)
endif()

set(C_API_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build")
set_target_properties(${TAICHI_C_API_NAME} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${C_API_OUTPUT_DIRECTORY}
Expand Down
1 change: 1 addition & 0 deletions cmake/TaichiCAPITests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ if (WIN32)
set_target_properties(${C_API_TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${C_API_TESTS_OUTPUT_DIR})
endif()
target_link_libraries(${C_API_TESTS_NAME} PRIVATE taichi_c_api)
target_link_libraries(${C_API_TESTS_NAME} PRIVATE taichi_common)
target_link_libraries(${C_API_TESTS_NAME} PRIVATE gtest_main)

if (TI_WITH_BACKTRACE)
Expand Down