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: |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame^] | 45 | logger.Log("Error: ANDROID_BUILD_TOP not defined. Please run envsetup.sh") |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 46 | raise errors.AbortError |
| 47 | return root_path |
Nicolas Catania | ff096c1 | 2009-05-01 11:55:36 -0700 | [diff] [blame^] | 48 | |
| 49 | |
| 50 | def GetHostOsArch(): |
| 51 | """Identify the host os and arch. |
| 52 | |
| 53 | Returns: |
| 54 | The triple (HOST_OS, HOST_ARCH, HOST_OS-HOST_ARCH). |
| 55 | |
| 56 | Raises: |
| 57 | AbortError: If the os and/or arch could not be found. |
| 58 | """ |
| 59 | command = ("CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core " |
| 60 | "make --no-print-directory -C \"%s\" -f build/core/config.mk " |
| 61 | "dumpvar-report_config") % GetTop() |
| 62 | |
| 63 | # Use the shell b/c we set some env variables before the make command. |
| 64 | config = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 65 | shell=True).communicate()[0] |
| 66 | host_os = re.search("HOST_OS=(\w+)", config).group(1) |
| 67 | host_arch = re.search("HOST_ARCH=(\w+)", config).group(1) |
| 68 | if not (host_os and host_arch): |
| 69 | logger.Log("Error: Could not get host's OS and/or ARCH") |
| 70 | raise errors.AbortError |
| 71 | return (host_os, host_arch, "%s-%s" % (host_os, host_arch)) |
| 72 | |
| 73 | |
| 74 | def GetHostBin(): |
| 75 | """Compute the full pathname to the host binary directory. |
| 76 | |
| 77 | Typically $ANDROID_BUILD_TOP/out/host/linux-x86/bin. |
| 78 | |
| 79 | Assumes build environment has been properly configured by envsetup & |
| 80 | lunch/choosecombo. |
| 81 | |
| 82 | Returns: |
| 83 | The absolute file path of the Android host binary directory. |
| 84 | |
| 85 | Raises: |
| 86 | AbortError: if Android host binary directory could not be found. |
| 87 | """ |
| 88 | (_, _, os_arch) = GetHostOsArch() |
| 89 | path = os.path.join(GetTop(), "out", "host", os_arch, "bin") |
| 90 | if not os.path.exists(path): |
| 91 | logger.Log("Error: Host bin path could not be found %s" % path) |
| 92 | raise errors.AbortError |
| 93 | return path |
| 94 | |
| 95 | |
| 96 | def GetProductOut(): |
| 97 | """Returns the full pathname to the target/product directory. |
| 98 | |
| 99 | Typically the value of the env variable $ANDROID_PRODUCT_OUT. |
| 100 | |
| 101 | Assumes build environment has been properly configured by envsetup & |
| 102 | lunch/choosecombo. |
| 103 | |
| 104 | Returns: |
| 105 | The absolute file path of the Android product directory. |
| 106 | |
| 107 | Raises: |
| 108 | AbortError: if Android product directory could not be found. |
| 109 | """ |
| 110 | path = os.getenv("ANDROID_PRODUCT_OUT") |
| 111 | if path is None: |
| 112 | logger.Log("Error: ANDROID_PRODUCT_OUT not defined. Please run envsetup.sh") |
| 113 | raise errors.AbortError |
| 114 | return path |
| 115 | |
| 116 | |
| 117 | def GetTargetSystemBin(): |
| 118 | """Returns the full pathname to the target/product system/bin directory. |
| 119 | |
| 120 | Typically the value of the env variable $ANDROID_PRODUCT_OUT/system/bin |
| 121 | |
| 122 | Assumes build environment has been properly configured by envsetup & |
| 123 | lunch/choosecombo. |
| 124 | |
| 125 | Returns: |
| 126 | The absolute file path of the Android target system bin directory. |
| 127 | |
| 128 | Raises: |
| 129 | AbortError: if Android target system bin directory could not be found. |
| 130 | """ |
| 131 | path = os.path.join(GetProductOut(), "system", "bin") |
| 132 | if not os.path.exists(path): |
| 133 | logger.Log("Error: Target system bin path could not be found") |
| 134 | raise errors.AbortError |
| 135 | return path |