The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # |
| 4 | # Copyright 2008, The Android Open Source Project |
| 5 | # |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 9 | # |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame] | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 11 | # |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 16 | # limitations under the License. |
| 17 | |
| 18 | """Contains utility functions for interacting with the Android build system.""" |
| 19 | |
| 20 | # Python imports |
| 21 | import os |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame] | 22 | import re |
| 23 | import subprocess |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 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 |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame] | 43 | root_path = os.getenv("ANDROID_BUILD_TOP") |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 44 | if root_path is None: |
Michael Wright | 5177da5 | 2012-05-25 15:58:33 -0700 | [diff] [blame] | 45 | logger.Log("Error: ANDROID_BUILD_TOP not defined. Please run " |
| 46 | "envsetup.sh and lunch/choosecombo") |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 47 | raise errors.AbortError |
| 48 | return root_path |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame] | 49 | |
| 50 | |
JP Abgrall | f38107c | 2013-07-11 17:39:16 -0700 | [diff] [blame] | 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 | |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame] | 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 | |
JP Abgrall | f38107c | 2013-07-11 17:39:16 -0700 | [diff] [blame] | 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 | |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame] | 109 | def GetHostBin(): |
| 110 | """Compute the full pathname to the host binary directory. |
| 111 | |
JP Abgrall | f38107c | 2013-07-11 17:39:16 -0700 | [diff] [blame] | 112 | Typically $ANDROID_HOST_OUT/bin. |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame] | 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 | """ |
JP Abgrall | f38107c | 2013-07-11 17:39:16 -0700 | [diff] [blame] | 123 | path = os.path.join(GetHostOutDir(), "bin") |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame] | 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: |
Michael Wright | 5177da5 | 2012-05-25 15:58:33 -0700 | [diff] [blame] | 146 | logger.Log("Error: ANDROID_PRODUCT_OUT not defined. Please run " |
| 147 | "envsetup.sh and lunch/choosecombo") |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame] | 148 | raise errors.AbortError |
| 149 | return path |
| 150 | |
| 151 | |
Tsu Chiang Chuang | c137f5f | 2014-02-25 17:25:10 -0800 | [diff] [blame] | 152 | def 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 Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame] | 171 | def 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 Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 190 | |
| 191 | def GetHostLibraryPath(): |
| 192 | """Returns the full pathname to the host java library output directory. |
| 193 | |
JP Abgrall | f38107c | 2013-07-11 17:39:16 -0700 | [diff] [blame] | 194 | Typically $ANDROID_HOST_OUT/framework. |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 195 | |
| 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 Abgrall | f38107c | 2013-07-11 17:39:16 -0700 | [diff] [blame] | 205 | path = os.path.join(GetHostOutDir(), "framework") |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 206 | 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 | |
| 211 | def 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 Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 221 | """ |
Brett Chabot | e0bf816 | 2009-06-29 13:55:30 -0700 | [diff] [blame] | 222 | return os.path.join(GetProductOut(), "data", "app") |