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