forked from Autodesk/maya-usd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
475 lines (385 loc) · 16 KB
/
build.py
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
from distutils.spawn import find_executable
import argparse
import contextlib
import datetime
import distutils
import multiprocessing
import os
import platform
import re
import shlex
import shutil
import subprocess
import sys
############################################################
# Helpers for printing output
verbosity = 1
def Print(msg):
if verbosity > 0:
print msg
def PrintWarning(warning):
if verbosity > 0:
print "WARNING:", warning
def PrintStatus(status):
if verbosity >= 1:
print "STATUS:", status
def PrintInfo(info):
if verbosity >= 2:
print "INFO:", info
def PrintCommandOutput(output):
if verbosity >= 3:
sys.stdout.write(output)
def PrintError(error):
if verbosity >= 3 and sys.exc_info()[1] is not None:
import traceback
traceback.print_exc()
print "ERROR:", error
############################################################
def Windows():
return platform.system() == "Windows"
def Linux():
return platform.system() == "Linux"
def MacOS():
return platform.system() == "Darwin"
def GetCommandOutput(command):
"""Executes the specified command and returns output or None."""
try:
return subprocess.check_output(
shlex.split(command), stderr=subprocess.STDOUT).strip()
except subprocess.CalledProcessError:
pass
return None
def GetXcodeDeveloperDirectory():
"""Returns the active developer directory as reported by 'xcode-select -p'.
Returns None if none is set."""
if not MacOS():
return None
return GetCommandOutput("xcode-select -p")
def GetVisualStudioCompilerAndVersion():
"""Returns a tuple containing the path to the Visual Studio compiler
and a tuple for its version, e.g. (14, 0). If the compiler is not found
or version number cannot be determined, returns None."""
if not Windows():
return None
msvcCompiler = find_executable('cl')
if msvcCompiler:
# VisualStudioVersion environment variable should be set by the
# Visual Studio Command Prompt.
match = re.search(
"(\d+).(\d+)",
os.environ.get("VisualStudioVersion", ""))
if match:
return (msvcCompiler, tuple(int(v) for v in match.groups()))
return None
def IsVisualStudio2017OrGreater():
VISUAL_STUDIO_2017_VERSION = (15, 0)
msvcCompilerAndVersion = GetVisualStudioCompilerAndVersion()
if msvcCompilerAndVersion:
_, version = msvcCompilerAndVersion
return version >= VISUAL_STUDIO_2017_VERSION
return False
def GetPythonInfo():
"""Returns a tuple containing the path to the Python executable, shared
library, and include directory corresponding to the version of Python
currently running. Returns None if any path could not be determined. This
function always returns None on Windows or Linux.
This function is primarily used to determine which version of
Python USD should link against when multiple versions are installed.
"""
# We just skip all this on Windows. Users on Windows are unlikely to have
# multiple copies of the same version of Python, so the problem this
# function is intended to solve doesn't arise on that platform.
if Windows():
return None
# We also skip all this on Linux. The below code gets the wrong answer on
# certain distributions like Ubuntu, which organizes libraries based on
# multiarch. The below code yields /usr/lib/libpython2.7.so, but
# the library is actually in /usr/lib/x86_64-linux-gnu. Since the problem
# this function is intended to solve primarily occurs on macOS, so it's
# simpler to just skip this for now.
if Linux():
return None
try:
import distutils.sysconfig
pythonExecPath = None
pythonLibPath = None
pythonPrefix = distutils.sysconfig.PREFIX
if pythonPrefix:
pythonExecPath = os.path.join(pythonPrefix, 'bin', 'python')
pythonLibPath = os.path.join(pythonPrefix, 'lib', 'libpython2.7.dylib')
pythonIncludeDir = distutils.sysconfig.get_python_inc()
except:
return None
if pythonExecPath and pythonIncludeDir and pythonLibPath:
# Ensure that the paths are absolute, since depending on the version of
# Python being run and the path used to invoke it, we may have gotten a
# relative path from distutils.sysconfig.PREFIX.
return (
os.path.abspath(pythonExecPath),
os.path.abspath(pythonLibPath),
os.path.abspath(pythonIncludeDir))
return None
def GetCPUCount():
try:
return multiprocessing.cpu_count()
except NotImplementedError:
return 1
def Run(cmd, logCommandOutput=True):
"""Run the specified command in a subprocess."""
PrintInfo('Running "{cmd}"'.format(cmd=cmd))
with open(context.logFileLocation, "a") as logfile:
logfile.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
logfile.write("\n")
logfile.write(cmd)
logfile.write("\n")
# Let exceptions escape from subprocess calls -- higher level
# code will handle them.
if logCommandOutput:
p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
while True:
l = p.stdout.readline()
if l != "":
logfile.write(l)
PrintCommandOutput(l)
elif p.poll() is not None:
break
else:
p = subprocess.Popen(shlex.split(cmd))
p.wait()
if p.returncode != 0:
# If verbosity >= 3, we'll have already been printing out command output
# so no reason to print the log file again.
if verbosity < 3:
with open(context.logFileLocation, "r") as logfile:
Print(logfile.read())
raise RuntimeError("Failed to run '{cmd}'\nSee {log} for more details."
.format(cmd=cmd, log=os.path.abspath(context.logFileLocation)))
def BuildVariant(context):
if context.buildDebug:
return "Debug"
elif context.buildRelease:
return "Release"
elif context.buildRelWithDebug:
return "RelWithDebInfo"
return "RelWithDebInfo"
def FormatMultiProcs(numJobs, generator):
tag = "-j"
if generator:
if "Visual Studio" in generator:
tag = "/M:"
elif "Xcode" in generator:
tag = "-j "
return "{tag}{procs}".format(tag=tag, procs=numJobs)
############################################################
# contextmanager
@contextlib.contextmanager
def CurrentWorkingDirectory(dir):
"""Context manager that sets the current working directory to the given
directory and resets it to the original directory when closed."""
curdir = os.getcwd()
os.chdir(dir)
try:
yield
finally:
os.chdir(curdir)
############################################################
# CMAKE
def RunCMake(context, force, extraArgs=None):
"""Invoke CMake to configure, build, and install a library whose
source code is located in the current working directory."""
srcDir = os.getcwd()
instDir = context.instDir
buildDir = context.buildDir
if force and os.path.isdir(buildDir):
shutil.rmtree(buildDir)
if force and os.path.isdir(instDir):
shutil.rmtree(instDir)
if not os.path.isdir(buildDir):
os.makedirs(buildDir)
generator = context.cmakeGenerator
# On Windows, we need to explicitly specify the generator to ensure we're
# building a 64-bit project. (Surely there is a better way to do this?)
# TODO: figure out exactly what "vcvarsall.bat x64" sets to force x64
if generator is None and Windows():
if IsVisualStudio2017OrGreater():
generator = "Visual Studio 15 2017 Win64"
else:
generator = "Visual Studio 14 2015 Win64"
if generator is not None:
generator = '-G "{gen}"'.format(gen=generator)
# On MacOS, enable the use of @rpath for relocatable builds.
osx_rpath = None
if MacOS():
osx_rpath = "-DCMAKE_MACOSX_RPATH=ON"
# get build variant
variant= BuildVariant(context)
with CurrentWorkingDirectory(buildDir):
# recreate build_log.txt everytime the script runs
if os.path.isfile(context.logFileLocation):
os.remove(context.logFileLocation)
Run('cmake '
'-DCMAKE_INSTALL_PREFIX="{instDir}" '
'-DCMAKE_BUILD_TYPE={variant} '
'-DCMAKE_EXPORT_COMPILE_COMMANDS=ON'
'{osx_rpath} '
'{generator} '
'{extraArgs} '
'"{srcDir}"'
.format(instDir=instDir,
variant=variant,
srcDir=srcDir,
osx_rpath=(osx_rpath or ""),
generator=(generator or ""),
extraArgs=(" ".join(extraArgs) if extraArgs else "")))
Run("cmake --build . --config {variant} --target install -- {multiproc}"
.format(variant=variant,
multiproc=FormatMultiProcs(context.numJobs, generator)))
############################################################
# Maya USD
def InstallMayaUSD(context, force, buildArgs):
with CurrentWorkingDirectory(context.mayaUsdSrcDir):
extraArgs = []
if context.mayaLocation:
extraArgs.append('-DMAYA_LOCATION="{mayaLocation}"'
.format(mayaLocation=context.mayaLocation))
if context.pxrUsdLocation:
extraArgs.append('-DPXR_USD_LOCATION="{pxrUsdLocation}"'
.format(pxrUsdLocation=context.pxrUsdLocation))
if context.devkitLocation:
extraArgs.append('-DMAYA_DEVKIT_LOCATION="{devkitLocation}"'
.format(devkitLocation=context.devkitLocation))
# Add on any user-specified extra arguments.
extraArgs += buildArgs
RunCMake(context, force, extraArgs)
############################################################
# ArgumentParser
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("workspace_location", type=str,
help="Directory where the project use as a workspace to build and install plugin/libraries.")
parser.add_argument("--generator", type=str,
help=("CMake generator to use when building libraries with "
"cmake"))
parser.add_argument("--build-location", type=str,
help=("Set Build directory"
"(default: <workspace_location>/build-location)"))
parser.add_argument("--install-location", type=str,
help=("Set Install directory"
"(default: <workspace_location>/install-location)"))
parser.add_argument("--maya-location", type=str,
help="Directory where Maya is installed.")
parser.add_argument("--pxrusd-location", type=str,
help="Directory where Pixar USD is installed.")
parser.add_argument("--devkit-location", type=str,
help="Directory where Maya Devkit is installed.")
parser.add_argument("--build-debug", dest="build_debug", action="store_true",
help="Build in Debug mode")
parser.add_argument("--build-release", dest="build_release", action="store_true",
help="Build in Release mode")
parser.add_argument("--build-relwithdebug", dest="build_relwithdebug", action="store_true",
help="Build in RelWithDebInfo mode")
parser.add_argument("--build-args", type=str, nargs="*", default=[],
help=("Custom arguments to pass to build system when "
"building libraries"))
parser.add_argument("-j", "--jobs", type=int, default=GetCPUCount(),
help=("Number of build jobs to run in parallel. "
"(default: # of processors [{0}])"
.format(GetCPUCount())))
parser.add_argument("--force", type=str, action="append", dest="force_clean_build",
default=[],
help=("Force clean build."))
args = parser.parse_args()
############################################################
# InstallContext
class InstallContext:
def __init__(self, args):
# Assume the project's top level cmake is in the current source directory
self.mayaUsdSrcDir = os.path.normpath(
os.path.join(os.path.abspath(os.path.dirname(__file__))))
# Build type
# Must be done early, so we can call BuildVariant(self)
self.buildDebug = args.build_debug
self.buildRelease = args.build_release
self.buildRelWithDebug = args.build_relwithdebug
# Workspace directory
self.workspaceDir = os.path.abspath(args.workspace_location)
# Build directory
self.buildDir = (os.path.abspath(args.build_location) if args.build_location
else os.path.join(self.workspaceDir, "build", BuildVariant(self)))
# Install directory
self.instDir = (os.path.abspath(args.install_location) if args.install_location
else os.path.join(self.workspaceDir, "install", BuildVariant(self)))
# Forced to be built
self.forceBuild = args.force_clean_build
# CMake generator
self.cmakeGenerator = args.generator
# Number of jobs
self.numJobs = args.jobs
if self.numJobs <= 0:
raise ValueError("Number of jobs must be greater than 0")
# Maya Location
self.mayaLocation = (os.path.abspath(args.maya_location)
if args.maya_location else None)
# PXR USD Location
self.pxrUsdLocation = (os.path.abspath(args.pxrusd_location)
if args.pxrusd_location else None)
# Maya Devkit Location
self.devkitLocation = (os.path.abspath(args.devkit_location)
if args.devkit_location else None)
# Log File Name
logFileName="build_log.txt"
self.logFileLocation=os.path.join(self.buildDir, logFileName)
# Build arguments
self.buildArgs = list()
for args in args.build_args:
argList = args.split(",")
for arg in argList:
self.buildArgs.append(arg)
try:
context = InstallContext(args)
except Exception as e:
PrintError(str(e))
sys.exit(1)
# Summarize
summaryMsg = """
Building with settings:
Source directory {mayaUsdSrcDir}
Workspace directory {workspaceDir}
Build directory {buildDir}
Install directory {instDir}
Variant {buildVariant}
CMake generator {cmakeGenerator}
Build Log {logFileLocation}"""
if context.buildArgs:
summaryMsg += """
Extra Build arguments {buildArgs}"""
summaryMsg = summaryMsg.format(
mayaUsdSrcDir=context.mayaUsdSrcDir,
workspaceDir=context.workspaceDir,
buildDir=context.buildDir,
instDir=context.instDir,
logFileLocation=context.logFileLocation,
buildArgs=context.buildArgs,
buildVariant=BuildVariant(context),
cmakeGenerator=("Default" if not context.cmakeGenerator
else context.cmakeGenerator)
)
Print(summaryMsg)
# Install MayaUSD
InstallMayaUSD(context, context.forceBuild, context.buildArgs)
# Ensure directory structure is created and is writable.
for dir in [context.workspaceDir, context.buildDir, context.instDir]:
try:
if os.path.isdir(dir):
testFile = os.path.join(dir, "canwrite")
open(testFile, "w").close()
os.remove(testFile)
else:
os.makedirs(dir)
except Exception as e:
PrintError("Could not write to directory {dir}. Change permissions "
"or choose a different location to install to."
.format(dir=dir))
sys.exit(1)
Print("""Success Maya USD build and install !!!!""")