scons writing solid code 02.12.2008. overview what is scons? scons basics other cools scons stuff...

29
scons Writing Solid Code 02.12.2008

Upload: anthony-powers

Post on 01-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

sconsWriting Solid Code

02.12.2008

Overview

•What is scons?

•scons Basics

•Other cools scons stuff

•Resources

What is scons?

•scons is a program for building software, similar to make.

•scons is written using the Python programming language.

Why use scons over make?

What is scons?• scons syntax is Python syntax; thus it

inherits the clarity and concise nature of the Python language.

• scons scripts are Python scripts; anything that can be done in a Python script can be done in an scons script, providing a powerful and flexible tool for building software.

• Building of executables is handled by scons, not the script itself; thus scons scripts are much more platform independent.

scons Basics

scons Basics

#include <stdio.h>

int main(){

printf(“Hello, World!\n”);return 0;

}

scons Basics

hello: hello.cgcc -o hello hello.c

clean: rm hello

Makefile:

scons Basics

SConstruct:

Program(‘hello.c’)

That’s it!

scons Basics

•cleaning

-scons -c

•creating object files

-Object(‘hello.c’)

•specifying executable names

-Program(‘hello_new’,’hello.c’)

scons Basics

•Compiling multiple files into one executable

-Program(‘hello’, [‘hello1.c’, ‘hello2.c’])

-The filenames are given in a typical Python list

scons BasicsSince multiple files are given as a list, there are other ways to make them easier to read:

sources = Split(‘hello1.c hello2.c’)Program(‘hello’, sources)

or:sources = Split(“””hello1.c hello2.c”””)Program(‘hello’, sources)

Other cool scons stuff

Arguments can also be specified by name, as a feature of the Python language. This reduces confusion as to what each argument is:sources = Split(‘hello1.c hello2.c’)Program(target = ‘hello’, source = sources)

sources = Split(‘hello1.c hello2.c’)Program(source = sources, target = ‘hello’)

Is equivalent to the following:

Other cool scons stuff

•Libraries can be created using the Library function call, similar to Program.

•Shared Libraries (DLLs) can be created using the SharedLibrary function call. scons will automatically add the -shared flag on UNIX systems.

Other cool scons stuffIncluding a library is as simple as specifying

them as arguments in the Program function call:Program(‘hello.c’, LIBS=[‘m’,’pthread’], LIBPATH=’.’)

Note that library prefixes or suffixes do not have to be specified, as scons will automatically use the correct extension based on the system the software is being built. This would result in something like this being executed:cc -o hello -L. -lm -lpthread

Other cool scons stuff

Both LIBS and LIBPATH can either be a Python list, or a single filename or path string, like the other arguments previously discussed.

Other cool scons stuff

By default, scons does not rebuild a target unless the content of a source file has actually been changed. To explicitly specify this behavior, use the SourceSignatures function call:

SourceSignatures(‘MD5’)SourceSignatures(‘timestamp’)

Other cool scons stuff

The same thing may be applied to targets as well, using the TargetSignatures function call. By default, scons uses the ‘build’ value. Another value that may be passed to this function is ‘content’, which allows scons to determine if the target doesn’t need to be rebuilt even if the source content was changed.

Other cool scons stuff

Another useful function is Ignore, which will cause scons to ignore the changes of a source or dependency when determining whether a target should be rebuilt:

Program(‘hello.c’)Ignore(‘/usr/include/stdio.h’)

Other cool scons stuff

If a target needs to always be built for whatever reason, the AlwaysBuild function can be used:

hello = Program(‘hello.c’)AlwaysBuild(hello)

Note that this is executed correctly even though AlwaysBuild comes after the Program call.

Other cool scons stuff

scons will not necessarily execute build commands in the specified order, and it executes other commands in the script before actually building the targets. This allows for functionality like that of AlwaysBuild, which takes as an argument the return value of Program.

Other cool scons stuff

scons also allows for multiple construction environments, using the Environment function:

env1 = Environment(CC = ‘gcc’, CCFLAGS = ‘-O2’)env2 = Environment(CC = ‘cc’, CCFLAGS = [‘-Wall’, ‘-pedantic’]

Other cool scons stuff

env1.Object(‘hello_1.o’, ‘hello.c’)env2.Object(‘hello_2.o’, ‘hello.c’)results in :gcc -o hello_1.o -c -O2 hello.ccc -o hello_2.o -c -Wall -pedantic hello.c

being executed

Other cool scons stuff

Environments can be copied, if one wants multiple environments that are slightly similar, using Environment.Clone:

env1 = Environment(CC = ‘gcc’)env2 = Environment.Clone(CCFLAGS = [‘-Wall’, ‘-pedantic’])env3 = Environment.Clone(CCFLAGS = [‘-Wall’, ‘-O2’]

Other cool scons stuff

You can check command line targets with the following:

if ‘test’ in COMMAND_LINE_TARGETS: print “You want test!”

Variables may also be set like so:

#scons -Q test=1

This value can be checked through the ARGUMENTS dictionary.

Other cool scons stuff

test = ARGUMENTS.get(‘test’, 0)if int(test): print “This is a test.”

Other cool scons stuff

Default targets can be set by using the Default function:

hello1 = Program(‘hello.c’)Default(hello1)hello2 = Program(‘hello2’, ‘hello.c’)

This will only build hello1 unless hello2 is given as a command line target. Calling Default multiple times, or calling it like so:

Default(prog1, prog2)

will append more targets to the default. This list can be accessed through the DEFAULT_TARGETS list.

Other cool scons stuff

If, for some reason there is a need to have nothing made by default, simply pass the variable None into the Default function.

Resources

•scons User Guide, v0.97http://www.scons.org/doc/HTML/scons-user.html