| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
import io |
| 6 |
import os |
| 7 |
import sys |
| 8 |
from datetime import datetime |
| 9 |
|
| 10 |
import buildconfig |
| 11 |
from mozbuild.preprocessor import Preprocessor |
| 12 |
from variables import get_buildid |
| 13 |
|
| 14 |
TEMPLATE = """ |
| 15 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 16 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 17 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 18 |
|
| 19 |
#include<winuser.h> |
| 20 |
#include<winver.h> |
| 21 |
|
| 22 |
// Note: if you contain versioning information in an included |
| 23 |
// RC script, it will be discarded |
| 24 |
// Use module.ver to explicitly set these values |
| 25 |
|
| 26 |
// Do not edit this file. Changes won't affect the build. |
| 27 |
|
| 28 |
{include} |
| 29 |
|
| 30 |
Identity LimitedAccessFeature {{ L"{lafidentity}_pcsmm0jrprpb2" }} |
| 31 |
|
| 32 |
|
| 33 |
///////////////////////////////////////////////////////////////////////////// |
| 34 |
// |
| 35 |
// Version |
| 36 |
// |
| 37 |
|
| 38 |
1 VERSIONINFO |
| 39 |
FILEVERSION {fileversion} |
| 40 |
PRODUCTVERSION {productversion} |
| 41 |
FILEFLAGSMASK 0x3fL |
| 42 |
FILEFLAGS {fileflags} |
| 43 |
FILEOS VOS__WINDOWS32 |
| 44 |
FILETYPE VFT_DLL |
| 45 |
FILESUBTYPE 0x0L |
| 46 |
BEGIN |
| 47 |
BLOCK "StringFileInfo" |
| 48 |
BEGIN |
| 49 |
BLOCK "000004b0" |
| 50 |
BEGIN |
| 51 |
VALUE "Comments", "{comment}" |
| 52 |
VALUE "LegalCopyright", "{copyright}" |
| 53 |
VALUE "CompanyName", "{company}" |
| 54 |
VALUE "FileDescription", "{description}" |
| 55 |
VALUE "FileVersion", "{mfversion}" |
| 56 |
VALUE "ProductVersion", "{mpversion}" |
| 57 |
VALUE "InternalName", "{module}" |
| 58 |
VALUE "LegalTrademarks", "{trademarks}" |
| 59 |
VALUE "OriginalFilename", "{binary}" |
| 60 |
VALUE "ProductName", "{productname}" |
| 61 |
VALUE "BuildID", "{buildid}" |
| 62 |
END |
| 63 |
END |
| 64 |
BLOCK "VarFileInfo" |
| 65 |
BEGIN |
| 66 |
VALUE "Translation", 0x0, 1200 |
| 67 |
END |
| 68 |
END |
| 69 |
|
| 70 |
""" |
| 71 |
|
| 72 |
|
| 73 |
class SystemClockDiscrepancy(Exception): |
| 74 |
"""Represents an error encountered during the build when determining delta between the build time and |
| 75 |
the commit time of milestone.txt via VCS.""" |
| 76 |
|
| 77 |
|
| 78 |
def preprocess(path, defines): |
| 79 |
pp = Preprocessor(defines=defines, marker="%") |
| 80 |
pp.context.update(defines) |
| 81 |
pp.out = io.StringIO() |
| 82 |
pp.do_filter("substitution") |
| 83 |
pp.do_include(open(path, encoding="latin1")) |
| 84 |
pp.out.seek(0) |
| 85 |
return pp.out |
| 86 |
|
| 87 |
|
| 88 |
def parse_module_ver(path, defines): |
| 89 |
result = {} |
| 90 |
for line in preprocess(path, defines): |
| 91 |
content, *comment = line.split("#", 1) |
| 92 |
if not content.strip(): |
| 93 |
continue |
| 94 |
entry, value = content.split("=", 1) |
| 95 |
result[entry.strip()] = value.strip() |
| 96 |
return result |
| 97 |
|
| 98 |
|
| 99 |
def last_winversion_segment(buildid, app_version_display): |
| 100 |
""" |
| 101 |
The last segment needs to fit into a 16 bit number. We also need to |
| 102 |
encode what channel this version is from. We'll do this by using 2 bits |
| 103 |
to encode the channel, and 14 bits to encode the number of hours since |
| 104 |
the 'config/milestone.txt' was modified (relative to the build time). |
| 105 |
|
| 106 |
This gives us about ~682 days of release hours that will yield a unique |
| 107 |
file version for a specific channel/milestone combination. This should suffice |
| 108 |
since the main problem we're trying to address is uniqueness in CI for a |
| 109 |
channel/milestone over about a 1 month period. |
| 110 |
|
| 111 |
If two builds for the same channel/milestone are done in CI within the same |
| 112 |
hour there's still a chance for overlap and issues with AV as originally |
| 113 |
reported in https://bugzilla.mozilla.org/show_bug.cgi?id=1872242 |
| 114 |
|
| 115 |
If a build is done after the ~682 day window of uniqueness, the value for |
| 116 |
this segment will always be the maximum value for the channel (no overflow). |
| 117 |
It will also always be the maximum value for the channel if a build is done |
| 118 |
from a source distribution, because we cannot determine the milestone date |
| 119 |
change without a VCS. |
| 120 |
|
| 121 |
If necessary, you can decode the result of this function. You just need to |
| 122 |
do integer division and divide it by 4. The quotient will be the delta |
| 123 |
between the milestone bump and the build time, and the remainder will be |
| 124 |
the channel digit. Refer to the if/else chain near the end of the function |
| 125 |
for what channels the channel digits map to. |
| 126 |
|
| 127 |
Example: |
| 128 |
Encoded: 1544 |
| 129 |
|
| 130 |
1554 / 4 = |
| 131 |
Quotient: 388 |
| 132 |
Remainder: 2 (ESR) |
| 133 |
""" |
| 134 |
from mozversioncontrol import MissingVCSTool, get_repository_object |
| 135 |
|
| 136 |
# Max 16 bit value with 2 most significant bits as 0 (reserved so we can |
| 137 |
# shift later and make room for the channel digit). |
| 138 |
MAX_VALUE = 0x3FFF |
| 139 |
|
| 140 |
try: |
| 141 |
import time |
| 142 |
from datetime import timedelta, timezone |
| 143 |
from pathlib import Path |
| 144 |
|
| 145 |
topsrcdir = buildconfig.topsrcdir |
| 146 |
repo = get_repository_object(topsrcdir) |
| 147 |
|
| 148 |
milestone_time = repo.get_last_modified_time_for_file( |
| 149 |
Path(topsrcdir) / "config" / "milestone.txt" |
| 150 |
) |
| 151 |
# The buildid doesn't include timezone info, but the milestone_time does. |
| 152 |
# We're building on this machine, so we just need the system local timezone |
| 153 |
# added to a buildid constructed datetime object to make a valid comparison. |
| 154 |
local_tz = timezone(timedelta(seconds=time.timezone)) |
| 155 |
buildid_time = datetime.strptime(buildid, "%Y%m%d%H%M%S").replace( |
| 156 |
tzinfo=local_tz |
| 157 |
) |
| 158 |
|
| 159 |
time_delta = buildid_time - milestone_time |
| 160 |
# If the time delta is negative it means that the system clock on the build machine is |
| 161 |
# significantly far ahead. If we're in CI we'll raise an error, since this number mostly |
| 162 |
# only matters for doing releases in CI. If we're not in CI, we'll just set the value to |
| 163 |
# the maximum instead of needlessly interrupting the build of a user with fast/intentionally |
| 164 |
# modified system clock. |
| 165 |
if time_delta.total_seconds() < 0: |
| 166 |
if "MOZ_AUTOMATION" in os.environ: |
| 167 |
raise SystemClockDiscrepancy( |
| 168 |
f"The system clock is ahead of the milestone.txt commit time " |
| 169 |
f"by at least {int(time_delta.total_seconds())} seconds (Since " |
| 170 |
f"the milestone commit must come before the build starts). This " |
| 171 |
f"is a problem because use a relative time difference to determine the" |
| 172 |
f"file_version (and it can't be negative), so we cannot proceed. \n\n" |
| 173 |
f"Please ensure the system clock is correct." |
| 174 |
) |
| 175 |
else: |
| 176 |
hours_from_milestone_date = MAX_VALUE |
| 177 |
else: |
| 178 |
# Convert from seconds to hours |
| 179 |
# When a build is done more than ~682 days in the future, we can't represent the value. |
| 180 |
# We'll always set the value to the maximum value instead of overflowing. |
| 181 |
hours_from_milestone_date = min( |
| 182 |
int(time_delta.total_seconds() / 3600), MAX_VALUE |
| 183 |
) |
| 184 |
except MissingVCSTool: |
| 185 |
# If we're here we can't use the VCS to determine the time differential, so |
| 186 |
# we'll just set it to the maximum value instead of doing something weird. |
| 187 |
hours_from_milestone_date = MAX_VALUE |
| 188 |
pass |
| 189 |
|
| 190 |
if buildconfig.substs.get("NIGHTLY_BUILD"): |
| 191 |
# Nightly |
| 192 |
channel_digit = 0 |
| 193 |
elif "b" in app_version_display: |
| 194 |
# Beta |
| 195 |
channel_digit = 1 |
| 196 |
elif buildconfig.substs.get("MOZ_ESR"): |
| 197 |
# ESR |
| 198 |
channel_digit = 2 |
| 199 |
else: |
| 200 |
# Release |
| 201 |
channel_digit = 3 |
| 202 |
# left shift to make room to encode the channel digit |
| 203 |
return str((hours_from_milestone_date << 2) + channel_digit) |
| 204 |
|
| 205 |
|
| 206 |
def digits_only(s): |
| 207 |
for l in range(len(s), 0, -1): |
| 208 |
if s[:l].isdigit(): |
| 209 |
return s[:l] |
| 210 |
return "0" |
| 211 |
|
| 212 |
|
| 213 |
def split_and_normalize_version(version, len): |
| 214 |
return ([digits_only(x) for x in version.split(".")] + ["0"] * len)[:len] |
| 215 |
|
| 216 |
|
| 217 |
def has_manifest(module_rc, manifest_id): |
| 218 |
for lineFromInput in module_rc.splitlines(): |
| 219 |
line = lineFromInput.split(None, 2) |
| 220 |
if len(line) < 2: |
| 221 |
continue |
| 222 |
id, what, *rest = line |
| 223 |
if id == manifest_id and what in ("24", "RT_MANIFEST"): |
| 224 |
return True |
| 225 |
return False |
| 226 |
|
| 227 |
|
| 228 |
def generate_module_rc(binary="", rcinclude=None): |
| 229 |
deps = set() |
| 230 |
buildid = get_buildid() |
| 231 |
milestone = buildconfig.substs["GRE_MILESTONE"] |
| 232 |
app_version = buildconfig.substs.get("MOZ_APP_VERSION") or milestone |
| 233 |
app_version_display = buildconfig.substs.get("MOZ_APP_VERSION_DISPLAY") |
| 234 |
app_winversion = ",".join(split_and_normalize_version(app_version, 4)) |
| 235 |
milestone_winversion = ",".join( |
| 236 |
split_and_normalize_version(milestone, 3) |
| 237 |
+ [last_winversion_segment(buildid, app_version_display)] |
| 238 |
) |
| 239 |
display_name = buildconfig.substs.get("MOZ_APP_DISPLAYNAME", "Mozilla") |
| 240 |
|
| 241 |
milestone_string = milestone |
| 242 |
|
| 243 |
flags = ["0"] |
| 244 |
if buildconfig.substs.get("MOZ_DEBUG"): |
| 245 |
flags.append("VS_FF_DEBUG") |
| 246 |
milestone_string += " Debug" |
| 247 |
if not buildconfig.substs.get("MOZILLA_OFFICIAL"): |
| 248 |
flags.append("VS_FF_PRIVATEBUILD") |
| 249 |
if buildconfig.substs.get("NIGHTLY_BUILD"): |
| 250 |
flags.append("VS_FF_PRERELEASE") |
| 251 |
|
| 252 |
defines = { |
| 253 |
"MOZ_APP_DISPLAYNAME": display_name, |
| 254 |
"MOZ_APP_VERSION": app_version, |
| 255 |
"MOZ_APP_WINVERSION": app_winversion, |
| 256 |
} |
| 257 |
|
| 258 |
relobjdir = os.path.relpath(".", buildconfig.topobjdir) |
| 259 |
srcdir = os.path.join(buildconfig.topsrcdir, relobjdir) |
| 260 |
module_ver = os.path.join(srcdir, "module.ver") |
| 261 |
if os.path.exists(module_ver): |
| 262 |
deps.add(module_ver) |
| 263 |
overrides = parse_module_ver(module_ver, defines) |
| 264 |
else: |
| 265 |
overrides = {} |
| 266 |
|
| 267 |
if rcinclude: |
| 268 |
include = f"// From included resource {rcinclude}\n{preprocess(rcinclude, defines).read()}" |
| 269 |
else: |
| 270 |
include = "" |
| 271 |
|
| 272 |
# Set the identity field for the Limited Access Feature |
| 273 |
# Must match the Product Name used in limited_access_features.rs |
| 274 |
lafidentity = "MozillaFirefox" |
| 275 |
# lafidentity = "FirefoxBeta" |
| 276 |
# lafidentity = "FirefoxNightly" |
| 277 |
|
| 278 |
data = TEMPLATE.format( |
| 279 |
include=include, |
| 280 |
lafidentity=lafidentity, |
| 281 |
fileversion=overrides.get("WIN32_MODULE_FILEVERSION", milestone_winversion), |
| 282 |
productversion=overrides.get( |
| 283 |
"WIN32_MODULE_PRODUCTVERSION", milestone_winversion |
| 284 |
), |
| 285 |
fileflags=" | ".join(flags), |
| 286 |
comment=overrides.get("WIN32_MODULE_COMMENT", ""), |
| 287 |
copyright=overrides.get("WIN32_MODULE_COPYRIGHT", "License: MPL 2"), |
| 288 |
company=overrides.get("WIN32_MODULE_COMPANYNAME", "Mozilla Foundation"), |
| 289 |
description=overrides.get("WIN32_MODULE_DESCRIPTION", ""), |
| 290 |
mfversion=overrides.get("WIN32_MODULE_FILEVERSION_STRING", milestone_string), |
| 291 |
mpversion=overrides.get("WIN32_MODULE_PRODUCTVERSION_STRING", milestone_string), |
| 292 |
module=overrides.get("WIN32_MODULE_NAME", ""), |
| 293 |
trademarks=overrides.get("WIN32_MODULE_TRADEMARKS", "Mozilla"), |
| 294 |
binary=overrides.get("WIN32_MODULE_ORIGINAL_FILENAME", binary), |
| 295 |
productname=overrides.get("WIN32_MODULE_PRODUCTNAME", display_name), |
| 296 |
buildid=buildid, |
| 297 |
) |
| 298 |
|
| 299 |
manifest_id = "2" if binary.lower().endswith(".dll") else "1" |
| 300 |
if binary and not has_manifest(data, manifest_id): |
| 301 |
manifest_path = os.path.join(srcdir, binary + ".manifest") |
| 302 |
if os.path.exists(manifest_path): |
| 303 |
manifest_path = manifest_path.replace("\\", "\\\\") |
| 304 |
data += f'\n{manifest_id} RT_MANIFEST "{manifest_path}"\n' |
| 305 |
|
| 306 |
with open("{}.rc".format(binary or "module"), "w", encoding="latin1") as fh: |
| 307 |
fh.write(data) |
| 308 |
|
| 309 |
|
| 310 |
if __name__ == "__main__": |
| 311 |
generate_module_rc(*sys.argv[1:]) |