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