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