1 #!/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 20 Specifications for Android.mk can be found at 21 development/ndk/docs/ANDROID-MK.txt 22 """ 23 24 import os 25 import re 26 from sets import Set 27 28 import logger 29 30 class AndroidMK(object): 31 """In memory representation of Android.mk file.""" 32 33 _RE_INCLUDE = re.compile(r'include\s+\$\((.+)\)') 34 _RE_VARIABLE_REF = re.compile(r'\$\((.+)\)') 35 _VAR_DELIMITER = ":=" 36 FILENAME = "Android.mk" 37 CERTIFICATE = "LOCAL_CERTIFICATE" 38 PACKAGE_NAME = "LOCAL_PACKAGE_NAME" 39 40 def __init__(self): 41 self._includes = Set() # variables included in makefile 42 self._variables = {} # variables defined in makefile 43 self._has_gtestlib = False 44 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() 60 # hack, look for explicit mention of libgtest_main 61 if line.find('libgtest_main') != -1: 62 self._has_gtestlib = True 63 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 76 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 108 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 118 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 122 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): 145 """Parse Android.mk at the specified path. 146 147 Args: 148 mk_path: path to Android.mk 149 Raises: 150 IOError: Android.mk cannot be found at given path, or cannot be opened 151 for reading 152 """ 153 mk = open(mk_path) 154 for line in mk: 155 self._ProcessMKLine(line) 156 mk.close() 157 158 159 def 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: 175 return None 176