blob: 2baa5afa7c0a6466e2dc5054128b53c207c81fcf [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
Tsu Chiang Chuangc137f5f2014-02-25 17:25:10 -0800152def 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
Nicolas Cataniaff096c12009-05-01 11:55:36 -0700171def 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
Brett Chabot764d3fa2009-06-25 17:57:31 -0700190
191def GetHostLibraryPath():
192 """Returns the full pathname to the host java library output directory.
193
JP Abgrallf38107c2013-07-11 17:39:16 -0700194 Typically $ANDROID_HOST_OUT/framework.
Brett Chabot764d3fa2009-06-25 17:57:31 -0700195
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 """
JP Abgrallf38107c2013-07-11 17:39:16 -0700205 path = os.path.join(GetHostOutDir(), "framework")
Brett Chabot764d3fa2009-06-25 17:57:31 -0700206 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.
Brett Chabot764d3fa2009-06-25 17:57:31 -0700221 """
Brett Chabote0bf8162009-06-29 13:55:30 -0700222 return os.path.join(GetProductOut(), "data", "app")