| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """ | 6 """ |
| 7 Tries to compile given code, produces different output depending on success. | 7 Tries to compile given code, produces different output depending on success. |
| 8 | 8 |
| 9 This is similar to checks done by ./configure scripts. | 9 This is similar to checks done by ./configure scripts. |
| 10 """ | 10 """ |
| 11 | 11 |
| 12 | 12 |
| 13 import optparse | 13 import optparse |
| 14 import os | 14 import os |
| 15 import shutil | 15 import shutil |
| 16 import subprocess | 16 import subprocess |
| 17 import sys | 17 import sys |
| 18 import tempfile | 18 import tempfile |
| 19 | 19 |
| 20 | 20 |
| 21 def DoMain(argv): | 21 def DoMain(argv): |
| 22 parser = optparse.OptionParser() | 22 parser = optparse.OptionParser() |
| 23 parser.add_option('--code') | 23 parser.add_option('--code') |
| 24 parser.add_option('--run-linker', action='store_true') |
| 24 parser.add_option('--on-success', default='') | 25 parser.add_option('--on-success', default='') |
| 25 parser.add_option('--on-failure', default='') | 26 parser.add_option('--on-failure', default='') |
| 26 | 27 |
| 27 options, args = parser.parse_args(argv) | 28 options, args = parser.parse_args(argv) |
| 28 | 29 |
| 29 if not options.code: | 30 if not options.code: |
| 30 parser.error('Missing required --code switch.') | 31 parser.error('Missing required --code switch.') |
| 31 | 32 |
| 32 cxx = os.environ.get('CXX', 'g++') | 33 cxx = os.environ.get('CXX', 'g++') |
| 33 | 34 |
| 34 tmpdir = tempfile.mkdtemp() | 35 tmpdir = tempfile.mkdtemp() |
| 35 try: | 36 try: |
| 36 cxx_path = os.path.join(tmpdir, 'test.cc') | 37 cxx_path = os.path.join(tmpdir, 'test.cc') |
| 37 with open(cxx_path, 'w') as f: | 38 with open(cxx_path, 'w') as f: |
| 38 f.write(options.code.decode('string-escape')) | 39 f.write(options.code.decode('string-escape')) |
| 39 | 40 |
| 40 o_path = os.path.join(tmpdir, 'test.o') | 41 o_path = os.path.join(tmpdir, 'test.o') |
| 41 | 42 |
| 42 cxx_popen = subprocess.Popen([cxx, cxx_path, '-o', o_path, '-c'], | 43 cxx_cmdline = [cxx, cxx_path, '-o', o_path] |
| 44 if not options.run_linker: |
| 45 cxx_cmdline.append('-c') |
| 46 # Pass remaining arguments to the compiler. |
| 47 cxx_cmdline += args |
| 48 cxx_popen = subprocess.Popen(cxx_cmdline, |
| 43 stdout=subprocess.PIPE, | 49 stdout=subprocess.PIPE, |
| 44 stderr=subprocess.PIPE) | 50 stderr=subprocess.PIPE) |
| 45 cxx_stdout, cxx_stderr = cxx_popen.communicate() | 51 cxx_stdout, cxx_stderr = cxx_popen.communicate() |
| 46 if cxx_popen.returncode == 0: | 52 if cxx_popen.returncode == 0: |
| 47 print options.on_success | 53 print options.on_success |
| 48 else: | 54 else: |
| 49 print options.on_failure | 55 print options.on_failure |
| 50 finally: | 56 finally: |
| 51 shutil.rmtree(tmpdir) | 57 shutil.rmtree(tmpdir) |
| 52 | 58 |
| 53 return 0 | 59 return 0 |
| 54 | 60 |
| 55 | 61 |
| 56 if __name__ == '__main__': | 62 if __name__ == '__main__': |
| 57 sys.exit(DoMain(sys.argv[1:])) | 63 sys.exit(DoMain(sys.argv[1:])) |
| OLD | NEW |