diff --git a/CMakeLists.txt b/CMakeLists.txt index 47c28d7e506..23c980e7160 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -259,7 +259,11 @@ target_link_libraries(portable_kernels_bindings INTERFACE portable_kernels) # Ensure that the load-time constructor functions run. By default, the linker # would remove them since there are no other references to them. -kernel_link_options(portable_kernels_bindings) +if(APPLE) + macos_kernel_link_options(portable_kernels_bindings) +else() + kernel_link_options(portable_kernels_bindings) +endif() # # executor_runner: A simple commandline tool that loads and runs a program file. diff --git a/build/Utils.cmake b/build/Utils.cmake index fdaf6ab77f0..8ed11fc1efb 100644 --- a/build/Utils.cmake +++ b/build/Utils.cmake @@ -32,3 +32,11 @@ function(kernel_link_options target_name) -Wl,--no-whole-archive ) endfunction() + +function(macos_kernel_link_options target_name) + target_link_options(${target_name} + INTERFACE + # Same as kernel_link_options but it's for MacOS linker + -Wl,-force_load,$ + ) +endfunction() diff --git a/examples/custom_ops/CMakeLists.txt b/examples/custom_ops/CMakeLists.txt index a4876d7ac28..5660637ca30 100644 --- a/examples/custom_ops/CMakeLists.txt +++ b/examples/custom_ops/CMakeLists.txt @@ -110,7 +110,11 @@ if(REGISTER_EXAMPLE_CUSTOM_OP_2) # Ensure that the load-time constructor functions run. By default, the linker # would remove them since there are no other references to them. - kernel_link_options("custom_ops_aot_lib") + if(APPLE) + macos_kernel_link_options("custom_ops_aot_lib") + else() + kernel_link_options("custom_ops_aot_lib") + endif() endif() # 1. C++ library to register custom ops into Executorch runtime.