Skip to content

Commit 336bbd2

Browse files
cccclaifacebook-github-bot
authored andcommitted
Patch changes needed for ios/android build
Summary: Mostly tweaks for android/ios tool chain iOS doesn't like binary Android tool chains seems using clang but the default one is not linking the kernel library and the `--whole-archive` works Reviewed By: larryliu0820 Differential Revision: D48203714 fbshipit-source-id: 731bb13068fc61f06725d5b08af667b64d088aee
1 parent 26f9efa commit 336bbd2

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

CMakeLists.txt

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
cmake_minimum_required(VERSION 3.13)
4444
project(executorch)
45+
include(build/Utils.cmake)
4546

4647
# option to register custom operator `my_ops::mul3` in
4748
# `examples/custom_ops/custom_ops_1.py`
@@ -260,33 +261,35 @@ target_link_libraries(portable_kernels_bindings INTERFACE portable_kernels)
260261
# would remove them since there are no other references to them.
261262
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL
262263
"AppleClang")
263-
target_link_options(
264-
portable_kernels_bindings
265-
INTERFACE
266-
# TODO(dbort): This will cause the .a to show up on the link line twice for
267-
# targets that depend on this library; once because CMake will add it, and
268-
# once because it's in this list of args. See if there's a way to avoid
269-
# that.
270-
-Wl,-force_load,libportable_kernels_bindings.a)
271-
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
264+
if(CMAKE_TOOLCHAIN_FILE MATCHES ".*android\.toolchain\.cmake$")
265+
# For Android tool chain
266+
gcc_kernel_link_options(portable_kernels_bindings)
267+
else()
268+
# For host tool chain
269+
clang_kernel_link_options(portable_kernels_bindings)
270+
endif()
271+
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
272272
# Using gcc
273-
target_link_options(
274-
portable_kernels_bindings INTERFACE
275-
# TODO(dbort): This will cause the .a to show up on the link line twice
276-
-Wl,--whole-archive libportable_kernels_bindings.a -Wl,--no-whole-archive)
273+
gcc_kernel_link_options(portable_kernels_bindings)
277274
endif()
278275

279276
#
280277
# executor_runner: A simple commandline tool that loads and runs a program file.
281278
#
282279

283-
add_executable(executor_runner ${_executor_runner__srcs})
284-
target_link_libraries(executor_runner executorch portable_kernels_bindings
285-
gflags)
286-
target_compile_options(executor_runner PUBLIC ${_common_compile_options})
280+
# ios can only build library but not binary
281+
if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*ios\.toolchain\.cmake$")
282+
add_executable(executor_runner ${_executor_runner__srcs})
283+
target_link_libraries(executor_runner executorch portable_kernels_bindings
284+
gflags)
285+
target_compile_options(executor_runner PUBLIC ${_common_compile_options})
286+
endif()
287287

288288
# Generate custom_ops_lib based on REGISTER_EXAMPLE_CUSTOM_OPS
289289
if(REGISTER_EXAMPLE_CUSTOM_OP_1 OR REGISTER_EXAMPLE_CUSTOM_OP_2)
290290
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples/custom_ops)
291291
target_link_libraries(executor_runner custom_ops_lib)
292292
endif()
293+
294+
# Print all summary
295+
executorch_print_configuration_summary()

build/Utils.cmake

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# This file is intended to have helper functions to keep the CMakeLists.txt concise. If there are any helper function can be re-used, it's recommented to add them here.
8+
9+
10+
# Public function to print summary for all configurations. For new variable, it's recommended to add them here.
11+
function(executorch_print_configuration_summary)
12+
message(STATUS "")
13+
message(STATUS "******** Summary ********")
14+
message(STATUS " BUCK : ${BUCK2}")
15+
message(STATUS " CMAKE_CXX_STANDARD : ${CMAKE_CXX_STANDARD}")
16+
message(STATUS " CMAKE_CXX_COMPILER_ID : ${CMAKE_CXX_COMPILER_ID}")
17+
message(STATUS " CMAKE_TOOLCHAIN_FILE : ${CMAKE_TOOLCHAIN_FILE}")
18+
message(STATUS " FLATBUFFERS_BUILD_FLATC : ${FLATBUFFERS_BUILD_FLATC}")
19+
message(STATUS " FLATBUFFERS_BUILD_FLATHASH : ${FLATBUFFERS_BUILD_FLATHASH}")
20+
message(STATUS " FLATBUFFERS_BUILD_FLATLIB : ${FLATBUFFERS_BUILD_FLATLIB}")
21+
message(STATUS " FLATBUFFERS_BUILD_TESTS : ${FLATBUFFERS_BUILD_TESTS}")
22+
message(STATUS " REGISTER_EXAMPLE_CUSTOM_OPS : ${REGISTER_EXAMPLE_CUSTOM_OPS}")
23+
endfunction()
24+
25+
# This is the funtion to use -Wl to link static library, used for clang
26+
function(clang_kernel_link_options target_name)
27+
target_link_options(${target_name}
28+
INTERFACE
29+
# TODO(dbort): This will cause the .a to show up on the link line twice
30+
# for targets that depend on this library; once because CMake will add
31+
# it, and once because it's in this list of args. See if there's a way
32+
# to avoid that.
33+
-Wl,$<TARGET_FILE:${target_name}>
34+
)
35+
endfunction()
36+
37+
# This is the funtion to use -Wl, --whole-archive to link static library, used for gcc
38+
function(gcc_kernel_link_options target_name)
39+
target_link_options(${target_name}
40+
INTERFACE
41+
# TODO(dbort): This will cause the .a to show up on the link line twice
42+
-Wl,--whole-archive
43+
$<TARGET_FILE:${target_name}>
44+
-Wl,--no-whole-archive
45+
)
46+
endfunction()

0 commit comments

Comments
 (0)