How to Simplify Your SConscripts

This is the sixth post in my SCons series. The topic of this post is building reusable infrastructure that can extremely simplify your module-level SConscript files.

Starting with the first non-trivial SCons project, the module-level SConscript files contained too much repetitive code. The goal of this enhancement is to go back to minimalistic SConscript files. The objective is to let the developer define the module-level targets with minimal code, and no hassle.

I continue using the same C++ project that I introduced in the basic example. In this post I present SCons shortcuts that are available in module-level SConscript files. These shortcuts are Python functions that take care of dirty details behind the scenes.

The final result is available on my GitHub scons-series repository.

Continue Reading…

On Python Closures

By Tuesday, October 14, 2014 1 Permalink 1

Closures are an interesting concept in computer programming. They can be powerful and useful, but they also can be tricky to understand and use well.

In this post, I try to provide a clear explanation of closures, and go into specifics on closures support in Python.

Being an “interesting programming concept” is nice, but not enough for me. I will present a real use-case for closures in Python.

No programming language is perfect (though Python comes close 😉 ). Programming languages that choose to support closures often need to make difficult tradeoffs. I will discuss some of the choices that were made in Python, and their implications.

Continue Reading…

Integrating SCons Flavors with the Terminal

This is the fifth post in my SCons series. The topic of this post is improving the previous multi-flavor project via terminal integration.

It can be a hassle to handle multi-flavor projects. In the multi-flavor project post, I suggested a solution to simplify the multi-flavor build process. Using that solution, you just run scons flavor_name to build a specific flavor. But there’s still room for improvement! If you want to run a program you just built, you still need to specify the path to the flavored executable.

For example, say you built a project with a program named say_hi in the module hello. You built it by running scons debug. To run it you execute ./build/debug/hello/say_hi. It can be a hassle to write ./build/debug over and over. Even worse, it’s the first time you need to know about the layout of the build directory. Up until now, such details were nicely hidden in the config file.

In addition, you may often want to work with just one flavor. You may be developing a new feature, and you want to only build and run the debug flavor. If you run scons without the debug argument, all flavors will be built. This can be annoying and time consuming.

In this post, I suggest a helper script to make things simpler. The purpose of the script is to allow you to activate a flavor in a terminal session. While a flavor is active, magical things happen:

  1. Running scons with no arguments builds only the active flavor.
  2. The executable programs of the active flavor can be executed more conveniently.
  3. The active flavor is indicated in the terminal prompt.

The final result is available on my GitHub scons-series repository. In the rest of this post I go into the details of the helper script and related changes.

Continue Reading…

The Right Way to Get the Directory of a bash Script

By Wednesday, October 8, 2014 0 , , , Permalink 4

When writing bash scripts, you might want to get the directory that contains your script. There are multiple ways to accomplish that. Due to the flexibility of bash, some solutions work in some cases, but not in others.

In this post, I evolve from a naive solution to a robust and consistent solution for this common problem. Spoiler – a “good enough” middle ground that I often use is "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )", as long as I know that symbolic links are out of the game.

Continue Reading…

Multi-Flavored SCons Project

This is the fourth post in my SCons series. The topic of this post is setting up a multi-flavor C++ project using SCons, with a separate build directory. By “flavor”, I mean something like debug vs. release.

In C++ projects, it is common to build multiple variants, or flavors, of the project. A debug flavor may build more quickly, and contain debug symbols. A release flavor may perform optimizations for runtime or other metrics. The different flavors serve different purposes, and they all can co-exist. The developer may choose which flavor(s) to build and run as she pleases.

In this post, I show how to use SCons to manage multiple flavors in a C++ project. My requirements from flavor support in a SCons-powered C++ project:

  1. Define flavor profiles easily. Allow to customize construction parameters per-flavor. Support common parameters that can be overridden by flavors.
  2. Support flavor-specific build directory. Build outputs should reside under their flavor build directory. Multiple flavors can co-exist at the same time, without interfering with each other. Incremental builds can be done per-flavor.
  3. Let the developer choose what to build. Allow choosing one flavor, all flavors, or any subset. Syntax should be simple and intuitive.
  4. The developer can execute built programs at any flavor she wants.

I add multi-flavor support on top of the previous episode in the series.

The final result is available on my GitHub scons-series repository. In the rest of this post I go into the details of what I came up with.

Continue Reading…

Multi-Module SCons Project With Separate Build Directory

By Thursday, September 25, 2014 0 , , Permalink 3

This is the third post in my SCons series. The topic of this post is setting up a multi-module C++ project using SCons, with a separate build directory.

In previous posts in the series I introduced the SCons open source build tool, and described a simple C++ example project that uses SCons.

In this post I use the exact same C++ project from the basic example. I rewrite the build scripts, using SCons, to achieve the following properties:

  1. Divide the project into distinct modules. A module maps to a directory in the project tree. Each module contains a SConscript file, describing the targets included in that module.
  2. Separate the build output directory from the module source code directories. I like my project tree clean and tidy, without object files scattered between source files.
  3. Allow build targets in modules to refer to other targets in other modules easily. This is required, for example, when a program in one module uses functions from a static library in another module.

The final result is available on my GitHub scons-series repository. In the rest of this post I explain the details of what I came up with.

Continue Reading…

A Basic SCons Project Example

By Thursday, September 18, 2014 0 , Permalink 2

SCons is an open source software construction tool – a next generation build tool.

I have previously written an introduction to SCons.

This post is a short example of a basic C++ project, that uses SCons.

The purpose of this basic example is to set a baseline. It demonstrates how to build a non-trivial C++ project with out-of-the-box SCons. Future posts in my SCons series will describe various extensions to SCons, so it is important to be able to compare to a simple baseline.

Continue Reading…

SCons Intro: A Sane Software Construction Tool

By Thursday, September 11, 2014 0 , , Permalink 5

If you ever built C/C++ programs, you probably know about Make. But Make has long been broken. Everybody knows that.

SCons is an open source software construction tool – a next generation build tool.

You can think of SCons as an improved, cross-platform substitute for Make. One that also bakes in features from autoconf/automake and ccache.

SCons is my chosen C/C++ software build framework.

This is the first post in a series of SCons posts. In the series I will describe SCons, and analyze its strengths and weaknesses. I also explain how I use it (in DayJob) to build non-trivial software, and explore potential enhancements to optimize my workflow.

I open the series with an introduction to SCons.

Continue Reading…