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."""
|
|
|
|
|
2012-10-19 07:46:09 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2011-05-09 15:58:13 +00:00
|
|
|
from optparse import OptionParser
|
2011-05-02 00:48:39 +00:00
|
|
|
import os
|
2014-09-19 15:49:00 +00:00
|
|
|
import pipes
|
2014-11-14 18:53:33 +00:00
|
|
|
import string
|
|
|
|
import subprocess
|
2011-05-02 00:48:39 +00:00
|
|
|
import sys
|
|
|
|
|
2015-05-26 21:19:28 +00:00
|
|
|
sourcedir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
sys.path.insert(0, os.path.join(sourcedir, 'misc'))
|
2011-08-17 18:32:39 +00:00
|
|
|
import ninja_syntax
|
2011-05-02 00:48:39 +00:00
|
|
|
|
2014-11-14 18:53:33 +00:00
|
|
|
|
2014-11-18 18:09:07 +00:00
|
|
|
class Platform(object):
|
|
|
|
"""Represents a host/target platform and its specific build attributes."""
|
|
|
|
def __init__(self, platform):
|
|
|
|
self._platform = platform
|
|
|
|
if self._platform is not None:
|
|
|
|
return
|
|
|
|
self._platform = sys.platform
|
|
|
|
if self._platform.startswith('linux'):
|
|
|
|
self._platform = 'linux'
|
|
|
|
elif self._platform.startswith('freebsd'):
|
|
|
|
self._platform = 'freebsd'
|
|
|
|
elif self._platform.startswith('gnukfreebsd'):
|
|
|
|
self._platform = 'freebsd'
|
|
|
|
elif self._platform.startswith('openbsd'):
|
|
|
|
self._platform = 'openbsd'
|
|
|
|
elif self._platform.startswith('solaris') or self._platform == 'sunos5':
|
|
|
|
self._platform = 'solaris'
|
|
|
|
elif self._platform.startswith('mingw'):
|
|
|
|
self._platform = 'mingw'
|
|
|
|
elif self._platform.startswith('win'):
|
|
|
|
self._platform = 'msvc'
|
|
|
|
elif self._platform.startswith('bitrig'):
|
|
|
|
self._platform = 'bitrig'
|
|
|
|
elif self._platform.startswith('netbsd'):
|
|
|
|
self._platform = 'netbsd'
|
2015-01-13 01:47:55 +00:00
|
|
|
elif self._platform.startswith('aix'):
|
|
|
|
self._platform = 'aix'
|
2017-06-10 16:53:43 +00:00
|
|
|
elif self._platform.startswith('dragonfly'):
|
|
|
|
self._platform = 'dragonfly'
|
2014-11-18 18:09:07 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def known_platforms():
|
|
|
|
return ['linux', 'darwin', 'freebsd', 'openbsd', 'solaris', 'sunos5',
|
2017-06-10 16:53:43 +00:00
|
|
|
'mingw', 'msvc', 'gnukfreebsd', 'bitrig', 'netbsd', 'aix',
|
|
|
|
'dragonfly']
|
2014-11-18 18:09:07 +00:00
|
|
|
|
|
|
|
def platform(self):
|
|
|
|
return self._platform
|
|
|
|
|
|
|
|
def is_linux(self):
|
|
|
|
return self._platform == 'linux'
|
|
|
|
|
|
|
|
def is_mingw(self):
|
|
|
|
return self._platform == 'mingw'
|
|
|
|
|
|
|
|
def is_msvc(self):
|
|
|
|
return self._platform == 'msvc'
|
|
|
|
|
|
|
|
def msvc_needs_fs(self):
|
|
|
|
popen = subprocess.Popen(['cl', '/nologo', '/?'],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
out, err = popen.communicate()
|
2016-01-25 13:38:08 +00:00
|
|
|
return b'/FS' in out
|
2014-11-18 18:09:07 +00:00
|
|
|
|
|
|
|
def is_windows(self):
|
|
|
|
return self.is_mingw() or self.is_msvc()
|
|
|
|
|
|
|
|
def is_solaris(self):
|
|
|
|
return self._platform == 'solaris'
|
|
|
|
|
2015-01-13 01:47:55 +00:00
|
|
|
def is_aix(self):
|
|
|
|
return self._platform == 'aix'
|
|
|
|
|
2014-11-18 18:09:07 +00:00
|
|
|
def uses_usr_local(self):
|
2017-06-10 16:53:43 +00:00
|
|
|
return self._platform in ('freebsd', 'openbsd', 'bitrig', 'dragonfly')
|
2014-11-18 18:09:07 +00:00
|
|
|
|
|
|
|
def supports_ppoll(self):
|
2017-06-10 16:53:43 +00:00
|
|
|
return self._platform in ('freebsd', 'linux', 'openbsd', 'bitrig',
|
|
|
|
'dragonfly')
|
2014-11-18 18:09:07 +00:00
|
|
|
|
2014-11-21 00:56:17 +00:00
|
|
|
def supports_ninja_browse(self):
|
2015-01-13 01:47:55 +00:00
|
|
|
return (not self.is_windows()
|
|
|
|
and not self.is_solaris()
|
|
|
|
and not self.is_aix())
|
2014-11-21 00:56:17 +00:00
|
|
|
|
2015-10-21 22:37:50 +00:00
|
|
|
def can_rebuild_in_place(self):
|
|
|
|
return not (self.is_windows() or self.is_aix())
|
2014-11-18 18:09:07 +00:00
|
|
|
|
2014-11-14 18:53:33 +00:00
|
|
|
class Bootstrap:
|
|
|
|
"""API shim for ninja_syntax.Writer that instead runs the commands.
|
|
|
|
|
|
|
|
Used to bootstrap Ninja from scratch. In --bootstrap mode this
|
|
|
|
class is used to execute all the commands to build an executable.
|
|
|
|
It also proxies all calls to an underlying ninja_syntax.Writer, to
|
|
|
|
behave like non-bootstrap mode.
|
|
|
|
"""
|
2014-12-11 04:09:38 +00:00
|
|
|
def __init__(self, writer, verbose=False):
|
2014-11-14 18:53:33 +00:00
|
|
|
self.writer = writer
|
2014-12-11 04:09:38 +00:00
|
|
|
self.verbose = verbose
|
2014-11-14 18:53:33 +00:00
|
|
|
# Map of variable name => expanded variable value.
|
|
|
|
self.vars = {}
|
|
|
|
# Map of rule name => dict of rule attributes.
|
|
|
|
self.rules = {
|
|
|
|
'phony': {}
|
|
|
|
}
|
|
|
|
|
|
|
|
def comment(self, text):
|
|
|
|
return self.writer.comment(text)
|
|
|
|
|
|
|
|
def newline(self):
|
|
|
|
return self.writer.newline()
|
|
|
|
|
|
|
|
def variable(self, key, val):
|
2016-02-05 18:28:56 +00:00
|
|
|
# In bootstrap mode, we have no ninja process to catch /showIncludes
|
|
|
|
# output.
|
|
|
|
self.vars[key] = self._expand(val).replace('/showIncludes', '')
|
2014-11-14 18:53:33 +00:00
|
|
|
return self.writer.variable(key, val)
|
|
|
|
|
|
|
|
def rule(self, name, **kwargs):
|
|
|
|
self.rules[name] = kwargs
|
|
|
|
return self.writer.rule(name, **kwargs)
|
|
|
|
|
|
|
|
def build(self, outputs, rule, inputs=None, **kwargs):
|
|
|
|
ruleattr = self.rules[rule]
|
|
|
|
cmd = ruleattr.get('command')
|
|
|
|
if cmd is None: # A phony rule, for example.
|
|
|
|
return
|
|
|
|
|
|
|
|
# Implement just enough of Ninja variable expansion etc. to
|
|
|
|
# make the bootstrap build work.
|
|
|
|
local_vars = {
|
|
|
|
'in': self._expand_paths(inputs),
|
|
|
|
'out': self._expand_paths(outputs)
|
|
|
|
}
|
|
|
|
for key, val in kwargs.get('variables', []):
|
|
|
|
local_vars[key] = ' '.join(ninja_syntax.as_list(val))
|
|
|
|
|
|
|
|
self._run_command(self._expand(cmd, local_vars))
|
|
|
|
|
|
|
|
return self.writer.build(outputs, rule, inputs, **kwargs)
|
|
|
|
|
|
|
|
def default(self, paths):
|
|
|
|
return self.writer.default(paths)
|
|
|
|
|
|
|
|
def _expand_paths(self, paths):
|
|
|
|
"""Expand $vars in an array of paths, e.g. from a 'build' block."""
|
|
|
|
paths = ninja_syntax.as_list(paths)
|
2015-06-22 22:28:04 +00:00
|
|
|
return ' '.join(map(self._shell_escape, (map(self._expand, paths))))
|
2014-11-14 18:53:33 +00:00
|
|
|
|
|
|
|
def _expand(self, str, local_vars={}):
|
|
|
|
"""Expand $vars in a string."""
|
|
|
|
return ninja_syntax.expand(str, self.vars, local_vars)
|
|
|
|
|
2015-06-22 22:28:04 +00:00
|
|
|
def _shell_escape(self, path):
|
|
|
|
"""Quote paths containing spaces."""
|
|
|
|
return '"%s"' % path if ' ' in path else path
|
|
|
|
|
2014-11-14 18:53:33 +00:00
|
|
|
def _run_command(self, cmdline):
|
|
|
|
"""Run a subcommand, quietly. Prints the full command on error."""
|
|
|
|
try:
|
2014-12-11 04:09:38 +00:00
|
|
|
if self.verbose:
|
|
|
|
print(cmdline)
|
2014-11-14 18:53:33 +00:00
|
|
|
subprocess.check_call(cmdline, shell=True)
|
2014-12-03 16:10:44 +00:00
|
|
|
except subprocess.CalledProcessError:
|
2014-11-14 18:53:33 +00:00
|
|
|
print('when running: ', cmdline)
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
2011-05-09 15:58:13 +00:00
|
|
|
parser = OptionParser()
|
2011-09-06 18:51:50 +00:00
|
|
|
profilers = ['gmon', 'pprof']
|
2014-11-14 18:53:33 +00:00
|
|
|
parser.add_option('--bootstrap', action='store_true',
|
|
|
|
help='bootstrap a ninja binary from nothing')
|
2014-12-11 04:09:38 +00:00
|
|
|
parser.add_option('--verbose', action='store_true',
|
|
|
|
help='enable verbose build')
|
2011-05-09 15:58:13 +00:00
|
|
|
parser.add_option('--platform',
|
2013-10-27 03:15:23 +00:00
|
|
|
help='target platform (' +
|
2014-11-18 18:09:07 +00:00
|
|
|
'/'.join(Platform.known_platforms()) + ')',
|
|
|
|
choices=Platform.known_platforms())
|
2011-12-20 19:01:04 +00:00
|
|
|
parser.add_option('--host',
|
2013-10-27 03:15:23 +00:00
|
|
|
help='host platform (' +
|
2014-11-18 18:09:07 +00:00
|
|
|
'/'.join(Platform.known_platforms()) + ')',
|
|
|
|
choices=Platform.known_platforms())
|
2011-07-25 18:22:33 +00:00
|
|
|
parser.add_option('--debug', action='store_true',
|
2012-05-02 03:46:34 +00:00
|
|
|
help='enable debugging extras',)
|
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) + ')',)
|
2014-09-18 02:48:26 +00:00
|
|
|
parser.add_option('--with-gtest', metavar='PATH', help='ignored')
|
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))
|
2013-05-23 13:12:53 +00:00
|
|
|
parser.add_option('--force-pselect', action='store_true',
|
2013-10-27 03:15:23 +00:00
|
|
|
help='ppoll() is used by default where available, '
|
|
|
|
'but some platforms may need to use pselect instead',)
|
2011-05-09 15:58:13 +00:00
|
|
|
(options, args) = parser.parse_args()
|
2012-01-05 04:01:04 +00:00
|
|
|
if args:
|
2012-10-19 07:46:09 +00:00
|
|
|
print('ERROR: extra unparsed command-line arguments:', args)
|
2012-01-05 04:01:04 +00:00
|
|
|
sys.exit(1)
|
2011-05-09 15:58:13 +00:00
|
|
|
|
2014-11-18 18:09:07 +00:00
|
|
|
platform = Platform(options.platform)
|
2013-04-30 15:29:13 +00:00
|
|
|
if options.host:
|
2014-11-18 18:09:07 +00:00
|
|
|
host = Platform(options.host)
|
2013-04-30 15:29:13 +00:00
|
|
|
else:
|
|
|
|
host = platform
|
2011-05-02 03:33:16 +00:00
|
|
|
|
2011-05-09 04:53:34 +00:00
|
|
|
BUILD_FILENAME = 'build.ninja'
|
2014-11-14 18:53:33 +00:00
|
|
|
ninja_writer = ninja_syntax.Writer(open(BUILD_FILENAME, 'w'))
|
|
|
|
n = ninja_writer
|
|
|
|
|
|
|
|
if options.bootstrap:
|
|
|
|
# Make the build directory.
|
|
|
|
try:
|
|
|
|
os.mkdir('build')
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
# Wrap ninja_writer with the Bootstrapper, which also executes the
|
|
|
|
# commands.
|
|
|
|
print('bootstrapping ninja...')
|
2014-12-11 04:09:38 +00:00
|
|
|
n = Bootstrap(n, verbose=options.verbose)
|
2014-11-14 18:53:33 +00:00
|
|
|
|
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()
|
|
|
|
|
2013-05-17 15:32:04 +00:00
|
|
|
n.variable('ninja_required_version', '1.3')
|
|
|
|
n.newline()
|
|
|
|
|
2011-09-06 21:04:31 +00:00
|
|
|
n.comment('The arguments passed to configure.py, for rerunning it.')
|
2014-11-14 18:53:33 +00:00
|
|
|
configure_args = sys.argv[1:]
|
|
|
|
if '--bootstrap' in configure_args:
|
|
|
|
configure_args.remove('--bootstrap')
|
|
|
|
n.variable('configure_args', ' '.join(configure_args))
|
2012-05-04 15:33:46 +00:00
|
|
|
env_keys = set(['CXX', 'AR', 'CFLAGS', 'LDFLAGS'])
|
2012-05-04 21:20:33 +00:00
|
|
|
configure_env = dict((k, os.environ[k]) for k in os.environ if k in env_keys)
|
2012-06-05 03:23:14 +00:00
|
|
|
if configure_env:
|
2014-09-19 15:49:00 +00:00
|
|
|
config_str = ' '.join([k + '=' + pipes.quote(configure_env[k])
|
|
|
|
for k in configure_env])
|
2012-06-05 03:23:14 +00:00
|
|
|
n.variable('configure_env', config_str + '$ ')
|
2011-12-05 21:38:31 +00:00
|
|
|
n.newline()
|
2011-09-06 21:04:31 +00:00
|
|
|
|
2012-05-04 15:44:30 +00:00
|
|
|
CXX = configure_env.get('CXX', 'g++')
|
2012-01-04 01:04:49 +00:00
|
|
|
objext = '.o'
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_msvc():
|
2012-05-04 15:44:30 +00:00
|
|
|
CXX = 'cl'
|
2012-01-04 01:04:49 +00:00
|
|
|
objext = '.obj'
|
|
|
|
|
2011-05-02 00:48:39 +00:00
|
|
|
def src(filename):
|
2015-11-17 22:26:18 +00:00
|
|
|
return os.path.join('$root', 'src', filename)
|
2011-05-02 00:48:39 +00:00
|
|
|
def built(filename):
|
|
|
|
return os.path.join('$builddir', filename)
|
2011-05-14 23:11:41 +00:00
|
|
|
def doc(filename):
|
2015-11-17 22:26:18 +00:00
|
|
|
return os.path.join('$root', 'doc', filename)
|
2012-01-04 01:04:49 +00:00
|
|
|
def cc(name, **kwargs):
|
|
|
|
return n.build(built(name + objext), 'cxx', src(name + '.c'), **kwargs)
|
2011-05-02 00:48:39 +00:00
|
|
|
def cxx(name, **kwargs):
|
2012-01-04 01:04:49 +00:00
|
|
|
return n.build(built(name + objext), 'cxx', src(name + '.cc'), **kwargs)
|
2011-12-20 19:38:56 +00:00
|
|
|
def binary(name):
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_windows():
|
2012-08-13 01:43:42 +00:00
|
|
|
exe = name + '.exe'
|
|
|
|
n.build(name, 'phony', exe)
|
|
|
|
return exe
|
2011-12-20 19:38:56 +00:00
|
|
|
return name
|
2011-05-02 00:48:39 +00:00
|
|
|
|
2015-11-17 22:28:08 +00:00
|
|
|
root = sourcedir
|
|
|
|
if root == os.getcwd():
|
|
|
|
# In the common case where we're building directly in the source
|
|
|
|
# tree, simplify all the paths to just be cwd-relative.
|
|
|
|
root = '.'
|
|
|
|
n.variable('root', root)
|
2011-05-02 00:48:39 +00:00
|
|
|
n.variable('builddir', 'build')
|
2012-05-04 15:44:30 +00:00
|
|
|
n.variable('cxx', CXX)
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_msvc():
|
2012-01-04 01:04:49 +00:00
|
|
|
n.variable('ar', 'link')
|
|
|
|
else:
|
2012-05-04 21:20:33 +00:00
|
|
|
n.variable('ar', configure_env.get('AR', 'ar'))
|
2012-01-04 01:04:49 +00:00
|
|
|
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_msvc():
|
2014-11-14 18:53:33 +00:00
|
|
|
cflags = ['/showIncludes',
|
|
|
|
'/nologo', # Don't print startup banner.
|
2012-08-12 21:07:38 +00:00
|
|
|
'/Zi', # Create pdb with debug info.
|
|
|
|
'/W4', # Highest warning level.
|
|
|
|
'/WX', # Warnings as errors.
|
2017-02-04 23:38:20 +00:00
|
|
|
'/wd4530', '/wd4100', '/wd4706', '/wd4244',
|
2012-08-12 21:07:38 +00:00
|
|
|
'/wd4512', '/wd4800', '/wd4702', '/wd4819',
|
2016-02-04 12:20:58 +00:00
|
|
|
# Disable warnings about constant conditional expressions.
|
|
|
|
'/wd4127',
|
2012-09-07 20:10:48 +00:00
|
|
|
# Disable warnings about passing "this" during initialization.
|
|
|
|
'/wd4355',
|
2014-12-31 11:17:21 +00:00
|
|
|
# Disable warnings about ignored typedef in DbgHelp.h
|
|
|
|
'/wd4091',
|
2012-08-12 21:07:38 +00:00
|
|
|
'/GR-', # Disable RTTI.
|
2012-08-10 20:39:25 +00:00
|
|
|
# Disable size_t -> int truncation warning.
|
|
|
|
# We never have strings or arrays larger than 2**31.
|
|
|
|
'/wd4267',
|
2012-03-16 12:59:28 +00:00
|
|
|
'/DNOMINMAX', '/D_CRT_SECURE_NO_WARNINGS',
|
2015-08-10 04:02:23 +00:00
|
|
|
'/D_HAS_EXCEPTIONS=0',
|
2012-07-27 17:05:20 +00:00
|
|
|
'/DNINJA_PYTHON="%s"' % options.with_python]
|
2013-10-18 04:12:11 +00:00
|
|
|
if platform.msvc_needs_fs():
|
|
|
|
cflags.append('/FS')
|
2012-01-14 03:17:14 +00:00
|
|
|
ldflags = ['/DEBUG', '/libpath:$builddir']
|
2012-01-04 01:04:49 +00:00
|
|
|
if not options.debug:
|
2012-01-14 03:17:14 +00:00
|
|
|
cflags += ['/Ox', '/DNDEBUG', '/GL']
|
|
|
|
ldflags += ['/LTCG', '/OPT:REF', '/OPT:ICF']
|
2012-01-04 01:04:49 +00:00
|
|
|
else:
|
|
|
|
cflags = ['-g', '-Wall', '-Wextra',
|
|
|
|
'-Wno-deprecated',
|
2015-11-17 22:23:13 +00:00
|
|
|
'-Wno-missing-field-initializers',
|
2012-01-04 01:04:49 +00:00
|
|
|
'-Wno-unused-parameter',
|
2012-03-06 21:42:47 +00:00
|
|
|
'-fno-rtti',
|
2012-01-04 01:04:49 +00:00
|
|
|
'-fno-exceptions',
|
|
|
|
'-fvisibility=hidden', '-pipe',
|
2012-07-27 17:05:20 +00:00
|
|
|
'-DNINJA_PYTHON="%s"' % options.with_python]
|
2012-01-13 17:02:19 +00:00
|
|
|
if options.debug:
|
|
|
|
cflags += ['-D_GLIBCXX_DEBUG', '-D_GLIBCXX_DEBUG_PEDANTIC']
|
2012-09-04 22:21:52 +00:00
|
|
|
cflags.remove('-fno-rtti') # Needed for above pedanticness.
|
2012-01-13 17:02:19 +00:00
|
|
|
else:
|
2012-01-04 01:04:49 +00:00
|
|
|
cflags += ['-O2', '-DNDEBUG']
|
2015-03-09 00:09:01 +00:00
|
|
|
try:
|
2015-03-09 17:24:41 +00:00
|
|
|
proc = subprocess.Popen(
|
2015-08-27 02:51:15 +00:00
|
|
|
[CXX, '-fdiagnostics-color', '-c', '-x', 'c++', '/dev/null',
|
|
|
|
'-o', '/dev/null'],
|
2015-03-09 17:24:41 +00:00
|
|
|
stdout=open(os.devnull, 'wb'), stderr=subprocess.STDOUT)
|
2015-03-10 16:09:49 +00:00
|
|
|
if proc.wait() == 0:
|
2015-03-09 17:24:41 +00:00
|
|
|
cflags += ['-fdiagnostics-color']
|
2015-03-09 00:09:01 +00:00
|
|
|
except:
|
|
|
|
pass
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_mingw():
|
2012-07-27 17:17:20 +00:00
|
|
|
cflags += ['-D_WIN32_WINNT=0x0501']
|
2012-01-04 01:04:49 +00:00
|
|
|
ldflags = ['-L$builddir']
|
2014-11-18 18:09:07 +00:00
|
|
|
if platform.uses_usr_local():
|
|
|
|
cflags.append('-I/usr/local/include')
|
|
|
|
ldflags.append('-L/usr/local/lib')
|
|
|
|
|
2011-09-07 23:47:57 +00:00
|
|
|
libs = []
|
2011-12-02 05:33:33 +00:00
|
|
|
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_mingw():
|
2011-12-05 15:08:47 +00:00
|
|
|
cflags.remove('-fvisibility=hidden');
|
2011-12-20 19:01:04 +00:00
|
|
|
ldflags.append('-static')
|
2014-11-01 19:20:42 +00:00
|
|
|
elif platform.is_solaris():
|
2012-02-07 10:00:20 +00:00
|
|
|
cflags.remove('-fvisibility=hidden')
|
2015-01-13 01:47:55 +00:00
|
|
|
elif platform.is_aix():
|
|
|
|
cflags.remove('-fvisibility=hidden')
|
2013-04-30 15:29:13 +00:00
|
|
|
elif platform.is_msvc():
|
2012-01-04 01:04:49 +00:00
|
|
|
pass
|
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':
|
2013-04-15 21:41:10 +00:00
|
|
|
cflags.append('-fno-omit-frame-pointer')
|
|
|
|
libs.extend(['-Wl,--no-as-needed', '-lprofiler'])
|
2011-05-06 21:17:07 +00:00
|
|
|
|
2014-11-18 18:09:07 +00:00
|
|
|
if platform.supports_ppoll() and not options.force_pselect:
|
2013-05-23 13:12:53 +00:00
|
|
|
cflags.append('-DUSE_PPOLL')
|
2014-11-21 00:56:17 +00:00
|
|
|
if platform.supports_ninja_browse():
|
2014-11-14 01:53:26 +00:00
|
|
|
cflags.append('-DNINJA_HAVE_BROWSE')
|
|
|
|
|
2015-05-26 22:47:18 +00:00
|
|
|
# Search for generated headers relative to build dir.
|
|
|
|
cflags.append('-I.')
|
|
|
|
|
2012-06-16 20:27:14 +00:00
|
|
|
def shell_escape(str):
|
2012-12-29 20:02:16 +00:00
|
|
|
"""Escape str such that it's interpreted as a single argument by
|
|
|
|
the shell."""
|
|
|
|
|
2012-06-16 20:27:14 +00:00
|
|
|
# This isn't complete, but it's just enough to make NINJA_PYTHON work.
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_windows():
|
2012-06-19 20:15:41 +00:00
|
|
|
return str
|
2012-06-16 20:27:14 +00:00
|
|
|
if '"' in str:
|
2012-06-16 20:29:09 +00:00
|
|
|
return "'%s'" % str.replace("'", "\\'")
|
2012-06-16 20:27:14 +00:00
|
|
|
return str
|
|
|
|
|
2012-05-04 21:20:33 +00:00
|
|
|
if 'CFLAGS' in configure_env:
|
|
|
|
cflags.append(configure_env['CFLAGS'])
|
2012-06-16 20:27:14 +00:00
|
|
|
n.variable('cflags', ' '.join(shell_escape(flag) for flag in cflags))
|
2012-05-04 21:20:33 +00:00
|
|
|
if 'LDFLAGS' in configure_env:
|
|
|
|
ldflags.append(configure_env['LDFLAGS'])
|
2012-06-16 20:27:14 +00:00
|
|
|
n.variable('ldflags', ' '.join(shell_escape(flag) for flag in ldflags))
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
|
|
|
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_msvc():
|
2012-01-04 01:04:49 +00:00
|
|
|
n.rule('cxx',
|
2014-11-14 18:53:33 +00:00
|
|
|
command='$cxx $cflags -c $in /Fo$out',
|
2013-01-06 23:33:46 +00:00
|
|
|
description='CXX $out',
|
2014-11-14 18:53:33 +00:00
|
|
|
deps='msvc' # /showIncludes is included in $cflags.
|
|
|
|
)
|
2012-01-04 01:04:49 +00:00
|
|
|
else:
|
|
|
|
n.rule('cxx',
|
2012-03-23 15:40:04 +00:00
|
|
|
command='$cxx -MMD -MT $out -MF $out.d $cflags -c $in -o $out',
|
2012-01-04 01:04:49 +00:00
|
|
|
depfile='$out.d',
|
2013-04-10 19:51:04 +00:00
|
|
|
deps='gcc',
|
2012-01-04 01:04:49 +00:00
|
|
|
description='CXX $out')
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
|
|
|
|
2013-04-30 15:29:13 +00:00
|
|
|
if host.is_msvc():
|
2011-12-02 05:33:33 +00:00
|
|
|
n.rule('ar',
|
2012-01-14 03:17:14 +00:00
|
|
|
command='lib /nologo /ltcg /out:$out $in',
|
2012-01-04 01:04:49 +00:00
|
|
|
description='LIB $out')
|
2013-04-30 15:29:13 +00:00
|
|
|
elif host.is_mingw():
|
2012-01-04 01:04:49 +00:00
|
|
|
n.rule('ar',
|
|
|
|
command='cmd /c $ar cqs $out.tmp $in && move /Y $out.tmp $out',
|
2011-12-02 05:33:33 +00:00
|
|
|
description='AR $out')
|
|
|
|
else:
|
|
|
|
n.rule('ar',
|
2012-01-04 01:04:49 +00:00
|
|
|
command='rm -f $out && $ar crs $out $in',
|
2011-12-02 05:33:33 +00:00
|
|
|
description='AR $out')
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
|
|
|
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_msvc():
|
2012-01-04 01:04:49 +00:00
|
|
|
n.rule('link',
|
|
|
|
command='$cxx $in $libs /nologo /link $ldflags /out:$out',
|
|
|
|
description='LINK $out')
|
|
|
|
else:
|
|
|
|
n.rule('link',
|
|
|
|
command='$cxx $ldflags -o $out $in $libs',
|
|
|
|
description='LINK $out')
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
|
|
|
|
|
|
|
objs = []
|
|
|
|
|
2014-11-21 00:56:17 +00:00
|
|
|
if platform.supports_ninja_browse():
|
2011-05-02 03:33:16 +00:00
|
|
|
n.comment('browse_py.h is used to inline browse.py.')
|
|
|
|
n.rule('inline',
|
2015-06-22 22:28:04 +00:00
|
|
|
command='"%s"' % src('inline.sh') + ' $varname < $in > $out',
|
2011-05-02 03:33:16 +00:00
|
|
|
description='INLINE $out')
|
|
|
|
n.build(built('browse_py.h'), 'inline', src('browse.py'),
|
2015-05-26 21:19:28 +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-29 21:00:27 +00:00
|
|
|
n.comment('the depfile parser and ninja lexers are generated using re2c.')
|
2012-08-16 03:11:27 +00:00
|
|
|
def has_re2c():
|
2012-08-16 18:28:03 +00:00
|
|
|
try:
|
2012-09-11 03:42:32 +00:00
|
|
|
proc = subprocess.Popen(['re2c', '-V'], stdout=subprocess.PIPE)
|
|
|
|
return int(proc.communicate()[0], 10) >= 1103
|
2012-08-16 18:28:03 +00:00
|
|
|
except OSError:
|
|
|
|
return False
|
2012-08-16 03:11:27 +00:00
|
|
|
if has_re2c():
|
2012-08-16 18:28:03 +00:00
|
|
|
n.rule('re2c',
|
|
|
|
command='re2c -b -i --no-generation-date -o $out $in',
|
|
|
|
description='RE2C $out')
|
|
|
|
# Generate the .cc files in the source directory so we can check them in.
|
|
|
|
n.build(src('depfile_parser.cc'), 're2c', src('depfile_parser.in.cc'))
|
|
|
|
n.build(src('lexer.cc'), 're2c', src('lexer.in.cc'))
|
2012-08-16 03:11:27 +00:00
|
|
|
else:
|
2012-10-19 07:46:09 +00:00
|
|
|
print("warning: A compatible version of re2c (>= 0.11.3) was not found; "
|
2012-09-11 03:42:32 +00:00
|
|
|
"changes to src/*.in.cc will not affect your build.")
|
2011-12-07 20:08:00 +00:00
|
|
|
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',
|
2015-06-12 06:53:32 +00:00
|
|
|
'clparser',
|
2013-09-10 22:34:01 +00:00
|
|
|
'debug_flags',
|
2011-12-20 21:06:01 +00:00
|
|
|
'depfile_parser',
|
2012-12-17 17:08:15 +00:00
|
|
|
'deps_log',
|
2011-12-20 21:06:01 +00:00
|
|
|
'disk_interface',
|
|
|
|
'edit_distance',
|
|
|
|
'eval_env',
|
|
|
|
'graph',
|
|
|
|
'graphviz',
|
2011-12-29 21:00:27 +00:00
|
|
|
'lexer',
|
2013-04-09 03:54:28 +00:00
|
|
|
'line_printer',
|
2012-07-09 19:09:31 +00:00
|
|
|
'manifest_parser',
|
2012-01-05 21:35:47 +00:00
|
|
|
'metrics',
|
2011-12-20 21:06:01 +00:00
|
|
|
'state',
|
2017-04-26 07:10:27 +00:00
|
|
|
'string_piece_util',
|
2012-10-30 16:40:59 +00:00
|
|
|
'util',
|
|
|
|
'version']:
|
2011-05-02 00:48:39 +00:00
|
|
|
objs += cxx(name)
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_windows():
|
2012-09-20 14:05:11 +00:00
|
|
|
for name in ['subprocess-win32',
|
|
|
|
'includes_normalize-win32',
|
|
|
|
'msvc_helper-win32',
|
|
|
|
'msvc_helper_main-win32']:
|
|
|
|
objs += cxx(name)
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_msvc():
|
2012-07-27 19:02:59 +00:00
|
|
|
objs += cxx('minidump-win32')
|
2012-01-04 01:04:49 +00:00
|
|
|
objs += cc('getopt')
|
2011-05-03 04:43:18 +00:00
|
|
|
else:
|
2012-07-27 19:24:40 +00:00
|
|
|
objs += cxx('subprocess-posix')
|
2015-01-13 01:53:14 +00:00
|
|
|
if platform.is_aix():
|
|
|
|
objs += cc('getopt')
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_msvc():
|
2012-01-04 01:04:49 +00:00
|
|
|
ninja_lib = n.build(built('ninja.lib'), 'ar', objs)
|
|
|
|
else:
|
|
|
|
ninja_lib = n.build(built('libninja.a'), 'ar', objs)
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
|
|
|
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_msvc():
|
2012-01-04 01:04:49 +00:00
|
|
|
libs.append('ninja.lib')
|
|
|
|
else:
|
|
|
|
libs.append('-lninja')
|
2011-09-07 23:47:57 +00:00
|
|
|
|
2015-01-13 01:48:07 +00:00
|
|
|
if platform.is_aix():
|
|
|
|
libs.append('-lperfstat')
|
|
|
|
|
2011-12-28 03:39:16 +00:00
|
|
|
all_targets = []
|
|
|
|
|
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-12-28 03:39:16 +00:00
|
|
|
all_targets += ninja
|
2011-05-02 00:48:39 +00:00
|
|
|
|
2014-11-14 18:53:33 +00:00
|
|
|
if options.bootstrap:
|
|
|
|
# We've built the ninja binary. Don't run any more commands
|
|
|
|
# through the bootstrap executor, but continue writing the
|
|
|
|
# build.ninja file.
|
|
|
|
n = ninja_writer
|
|
|
|
|
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
|
|
|
|
2011-12-20 19:38:56 +00:00
|
|
|
objs = []
|
|
|
|
|
2011-09-11 22:29:38 +00:00
|
|
|
for name in ['build_log_test',
|
|
|
|
'build_test',
|
|
|
|
'clean_test',
|
2015-06-12 06:53:32 +00:00
|
|
|
'clparser_test',
|
2011-12-19 18:59:29 +00:00
|
|
|
'depfile_parser_test',
|
2012-12-17 17:08:15 +00:00
|
|
|
'deps_log_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
|
|
|
'graph_test',
|
2011-12-29 21:00:27 +00:00
|
|
|
'lexer_test',
|
2012-07-09 19:09:31 +00:00
|
|
|
'manifest_parser_test',
|
2013-04-09 01:02:05 +00:00
|
|
|
'ninja_test',
|
2011-09-11 22:29:38 +00:00
|
|
|
'state_test',
|
2017-04-26 07:10:27 +00:00
|
|
|
'string_piece_util_test',
|
2011-09-11 22:29:38 +00:00
|
|
|
'subprocess_test',
|
|
|
|
'test',
|
|
|
|
'util_test']:
|
2014-09-18 02:48:26 +00:00
|
|
|
objs += cxx(name)
|
2013-04-30 15:29:13 +00:00
|
|
|
if platform.is_windows():
|
2012-08-12 22:51:21 +00:00
|
|
|
for name in ['includes_normalize_test', 'msvc_helper_test']:
|
2014-09-18 02:48:26 +00:00
|
|
|
objs += cxx(name)
|
2011-09-07 23:47:57 +00:00
|
|
|
|
2011-12-28 03:39:16 +00:00
|
|
|
ninja_test = n.build(binary('ninja_test'), 'link', objs, implicit=ninja_lib,
|
2014-11-22 02:18:30 +00:00
|
|
|
variables=[('libs', libs)])
|
2011-05-02 00:48:39 +00:00
|
|
|
n.newline()
|
2011-12-28 03:39:16 +00:00
|
|
|
all_targets += ninja_test
|
2011-05-02 00:48:39 +00:00
|
|
|
|
2012-04-30 15:06:28 +00:00
|
|
|
|
2012-10-03 08:10:53 +00:00
|
|
|
n.comment('Ancillary executables.')
|
2017-04-12 02:12:38 +00:00
|
|
|
|
|
|
|
for name in ['build_log_perftest',
|
|
|
|
'canon_perftest',
|
|
|
|
'depfile_parser_perftest',
|
|
|
|
'hash_collision_bench',
|
|
|
|
'manifest_parser_perftest',
|
|
|
|
'clparser_perftest']:
|
|
|
|
objs = cxx(name)
|
|
|
|
all_targets += n.build(binary(name), '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('Generate a graph using the "graph" tool.')
|
|
|
|
n.rule('gendot',
|
2012-09-04 23:03:35 +00:00
|
|
|
command='./ninja -t graph all > $out')
|
2011-05-02 00:48:39 +00:00
|
|
|
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',
|
2013-02-17 01:13:10 +00:00
|
|
|
command='asciidoc -b docbook -d book -o $out $in',
|
|
|
|
description='ASCIIDOC $out')
|
|
|
|
n.rule('xsltproc',
|
|
|
|
command='xsltproc --nonet doc/docbook.xsl $in > $out',
|
|
|
|
description='XSLTPROC $out')
|
2015-11-17 22:04:50 +00:00
|
|
|
docbookxml = n.build(built('manual.xml'), 'asciidoc', doc('manual.asciidoc'))
|
|
|
|
manual = n.build(doc('manual.html'), 'xsltproc', docbookxml,
|
|
|
|
implicit=[doc('style.css'), doc('docbook.xsl')])
|
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()
|
|
|
|
|
2015-11-17 22:04:50 +00:00
|
|
|
n.rule('dblatex',
|
|
|
|
command='dblatex -q -o $out -p doc/dblatex.xsl $in',
|
|
|
|
description='DBLATEX $out')
|
|
|
|
n.build(doc('manual.pdf'), 'dblatex', docbookxml,
|
|
|
|
implicit=[doc('dblatex.xsl')])
|
|
|
|
|
2011-05-02 00:48:39 +00:00
|
|
|
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',
|
2012-08-16 18:31:37 +00:00
|
|
|
['README', 'COPYING'],
|
2011-05-02 00:48:39 +00:00
|
|
|
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()
|
|
|
|
|
2013-04-30 15:29:13 +00:00
|
|
|
if not host.is_mingw():
|
2011-12-02 05:33:33 +00:00
|
|
|
n.comment('Regenerate build files if build script changes.')
|
|
|
|
n.rule('configure',
|
2015-11-17 22:26:18 +00:00
|
|
|
command='${configure_env}%s $root/configure.py $configure_args' %
|
2012-05-04 15:33:46 +00:00
|
|
|
options.with_python,
|
2011-12-02 05:33:33 +00:00
|
|
|
generator=True)
|
|
|
|
n.build('build.ninja', 'configure',
|
2015-11-17 22:26:18 +00:00
|
|
|
implicit=['$root/configure.py',
|
|
|
|
os.path.normpath('$root/misc/ninja_syntax.py')])
|
2011-12-02 05:33:33 +00:00
|
|
|
n.newline()
|
2011-09-06 20:00:38 +00:00
|
|
|
|
2011-12-20 19:38:56 +00:00
|
|
|
n.default(ninja)
|
2011-12-28 03:39:16 +00:00
|
|
|
n.newline()
|
|
|
|
|
2013-04-30 15:29:13 +00:00
|
|
|
if host.is_linux():
|
2012-07-26 12:21:30 +00:00
|
|
|
n.comment('Packaging')
|
|
|
|
n.rule('rpmbuild',
|
2012-09-14 10:46:01 +00:00
|
|
|
command="misc/packaging/rpmbuild.sh",
|
|
|
|
description='Building rpms..')
|
|
|
|
n.build('rpm', 'rpmbuild')
|
2012-07-26 12:21:30 +00:00
|
|
|
n.newline()
|
|
|
|
|
2011-12-28 03:39:16 +00:00
|
|
|
n.build('all', 'phony', all_targets)
|
2011-05-09 04:53:34 +00:00
|
|
|
|
2014-11-14 18:53:33 +00:00
|
|
|
n.close()
|
2012-10-19 07:46:09 +00:00
|
|
|
print('wrote %s.' % BUILD_FILENAME)
|
2014-11-14 18:53:33 +00:00
|
|
|
|
|
|
|
if options.bootstrap:
|
|
|
|
print('bootstrap complete. rebuilding...')
|
2015-10-21 22:37:50 +00:00
|
|
|
|
|
|
|
rebuild_args = []
|
|
|
|
|
|
|
|
if platform.can_rebuild_in_place():
|
|
|
|
rebuild_args.append('./ninja')
|
|
|
|
else:
|
|
|
|
if platform.is_windows():
|
|
|
|
bootstrap_exe = 'ninja.bootstrap.exe'
|
|
|
|
final_exe = 'ninja.exe'
|
|
|
|
else:
|
|
|
|
bootstrap_exe = './ninja.bootstrap'
|
|
|
|
final_exe = './ninja'
|
|
|
|
|
2014-11-14 18:53:33 +00:00
|
|
|
if os.path.exists(bootstrap_exe):
|
|
|
|
os.unlink(bootstrap_exe)
|
2015-10-21 22:37:50 +00:00
|
|
|
os.rename(final_exe, bootstrap_exe)
|
|
|
|
|
|
|
|
rebuild_args.append(bootstrap_exe)
|
|
|
|
|
|
|
|
if options.verbose:
|
|
|
|
rebuild_args.append('-v')
|
|
|
|
|
|
|
|
subprocess.check_call(rebuild_args)
|