Add support for Python 3
[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 shutil
23 import subprocess
24
25 if sys.version_info[0] == 3:
26     import builtins
27     print_ = getattr(builtins, "print")
28 else:
29     def print_(*args):
30         sys.stdout.write(" ".join(str(x) for x in args))
31         sys.stdout.write("\n")
32
33 os.chdir(os.path.dirname(os.path.abspath(__file__)))
34
35 parser = OptionParser()
36 parser.add_option('--verbose', action='store_true',
37                   help='enable verbose build',)
38 parser.add_option('--x64', action='store_true',
39                   help='force 64-bit build (Windows)',)
40 (options, conf_args) = parser.parse_args()
41
42 def run(*args, **kwargs):
43     returncode = subprocess.call(*args, **kwargs)
44     if returncode != 0:
45         sys.exit(returncode)
46
47 # Compute system-specific CFLAGS/LDFLAGS as used in both in the below
48 # g++ call as well as in the later configure.py.
49 cflags = os.environ.get('CFLAGS', '').split()
50 ldflags = os.environ.get('LDFLAGS', '').split()
51 if sys.platform.startswith('freebsd'):
52     cflags.append('-I/usr/local/include')
53     ldflags.append('-L/usr/local/lib')
54
55 print_('Building ninja manually...')
56
57 try:
58     os.mkdir('build')
59 except OSError:
60     e = sys.exc_info()[1]
61     if e.errno != errno.EEXIST:
62         raise
63
64 sources = []
65 for src in glob.glob('src/*.cc'):
66     if src.endswith('test.cc') or src.endswith('.in.cc'):
67         continue
68     if src.endswith('bench.cc'):
69         continue
70
71     filename = os.path.basename(src)
72     if filename == 'browse.cc':  # Depends on generated header.
73         continue
74
75     if sys.platform.startswith('win32'):
76         if src.endswith('-posix.cc'):
77             continue
78     else:
79         if src.endswith('-win32.cc'):
80             continue
81
82     sources.append(src)
83
84 if sys.platform.startswith('win32'):
85     sources.append('src/getopt.c')
86
87 vcdir = os.environ.get('VCINSTALLDIR')
88 if vcdir:
89     if options.x64:
90         cl = [os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')]
91     else:
92         cl = [os.path.join(vcdir, 'bin', 'cl.exe')]
93     args = cl + ['/nologo', '/EHsc', '/DNOMINMAX']
94 else:
95     args = shlex.split(os.environ.get('CXX', 'g++'))
96     cflags.extend(['-Wno-deprecated',
97                    '-DNINJA_PYTHON="' + sys.executable + '"',
98                    '-DNINJA_BOOTSTRAP'])
99     if sys.platform.startswith('win32'):
100         cflags.append('-D_WIN32_WINNT=0x0501')
101     if options.x64:
102         cflags.append('-m64')
103 args.extend(cflags)
104 args.extend(ldflags)
105 binary = 'ninja.bootstrap'
106 if sys.platform.startswith('win32'):
107     binary = 'ninja.bootstrap.exe'
108 args.extend(sources)
109 if vcdir:
110     args.extend(['/link', '/out:' + binary])
111 else:
112     args.extend(['-o', binary])
113
114 if options.verbose:
115     print_(' '.join(args))
116
117 run(args)
118
119 verbose = []
120 if options.verbose:
121     verbose = ['-v']
122
123 if sys.platform.startswith('win32'):
124     print_('Building ninja using itself...')
125     run([sys.executable, 'configure.py', '--with-ninja=%s' % binary] +
126         conf_args)
127     run(['./' + binary] + verbose)
128
129     # Copy the new executable over the bootstrap one.
130     shutil.copyfile('ninja.exe', binary)
131
132     # Clean up.
133     for obj in glob.glob('*.obj'):
134         os.unlink(obj)
135
136     print_("""
137 Done!
138
139 Note: to work around Windows file locking, where you can't rebuild an
140 in-use binary, to run ninja after making any changes to build ninja itself
141 you should run ninja.bootstrap instead.  Your build is also configured to
142 use ninja.bootstrap.exe as the MSVC helper; see the --with-ninja flag of
143 the --help output of configure.py.""")
144 else:
145     print_('Building ninja using itself...')
146     run([sys.executable, 'configure.py'] + conf_args)
147     run(['./' + binary] + verbose)
148     os.unlink(binary)
149     print_('Done!')