File: GenerateAndroidMk.py

package info (click to toggle)
intel-media-driver 23.1.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 146,180 kB
  • sloc: cpp: 1,508,381; ansic: 205,392; asm: 45,336; python: 653; sh: 177; makefile: 16
file content (251 lines) | stat: -rwxr-xr-x 8,327 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#! /usr/bin/env python3
"""
* Copyright (c) 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
"""

"""
the basic idea from this script are:
1. generate linux make file using cmake
2. get source files using "grep -ir \\.o:$ Makefile  |sed s/.c.o:/.c/ |sed s/.cpp.o:/.cpp/ | sed s,^,\\t, | sed s/$/' \\'/"
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/$/ \\/'"
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/$/ \\/'"
5. replace related text in template
"""

import os
import os.path as path
import re

INDENT = "    "
TOOL_DIR = "Tools/MediaDriverTools/Android/"
TEMPLATE_DIR = path.join(TOOL_DIR, "mk")


def remove(f):
    cmd = "rm " + f + "> /dev/null 2>&1"
    os.system(cmd)

def getDriverName(root):
    driver = "mod"
    if not path.exists(path.join(root, driver)):
        driver = "media-driver"
    if not path.exists(path.join(root, driver)):
        raise Exception("driver path " + driver +" not existed")
    return driver

class Generator:
    def __init__(self, src, root, makefile=None):
        #where to put the Android makefile
        self.makefile = makefile if makefile else src

        #where is the major source file
        self.src = src

        #driver name
        driver = getDriverName(root)
        #where to put build file and template
        self.tool = path.join(root, driver, TOOL_DIR)

        """where to put the template"""
        self.templ = path.join(root, driver, TEMPLATE_DIR)

    #major function
    def generate(self):

        if path.exists(self.src) == False:
            raise Exception(self.src + "not existed")
        self.generateMakefile()

        mk = path.join(self.makefile, "Android.mk")
        #remove old Android.mk
        remove(mk)

        #create new Android.mk
        with open(self.getTemplatePath()) as f:
            tpl = f.read()
        tpl = tpl.replace("@LOCAL_SRC_FILES", self.getSources())
        tpl = tpl.replace("@LOCAL_CFLAGS", self.getDefines("CXX_DEFINES"))
        tpl = tpl.replace("@LOCAL_C_INCLUDES", self.getIncludes())
        with open(mk, "w") as f:
            f.write(tpl)
        print("generated " + self.getName() + " to " + self.makefile)

    #virtuall functions
    def getTemplate(self):
        raise Exception("pure virtul function")

    def getFlagsfile(self):
        raise Exception("pure virtul function")

    def getMakefile(self):
        return "Makefile"

    def getName(self):
        return self.getTemplate().split('.')[0]

    def getTemplatePath(self):
        return path.join(self.templ, self.getTemplate())

    def getBuildDir(self):
        return path.join(self.tool, 'build', self.getName())

    def adjustSources(self, lines):
        #print(lines)
        return lines

    def adjustIncludes(self, lines):
        #print(lines)
        return lines

    def getCmakeCmd(self):
        return "cmake " + self.src

    def generateMakefile(self, debug=False):
        #Win env can help us debug the script
        #but we do not want generate makefile on Win-system
        if os.name == "nt":
            return
        verbose = ";" if debug else "> /dev/null 2>&1;"
        build = self.getBuildDir()
        cmd = "rm " + path.join(build, "CMakeCache.txt") + verbose
        cmd += "mkdir -p " + build + verbose
        cmd += "cd " + build
        cmd += "&& " + self.getCmakeCmd() + verbose
        os.system(cmd)

    def getIncludes(self):
        text = self.getDefines("CXX_INCLUDES")
        includes = []
        lines = text.split("\n")
        for l in lines:
            #normpath will make sure we did not refer outside.
            p = path.normpath(l)
            j = p.find(self.src)
            if j != -1:
                includes.append(p[j:].replace(self.src, "$(LOCAL_PATH)"))
        return INDENT + ("\n" + INDENT).join(includes) if includes else ""

    def getDefines(self, name):
        flags = path.join(self.getBuildDir(), self.getFlagsfile())
        with open(flags) as f:
            text = f.read()
        line = re.findall(name + " =.*\n", text)[0]
        if not line:
            return ""

        line = line.replace(name + " = ", "").strip()
        return INDENT + line.replace(" ", " \\\n" + INDENT) if line else ""

    def getSources(self):
        makefile = path.join(self.getBuildDir(), self.getMakefile())
        with open(makefile) as f:
            text = f.read()
        lines = re.findall(".*?\\.o:\\n", text)
        lines = [l.replace(".o:\n", " \\") for l in lines]
        self.adjustSources(lines)
        #make source pretty
        return INDENT + ("\n" + INDENT).join(lines)


class GmmGeneator(Generator):
    def __init__(self, root):
        src = path.join(root, "gmmlib")
        super(GmmGeneator, self).__init__(src, root)

    def getTemplate(self):
        return "gmm.tpl"

    def getMakefile(self):
        return "Source/GmmLib/Makefile"
    def getFlagsfile(self):
        return "Source/GmmLib/CMakeFiles/igfx_gmmumd_dll.dir/flags.make"

    def adjustSources(self, lines):
        for i, l in enumerate(lines):
            j = l.find("__/")
            if j == -1:
                lines[i] = path.join("Source/GmmLib", l)
            else:
                lines[i] = path.join("Source", l[j+3:])


class CmrtGeneator(Generator):
    def __init__(self, root):
        src = path.join(root, getDriverName(root), "cmrtlib")
        super(CmrtGeneator, self).__init__(src, root)

    def getTemplate(self):
        return "cmrt.tpl"

    def getMakefile(self):
        return "linux/Makefile"

    def getFlagsfile(self):
        return "linux/CMakeFiles/igfxcmrt.dir/flags.make"

    def adjustSources(self, lines):
        for i, l in enumerate(lines):
            j = l.find("__/")
            if j == -1:
                lines[i] = path.join("linux", l)
            else:
                lines[i] = l[j+3:]


class DriverGeneator(Generator):
    def __init__(self, root):
        src = path.join(root, getDriverName(root), "media_driver")
        super(DriverGeneator, self).__init__(src, root)

    def getCmakeCmd(self):
        wd = path.join(self.src, "..")
        cmd = 'cmake ' + wd +' -DCMAKE_INSTALL_PREFIX=/usr'
        cmd += ' -DBUILD_ALONG_WITH_CMRTLIB=1 -DBS_DIR_GMMLIB=' + path.join(wd, '../gmmlib/Source/GmmLib/')
        cmd += ' -DBS_DIR_COMMON=' + path.join(wd, '../gmmlib/Source/Common/')
        cmd += ' -DBS_DIR_INC=' + path.join(wd, '../gmmlib/Source/inc/')
        cmd += ' -DBS_DIR_MEDIA=' + wd
        return cmd

    def getTemplate(self):
        return "media_driver.tpl"

    def getMakefile(self):
        return "media_driver/Makefile"

    def getFlagsfile(self):
        return "media_driver/CMakeFiles/iHD_drv_video.dir/flags.make"

    def adjustSources(self, lines):
        lines[:] = [l for l in lines if "media_libva_putsurface_linux.cpp" not in l]

class Main:

    def run(self):
        tool = path.dirname(__file__)
        root = path.abspath(path.join(tool, "../../../../"))
        print("root = "+root)
        gens = [GmmGeneator(root), CmrtGeneator(root), DriverGeneator(root)]
        for g in gens:
            g.generate()


m = Main()
m.run()