Merge pull request #325 from luvit/fix-path-magic
[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
57     filename = os.path.basename(src)
58     if filename == 'browse.cc':  # Depends on generated header.
59         continue
60
61     if sys.platform.startswith('win32'):
62         if filename == 'subprocess.cc':
63             continue
64     else:
65         if src.endswith('-win32.cc'):
66             continue
67
68     sources.append(src)
69
70 if sys.platform.startswith('win32'):
71     sources.append('src/getopt.c')
72
73 vcdir = os.environ.get('VCINSTALLDIR')
74 if vcdir:
75     args = [os.path.join(vcdir, 'bin', 'cl.exe'), '/nologo', '/EHsc', '/DNOMINMAX']
76 else:
77     args = shlex.split(os.environ.get('CXX', 'g++'))
78     args.extend(['-Wno-deprecated',
79                  '-DNINJA_PYTHON="' + sys.executable + '"',
80                  '-DNINJA_BOOTSTRAP'])
81 args.extend(cflags)
82 args.extend(ldflags)
83 binary = 'ninja.bootstrap'
84 if sys.platform.startswith('win32'):
85     binary = 'ninja.bootstrap.exe'
86 args.extend(sources)
87 if vcdir:
88     args.extend(['/link', '/out:' + binary])
89 else:
90     args.extend(['-o', binary])
91
92 if options.verbose:
93     print ' '.join(args)
94
95 run(args)
96
97 verbose = []
98 if options.verbose:
99     verbose = ['-v']
100
101 print 'Building ninja using itself...'
102 run([sys.executable, 'configure.py'] + conf_args)
103 run(['./' + binary] + verbose)
104 os.unlink(binary)
105
106 print 'Done!'