Skip to content

Commit dd02e5c

Browse files
wenbinglDu Li
authored and
Du Li
committed
Enable the xcode build for Apple Silicon (arm64 MacOS) (#5924)
* fix the build script for macos/xcode * add the version check * correct the osx-arch configuration * typo (cherry picked from commit 1852ade)
1 parent e67f61d commit dd02e5c

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ cmake-build-debug
1414
gen
1515
*~
1616
.vs
17+
.DS_Store
1718
TestResults/
1819
.idea/
1920
onnxruntime.egg-info

cmake/onnxruntime_mlas.cmake

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ if(MSVC)
124124
)
125125
endif()
126126
else()
127+
if (CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
128+
set(ARM64 TRUE)
129+
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "arm")
130+
set(ARM TRUE)
131+
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
132+
set(X86_64 TRUE)
133+
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "i386")
134+
set(X86 TRUE)
135+
endif()
127136
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
128137
if (CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a")
129138
set(ARM TRUE)
@@ -136,15 +145,6 @@ else()
136145
endif()
137146
elseif(CMAKE_SYSTEM_NAME STREQUAL "iOS" OR CMAKE_SYSTEM_NAME STREQUAL "iOSCross")
138147
set(IOS TRUE)
139-
if (CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
140-
set(ARM64 TRUE)
141-
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "arm")
142-
set(ARM TRUE)
143-
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
144-
set(X86_64 TRUE)
145-
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "i386")
146-
set(X86 TRUE)
147-
endif()
148148
else()
149149
execute_process(
150150
COMMAND ${CMAKE_C_COMPILER} -dumpmachine

tools/ci_build/build.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
import subprocess
1111
import sys
1212
import hashlib
13+
import platform
1314
from logger import get_logger
1415
from amd_hipify import amd_hipify
15-
16+
from distutils.version import StrictVersion
1617

1718
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
1819
REPO_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, "..", ".."))
@@ -285,7 +286,9 @@ def parse_arguments():
285286
"--use_xcode", action='store_true',
286287
help="Use Xcode as cmake generator, this is only supported on MacOS.")
287288
parser.add_argument(
288-
"--osx_arch", default="arm64", choices=["arm64", "x86_64"],
289+
"--osx_arch",
290+
default="arm64" if platform.machine() == "arm64" else "x86_64",
291+
choices=["arm64", "x86_64"],
289292
help="Specify the Target specific architectures for macOS and iOS, This is only supported on MacOS")
290293
parser.add_argument(
291294
"--apple_deploy_target", type=str,
@@ -845,6 +848,13 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home
845848
if args.android_cpp_shared:
846849
cmake_args += ["-DANDROID_STL=c++_shared"]
847850

851+
if is_macOS() and not args.android:
852+
cmake_args += ["-DCMAKE_OSX_ARCHITECTURES=" + args.osx_arch]
853+
# since cmake 3.19, it uses the xcode latest buildsystem, which is not supported by this project.
854+
cmake_verstr = subprocess.check_output(['cmake', '--version']).decode('utf-8').split()[2]
855+
if args.use_xcode and StrictVersion(cmake_verstr) >= StrictVersion('3.19.0'):
856+
cmake_args += ["-T", "buildsystem=1"]
857+
848858
if args.ios:
849859
if is_macOS():
850860
needed_args = [
@@ -870,7 +880,6 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home
870880
"-DCMAKE_SYSTEM_NAME=iOS",
871881
"-Donnxruntime_BUILD_SHARED_LIB=ON",
872882
"-DCMAKE_OSX_SYSROOT=" + args.ios_sysroot,
873-
"-DCMAKE_OSX_ARCHITECTURES=" + args.osx_arch,
874883
"-DCMAKE_OSX_DEPLOYMENT_TARGET=" + args.apple_deploy_target,
875884
# we do not need protoc binary for ios cross build
876885
"-Dprotobuf_BUILD_PROTOC_BINARIES=OFF",
@@ -1735,9 +1744,8 @@ def build_protoc_for_host(cmake_path, source_dir, build_dir, args):
17351744
# CMake < 3.18 has a bug setting system arch to arm64 (if not specified) for Xcode 12,
17361745
# protoc for host should be built using host architecture
17371746
# Explicitly specify the CMAKE_OSX_ARCHITECTURES for x86_64 Mac.
1738-
import platform
1739-
if platform.machine() == 'x86_64':
1740-
cmd_args += ['-DCMAKE_OSX_ARCHITECTURES=x86_64']
1747+
cmd_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(
1748+
'arm64' if platform.machine() == 'arm64' else 'x86_64')]
17411749

17421750
run_subprocess(cmd_args, cwd=protoc_build_dir)
17431751
# Build step

0 commit comments

Comments
 (0)