Home | History | Annotate | Download | only in build
      1 # -*- coding: utf-8 -*-
      2 
      3 #-------------------------------------------------------------------------
      4 # drawElements Quality Program utilities
      5 # --------------------------------------
      6 #
      7 # Copyright 2015 The Android Open Source Project
      8 #
      9 # Licensed under the Apache License, Version 2.0 (the "License");
     10 # you may not use this file except in compliance with the License.
     11 # You may obtain a copy of the License at
     12 #
     13 #      http://www.apache.org/licenses/LICENSE-2.0
     14 #
     15 # Unless required by applicable law or agreed to in writing, software
     16 # distributed under the License is distributed on an "AS IS" BASIS,
     17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     18 # See the License for the specific language governing permissions and
     19 # limitations under the License.
     20 #
     21 #-------------------------------------------------------------------------
     22 
     23 import os
     24 import sys
     25 import copy
     26 import platform
     27 import multiprocessing
     28 
     29 from common import which, DEQP_DIR
     30 
     31 try:
     32 	import _winreg
     33 except:
     34 	_winreg = None
     35 
     36 class BuildConfig:
     37 	def __init__ (self, buildDir, buildType, args, srcPath = DEQP_DIR):
     38 		self.srcPath		= srcPath
     39 		self.buildDir		= buildDir
     40 		self.buildType		= buildType
     41 		self.args			= copy.copy(args)
     42 
     43 	def getSrcPath (self):
     44 		return self.srcPath
     45 
     46 	def getBuildDir (self):
     47 		return self.buildDir
     48 
     49 	def getBuildType (self):
     50 		return self.buildType
     51 
     52 	def getArgs (self):
     53 		return self.args
     54 
     55 class CMakeGenerator:
     56 	def __init__ (self, name, isMultiConfig = False, extraBuildArgs = []):
     57 		self.name			= name
     58 		self.isMultiConfig	= isMultiConfig
     59 		self.extraBuildArgs	= copy.copy(extraBuildArgs)
     60 
     61 	def getName (self):
     62 		return self.name
     63 
     64 	def getGenerateArgs (self, buildType):
     65 		args = ['-G', self.name]
     66 		if not self.isMultiConfig:
     67 			args.append('-DCMAKE_BUILD_TYPE=%s' % buildType)
     68 		return args
     69 
     70 	def getBuildArgs (self, buildType):
     71 		args = []
     72 		if self.isMultiConfig:
     73 			args += ['--config', buildType]
     74 		if len(self.extraBuildArgs) > 0:
     75 			args += ['--'] + self.extraBuildArgs
     76 		return args
     77 
     78 	def getBinaryPath (self, buildType, basePath):
     79 		return basePath
     80 
     81 class UnixMakefileGenerator(CMakeGenerator):
     82 	def __init__(self):
     83 		CMakeGenerator.__init__(self, "Unix Makefiles", extraBuildArgs = ["-j%d" % multiprocessing.cpu_count()])
     84 
     85 	def isAvailable (self):
     86 		return which('make') != None
     87 
     88 class NMakeGenerator(CMakeGenerator):
     89 	def __init__(self):
     90 		CMakeGenerator.__init__(self, "NMake Makefiles")
     91 
     92 	def isAvailable (self):
     93 		return which('nmake.exe') != None
     94 
     95 class NinjaGenerator(CMakeGenerator):
     96 	def __init__(self):
     97 		CMakeGenerator.__init__(self, "Ninja")
     98 
     99 	def isAvailable (self):
    100 		return which('ninja') != None
    101 
    102 class VSProjectGenerator(CMakeGenerator):
    103 	ARCH_32BIT	= 0
    104 	ARCH_64BIT	= 1
    105 
    106 	def __init__(self, version, arch):
    107 		name = "Visual Studio %d" % version
    108 		if arch == self.ARCH_64BIT:
    109 			name += " Win64"
    110 
    111 		CMakeGenerator.__init__(self, name, isMultiConfig = True, extraBuildArgs = ['/m'])
    112 		self.version		= version
    113 		self.arch			= arch
    114 
    115 	def getBinaryPath (self, buildType, basePath):
    116 		return os.path.join(os.path.dirname(basePath), buildType, os.path.basename(basePath) + ".exe")
    117 
    118 	@staticmethod
    119 	def getNativeArch ():
    120 		arch = platform.machine().lower()
    121 
    122 		if arch == 'x86':
    123 			return VSProjectGenerator.ARCH_32BIT
    124 		elif arch == 'amd64':
    125 			return VSProjectGenerator.ARCH_64BIT
    126 		else:
    127 			raise Exception("Unhandled arch '%s'" % arch)
    128 
    129 	@staticmethod
    130 	def registryKeyAvailable (root, arch, name):
    131 		try:
    132 			key = _winreg.OpenKey(root, name, 0, _winreg.KEY_READ | arch)
    133 			_winreg.CloseKey(key)
    134 			return True
    135 		except:
    136 			return False
    137 
    138 	def isAvailable (self):
    139 		if sys.platform == 'win32' and _winreg != None:
    140 			nativeArch = VSProjectGenerator.getNativeArch()
    141 			if nativeArch == self.ARCH_32BIT and self.arch == self.ARCH_64BIT:
    142 				return False
    143 
    144 			arch = _winreg.KEY_WOW64_32KEY if nativeArch == self.ARCH_64BIT else 0
    145 			keyMap = {
    146 				10:		[(_winreg.HKEY_CLASSES_ROOT, "VisualStudio.DTE.10.0"), (_winreg.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\VCExpress\\10.0")],
    147 				11:		[(_winreg.HKEY_CLASSES_ROOT, "VisualStudio.DTE.11.0"), (_winreg.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\VCExpress\\11.0")],
    148 				12:		[(_winreg.HKEY_CLASSES_ROOT, "VisualStudio.DTE.12.0"), (_winreg.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\VCExpress\\12.0")],
    149 				14:		[(_winreg.HKEY_CLASSES_ROOT, "VisualStudio.DTE.14.0"), (_winreg.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\VCExpress\\14.0")],
    150 			}
    151 
    152 			if not self.version in keyMap:
    153 				raise Exception("Unsupported VS version %d" % self.version)
    154 
    155 			keys = keyMap[self.version]
    156 			for root, name in keys:
    157 				if VSProjectGenerator.registryKeyAvailable(root, arch, name):
    158 					return True
    159 			return False
    160 		else:
    161 			return False
    162 
    163 # Pre-defined generators
    164 
    165 MAKEFILE_GENERATOR		= UnixMakefileGenerator()
    166 NMAKE_GENERATOR			= NMakeGenerator()
    167 NINJA_GENERATOR			= NinjaGenerator()
    168 VS2010_X32_GENERATOR	= VSProjectGenerator(10, VSProjectGenerator.ARCH_32BIT)
    169 VS2010_X64_GENERATOR	= VSProjectGenerator(10, VSProjectGenerator.ARCH_64BIT)
    170 VS2012_X32_GENERATOR	= VSProjectGenerator(11, VSProjectGenerator.ARCH_32BIT)
    171 VS2012_X64_GENERATOR	= VSProjectGenerator(11, VSProjectGenerator.ARCH_64BIT)
    172 VS2013_X32_GENERATOR	= VSProjectGenerator(12, VSProjectGenerator.ARCH_32BIT)
    173 VS2013_X64_GENERATOR	= VSProjectGenerator(12, VSProjectGenerator.ARCH_64BIT)
    174 VS2015_X32_GENERATOR	= VSProjectGenerator(14, VSProjectGenerator.ARCH_32BIT)
    175 VS2015_X64_GENERATOR	= VSProjectGenerator(14, VSProjectGenerator.ARCH_64BIT)
    176 
    177 def selectFirstAvailableGenerator (generators):
    178 	for generator in generators:
    179 		if generator.isAvailable():
    180 			return generator
    181 	return None
    182 
    183 ANY_VS_X32_GENERATOR	= selectFirstAvailableGenerator([
    184 								VS2015_X32_GENERATOR,
    185 								VS2013_X32_GENERATOR,
    186 								VS2012_X32_GENERATOR,
    187 								VS2010_X32_GENERATOR,
    188 							])
    189 ANY_VS_X64_GENERATOR	= selectFirstAvailableGenerator([
    190 								VS2015_X64_GENERATOR,
    191 								VS2013_X64_GENERATOR,
    192 								VS2012_X64_GENERATOR,
    193 								VS2010_X64_GENERATOR,
    194 							])
    195 ANY_UNIX_GENERATOR		= selectFirstAvailableGenerator([
    196 								NINJA_GENERATOR,
    197 								MAKEFILE_GENERATOR,
    198 								NMAKE_GENERATOR,
    199 							])
    200 ANY_GENERATOR			= selectFirstAvailableGenerator([
    201 								VS2015_X64_GENERATOR,
    202 								VS2015_X32_GENERATOR,
    203 								VS2013_X64_GENERATOR,
    204 								VS2012_X64_GENERATOR,
    205 								VS2010_X64_GENERATOR,
    206 								VS2013_X32_GENERATOR,
    207 								VS2012_X32_GENERATOR,
    208 								VS2010_X32_GENERATOR,
    209 								NINJA_GENERATOR,
    210 								MAKEFILE_GENERATOR,
    211 								NMAKE_GENERATOR,
    212 							])
    213