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 GetOutDir():
71  """Returns the full pathname of the "out" of the Android development tree.
72
73  Assumes build environment has been properly configured by envsetup &
74  lunch/choosecombo.
75
76  Returns:
77    the absolute file path of the Android build output directory.
78  """
79  root_path = os.getenv("OUT_DIR")
80  if root_path is None:
81    root_path = os.path.join(GetTop(), "out")
82  return root_path
83
84
85def GetHostBin():
86  """Compute the full pathname to the host binary directory.
87
88  Typically $ANDROID_HOST_OUT/bin.
89
90  Assumes build environment has been properly configured by envsetup &
91  lunch/choosecombo.
92
93  Returns:
94    The absolute file path of the Android host binary directory.
95
96  Raises:
97    AbortError: if Android host binary directory could not be found.
98  """
99  path = os.path.join(GetHostOutDir(), "bin")
100  if not os.path.exists(path):
101    logger.Log("Error: Host bin path could not be found %s" % path)
102    raise errors.AbortError
103  return path
104
105
106def GetProductOut():
107  """Returns the full pathname to the target/product directory.
108
109  Typically the value of the env variable $ANDROID_PRODUCT_OUT.
110
111  Assumes build environment has been properly configured by envsetup &
112  lunch/choosecombo.
113
114  Returns:
115    The absolute file path of the Android product directory.
116
117  Raises:
118    AbortError: if Android product directory could not be found.
119  """
120  path = os.getenv("ANDROID_PRODUCT_OUT")
121  if path is None:
122    logger.Log("Error: ANDROID_PRODUCT_OUT not defined. Please run "
123               "envsetup.sh and lunch/choosecombo")
124    raise errors.AbortError
125  return path
126
127
128def GetTargetNativeTestPath():
129  """Returns the full pathname to target/product data/nativetest/ directory.
130
131  Assumes build environment has been properly configured by envsetup &
132  lunch/choosecombo.
133
134  Returns:
135    The absolute file path of the Android target native test directory.
136
137  Raises:
138    AbortError: if Android target native test directory could not be found.
139  """
140  path = os.path.join(GetProductOut(), "data", "nativetest")
141  if not os.path.exists(path):
142    logger.Log("Error: Target native test path could not be found")
143    raise errors.AbortError
144  return path
145
146
147def GetTargetSystemBin():
148  """Returns the full pathname to the target/product system/bin directory.
149
150  Typically the value of the env variable $ANDROID_PRODUCT_OUT/system/bin
151
152  Assumes build environment has been properly configured by envsetup &
153  lunch/choosecombo.
154
155  Returns:
156    The absolute file path of the Android target system bin directory.
157
158  Raises:
159    AbortError: if Android target system bin directory could not be found.
160  """
161  path = os.path.join(GetProductOut(), "system", "bin")
162  if not os.path.exists(path):
163    logger.Log("Error: Target system bin path could not be found")
164    raise errors.AbortError
165  return path
166
167def GetHostLibraryPath():
168  """Returns the full pathname to the host java library output directory.
169
170  Typically $ANDROID_HOST_OUT/framework.
171
172  Assumes build environment has been properly configured by envsetup &
173  lunch/choosecombo.
174
175  Returns:
176    The absolute file path of the Android host java library directory.
177
178  Raises:
179    AbortError: if Android host java library directory could not be found.
180  """
181  path = os.path.join(GetHostOutDir(), "framework")
182  if not os.path.exists(path):
183    logger.Log("Error: Host library path could not be found %s" % path)
184    raise errors.AbortError
185  return path
186
187def GetTestAppPath():
188  """Returns the full pathname to the test app build output directory.
189
190  Typically $ANDROID_PRODUCT_OUT/data/app
191
192  Assumes build environment has been properly configured by envsetup &
193  lunch/choosecombo.
194
195  Returns:
196    The absolute file path of the Android test app build directory.
197  """
198  return os.path.join(GetProductOut(), "data", "app")
199