Add gtest target support for runtest --path.

This is a rough initial version. It makes several assumptions:
 - assumes each test source file is built into module with name equal
to basename of source file
 - assumes tests get installed into /data/nativetest

However, given those assumptions, both:
$ runtest --path <path to directory containing gtest source> and
$ runtest --path <path to single source file>
should work.

Coming soon: gtest host support

Bugs 4563370, 4584339

Change-Id: Ia42aeed7f9ee6402b0ceb7b5ccaaa66ac636fe49
This commit is contained in:
Brett Chabot
2011-06-17 17:07:12 -07:00
parent 6b47b8888c
commit bb5918ed81
10 changed files with 675 additions and 159 deletions

View File

@@ -21,25 +21,26 @@ Specifications for Android.mk can be found at
development/ndk/docs/ANDROID-MK.txt
"""
import os
import re
from sets import Set
import logger
class AndroidMK(object):
"""In memory representation of Android.mk file."""
_RE_INCLUDE = re.compile(r'include\s+\$\((.+)\)')
_RE_VARIABLE_REF = re.compile(r'\$\((.+)\)')
_VAR_DELIMITER = ":="
FILENAME = "Android.mk"
CERTIFICATE = "LOCAL_CERTIFICATE"
PACKAGE_NAME = "LOCAL_PACKAGE_NAME"
def __init__(self, app_path=None):
def __init__(self):
self._includes = Set() # variables included in makefile
self._variables = {} # variables defined in makefile
if app_path:
self.ParseMK(app_path)
self._has_gtestlib = False
def _ProcessMKLine(self, line):
"""Add a variable definition or include.
@@ -56,6 +57,9 @@ class AndroidMK(object):
parts = line.split(self._VAR_DELIMITER)
if len(parts) > 1:
self._variables[parts[0].strip()] = parts[1].strip()
# hack, look for explicit mention of libgtest_main
if line.find('libgtest_main') != -1:
self._has_gtestlib = True
def GetVariable(self, identifier):
"""Retrieve makefile variable.
@@ -69,6 +73,38 @@ class AndroidMK(object):
# so None is returned if identifier not found
return self._variables.get(identifier, None)
def GetExpandedVariable(self, identifier):
"""Retrieve makefile variable.
If variable value refers to another variable, recursively expand it to
find its literal value
Args:
identifier: name of variable to retrieve
Returns:
value of specified identifier, None if identifier not found in makefile
"""
# use dict.get(x) rather than dict[x] to avoid KeyError exception,
# so None is returned if identifier not found
return self.__RecursiveGetVariable(identifier, Set())
def __RecursiveGetVariable(self, identifier, visited_variables):
variable_value = self.GetVariable(identifier)
if not variable_value:
return None
if variable_value in visited_variables:
raise RuntimeError('recursive loop found for makefile variable %s'
% variable_value)
m = self._RE_VARIABLE_REF.match(variable_value)
if m:
logger.SilentLog('Found variable ref %s for identifier %s'
% (variable_value, identifier))
variable_ref = m.group(1)
visited_variables.add(variable_ref)
return self.__RecursiveGetVariable(variable_ref, visited_variables)
else:
return variable_value
def HasInclude(self, identifier):
"""Check variable is included in makefile.
@@ -79,18 +115,57 @@ class AndroidMK(object):
"""
return identifier in self._includes
def ParseMK(self, app_path):
def HasJavaLibrary(self, library_name):
"""Check if library is specified as a local java library in makefile.
Args:
library_name: name of library to check
Returns:
True if library_name is included in makefile, otherwise False
"""
java_lib_string = self.GetExpandedVariable('LOCAL_JAVA_LIBRARIES')
if java_lib_string:
java_libs = java_lib_string.split(' ')
return library_name in java_libs
return False
def HasGTest(self):
"""Check if makefile includes rule to build a native gtest.
Returns:
True if rule to build native test is in makefile, otherwise False
"""
return self._has_gtestlib or self.HasInclude('BUILD_NATIVE_TEST')
def _ParseMK(self, mk_path):
"""Parse Android.mk at the specified path.
Args:
app_path: path to folder containing Android.mk
mk_path: path to Android.mk
Raises:
IOError: Android.mk cannot be found at given path, or cannot be opened
for reading
"""
self.app_path = app_path.rstrip("/")
self.mk_path = "%s/%s" % (self.app_path, self.FILENAME)
mk = open(self.mk_path)
mk = open(mk_path)
for line in mk:
self._ProcessMKLine(line)
mk.close()
def CreateAndroidMK(path, filename=AndroidMK.FILENAME):
"""Factory method for creating a AndroidMK.
Args:
path: the directory of the make file
filename: the filename of the makefile
Return:
the AndroidMK or None if there was no file present
"""
mk_path = os.path.join(path, filename)
if os.path.isfile(mk_path):
mk = AndroidMK()
mk._ParseMK(mk_path)
return mk
else:
return None