Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / tools / compile_test / compile_test.py
bloba52c0720209142af858d656a071ca43328dd3b16
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('--on-success', default='')
25 parser.add_option('--on-failure', default='')
27 options, args = parser.parse_args(argv)
29 if not options.code:
30 parser.error('Missing required --code switch.')
32 cxx = os.environ.get('CXX', 'g++')
34 tmpdir = tempfile.mkdtemp()
35 try:
36 cxx_path = os.path.join(tmpdir, 'test.cc')
37 with open(cxx_path, 'w') as f:
38 f.write(options.code.decode('string-escape'))
40 o_path = os.path.join(tmpdir, 'test.o')
42 cxx_popen = subprocess.Popen([cxx, cxx_path, '-o', o_path, '-c'],
43 stdout=subprocess.PIPE,
44 stderr=subprocess.PIPE)
45 cxx_stdout, cxx_stderr = cxx_popen.communicate()
46 if cxx_popen.returncode == 0:
47 print options.on_success
48 else:
49 print options.on_failure
50 finally:
51 shutil.rmtree(tmpdir)
53 return 0
56 if __name__ == '__main__':
57 sys.exit(DoMain(sys.argv[1:]))