4
4
5
5
import argparse
6
6
import contextlib
7
- import glob
8
7
import os
9
8
import re
10
9
import shlex
@@ -286,15 +285,15 @@ def parse_arguments():
286
285
"CMake setup. Delete CMakeCache.txt if needed" )
287
286
parser .add_argument (
288
287
"--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 "
290
289
"CMake setup. Delete CMakeCache.txt if needed" )
291
290
parser .add_argument (
292
291
"--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 "
294
293
"CMake setup. Delete CMakeCache.txt if needed" )
295
294
parser .add_argument (
296
295
"--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 "
298
297
"CMake setup. Delete CMakeCache.txt if needed" )
299
298
parser .add_argument (
300
299
"--msvc_toolset" , help = "MSVC toolset to use. e.g. 14.11" )
@@ -319,13 +318,6 @@ def parse_arguments():
319
318
parser .add_argument (
320
319
"--ios_sysroot" , default = "" ,
321
320
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" )
329
321
parser .add_argument (
330
322
"--xcode_code_signing_team_id" , default = "" ,
331
323
help = "The development team ID used for code signing in Xcode" )
@@ -335,16 +327,6 @@ def parse_arguments():
335
327
parser .add_argument (
336
328
"--use_xcode" , action = 'store_true' ,
337
329
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" )
348
330
349
331
# WebAssembly build
350
332
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
761
743
# set vars for migraphx
762
744
"-Donnxruntime_USE_MIGRAPHX=" + ("ON" if args .use_migraphx else "OFF" ),
763
745
"-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" ),
767
746
"-Donnxruntime_DISABLE_CONTRIB_OPS=" + ("ON" if args .disable_contrib_ops else "OFF" ),
768
747
"-Donnxruntime_DISABLE_ML_OPS=" + ("ON" if args .disable_ml_ops else "OFF" ),
769
748
"-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
827
806
# It should be default ON in CI build pipelines, and OFF in packaging pipelines.
828
807
# And OFF for the people who are not actively developing onnx runtime.
829
808
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
+
830
841
if args .use_cuda :
831
842
add_cmake_define_without_override (cmake_extra_defines , "onnxruntime_USE_CUDA" , "ON" )
832
843
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
934
945
raise BuildError ("android_ndk_path required to build for Android" )
935
946
if not args .android_sdk_path :
936
947
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 ))
944
960
945
961
if args .android_cpp_shared :
946
962
cmake_args += ["-DANDROID_STL=c++_shared" ]
@@ -972,77 +988,32 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home
972
988
cmake_args += ["-Donnxruntime_USE_COREML=ON" ]
973
989
974
990
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
+ ]
1046
1017
1047
1018
if args .build_wasm :
1048
1019
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
1901
1872
run_subprocess (cmd_args , cwd = csharp_source_dir )
1902
1873
1903
1874
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
-
1914
1875
def build_protoc_for_host (cmake_path , source_dir , build_dir , args ):
1915
1876
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 ()):
1917
1878
raise BuildError (
1918
1879
'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' )
1920
1881
1921
1882
log .info (
1922
1883
"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):
1940
1901
elif is_macOS ():
1941
1902
if args .use_xcode :
1942
1903
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' )]
1948
1904
1949
1905
run_subprocess (cmd_args , cwd = protoc_build_dir )
1950
1906
# Build step
@@ -2134,9 +2090,9 @@ def main():
2134
2090
os .makedirs (build_dir , exist_ok = True )
2135
2091
2136
2092
log .info ("Build started" )
2093
+ path_to_protoc_exe = args .path_to_protoc_exe
2137
2094
if args .update :
2138
2095
cmake_extra_args = []
2139
- path_to_protoc_exe = args .path_to_protoc_exe
2140
2096
if not args .skip_submodule_sync :
2141
2097
update_submodules (source_dir )
2142
2098
if is_windows ():
@@ -2195,12 +2151,6 @@ def main():
2195
2151
elif is_macOS ():
2196
2152
if args .use_xcode :
2197
2153
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
2204
2154
2205
2155
if args .build_wasm :
2206
2156
emsdk_version = args .emsdk_version
@@ -2212,19 +2162,14 @@ def main():
2212
2162
log .info ("Activating emsdk..." )
2213
2163
run_subprocess ([emsdk_file , "activate" , emsdk_version ], cwd = emsdk_dir )
2214
2164
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 :
2217
2166
# Cross-compiling for Android, iOS, and WebAssembly
2218
2167
path_to_protoc_exe = build_protoc_for_host (
2219
2168
cmake_path , source_dir , build_dir , args )
2220
2169
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 )
2228
2173
2229
2174
if args .enable_pybind and is_windows ():
2230
2175
install_python_deps (args .numpy_version )
0 commit comments