Merge branch 'master' of github.com:martine/ninja
[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 src.endswith('-posix.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'),
78             '/nologo', '/EHsc', '/DNOMINMAX']
79 else:
80     args = shlex.split(os.environ.get('CXX', 'g++'))
81     cflags.extend(['-Wno-deprecated',
82                    '-DNINJA_PYTHON="' + sys.executable + '"',
83                    '-DNINJA_BOOTSTRAP'])
84     if sys.platform.startswith('win32'):
85         cflags.append('-D_WIN32_WINNT=0x0501')
86 args.extend(cflags)
87 args.extend(ldflags)
88 binary = 'ninja.bootstrap'
89 if sys.platform.startswith('win32'):
90     binary = 'ninja.bootstrap.exe'
91 args.extend(sources)
92 if vcdir:
93     args.extend(['/link', '/out:' + binary])
94 else:
95     args.extend(['-o', binary])
96
97 if options.verbose:
98     print ' '.join(args)
99
100 run(args)
101
102 verbose = []
103 if options.verbose:
104     verbose = ['-v']
105
106 print 'Building ninja using itself...'
107 run([sys.executable, 'configure.py'] + conf_args)
108 run(['./' + binary] + verbose)
109 os.unlink(binary)
110
111 print 'Done!'