1#!/usr/bin/python2.4
2#
3#
4# Copyright 2008, 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"""Contains utility functions for interacting with the Android build system."""
19
20# Python imports
21import os
22import re
23import subprocess
24
25# local imports
26import errors
27import logger
28
29
30def GetTop():
31  """Returns the full pathname of the "top" of the Android development tree.
32
33  Assumes build environment has been properly configured by envsetup &
34  lunch/choosecombo.
35
36  Returns:
37    the absolute file path of the Android build root.
38
39  Raises:
40    AbortError: if Android build root could not be found.
41  """
42  # TODO: does this need to be reimplemented to be like gettop() in envsetup.sh
43  root_path = os.getenv("ANDROID_BUILD_TOP")
44  if root_path is None:
45    logger.Log("Error: ANDROID_BUILD_TOP not defined. Please run "
46               "envsetup.sh and lunch/choosecombo")
47    raise errors.AbortError
48  return root_path
49
50
51def GetHostOutDir():
52  """Returns the full pathname of out/host/arch of the Android development tree.
53
54  Assumes build environment has been properly configured by envsetup &
55  lunch/choosecombo.
56
57  Returns:
58    the absolute file path of the Android host output directory.
59  Raises:
60    AbortError: if Android host output directory could not be found.
61  """
62  host_out_path = os.getenv("ANDROID_HOST_OUT")
63  if host_out_path is None:
64    logger.Log("Error: ANDROID_HOST_OUT not defined. Please run "
65               "envsetup.sh and lunch/choosecombo")
66    raise errors.AbortError
67  return host_out_path
68
69
70def GetHostOsArch():
71  """Identify the host os and arch.
72
73  Returns:
74    The triple (HOST_OS, HOST_ARCH, HOST_OS-HOST_ARCH).
75
76  Raises:
77    AbortError: If the os and/or arch could not be found.
78  """
79  command = ("CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core "
80             "make --no-print-directory -C \"%s\" -f build/core/config.mk "
81             "dumpvar-report_config") % GetTop()
82
83  # Use the shell b/c we set some env variables before the make command.
84  config = subprocess.Popen(command, stdout=subprocess.PIPE,
85                            shell=True).communicate()[0]
86  host_os = re.search("HOST_OS=(\w+)", config).group(1)
87  host_arch = re.search("HOST_ARCH=(\w+)", config).group(1)
88  if not (host_os and host_arch):
89    logger.Log("Error: Could not get host's OS and/or ARCH")
90    raise errors.AbortError
91  return (host_os, host_arch, "%s-%s" % (host_os, host_arch))
92
93
94def GetOutDir():
95  """Returns the full pathname of the "out" of the Android development tree.
96
97  Assumes build environment has been properly configured by envsetup &
98  lunch/choosecombo.
99
100  Returns:
101    the absolute file path of the Android build output directory.
102  """
103  root_path = os.getenv("OUT_DIR")
104  if root_path is None:
105    root_path = os.path.join(GetTop(), "out")
106  return root_path
107
108
109def GetHostBin():
110  """Compute the full pathname to the host binary directory.
111
112  Typically $ANDROID_HOST_OUT/bin.
113
114  Assumes build environment has been properly configured by envsetup &
115  lunch/choosecombo.
116
117  Returns:
118    The absolute file path of the Android host binary directory.
119
120  Raises:
121    AbortError: if Android host binary directory could not be found.
122  """
123  path = os.path.join(GetHostOutDir(), "bin")
124  if not os.path.exists(path):
125    logger.Log("Error: Host bin path could not be found %s" % path)
126    raise errors.AbortError
127  return path
128
129
130def GetProductOut():
131  """Returns the full pathname to the target/product directory.
132
133  Typically the value of the env variable $ANDROID_PRODUCT_OUT.
134
135  Assumes build environment has been properly configured by envsetup &
136  lunch/choosecombo.
137
138  Returns:
139    The absolute file path of the Android product directory.
140
141  Raises:
142    AbortError: if Android product directory could not be found.
143  """
144  path = os.getenv("ANDROID_PRODUCT_OUT")
145  if path is None:
146    logger.Log("Error: ANDROID_PRODUCT_OUT not defined. Please run "
147               "envsetup.sh and lunch/choosecombo")
148    raise errors.AbortError
149  return path
150
151
152def GetTargetNativeTestPath():
153  """Returns the full pathname to target/product data/nativetest/ directory.
154
155  Assumes build environment has been properly configured by envsetup &
156  lunch/choosecombo.
157
158  Returns:
159    The absolute file path of the Android target native test directory.
160
161  Raises:
162    AbortError: if Android target native test directory could not be found.
163  """
164  path = os.path.join(GetProductOut(), "data", "nativetest")
165  if not os.path.exists(path):
166    logger.Log("Error: Target native test path could not be found")
167    raise errors.AbortError
168  return path
169
170
171def GetTargetSystemBin():
172  """Returns the full pathname to the target/product system/bin directory.
173
174  Typically the value of the env variable $ANDROID_PRODUCT_OUT/system/bin
175
176  Assumes build environment has been properly configured by envsetup &
177  lunch/choosecombo.
178
179  Returns:
180    The absolute file path of the Android target system bin directory.
181
182  Raises:
183    AbortError: if Android target system bin directory could not be found.
184  """
185  path = os.path.join(GetProductOut(), "system", "bin")
186  if not os.path.exists(path):
187    logger.Log("Error: Target system bin path could not be found")
188    raise errors.AbortError
189  return path
190
191def GetHostLibraryPath():
192  """Returns the full pathname to the host java library output directory.
193
194  Typically $ANDROID_HOST_OUT/framework.
195
196  Assumes build environment has been properly configured by envsetup &
197  lunch/choosecombo.
198
199  Returns:
200    The absolute file path of the Android host java library directory.
201
202  Raises:
203    AbortError: if Android host java library directory could not be found.
204  """
205  path = os.path.join(GetHostOutDir(), "framework")
206  if not os.path.exists(path):
207    logger.Log("Error: Host library path could not be found %s" % path)
208    raise errors.AbortError
209  return path
210
211def GetTestAppPath():
212  """Returns the full pathname to the test app build output directory.
213
214  Typically $ANDROID_PRODUCT_OUT/data/app
215
216  Assumes build environment has been properly configured by envsetup &
217  lunch/choosecombo.
218
219  Returns:
220    The absolute file path of the Android test app build directory.
221  """
222  return os.path.join(GetProductOut(), "data", "app")
223