Skip to content

Commit 2ec211e

Browse files
authored
Support the cross compiling for Apple Silicon (#5974)
* support macos_arm64 cross compiling * update the build docs * update as commented. * Update BUILD.md
1 parent fb310fb commit 2ec211e

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

BUILD.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,23 @@ Open Developer Command Prompt for Visual Studio version you are going to use. Th
6060
The default Windows CMake Generator is Visual Studio 2017, but you can also use the newer Visual Studio 2019 by passing `--cmake_generator "Visual Studio 16 2019"` to `.\build.bat`
6161

6262

63-
#### Linux/macOS
63+
#### Linux
6464
```
6565
./build.sh --config RelWithDebInfo --build_shared_lib --parallel
6666
```
67+
68+
6769
##### macOS
6870
By default, ORT is configured to be built for a minimum target macOS version of 10.12.
6971
The shared library in the release Nuget(s) and the Python wheel may be installed on macOS versions of 10.12+.
7072

71-
If you would like to use [Xcode](https://developer.apple.com/xcode/) to build the onnxruntime for x86_64 macOS, use
72-
* With Xcode 11
73+
If you would like to use [Xcode](https://developer.apple.com/xcode/) to build the onnxruntime for x86_64 macOS, please add the --user_xcode argument in the command line
7374
```
7475
./build.sh --config RelWithDebInfo --build_shared_lib --parallel --use_xcode
7576
```
76-
* With Xcode 12
77-
```
78-
./build.sh --config RelWithDebInfo --build_shared_lib --parallel --use_xcode \
79-
--cmake_extra_defines CMAKE_OSX_ARCHITECTURES=x86_64
80-
```
77+
While without this flag, the cmake build generator will be Unix makefile by default.
78+
Also, if you want to try cross compiling for Apple Silicon in an Intel-based MacOS machine, please add the argument --osx_arch arm64 with a cmake > 3.19, however the unit tests will be skipped due to the incompatible CPU instruction set.
79+
8180

8281
#### Notes
8382

tools/ci_build/build.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,8 +1702,19 @@ def run_csharp_tests(source_dir, build_dir, use_cuda, use_openvino, use_tensorrt
17021702
run_subprocess(cmd_args, cwd=csharp_source_dir)
17031703

17041704

1705+
def is_cross_compiling_on_apple(args):
1706+
if not is_macOS():
1707+
return False
1708+
if args.ios:
1709+
return True
1710+
if args.osx_arch != platform.machine():
1711+
return True
1712+
return False
1713+
1714+
17051715
def build_protoc_for_host(cmake_path, source_dir, build_dir, args):
1706-
if (args.arm or args.arm64 or args.enable_windows_store) and (not is_windows() and not args.ios):
1716+
if (args.arm or args.arm64 or args.enable_windows_store) and \
1717+
not (is_windows() or is_cross_compiling_on_apple(args)):
17071718
raise BuildError(
17081719
'Currently only support building protoc for Windows host while '
17091720
'cross-compiling for ARM/ARM64/Store and linux cross-compiling iOS')
@@ -1932,7 +1943,7 @@ def main():
19321943
# Cannot test on host build machine for cross-compiled
19331944
# builds (Override any user-defined behaviour for test if any)
19341945
if args.test:
1935-
log.info(
1946+
log.warning(
19361947
"Cannot test on host build machine for cross-compiled "
19371948
"ARM(64) builds. Will skip test running after build.")
19381949
args.test = False
@@ -1966,10 +1977,18 @@ def main():
19661977
cmake_extra_args.append('-DCMAKE_USER_MAKE_RULES_OVERRIDE=wcos_rules_override.cmake')
19671978
elif args.cmake_generator is not None and not (is_macOS() and args.use_xcode):
19681979
cmake_extra_args += ['-G', args.cmake_generator]
1969-
elif is_macOS() and args.use_xcode:
1970-
cmake_extra_args += ['-G', 'Xcode']
1980+
elif is_macOS():
1981+
if args.use_xcode:
1982+
cmake_extra_args += ['-G', 'Xcode']
1983+
if not args.ios and not args.android and \
1984+
args.osx_arch == 'arm64' and platform.machine() == 'x86_64':
1985+
if args.test:
1986+
log.warning(
1987+
"Cannot test ARM64 build on X86_64. Will skip test running after build.")
1988+
args.test = False
19711989

1972-
if (args.android or args.ios or args.enable_windows_store) and args.path_to_protoc_exe is None:
1990+
if (args.android or args.ios or args.enable_windows_store
1991+
or is_cross_compiling_on_apple(args)) and args.path_to_protoc_exe is None:
19731992
# Cross-compiling for Android and iOS
19741993
path_to_protoc_exe = build_protoc_for_host(
19751994
cmake_path, source_dir, build_dir, args)

0 commit comments

Comments
 (0)