Skip to content

Commit df02205

Browse files
committed
update
1 parent 20ec481 commit df02205

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+838
-612
lines changed

cmake/rpi_toolchain.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#This is a cmake tool chain file to demonstrate how to do cross-compiling for Raspberry Pi OS 64-bit. However, most Raspberry users are using 32-bit operating systems. If you are the case, please adjust the settings accordingly before use.
2+
SET(CMAKE_SYSTEM_NAME Linux)
3+
SET(CMAKE_SYSTEM_PROCESSOR aarch64)
4+
SET(CMAKE_SYSTEM_VERSION 1)
5+
SET(CMAKE_C_COMPILER aarch64-linux-gnu-gcc-8)
6+
SET(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++-8)
7+
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
8+
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
9+
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
10+
SET(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
11+
SET(CMAKE_FIND_ROOT_PATH /data/piroot)

tools/ci_build/build.py

Lines changed: 80 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import argparse
66
import contextlib
7-
import glob
87
import os
98
import re
109
import shlex
@@ -286,15 +285,15 @@ def parse_arguments():
286285
"CMake setup. Delete CMakeCache.txt if needed")
287286
parser.add_argument(
288287
"--arm", action='store_true',
289-
help="[cross-compiling] Create ARM makefiles. Requires --update and no existing cache "
288+
help="[cross-compiling] Create Windows ARM makefiles. Requires --update and no existing cache "
290289
"CMake setup. Delete CMakeCache.txt if needed")
291290
parser.add_argument(
292291
"--arm64", action='store_true',
293-
help="[cross-compiling] Create ARM64 makefiles. Requires --update and no existing cache "
292+
help="[cross-compiling] Create Windows ARM64 makefiles. Requires --update and no existing cache "
294293
"CMake setup. Delete CMakeCache.txt if needed")
295294
parser.add_argument(
296295
"--arm64ec", action='store_true',
297-
help="[cross-compiling] Create ARM64EC makefiles. Requires --update and no existing cache "
296+
help="[cross-compiling] Create Windows ARM64EC makefiles. Requires --update and no existing cache "
298297
"CMake setup. Delete CMakeCache.txt if needed")
299298
parser.add_argument(
300299
"--msvc_toolset", help="MSVC toolset to use. e.g. 14.11")
@@ -319,13 +318,6 @@ def parse_arguments():
319318
parser.add_argument(
320319
"--ios_sysroot", default="",
321320
help="Specify the location name of the macOS platform SDK to be used")
322-
parser.add_argument(
323-
"--ios_toolchain_dir", default="",
324-
help="Path to ios toolchain binaries")
325-
parser.add_argument(
326-
"--ios_toolchain_file", default="",
327-
help="Path to ios toolchain file, "
328-
"or cmake/onnxruntime_ios.toolchain.cmake will be used")
329321
parser.add_argument(
330322
"--xcode_code_signing_team_id", default="",
331323
help="The development team ID used for code signing in Xcode")
@@ -335,16 +327,6 @@ def parse_arguments():
335327
parser.add_argument(
336328
"--use_xcode", action='store_true',
337329
help="Use Xcode as cmake generator, this is only supported on MacOS.")
338-
parser.add_argument(
339-
"--osx_arch",
340-
default="arm64" if platform.machine() == "arm64" else "x86_64",
341-
choices=["arm64", "arm64e", "x86_64"],
342-
help="Specify the Target specific architectures for macOS and iOS, This is only supported on MacOS")
343-
parser.add_argument(
344-
"--apple_deploy_target", type=str,
345-
help="Specify the minimum version of the target platform "
346-
"(e.g. macOS or iOS)"
347-
"This is only supported on MacOS")
348330

349331
# WebAssembly build
350332
parser.add_argument("--build_wasm", action='store_true', help="Build for WebAssembly")
@@ -761,9 +743,6 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home
761743
# set vars for migraphx
762744
"-Donnxruntime_USE_MIGRAPHX=" + ("ON" if args.use_migraphx else "OFF"),
763745
"-Donnxruntime_MIGRAPHX_HOME=" + (migraphx_home if args.use_migraphx else ""),
764-
# By default - we currently support only cross compiling for ARM/ARM64
765-
# (no native compilation supported through this script).
766-
"-Donnxruntime_CROSS_COMPILING=" + ("ON" if args.arm64 or args.arm64ec or args.arm else "OFF"),
767746
"-Donnxruntime_DISABLE_CONTRIB_OPS=" + ("ON" if args.disable_contrib_ops else "OFF"),
768747
"-Donnxruntime_DISABLE_ML_OPS=" + ("ON" if args.disable_ml_ops else "OFF"),
769748
"-Donnxruntime_DISABLE_RTTI=" + ("ON" if args.disable_rtti or (args.minimal_build is not None
@@ -827,6 +806,38 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home
827806
# It should be default ON in CI build pipelines, and OFF in packaging pipelines.
828807
# And OFF for the people who are not actively developing onnx runtime.
829808
add_cmake_define_without_override(cmake_extra_defines, "onnxruntime_DEV_MODE", use_dev_mode(args))
809+
if is_macOS:
810+
ARCHFLAGS = os.environ.get('ARCHFLAGS', None)
811+
OSX_ARCHS = []
812+
if ARCHFLAGS:
813+
ARCHFLAGS = shlex.split(ARCHFLAGS)
814+
for i in range(0, len(ARCHFLAGS)-1):
815+
if ARCHFLAGS[i] == '-arch':
816+
OSX_ARCHS.append(ARCHFLAGS[i+1])
817+
818+
if len(OSX_ARCHS) == 1 and OSX_ARCHS[0] == platform.machine():
819+
# We have use the default value
820+
OSX_ARCHS = None
821+
822+
is_cmake_osx_architectures_set = False
823+
if OSX_ARCHS:
824+
is_cmake_osx_architectures_set = True
825+
add_cmake_define_without_override(cmake_extra_defines, "CMAKE_OSX_ARCHITECTURES", ";".join(OSX_ARCHS))
826+
else:
827+
for x in cmake_extra_defines:
828+
if x.startswith("CMAKE_OSX_ARCHITECTURES="):
829+
is_cmake_osx_architectures_set = True
830+
break
831+
832+
if is_cmake_osx_architectures_set:
833+
print("CMAKE_OSX_ARCHITECTURES is set")
834+
else:
835+
print("CMAKE_OSX_ARCHITECTURES is not set")
836+
elif args.arm64 or args.arm64ec or args.arm:
837+
# In most cases, we don't need to manually set this variable.
838+
# Please refer https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html
839+
add_cmake_define_without_override(cmake_extra_defines, "CMAKE_CROSSCOMPILING", "ON")
840+
830841
if args.use_cuda:
831842
add_cmake_define_without_override(cmake_extra_defines, "onnxruntime_USE_CUDA", "ON")
832843
add_cmake_define_without_override(cmake_extra_defines, "onnxruntime_CUDA_VERSION", args.cuda_version)
@@ -934,13 +945,18 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home
934945
raise BuildError("android_ndk_path required to build for Android")
935946
if not args.android_sdk_path:
936947
raise BuildError("android_sdk_path required to build for Android")
937-
cmake_args += [
938-
"-DCMAKE_TOOLCHAIN_FILE=" + os.path.join(
939-
args.android_ndk_path, 'build', 'cmake', 'android.toolchain.cmake'),
940-
"-DANDROID_PLATFORM=android-" + str(args.android_api),
941-
"-DANDROID_ABI=" + str(args.android_abi),
942-
"-DANDROID_MIN_SDK=" + str(args.android_api),
943-
]
948+
android_abi_str = args.android_abi
949+
if android_abi_str == 'armeabi-v7a':
950+
# This one is from NDK doc
951+
add_cmake_define_without_override(cmake_extra_defines, "ANDROID_ARM_NEON", "ON")
952+
# This one is from https://cmake.org/cmake/help/latest/variable/CMAKE_ANDROID_ARM_NEON.html
953+
# Don't use!
954+
# add_cmake_define_without_override(cmake_extra_defines, "CMAKE_ANDROID_ARM_NEON", "ON")
955+
add_cmake_define_without_override(cmake_extra_defines, "CMAKE_TOOLCHAIN_FILE", os.path.join(
956+
args.android_ndk_path, 'build', 'cmake', 'android.toolchain.cmake'))
957+
add_cmake_define_without_override(cmake_extra_defines, "ANDROID_PLATFORM", "android-%d" % args.android_api)
958+
add_cmake_define_without_override(cmake_extra_defines, "ANDROID_ABI", android_abi_str)
959+
add_cmake_define_without_override(cmake_extra_defines, "ANDROID_MIN_SDK", str(args.android_api))
944960

945961
if args.android_cpp_shared:
946962
cmake_args += ["-DANDROID_STL=c++_shared"]
@@ -972,77 +988,32 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home
972988
cmake_args += ["-Donnxruntime_USE_COREML=ON"]
973989

974990
if args.ios:
975-
if is_macOS():
976-
needed_args = [
977-
args.use_xcode,
978-
args.ios_sysroot,
979-
args.apple_deploy_target,
980-
]
981-
arg_names = [
982-
"--use_xcode " +
983-
"<need use xcode to cross build iOS on MacOS>",
984-
"--ios_sysroot " +
985-
"<the location or name of the macOS platform SDK>",
986-
"--apple_deploy_target " +
987-
"<the minimum version of the target platform>",
988-
]
989-
if not all(needed_args):
990-
raise BuildError(
991-
"iOS build on MacOS canceled due to missing arguments: " +
992-
', '.join(
993-
val for val, cond in zip(arg_names, needed_args)
994-
if not cond))
995-
cmake_args += [
996-
"-DCMAKE_SYSTEM_NAME=iOS",
997-
"-Donnxruntime_BUILD_SHARED_LIB=ON",
998-
"-DCMAKE_OSX_SYSROOT=" + args.ios_sysroot,
999-
"-DCMAKE_OSX_DEPLOYMENT_TARGET=" + args.apple_deploy_target,
1000-
# we do not need protoc binary for ios cross build
1001-
"-Dprotobuf_BUILD_PROTOC_BINARIES=OFF",
1002-
"-DCMAKE_TOOLCHAIN_FILE=" + (
1003-
args.ios_toolchain_file if args.ios_toolchain_file
1004-
else "../cmake/onnxruntime_ios.toolchain.cmake")
1005-
]
1006-
else:
1007-
# TODO: the cross compiling on Linux is not officially supported by Apple
1008-
# and is already broken with the latest codebase, so it should be removed.
1009-
# We are cross compiling on Linux
1010-
needed_args = [
1011-
args.ios_sysroot,
1012-
args.arm64 or args.arm,
1013-
args.ios_toolchain_dir
1014-
]
1015-
arg_names = [
1016-
"--ios_sysroot <path to sysroot>",
1017-
"--arm or --arm64",
1018-
"--ios_toolchain_dir <path to toolchain>"
1019-
]
1020-
if not all(needed_args):
1021-
raise BuildError(
1022-
"iOS build canceled due to missing arguments: " +
1023-
', '.join(
1024-
val for val, cond in zip(arg_names, needed_args)
1025-
if not cond))
1026-
compilers = sorted(
1027-
glob.glob(args.ios_toolchain_dir + "/bin/*-clang*"))
1028-
os.environ["PATH"] = os.path.join(
1029-
args.ios_toolchain_dir, "bin") + os.pathsep + os.environ.get(
1030-
"PATH", "")
1031-
os.environ["LD_LIBRARY_PATH"] = os.path.join(
1032-
args.ios_toolchain_dir, "/lib") + os.pathsep + os.environ.get(
1033-
"LD_LIBRARY_PATH", "")
1034-
if len(compilers) != 2:
1035-
raise BuildError(
1036-
"error identifying compilers in ios_toolchain_dir")
1037-
cmake_args += [
1038-
"-DCMAKE_OSX_ARCHITECTURES=" +
1039-
("arm64" if args.arm64 else "arm"),
1040-
"-DCMAKE_SYSTEM_NAME=iOSCross",
1041-
"-Donnxruntime_BUILD_UNIT_TESTS=OFF",
1042-
"-DCMAKE_OSX_SYSROOT=" + args.ios_sysroot,
1043-
"-DCMAKE_C_COMPILER=" + compilers[0],
1044-
"-DCMAKE_CXX_COMPILER=" + compilers[1]
1045-
]
991+
needed_args = [
992+
args.use_xcode,
993+
args.ios_sysroot,
994+
]
995+
arg_names = [
996+
"--use_xcode " +
997+
"<need use xcode to cross build iOS on MacOS>",
998+
"--ios_sysroot " +
999+
"<the location or name of the macOS platform SDK>",
1000+
]
1001+
if not all(needed_args):
1002+
raise BuildError(
1003+
"iOS build on MacOS canceled due to missing arguments: " +
1004+
', '.join(
1005+
val for val, cond in zip(arg_names, needed_args)
1006+
if not cond))
1007+
cmake_args += [
1008+
"-DCMAKE_SYSTEM_NAME=iOS",
1009+
# Use of CMAKE_CROSSCOMPILING is not recommended for projects targeting Apple devices.
1010+
"-DCMAKE_CROSSCOMPILING=OFF",
1011+
"-Donnxruntime_BUILD_SHARED_LIB=ON",
1012+
"-DCMAKE_OSX_SYSROOT=" + args.ios_sysroot,
1013+
# we do not need protoc binary for ios cross build
1014+
"-Dprotobuf_BUILD_PROTOC_BINARIES=OFF",
1015+
"-DCMAKE_TOOLCHAIN_FILE=../cmake/onnxruntime_ios.toolchain.cmake"
1016+
]
10461017

10471018
if args.build_wasm:
10481019
emsdk_dir = os.path.join(cmake_dir, "external", "emsdk")
@@ -1901,22 +1872,12 @@ def run_csharp_tests(source_dir, build_dir, use_cuda, use_openvino, use_tensorrt
19011872
run_subprocess(cmd_args, cwd=csharp_source_dir)
19021873

19031874

1904-
def is_cross_compiling_on_apple(args):
1905-
if not is_macOS():
1906-
return False
1907-
if args.ios:
1908-
return True
1909-
if args.osx_arch != platform.machine():
1910-
return True
1911-
return False
1912-
1913-
19141875
def build_protoc_for_host(cmake_path, source_dir, build_dir, args):
19151876
if (args.arm or args.arm64 or args.arm64ec or args.enable_windows_store) and \
1916-
not (is_windows() or is_cross_compiling_on_apple(args)):
1877+
not (is_windows()):
19171878
raise BuildError(
19181879
'Currently only support building protoc for Windows host while '
1919-
'cross-compiling for ARM/ARM64/Store and linux cross-compiling iOS')
1880+
'cross-compiling for ARM/ARM64/Store')
19201881

19211882
log.info(
19221883
"Building protoc for host to be used in cross-compiled build process")
@@ -1940,11 +1901,6 @@ def build_protoc_for_host(cmake_path, source_dir, build_dir, args):
19401901
elif is_macOS():
19411902
if args.use_xcode:
19421903
cmd_args += ['-G', 'Xcode']
1943-
# CMake < 3.18 has a bug setting system arch to arm64 (if not specified) for Xcode 12,
1944-
# protoc for host should be built using host architecture
1945-
# Explicitly specify the CMAKE_OSX_ARCHITECTURES for x86_64 Mac.
1946-
cmd_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(
1947-
'arm64' if platform.machine() == 'arm64' else 'x86_64')]
19481904

19491905
run_subprocess(cmd_args, cwd=protoc_build_dir)
19501906
# Build step
@@ -2134,9 +2090,9 @@ def main():
21342090
os.makedirs(build_dir, exist_ok=True)
21352091

21362092
log.info("Build started")
2093+
path_to_protoc_exe = args.path_to_protoc_exe
21372094
if args.update:
21382095
cmake_extra_args = []
2139-
path_to_protoc_exe = args.path_to_protoc_exe
21402096
if not args.skip_submodule_sync:
21412097
update_submodules(source_dir)
21422098
if is_windows():
@@ -2195,12 +2151,6 @@ def main():
21952151
elif is_macOS():
21962152
if args.use_xcode:
21972153
cmake_extra_args += ['-G', 'Xcode']
2198-
if not args.ios and not args.android and \
2199-
args.osx_arch == 'arm64' and platform.machine() == 'x86_64':
2200-
if args.test:
2201-
log.warning(
2202-
"Cannot test ARM64 build on X86_64. Will skip test running after build.")
2203-
args.test = False
22042154

22052155
if args.build_wasm:
22062156
emsdk_version = args.emsdk_version
@@ -2212,19 +2162,14 @@ def main():
22122162
log.info("Activating emsdk...")
22132163
run_subprocess([emsdk_file, "activate", emsdk_version], cwd=emsdk_dir)
22142164

2215-
if (args.android or args.ios or args.enable_windows_store or args.build_wasm
2216-
or is_cross_compiling_on_apple(args)) and args.path_to_protoc_exe is None:
2165+
if (args.android or args.ios or args.enable_windows_store or args.build_wasm) and path_to_protoc_exe is None:
22172166
# Cross-compiling for Android, iOS, and WebAssembly
22182167
path_to_protoc_exe = build_protoc_for_host(
22192168
cmake_path, source_dir, build_dir, args)
22202169

2221-
if is_ubuntu_1604():
2222-
if (args.arm or args.arm64):
2223-
raise BuildError(
2224-
"Only Windows ARM(64) cross-compiled builds supported "
2225-
"currently through this script")
2226-
if not is_docker() and not args.use_acl and not args.use_armnn:
2227-
install_python_deps()
2170+
# Cross-compiling for Android, iOS, and WebAssembly
2171+
path_to_protoc_exe = build_protoc_for_host(
2172+
cmake_path, source_dir, build_dir, args)
22282173

22292174
if args.enable_pybind and is_windows():
22302175
install_python_deps(args.numpy_version)

tools/ci_build/get_docker_image.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import argparse
66
import collections
77
import hashlib
8+
import pathlib
89
import os
910
import shlex
1011
import sys
12+
import shutil
1113
from logger import get_logger
1214

1315

@@ -110,13 +112,36 @@ def update_hash_with_file(file_info: FileInfo, hash_obj):
110112
hash_obj.update(read_bytes)
111113

112114

115+
# Return true if we need to copy manylinux build scripts to context_path
116+
def is_manylinux(dockerfile_path, context_path):
117+
ret = False
118+
with open(dockerfile_path, mode="r") as f:
119+
for index, line in enumerate(f):
120+
if line.strip() == "#Build manylinux2014 docker image begin":
121+
ret = True
122+
break
123+
return ret and not os.path.exists(os.path.join(context_path, 'manylinux-entrypoint'))
124+
125+
126+
def find_manylinux_scripts(dockerfile_path):
127+
for p in pathlib.Path(dockerfile_path).resolve().parents:
128+
print(p / 'manylinux-entrypoint')
129+
if (p / 'manylinux-entrypoint').exists():
130+
return p
131+
return None
132+
133+
113134
def generate_tag(dockerfile_path, context_path, docker_build_args_str):
114135
hash_obj = hashlib.sha256()
115136
hash_obj.update(docker_build_args_str.encode())
116137
update_hash_with_file(
117138
make_file_info_from_path(dockerfile_path), hash_obj)
118139
update_hash_with_directory(
119140
make_file_info_from_path(context_path), hash_obj)
141+
if is_manylinux(dockerfile_path, context_path):
142+
p = find_manylinux_scripts(dockerfile_path)
143+
update_hash_with_file(make_file_info_from_path(p / 'manylinux-entrypoint'), hash_obj)
144+
update_hash_with_directory(make_file_info_from_path(p / 'build_scripts'), hash_obj)
120145
return "image_content_digest_{}".format(hash_obj.hexdigest())
121146

122147

@@ -156,6 +181,16 @@ def main():
156181
run(args.docker_path, "pull", full_image_name)
157182
else:
158183
log.info("Building image...")
184+
if is_manylinux(args.dockerfile, args.context):
185+
manyliux_script_root = find_manylinux_scripts(args.dockerfile)
186+
log.info("Copying manylinux scripts from %s to %s ..." % (manyliux_script_root, args.dockerfile))
187+
shutil.copy(manyliux_script_root / 'manylinux-entrypoint',
188+
pathlib.Path(args.context) / 'manylinux-entrypoint')
189+
dest_build_scripts_dir = pathlib.Path(args.context) / 'build_scripts'
190+
shutil.copytree(manyliux_script_root / 'build_scripts', dest_build_scripts_dir)
191+
if not (dest_build_scripts_dir / 'fixup-mirrors.sh').exists():
192+
log.error("File copy failed")
193+
return -1
159194
run(args.docker_path, "build",
160195
"--pull",
161196
*shlex.split(args.docker_build_args),

0 commit comments

Comments
 (0)