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
23import os
24import subprocess
25import sys
26import argparse
27
28def readFile (filename):
29	f = open(filename, 'rb')
30	d = f.read()
31	f.close()
32	return d
33
34def writeFile (filename, data):
35	f = open(filename, 'wb')
36	f.write(data)
37	f.close()
38
39def getHead (gitDir):
40	commit = subprocess.check_output(["git", "--git-dir", gitDir,
41									  "rev-parse", "HEAD"])
42	return commit.decode().strip()
43
44def makeReleaseInfo (name, id, glslname, spirvtoolsname, spirvheadersname):
45	return """
46/* WARNING: auto-generated file, use {genFileName} to modify */
47
48#define DEQP_RELEASE_NAME				"{releaseName}"
49#define DEQP_RELEASE_ID					{releaseId}
50#define DEQP_RELEASE_GLSL_NAME			"{glslReleaseName}"
51#define DEQP_RELEASE_SPIRV_TOOLS_NAME	"{spirvToolsReleaseName}"
52#define DEQP_RELEASE_SPIRV_HEADERS_NAME	"{spirvHeadersReleaseName}"
53
54"""[1:].format(
55		genFileName				= os.path.basename(__file__),
56		releaseName				= name,
57		releaseId				= id,
58		glslReleaseName			= glslname,
59		spirvToolsReleaseName	= spirvtoolsname,
60		spirvHeadersReleaseName	= spirvheadersname)
61
62def parseArgs ():
63	parser = argparse.ArgumentParser(description="Generate release info for build")
64	parser.add_argument('--name', dest='releaseName', default=None, help="Release name")
65	parser.add_argument('--id', dest='releaseId', default=None, help="Release ID (must be C integer literal)")
66	parser.add_argument('--git', dest='git', action='store_true', default=False, help="Development build, use git HEAD to identify")
67	parser.add_argument('--git-dir', dest='gitDir', default=None, help="Use specific git dir for extracting info")
68	parser.add_argument('--glsl-git-dir', dest='glslGitDir', default=None, help="Use specific git dir for extracting glsl version info")
69	parser.add_argument('--glsl-spirvtools-dir', dest='spirvToolsGitDir', default=None, help="Use specific git dir for extracting spirv tools version info")
70	parser.add_argument('--glsl-spirvheaders-dir', dest='spirvHeadersGitDir', default=None, help="Use specific git dir for extracting spirv headers version info")
71	parser.add_argument('--out', dest='out', default=None, help="Output file")
72
73	args = parser.parse_args()
74
75	if (args.releaseName == None) != (args.releaseId == None):
76		print "Both --name and --id must be specified"
77		parser.print_help()
78		sys.exit(-1)
79
80	if (args.releaseName != None) == args.git:
81		print "Either --name and --id, or --git must be specified"
82		parser.print_help()
83		sys.exit(-1)
84
85	return args
86
87if __name__ == "__main__":
88	curDir						= os.path.dirname(__file__)
89	defaultGitDir				= os.path.normpath(os.path.join(curDir, "..", "..", ".git"))
90	defaultGlslGitDir			= os.path.normpath(os.path.join(curDir, "..", "..", "external", "glslang", "src", ".git"))
91	defaultSpirvToolsGitDir		= os.path.normpath(os.path.join(curDir, "..", "..", "external", "spirv-tools", "src", ".git"))
92	defaultSpirvHeadersGitDir	= os.path.normpath(os.path.join(curDir, "..", "..", "external", "spirv-headers", "src", ".git"))
93	defaultDstFile				= os.path.join(curDir, "qpReleaseInfo.inl")
94
95	args = parseArgs()
96
97	if args.git:
98		gitDir				= args.gitDir				if args.gitDir				!= None else defaultGitDir
99		head				= getHead(gitDir)
100		releaseName			= "git-%s" % head
101		releaseId			= "0x%s" % head[0:8]
102	else:
103		releaseName			= args.releaseName
104		releaseId			= args.releaseId
105
106	if args.glslGitDir or os.path.exists(defaultGlslGitDir):
107		glslGitDir			= args.glslGitDir			if args.glslGitDir			!= None else defaultGlslGitDir
108		glslHead			= getHead(glslGitDir)
109		glslName			= "git-%s" % glslHead
110	elif args.git:
111		glslName			= "unknown"
112	else:
113		glslName			= args.releaseName
114
115	if args.spirvToolsGitDir or os.path.exists(defaultSpirvToolsGitDir):
116		spirvToolsGitDir	= args.spirvToolsGitDir		if args.spirvToolsGitDir	!= None else defaultSpirvToolsGitDir
117		spirvToolsHead		= getHead(spirvToolsGitDir)
118		spirvToolsName		= "git-%s" % spirvToolsHead
119	elif args.git:
120		spirvToolsName		= "unknown"
121	else:
122		spirvToolsName		= args.releaseName
123
124	if args.spirvHeadersGitDir or os.path.exists(defaultSpirvHeadersGitDir):
125		spirvHeadersGitDir	= args.spirvHeadersGitDir	if args.spirvHeadersGitDir	!= None else defaultSpirvHeadersGitDir
126		spirvHeadersHead	= getHead(spirvHeadersGitDir)
127		spirvHeadersName	= "git-%s" % spirvHeadersHead
128	elif args.git:
129		spirvHeadersName	= "unknown"
130	else:
131		spirvHeadersName	= args.releaseName
132
133	releaseInfo	= makeReleaseInfo(releaseName, releaseId, glslName, spirvToolsName, spirvHeadersName)
134	dstFile		= args.out if args.out != None else defaultDstFile
135
136	writeFile(dstFile, releaseInfo)
137