summaryrefslogtreecommitdiffstats
path: root/testrunner/android_build.py
blob: 2baa5afa7c0a6466e2dc5054128b53c207c81fcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/python2.4
#
#
# Copyright 2008, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Contains utility functions for interacting with the Android build system."""

# Python imports
import os
import re
import subprocess

# local imports
import errors
import logger


def GetTop():
  """Returns the full pathname of the "top" of the Android development tree.

  Assumes build environment has been properly configured by envsetup &
  lunch/choosecombo.

  Returns:
    the absolute file path of the Android build root.

  Raises:
    AbortError: if Android build root could not be found.
  """
  # TODO: does this need to be reimplemented to be like gettop() in envsetup.sh
  root_path = os.getenv("ANDROID_BUILD_TOP")
  if root_path is None:
    logger.Log("Error: ANDROID_BUILD_TOP not defined. Please run "
               "envsetup.sh and lunch/choosecombo")
    raise errors.AbortError
  return root_path


def GetHostOutDir():
  """Returns the full pathname of out/host/arch of the Android development tree.

  Assumes build environment has been properly configured by envsetup &
  lunch/choosecombo.

  Returns:
    the absolute file path of the Android host output directory.
  Raises:
    AbortError: if Android host output directory could not be found.
  """
  host_out_path = os.getenv("ANDROID_HOST_OUT")
  if host_out_path is None:
    logger.Log("Error: ANDROID_HOST_OUT not defined. Please run "
               "envsetup.sh and lunch/choosecombo")
    raise errors.AbortError
  return host_out_path


def GetHostOsArch():
  """Identify the host os and arch.

  Returns:
    The triple (HOST_OS, HOST_ARCH, HOST_OS-HOST_ARCH).

  Raises:
    AbortError: If the os and/or arch could not be found.
  """
  command = ("CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core "
             "make --no-print-directory -C \"%s\" -f build/core/config.mk "
             "dumpvar-report_config") % GetTop()

  # Use the shell b/c we set some env variables before the make command.
  config = subprocess.Popen(command, stdout=subprocess.PIPE,
                            shell=True).communicate()[0]
  host_os = re.search("HOST_OS=(\w+)", config).group(1)
  host_arch = re.search("HOST_ARCH=(\w+)", config).group(1)
  if not (host_os and host_arch):
    logger.Log("Error: Could not get host's OS and/or ARCH")
    raise errors.AbortError
  return (host_os, host_arch, "%s-%s" % (host_os, host_arch))


def GetOutDir():
  """Returns the full pathname of the "out" of the Android development tree.

  Assumes build environment has been properly configured by envsetup &
  lunch/choosecombo.

  Returns:
    the absolute file path of the Android build output directory.
  """
  root_path = os.getenv("OUT_DIR")
  if root_path is None:
    root_path = os.path.join(GetTop(), "out")
  return root_path


def GetHostBin():
  """Compute the full pathname to the host binary directory.

  Typically $ANDROID_HOST_OUT/bin.

  Assumes build environment has been properly configured by envsetup &
  lunch/choosecombo.

  Returns:
    The absolute file path of the Android host binary directory.

  Raises:
    AbortError: if Android host binary directory could not be found.
  """
  path = os.path.join(GetHostOutDir(), "bin")
  if not os.path.exists(path):
    logger.Log("Error: Host bin path could not be found %s" % path)
    raise errors.AbortError
  return path


def GetProductOut():
  """Returns the full pathname to the target/product directory.

  Typically the value of the env variable $ANDROID_PRODUCT_OUT.

  Assumes build environment has been properly configured by envsetup &
  lunch/choosecombo.

  Returns:
    The absolute file path of the Android product directory.

  Raises:
    AbortError: if Android product directory could not be found.
  """
  path = os.getenv("ANDROID_PRODUCT_OUT")
  if path is None:
    logger.Log("Error: ANDROID_PRODUCT_OUT not defined. Please run "
               "envsetup.sh and lunch/choosecombo")
    raise errors.AbortError
  return path


def GetTargetNativeTestPath():
  """Returns the full pathname to target/product data/nativetest/ directory.

  Assumes build environment has been properly configured by envsetup &
  lunch/choosecombo.

  Returns:
    The absolute file path of the Android target native test directory.

  Raises:
    AbortError: if Android target native test directory could not be found.
  """
  path = os.path.join(GetProductOut(), "data", "nativetest")
  if not os.path.exists(path):
    logger.Log("Error: Target native test path could not be found")
    raise errors.AbortError
  return path


def GetTargetSystemBin():
  """Returns the full pathname to the target/product system/bin directory.

  Typically the value of the env variable $ANDROID_PRODUCT_OUT/system/bin

  Assumes build environment has been properly configured by envsetup &
  lunch/choosecombo.

  Returns:
    The absolute file path of the Android target system bin directory.

  Raises:
    AbortError: if Android target system bin directory could not be found.
  """
  path = os.path.join(GetProductOut(), "system", "bin")
  if not os.path.exists(path):
    logger.Log("Error: Target system bin path could not be found")
    raise errors.AbortError
  return path

def GetHostLibraryPath():
  """Returns the full pathname to the host java library output directory.

  Typically $ANDROID_HOST_OUT/framework.

  Assumes build environment has been properly configured by envsetup &
  lunch/choosecombo.

  Returns:
    The absolute file path of the Android host java library directory.

  Raises:
    AbortError: if Android host java library directory could not be found.
  """
  path = os.path.join(GetHostOutDir(), "framework")
  if not os.path.exists(path):
    logger.Log("Error: Host library path could not be found %s" % path)
    raise errors.AbortError
  return path

def GetTestAppPath():
  """Returns the full pathname to the test app build output directory.

  Typically $ANDROID_PRODUCT_OUT/data/app

  Assumes build environment has been properly configured by envsetup &
  lunch/choosecombo.

  Returns:
    The absolute file path of the Android test app build directory.
  """
  return os.path.join(GetProductOut(), "data", "app")