From 40ad6197a303f684cf28d45381c197d99ceef6e7 Mon Sep 17 00:00:00 2001 From: krazijames Date: Mon, 7 Oct 2024 12:01:41 +0900 Subject: [PATCH 1/3] Hide Symbols --- static_lib/CMakeLists.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/static_lib/CMakeLists.txt b/static_lib/CMakeLists.txt index df4f697..75944b6 100644 --- a/static_lib/CMakeLists.txt +++ b/static_lib/CMakeLists.txt @@ -1,6 +1,12 @@ cmake_minimum_required(VERSION 3.26) project(onnxruntime_static_lib) +set(C_HEADER_FILE ${ONNXRUNTIME_SOURCE_DIR}/include/onnxruntime/core/session/onnxruntime_c_api.h) +file(COPY_FILE ${C_HEADER_FILE} ${C_HEADER_FILE}.bak) +file(READ ${C_HEADER_FILE} C_HEADER_CONTENTS) +string(REPLACE "__attribute__((visibility(\"default\")))" "__attribute__((visibility(\"hidden\")))" MODIFIED_C_HEADER_CONTENTS "${C_HEADER_CONTENTS}") +file(WRITE ${C_HEADER_FILE} "${MODIFIED_C_HEADER_CONTENTS}") + set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) set(ONNX_USE_MSVC_STATIC_RUNTIME ON) set(protobuf_MSVC_STATIC_RUNTIME ON) @@ -23,3 +29,7 @@ install( RENAME ${CMAKE_STATIC_LIBRARY_PREFIX}onnxruntime${CMAKE_STATIC_LIBRARY_SUFFIX} TYPE LIB ) + +install( + CODE "file(RENAME ${C_HEADER_FILE}.bak ${C_HEADER_FILE})" +) From 3e7f6cb654fdad3acd353442535c18ccff8638a8 Mon Sep 17 00:00:00 2001 From: krazijames Date: Mon, 7 Oct 2024 12:02:28 +0900 Subject: [PATCH 2/3] Set default symbol visibility to hidden --- static_lib/CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/static_lib/CMakeLists.txt b/static_lib/CMakeLists.txt index 75944b6..62cd1aa 100644 --- a/static_lib/CMakeLists.txt +++ b/static_lib/CMakeLists.txt @@ -1,6 +1,11 @@ cmake_minimum_required(VERSION 3.26) project(onnxruntime_static_lib) +set(CMAKE_POLICY_DEFAULT_CMP0063 NEW) +set(CMAKE_C_VISIBILITY_PRESET hidden) +set(CMAKE_CXX_VISIBILITY_PRESET hidden) +set(CMAKE_VISIBILITY_INLINES_HIDDEN ON) + set(C_HEADER_FILE ${ONNXRUNTIME_SOURCE_DIR}/include/onnxruntime/core/session/onnxruntime_c_api.h) file(COPY_FILE ${C_HEADER_FILE} ${C_HEADER_FILE}.bak) file(READ ${C_HEADER_FILE} C_HEADER_CONTENTS) From 9e322a62d8291f79cf2aa74096ac0fde3e568c80 Mon Sep 17 00:00:00 2001 From: krazijames Date: Mon, 7 Oct 2024 12:11:23 +0900 Subject: [PATCH 3/3] Add comments --- static_lib/CMakeLists.txt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/static_lib/CMakeLists.txt b/static_lib/CMakeLists.txt index 62cd1aa..732d8d2 100644 --- a/static_lib/CMakeLists.txt +++ b/static_lib/CMakeLists.txt @@ -1,40 +1,49 @@ cmake_minimum_required(VERSION 3.26) project(onnxruntime_static_lib) +# Set default visibility set(CMAKE_POLICY_DEFAULT_CMP0063 NEW) set(CMAKE_C_VISIBILITY_PRESET hidden) set(CMAKE_CXX_VISIBILITY_PRESET hidden) set(CMAKE_VISIBILITY_INLINES_HIDDEN ON) +# Hide symbols set(C_HEADER_FILE ${ONNXRUNTIME_SOURCE_DIR}/include/onnxruntime/core/session/onnxruntime_c_api.h) file(COPY_FILE ${C_HEADER_FILE} ${C_HEADER_FILE}.bak) file(READ ${C_HEADER_FILE} C_HEADER_CONTENTS) string(REPLACE "__attribute__((visibility(\"default\")))" "__attribute__((visibility(\"hidden\")))" MODIFIED_C_HEADER_CONTENTS "${C_HEADER_CONTENTS}") file(WRITE ${C_HEADER_FILE} "${MODIFIED_C_HEADER_CONTENTS}") +# Set MSVC runtime library set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) set(ONNX_USE_MSVC_STATIC_RUNTIME ON) set(protobuf_MSVC_STATIC_RUNTIME ON) + +# Set ONNX Runtime options set(onnxruntime_BUILD_SHARED_LIB ON) +# Suppress C++23 deprecation warnings add_compile_definitions(_SILENCE_ALL_CXX23_DEPRECATION_WARNINGS) +# Add ONNX Runtime add_subdirectory(${ONNXRUNTIME_SOURCE_DIR}/cmake onnxruntime EXCLUDE_FROM_ALL) +# Bundle the static library include(bundle_static_library.cmake) bundle_static_library(${PROJECT_NAME} onnxruntime) +# Install the static library install( FILES $ TYPE INCLUDE ) - install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${PROJECT_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX} RENAME ${CMAKE_STATIC_LIBRARY_PREFIX}onnxruntime${CMAKE_STATIC_LIBRARY_SUFFIX} TYPE LIB ) +# Restore the original header file install( CODE "file(RENAME ${C_HEADER_FILE}.bak ${C_HEADER_FILE})" )