1

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?

3
  • 3
    Why would you want self references? Would that create a circular dependency as it tries to determine what sequence of operations to execute? Feb 3, 2015 at 1:00
  • For instance, you could continue to transform the same variable based on its previous value, I also want to include IF statements. I updated the question about this aspect Feb 3, 2015 at 18:33
  • @RichardLevasseur Oh and yes, it would result in a circular dependency resulting in stack overflow during evaluation. I'm interpreting the AST, unfortunately, currently, it doesn't currently allow to replace a name reference with its original value. Since it would be quite a little work to implement, I first asked this question. Feb 3, 2015 at 19:37

1 Answer 1

1

It's probably a good idea to read through https://wiki.debian.org/UpstreamGuide#Your_Build_System (other Linux distros will probably have something similar). Respecting environment variables (e.g. CFLAGS) is important (see the SCons and waf discussion for the problems created by not doing this).

1
  • Thanks a lot for this helpful link, I will definitely take this information into consideration to produce a quality and acceptable build system. I think it also helps me indirectly to improve the overall design. Feb 5, 2015 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.