# # Copyright 2019 Pixar # # Licensed under the Apache License, Version 2.0 (the "Apache License") # with the following modification; you may not use this file except in # compliance with the Apache License and the following modification to it: # Section 6. Trademarks. is deleted and replaced with: # # 6. Trademarks. This License does not grant permission to use the trade # names, trademarks, service marks, or product names of the Licensor # and its affiliates, except as required to comply with Section 4(c) of # the License and to reproduce the content of the NOTICE file. # # You may obtain a copy of the Apache License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the Apache License with the above modification is # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the Apache License for the specific # language governing permissions and limitations under the Apache License. # from distutils.spawn import find_executable import argparse import contextlib import datetime import distutils import fnmatch import glob import multiprocessing import os import platform import re import shlex import shutil import subprocess import sys import tarfile import urllib2 import zipfile # 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): print "ERROR:", error # Helpers for determining platform 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 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("log.txt", "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("log.txt", "r") as logfile: Print(logfile.read()) raise RuntimeError("Failed to run '{cmd}'\nSee {log} for more details." .format(cmd=cmd, log=os.path.abspath("log.txt"))) @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) def CopyFiles(context, src, dest): """Copy files like shutil.copy, but src may be a glob pattern.""" filesToCopy = glob.glob(src) if not filesToCopy: raise RuntimeError("File(s) to copy {src} not found".format(src=src)) instDestDir = os.path.join(context.instDir, dest) if not os.path.isdir(instDestDir): os.makedirs(instDestDir) for f in filesToCopy: PrintCommandOutput("Copying {file} to {destDir}\n" .format(file=f, destDir=instDestDir)) shutil.copy(f, instDestDir) def CopyDirectory(context, srcDir, destDir): """Copy directory like shutil.copytree.""" instDestDir = os.path.join(context.instDir, destDir) if os.path.isdir(instDestDir): shutil.rmtree(instDestDir) PrintCommandOutput("Copying {srcDir} to {destDir}\n" .format(srcDir=srcDir, destDir=instDestDir)) shutil.copytree(srcDir, instDestDir) 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.""" # Create a directory for out-of-source builds in the build directory # using the name of the current working directory. srcDir = os.getcwd() instDir = (context.osdInstDir if srcDir == context.osdSrcDir else context.instDir) buildDir = os.path.join(context.buildDir, os.path.split(srcDir)[1]) if force and os.path.isdir(buildDir): shutil.rmtree(buildDir) 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?) if generator is None and Windows(): if IsVisualStudio2017OrGreater(): generator = "Visual Studio 15 2017 Win64" else: generator = "Visual Studio 14 2015 Win64" # On macOS default to Xcode if generator is None and MacOS(): generator = "Xcode" multiproc = "-j{procs}" if Windows(): multiproc = "/M:{procs}" if generator == "Xcode": multiproc = "-jobs {procs} -parallelizeTargets" multiproc = multiproc.format(procs=context.numJobs) # On MacOS, enable the use of @rpath for relocatable builds. osx_rpath = None if MacOS(): osx_rpath = "-DCMAKE_MACOSX_RPATH=ON" # Format generator appropriately if generator is not None: generator = '-G "{gen}"'.format(gen=generator) with CurrentWorkingDirectory(buildDir): Run('cmake ' '-DCMAKE_INSTALL_PREFIX="{instDir}" ' '-DCMAKE_PREFIX_PATH="{depsInstDir}" ' '{osx_rpath} ' '{generator} ' '{extraArgs} ' '"{srcDir}"' .format(instDir=instDir, depsInstDir=context.instDir, srcDir=srcDir, osx_rpath=(osx_rpath or ""), generator=(generator or ""), extraArgs=(" ".join(extraArgs) if extraArgs else ""))) Run("cmake --build . --config Release --target install -- {jobs}" .format(jobs=multiproc)) def PatchFile(filename, patches): """Applies patches to the specified file. patches is a list of tuples (old string, new string).""" oldLines = open(filename, 'r').readlines() newLines = oldLines for (oldLine, newLine) in patches: newLines = [s.replace(oldLine, newLine) for s in newLines] if newLines != oldLines: PrintInfo("Patching file {filename} (original in {oldFilename})..." .format(filename=filename, oldFilename=filename + ".old")) shutil.copy(filename, filename + ".old") open(filename, 'w').writelines(newLines) def DownloadFileWithCurl(url, outputFilename): # Don't log command output so that curl's progress # meter doesn't get written to the log file. Run("curl {progress} -L -o {filename} {url}".format( progress="-#" if verbosity >= 2 else "-s", filename=outputFilename, url=url), logCommandOutput=False) def DownloadFileWithPowershell(url, outputFilename): # It's important that we specify to use TLS v1.2 at least or some # of the downloads will fail. cmd = "powershell [Net.ServicePointManager]::SecurityProtocol = \ [Net.SecurityProtocolType]::Tls12; \"(new-object \ System.Net.WebClient).DownloadFile('{url}', '{filename}')\""\ .format(filename=outputFilename, url=url) Run(cmd,logCommandOutput=False) def DownloadFileWithUrllib(url, outputFilename): r = urllib2.urlopen(url) with open(outputFilename, "wb") as outfile: outfile.write(r.read()) def DownloadURL(url, context, force, dontExtract = None): """Download and extract the archive file at given URL to the source directory specified in the context. dontExtract may be a sequence of path prefixes that will be excluded when extracting the archive. Returns the absolute path to the directory where files have been extracted.""" with CurrentWorkingDirectory(context.srcDir): # Extract filename from URL and see if file already exists. filename = url.split("/")[-1] if force and os.path.exists(filename): os.remove(filename) if os.path.exists(filename): PrintInfo("{0} already exists, skipping download" .format(os.path.abspath(filename))) else: PrintInfo("Downloading {0} to {1}" .format(url, os.path.abspath(filename))) # To work around occasional hiccups with downloading from websites # (SSL validation errors, etc.), retry a few times if we don't # succeed in downloading the file. maxRetries = 5 lastError = None # Download to a temporary file and rename it to the expected # filename when complete. This ensures that incomplete downloads # will be retried if the script is run again. tmpFilename = filename + ".tmp" if os.path.exists(tmpFilename): os.remove(tmpFilename) for i in xrange(maxRetries): try: context.downloader(url, tmpFilename) break except Exception as e: PrintCommandOutput("Retrying download due to error: {err}\n" .format(err=e)) lastError = e else: errorMsg = str(lastError) if "SSL: TLSV1_ALERT_PROTOCOL_VERSION" in errorMsg: errorMsg += ("\n\n" "Your OS or version of Python may not support " "TLS v1.2+, which is required for downloading " "files from certain websites. This support " "was added in Python 2.7.9." "\n\n" "You can use curl to download dependencies " "by installing it in your PATH and re-running " "this script.") raise RuntimeError("Failed to download {url}: {err}" .format(url=url, err=errorMsg)) shutil.move(tmpFilename, filename) # Open the archive and retrieve the name of the top-most directory. # This assumes the archive contains a single directory with all # of the contents beneath it. archive = None rootDir = None members = None try: if tarfile.is_tarfile(filename): archive = tarfile.open(filename) rootDir = archive.getnames()[0].split('/')[0] if dontExtract != None: members = (m for m in archive.getmembers() if not any((fnmatch.fnmatch(m.name, p) for p in dontExtract))) elif zipfile.is_zipfile(filename): archive = zipfile.ZipFile(filename) rootDir = archive.namelist()[0].split('/')[0] if dontExtract != None: members = (m for m in archive.getnames() if not any((fnmatch.fnmatch(m, p) for p in dontExtract))) else: raise RuntimeError("unrecognized archive file type") with archive: extractedPath = os.path.abspath(rootDir) if force and os.path.isdir(extractedPath): shutil.rmtree(extractedPath) if os.path.isdir(extractedPath): PrintInfo("Directory {0} already exists, skipping extract" .format(extractedPath)) else: PrintInfo("Extracting archive to {0}".format(extractedPath)) # Extract to a temporary directory then move the contents # to the expected location when complete. This ensures that # incomplete extracts will be retried if the script is run # again. tmpExtractedPath = os.path.abspath("extract_dir") if os.path.isdir(tmpExtractedPath): shutil.rmtree(tmpExtractedPath) archive.extractall(tmpExtractedPath, members=members) shutil.move(os.path.join(tmpExtractedPath, rootDir), extractedPath) shutil.rmtree(tmpExtractedPath) return extractedPath except Exception as e: # If extraction failed for whatever reason, assume the # archive file was bad and move it aside so that re-running # the script will try downloading and extracting again. shutil.move(filename, filename + ".bad") raise RuntimeError("Failed to extract archive {filename}: {err}" .format(filename=filename, err=e)) ############################################################ # 3rd-Party Dependencies AllDependencies = list() AllDependenciesByName = dict() class Dependency(object): def __init__(self, name, installer, *files): self.name = name self.installer = installer self.filesToCheck = files AllDependencies.append(self) AllDependenciesByName.setdefault(name.lower(), self) def Exists(self, context): return all([os.path.isfile(os.path.join(context.instDir, f)) for f in self.filesToCheck]) ############################################################ # Intel TBB if Windows(): TBB_URL = "https://github.com/01org/tbb/releases/download/2017_U5/tbb2017_20170226oss_win.zip" elif MacOS(): TBB_URL = "https://github.com/01org/tbb/archive/2017_U2.tar.gz" else: TBB_URL = "https://github.com/01org/tbb/archive/4.4.6.tar.gz" def InstallTBB(context, force, buildArgs): if Windows(): InstallTBB_Windows(context, force, buildArgs) elif Linux() or MacOS(): InstallTBB_LinuxOrMacOS(context, force, buildArgs) def InstallTBB_Windows(context, force, buildArgs): with CurrentWorkingDirectory(DownloadURL(TBB_URL, context, force)): # On Windows, we simply copy headers and pre-built DLLs to # the appropriate location. if buildArgs: PrintWarning("Ignoring build arguments {}, TBB is " "not built from source on this platform." .format(buildArgs)) CopyFiles(context, "bin\\intel64\\vc14\\*.*", "bin") CopyFiles(context, "lib\\intel64\\vc14\\*.*", "lib") CopyDirectory(context, "include\\serial", "include\\serial") CopyDirectory(context, "include\\tbb", "include\\tbb") def InstallTBB_LinuxOrMacOS(context, force, buildArgs): with CurrentWorkingDirectory(DownloadURL(TBB_URL, context, force)): # TBB does not support out-of-source builds in a custom location. Run('make -j{procs} {buildArgs}' .format(procs=context.numJobs, buildArgs=" ".join(buildArgs))) CopyFiles(context, "build/*_release/libtbb*.*", "lib") CopyDirectory(context, "include/serial", "include/serial") CopyDirectory(context, "include/tbb", "include/tbb") TBB = Dependency("TBB", InstallTBB, "include/tbb/tbb.h") ############################################################ # GLEW if Windows(): GLEW_URL = "https://downloads.sourceforge.net/project/glew/glew/2.0.0/glew-2.0.0-win32.zip" else: # Important to get source package from this URL and NOT github. This package # contains pre-generated code that the github repo does not. GLEW_URL = "https://downloads.sourceforge.net/project/glew/glew/2.0.0/glew-2.0.0.tgz" def InstallGLEW(context, force, buildArgs): if Windows(): InstallGLEW_Windows(context, force) elif Linux() or MacOS(): InstallGLEW_LinuxOrMacOS(context, force, buildArgs) def InstallGLEW_Windows(context, force): with CurrentWorkingDirectory(DownloadURL(GLEW_URL, context, force)): # On Windows, we install headers and pre-built binaries per # https://glew.sourceforge.net/install.html # Note that we are installing just the shared library. This is required # by the OpenSubdiv build; if the static library is present, that one # will be used and that causes errors with OpenSubdiv. CopyFiles(context, "bin\\Release\\x64\\glew32.dll", "bin") CopyFiles(context, "lib\\Release\\x64\\glew32.lib", "lib") CopyDirectory(context, "include\\GL", "include\\GL") def InstallGLEW_LinuxOrMacOS(context, force, buildArgs): with CurrentWorkingDirectory(DownloadURL(GLEW_URL, context, force)): Run('make GLEW_DEST="{instDir}" -j{procs} {buildArgs} install' .format(instDir=context.instDir, procs=context.numJobs, buildArgs=" ".join(buildArgs))) GLEW = Dependency("GLEW", InstallGLEW, "include/GL/glew.h") ############################################################ # GLFW GLFW_URL = "https://github.com/glfw/glfw/archive/3.2.1.zip" def InstallGLFW(context, force, buildArgs): with CurrentWorkingDirectory(DownloadURL(GLFW_URL, context, force)): RunCMake(context, force, buildArgs) GLFW = Dependency("GLFW", InstallGLFW, "include/GLFW/glfw3.h") ############################################################ # zlib ZLIB_URL = "https://github.com/madler/zlib/archive/v1.2.11.zip" def InstallZlib(context, force, buildArgs): with CurrentWorkingDirectory(DownloadURL(ZLIB_URL, context, force)): RunCMake(context, force, buildArgs) ZLIB = Dependency("zlib", InstallZlib, "include/zlib.h") ############################################################ # Ptex PTEX_URL = "https://github.com/wdas/ptex/archive/v2.1.28.zip" def InstallPtex(context, force, buildArgs): if Windows(): InstallPtex_Windows(context, force, buildArgs) else: InstallPtex_LinuxOrMacOS(context, force, buildArgs) def InstallPtex_Windows(context, force, buildArgs): with CurrentWorkingDirectory(DownloadURL(PTEX_URL, context, force)): # Ptex has a bug where the import library for the dynamic library and # the static library both get the same name, Ptex.lib, and as a # result one clobbers the other. We hack the appropriate CMake # file to prevent that. Since we don't need the static library we'll # rename that. # # In addition src\tests\CMakeLists.txt adds -DPTEX_STATIC to the # compiler but links tests against the dynamic library, causing the # links to fail. We patch the file to not add the -DPTEX_STATIC PatchFile('src\\ptex\\CMakeLists.txt', [("set_target_properties(Ptex_static PROPERTIES OUTPUT_NAME Ptex)", "set_target_properties(Ptex_static PROPERTIES OUTPUT_NAME Ptexs)")]) PatchFile('src\\tests\\CMakeLists.txt', [("add_definitions(-DPTEX_STATIC)", "# add_definitions(-DPTEX_STATIC)")]) RunCMake(context, force, buildArgs) def InstallPtex_LinuxOrMacOS(context, force, buildArgs): with CurrentWorkingDirectory(DownloadURL(PTEX_URL, context, force)): RunCMake(context, force, buildArgs) PTEX = Dependency("Ptex", InstallPtex, "include/PtexVersion.h") ############################################################ # OpenSubdiv def InstallOpenSubdiv(context, force, buildArgs): with CurrentWorkingDirectory(context.osdSrcDir): extraArgs = [] if context.buildDocs: extraArgs.append('-DNO_DOC=OFF') else: extraArgs.append('-DNO_DOC=ON') if context.buildPtex: extraArgs.append('-DNO_PTEX=OFF') else: extraArgs.append('-DNO_PTEX=ON') if context.buildTBB: extraArgs.append('-DNO_TBB=OFF') else: extraArgs.append('-DNO_TBB=ON') if context.buildTests: extraArgs.append('-DNO_REGRESSION=OFF') extraArgs.append('-DNO_TESTS=OFF') else: extraArgs.append('-DNO_REGRESSION=ON') extraArgs.append('-DNO_TESTS=ON') if context.buildExamples: extraArgs.append('-DNO_EXAMPLES=OFF') else: extraArgs.append('-DNO_EXAMPLES=ON') if context.buildTutorials: extraArgs.append('-DNO_TUTORIALS=OFF') else: extraArgs.append('-DNO_TUTORIALS=ON') if context.buildOMP: extraArgs.append('-DNO_OMP=OFF') else: extraArgs.append('-DNO_OMP=ON') if context.buildCUDA: extraArgs.append('-DNO_CUDA=OFF') if context.cudaLocation: extraArgs.append('-DCUDA_TOOLKIT_ROOT_DIR="{location}"' .format(location=context.cudaLocation)) else: extraArgs.append('-DNO_CUDA=ON') if context.buildOpenCL: extraArgs.append('-DNO_OPENCL=OFF') else: extraArgs.append('-DNO_OPENCL=ON') if context.buildDX: extraArgs.append('-DNO_DX=OFF') else: extraArgs.append('-DNO_DX=ON') # Options we haven't yet exposed: # NO_CLEW # NO_OPENGL # NO_METAL # NO_GLTESTS # NO_GLFW # NO_GLFW_X11 # OpenSubdiv's FindGLEW module won't look in CMAKE_PREFIX_PATH, so # we need to explicitly specify GLEW_LOCATION here. extraArgs.append('-DGLEW_LOCATION="{instDir}"' .format(instDir=context.instDir)) extraArgs.append('-DGLFW_LOCATION="{instDir}"' .format(instDir=context.instDir)) # Add on any user-specified extra arguments. extraArgs += buildArgs RunCMake(context, force, extraArgs) OPENSUBDIV = Dependency("OpenSubdiv", InstallOpenSubdiv, "include/opensubdiv/version.h") ############################################################ # Install script programDescription = """\ Installation Script for OpenSubdiv Builds and installs OpenSubidv and 3rd-party dependencies to specified location. - Libraries: The following is a list of libraries that this script will download and build as needed. These names can be used to identify libraries for various script options, like --force or --build-args. {libraryList} - Downloading Libraries: If curl or powershell (on Windows) are installed and located in PATH, they will be used to download dependencies. Otherwise, a built-in downloader will be used. - Specifying Custom Build Arguments: Users may specify custom build arguments for libraries using the --build-args option. This values for this option must take the form ,