I want to write a little tool that parses build definitions and converts them to a ninja.build
file. It should not abstract compilation like CMake or Meson, but be similar to make in that you manually write the commands for a build step.
I have built an almost working solution in Python, but there are some stepping stones that I could not easily solve yet and the design is not ideal. My main goal is to have modular build definitions and be able to modify these definitions from another project for its requirements. Imagine
use platform
use platform.compiler as compiler
# Another user created build definition file.
use foolib
build_dir : join(project_path, 'build')
foolib.build_dir : build_dir # Here we change the build_dir of the library
foolib.defines : array(foolib.defines, 'FOOLIB_LEGACY_API') # And here we add another define
sources : wildcard('source/*.cpp')
objects : transform(sources, 'source', join(build_dir, 'obj'), platform.objsuffix)
executable : join(build_dir, platform.exename('main'))
includes : array(localpath('include'), foolib.includes)
defines : array(foolib.defines, 'BUFSIZE=512')
flags : compiler.includes(includes) compiler.defines(defines)
# Automatically deduces to issue a command for each source/object pair
target sources -> objects
compiler.cpp quote(input) flags compiler.compile_only compiler.out(output)
target array(objects, foolib.library) -> executable
compiler.cpp quote(input) flags compiler.out(executable)
foolib
would look pretty similar, outputting a library file from source files to its build_dir
.
I use lazy variable expansion at all times to allow things like changing the build directory of an imported project or update macro definitions used while compiling the dependency.
The problem I have with lazy evaluation is with self-references, and it also adds a lot of overhead. However, doing immediate evaluation on assignment will not allow this late configuration change.
Another aspect is that I want to be able to specify options on the command-line that will have effect on the modules configuration. For instance, let's say I want to build my program in debug mode.
if debug == 'true'
executable : join(build_dir, platform.exename('main-dbg'))
flags : flags compiler.debug
defines : array(defines, 'DEBUG')
If foolib
uses the same conditional to update its compilation flags and defines, it will be built in debug mode as well. Although in this special case, it would be desirable, there might be a case where I want to be specific to make it set only for my program but not for foolib
module.
Do you have ideas for improvements, a different or better design?