Shogun is a small Python 3 module aimed at generating Ninja build files. It attempts to be as simple as possible and does not offer rule generation by design. Instead, you are expected to provide your own rule file to be used with the target list generated by Shogun.
Building a simple C project. configure.py
:
import shogun
obj = shogun.Objects("source/*.c", "cc", "o")
exe = shogun.Assembly("$builddir/helloworld", "ccld", obj,
options = { "libs": "-lm" })
shogun.build(obj, exe)
And build.ninja
:
rule cc
command = $cc $cflags -MMD -MT $out -MF $out.d -c $in -o $out
description = CC $out
depfile = $out.d
deps = gcc
rule ccld
command = $cc $ldflags $libs $in -o $out
description = CCLD $out
include targets.ninja
cc = clang
cflags = -Wall -Wextra -pedantic -Iinclude -O2 -pipe
ldflags =
default $builddir/helloworld
This order is important: rules must be defined before targets and our extra
variables as well as default
depend on variables that are defined in
targets.ninja
.
You can also build a multi executable project easily:
import shogun
obj_common = shogun.Objects("common/*.c", "cc", "o")
obj1 = shogun.Objects("src1/*.c", "cc", "o")
obj2 = shogun.Objects("src2/*.c", "cc", "o")
exe1 = shogun.Assembly("$builddir/exe1", "ccld", obj_common, obj1)
exe2 = shogun.Assembly("$builddir/exe2", "ccld", obj_common, obj2)
shogun.build(obj_common, obj1, obj2, exe1, exe2)
See Ninja's documentation to familiarize
yourself with Ninja itself.
Shogun provides three simple classes to help build dependency trees and
inserting variables into your build files:
Objects(self, pathname, rule, extout, *, recursive = False)
This is for adding batches of files such as C object files. pathname
and
recursive
are passed through to glob.glob()
to generate listings. rule
is
the Ninja rule to use to build the objects and extout
is the extension to
substitute for the output file. All objects go to $builddir
.
Assembly(self, path, rule, *objects, options = {})
Used for linking executables and packing libraries. path
is the output file's
path, objects
are instances of Objects
or Assembly
and options
are extra
variables to be passed to the rules, such as the libraries to link with.
Variables(self, *, comment = None, **variables)
Extra variables to insert into the build.
Shogun currently provides two built-in variables that it uses for its own functionality:
$builddir
Output directory for the build.
build(*targets, out = "targets.ninja", builddir = None)
targets
is a list of objects and assemblies to build. out
is the build file
to be generated. If builddir
it defaults to build
.