29
scons Writing Solid Code 02.12.2008

scons

Embed Size (px)

DESCRIPTION

scons. Writing 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?. - PowerPoint PPT Presentation

Citation preview

Page 1: scons

sconsWriting Solid Code

02.12.2008

Page 2: scons

Overview

•What is scons?

•scons Basics

•Other cools scons stuff

•Resources

Page 3: scons

What is scons?

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

•scons is written using the Python programming language.

Page 4: scons

Why use scons over make?

Page 5: scons

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.

Page 6: scons

scons Basics

Page 7: scons

scons Basics

#include <stdio.h>

int main(){

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

}

Page 8: scons

scons Basics

hello: hello.cgcc -o hello hello.c

clean: rm hello

Makefile:

Page 9: scons

scons Basics

SConstruct:

Program(‘hello.c’)

That’s it!

Page 10: scons

scons Basics

•cleaning

-scons -c

•creating object files

-Object(‘hello.c’)

•specifying executable names

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

Page 11: scons

scons Basics

•Compiling multiple files into one executable

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

-The filenames are given in a typical Python list

Page 12: scons

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)

Page 13: scons

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:

Page 14: scons

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.

Page 15: scons

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

Page 16: scons

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.

Page 17: scons

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’)

Page 18: scons

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.

Page 19: scons

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’)

Page 20: scons

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.

Page 21: scons

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.

Page 22: scons

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’]

Page 23: scons

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

Page 24: scons

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’]

Page 25: scons

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.

Page 26: scons

Other cool scons stuff

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

Page 27: scons

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.

Page 28: scons

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.

Page 29: scons

Resources

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