Merge vk-gl-cts/vulkan-cts-1.0.2 into vk-gl-cts/master
[platform/upstream/VK-GL-CTS.git] / scripts / build / common.py
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 shlex
26 import platform
27 import subprocess
28
29 DEQP_DIR = os.path.realpath(os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "..")))
30
31 # HostInfo describes properties of the host where these scripts
32 # are running on.
33 class HostInfo:
34         OS_WINDOWS      = 0
35         OS_LINUX        = 1
36         OS_OSX          = 2
37
38         @staticmethod
39         def getOs ():
40                 if sys.platform == 'darwin':
41                         return HostInfo.OS_OSX
42                 elif sys.platform == 'win32':
43                         return HostInfo.OS_WINDOWS
44                 elif sys.platform.startswith('linux'):
45                         return HostInfo.OS_LINUX
46                 else:
47                         raise Exception("Unknown sys.platform '%s'" % sys.platform)
48
49         @staticmethod
50         def getArchBits ():
51                 MACHINE_BITS = {
52                         "i386":         32,
53                         "i686":         32,
54                         "x86":          32,
55                         "x86_64":       64,
56                         "AMD64":        64
57                 }
58                 machine = platform.machine()
59
60                 if not machine in MACHINE_BITS:
61                         raise Exception("Unknown platform.machine() '%s'" % machine)
62
63                 return MACHINE_BITS[machine]
64
65 def die (msg):
66         print(msg)
67         exit(-1)
68
69 def shellquote(s):
70         return '"%s"' % s.replace('\\', '\\\\').replace('"', '\"').replace('$', '\$').replace('`', '\`')
71
72 g_workDirStack = []
73
74 def pushWorkingDir (path):
75         oldDir = os.getcwd()
76         os.chdir(path)
77         g_workDirStack.append(oldDir)
78
79 def popWorkingDir ():
80         assert len(g_workDirStack) > 0
81         newDir = g_workDirStack[-1]
82         g_workDirStack.pop()
83         os.chdir(newDir)
84
85 def execute (args):
86         retcode = subprocess.call(args)
87         if retcode != 0:
88                 raise Exception("Failed to execute '%s', got %d" % (str(args), retcode))
89
90 def readFile (filename):
91         f = open(filename, 'rb')
92         data = f.read()
93         f.close()
94         return data
95
96 def writeFile (filename, data):
97         f = open(filename, 'wb')
98         f.write(data)
99         f.close()
100
101 def which (binName, paths = None):
102         if paths == None:
103                 paths = os.environ['PATH'].split(os.pathsep)
104
105         def whichImpl (binWithExt):
106                 for path in paths:
107                         path = path.strip('"')
108                         fullPath = os.path.join(path, binWithExt)
109                         if os.path.isfile(fullPath) and os.access(fullPath, os.X_OK):
110                                 return fullPath
111
112                 return None
113
114         extensions = [""]
115         if HostInfo.getOs() == HostInfo.OS_WINDOWS:
116                 extensions += [".exe", ".bat"]
117
118         for extension in extensions:
119                 extResult = whichImpl(binName + extension)
120                 if extResult != None:
121                         return extResult
122
123         return None