| 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 """ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 parser.add_option('--code') | 23 parser.add_option('--code') |
| 24 parser.add_option('--run-linker', action='store_true') | 24 parser.add_option('--run-linker', action='store_true') |
| 25 parser.add_option('--on-success', default='') | 25 parser.add_option('--on-success', default='') |
| 26 parser.add_option('--on-failure', default='') | 26 parser.add_option('--on-failure', default='') |
| 27 | 27 |
| 28 options, args = parser.parse_args(argv) | 28 options, args = parser.parse_args(argv) |
| 29 | 29 |
| 30 if not options.code: | 30 if not options.code: |
| 31 parser.error('Missing required --code switch.') | 31 parser.error('Missing required --code switch.') |
| 32 | 32 |
| 33 cxx = os.environ.get('CXX', 'g++') | 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() |
| 34 | 36 |
| 35 tmpdir = tempfile.mkdtemp() | 37 tmpdir = tempfile.mkdtemp() |
| 36 try: | 38 try: |
| 37 cxx_path = os.path.join(tmpdir, 'test.cc') | 39 cxx_path = os.path.join(tmpdir, 'test.cc') |
| 38 with open(cxx_path, 'w') as f: | 40 with open(cxx_path, 'w') as f: |
| 39 f.write(options.code.decode('string-escape')) | 41 f.write(options.code.decode('string-escape')) |
| 40 | 42 |
| 41 o_path = os.path.join(tmpdir, 'test.o') | 43 o_path = os.path.join(tmpdir, 'test.o') |
| 42 | 44 |
| 43 cxx_cmdline = [cxx, cxx_path, '-o', o_path] | 45 cxx_cmdline = cxx + [cxx_path, '-o', o_path] |
| 44 if not options.run_linker: | 46 if not options.run_linker: |
| 45 cxx_cmdline.append('-c') | 47 cxx_cmdline.append('-c') |
| 46 # Pass remaining arguments to the compiler. | 48 # Pass remaining arguments to the compiler. |
| 47 cxx_cmdline += args | 49 cxx_cmdline += args |
| 48 cxx_popen = subprocess.Popen(cxx_cmdline, | 50 cxx_popen = subprocess.Popen(cxx_cmdline, |
| 49 stdout=subprocess.PIPE, | 51 stdout=subprocess.PIPE, |
| 50 stderr=subprocess.PIPE) | 52 stderr=subprocess.PIPE) |
| 51 cxx_stdout, cxx_stderr = cxx_popen.communicate() | 53 cxx_stdout, cxx_stderr = cxx_popen.communicate() |
| 52 if cxx_popen.returncode == 0: | 54 if cxx_popen.returncode == 0: |
| 53 print options.on_success | 55 print options.on_success |
| 54 else: | 56 else: |
| 55 print options.on_failure | 57 print options.on_failure |
| 56 finally: | 58 finally: |
| 57 shutil.rmtree(tmpdir) | 59 shutil.rmtree(tmpdir) |
| 58 | 60 |
| 59 return 0 | 61 return 0 |
| 60 | 62 |
| 61 | 63 |
| 62 if __name__ == '__main__': | 64 if __name__ == '__main__': |
| 63 sys.exit(DoMain(sys.argv[1:])) | 65 sys.exit(DoMain(sys.argv[1:])) |
| OLD | NEW |