-
Notifications
You must be signed in to change notification settings - Fork 8
/
stat_makefile_generator.py
66 lines (52 loc) · 2.42 KB
/
stat_makefile_generator.py
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
#!/usr/bin/env python
# SPDX-FileCopyrightText: (c) 2020 Western Digital Corporation or its affiliates,
# Arseniy Aharonov <[email protected]>
#
# SPDX-License-Identifier: MIT
import os
import platform
import stat_attributes as attributes
from services import toPosixPath, mkdir, nameExecutable
from stat_configuration import StatConfiguration
from build_tools_crawler import BuildToolsCrawler
from stat_makefile import StatMakefile
_makFileTemplate = """# Makefile autogenerated by STAT
{variables}
include {product_file_path}
include {tool_path}/components.mak
include {tool_path}/stat_build.mak
"""
class StatMakefileGenerator(object):
def __init__(self, productMakefile):
self.__targetName = productMakefile.split('.')[0]
self.__productFilePath = '/'.join([toPosixPath(attributes.PRODUCT_DIRECTORY), productMakefile])
if not os.path.isfile(self.__productFilePath):
raise StatMakefileGeneratorException("The product file '{0}' was not found".format(self.__productFilePath))
def generate(self):
variables = self.__constructVariablesInitialization()
fileContext = _makFileTemplate.format(variables=variables,
tool_path=toPosixPath(os.path.relpath(attributes.TOOL_PATH)),
product_file_path=self.__productFilePath)
fileName = attributes.AUTO_GENERATED_MAKEFILE
directory = os.path.dirname(fileName)
mkdir(directory, exist_ok=True)
fileHandle = open(fileName, 'w')
fileHandle.write(fileContext)
fileHandle.flush()
fileHandle.close()
def __constructVariablesInitialization(self):
def _formatAssignment(_variable, _value):
return '{0} = {1}'.format(_variable, _value)
def _getValues(*_valueSets):
return [_formatAssignment(_key, _values[_key]) for _values in _valueSets for _key in iter(_values)]
values = [
_formatAssignment(StatMakefile.OS, platform.system()),
_formatAssignment(StatMakefile.NAME, self.__targetName),
_formatAssignment(StatMakefile.EXEC, nameExecutable(self.__targetName))
]
values += _getValues(StatConfiguration(), BuildToolsCrawler().getBuildAttributes())
return '\n'.join(values)
class StatMakefileGeneratorException(Exception):
"""
Custom exception for STAT mak-file generator
"""