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