Main Content

Scons - a build system in Python - building hello world

Archive - Originally posted on "The Horse's Mouth" - 2016-10-29 21:23:46 - Graham Ellis

Scons ...

It's a build system!!

Like Make (if you're familiar with that) or Ant (if you know that!)

The initial problem being solved ...

You have a whole lot of different development files (source files, libraries, manual text, configuration, etc) and a whole load of steps you use to convert those files - perhaps via intermediates - to executable programs, webpages, etc.. An you have a whole load of tests you may want to run too. A batch file doesn't 'cut' it as shell really doesn't have the facilities, and there's a lot of checking that needs to be done to see whether steps of a very long build actually need to be done every time when you've only changed one element of the source.

Add to the above the need to build for different operating systems, for different environments, to packages the results for distribution and add a signature to them, and to work with source code control systems ... and there's you problem.

Features (text from the Scons site - they know what they want to sell!!)

• Configuration files are Python scripts--use the power of a real programming language to solve build problems.
• Reliable, automatic dependency analysis built-in for C, C++ and Fortran--no more "make depend" or "make clean" to get all of the dependencies. Dependency analysis is easily extensible through user-defined dependency Scanners for other languages or file types.
• Built-in support for C, C++, D, Java, Fortran, Yacc, Lex, Qt and SWIG, and building TeX and LaTeX documents. Easily extensible through user-defined Builders for other languages or file types.
• Building from central repositories of source code and/or pre-built targets.
• Built-in support for fetching source files from SCCS, RCS, CVS, BitKeeper and Perforce.
• Built-in support for Microsoft Visual Studio .NET and past Visual Studio versions, including generation of .dsp, .dsw, .sln and .vcproj files.
• Reliable detection of build changes using MD5 signatures; optional, configurable support for traditional timestamps.
• Improved support for parallel builds--like make -j but keeps N jobs running simultaneously regardless of directory hierarchy.
• Integrated Autoconf-like support for finding #include files, libraries, functions and typedefs.
• Global view of all dependencies--no more multiple build passes or reordering targets to build everything.
• Ability to share built files in a cache to speed up multiple builds--like ccache but for any type of target file, not just C/C++ compilation.
• Designed from the ground up for cross-platform builds, and known to work on Linux, other POSIX systems (including AIX, *BSD systems, HP/UX, IRIX and Solaris), Windows NT, Mac OS X, and OS/2

Installation (lets do it easy!)

Download, unpack the .tar.gz and
  sudo python setup.py install

The latest version (2.5.0) requires Python 2.7. "As we move towards Python 3" they say! - October 2016

http://scons.org/documentation.html

Hello Scons world

Building a "do nothing really" SConstruct file:

  WomanWithCat:q3 grahamellis$ vi SConstruct
  WomanWithCat:q3 grahamellis$ cat SConstruct
  # This is my Scons build file demo
  
  print "hello world"
  WomanWithCat:q3 grahamellis$ scons
  scons: Reading SConscript files ...
  hello world
  scons: done reading SConscript files.
  scons: Building targets ...
  scons: `.' is up to date.
  scons: done building targets.
  WomanWithCat:q3 grahamellis$ ls
  SConstruct
  WomanWithCat:q3 grahamellis$


Building a "make a program called hello from hello.c" construct file

  WomanWithCat:q3 grahamellis$ cat hello.c
  
  #include <stdio.h>
  
  int main() {
    printf("Hello World!\n");
  }
  
  WomanWithCat:q3 grahamellis$ cat scons_1
  
  Program('hello.c')
  
  WomanWithCat:q3 grahamellis$ scons -f scons_1
  scons: Reading SConscript files ...
  scons: done reading SConscript files.
  scons: Building targets ...
  gcc -o hello.o -c hello.c
  scons: done building targets.
  
  WomanWithCat:q3 grahamellis$ scons -f scons_1
  scons: Reading SConscript files ...
  scons: done reading SConscript files.
  scons: Building targets ...
  scons: `.' is up to date.
  scons: done building targets.
  
  WomanWithCat:q3 grahamellis$ ls
  SConstruct hello hello.c hello.o scons_1
  
  WomanWithCat:q3 grahamellis$ ./hello
  Hello World!


Building a program with multiple sources

  WomanWithCat:q3 grahamellis$ cat scons_2
  
  result = "mealorder"
  Program(result,['today.c','meals.c','snacks.c'])
  
  WomanWithCat:q3 grahamellis$
  
  
  WomanWithCat:q3 grahamellis$ cat today.c
  #include <stdio.h>
  
  #include "yum.h"
  
  int main() {
    printf("Rise and shine\n");
    breakfast();
    morning_coffee();
    lunch();
    afternoon_tea();
    dinner();
    supper();
    printf("Time for a kip\n");
    }
  WomanWithCat:q3 grahamellis$ cat yum.h
  void breakfast();
  void morning_coffee();
  void lunch();
  void afternoon_tea();
  void dinner();
  void supper();
  WomanWithCat:q3 grahamellis$ cat meals.c
  #include <stdio.h>
  
  void breakfast(){
    printf("breakfast\n");
    }
  
  void lunch(){
    printf("lunch\n");
    }
  
  void dinner(){
    printf("dinner\n");
    }
  WomanWithCat:q3 grahamellis$ cat snacks.c
  #include <stdio.h>
  
  void morning_coffee(){
    printf("morning coffee\n");
    }
  
  void supper(){
    printf("nightcap\n");
    }
  
  void afternoon_tea(){
    printf("afternoon tea\n");
    }
  WomanWithCat:q3 grahamellis$
  
  WomanWithCat:q3 grahamellis$ scons -f scons_2
  scons: Reading SConscript files ...
  scons: done reading SConscript files.
  scons: Building targets ...
  gcc -o today.o -c today.c
  gcc -o meals.o -c meals.c
  gcc -o snacks.o -c snacks.c
  gcc -o mealorder today.o meals.o snacks.o
  scons: done building targets.
  WomanWithCat:q3 grahamellis$ scons -f scons_2
  scons: Reading SConscript files ...
  scons: done reading SConscript files.
  scons: Building targets ...
  scons: `.' is up to date.
  scons: done building targets.
  WomanWithCat:q3 grahamellis$ vi today.c
  WomanWithCat:q3 grahamellis$ scons -f scons_2
  scons: Reading SConscript files ...
  scons: done reading SConscript files.
  scons: Building targets ...
  gcc -o today.o -c today.c
  gcc -o mealorder today.o meals.o snacks.o
  scons: done building targets.
  WomanWithCat:q3 grahamellis$ ./mealorder
  Rise and shine
  breakfast
  morning coffee
  morning coffee
  lunch
  afternoon tea
  afternoon tea
  dinner
  nightcap
  Time for a kip
  WomanWithCat:q3 grahamellis$

And here ends what we cover on this day ;-)