Home | History | Annotate | Download | only in scripts
      1 # -*- coding: utf-8 -*-
      2 
      3 #-------------------------------------------------------------------------
      4 # drawElements Quality Program utilities
      5 # --------------------------------------
      6 #
      7 # Copyright 2016 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 argparse
     25 import tempfile
     26 
     27 from build.common import *
     28 from build.build import *
     29 
     30 class Environment:
     31 	def __init__ (self, srcDir, tmpDir):
     32 		self.srcDir	= srcDir
     33 		self.tmpDir	= tmpDir
     34 
     35 class BuildTestStep:
     36 	def getName (self):
     37 		return "<unknown>"
     38 
     39 	def isAvailable (self, env):
     40 		return True
     41 
     42 	def run (self, env):
     43 		raise Exception("Not implemented")
     44 
     45 class RunScript(BuildTestStep):
     46 	def __init__ (self, scriptPath, getExtraArgs = None):
     47 		self.scriptPath		= scriptPath
     48 		self.getExtraArgs	= getExtraArgs
     49 
     50 	def getName (self):
     51 		return self.scriptPath
     52 
     53 	def run (self, env):
     54 		args = ["python", os.path.join(env.srcDir, self.scriptPath)]
     55 
     56 		if self.getExtraArgs != None:
     57 			args += self.getExtraArgs(env)
     58 
     59 		execute(args)
     60 
     61 def makeCflagsArgs (cflags):
     62 	cflagsStr = " ".join(cflags)
     63 	return ["-DCMAKE_C_FLAGS=%s" % cflagsStr, "-DCMAKE_CXX_FLAGS=%s" % cflagsStr]
     64 
     65 def makeBuildArgs (target, cc, cpp, cflags):
     66 	return ["-DDEQP_TARGET=%s" % target, "-DCMAKE_C_COMPILER=%s" % cc, "-DCMAKE_CXX_COMPILER=%s" % cpp] + makeCflagsArgs(cflags)
     67 
     68 class BuildConfigGen:
     69 	def isAvailable (self, env):
     70 		return True
     71 
     72 class UnixConfig(BuildConfigGen):
     73 	def __init__ (self, target, buildType, cc, cpp, cflags):
     74 		self.target		= target
     75 		self.buildType	= buildType
     76 		self.cc			= cc
     77 		self.cpp		= cpp
     78 		self.cflags		= cflags
     79 
     80 	def isAvailable (self, env):
     81 		return which(self.cc) != None and which(self.cpp) != None
     82 
     83 	def getBuildConfig (self, env, buildDir):
     84 		args = makeBuildArgs(self.target, self.cc, self.cpp, self.cflags)
     85 		return BuildConfig(buildDir, self.buildType, args, env.srcDir)
     86 
     87 class VSConfig(BuildConfigGen):
     88 	def __init__ (self, buildType):
     89 		self.buildType = buildType
     90 
     91 	def getBuildConfig (self, env, buildDir):
     92 		args = ["-DCMAKE_C_FLAGS=/WX -DCMAKE_CXX_FLAGS=/WX"]
     93 		return BuildConfig(buildDir, self.buildType, args, env.srcDir)
     94 
     95 class Build(BuildTestStep):
     96 	def __init__ (self, buildDir, configGen, generator):
     97 		self.buildDir	= buildDir
     98 		self.configGen	= configGen
     99 		self.generator	= generator
    100 
    101 	def getName (self):
    102 		return self.buildDir
    103 
    104 	def isAvailable (self, env):
    105 		return self.configGen.isAvailable(env) and self.generator != None and self.generator.isAvailable()
    106 
    107 	def run (self, env):
    108 		# specialize config for env
    109 		buildDir	= os.path.join(env.tmpDir, self.buildDir)
    110 		curConfig	= self.configGen.getBuildConfig(env, buildDir)
    111 
    112 		build(curConfig, self.generator)
    113 
    114 class CheckSrcChanges(BuildTestStep):
    115 	def getName (self):
    116 		return "check for changes"
    117 
    118 	def run (self, env):
    119 		pushWorkingDir(env.srcDir)
    120 		execute(["git", "diff", "--exit-code"])
    121 		popWorkingDir()
    122 
    123 def getClangVersion ():
    124 	knownVersions = ["4.0", "3.9", "3.8", "3.7", "3.6", "3.5"]
    125 	for version in knownVersions:
    126 		if which("clang-" + version) != None:
    127 			return "-" + version
    128 	return ""
    129 
    130 def runSteps (steps):
    131 	for step in steps:
    132 		if step.isAvailable(env):
    133 			print "Run: %s" % step.getName()
    134 			step.run(env)
    135 		else:
    136 			print "Skip: %s" % step.getName()
    137 
    138 COMMON_GCC_CFLAGS	= ["-Werror"]
    139 COMMON_CLANG_CFLAGS	= COMMON_GCC_CFLAGS + ["-Wno-error=unused-command-line-argument"]
    140 GCC_32BIT_CFLAGS	= COMMON_GCC_CFLAGS + ["-m32"]
    141 CLANG_32BIT_CFLAGS	= COMMON_CLANG_CFLAGS + ["-m32"]
    142 GCC_64BIT_CFLAGS	= COMMON_GCC_CFLAGS + ["-m64"]
    143 CLANG_64BIT_CFLAGS	= COMMON_CLANG_CFLAGS + ["-m64"]
    144 CLANG_VERSION		= getClangVersion()
    145 
    146 # Always ran before any receipe
    147 PREREQUISITES		= [
    148 	RunScript(os.path.join("external", "fetch_sources.py"))
    149 ]
    150 
    151 # Always ran after any receipe
    152 POST_CHECKS			= [
    153 	CheckSrcChanges()
    154 ]
    155 
    156 BUILD_TARGETS		= [
    157 	Build("clang-64-debug",
    158 		  UnixConfig("null",
    159 					 "Debug",
    160 					 "clang" + CLANG_VERSION,
    161 					 "clang++" + CLANG_VERSION,
    162 					 CLANG_64BIT_CFLAGS),
    163 		  ANY_UNIX_GENERATOR),
    164 	Build("gcc-32-debug",
    165 		  UnixConfig("null",
    166 					 "Debug",
    167 					 "gcc",
    168 					 "g++",
    169 					 GCC_32BIT_CFLAGS),
    170 		  ANY_UNIX_GENERATOR),
    171 	Build("gcc-64-release",
    172 		  UnixConfig("null",
    173 					 "Release",
    174 					 "gcc",
    175 					 "g++",
    176 					 GCC_64BIT_CFLAGS),
    177 		  ANY_UNIX_GENERATOR),
    178 	Build("vs-64-debug",
    179 		  VSConfig("Debug"),
    180 		  ANY_VS_X64_GENERATOR),
    181 ]
    182 
    183 SPECIAL_RECIPES		= [
    184 	('android-mustpass', [
    185 			RunScript(os.path.join("scripts", "build_android_mustpass.py"),
    186 					  lambda env: ["--build-dir", os.path.join(env.tmpDir, "android-mustpass")]),
    187 		]),
    188 	('vulkan-mustpass', [
    189 			RunScript(os.path.join("external", "vulkancts", "scripts", "build_mustpass.py"),
    190 					  lambda env: ["--build-dir", os.path.join(env.tmpDir, "vulkan-mustpass")]),
    191 		]),
    192 	('gen-inl-files', [
    193 			RunScript(os.path.join("scripts", "gen_egl.py")),
    194 			RunScript(os.path.join("scripts", "opengl", "gen_all.py")),
    195 			RunScript(os.path.join("external", "vulkancts", "scripts", "gen_framework.py")),
    196 			RunScript(os.path.join("scripts", "src_util", "check_all.py")),
    197 		])
    198 ]
    199 
    200 def getBuildRecipes ():
    201 	return [(b.getName(), [b]) for b in BUILD_TARGETS]
    202 
    203 def getAllRecipe (recipes):
    204 	allSteps = []
    205 	for name, steps in recipes:
    206 		allSteps += steps
    207 	return ("all", allSteps)
    208 
    209 def getRecipes ():
    210 	recipes = getBuildRecipes()
    211 	recipes += SPECIAL_RECIPES
    212 	return recipes
    213 
    214 def getRecipe (recipes, recipeName):
    215 	for curName, steps in recipes:
    216 		if curName == recipeName:
    217 			return (curName, steps)
    218 	return None
    219 
    220 RECIPES			= getRecipes()
    221 
    222 def parseArgs ():
    223 	parser = argparse.ArgumentParser(description = "Build and test source",
    224 									 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    225 	parser.add_argument("-s",
    226 						"--src-dir",
    227 						dest="srcDir",
    228 						default=DEQP_DIR,
    229 						help="Source directory")
    230 	parser.add_argument("-t",
    231 						"--tmp-dir",
    232 						dest="tmpDir",
    233 						default=os.path.join(tempfile.gettempdir(), "deqp-build-test"),
    234 						help="Temporary directory")
    235 	parser.add_argument("-r",
    236 						"--recipe",
    237 						dest="recipe",
    238 						choices=[n for n, s in RECIPES] + ["all"],
    239 						default="all",
    240 						help="Build / test recipe")
    241 	parser.add_argument("-d",
    242 						"--dump-recipes",
    243 						dest="dumpRecipes",
    244 						action="store_true",
    245 						help="Print out recipes that have any available actions")
    246 	parser.add_argument("--skip-prerequisites",
    247 						dest="skipPrerequisites",
    248 						action="store_true",
    249 						help="Skip external dependency fetch")
    250 
    251 	return parser.parse_args()
    252 
    253 if __name__ == "__main__":
    254 	args	= parseArgs()
    255 	env		= Environment(args.srcDir, args.tmpDir)
    256 
    257 	if args.dumpRecipes:
    258 		for name, steps in RECIPES:
    259 			for step in steps:
    260 				if step.isAvailable(env):
    261 					print name
    262 					break
    263 	else:
    264 		name, steps	= getAllRecipe(RECIPES) if args.recipe == "all" \
    265 					  else getRecipe(RECIPES, args.recipe)
    266 
    267 		print "Running %s" % name
    268 
    269 		allSteps = (PREREQUISITES if (args.skipPrerequisites == False) else []) + steps + POST_CHECKS
    270 		runSteps(allSteps)
    271 
    272 		print "All steps completed successfully"
    273