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 21 import os 22 import re 23 import subprocess 24 25 # local imports 26 import errors 27 import logger 28 29 30 def 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 51 def 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 70 def 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 94 def 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 109 def 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 130 def 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 152 def 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 171 172 def GetHostLibraryPath(): 173 """Returns the full pathname to the host java library output directory. 174 175 Typically $ANDROID_HOST_OUT/framework. 176 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 """ 186 path = os.path.join(GetHostOutDir(), "framework") 187 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 192 def 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. 202 """ 203 return os.path.join(GetProductOut(), "data", "app") 204