Merge branch '289-renderpass-formats' into 'vulkan-cts-1.0'
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / build_spirv_binaries.py
1 # -*- coding: utf-8 -*-
2
3 #-------------------------------------------------------------------------
4 # Vulkan CTS
5 # ----------
6 #
7 # Copyright (c) 2015 Google Inc.
8 #
9 # Permission is hereby granted, free of charge, to any person obtaining a
10 # copy of this software and/or associated documentation files (the
11 # "Materials"), to deal in the Materials without restriction, including
12 # without limitation the rights to use, copy, modify, merge, publish,
13 # distribute, sublicense, and/or sell copies of the Materials, and to
14 # permit persons to whom the Materials are furnished to do so, subject to
15 # the following conditions:
16 #
17 # The above copyright notice(s) and this permission notice shall be
18 # included in all copies or substantial portions of the Materials.
19 #
20 # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
27 #
28 #-------------------------------------------------------------------------
29
30 import os
31 import sys
32 import string
33 import argparse
34 import tempfile
35 import shutil
36 import fnmatch
37
38 sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "scripts"))
39
40 from build.common import *
41 from build.config import *
42 from build.build import *
43
44 class Module:
45         def __init__ (self, name, dirName, binName):
46                 self.name               = name
47                 self.dirName    = dirName
48                 self.binName    = binName
49
50 VULKAN_MODULE           = Module("dEQP-VK", "../external/vulkancts/modules/vulkan", "deqp-vk")
51 DEFAULT_BUILD_DIR       = os.path.join(tempfile.gettempdir(), "spirv-binaries", "{targetName}-{buildType}")
52 DEFAULT_TARGET          = "null"
53 DEFAULT_DST_DIR         = os.path.join(DEQP_DIR, "external", "vulkancts", "data", "vulkan", "prebuilt")
54
55 def getBuildConfig (buildPathPtrn, targetName, buildType):
56         buildPath = buildPathPtrn.format(
57                 targetName      = targetName,
58                 buildType       = buildType)
59
60         return BuildConfig(buildPath, buildType, ["-DDEQP_TARGET=%s" % targetName])
61
62 def cleanDstDir (dstPath):
63         binFiles = [f for f in os.listdir(dstPath) if os.path.isfile(os.path.join(dstPath, f)) and fnmatch.fnmatch(f, "*.spirv")]
64
65         for binFile in binFiles:
66                 print "Removing %s" % os.path.join(dstPath, binFile)
67                 os.remove(os.path.join(dstPath, binFile))
68
69 def execBuildPrograms (buildCfg, generator, module, mode, dstPath):
70         workDir = os.path.join(buildCfg.getBuildDir(), "modules", module.dirName)
71
72         pushWorkingDir(workDir)
73
74         try:
75                 binPath = generator.getBinaryPath(buildCfg.getBuildType(), os.path.join(".", "vk-build-programs"))
76                 execute([binPath, "--mode", mode, "--dst-path", dstPath])
77         finally:
78                 popWorkingDir()
79
80 def parseArgs ():
81         parser = argparse.ArgumentParser(description = "Build SPIR-V programs",
82                                                                          formatter_class=argparse.ArgumentDefaultsHelpFormatter)
83         parser.add_argument("-b",
84                                                 "--build-dir",
85                                                 dest="buildDir",
86                                                 default=DEFAULT_BUILD_DIR,
87                                                 help="Temporary build directory")
88         parser.add_argument("-t",
89                                                 "--build-type",
90                                                 dest="buildType",
91                                                 default="Debug",
92                                                 help="Build type")
93         parser.add_argument("-c",
94                                                 "--deqp-target",
95                                                 dest="targetName",
96                                                 default=DEFAULT_TARGET,
97                                                 help="dEQP build target")
98         parser.add_argument("--mode",
99                                                 dest="mode",
100                                                 default="build",
101                                                 help="Build mode (build or verify)")
102         parser.add_argument("-d",
103                                                 "--dst-path",
104                                                 dest="dstPath",
105                                                 default=DEFAULT_DST_DIR,
106                                                 help="Destination path")
107         return parser.parse_args()
108
109 if __name__ == "__main__":
110         args = parseArgs()
111
112         generator       = ANY_GENERATOR
113         buildCfg        = getBuildConfig(args.buildDir, args.targetName, args.buildType)
114         module          = VULKAN_MODULE
115
116         build(buildCfg, generator, ["vk-build-programs"])
117
118         if args.mode == "build":
119                 if os.path.exists(args.dstPath):
120                         cleanDstDir(args.dstPath)
121                 else:
122                         os.makedirs(args.dstPath)
123
124         execBuildPrograms(buildCfg, generator, module, args.mode, args.dstPath)