3 # This script is run during configure, taking variables set in configure
4 # and producing a JSON file that describes some portions of the build
5 # configuration, such as the target OS and CPU.
7 # The output file is intended to be used as input to the mozinfo package.
8 from __future__
import with_statement
11 def build_dict(env
=os
.environ
):
13 Build a dict containing data about the build configuration from
17 # Check that all required variables are present first.
18 required
= ["TARGET_CPU", "OS_TARGET", "MOZ_WIDGET_TOOLKIT"]
19 missing
= [r
for r
in required
if r
not in env
]
21 raise Exception("Missing required environment variables: %s" %
25 known_os
= {"Linux": "linux",
32 # Allow unknown values, just lowercase them.
35 # Widget toolkit, just pass the value directly through.
36 d
["toolkit"] = env
["MOZ_WIDGET_TOOLKIT"]
40 # for universal mac builds, put in a special value
41 if d
["os"] == "mac" and "UNIVERSAL_BINARY" in env
and env
["UNIVERSAL_BINARY"] == "1":
42 p
= "universal-x86-x86_64"
44 # do some slight massaging for some values
45 #TODO: retain specific values in case someone wants them?
46 if p
.startswith("arm"):
48 elif re
.match("i[3-9]86", p
):
51 # hardcoded list of 64-bit CPUs
52 if p
in ["x86_64", "ppc64"]:
54 # hardcoded list of known 32-bit CPUs
55 elif p
in ["x86", "arm", "ppc"]:
57 # other CPUs will wind up with unknown bits
60 d
["debug"] = 'MOZ_DEBUG' in env
and env
['MOZ_DEBUG'] == '1'
63 d
["crashreporter"] = 'MOZ_CRASHREPORTER' in env
and env
['MOZ_CRASHREPORTER'] == '1'
66 #TODO: replace this with the json module when Python >= 2.6 is a requirement.
69 A class to serialize Python values into JSON-compatible representations.
71 def __init__(self
, v
):
72 if v
is not None and not (isinstance(v
,str) or isinstance(v
,bool) or isinstance(v
,int)):
73 raise Exception("Unhandled data type: %s" % type(v
))
78 if isinstance(self
.v
,bool):
79 return str(self
.v
).lower()
84 Return a JSON string of the dict |d|. Only handles a subset of Python
85 value types: bool, str, int, None.
88 for k
, v
in d
.iteritems():
92 def write_json(file, env
=os
.environ
):
94 Write JSON data about the configuration specified in |env|
95 to |file|, which may be a filename or file-like object.
96 See build_dict for information about what environment variables are used,
97 and what keys are produced.
99 s
= jsonify(build_dict(env
))
100 if isinstance(file, basestring
):
101 with
open(file, "w") as f
:
106 if __name__
== '__main__':
108 write_json(sys
.argv
[1] if len(sys
.argv
) > 1 else sys
.stdout
)
110 print >>sys
.stderr
, str(e
)