Merge pull request #265 from mathstuf/dev/verbose-bootstrap
[platform/upstream/ninja.git] / bootstrap.py
1 #!/usr/bin/env python
2 # Copyright 2011 Google Inc. All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 from optparse import OptionParser
17 import sys
18 import os
19 import glob
20 import errno
21 import shlex
22 import subprocess
23
24 parser = OptionParser()
25 parser.add_option('--verbose', action='store_true',
26                   help='enable verbose build',)
27 (options, conf_args) = parser.parse_args()
28
29 def run(*args, **kwargs):
30     try:
31         subprocess.check_call(*args, **kwargs)
32     except subprocess.CalledProcessError, e:
33         sys.exit(e.returncode)
34
35 # Compute system-specific CFLAGS/LDFLAGS as used in both in the below
36 # g++ call as well as in the later configure.py.
37 cflags = os.environ.get('CFLAGS', '').split()
38 ldflags = os.environ.get('LDFLAGS', '').split()
39 if sys.platform.startswith('freebsd'):
40     cflags.append('-I/usr/local/include')
41     ldflags.append('-L/usr/local/lib')
42
43 print 'Building ninja manually...'
44
45 try:
46     os.mkdir('build')
47 except OSError, e:
48     if e.errno != errno.EEXIST:
49         raise
50
51 sources = []
52 for src in glob.glob('src/*.cc'):
53     if src.endswith('test.cc') or src.endswith('.in.cc'):
54         continue
55
56     filename = os.path.basename(src)
57     if filename == 'browse.cc':  # Depends on generated header.
58         continue
59
60     if sys.platform.startswith('win32'):
61         if filename == 'subprocess.cc':
62             continue
63     else:
64         if src.endswith('-win32.cc'):
65             continue
66
67     sources.append(src)
68
69 if sys.platform.startswith('win32'):
70     sources.append('src/getopt.c')
71
72 vcdir = os.environ.get('VCINSTALLDIR')
73 if vcdir:
74     args = [os.path.join(vcdir, 'bin', 'cl.exe'), '/nologo', '/EHsc', '/DNOMINMAX']
75 else:
76     args = shlex.split(os.environ.get('CXX', 'g++'))
77     args.extend(['-Wno-deprecated',
78                  '-DNINJA_PYTHON="' + sys.executable + '"',
79                  '-DNINJA_BOOTSTRAP'])
80 args.extend(cflags)
81 args.extend(ldflags)
82 binary = 'ninja.bootstrap'
83 if sys.platform.startswith('win32'):
84     binary = 'ninja.bootstrap.exe'
85 args.extend(sources)
86 if vcdir:
87     args.extend(['/link', '/out:' + binary])
88 else:
89     args.extend(['-o', binary])
90
91 if options.verbose:
92     print ' '.join(args)
93
94 run(args)
95
96 verbose = []
97 if options.verbose:
98     verbose = ['-v']
99
100 print 'Building ninja using itself...'
101 run([sys.executable, 'configure.py'] + conf_args)
102 run(['./' + binary] + verbose)
103 os.unlink(binary)
104
105 print 'Done!'