Debian Codegraph @

Tools/MediaDriverTools/Android/GenerateAndroidMk.py

raw
1 #! /usr/bin/env python3
2 """
3 * Copyright (c) 2018, Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 """
23
24 """
25 the basic idea from this script are:
26 1. generate linux make file using cmake
27 2. get source files using "grep -ir \\.o:$ Makefile |sed s/.c.o:/.c/ |sed s/.cpp.o:/.cpp/ | sed s,^,\\t, | sed s/$/' \\'/"
28 3. get c/cpp defines using "grep CXX_DEFINES flags.make | sed s/"CXX_DEFINES = "/""/g | sed s/" "/"\n"/g | sed s/"^"/"\t"/g | sed 's/$/ \\/'"
29 4. get includes directories using "grep CXX_INCLUDES flags.make | sed s/"CXX_DEFINES = "/""/g | sed s/" "/"\n"/g | sed s/"^"/"\t"/g | sed 's/$/ \\/'"
30 5. replace related text in template
31 """
32
33 import os
34 import os.path as path
35 import re
36
37 INDENT = " "
38 TOOL_DIR = "Tools/MediaDriverTools/Android/"
39 TEMPLATE_DIR = path.join(TOOL_DIR, "mk")
40
41
42 def remove(f):
43 cmd = "rm " + f + "> /dev/null 2>&1"
44 os.system(cmd)
45
46 def getDriverName(root):
47 driver = "mod"
48 if not path.exists(path.join(root, driver)):
49 driver = "media-driver"
50 if not path.exists(path.join(root, driver)):
51 raise Exception("driver path " + driver +" not existed")
52 return driver
53
54 class Generator:
55 def __init__(self, src, root, makefile=None):
56 #where to put the Android makefile
57 self.makefile = makefile if makefile else src
58
59 #where is the major source file
60 self.src = src
61
62 #driver name
63 driver = getDriverName(root)
64 #where to put build file and template
65 self.tool = path.join(root, driver, TOOL_DIR)
66
67 """where to put the template"""
68 self.templ = path.join(root, driver, TEMPLATE_DIR)
69
70 #major function
71 def generate(self):
72
73 if path.exists(self.src) == False:
74 raise Exception(self.src + "not existed")
75 self.generateMakefile()
76
77 mk = path.join(self.makefile, "Android.mk")
78 #remove old Android.mk
79 remove(mk)
80
81 #create new Android.mk
82 with open(self.getTemplatePath()) as f:
83 tpl = f.read()
84 tpl = tpl.replace("@LOCAL_SRC_FILES", self.getSources())
85 tpl = tpl.replace("@LOCAL_CFLAGS", self.getDefines("CXX_DEFINES"))
86 tpl = tpl.replace("@LOCAL_C_INCLUDES", self.getIncludes())
87 with open(mk, "w") as f:
88 f.write(tpl)
89 print("generated " + self.getName() + " to " + self.makefile)
90
91 #virtuall functions
92 def getTemplate(self):
93 raise Exception("pure virtul function")
94
95 def getFlagsfile(self):
96 raise Exception("pure virtul function")
97
98 def getMakefile(self):
99 return "Makefile"
100
101 def getName(self):
102 return self.getTemplate().split('.')[0]
103
104 def getTemplatePath(self):
105 return path.join(self.templ, self.getTemplate())
106
107 def getBuildDir(self):
108 return path.join(self.tool, 'build', self.getName())
109
110 def adjustSources(self, lines):
111 #print(lines)
112 return lines
113
114 def adjustIncludes(self, lines):
115 #print(lines)
116 return lines
117
118 def getCmakeCmd(self):
119 return "cmake " + self.src
120
121 def generateMakefile(self, debug=False):
122 #Win env can help us debug the script
123 #but we do not want generate makefile on Win-system
124 if os.name == "nt":
125 return
126 verbose = ";" if debug else "> /dev/null 2>&1;"
127 build = self.getBuildDir()
128 cmd = "rm " + path.join(build, "CMakeCache.txt") + verbose
129 cmd += "mkdir -p " + build + verbose
130 cmd += "cd " + build
131 cmd += "&& " + self.getCmakeCmd() + verbose
132 os.system(cmd)
133
134 def getIncludes(self):
135 text = self.getDefines("CXX_INCLUDES")
136 includes = []
137 lines = text.split("\n")
138 for l in lines:
139 #normpath will make sure we did not refer outside.
140 p = path.normpath(l)
141 j = p.find(self.src)
142 if j != -1:
143 includes.append(p[j:].replace(self.src, "$(LOCAL_PATH)"))
144 return INDENT + ("\n" + INDENT).join(includes) if includes else ""
145
146 def getDefines(self, name):
147 flags = path.join(self.getBuildDir(), self.getFlagsfile())
148 with open(flags) as f:
149 text = f.read()
150 line = re.findall(name + " =.*\n", text)[0]
151 if not line:
152 return ""
153
154 line = line.replace(name + " = ", "").strip()
155 return INDENT + line.replace(" ", " \\\n" + INDENT) if line else ""
156
157 def getSources(self):
158 makefile = path.join(self.getBuildDir(), self.getMakefile())
159 with open(makefile) as f:
160 text = f.read()
161 lines = re.findall(".*?\\.o:\\n", text)
162 lines = [l.replace(".o:\n", " \\") for l in lines]
163 self.adjustSources(lines)
164 #make source pretty
165 return INDENT + ("\n" + INDENT).join(lines)
166
167
168 class GmmGeneator(Generator):
169 def __init__(self, root):
170 src = path.join(root, "gmmlib")
171 super(GmmGeneator, self).__init__(src, root)
172
173 def getTemplate(self):
174 return "gmm.tpl"
175
176 def getMakefile(self):
177 return "Source/GmmLib/Makefile"
178 def getFlagsfile(self):
179 return "Source/GmmLib/CMakeFiles/igfx_gmmumd_dll.dir/flags.make"
180
181 def adjustSources(self, lines):
182 for i, l in enumerate(lines):
183 j = l.find("__/")
184 if j == -1:
185 lines[i] = path.join("Source/GmmLib", l)
186 else:
187 lines[i] = path.join("Source", l[j+3:])
188
189
190 class CmrtGeneator(Generator):
191 def __init__(self, root):
192 src = path.join(root, getDriverName(root), "cmrtlib")
193 super(CmrtGeneator, self).__init__(src, root)
194
195 def getTemplate(self):
196 return "cmrt.tpl"
197
198 def getMakefile(self):
199 return "linux/Makefile"
200
201 def getFlagsfile(self):
202 return "linux/CMakeFiles/igfxcmrt.dir/flags.make"
203
204 def adjustSources(self, lines):
205 for i, l in enumerate(lines):
206 j = l.find("__/")
207 if j == -1:
208 lines[i] = path.join("linux", l)
209 else:
210 lines[i] = l[j+3:]
211
212
213 class DriverGeneator(Generator):
214 def __init__(self, root):
215 src = path.join(root, getDriverName(root), "media_driver")
216 super(DriverGeneator, self).__init__(src, root)
217
218 def getCmakeCmd(self):
219 wd = path.join(self.src, "..")
220 cmd = 'cmake ' + wd +' -DCMAKE_INSTALL_PREFIX=/usr'
221 cmd += ' -DBUILD_ALONG_WITH_CMRTLIB=1 -DBS_DIR_GMMLIB=' + path.join(wd, '../gmmlib/Source/GmmLib/')
222 cmd += ' -DBS_DIR_COMMON=' + path.join(wd, '../gmmlib/Source/Common/')
223 cmd += ' -DBS_DIR_INC=' + path.join(wd, '../gmmlib/Source/inc/')
224 cmd += ' -DBS_DIR_MEDIA=' + wd
225 return cmd
226
227 def getTemplate(self):
228 return "media_driver.tpl"
229
230 def getMakefile(self):
231 return "media_driver/Makefile"
232
233 def getFlagsfile(self):
234 return "media_driver/CMakeFiles/iHD_drv_video.dir/flags.make"
235
236 def adjustSources(self, lines):
237 lines[:] = [l for l in lines if "media_libva_putsurface_linux.cpp" not in l]
238
239 class Main:
240
241 def run(self):
242 tool = path.dirname(__file__)
243 root = path.abspath(path.join(tool, "../../../../"))
244 print("root = "+root)
245 gens = [GmmGeneator(root), CmrtGeneator(root), DriverGeneator(root)]
246 for g in gens:
247 g.generate()
248
249
250 m = Main()
251 m.run()