blob: cacd67e009650ee8a819a35527c23643973d8f36 [file] [log] [blame]
The Android Open Source Project6ffae012009-03-18 17:39:43 -07001#!/usr/bin/python2.4
2#
3#
4# Copyright 2008, The Android Open Source Project
5#
Nicolas Cataniaff096c12009-05-01 11:55:36 -07006# 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
The Android Open Source Project6ffae012009-03-18 17:39:43 -07009#
Nicolas Cataniaff096c12009-05-01 11:55:36 -070010# http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project6ffae012009-03-18 17:39:43 -070011#
Nicolas Cataniaff096c12009-05-01 11:55:36 -070012# 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
The Android Open Source Project6ffae012009-03-18 17:39:43 -070016# limitations under the License.
17
18"""Contains utility functions for interacting with the Android build system."""
19
20# Python imports
21import os
Nicolas Cataniaff096c12009-05-01 11:55:36 -070022import re
23import subprocess
The Android Open Source Project6ffae012009-03-18 17:39:43 -070024
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
Nicolas Cataniaff096c12009-05-01 11:55:36 -070043 root_path = os.getenv("ANDROID_BUILD_TOP")
The Android Open Source Project6ffae012009-03-18 17:39:43 -070044 if root_path is None:
Michael Wright5177da52012-05-25 15:58:33 -070045 logger.Log("Error: ANDROID_BUILD_TOP not defined. Please run "
46 "envsetup.sh and lunch/choosecombo")
The Android Open Source Project6ffae012009-03-18 17:39:43 -070047 raise errors.AbortError
48 return root_path
Nicolas Cataniaff096c12009-05-01 11:55:36 -070049
50
JP Abgrallf38107c2013-07-11 17:39:16 -070051def 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
Nicolas Cataniaff096c12009-05-01 11:55:36 -070070def 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
JP Abgrallf38107c2013-07-11 17:39:16 -070094def 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
Nicolas Cataniaff096c12009-05-01 11:55:36 -0700109def GetHostBin():
110 """Compute the full pathname to the host binary directory.
111
JP Abgrallf38107c2013-07-11 17:39:16 -0700112 Typically $ANDROID_HOST_OUT/bin.
Nicolas Cataniaff096c12009-05-01 11:55:36 -0700113
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 """
JP Abgrallf38107c2013-07-11 17:39:16 -0700123 path = os.path.join(GetHostOutDir(), "bin")
Nicolas Cataniaff096c12009-05-01 11:55:36 -0700124 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:
Michael Wright5177da52012-05-25 15:58:33 -0700146 logger.Log("Error: ANDROID_PRODUCT_OUT not defined. Please run "
147 "envsetup.sh and lunch/choosecombo")
Nicolas Cataniaff096c12009-05-01 11:55:36 -0700148 raise errors.AbortError
149 return path
150
151
152def GetTargetSystemBin():
153 """Returns the full pathname to the target/product system/bin directory.
154
155 Typically the value of the env variable $ANDROID_PRODUCT_OUT/system/bin
156
157 Assumes build environment has been properly configured by envsetup &
158 lunch/choosecombo.
159
160 Returns:
161 The absolute file path of the Android target system bin directory.
162
163 Raises:
164 AbortError: if Android target system bin directory could not be found.
165 """
166 path = os.path.join(GetProductOut(), "system", "bin")
167 if not os.path.exists(path):
168 logger.Log("Error: Target system bin path could not be found")
169 raise errors.AbortError
170 return path
Brett Chabot764d3fa2009-06-25 17:57:31 -0700171
172def GetHostLibraryPath():
173 """Returns the full pathname to the host java library output directory.
174
JP Abgrallf38107c2013-07-11 17:39:16 -0700175 Typically $ANDROID_HOST_OUT/framework.
Brett Chabot764d3fa2009-06-25 17:57:31 -0700176
177 Assumes build environment has been properly configured by envsetup &
178 lunch/choosecombo.
179
180 Returns:
181 The absolute file path of the Android host java library directory.
182
183 Raises:
184 AbortError: if Android host java library directory could not be found.
185 """
JP Abgrallf38107c2013-07-11 17:39:16 -0700186 path = os.path.join(GetHostOutDir(), "framework")
Brett Chabot764d3fa2009-06-25 17:57:31 -0700187 if not os.path.exists(path):
188 logger.Log("Error: Host library path could not be found %s" % path)
189 raise errors.AbortError
190 return path
191
192def GetTestAppPath():
193 """Returns the full pathname to the test app build output directory.
194
195 Typically $ANDROID_PRODUCT_OUT/data/app
196
197 Assumes build environment has been properly configured by envsetup &
198 lunch/choosecombo.
199
200 Returns:
201 The absolute file path of the Android test app build directory.
Brett Chabot764d3fa2009-06-25 17:57:31 -0700202 """
Brett Chabote0bf8162009-06-29 13:55:30 -0700203 return os.path.join(GetProductOut(), "data", "app")