2 # Copyright 2011 Google Inc. All Rights Reserved.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from optparse import OptionParser
25 if sys.version_info[0] == 3:
27 print_ = getattr(builtins, "print")
30 sys.stdout.write(" ".join(str(x) for x in args))
31 sys.stdout.write("\n")
33 os.chdir(os.path.dirname(os.path.abspath(__file__)))
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()
42 def run(*args, **kwargs):
43 returncode = subprocess.call(*args, **kwargs)
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')
55 print_('Building ninja manually...')
61 if e.errno != errno.EEXIST:
65 for src in glob.glob('src/*.cc'):
66 if src.endswith('test.cc') or src.endswith('.in.cc'):
68 if src.endswith('bench.cc'):
71 filename = os.path.basename(src)
72 if filename == 'browse.cc': # Depends on generated header.
75 if sys.platform.startswith('win32'):
76 if src.endswith('-posix.cc'):
79 if src.endswith('-win32.cc'):
84 if sys.platform.startswith('win32'):
85 sources.append('src/getopt.c')
87 vcdir = os.environ.get('VCINSTALLDIR')
90 cl = [os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')]
92 cl = [os.path.join(vcdir, 'bin', 'cl.exe')]
93 args = cl + ['/nologo', '/EHsc', '/DNOMINMAX']
95 args = shlex.split(os.environ.get('CXX', 'g++'))
96 cflags.extend(['-Wno-deprecated',
97 '-DNINJA_PYTHON="' + sys.executable + '"',
99 if sys.platform.startswith('win32'):
100 cflags.append('-D_WIN32_WINNT=0x0501')
102 cflags.append('-m64')
105 binary = 'ninja.bootstrap'
106 if sys.platform.startswith('win32'):
107 binary = 'ninja.bootstrap.exe'
110 args.extend(['/link', '/out:' + binary])
112 args.extend(['-o', binary])
115 print_(' '.join(args))
123 if sys.platform.startswith('win32'):
124 print_('Building ninja using itself...')
125 run([sys.executable, 'configure.py', '--with-ninja=%s' % binary] +
127 run(['./' + binary] + verbose)
129 # Copy the new executable over the bootstrap one.
130 shutil.copyfile('ninja.exe', binary)
133 for obj in glob.glob('*.obj'):
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.""")
145 print_('Building ninja using itself...')
146 run([sys.executable, 'configure.py'] + conf_args)
147 run(['./' + binary] + verbose)