Bug 1639153 - Part 6.6: Add tls dependency for truncate i32. r=lth
[gecko.git] / build / variables.py
blob01ae2f0d8bc28606d47252b8b4673682af1b0a2f
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/.
5 from __future__ import print_function, unicode_literals
7 import os
8 import subprocess
9 import sys
10 from datetime import datetime
12 SOURCESTAMP_FILENAME = 'sourcestamp.txt'
15 def buildid_header(output):
16 buildid = os.environ.get('MOZ_BUILD_DATE')
17 if buildid and len(buildid) != 14:
18 print('Ignoring invalid MOZ_BUILD_DATE: %s' % buildid, file=sys.stderr)
19 buildid = None
20 if not buildid:
21 buildid = datetime.now().strftime('%Y%m%d%H%M%S')
22 output.write("#define MOZ_BUILDID %s\n" % buildid)
25 def get_program_output(*command):
26 try:
27 with open(os.devnull) as stderr:
28 return subprocess.check_output(command, stderr=stderr,
29 universal_newlines=True)
30 except Exception:
31 return ''
34 def get_hg_info(workdir):
35 repo = get_program_output('hg', '-R', workdir, 'path', 'default')
36 if repo:
37 repo = repo.strip()
38 if repo.startswith('ssh://'):
39 repo = 'https://' + repo[6:]
40 repo = repo.rstrip('/')
42 changeset = get_hg_changeset(workdir)
44 return repo, changeset
47 def get_hg_changeset(path):
48 return get_program_output('hg', '-R', path, 'parent', '--template={node}')
51 def get_info_from_sourcestamp(sourcestamp_path):
52 """Read the repository and changelog information from the sourcestamp
53 file. This assumes that the file exists and returns the results as a list
54 (either strings or None in case of error).
55 """
57 # Load the content of the file.
58 lines = None
59 with open(sourcestamp_path) as f:
60 lines = f.read().splitlines()
62 # Parse the repo and the changeset. The sourcestamp file is supposed to
63 # contain two lines: the first is the build id and the second is the source
64 # URL.
65 if len(lines) != 2 or not lines[1].startswith('http'):
66 # Just return if the file doesn't contain what we expect.
67 return None, None
69 # Return the repo and the changeset.
70 return lines[1].split('/rev/')
73 def source_repo_header(output):
74 # We allow the source repo and changeset to be specified via the
75 # environment (see configure)
76 import buildconfig
77 repo = buildconfig.substs.get('MOZ_SOURCE_REPO')
78 changeset = buildconfig.substs.get('MOZ_SOURCE_CHANGESET')
79 source = ''
81 if not repo:
82 sourcestamp_path = os.path.join(
83 buildconfig.topsrcdir, SOURCESTAMP_FILENAME)
84 if os.path.exists(os.path.join(buildconfig.topsrcdir, '.hg')):
85 repo, changeset = get_hg_info(buildconfig.topsrcdir)
86 elif os.path.exists(sourcestamp_path):
87 repo, changeset = get_info_from_sourcestamp(sourcestamp_path)
88 elif not changeset:
89 changeset = get_hg_changeset(buildconfig.topsrcdir)
90 if not changeset:
91 raise Exception('could not resolve changeset; '
92 'try setting MOZ_SOURCE_CHANGESET')
94 if changeset:
95 output.write('#define MOZ_SOURCE_STAMP %s\n' % changeset)
97 if repo and buildconfig.substs.get('MOZ_INCLUDE_SOURCE_INFO'):
98 source = '%s/rev/%s' % (repo, changeset)
99 output.write('#define MOZ_SOURCE_REPO %s\n' % repo)
100 output.write('#define MOZ_SOURCE_URL %s\n' % source)
103 def main(args):
104 if (len(args)):
105 func = globals().get(args[0])
106 if func:
107 return func(sys.stdout, *args[1:])
110 if __name__ == '__main__':
111 sys.exit(main(sys.argv[1:]))