Add two bits of UMA docs.
[chromium-blink-merge.git] / tools / compile_test / compile_test.py
blobbbda4abfe54f327aabede26f400bebd5e1376d82
1 #!/usr/bin/env python
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """
7 Tries to compile given code, produces different output depending on success.
9 This is similar to checks done by ./configure scripts.
10 """
13 import optparse
14 import os
15 import shutil
16 import subprocess
17 import sys
18 import tempfile
21 def DoMain(argv):
22 parser = optparse.OptionParser()
23 parser.add_option('--code')
24 parser.add_option('--run-linker', action='store_true')
25 parser.add_option('--on-success', default='')
26 parser.add_option('--on-failure', default='')
28 options, args = parser.parse_args(argv)
30 if not options.code:
31 parser.error('Missing required --code switch.')
33 # The environment variable might expand to a string with spaces,
34 # e.g. "ccache g++". Convert it to a list suitable for argv.
35 cxx = os.environ.get('CXX', 'g++').split()
37 tmpdir = tempfile.mkdtemp()
38 try:
39 cxx_path = os.path.join(tmpdir, 'test.cc')
40 with open(cxx_path, 'w') as f:
41 f.write(options.code.decode('string-escape'))
43 o_path = os.path.join(tmpdir, 'test.o')
45 cxx_cmdline = cxx + [cxx_path, '-o', o_path]
46 if not options.run_linker:
47 cxx_cmdline.append('-c')
48 # Pass remaining arguments to the compiler.
49 cxx_cmdline += args
50 cxx_popen = subprocess.Popen(cxx_cmdline,
51 stdout=subprocess.PIPE,
52 stderr=subprocess.PIPE)
53 cxx_stdout, cxx_stderr = cxx_popen.communicate()
54 if cxx_popen.returncode == 0:
55 print options.on_success
56 else:
57 print options.on_failure
58 finally:
59 shutil.rmtree(tmpdir)
61 return 0
64 if __name__ == '__main__':
65 sys.exit(DoMain(sys.argv[1:]))