diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 35822e2107..4282186cb4 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -452,7 +452,9 @@ endif() # Mark symbols to be invisible, for macOS/iOS target only # Due to many dependencies have different symbol visibility settings, set global compile flags here. if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS") - string(APPEND CMAKE_CXX_FLAGS " -fvisibility=hidden -fvisibility-inlines-hidden") + foreach(flags CMAKE_CXX_FLAGS CMAKE_OBJC_FLAGS CMAKE_OBJCXX_FLAGS) + string(APPEND ${flags} " -fvisibility=hidden -fvisibility-inlines-hidden") + endforeach() endif() #must after OpenMP settings @@ -1025,7 +1027,7 @@ function(onnxruntime_set_compile_flags target_name) target_compile_options(${target_name} PRIVATE "$<$:SHELL:--compiler-options -Wno-deprecated-copy>" "$<$:-Wno-deprecated-copy>") endif() if(onnxruntime_USE_CUDA) - if((NVCC_HAS_STRICT_ALIASING AND "${target_name}" MATCHES "cuda") OR (HAS_STRIC_ALIASING AND NOT "${target_name}" MATCHES "cuda")) + if((NVCC_HAS_STRICT_ALIASING AND "${target_name}" MATCHES "cuda") OR (HAS_STRICT_ALIASING AND NOT "${target_name}" MATCHES "cuda")) target_compile_options(${target_name} PRIVATE "$<$:-Wno-strict-aliasing>") endif() endif() @@ -1034,10 +1036,19 @@ function(onnxruntime_set_compile_flags target_name) endforeach() endfunction() -function(onnxruntime_add_shared_library target_name) - add_library(${target_name} SHARED ${ARGN}) +function(onnxruntime_set_source_file_properties target_name) + get_target_property(srcs ${target_name} SOURCES) + + # enable ARC for Objective-C/C++ + set(objective_c_cc_srcs ${srcs}) + list(FILTER objective_c_cc_srcs INCLUDE REGEX "\.mm?$") + set_property(SOURCE ${objective_c_cc_srcs} APPEND PROPERTY COMPILE_OPTIONS "-fobjc-arc") +endfunction() + +function(onnxruntime_configure_target target_name) target_link_directories(${target_name} PRIVATE ${onnxruntime_LINK_DIRS}) onnxruntime_set_compile_flags(${target_name}) + onnxruntime_set_source_file_properties(${target_name}) target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ONNXRUNTIME_ROOT}) if(onnxruntime_ENABLE_LTO) set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE) @@ -1046,16 +1057,14 @@ function(onnxruntime_add_shared_library target_name) endif() endfunction() +function(onnxruntime_add_shared_library target_name) + add_library(${target_name} SHARED ${ARGN}) + onnxruntime_configure_target(${target_name}) +endfunction() + function(onnxruntime_add_static_library target_name) add_library(${target_name} ${ARGN}) - target_link_directories(${target_name} PRIVATE ${onnxruntime_LINK_DIRS}) - onnxruntime_set_compile_flags(${target_name}) - target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ONNXRUNTIME_ROOT}) - if(onnxruntime_ENABLE_LTO) - set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE) - set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE) - set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL TRUE) - endif() + onnxruntime_configure_target(${target_name}) endfunction() #For plugins that are not linked into other targets but may be loaded dynamically at runtime using dlopen-like functionality. @@ -1067,30 +1076,15 @@ function(onnxruntime_add_shared_library_module target_name) add_library(${target_name} MODULE ${ARGN}) endif() - target_link_directories(${target_name} PRIVATE ${onnxruntime_LINK_DIRS}) - onnxruntime_set_compile_flags(${target_name}) - target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ONNXRUNTIME_ROOT}) - if(onnxruntime_ENABLE_LTO) - set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE) - set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE) - set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL TRUE) - endif() + onnxruntime_configure_target(${target_name}) endfunction() -#almost the same as the above function, except the first line of the body function(onnxruntime_add_executable target_name) if(${CMAKE_SYSTEM_NAME} MATCHES "iOSCross") message(FATAL_ERROR "iOS doesn't support commmand line tool") endif() add_executable(${target_name} ${ARGN}) - target_link_directories(${target_name} PRIVATE ${onnxruntime_LINK_DIRS}) - onnxruntime_set_compile_flags(${target_name}) - target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ONNXRUNTIME_ROOT}) - if(onnxruntime_ENABLE_LTO) - set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE) - set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE) - set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL TRUE) - endif() + onnxruntime_configure_target(${target_name}) endfunction() function(onnxruntime_add_include_to_target dst_target) diff --git a/cmake/onnxruntime_common.cmake b/cmake/onnxruntime_common.cmake index 25571a4d7f..708b506065 100644 --- a/cmake/onnxruntime_common.cmake +++ b/cmake/onnxruntime_common.cmake @@ -11,7 +11,6 @@ set(onnxruntime_common_src_patterns "${ONNXRUNTIME_ROOT}/core/common/logging/*.cc" "${ONNXRUNTIME_ROOT}/core/common/logging/sinks/*.h" "${ONNXRUNTIME_ROOT}/core/common/logging/sinks/*.cc" - "${ONNXRUNTIME_ROOT}/core/inc/*.h" "${ONNXRUNTIME_ROOT}/core/platform/env.h" "${ONNXRUNTIME_ROOT}/core/platform/env.cc" "${ONNXRUNTIME_ROOT}/core/platform/env_time.h" @@ -21,6 +20,8 @@ set(onnxruntime_common_src_patterns "${ONNXRUNTIME_ROOT}/core/platform/scoped_resource.h" "${ONNXRUNTIME_ROOT}/core/platform/telemetry.h" "${ONNXRUNTIME_ROOT}/core/platform/telemetry.cc" + "${ONNXRUNTIME_ROOT}/core/platform/logging/make_platform_default_log_sink.h" + "${ONNXRUNTIME_ROOT}/core/platform/logging/make_platform_default_log_sink.cc" ) if(WIN32) @@ -53,6 +54,13 @@ else() "${ONNXRUNTIME_ROOT}/core/platform/android/logging/*.cc" ) endif() + + if (APPLE) + list(APPEND onnxruntime_common_src_patterns + "${ONNXRUNTIME_ROOT}/core/platform/apple/logging/*.h" + "${ONNXRUNTIME_ROOT}/core/platform/apple/logging/*.mm" + ) + endif() endif() if(CMAKE_GENERATOR_PLATFORM) @@ -164,3 +172,7 @@ endif() if (onnxruntime_LINK_LIBATOMIC) list(APPEND onnxruntime_EXTERNAL_LIBRARIES atomic) endif() + +if(APPLE) + target_link_libraries(onnxruntime_common "-framework Foundation") +endif() diff --git a/cmake/onnxruntime_objectivec.cmake b/cmake/onnxruntime_objectivec.cmake index 3524bcf9a3..fa0bba6f14 100644 --- a/cmake/onnxruntime_objectivec.cmake +++ b/cmake/onnxruntime_objectivec.cmake @@ -5,18 +5,22 @@ if(${CMAKE_VERSION} VERSION_LESS "3.18") message(FATAL_ERROR "CMake 3.18+ is required when building the Objective-C API.") endif() +if(NOT APPLE) + message(FATAL_ERROR "Objective-C API must be built on an Apple platform.") +endif() + check_language(OBJC) if(CMAKE_OBJC_COMPILER) - enable_language(OBJC) + enable_language(OBJC) else() - message(FATAL_ERROR "Objective-C is not supported.") + message(FATAL_ERROR "Objective-C is not supported.") endif() check_language(OBJCXX) if(CMAKE_OBJCXX_COMPILER) - enable_language(OBJCXX) + enable_language(OBJCXX) else() - message(FATAL_ERROR "Objective-C++ is not supported.") + message(FATAL_ERROR "Objective-C++ is not supported.") endif() add_compile_options( @@ -29,8 +33,6 @@ endif() set(OBJC_ROOT "${REPO_ROOT}/objectivec") -set(OBJC_ARC_COMPILE_OPTIONS "-fobjc-arc" "-fobjc-arc-exceptions") - # onnxruntime_objc target # these headers are the public interface @@ -57,7 +59,7 @@ source_group(TREE "${OBJC_ROOT}" FILES ${onnxruntime_objc_srcs} ${onnxruntime_objc_common_srcs}) -add_library(onnxruntime_objc SHARED +onnxruntime_add_shared_library(onnxruntime_objc ${onnxruntime_objc_headers} ${onnxruntime_objc_srcs} ${onnxruntime_objc_common_srcs}) @@ -77,8 +79,6 @@ target_link_libraries(onnxruntime_objc safeint_interface ${FOUNDATION_LIB}) -target_compile_options(onnxruntime_objc PRIVATE ${OBJC_ARC_COMPILE_OPTIONS}) - set_target_properties(onnxruntime_objc PROPERTIES FRAMEWORK TRUE VERSION "1.0.0" @@ -89,6 +89,8 @@ set_target_properties(onnxruntime_objc PROPERTIES CXX_STANDARD 17 # TODO remove when everything else moves to 17 ) +set_property(TARGET onnxruntime_objc APPEND PROPERTY COMPILE_OPTIONS "-fvisibility=default") + target_link_options(onnxruntime_objc PRIVATE "-Wl,-headerpad_max_install_names") add_custom_command(TARGET onnxruntime_objc POST_BUILD @@ -122,12 +124,12 @@ if(onnxruntime_BUILD_UNIT_TESTS) ${onnxruntime_objc_test_srcs} ${onnxruntime_objc_common_srcs}) + onnxruntime_configure_target(onnxruntime_objc_test) + target_include_directories(onnxruntime_objc_test PRIVATE "${OBJC_ROOT}") - target_compile_options(onnxruntime_objc_test PRIVATE ${OBJC_ARC_COMPILE_OPTIONS}) - set_target_properties(onnxruntime_objc_test PROPERTIES FOLDER "ONNXRuntimeTest") diff --git a/cmake/onnxruntime_providers.cmake b/cmake/onnxruntime_providers.cmake index 6ba1be3d99..f7f068a592 100644 --- a/cmake/onnxruntime_providers.cmake +++ b/cmake/onnxruntime_providers.cmake @@ -721,11 +721,6 @@ if (onnxruntime_USE_COREML) "${ONNXRUNTIME_ROOT}/core/providers/coreml/model/host_utils.mm" ) - set_source_files_properties( - ${onnxruntime_providers_coreml_objcc_srcs} - COMPILE_FLAGS "${CMAKE_OBJC_FLAGS} -Xclang -x -Xclang objective-c++ -fobjc-arc" - ) - set(onnxruntime_providers_coreml_cc_srcs ${onnxruntime_providers_coreml_cc_srcs_top} ${onnxruntime_providers_coreml_cc_srcs_nested} diff --git a/onnxruntime/core/platform/apple/logging/apple_log_sink.h b/onnxruntime/core/platform/apple/logging/apple_log_sink.h new file mode 100644 index 0000000000..6f6f092679 --- /dev/null +++ b/onnxruntime/core/platform/apple/logging/apple_log_sink.h @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#pragma once + +#include "core/common/logging/isink.h" + +namespace onnxruntime { +namespace logging { + +/** + * Log sink for Apple platforms. + */ +class AppleLogSink : public ISink { + private: + void SendImpl(const Timestamp& timestamp, const std::string& logger_id, const Capture& message) override; +}; + +} // namespace logging +} // namespace onnxruntime diff --git a/onnxruntime/core/platform/apple/logging/apple_log_sink.mm b/onnxruntime/core/platform/apple/logging/apple_log_sink.mm new file mode 100644 index 0000000000..8dbd8eecd0 --- /dev/null +++ b/onnxruntime/core/platform/apple/logging/apple_log_sink.mm @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#include "core/platform/apple/logging/apple_log_sink.h" + +#import + +#include + +#include "date/date.h" + +namespace onnxruntime { +namespace logging { + +void AppleLogSink::SendImpl(const Timestamp& timestamp, const std::string& logger_id, const Capture& message) { + using date::operator<<; + std::ostringstream msg; + msg << timestamp << " [" << message.SeverityPrefix() << ":" << message.Category() << ":" << logger_id << ", " + << message.Location().ToString() << "] " << message.Message(); + NSLog(@"%s", msg.str().c_str()); +} + +} // namespace logging +} // namespace onnxruntime diff --git a/onnxruntime/core/platform/logging/make_platform_default_log_sink.cc b/onnxruntime/core/platform/logging/make_platform_default_log_sink.cc new file mode 100644 index 0000000000..2b66abfd7d --- /dev/null +++ b/onnxruntime/core/platform/logging/make_platform_default_log_sink.cc @@ -0,0 +1,28 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#include "core/platform/logging/make_platform_default_log_sink.h" + +#if defined(__ANDROID__) +#include "core/platform/android/logging/android_log_sink.h" +#elif defined(__APPLE__) +#include "core/platform/apple/logging/apple_log_sink.h" +#else +#include "core/common/logging/sinks/clog_sink.h" +#endif + +namespace onnxruntime { +namespace logging { + +std::unique_ptr MakePlatformDefaultLogSink() { +#if defined(__ANDROID__) + return std::make_unique(); +#elif defined(__APPLE__) + return std::make_unique(); +#else + return std::make_unique(); +#endif +} + +} // namespace logging +} // namespace onnxruntime diff --git a/onnxruntime/core/platform/logging/make_platform_default_log_sink.h b/onnxruntime/core/platform/logging/make_platform_default_log_sink.h new file mode 100644 index 0000000000..ff742686a9 --- /dev/null +++ b/onnxruntime/core/platform/logging/make_platform_default_log_sink.h @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#pragma once + +#include + +#include "core/common/logging/isink.h" + +namespace onnxruntime { +namespace logging { + +/** + * Creates a log sink that is appropriate for the current platform. + */ +std::unique_ptr MakePlatformDefaultLogSink(); + +} // namespace logging +} // namespace onnxruntime diff --git a/onnxruntime/core/session/ort_env.cc b/onnxruntime/core/session/ort_env.cc index 057e538632..4645b55fb6 100644 --- a/onnxruntime/core/session/ort_env.cc +++ b/onnxruntime/core/session/ort_env.cc @@ -11,11 +11,7 @@ #include "core/session/allocator_impl.h" #include "core/common/logging/logging.h" #include "core/framework/provider_shutdown.h" -#ifdef __ANDROID__ -#include "core/platform/android/logging/android_log_sink.h" -#else -#include "core/common/logging/sinks/clog_sink.h" -#endif +#include "core/platform/logging/make_platform_default_log_sink.h" using namespace onnxruntime; using namespace onnxruntime::logging; @@ -28,7 +24,7 @@ LoggingWrapper::LoggingWrapper(OrtLoggingFunction logging_function, void* logger : logging_function_(logging_function), logger_param_(logger_param) { } -void LoggingWrapper::SendImpl(const onnxruntime::logging::Timestamp& /*timestamp*/ /*timestamp*/, const std::string& logger_id, +void LoggingWrapper::SendImpl(const onnxruntime::logging::Timestamp& /*timestamp*/, const std::string& logger_id, const onnxruntime::logging::Capture& message) { std::string s = message.Location().ToString(); logging_function_(logger_param_, static_cast(message.Severity()), message.Category(), @@ -55,20 +51,16 @@ OrtEnv* OrtEnv::GetInstance(const OrtEnv::LoggingManagerConstructionInfo& lm_inf std::string name = lm_info.logid; if (lm_info.logging_function) { std::unique_ptr logger = std::make_unique(lm_info.logging_function, - lm_info.logger_param); + lm_info.logger_param); lmgr.reset(new LoggingManager(std::move(logger), static_cast(lm_info.default_warning_level), false, LoggingManager::InstanceType::Default, &name)); } else { -#ifdef __ANDROID__ - ISink* sink = new AndroidLogSink(); -#else - ISink* sink = new CLogSink(); -#endif + auto sink = MakePlatformDefaultLogSink(); - lmgr.reset(new LoggingManager(std::unique_ptr{sink}, + lmgr.reset(new LoggingManager(std::move(sink), static_cast(lm_info.default_warning_level), false, LoggingManager::InstanceType::Default, diff --git a/onnxruntime/core/session/ort_env.h b/onnxruntime/core/session/ort_env.h index cb9dcb344e..f1967f3b04 100644 --- a/onnxruntime/core/session/ort_env.h +++ b/onnxruntime/core/session/ort_env.h @@ -18,7 +18,7 @@ class LoggingWrapper : public onnxruntime::logging::ISink { public: LoggingWrapper(OrtLoggingFunction logging_function, void* logger_param); - void SendImpl(const onnxruntime::logging::Timestamp& /*timestamp*/ /*timestamp*/, const std::string& logger_id, + void SendImpl(const onnxruntime::logging::Timestamp& /*timestamp*/, const std::string& logger_id, const onnxruntime::logging::Capture& message) override; private: