Debian Codegraph @

build/variables.py

raw
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 os
6 import subprocess
7 import sys
8 from datetime import datetime
9
10 SOURCESTAMP_FILENAME = "sourcestamp.txt"
11
12
13 def get_buildid():
14 import buildconfig
15
16 path = os.path.join(buildconfig.topobjdir, "buildid.h")
17 _define, _MOZ_BUILDID, buildid = open(path, encoding="utf-8").read().split()
18 return buildid
19
20
21 def buildid_header(output):
22 buildid = os.environ.get("MOZ_BUILD_DATE")
23 if buildid and len(buildid) != 14:
24 print("Ignoring invalid MOZ_BUILD_DATE: %s" % buildid, file=sys.stderr)
25 buildid = None
26 if not buildid:
27 buildid = datetime.now().strftime("%Y%m%d%H%M%S")
28 # If this output changes, be sure to update `get_buildid()`.
29 output.write("#define MOZ_BUILDID %s\n" % buildid)
30
31
32 def get_program_output(*command):
33 try:
34 with open(os.devnull) as stderr:
35 return subprocess.check_output(
36 command, stderr=stderr, universal_newlines=True
37 )
38 except Exception:
39 return ""
40
41
42 def get_hg_info(workdir):
43 repo = get_program_output("hg", "-R", workdir, "path", "default")
44 if repo:
45 repo = repo.strip()
46 if repo.startswith("ssh://"):
47 repo = "https://" + repo[6:]
48 repo = repo.rstrip("/")
49
50 changeset = get_hg_changeset(workdir)
51
52 return repo, changeset
53
54
55 def get_hg_changeset(path):
56 return get_program_output("hg", "-R", path, "parent", "--template={node}")
57
58
59 def get_info_from_sourcestamp(sourcestamp_path):
60 """Read the repository and changelog information from the sourcestamp
61 file. This assumes that the file exists and returns the results as a list
62 (either strings or None in case of error).
63 """
64
65 # Load the content of the file.
66 lines = None
67 with open(sourcestamp_path) as f:
68 lines = f.read().splitlines()
69
70 # Parse the repo and the changeset. The sourcestamp file is supposed to
71 # contain two lines: the first is the build id and the second is the source
72 # URL.
73 if len(lines) != 2 or not lines[1].startswith("http"):
74 # Just return if the file doesn't contain what we expect.
75 return None, None
76
77 # Return the repo and the changeset.
78 return lines[1].split("/rev/")
79
80
81 def source_repo_header(output):
82 # We allow the source repo and changeset to be specified via the
83 # environment (see configure)
84 import buildconfig
85
86 repo = buildconfig.substs.get("MOZ_SOURCE_REPO")
87 changeset = buildconfig.substs.get("MOZ_SOURCE_CHANGESET")
88 source = ""
89
90 if not repo:
91 sourcestamp_path = os.path.join(buildconfig.topsrcdir, SOURCESTAMP_FILENAME)
92 if os.path.exists(os.path.join(buildconfig.topsrcdir, ".hg")):
93 repo, changeset = get_hg_info(buildconfig.topsrcdir)
94 elif os.path.exists(sourcestamp_path):
95 repo, changeset = get_info_from_sourcestamp(sourcestamp_path)
96 elif not changeset:
97 changeset = get_hg_changeset(buildconfig.topsrcdir)
98 if not changeset:
99 raise Exception(
100 "could not resolve changeset; " "try setting MOZ_SOURCE_CHANGESET"
101 )
102
103 if changeset:
104 output.write("#define MOZ_SOURCE_STAMP %s\n" % changeset)
105
106 if repo and buildconfig.substs.get("MOZ_INCLUDE_SOURCE_INFO"):
107 source = "%s/rev/%s" % (repo, changeset)
108 output.write("#define MOZ_SOURCE_REPO %s\n" % repo)
109 output.write("#define MOZ_SOURCE_URL %s\n" % source)
110
111
112 def main(args):
113 if len(args):
114 func = globals().get(args[0])
115 if func:
116 return func(sys.stdout, *args[1:])
117
118
119 if __name__ == "__main__":
120 sys.exit(main(sys.argv[1:]))