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 GetHostOsArch(): 52 """Identify the host os and arch. 53 54 Returns: 55 The triple (HOST_OS, HOST_ARCH, HOST_OS-HOST_ARCH). 56 57 Raises: 58 AbortError: If the os and/or arch could not be found. 59 """ 60 command = ("CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core " 61 "make --no-print-directory -C \"%s\" -f build/core/config.mk " 62 "dumpvar-report_config") % GetTop() 63 64 # Use the shell b/c we set some env variables before the make command. 65 config = subprocess.Popen(command, stdout=subprocess.PIPE, 66 shell=True).communicate()[0] 67 host_os = re.search("HOST_OS=(\w+)", config).group(1) 68 host_arch = re.search("HOST_ARCH=(\w+)", config).group(1) 69 if not (host_os and host_arch): 70 logger.Log("Error: Could not get host's OS and/or ARCH") 71 raise errors.AbortError 72 return (host_os, host_arch, "%s-%s" % (host_os, host_arch)) 73 74 75def GetHostBin(): 76 """Compute the full pathname to the host binary directory. 77 78 Typically $ANDROID_BUILD_TOP/out/host/linux-x86/bin. 79 80 Assumes build environment has been properly configured by envsetup & 81 lunch/choosecombo. 82 83 Returns: 84 The absolute file path of the Android host binary directory. 85 86 Raises: 87 AbortError: if Android host binary directory could not be found. 88 """ 89 (_, _, os_arch) = GetHostOsArch() 90 path = os.path.join(GetTop(), "out", "host", os_arch, "bin") 91 if not os.path.exists(path): 92 logger.Log("Error: Host bin path could not be found %s" % path) 93 raise errors.AbortError 94 return path 95 96 97def GetProductOut(): 98 """Returns the full pathname to the target/product directory. 99 100 Typically the value of the env variable $ANDROID_PRODUCT_OUT. 101 102 Assumes build environment has been properly configured by envsetup & 103 lunch/choosecombo. 104 105 Returns: 106 The absolute file path of the Android product directory. 107 108 Raises: 109 AbortError: if Android product directory could not be found. 110 """ 111 path = os.getenv("ANDROID_PRODUCT_OUT") 112 if path is None: 113 logger.Log("Error: ANDROID_PRODUCT_OUT not defined. Please run " 114 "envsetup.sh and lunch/choosecombo") 115 raise errors.AbortError 116 return path 117 118 119def GetTargetSystemBin(): 120 """Returns the full pathname to the target/product system/bin directory. 121 122 Typically the value of the env variable $ANDROID_PRODUCT_OUT/system/bin 123 124 Assumes build environment has been properly configured by envsetup & 125 lunch/choosecombo. 126 127 Returns: 128 The absolute file path of the Android target system bin directory. 129 130 Raises: 131 AbortError: if Android target system bin directory could not be found. 132 """ 133 path = os.path.join(GetProductOut(), "system", "bin") 134 if not os.path.exists(path): 135 logger.Log("Error: Target system bin path could not be found") 136 raise errors.AbortError 137 return path 138 139def GetHostLibraryPath(): 140 """Returns the full pathname to the host java library output directory. 141 142 Typically $ANDROID_BUILD_TOP/out/host/<host_os>/framework. 143 144 Assumes build environment has been properly configured by envsetup & 145 lunch/choosecombo. 146 147 Returns: 148 The absolute file path of the Android host java library directory. 149 150 Raises: 151 AbortError: if Android host java library directory could not be found. 152 """ 153 (_, _, os_arch) = GetHostOsArch() 154 path = os.path.join(GetTop(), "out", "host", os_arch, "framework") 155 if not os.path.exists(path): 156 logger.Log("Error: Host library path could not be found %s" % path) 157 raise errors.AbortError 158 return path 159 160def GetTestAppPath(): 161 """Returns the full pathname to the test app build output directory. 162 163 Typically $ANDROID_PRODUCT_OUT/data/app 164 165 Assumes build environment has been properly configured by envsetup & 166 lunch/choosecombo. 167 168 Returns: 169 The absolute file path of the Android test app build directory. 170 """ 171 return os.path.join(GetProductOut(), "data", "app") 172