forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
162 lines (151 loc) · 5.45 KB
/
CMakeLists.txt
File metadata and controls
162 lines (151 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
set(CMAKE_POLICY_VERSION_MINIMUM 3.5)
add_subdirectory(json)
# hexagon toolchain does not support mkdir, which is used in gflags.
if(NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES Hexagon)
add_subdirectory(gflags)
endif()
if(EXECUTORCH_BUILD_PYBIND)
add_subdirectory(pybind11)
endif()
if(BUILD_TESTING)
add_subdirectory(googletest)
endif()
# MARK: - flatbuffers
if(WIN32)
set(_executorch_external_project_additional_args)
else()
# Always use Make to avoid needing to codesign flatc if the project is using
# Xcode.
set(_executorch_external_project_additional_args CMAKE_GENERATOR
"Unix Makefiles"
)
endif()
# We use ExternalProject to build flatc from source to force it target the host.
# Otherwise, flatc will target the project's toolchain (i.e. iOS, or Android).
ExternalProject_Add(
flatbuffers_ep
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/flatc_ep
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/flatc_ep/src/build
SOURCE_DIR ${PROJECT_SOURCE_DIR}/third-party/flatbuffers
CMAKE_ARGS
-DFLATBUFFERS_BUILD_FLATC=ON
-DFLATBUFFERS_INSTALL=ON
-DFLATBUFFERS_BUILD_FLATHASH=OFF
-DFLATBUFFERS_BUILD_FLATLIB=OFF
-DFLATBUFFERS_BUILD_TESTS=OFF
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-DCMAKE_CXX_FLAGS="-DFLATBUFFERS_MAX_ALIGNMENT=${EXECUTORCH_FLATBUFFERS_MAX_ALIGNMENT}"
# Unset the toolchain to build for the host instead of the toolchain set for
# the project.
-DCMAKE_TOOLCHAIN_FILE=
# If building for iOS, "unset" these variables to rely on the host (macOS)
# defaults.
$<$<AND:$<BOOL:${APPLE}>,$<BOOL:$<FILTER:${PLATFORM},EXCLUDE,^MAC>>>:-DCMAKE_OSX_SYSROOT=>
-DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=${CMAKE_OSX_DEPLOYMENT_TARGET}
BUILD_BYPRODUCTS <INSTALL_DIR>/bin/flatc
${_executorch_external_project_additional_args}
)
ExternalProject_Get_Property(flatbuffers_ep INSTALL_DIR)
add_executable(flatc IMPORTED GLOBAL)
add_dependencies(flatc flatbuffers_ep)
if(WIN32 AND NOT CMAKE_CROSSCOMPILING)
# flatbuffers does not use CMAKE_BUILD_TYPE. Internally, the build forces
# Release config, but from CMake's perspective the build type is always Debug.
set_target_properties(
flatc PROPERTIES IMPORTED_LOCATION ${INSTALL_DIR}/bin/flatc.exe
)
else()
set_target_properties(
flatc PROPERTIES IMPORTED_LOCATION ${INSTALL_DIR}/bin/flatc
)
endif()
# TODO: re-enable once flatbuffers is added as a subdirectory.
# set(FLATBUFFERS_BUILD_FLATC OFF) set(FLATBUFFERS_INSTALL OFF)
# set(FLATBUFFERS_BUILD_FLATHASH OFF) set(FLATBUFFERS_BUILD_FLATLIB OFF)
# set(FLATBUFFERS_BUILD_TESTS OFF)
# MARK: - flatcc
if(WIN32)
# For some reason, when configuring the external project during build
# CMAKE_C_SIMULATE_ID is set to MSVC, but CMAKE_CXX_SIMULATE_ID is not set. To
# make sure the external project is configured correctly, set it explicitly
# here.
set(_flatcc_extra_cmake_args -DCMAKE_CXX_SIMULATE_ID=MSVC)
else()
set(_flatcc_extra_cmake_args)
endif()
# Similar to flatbuffers, we want to build flatcc for the host. See inline
# comments in the flatbuffers ExternalProject_Add for more details.
ExternalProject_Add(
flatcc_ep
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/flatcc_ep
SOURCE_DIR ${PROJECT_SOURCE_DIR}/third-party/flatcc
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/flatcc_ep/src/build
CMAKE_ARGS
-DFLATCC_RTONLY=OFF
-DFLATCC_TEST=OFF
-DFLATCC_REFLECTION=OFF
-DFLATCC_DEBUG_CLANG_SANITIZE=OFF
-DFLATCC_ALLOW_WERROR=OFF
-DFLATCC_INSTALL=ON
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DCMAKE_TOOLCHAIN_FILE=
$<$<AND:$<BOOL:${APPLE}>,$<BOOL:$<FILTER:${PLATFORM},EXCLUDE,^MAC>>>:-DCMAKE_OSX_SYSROOT=>
-DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=${CMAKE_OSX_DEPLOYMENT_TARGET}
${_flatcc_extra_cmake_args}
BUILD_BYPRODUCTS <INSTALL_DIR>/bin/flatcc
${_executorch_external_project_additional_args}
)
file(REMOVE_RECURSE ${PROJECT_SOURCE_DIR}/third-party/flatcc/lib)
ExternalProject_Get_Property(flatcc_ep INSTALL_DIR)
add_executable(flatcc_cli IMPORTED GLOBAL)
add_dependencies(flatcc_cli flatcc_ep)
if(WIN32 AND NOT CMAKE_CROSSCOMPILING)
set_target_properties(
flatcc_cli PROPERTIES IMPORTED_LOCATION ${INSTALL_DIR}/bin/flatcc.exe
)
else()
set_target_properties(
flatcc_cli PROPERTIES IMPORTED_LOCATION ${INSTALL_DIR}/bin/flatcc
)
endif()
set(FLATCC_RTONLY
ON
CACHE BOOL ""
)
set(FLATCC_TEST
OFF
CACHE BOOL ""
)
set(FLATCC_REFLECTION
OFF
CACHE BOOL ""
)
set(FLATCC_DEBUG_CLANG_SANITIZE
OFF
CACHE BOOL ""
)
set(FLATCC_INSTALL
OFF
CACHE BOOL ""
)
add_subdirectory(flatcc)
# Unfortunately flatcc writes libs directly in to the source tree [1]. So to
# ensure the target lib is created last, force flatcc_cli to build first.
#
# [1]
# https://github.com/dvidelabs/flatcc/blob/896db54787e8b730a6be482c69324751f3f5f117/CMakeLists.txt#L168
add_dependencies(flatccrt flatcc_cli)
# Fix for "relocation R_X86_64_32 against `.rodata' can not be used when making
# a shared object; recompile with -fPIC" when building on some x86 linux
# systems.
#
# Learn more: https://github.com/pytorch/executorch/pull/2467
set_property(TARGET flatccrt PROPERTY POSITION_INDEPENDENT_CODE ON)
install(TARGETS flatccrt DESTINATION ${CMAKE_BINARY_DIR}/lib)