Download presentation
Presentation is loading. Please wait.
Published byAnthony Powers Modified over 9 years ago
1
scons Writing Solid Code 02.12.2008
2
Overview What is scons? scons Basics Other cools scons stuff Resources
3
What is scons? scons is a program for building software, similar to make. scons is written using the Python programming language.
4
Why use scons over make?
5
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.
6
scons Basics
7
#include int main() { printf(“Hello, World!\n”); return 0; }
8
scons Basics hello: hello.c gcc -o hello hello.c clean: rm hello Makefile:
9
scons Basics SConstruct: Program(‘hello.c’) That’s it!
10
scons Basics cleaning scons -c creating object files Object(‘hello.c’) specifying executable names Program(‘hello_new’,’hello.c’)
11
scons Basics Compiling multiple files into one executable Program(‘hello’, [‘hello1.c’, ‘hello2.c’]) The filenames are given in a typical Python list
12
scons Basics Since 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)
13
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:
14
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.
15
Other cool scons stuff Including 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
16
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.
17
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’)
18
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.
19
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’)
20
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.
21
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.
22
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’]
23
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.c cc -o hello_2.o -c -Wall -pedantic hello.c being executed
24
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’]
25
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.
26
Other cool scons stuff test = ARGUMENTS.get(‘test’, 0) if int(test): print “This is a test.”
27
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.
28
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.
29
Resources scons User Guide, v0.97 http://www.scons.org/doc/HTML/scons- user.html http://www.scons.org/doc/HTML/scons- user.html
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.