blob: 7a7769f6864aef26940399164be00136572b921b [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
Brett Chabotbb5918e2011-06-17 17:07:12 -070024import os
Jack Wang2abd80a2009-06-29 18:47:03 -070025import re
26from sets import Set
27
Brett Chabotbb5918e2011-06-17 17:07:12 -070028import logger
Jack Wang2abd80a2009-06-29 18:47:03 -070029
30class AndroidMK(object):
31 """In memory representation of Android.mk file."""
32
33 _RE_INCLUDE = re.compile(r'include\s+\$\((.+)\)')
Brett Chabotbb5918e2011-06-17 17:07:12 -070034 _RE_VARIABLE_REF = re.compile(r'\$\((.+)\)')
Jack Wang2abd80a2009-06-29 18:47:03 -070035 _VAR_DELIMITER = ":="
36 FILENAME = "Android.mk"
37 CERTIFICATE = "LOCAL_CERTIFICATE"
38 PACKAGE_NAME = "LOCAL_PACKAGE_NAME"
39
Brett Chabotbb5918e2011-06-17 17:07:12 -070040 def __init__(self):
Jack Wang2abd80a2009-06-29 18:47:03 -070041 self._includes = Set() # variables included in makefile
42 self._variables = {} # variables defined in makefile
Brett Chabotbb5918e2011-06-17 17:07:12 -070043 self._has_gtestlib = False
Jack Wang2abd80a2009-06-29 18:47:03 -070044
45 def _ProcessMKLine(self, line):
46 """Add a variable definition or include.
47
48 Ignores unrecognized lines.
49
50 Args:
51 line: line of text from makefile
52 """
53 m = self._RE_INCLUDE.match(line)
54 if m:
55 self._includes.add(m.group(1))
56 else:
57 parts = line.split(self._VAR_DELIMITER)
58 if len(parts) > 1:
59 self._variables[parts[0].strip()] = parts[1].strip()
Brett Chabotbb5918e2011-06-17 17:07:12 -070060 # hack, look for explicit mention of libgtest_main
61 if line.find('libgtest_main') != -1:
62 self._has_gtestlib = True
Jack Wang2abd80a2009-06-29 18:47:03 -070063
64 def GetVariable(self, identifier):
65 """Retrieve makefile variable.
66
67 Args:
68 identifier: name of variable to retrieve
69 Returns:
70 value of specified identifier, None if identifier not found in makefile
71 """
72 # use dict.get(x) rather than dict[x] to avoid KeyError exception,
73 # so None is returned if identifier not found
74 return self._variables.get(identifier, None)
75
Brett Chabotbb5918e2011-06-17 17:07:12 -070076 def GetExpandedVariable(self, identifier):
77 """Retrieve makefile variable.
78
79 If variable value refers to another variable, recursively expand it to
80 find its literal value
81
82 Args:
83 identifier: name of variable to retrieve
84 Returns:
85 value of specified identifier, None if identifier not found in makefile
86 """
87 # use dict.get(x) rather than dict[x] to avoid KeyError exception,
88 # so None is returned if identifier not found
89 return self.__RecursiveGetVariable(identifier, Set())
90
91 def __RecursiveGetVariable(self, identifier, visited_variables):
92 variable_value = self.GetVariable(identifier)
93 if not variable_value:
94 return None
95 if variable_value in visited_variables:
96 raise RuntimeError('recursive loop found for makefile variable %s'
97 % variable_value)
98 m = self._RE_VARIABLE_REF.match(variable_value)
99 if m:
100 logger.SilentLog('Found variable ref %s for identifier %s'
101 % (variable_value, identifier))
102 variable_ref = m.group(1)
103 visited_variables.add(variable_ref)
104 return self.__RecursiveGetVariable(variable_ref, visited_variables)
105 else:
106 return variable_value
107
Jack Wang2abd80a2009-06-29 18:47:03 -0700108 def HasInclude(self, identifier):
109 """Check variable is included in makefile.
110
111 Args:
112 identifer: name of variable to check
113 Returns:
114 True if identifer is included in makefile, otherwise False
115 """
116 return identifier in self._includes
117
Brett Chabot8ac51182012-09-19 07:35:35 -0700118 def IncludesMakefilesUnder(self):
119 """Check if makefile has a 'include makefiles under here' rule"""
120 return self.HasInclude('call all-makefiles-under,$(LOCAL_PATH)')
121
Brett Chabotbb5918e2011-06-17 17:07:12 -0700122 def HasJavaLibrary(self, library_name):
123 """Check if library is specified as a local java library in makefile.
124
125 Args:
126 library_name: name of library to check
127 Returns:
128 True if library_name is included in makefile, otherwise False
129 """
130 java_lib_string = self.GetExpandedVariable('LOCAL_JAVA_LIBRARIES')
131 if java_lib_string:
132 java_libs = java_lib_string.split(' ')
133 return library_name in java_libs
134 return False
135
136 def HasGTest(self):
137 """Check if makefile includes rule to build a native gtest.
138
139 Returns:
140 True if rule to build native test is in makefile, otherwise False
141 """
142 return self._has_gtestlib or self.HasInclude('BUILD_NATIVE_TEST')
143
144 def _ParseMK(self, mk_path):
Jack Wang2abd80a2009-06-29 18:47:03 -0700145 """Parse Android.mk at the specified path.
146
147 Args:
Brett Chabotbb5918e2011-06-17 17:07:12 -0700148 mk_path: path to Android.mk
Jack Wang2abd80a2009-06-29 18:47:03 -0700149 Raises:
150 IOError: Android.mk cannot be found at given path, or cannot be opened
151 for reading
152 """
Brett Chabotbb5918e2011-06-17 17:07:12 -0700153 mk = open(mk_path)
Jack Wang2abd80a2009-06-29 18:47:03 -0700154 for line in mk:
155 self._ProcessMKLine(line)
156 mk.close()
Brett Chabotbb5918e2011-06-17 17:07:12 -0700157
158
159def CreateAndroidMK(path, filename=AndroidMK.FILENAME):
160 """Factory method for creating a AndroidMK.
161
162 Args:
163 path: the directory of the make file
164 filename: the filename of the makefile
165
166 Return:
167 the AndroidMK or None if there was no file present
168 """
169 mk_path = os.path.join(path, filename)
170 if os.path.isfile(mk_path):
171 mk = AndroidMK()
172 mk._ParseMK(mk_path)
173 return mk
174 else:
Brett Chabot8ac51182012-09-19 07:35:35 -0700175 return None