1# Copyright 2021 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15load(":build_id.rbc|init", _build_id_init = "init")
16
17def _all_versions():
18    """Returns all known versions."""
19    versions = ["OPR1", "OPD1", "OPD2", "OPM1", "OPM2", "PPR1", "PPD1", "PPD2", "PPM1", "PPM2", "QPR1"]
20    for v in ("Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"):
21        for e in ("P1A", "P1B", "P2A", "P2B", "D1A", "D1B", "D2A", "D2B", "Q1A", "Q1B", "Q2A", "Q2B", "Q3A", "Q3B"):
22            versions.append(v + e)
23    return versions
24
25def _allowed_versions(all_versions, min_version, max_version, default_version):
26    """Checks that version range and default versions is valid, returns all versions in range."""
27    for v in (min_version, max_version, default_version):
28        if v not in all_versions:
29            fail("% is invalid" % v)
30
31    min_i = all_versions.index(min_version)
32    max_i = all_versions.index(max_version)
33    def_i = all_versions.index(default_version)
34    if min_i > max_i:
35        fail("%s should come before %s in the version list" % (min_version, max_version))
36    if def_i < min_i or def_i > max_i:
37        fail("%s should come between % and %s" % (default_version, min_version, max_version))
38    return all_versions[min_i:max_i + 1]
39
40# This function is a manual conversion of the version_defaults.mk
41def _versions_default(g, all_versions):
42    """Handle various build version information.
43
44    Guarantees that the following are defined:
45     PLATFORM_VERSION
46     PLATFORM_SDK_VERSION
47     PLATFORM_VERSION_CODENAME
48     DEFAULT_APP_TARGET_SDK
49     BUILD_ID
50     BUILD_NUMBER
51     PLATFORM_SECURITY_PATCH
52     PLATFORM_VNDK_VERSION
53     PLATFORM_SYSTEMSDK_VERSIONS
54    """
55
56    # If build_id.rbc exists, it may override some of the defaults.
57    # Note that build.prop target also wants INTERNAL_BUILD_ID_MAKEFILE to be set if the file exists.
58    if _build_id_init != None:
59        _build_id_init(g)
60        g["INTERNAL_BUILD_ID_MAKEFILE"] = "build/make/core/build_id"
61
62    allowed_versions = _allowed_versions(all_versions, v_min, v_max, v_default)
63    g.setdefault("TARGET_PLATFORM_VERSION", v_default)
64    if g["TARGET_PLATFORM_VERSION"] not in allowed_versions:
65        fail("% is not valid, must be one of %s" % (g["TARGET_PLATFORM_VERSION"], allowed_versions))
66
67    g["DEFAULT_PLATFORM_VERSION"] = v_default
68    g["PLATFORM_VERSION_LAST_STABLE"] = 11
69    g.setdefault("PLATFORM_VERSION_CODENAME", g["TARGET_PLATFORM_VERSION"])
70    # TODO(asmundak): set PLATFORM_VERSION_ALL_CODENAMES
71
72    g.setdefault("PLATFORM_SDK_VERSION", 30)
73    version_codename = g["PLATFORM_VERSION_CODENAME"]
74    if version_codename == "REL":
75        g.setdefault("PLATFORM_VERSION", g["PLATFORM_VERSION_LAST_STABLE"])
76        g["PLATFORM_PREVIEW_SDK_VERSION"] = 0
77        g.setdefault("DEFAULT_APP_TARGET_SDK", g["PLATFORM_SDK_VERSION"])
78        g.setdefault("PLATFORM_VNDK_VERSION", g["PLATFORM_SDK_VERSION"])
79    else:
80        g.setdefault("PLATFORM_VERSION", version_codename)
81        g.setdefault("PLATFORM_PREVIEW_SDK_VERSION", 1)
82        g.setdefault("DEFAULT_APP_TARGET_SDK", version_codename)
83        g.setdefault("PLATFORM_VNDK_VERSION", version_codename)
84
85    g.setdefault("PLATFORM_SYSTEMSDK_MIN_VERSION", 28)
86    versions = [str(i) for i in range(g["PLATFORM_SYSTEMSDK_MIN_VERSION"], g["PLATFORM_SDK_VERSION"] + 1)]
87    versions.append(version_codename)
88    g["PLATFORM_SYSTEMSDK_VERSIONS"] = sorted(versions)
89
90    #  Used to indicate the security patch that has been applied to the device.
91    #  It must signify that the build includes all security patches issued up through the designated Android Public Security Bulletin.
92    #  It must be of the form "YYYY-MM-DD" on production devices.
93    #  It must match one of the Android Security Patch Level strings of the Public Security Bulletins.
94    #  If there is no $PLATFORM_SECURITY_PATCH set, keep it empty.
95    g.setdefault("PLATFORM_SECURITY_PATCH", "2021-03-05")
96    dt = 'TZ="GMT" %s' % g["PLATFORM_SECURITY_PATCH"]
97    g.setdefault("PLATFORM_SECURITY_PATCH_TIMESTAMP", rblf_shell("date -d '%s' +%%s" % dt))
98
99    # Used to indicate the base os applied to the device. Can be an arbitrary string, but must be a single word.
100    # If there is no $PLATFORM_BASE_OS set, keep it empty.
101    g.setdefault("PLATFORM_BASE_OS", "")
102
103    # Used to signify special builds.  E.g., branches and/or releases, like "M5-RC7".  Can be an arbitrary string, but
104    # must be a single word and a valid file name. If there is no BUILD_ID set, make it obvious.
105    g.setdefault("BUILD_ID", "UNKNOWN")
106
107    # BUILD_NUMBER should be set to the source control value that represents the current state of the source code.
108    # E.g., a perforce changelist number or a git hash.  Can be an arbitrary string (to allow for source control that
109    # uses something other than numbers), but must be a single word and a valid file name.
110    #
111    # If no BUILD_NUMBER is set, create a useful "I am an engineering build from this date/time" value.  Make it start
112    # with a non-digit so that anyone trying to parse it as an integer will probably get "0".
113    g.setdefault("BUILD_NUMBER", "eng.%s.%s" % (g["USER"], "TIMESTAMP"))
114
115    # Used to set minimum supported target sdk version. Apps targeting SDK version lower than the set value will result
116    # in a warning being shown when any activity from the app is started.
117    g.setdefault("PLATFORM_MIN_SUPPORTED_TARGET_SDK_VERSION", 23)
118
119def init(g):
120    """Initializes globals.
121
122    The code is the Starlark counterpart of the contents of the
123    envsetup.mk file.
124    Args:
125        g: globals dictionary
126    """
127    all_versions = _all_versions()
128    _versions_default(g, all_versions)
129    for v in all_versions:
130        g["IS_AT_LEAST" + v] = True
131        if v == g["TARGET_PLATFORM_VERSION"]:
132            break
133
134    # ---------------------------------------------------------------
135    # If you update the build system such that the environment setup or buildspec.mk need to be updated,
136    # increment this number, and people who haven't re-run those will have to do so before they can build.
137    # Make sure to also update the corresponding value in buildspec.mk.default and envsetup.sh.
138    g["CORRECT_BUILD_ENV_SEQUENCE_NUMBER"] = 13
139
140    g.setdefault("TARGET_PRODUCT", "aosp_arm")
141    g.setdefault("TARGET_BUILD_VARIANT", "eng")
142
143    g.setdefault("TARGET_BUILD_APPS", [])
144    g["TARGET_BUILD_UNBUNDLED"] = (g["TARGET_BUILD_APPS"] != []) or (getattr(g, "TARGET_BUILD_UNBUNDLED_IMAGE", "") != "")
145
146    # ---------------------------------------------------------------
147    # Set up configuration for host machine.  We don't do cross-compiles except for arm, so the HOST
148    # is whatever we are running on.
149    host = rblf_shell("uname -sm")
150    if host.find("Linux") >= 0:
151        g["HOST_OS"] = "linux"
152    elif host.find("Darwin") >= 0:
153        g["HOST_OS"] = "darwin"
154    else:
155        fail("Cannot run on %s OS" % host)
156
157    # TODO(asmundak): set g.HOST_OS_EXTRA
158
159    g["BUILD_OS"] = g["HOST_OS"]
160
161    # TODO(asmundak): check cross-OS build
162
163    if host.find("x86_64") >= 0:
164        g["HOST_ARCH"] = "x86_64"
165        g["HOST_2ND_ARCH"] = "x86"
166        g["HOST_IS_64_BIT"] = True
167    elif host.find("i686") >= 0 or host.find("x86") >= 0:
168        fail("Building on a 32-bit x86 host is not supported: %s" % host)
169    elif g["HOST_OS"] == "darwin":
170        g["HOST_2ND_ARCH"] = ""
171
172    g["HOST_2ND_ARCH_VAR_PREFIX"] = "2ND_"
173    g["HOST_2ND_ARCH_MODULE_SUFFIX"] = "_32"
174    g["HOST_CROSS_2ND_ARCH_VAR_PREFIX"] = "2ND_"
175    g["HOST_CROSS_2ND_ARCH_MODULE_SUFFIX"] = "_64"
176    g["TARGET_2ND_ARCH_VAR_PREFIX"] = "2ND_"
177
178    # TODO(asmundak): envsetup.mk lines 216-226:
179    # convert combo-related stuff from combo/select.mk
180
181    # on windows, the tools have .exe at the end, and we depend on the
182    # host config stuff being done first
183    g["BUILD_ARCH"] = g["HOST_ARCH"]
184    g["BUILD_2ND_ARCH"] = g["HOST_2ND_ARCH"]
185
186    # the host build defaults to release, and it must be release or debug
187    g.setdefault("HOST_BUILD_TYPE", "release")
188    if g["HOST_BUILD_TYPE"] not in ["release", "debug"]:
189        fail("HOST_BUILD_TYPE must be either release or debug, not '%s'" % g["HOST_BUILD_TYPE"])
190
191    # TODO(asmundak): there is more stuff in envsetup.mk lines 249-292, but
192    # it does not seem to affect product configuration. Revisit this.
193
194    g["ART_APEX_JARS"] = [
195        "com.android.art:core-oj",
196        "com.android.art:core-libart",
197        "com.android.art:okhttp",
198        "com.android.art:bouncycastle",
199        "com.android.art:apache-xml",
200    ]
201
202    if g.get("TARGET_BUILD_TYPE", "") != "debug":
203        g["TARGET_BUILD_TYPE"] = "release"
204
205v_default = "SP1A"
206v_min = "SP1A"
207v_max = "SP1A"
208