blob: 663aa5c14e3ebfd259e9d8c0b60ad4aba7739c94 [file] [log] [blame]
Jack Wang2abd80a2009-06-29 18:47:03 -07001#!/usr/bin/python2.4
2#
3#
4# Copyright 2009, The Android Open Source Project
5#
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
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
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.
17
18"""In memory representation of Android.mk file.
19
20Specifications for Android.mk can be found at
21development/ndk/docs/ANDROID-MK.txt
22"""
23
24import re
25from sets import Set
26
27
28class AndroidMK(object):
29 """In memory representation of Android.mk file."""
30
31 _RE_INCLUDE = re.compile(r'include\s+\$\((.+)\)')
32 _VAR_DELIMITER = ":="
33 FILENAME = "Android.mk"
34 CERTIFICATE = "LOCAL_CERTIFICATE"
35 PACKAGE_NAME = "LOCAL_PACKAGE_NAME"
36
37 def __init__(self, app_path=None):
38 self._includes = Set() # variables included in makefile
39 self._variables = {} # variables defined in makefile
40
41 if app_path:
42 self.ParseMK(app_path)
43
44 def _ProcessMKLine(self, line):
45 """Add a variable definition or include.
46
47 Ignores unrecognized lines.
48
49 Args:
50 line: line of text from makefile
51 """
52 m = self._RE_INCLUDE.match(line)
53 if m:
54 self._includes.add(m.group(1))
55 else:
56 parts = line.split(self._VAR_DELIMITER)
57 if len(parts) > 1:
58 self._variables[parts[0].strip()] = parts[1].strip()
59
60 def GetVariable(self, identifier):
61 """Retrieve makefile variable.
62
63 Args:
64 identifier: name of variable to retrieve
65 Returns:
66 value of specified identifier, None if identifier not found in makefile
67 """
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)
71
72 def HasInclude(self, identifier):
73 """Check variable is included in makefile.
74
75 Args:
76 identifer: name of variable to check
77 Returns:
78 True if identifer is included in makefile, otherwise False
79 """
80 return identifier in self._includes
81
82 def ParseMK(self, app_path):
83 """Parse Android.mk at the specified path.
84
85 Args:
86 app_path: path to folder containing Android.mk
87 Raises:
88 IOError: Android.mk cannot be found at given path, or cannot be opened
89 for reading
90 """
91 self.app_path = app_path.rstrip("/")
92 self.mk_path = "%s/%s" % (self.app_path, self.FILENAME)
93 mk = open(self.mk_path)
94 for line in mk:
95 self._ProcessMKLine(line)
96 mk.close()