2011-11-25 07:30:19 +00:00
|
|
|
#!/usr/bin/env python
|
2011-05-02 00:48:39 +00:00
|
|
|
#
|
|
|
|
# Copyright 2001 Google Inc. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""Script that generates the build.ninja for ninja itself.
|
|
|
|
|
|
|
|
Projects that use ninja themselves should either write a similar script
|
|
|
|
or use a meta-build system that supports Ninja output."""
|
|
|
|
|
2011-05-09 15:58:13 +00:00
|
|
|
from optparse import OptionParser
|
2011-05-02 00:48:39 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
sys.path.insert(0, 'misc')
|
|
|
|
|
2011-08-17 18:32:39 +00:00
|
|
|
import ninja_syntax
|
2011-05-02 00:48:39 +00:00
|
|
|
|
2011-05-09 15:58:13 +00:00
|
|
|
parser = OptionParser()
|
2011-12-02 05:33:33 +00:00
|
|
|
platforms = ['linux', 'freebsd', 'mingw']
|
2011-09-06 18:51:50 +00:00
|
|
|
profilers = ['gmon', 'pprof']
|
2011-05-09 15:58:13 +00:00
|
|
|
parser.add_option('--platform',
|
|
|
|
help='target platform (' + '/'.join(platforms) + ')',
|
|
|
|
choices=platforms)
|
2011-12-20 19:01:04 +00:00
|
|
|
parser.add_option('--host',
|
|
|
|
help='host platform (' + '/'.join(platforms) + ')',
|
|
|
|
choices=platforms)
|
2011-07-25 18:22:33 +00:00
|
|
|
parser.add_option('--debug', action='store_true',
|
|
|
|
help='enable debugging flags',)
|
2011-09-06 18:51:50 +00:00
|
|
|
parser.add_option('--profile', metavar='TYPE',
|
2011-11-19 14:48:30 +00:00
|
|
|
choices=profilers,
|
2011-09-06 18:51:50 +00:00
|
|
|
help='enable profiling (' + '/'.join(profilers) + ')',)
|
2011-09-07 23:47:57 +00:00
|
|
|
parser.add_option('--with-gtest', metavar='PATH',
|
2011-12-20 19:38:56 +00:00
|
|
|
help='use gtest unpacked in directory PATH')
|
2011-12-14 20:43:19 +00:00
|
|
|
parser.add_option('--with-python', metavar='EXE',
|
|
|
|
help='use EXE as the Python interpreter',
|
|
|
|
default=os.path.basename(sys.executable))
|
2011-05-09 15:58:13 +00:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
platform = options.platform
|
|
|
|
if platform is None:
|
|
|
|
platform = sys.platform
|
|
|
|
if platform.startswith('linux'):
|
|
|
|
platform = 'linux'
|
|
|
|
elif platform.startswith('freebsd'):
|
|
|
|
platform = 'freebsd'
|
2011-12-02 05:33:33 +00:00
|
|
|
elif platform.startswith('mingw') or platform.startswith('win'):
|
2011-05-09 15:58:13 +00:00
|
|
|
platform = 'mingw'
|
2011-12-20 19:01:04 +00:00
|
|
|
host = options.host or platform
|
2011-05-02 03:33:16 +00:00
|
|
|
|
2011-05-09 04:53:34 +00:00
|
|
|
BUILD_FILENAME = 'build.ninja'
|
|
|
|
buildfile = open(BUILD_FILENAME, 'w')
|
2011-08-17 18:32:39 +00:00
|
|
|
n = ninja_syntax.Writer(buildfile)
|
2011-05-02 00:48:39 +00:00
|
|
|
n.comment('This file is used to build ninja itself.')
|
|
|
|
n.comment('It is generated by ' + os.path.basename(__file__) + '.')
|
|
|
|
n.newline()
|
|
|
|
|
2011-09-06 21:04:31 +00:00
|
|
|
n.comment('The arguments passed to configure.py, for rerunning it.')
|
|
|
|
n.variable('configure_args', ' '.join(sys.argv[1:]))
|
2011-12-05 21:38:31 +00:00
|
|
|
n.newline()
|
2011-09-06 21:04:31 +00:00
|
|
|
|
2011-05-02 00:48:39 +00:00
|
|
|
def src(filename):
|
|
|
|
return os.path.join('src', filename)
|
|
|
|
def built(filename):
|
|
|
|
return os.path.join('$builddir', filename)
|
2011-05-14 23:11:41 +00:00
|
|
|
def doc(filename):
|
|
|
|
return os.path.join('doc', filename)
|
2011-05-02 00:48:39 +00:00
|
|
|
def cxx(name, **kwargs):
|
|
|
|
return n.build(built(name + '.o'), 'cxx', src(name + '.cc'), **kwargs)
|
2011-12-20 19:38:56 +00:00
|
|
|
def binary(name):
|
|
|
|
if platform == 'mingw':
|
|
|
|
return name + '.exe'
|
|
|
|
return name
|
2011-05-02 00:48:39 +00:00
|
|
|
|
|
|
|
n.variable('builddir', 'build')
|
2011-12-02 05:33:33 +00:00
|
|
|
n.variable('cxx', os.environ.get('CXX', 'g++'))
|
|
|
|
n.variable('ar', os.environ.get('AR', 'ar'))
|
2011-05-02 03:33:16 +00:00
|
|
|
|
2011-12-05 19:53:33 +00:00
|
|
|
cflags = ['-g', '-Wall', '-Wextra',
|
|
|
|
'-Wno-deprecated',
|
|
|
|
'-Wno-unused-parameter',
|
|
|
|
'-fno-exceptions',
|
2011-12-14 20:43:19 +00:00
|
|
|
'-fvisibility=hidden', '-pipe',
|
|
|
|
"'-DNINJA_PYTHON=\"%s\"'" % (options.with_python,)]
|
2011-07-25 18:22:33 +00:00
|
|
|
if not options.debug:
|
2011-09-21 13:09:42 +00:00
|
|
|
cflags += ['-O2', '-DNDEBUG']
|
2011-09-07 23:47:57 +00:00
|
|
|
ldflags = ['-L$builddir']
|
|
|
|
libs = []
|
2011-12-02 05:33:33 +00:00
|
|
|
|
2011-05-02 03:33:16 +00:00
|
|
|
if platform == 'mingw':
|
2011-12-05 15:08:47 +00:00
|
|
|
cflags.remove('-fvisibility=hidden');
|
2011-12-20 19:01:04 +00:00
|
|
|
ldflags.append('-static')
|
2011-05-02 03:33:16 +00:00
|
|
|
else:
|
2011-09-06 18:51:50 +00:00
|
|
|
if options.profile == 'gmon':
|
2011-05-25 17:38:00 +00:00
|
|
|
cflags.append('-pg')
|
|
|
|
ldflags.append('-pg')
|
2011-09-06 18:51:50 +00:00
|
|
|
elif options.profile == 'pprof':
|
2011-09-07 23:47:57 +00:00
|
|
|
libs.append('-lprofiler')
|
2011-05-06 21:17:07 +00:00
|
|
|
|
2011-05-02 00:48:39 +00:00
|
|
|
if 'CFLAGS' in os.environ:
|
2011-05-02 03:33:16 +00:00
|
|
|
cflags.append(os.environ['CFLAGS'])
|
|
|
|
n.variable('cflags', ' '.join(cflags))
|
2011-05-06 21:17:07 +00:00
|
|
|
if 'LDFLAGS' in os.environ:
|
|
|
|
ldflags.append(os.environ['LDFLAGS'])
|
|
|
|
n.variable('ldflags', ' '.join(ldflags))
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
|
|
|
|
|
|
|
n.rule('cxx',
|
|
|
|
command='$cxx -MMD -MF $out.d $cflags -c $in -o $out',
|
|
|
|
depfile='$out.d',
|
2011-05-06 20:04:38 +00:00
|
|
|
description='CXX $out')
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
|
|
|
|
2011-12-20 19:01:04 +00:00
|
|
|
if host != 'mingw':
|
2011-12-02 05:33:33 +00:00
|
|
|
n.rule('ar',
|
|
|
|
command='rm -f $out && $ar crs $out $in',
|
|
|
|
description='AR $out')
|
|
|
|
else:
|
|
|
|
n.rule('ar',
|
|
|
|
command='cmd /c $ar cqs $out.tmp $in && move /Y $out.tmp $out',
|
|
|
|
description='AR $out')
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
|
|
|
|
|
|
|
n.rule('link',
|
2011-05-02 03:33:16 +00:00
|
|
|
command='$cxx $ldflags -o $out $in $libs',
|
2011-05-02 00:48:39 +00:00
|
|
|
description='LINK $out')
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
objs = []
|
|
|
|
|
2011-12-02 05:33:33 +00:00
|
|
|
if platform != 'mingw':
|
2011-05-02 03:33:16 +00:00
|
|
|
n.comment('browse_py.h is used to inline browse.py.')
|
|
|
|
n.rule('inline',
|
|
|
|
command='src/inline.sh $varname < $in > $out',
|
|
|
|
description='INLINE $out')
|
|
|
|
n.build(built('browse_py.h'), 'inline', src('browse.py'),
|
2011-11-13 20:48:32 +00:00
|
|
|
implicit='src/inline.sh',
|
2011-05-02 03:33:16 +00:00
|
|
|
variables=[('varname', 'kBrowsePy')])
|
|
|
|
n.newline()
|
|
|
|
|
2011-11-13 20:48:32 +00:00
|
|
|
objs += cxx('browse', order_only=built('browse_py.h'))
|
2011-05-02 03:33:16 +00:00
|
|
|
n.newline()
|
2011-05-02 00:48:39 +00:00
|
|
|
|
2011-12-07 20:08:00 +00:00
|
|
|
n.comment('the depfile parser is generated using re2c.')
|
|
|
|
n.rule('re2c',
|
2011-12-19 19:14:35 +00:00
|
|
|
command='re2c -b -i --no-generation-date -o $out $in',
|
2011-12-07 20:08:00 +00:00
|
|
|
description='RE2C $out')
|
|
|
|
# Generate the .cc file in the source directory so we can check it in.
|
|
|
|
n.build(src('depfile_parser.cc'), 're2c', src('depfile_parser.in.cc'))
|
|
|
|
n.newline()
|
|
|
|
|
2011-05-02 00:48:39 +00:00
|
|
|
n.comment('Core source files all build into ninja library.')
|
2011-12-20 21:06:01 +00:00
|
|
|
for name in ['build',
|
|
|
|
'build_log',
|
|
|
|
'clean',
|
|
|
|
'depfile_parser',
|
|
|
|
'disk_interface',
|
|
|
|
'edit_distance',
|
|
|
|
'eval_env',
|
|
|
|
'graph',
|
|
|
|
'graphviz',
|
|
|
|
'parsers',
|
|
|
|
'state',
|
|
|
|
'util']:
|
2011-05-02 00:48:39 +00:00
|
|
|
objs += cxx(name)
|
2011-05-03 04:43:18 +00:00
|
|
|
if platform == 'mingw':
|
|
|
|
objs += cxx('subprocess-win32')
|
|
|
|
else:
|
|
|
|
objs += cxx('subprocess')
|
2011-05-02 03:33:16 +00:00
|
|
|
ninja_lib = n.build(built('libninja.a'), 'ar', objs)
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
|
|
|
|
2011-09-07 23:47:57 +00:00
|
|
|
libs.append('-lninja')
|
|
|
|
|
2011-05-02 00:48:39 +00:00
|
|
|
n.comment('Main executable is library plus main() function.')
|
|
|
|
objs = cxx('ninja')
|
2011-12-20 19:38:56 +00:00
|
|
|
ninja = n.build(binary('ninja'), 'link', objs, implicit=ninja_lib,
|
|
|
|
variables=[('libs', libs)])
|
2011-05-24 17:07:08 +00:00
|
|
|
n.newline()
|
2011-05-02 00:48:39 +00:00
|
|
|
|
|
|
|
n.comment('Tests all build into ninja_test executable.')
|
2011-09-07 23:47:57 +00:00
|
|
|
|
|
|
|
variables = []
|
|
|
|
test_cflags = None
|
|
|
|
test_ldflags = None
|
2011-12-20 19:38:56 +00:00
|
|
|
test_libs = libs
|
|
|
|
objs = []
|
2011-09-07 23:47:57 +00:00
|
|
|
if options.with_gtest:
|
|
|
|
path = options.with_gtest
|
2011-12-20 19:38:56 +00:00
|
|
|
|
|
|
|
gtest_all_incs = '-I%s -I%s' % (path, os.path.join(path, 'include'))
|
|
|
|
objs += n.build(built('gtest-all.o'), 'cxx',
|
|
|
|
os.path.join(path, 'src/gtest-all.cc'),
|
|
|
|
variables=[('cflags', gtest_all_incs)])
|
|
|
|
objs += n.build(built('gtest_main.o'), 'cxx',
|
|
|
|
os.path.join(path, 'src/gtest_main.cc'),
|
|
|
|
variables=[('cflags', gtest_all_incs)])
|
|
|
|
|
2011-09-07 23:47:57 +00:00
|
|
|
test_cflags = cflags + ['-I%s' % os.path.join(path, 'include')]
|
|
|
|
else:
|
2011-12-20 19:38:56 +00:00
|
|
|
test_libs.extend(['-lgtest_main', '-lgtest'])
|
2011-09-07 23:47:57 +00:00
|
|
|
|
2011-09-11 22:29:38 +00:00
|
|
|
for name in ['build_log_test',
|
|
|
|
'build_test',
|
|
|
|
'clean_test',
|
2011-12-19 18:59:29 +00:00
|
|
|
'depfile_parser_test',
|
2011-09-11 22:29:38 +00:00
|
|
|
'disk_interface_test',
|
2011-11-10 20:58:00 +00:00
|
|
|
'edit_distance_test',
|
2011-09-11 22:29:38 +00:00
|
|
|
'eval_env_test',
|
|
|
|
'graph_test',
|
|
|
|
'parsers_test',
|
|
|
|
'state_test',
|
|
|
|
'subprocess_test',
|
|
|
|
'test',
|
|
|
|
'util_test']:
|
2011-09-07 23:47:57 +00:00
|
|
|
objs += cxx(name, variables=[('cflags', test_cflags)])
|
|
|
|
|
2011-05-06 21:17:07 +00:00
|
|
|
if platform != 'mingw':
|
2011-09-09 15:53:08 +00:00
|
|
|
test_libs.append('-lpthread')
|
2011-12-20 19:38:56 +00:00
|
|
|
n.build(binary('ninja_test'), 'link', objs, implicit=ninja_lib,
|
2011-09-07 23:47:57 +00:00
|
|
|
variables=[('ldflags', test_ldflags),
|
|
|
|
('libs', test_libs)])
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
|
|
|
|
2011-05-24 17:07:08 +00:00
|
|
|
n.comment('Perftest executable.')
|
|
|
|
objs = cxx('parser_perftest')
|
2011-12-20 19:38:56 +00:00
|
|
|
n.build(binary('parser_perftest'), 'link', objs, implicit=ninja_lib,
|
2011-05-24 17:07:08 +00:00
|
|
|
variables=[('libs', '-L$builddir -lninja')])
|
|
|
|
n.newline()
|
|
|
|
|
2011-05-02 00:48:39 +00:00
|
|
|
n.comment('Generate a graph using the "graph" tool.')
|
|
|
|
n.rule('gendot',
|
|
|
|
command='./ninja -t graph > $out')
|
|
|
|
n.rule('gengraph',
|
|
|
|
command='dot -Tpng $in > $out')
|
|
|
|
dot = n.build(built('graph.dot'), 'gendot', ['ninja', 'build.ninja'])
|
|
|
|
n.build('graph.png', 'gengraph', dot)
|
|
|
|
n.newline()
|
|
|
|
|
|
|
|
n.comment('Generate the manual using asciidoc.')
|
|
|
|
n.rule('asciidoc',
|
2011-05-15 23:38:11 +00:00
|
|
|
command='asciidoc -a toc -o $out $in',
|
2011-05-02 00:48:39 +00:00
|
|
|
description='ASCIIDOC $in')
|
2011-05-14 23:11:41 +00:00
|
|
|
manual = n.build(doc('manual.html'), 'asciidoc', doc('manual.asciidoc'))
|
2011-05-02 00:48:39 +00:00
|
|
|
n.build('manual', 'phony',
|
2011-05-14 23:11:41 +00:00
|
|
|
order_only=manual)
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
|
|
|
|
|
|
|
n.comment('Generate Doxygen.')
|
|
|
|
n.rule('doxygen',
|
|
|
|
command='doxygen $in',
|
|
|
|
description='DOXYGEN $in')
|
|
|
|
n.variable('doxygen_mainpage_generator',
|
2011-05-14 23:11:41 +00:00
|
|
|
src('gen_doxygen_mainpage.sh'))
|
2011-05-02 00:48:39 +00:00
|
|
|
n.rule('doxygen_mainpage',
|
|
|
|
command='$doxygen_mainpage_generator $in > $out',
|
|
|
|
description='DOXYGEN_MAINPAGE $out')
|
|
|
|
mainpage = n.build(built('doxygen_mainpage'), 'doxygen_mainpage',
|
|
|
|
['README', 'HACKING', 'COPYING'],
|
|
|
|
implicit=['$doxygen_mainpage_generator'])
|
2011-05-14 23:11:41 +00:00
|
|
|
n.build('doxygen', 'doxygen', doc('doxygen.config'),
|
2011-05-13 23:24:27 +00:00
|
|
|
implicit=mainpage)
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
|
|
|
|
2011-12-20 19:38:56 +00:00
|
|
|
if host != 'mingw':
|
2011-12-02 05:33:33 +00:00
|
|
|
n.comment('Regenerate build files if build script changes.')
|
|
|
|
n.rule('configure',
|
2011-12-14 20:43:19 +00:00
|
|
|
command=options.with_python + ' configure.py $configure_args',
|
2011-12-02 05:33:33 +00:00
|
|
|
generator=True)
|
|
|
|
n.build('build.ninja', 'configure',
|
|
|
|
implicit=['configure.py', 'misc/ninja_syntax.py'])
|
|
|
|
n.newline()
|
2011-09-06 20:00:38 +00:00
|
|
|
|
|
|
|
n.comment('Build only the main binary by default.')
|
2011-12-20 19:38:56 +00:00
|
|
|
n.default(ninja)
|
2011-05-09 04:53:34 +00:00
|
|
|
|
|
|
|
print 'wrote %s.' % BUILD_FILENAME
|