3 # Copyright 2001 Google Inc. All Rights Reserved.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Script that generates the build.ninja for ninja itself.
19 Projects that use ninja themselves should either write a similar script
20 or use a meta-build system that supports Ninja output."""
22 from optparse import OptionParser
25 sys.path.insert(0, 'misc')
29 parser = OptionParser()
30 platforms = ['linux', 'freebsd', 'mingw', 'windows']
31 profilers = ['gmon', 'pprof']
32 parser.add_option('--platform',
33 help='target platform (' + '/'.join(platforms) + ')',
35 parser.add_option('--host',
36 help='host platform (' + '/'.join(platforms) + ')',
38 parser.add_option('--debug', action='store_true',
39 help='enable debugging flags',)
40 parser.add_option('--profile', metavar='TYPE',
42 help='enable profiling (' + '/'.join(profilers) + ')',)
43 parser.add_option('--with-gtest', metavar='PATH',
44 help='use gtest unpacked in directory PATH')
45 parser.add_option('--with-python', metavar='EXE',
46 help='use EXE as the Python interpreter',
47 default=os.path.basename(sys.executable))
48 (options, args) = parser.parse_args()
50 print 'ERROR: extra unparsed command-line arguments:', args
53 platform = options.platform
55 platform = sys.platform
56 if platform.startswith('linux'):
58 elif platform.startswith('freebsd'):
60 elif platform.startswith('mingw'):
62 elif platform.startswith('win'):
64 host = options.host or platform
66 BUILD_FILENAME = 'build.ninja'
67 buildfile = open(BUILD_FILENAME, 'w')
68 n = ninja_syntax.Writer(buildfile)
69 n.comment('This file is used to build ninja itself.')
70 n.comment('It is generated by ' + os.path.basename(__file__) + '.')
73 n.comment('The arguments passed to configure.py, for rerunning it.')
74 n.variable('configure_args', ' '.join(sys.argv[1:]))
78 if platform == 'windows':
82 return os.path.join('src', filename)
84 return os.path.join('$builddir', filename)
86 return os.path.join('doc', filename)
87 def cc(name, **kwargs):
88 return n.build(built(name + objext), 'cxx', src(name + '.c'), **kwargs)
89 def cxx(name, **kwargs):
90 return n.build(built(name + objext), 'cxx', src(name + '.cc'), **kwargs)
92 if platform in ('mingw', 'windows'):
96 n.variable('builddir', 'build')
97 if platform == 'windows':
98 n.variable('cxx', 'cl')
99 n.variable('ar', 'link')
101 n.variable('cxx', os.environ.get('CXX', 'g++'))
102 n.variable('ar', os.environ.get('AR', 'ar'))
104 if platform == 'windows':
105 cflags = ['/nologo', '/Zi', '/W4', '/WX', '/wd4530', '/wd4100', '/wd4706',
106 '/wd4512', '/wd4800', '/wd4702', '/wd4819',
107 '/DNOMINMAX', '/D_CRT_SECURE_NO_WARNINGS',
108 "/DNINJA_PYTHON=\"%s\"" % (options.with_python,)]
109 ldflags = ['/DEBUG', '/libpath:$builddir']
110 if not options.debug:
111 cflags += ['/Ox', '/DNDEBUG', '/GL']
112 ldflags += ['/LTCG', '/OPT:REF', '/OPT:ICF']
114 cflags = ['-g', '-Wall', '-Wextra',
116 '-Wno-unused-parameter',
119 '-fvisibility=hidden', '-pipe',
120 "'-DNINJA_PYTHON=\"%s\"'" % (options.with_python,)]
122 cflags += ['-D_GLIBCXX_DEBUG', '-D_GLIBCXX_DEBUG_PEDANTIC']
124 cflags += ['-O2', '-DNDEBUG']
125 ldflags = ['-L$builddir']
128 if platform == 'mingw':
129 cflags.remove('-fvisibility=hidden');
130 ldflags.append('-static')
131 elif platform == 'sunos5':
132 cflags.remove('-fvisibility=hidden')
133 elif platform == 'windows':
136 if options.profile == 'gmon':
138 ldflags.append('-pg')
139 elif options.profile == 'pprof':
140 libs.append('-lprofiler')
142 if 'CFLAGS' in os.environ:
143 cflags.append(os.environ['CFLAGS'])
144 n.variable('cflags', ' '.join(cflags))
145 if 'LDFLAGS' in os.environ:
146 ldflags.append(os.environ['LDFLAGS'])
147 n.variable('ldflags', ' '.join(ldflags))
150 if platform == 'windows':
152 command='$cxx $cflags -c $in /Fo$out',
154 description='CXX $out')
157 command='$cxx -MMD -MT $out -MF $out.d $cflags -c $in -o $out',
159 description='CXX $out')
162 if host == 'windows':
164 command='lib /nologo /ltcg /out:$out $in',
165 description='LIB $out')
166 elif host == 'mingw':
168 command='cmd /c $ar cqs $out.tmp $in && move /Y $out.tmp $out',
169 description='AR $out')
172 command='rm -f $out && $ar crs $out $in',
173 description='AR $out')
176 if platform == 'windows':
178 command='$cxx $in $libs /nologo /link $ldflags /out:$out',
179 description='LINK $out')
182 command='$cxx $ldflags -o $out $in $libs',
183 description='LINK $out')
188 if platform not in ('mingw', 'windows'):
189 n.comment('browse_py.h is used to inline browse.py.')
191 command='src/inline.sh $varname < $in > $out',
192 description='INLINE $out')
193 n.build(built('browse_py.h'), 'inline', src('browse.py'),
194 implicit='src/inline.sh',
195 variables=[('varname', 'kBrowsePy')])
198 objs += cxx('browse', order_only=built('browse_py.h'))
201 n.comment('the depfile parser and ninja lexers are generated using re2c.')
203 command='re2c -b -i --no-generation-date -o $out $in',
204 description='RE2C $out')
205 # Generate the .cc files in the source directory so we can check them in.
206 n.build(src('depfile_parser.cc'), 're2c', src('depfile_parser.in.cc'))
207 n.build(src('lexer.cc'), 're2c', src('lexer.in.cc'))
210 n.comment('Core source files all build into ninja library.')
211 for name in ['build',
227 if platform == 'mingw' or platform == 'windows':
228 objs += cxx('subprocess-win32')
231 objs += cxx('subprocess')
232 if platform == 'windows':
233 ninja_lib = n.build(built('ninja.lib'), 'ar', objs)
235 ninja_lib = n.build(built('libninja.a'), 'ar', objs)
238 if platform == 'windows':
239 libs.append('ninja.lib')
241 libs.append('-lninja')
245 n.comment('Main executable is library plus main() function.')
247 ninja = n.build(binary('ninja'), 'link', objs, implicit=ninja_lib,
248 variables=[('libs', libs)])
252 n.comment('Tests all build into ninja_test executable.')
259 if options.with_gtest:
260 path = options.with_gtest
262 gtest_all_incs = '-I%s -I%s' % (path, os.path.join(path, 'include'))
263 gtest_cflags = '-fvisibility=hidden ' + gtest_all_incs
264 objs += n.build(built('gtest-all.o'), 'cxx',
265 os.path.join(path, 'src/gtest-all.cc'),
266 variables=[('cflags', gtest_cflags)])
267 objs += n.build(built('gtest_main.o'), 'cxx',
268 os.path.join(path, 'src/gtest_main.cc'),
269 variables=[('cflags', gtest_cflags)])
271 test_cflags = cflags + ['-I%s' % os.path.join(path, 'include')]
272 elif platform == 'windows':
273 test_libs.extend(['gtest_main.lib', 'gtest.lib'])
275 test_libs.extend(['-lgtest_main', '-lgtest'])
277 for name in ['build_log_test',
280 'depfile_parser_test',
281 'disk_interface_test',
282 'edit_distance_test',
290 objs += cxx(name, variables=[('cflags', test_cflags)])
292 if platform != 'mingw' and platform != 'windows':
293 test_libs.append('-lpthread')
294 ninja_test = n.build(binary('ninja_test'), 'link', objs, implicit=ninja_lib,
295 variables=[('ldflags', test_ldflags),
296 ('libs', test_libs)])
298 all_targets += ninja_test
300 n.comment('Perftest executable.')
301 objs = cxx('parser_perftest')
302 parser_perftest = n.build(binary('parser_perftest'), 'link', objs,
304 variables=[('libs', '-L$builddir -lninja')])
306 all_targets += parser_perftest
308 n.comment('Generate a graph using the "graph" tool.')
310 command='./ninja -t graph > $out')
312 command='dot -Tpng $in > $out')
313 dot = n.build(built('graph.dot'), 'gendot', ['ninja', 'build.ninja'])
314 n.build('graph.png', 'gengraph', dot)
317 n.comment('Generate the manual using asciidoc.')
319 command='asciidoc -a toc -o $out $in',
320 description='ASCIIDOC $in')
321 manual = n.build(doc('manual.html'), 'asciidoc', doc('manual.asciidoc'))
322 n.build('manual', 'phony',
326 n.comment('Generate Doxygen.')
328 command='doxygen $in',
329 description='DOXYGEN $in')
330 n.variable('doxygen_mainpage_generator',
331 src('gen_doxygen_mainpage.sh'))
332 n.rule('doxygen_mainpage',
333 command='$doxygen_mainpage_generator $in > $out',
334 description='DOXYGEN_MAINPAGE $out')
335 mainpage = n.build(built('doxygen_mainpage'), 'doxygen_mainpage',
336 ['README', 'HACKING', 'COPYING'],
337 implicit=['$doxygen_mainpage_generator'])
338 n.build('doxygen', 'doxygen', doc('doxygen.config'),
343 n.comment('Regenerate build files if build script changes.')
345 command=options.with_python + ' configure.py $configure_args',
347 n.build('build.ninja', 'configure',
348 implicit=['configure.py', 'misc/ninja_syntax.py'])
351 n.comment('Build only the main binary by default.')
355 n.build('all', 'phony', all_targets)
357 print 'wrote %s.' % BUILD_FILENAME