check in a script that generates build.ninja
authorEvan Martin <martine@danga.com>
Mon, 2 May 2011 00:48:39 +0000 (17:48 -0700)
committerEvan Martin <martine@danga.com>
Mon, 2 May 2011 01:02:13 +0000 (18:02 -0700)
This will allow us to:
1) simplify bootstrap
2) generate windows-style paths on windows

It is also in line with ninja's philosophy: the build file syntax
is simple because the build files are easy to generate.

.gitignore
bootstrap.sh
build.ninja [deleted file]
gen-build-file.py [new file with mode: 0755]
misc/ninja.py

index 7433a62..6f33f81 100644 (file)
@@ -1,10 +1,10 @@
 *.pyc
 TAGS
 /build
+/build.ninja
 /ninja
 /ninja_test
 /graph.png
 /README.html
-/config.ninja
 /manual.html
 /doxygen
index 7722c18..6ff90e5 100755 (executable)
@@ -18,30 +18,24 @@ set -e
 
 SYSTEMNAME=`uname -s`
 
+# Compute system-specific CFLAGS/LDFLAGS as used in both in the below
+# g++ call as well as in the later gen-build-file.py.
 if [ "${SYSTEMNAME}" = "Linux" ]; then
-    EXTRA_CFLAGS=
-    EXTRA_LDFLAGS=
+    export CFLAGS=
+    export LDFLAGS=
 elif [ "${SYSTEMNAME}" = "FreeBSD" ]; then
-    EXTRA_CFLAGS="-I/usr/local/include"
-    EXTRA_LDFLAGS="-L/usr/local/lib"
+    export CFLAGS="-I/usr/local/include"
+    export LDFLAGS="-L/usr/local/lib"
 fi
 
-cat >config.ninja <<EOT
-# This file is generated by bootstrap.sh.
-conf_cflags = -O2 ${EXTRA_CFLAGS}
-conf_ldflags = -s ${EXTRA_LDFLAGS}
-# When developing:
-# conf_cflags = -g -Wall ${EXTRA_CFLAGS}
-# conf_ldflags = ${EXTRA_LDFLAGS}
-EOT
-
 echo "Building ninja manually..."
 mkdir -p build
 ./src/inline.sh kBrowsePy < src/browse.py > build/browse_py.h
 srcs=$(ls src/*.cc | grep -v test)
-g++ -Wno-deprecated ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o ninja.bootstrap $srcs
+g++ -Wno-deprecated ${CFLAGS} ${LDFLAGS} -o ninja.bootstrap $srcs
 
 echo "Building ninja using itself..."
+./gen-build-file.py > build.ninja
 ./ninja.bootstrap ninja
 rm ninja.bootstrap
 
diff --git a/build.ninja b/build.ninja
deleted file mode 100644 (file)
index 1b27790..0000000
+++ /dev/null
@@ -1,105 +0,0 @@
-# This file is used to build ninja itself.
-
-# NOTE: it is an explicit non-goal of ninja to make it convenient to
-# write these files by hand.  For a real project, you'd generate this
-# file from higher-level rules.
-
-# I was tempted to generate this file even for ninja itself but I
-# figured it'd be easier to bootstrap this way.
-
-builddir = build
-cxx = g++
-#cxx = /home/evanm/projects/src/llvm/Release+Asserts/bin/clang++
-cflags = -g -Wall -Wno-deprecated -fno-exceptions -fvisibility=hidden -pipe
-# -rdynamic is needed for backtrace()
-ldflags = -g -rdynamic
-
-# bootstrap.sh generates a "config.ninja" file, which contains some
-# minor build customization for development purposes.
-include config.ninja
-
-rule cxx
-  depfile = $out.d
-  command = $cxx -MMD -MF $out.d $conf_cflags $cflags -c $in -o $out
-  description = CC $out
-
-rule ar
-  command = ar crs $out $in
-  description = AR $out
-
-rule link
-  command = $cxx $conf_ldflags $ldflags -o $out $in
-  description = LINK $out
-
-rule inline
-  command = src/inline.sh $varname < $in > $out
-  description = INLINE $out
-
-build $builddir/browse_py.h: inline src/browse.py
-  varname = kBrowsePy
-
-build $builddir/browse.o: cxx src/browse.cc | src/inline.sh || $builddir/browse_py.h
-build $builddir/build.o: cxx src/build.cc
-build $builddir/build_log.o: cxx src/build_log.cc
-build $builddir/eval_env.o: cxx src/eval_env.cc
-build $builddir/graph.o: cxx src/graph.cc
-build $builddir/graphviz.o: cxx src/graphviz.cc
-build $builddir/parsers.o: cxx src/parsers.cc
-build $builddir/subprocess.o: cxx src/subprocess.cc
-build $builddir/util.o: cxx src/util.cc
-build $builddir/clean.o: cxx src/clean.cc
-build $builddir/ninja_jumble.o: cxx src/ninja_jumble.cc
-build $builddir/ninja.a: ar $builddir/browse.o $builddir/build.o \
-    $builddir/build_log.o $builddir/eval_env.o $builddir/graph.o \
-    $builddir/graphviz.o $builddir/parsers.o $builddir/subprocess.o \
-    $builddir/util.o $builddir/ninja_jumble.o $builddir/clean.o
-
-build $builddir/ninja.o: cxx src/ninja.cc
-build ninja: link $builddir/ninja.o $builddir/ninja.a
-
-build $builddir/build_test.o: cxx src/build_test.cc
-build $builddir/build_log_test.o: cxx src/build_log_test.cc
-build $builddir/graph_test.o: cxx src/graph_test.cc
-build $builddir/ninja_test.o: cxx src/ninja_test.cc
-build $builddir/parsers_test.o: cxx src/parsers_test.cc
-build $builddir/subprocess_test.o: cxx src/subprocess_test.cc
-build $builddir/test.o: cxx src/test.cc
-build $builddir/util_test.o: cxx src/util_test.cc
-build ninja_test: link $builddir/build_test.o $builddir/build_log_test.o \
-    $builddir/graph_test.o $builddir/ninja_test.o $builddir/parsers_test.o \
-    $builddir/subprocess_test.o $builddir/test.o $builddir/util_test.o \
-    $builddir/ninja.a
-  ldflags = -g -rdynamic -lgtest -lgtest_main -lpthread
-
-
-# Generate a graph using the -g flag.
-rule gendot
-  command = ./ninja -t graph > $out
-rule gengraph
-  command = dot -Tpng $in > $out
-
-build $builddir/graph.dot: gendot ninja build.ninja
-build graph.png: gengraph $builddir/graph.dot
-
-# Generate the manual.
-rule asciidoc
-  command = asciidoc -a toc $in
-  description = ASCIIDOC $in
-
-build manual.html: asciidoc manual.asciidoc
-build manual: phony || manual.html
-
-# Generate Doxygen.
-rule doxygen
-  command = doxygen $in
-  description = DOXYGEN $in
-
-doxygen_mainpage_generator = ./gen_doxygen_mainpage.sh
-
-rule doxygen_mainpage
-  command = $doxygen_mainpage_generator $in > $out
-  description = DOXYGEN_MAINPAGE $out
-
-build $builddir/doxygen_mainpage: doxygen_mainpage \
-  README HACKING COPYING | $doxygen_mainpage_generator
-build doxygen: doxygen doxygen.config | $builddir/doxygen_mainpage || $builddir/doxygen_mainpage
diff --git a/gen-build-file.py b/gen-build-file.py
new file mode 100755 (executable)
index 0000000..904035c
--- /dev/null
@@ -0,0 +1,147 @@
+#!/usr/bin/python
+#
+# 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."""
+
+import os
+import sys
+sys.path.insert(0, 'misc')
+
+import ninja
+
+n = ninja.Writer(sys.stdout)
+n.comment('This file is used to build ninja itself.')
+n.comment('It is generated by ' + os.path.basename(__file__) + '.')
+n.newline()
+
+def src(filename):
+    return os.path.join('src', filename)
+def built(filename):
+    return os.path.join('$builddir', filename)
+def cxx(name, **kwargs):
+    return n.build(built(name + '.o'), 'cxx', src(name + '.cc'), **kwargs)
+
+n.variable('builddir', 'build')
+n.variable('cxx', os.environ.get('CC', 'g++'))
+cflags = ('-O2 -g -Wall -Wno-deprecated -fno-exceptions -fvisibility=hidden '
+          '-pipe')
+if 'CFLAGS' in os.environ:
+    cflags += ' ' + os.environ['CFLAGS']
+n.variable('cflags', cflags)
+n.variable('ldflags', os.environ.get('LDFLAGS', ''))
+n.newline()
+
+n.rule('cxx',
+       command='$cxx -MMD -MF $out.d $cflags -c $in -o $out',
+       depfile='$out.d',
+       description='CC $out')
+n.newline()
+
+n.rule('ar',
+       command='ar crs $out $in',
+       description='AR $out')
+n.newline()
+
+n.rule('link',
+       command='$cxx $ldflags -o $out $in',
+       description='LINK $out')
+n.newline()
+
+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'),
+        variables=[('varname', 'kBrowsePy')])
+n.newline()
+
+objs = []
+
+n.comment("TODO: this shouldn't need to depend on inline.sh.")
+objs += cxx('browse',
+            implicit='src/inline.sh',
+            order_only=built('browse_py.h'))
+n.newline()
+
+n.comment('Core source files all build into ninja library.')
+for name in ['build', 'build_log', 'clean', 'eval_env', 'graph', 'graphviz',
+             'parsers', 'subprocess', 'util',
+             'ninja_jumble']:
+    objs += cxx(name)
+ninja_lib = n.build(built('ninja.a'), 'ar', objs)
+n.newline()
+
+n.comment('Main executable is library plus main() function.')
+objs = cxx('ninja')
+n.build('ninja', 'link', objs + ninja_lib)
+
+n.comment('Tests all build into ninja_test executable.')
+objs = []
+for name in ['build_test', 'build_log_test', 'graph_test', 'ninja_test',
+             'parsers_test', 'subprocess_test', 'util_test',
+             'test']:
+    objs += cxx(name)
+ldflags = '-lgtest -lgtest_main -lpthread'
+if 'LDFLAGS' in os.environ:
+    ldflags += ' ' + os.environ.get('LDFLAGS')
+n.build('ninja_test', 'link',
+        objs + ninja_lib,
+        variables=[('ldflags', ldflags)])
+n.newline()
+
+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',
+       command='asciidoc -a toc $in',
+       description='ASCIIDOC $in')
+n.build('manual.html', 'asciidoc', 'manual.asciidoc')
+n.build('manual', 'phony',
+        order_only='manual.html')
+n.newline()
+
+n.comment('Generate Doxygen.')
+n.rule('doxygen',
+       command='doxygen $in',
+       description='DOXYGEN $in')
+n.variable('doxygen_mainpage_generator',
+           './gen_doxygen_mainpage.sh')
+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'])
+n.build('doxygen', 'doxygen', 'doxygen.config',
+        implicit=mainpage,
+        order_only=mainpage)
+n.newline()
+
+n.comment('Regenerate build files if build script changes.')
+n.rule('gen-build-file',
+       command='./gen-build-file.py > $out')
+n.build('build.ninja', 'gen-build-file',
+        implicit='gen-build-file.py')
index f13adfd..e1abbf7 100644 (file)
@@ -52,6 +52,8 @@ class Writer(object):
             for key, val in variables:
                 self.variable(key, val, indent=1)
 
+        return outputs
+
     def _line(self, text, indent=0):
         while len(text) > self.width:
             space = text.rfind(' ', 0, self.width - 4)