Merge pull request #313 from wolfp/restat_missing_input
[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 os.chdir(os.path.dirname(os.path.abspath(__file__)))
25
26 parser = OptionParser()
27 parser.add_option('--verbose', action='store_true',
28                   help='enable verbose build',)
29 (options, conf_args) = parser.parse_args()
30
31 def run(*args, **kwargs):
32     returncode = subprocess.call(*args, **kwargs)
33     if returncode != 0:
34         sys.exit(returncode)
35
36 # Compute system-specific CFLAGS/LDFLAGS as used in both in the below
37 # g++ call as well as in the later configure.py.
38 cflags = os.environ.get('CFLAGS', '').split()
39 ldflags = os.environ.get('LDFLAGS', '').split()
40 if sys.platform.startswith('freebsd'):
41     cflags.append('-I/usr/local/include')
42     ldflags.append('-L/usr/local/lib')
43
44 print 'Building ninja manually...'
45
46 try:
47     os.mkdir('build')
48 except OSError, e:
49     if e.errno != errno.EEXIST:
50         raise
51
52 sources = []
53 for src in glob.glob('src/*.cc'):
54     if src.endswith('test.cc') or src.endswith('.in.cc'):
55         continue
56     if src.endswith('bench.cc'):
57         continue
58
59     filename = os.path.basename(src)
60     if filename == 'browse.cc':  # Depends on generated header.
61         continue
62
63     if sys.platform.startswith('win32'):
64         if filename == 'subprocess.cc':
65             continue
66     else:
67         if src.endswith('-win32.cc'):
68             continue
69
70     sources.append(src)
71
72 if sys.platform.startswith('win32'):
73     sources.append('src/getopt.c')
74
75 vcdir = os.environ.get('VCINSTALLDIR')
76 if vcdir:
77     args = [os.path.join(vcdir, 'bin', 'cl.exe'), '/nologo', '/EHsc', '/DNOMINMAX']
78 else:
79     args = shlex.split(os.environ.get('CXX', 'g++'))
80     args.extend(['-Wno-deprecated',
81                  '-DNINJA_PYTHON="' + sys.executable + '"',
82                  '-DNINJA_BOOTSTRAP'])
83 args.extend(cflags)
84 args.extend(ldflags)
85 binary = 'ninja.bootstrap'
86 if sys.platform.startswith('win32'):
87     binary = 'ninja.bootstrap.exe'
88 args.extend(sources)
89 if vcdir:
90     args.extend(['/link', '/out:' + binary])
91 else:
92     args.extend(['-o', binary])
93
94 if options.verbose:
95     print ' '.join(args)
96
97 run(args)
98
99 verbose = []
100 if options.verbose:
101     verbose = ['-v']
102
103 print 'Building ninja using itself...'
104 run([sys.executable, 'configure.py'] + conf_args)
105 run(['./' + binary] + verbose)
106 os.unlink(binary)
107
108 print 'Done!'