4 # Copyright 2009, The Android Open Source Project
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 """In memory representation of Android.mk file.
20 Specifications for Android.mk can be found at
21 development/ndk/docs/ANDROID-MK.txt
28 class AndroidMK(object):
29 """In memory representation of Android.mk file."""
31 _RE_INCLUDE = re.compile(r'include\s+\$\((.+)\)')
33 FILENAME = "Android.mk"
34 CERTIFICATE = "LOCAL_CERTIFICATE"
35 PACKAGE_NAME = "LOCAL_PACKAGE_NAME"
37 def __init__(self, app_path=None):
38 self._includes = Set() # variables included in makefile
39 self._variables = {} # variables defined in makefile
42 self.ParseMK(app_path)
44 def _ProcessMKLine(self, line):
45 """Add a variable definition or include.
47 Ignores unrecognized lines.
50 line: line of text from makefile
52 m = self._RE_INCLUDE.match(line)
54 self._includes.add(m.group(1))
56 parts = line.split(self._VAR_DELIMITER)
58 self._variables[parts[0].strip()] = parts[1].strip()
60 def GetVariable(self, identifier):
61 """Retrieve makefile variable.
64 identifier: name of variable to retrieve
66 value of specified identifier, None if identifier not found in makefile
68 # use dict.get(x) rather than dict[x] to avoid KeyError exception,
69 # so None is returned if identifier not found
70 return self._variables.get(identifier, None)
72 def HasInclude(self, identifier):
73 """Check variable is included in makefile.
76 identifer: name of variable to check
78 True if identifer is included in makefile, otherwise False
80 return identifier in self._includes
82 def ParseMK(self, app_path):
83 """Parse Android.mk at the specified path.
86 app_path: path to folder containing Android.mk
88 IOError: Android.mk cannot be found at given path, or cannot be opened
91 self.app_path = app_path.rstrip("/")
92 self.mk_path = "%s/%s" % (self.app_path, self.FILENAME)
93 mk = open(self.mk_path)
95 self._ProcessMKLine(line)