-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathapple_utils.py
More file actions
255 lines (219 loc) · 8.68 KB
/
apple_utils.py
File metadata and controls
255 lines (219 loc) · 8.68 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#
# Copyright 2022 Pixar
#
# Licensed under the terms set forth in the LICENSE.txt file available at
# https://openusd.org/license.
#
# Utilities for managing Apple OS build concerns.
#
# NOTE: This file and its contents may change significantly as we continue
# working to make the build scripts more modular. We anticipate providing
# a clearer and more extensible way of expressing platform specific concerns
# as we add support for additional platforms.
import sys
import locale
import os
import platform
import shlex
import subprocess
from typing import Optional, List
TARGET_NATIVE = "native"
TARGET_X86 = "x86_64"
TARGET_ARM64 = "arm64"
TARGET_UNIVERSAL = "universal"
TARGET_IOS = "iOS"
TARGET_VISIONOS = "visionOS"
TARGET_IPHONE_SIMULATOR= "iPhoneSimulator"
EMBEDDED_PLATFORMS = [TARGET_IOS, TARGET_VISIONOS, TARGET_IPHONE_SIMULATOR]
def GetBuildTargets():
return [TARGET_NATIVE,
TARGET_X86,
TARGET_ARM64,
TARGET_UNIVERSAL,
TARGET_IOS,
TARGET_VISIONOS,
TARGET_IPHONE_SIMULATOR]
def GetBuildTargetDefault():
return TARGET_NATIVE
def MacOS():
return platform.system() == "Darwin"
def TargetEmbeddedOS(context):
return context.buildTarget in EMBEDDED_PLATFORMS
def GetLocale():
return sys.stdout.encoding or locale.getdefaultlocale()[1] or "UTF-8"
def GetCommandOutput(command, **kwargs):
"""Executes the specified command and returns output or None."""
try:
return subprocess.check_output(
command, stderr=subprocess.STDOUT, **kwargs).decode(
GetLocale(), 'replace').strip()
except:
return None
def GetTargetArmArch():
# Allows the arm architecture string to be overridden by
# setting MACOS_ARM_ARCHITECTURE
return os.environ.get('MACOS_ARM_ARCHITECTURE') or TARGET_ARM64
def GetHostArch():
macArch = GetCommandOutput(["arch"])
if macArch == "i386" or macArch == TARGET_X86:
macArch = TARGET_X86
else:
macArch = GetTargetArmArch()
return macArch
def GetTargetArch(context):
if TargetEmbeddedOS(context):
return GetTargetArmArch()
if context.targetNative:
macTargets = GetHostArch()
else:
if context.targetX86:
macTargets = TARGET_X86
if context.targetARM64:
macTargets = GetTargetArmArch()
if context.targetUniversal:
macTargets = TARGET_X86 + ";" + GetTargetArmArch()
return macTargets
def IsHostArm():
return GetHostArch() != TARGET_X86
def IsTargetArm(context):
return GetTargetArch(context) != TARGET_X86
def GetTargetArchPair(context):
secondaryArch = None
if context.targetNative:
primaryArch = GetHostArch()
if context.targetX86:
primaryArch = TARGET_X86
if context.targetARM64:
primaryArch = GetTargetArmArch()
if context.buildTarget in EMBEDDED_PLATFORMS:
primaryArch = GetTargetArmArch()
if context.targetUniversal:
primaryArch = GetHostArch()
if (primaryArch == TARGET_X86):
secondaryArch = GetTargetArmArch()
else:
secondaryArch = TARGET_X86
return (primaryArch, secondaryArch)
def SupportsMacOSUniversalBinaries():
if not MacOS():
return False
XcodeOutput = GetCommandOutput(["/usr/bin/xcodebuild", "-version"])
XcodeFind = XcodeOutput.rfind('Xcode ', 0, len(XcodeOutput))
XcodeVersion = XcodeOutput[XcodeFind:].split(' ')[1]
return (XcodeVersion > '11.0')
def GetSDKRoot(context) -> Optional[str]:
sdk = "macosx"
if context.buildTarget == TARGET_IOS:
sdk = "iphoneos"
elif context.buildTarget == TARGET_VISIONOS:
sdk = "xros"
elif context.buildTarget == TARGET_IPHONE_SIMULATOR:
sdk = "iphonesimulator"
for arg in (context.cmakeBuildArgs or '').split():
if "CMAKE_OSX_SYSROOT" in arg:
override = arg.split('=')[1].strip('"').strip()
if override:
sdk = override
return GetCommandOutput(["xcrun", "--sdk", sdk, "--show-sdk-path"])
def SetTarget(context, targetName):
context.targetNative = (targetName == TARGET_NATIVE)
context.targetX86 = (targetName == TARGET_X86)
context.targetARM64 = (targetName == GetTargetArmArch())
context.targetUniversal = (targetName == TARGET_UNIVERSAL)
context.targetIOS = (targetName == TARGET_IOS)
context.targetVisionOS = (targetName == TARGET_VISIONOS)
context.targetIPhoneSimulator = (targetName == TARGET_IPHONE_SIMULATOR)
if context.targetUniversal and not SupportsMacOSUniversalBinaries():
context.targetUniversal = False
raise ValueError(
"Universal binaries only supported in macOS 11.0 and later.")
def GetTargetName(context):
return (TARGET_NATIVE if context.targetNative else
TARGET_X86 if context.targetX86 else
GetTargetArmArch() if context.targetARM64 else
TARGET_UNIVERSAL if context.targetUniversal else
context.buildTarget)
devout = open(os.devnull, 'w')
def ExtractFilesRecursive(path, cond):
files = []
for r, d, f in os.walk(path):
for file in f:
if cond(os.path.join(r,file)):
files.append(os.path.join(r, file))
return files
def CodesignFiles(files):
SDKVersion = subprocess.check_output(
['xcodebuild', '-version']).strip()[6:10]
codeSignIDs = subprocess.check_output(
['security', 'find-identity', '-vp', 'codesigning'])
codeSignID = "-"
if os.environ.get('CODE_SIGN_ID'):
codeSignID = os.environ.get('CODE_SIGN_ID')
elif float(SDKVersion) >= 11.0 and \
codeSignIDs.find(b'Apple Development') != -1:
codeSignID = "Apple Development"
elif codeSignIDs.find(b'Mac Developer') != -1:
codeSignID = "Mac Developer"
for f in files:
subprocess.call(['codesign', '-f', '-s', '{codesignid}'
.format(codesignid=codeSignID), f],
stdout=devout, stderr=devout)
def Codesign(install_path, verbose_output=False):
if not MacOS():
return False
if verbose_output:
global devout
devout = sys.stdout
files = ExtractFilesRecursive(install_path,
(lambda file: '.so' in file or '.dylib' in file))
CodesignFiles(files)
def CreateUniversalBinaries(context, libNames, x86Dir, armDir):
if not MacOS():
return False
lipoCommands = []
xcodeRoot = subprocess.check_output(
["xcode-select", "--print-path"]).decode('utf-8').strip()
lipoBinary = \
"{XCODE_ROOT}/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo".format(
XCODE_ROOT=xcodeRoot)
for libName in libNames:
outputName = os.path.join(context.instDir, "lib", libName)
if not os.path.islink("{x86Dir}/{libName}".format(
x86Dir=x86Dir, libName=libName)):
if os.path.exists(outputName):
os.remove(outputName)
lipoCmd = "{lipo} -create {x86Dir}/{libName} {armDir}/{libName} " \
"-output {outputName}".format(
lipo=lipoBinary,
x86Dir=x86Dir, armDir=armDir,
libName=libName, outputName=outputName)
lipoCommands.append(lipoCmd)
p = subprocess.Popen(shlex.split(lipoCmd))
p.wait()
for libName in libNames:
if os.path.islink("{x86Dir}/{libName}".format(
x86Dir=x86Dir, libName=libName)):
outputName = os.path.join(context.instDir, "lib", libName)
if os.path.exists(outputName):
os.unlink(outputName)
targetName = os.readlink("{x86Dir}/{libName}".format(
x86Dir=x86Dir, libName=libName))
targetName = os.path.basename(targetName)
os.symlink("{instDir}/lib/{libName}".format(
instDir=context.instDir, libName=targetName),
outputName)
return lipoCommands
def ConfigureCMakeExtraArgs(context, args:List[str]) -> List[str]:
system_name = None
if TargetEmbeddedOS(context):
system_name = context.buildTarget
if system_name == TARGET_IPHONE_SIMULATOR:
system_name = TARGET_IOS
if system_name:
args.append(f"-DCMAKE_SYSTEM_NAME={system_name}")
args.append(f"-DCMAKE_OSX_SYSROOT={GetSDKRoot(context)}")
# Required to find locally built libs not from the sysroot.
args.append(f"-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH")
args.append(f"-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH")
args.append(f"-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=BOTH")
return args