5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 &SCons; provides a number of ways that
29 allow the writer of the &SConscript; files
30 to give users a great deal of control over how to run the builds.
35 <title>Not Having to Specify Command-Line Options Each Time: the &SCONSFLAGS; Environment Variable</title>
39 Users may find themselves supplying
40 the same command-line options every time
42 For example, a user might find that it saves time
43 to specify a value of <literal>-j 2</literal>
44 to run the builds in parallel.
45 To avoid having to type <literal>-j 2</literal> by hand
47 you can set the external environment variable
48 &SCONSFLAGS; to a string containing
49 command-line options that you want &SCons; to use.
56 and you're using a POSIX shell that's
57 compatible with the Bourne shell,
58 and you always want &SCons; to use the
59 <literal>-Q</literal> option,
60 you can set the &SCONSFLAGS;
61 environment as follows:
65 <scons_example name="SCONSFLAGS">
66 <file name="SConstruct">
67 def b(target, source, env):
69 def s(target, source, env):
70 return " ... [build output] ..."
71 a = Action(b, strfunction = s)
72 env = Environment(BUILDERS = {'A' : Builder(action=a)})
73 env.A('foo.out', 'foo.in')
80 <scons_output example="SCONSFLAGS">
81 <scons_output_command>scons</scons_output_command>
82 <scons_output_command>export SCONSFLAGS="-Q"</scons_output_command>
83 <scons_output_command environment="SCONSFLAGS=-Q">scons</scons_output_command>
88 Users of &csh;-style shells on POSIX systems
89 can set the &SCONSFLAGS; environment as follows:
94 $ <userinput>setenv SCONSFLAGS "-Q"</userinput>
99 Windows users may typically want to set this
100 &SCONSFLAGS; in the appropriate tab of the
101 <literal>System Properties</literal> window.
108 <title>Getting at Command-Line Targets</title>
112 &SCons; supports a &COMMAND_LINE_TARGETS; variable
113 that lets you get at the list of targets that the
114 user specified on the command line.
115 You can use the targets to manipulate the
116 build in any way you wish.
118 suppose that you want to print a reminder
119 to the user whenever a specific program is built.
120 You can do this by checking for the
121 target in the &COMMAND_LINE_TARGETS; list:
125 <scons_example name="COMMAND_LINE_TARGETS">
126 <file name="SConstruct" printme="1">
127 if 'bar' in COMMAND_LINE_TARGETS:
128 print "Don't forget to copy `bar' to the archive!"
129 Default(Program('foo.c'))
142 Then, running &SCons; with the default target
143 works as it always does,
144 but explicity specifying the &bar; target
145 on the command line generates the warning message:
149 <scons_output example="COMMAND_LINE_TARGETS">
150 <scons_output_command>scons -Q</scons_output_command>
151 <scons_output_command>scons -Q bar</scons_output_command>
156 Another practical use for the &COMMAND_LINE_TARGETS; variable
157 might be to speed up a build
158 by only reading certain subsidiary &SConscript;
159 files if a specific target is requested.
166 <title>Controlling the Default Targets</title>
170 One of the most basic things you can control
171 is which targets &SCons; will build by default--that is,
172 when there are no targets specified on the command line.
173 As mentioned previously,
174 &SCons; will normally build every target
175 in or below the current directory
176 by default--that is, when you don't
177 explicitly specify one or more targets
179 Sometimes, however, you may want
180 to specify explicitly that only
181 certain programs, or programs in certain directories,
182 should be built by default.
183 You do this with the &Default; function:
187 <scons_example name="Default1">
188 <file name="SConstruct" printme="1">
190 hello = env.Program('hello.c')
191 env.Program('goodbye.c')
194 <file name="hello.c">
197 <file name="goodbye.c">
204 This &SConstruct; file knows how to build two programs,
205 &hello; and &goodbye;,
207 &hello; program by default:
211 <scons_output example="Default1">
212 <scons_output_command>scons -Q</scons_output_command>
213 <scons_output_command>scons -Q</scons_output_command>
214 <scons_output_command>scons -Q goodbye</scons_output_command>
219 Note that, even when you use the &Default;
220 function in your &SConstruct; file,
221 you can still explicitly specify the current directory
222 (<literal>.</literal>) on the command line
223 to tell &SCons; to build
224 everything in (or below) the current directory:
228 <scons_output example="Default1">
229 <scons_output_command>scons -Q .</scons_output_command>
234 You can also call the &Default;
235 function more than once,
236 in which case each call
237 adds to the list of targets to be built by default:
241 <scons_example name="Default2">
242 <file name="SConstruct" printme="1">
244 prog1 = env.Program('prog1.c')
246 prog2 = env.Program('prog2.c')
247 prog3 = env.Program('prog3.c')
250 <file name="prog1.c">
253 <file name="prog2.c">
256 <file name="prog3.c">
263 Or you can specify more than one target
264 in a single call to the &Default; function:
270 prog1 = env.Program('prog1.c')
271 prog2 = env.Program('prog2.c')
272 prog3 = env.Program('prog3.c')
273 Default(prog1, prog3)
278 Either of these last two examples
280 <application>prog1</application>
282 <application>prog3</application>
287 <scons_output example="Default2">
288 <scons_output_command>scons -Q</scons_output_command>
289 <scons_output_command>scons -Q .</scons_output_command>
294 You can list a directory as
295 an argument to &Default;:
299 <scons_example name="Default3">
300 <file name="SConstruct" printme="1">
302 env.Program(['prog1/main.c', 'prog1/foo.c'])
303 env.Program(['prog2/main.c', 'prog2/bar.c'])
306 <directory name="prog1"></directory>
307 <directory name="prog2"></directory>
308 <file name="prog1/main.c">
309 int main() { printf("prog1/main.c\n"); }
311 <file name="prog1/foo.c">
312 int foo() { printf("prog1/foo.c\n"); }
314 <file name="prog2/main.c">
315 int main() { printf("prog2/main.c\n"); }
317 <file name="prog2/bar.c">
318 int bar() { printf("prog2/bar.c\n"); }
324 In which case only the target(s) in that
325 directory will be built by default:
329 <scons_output example="Default3">
330 <scons_output_command>scons -Q</scons_output_command>
331 <scons_output_command>scons -Q</scons_output_command>
332 <scons_output_command>scons -Q .</scons_output_command>
337 Lastly, if for some reason you don't want
338 any targets built by default,
339 you can use the Python <literal>None</literal>
344 <scons_example name="Default4">
345 <file name="SConstruct" printme="1">
347 prog1 = env.Program('prog1.c')
348 prog2 = env.Program('prog2.c')
351 <file name="prog1.c">
354 <file name="prog2.c">
361 Which would produce build output like:
365 <scons_output example="Default4">
366 <scons_output_command>scons -Q</scons_output_command>
367 <scons_output_command>scons -Q .</scons_output_command>
371 <title>Getting at the List of Default Targets</title>
375 &SCons; supports a &DEFAULT_TARGETS; variable
376 that lets you get at the current list of default targets.
377 The &DEFAULT_TARGETS variable has
378 two important differences from the &COMMAND_LINE_TARGETS; variable.
379 First, the &DEFAULT_TARGETS; variable is a list of
380 internal &SCons; nodes,
381 so you need to convert the list elements to strings
382 if you want to print them or look for a specific target name.
383 Fortunately, you can do this easily
384 by using the Python <function>map</function> function
385 to run the list through <function>str</function>:
389 <scons_example name="DEFAULT_TARGETS_1">
390 <file name="SConstruct" printme="1">
391 prog1 = Program('prog1.c')
393 print "DEFAULT_TARGETS is", map(str, DEFAULT_TARGETS)
395 <file name="prog1.c">
402 (Keep in mind that all of the manipulation of the
403 &DEFAULT_TARGETS; list takes place during the
404 first phase when &SCons; is reading up the &SConscript; files,
406 we leave off the <literal>-Q</literal> flag when we run &SCons;:)
410 <scons_output example="DEFAULT_TARGETS_1">
411 <scons_output_command>scons</scons_output_command>
417 the contents of the &DEFAULT_TARGETS; list change
418 in response to calls to the &Default: function,
419 as you can see from the following &SConstruct; file:
423 <scons_example name="DEFAULT_TARGETS_2">
424 <file name="SConstruct" printme="1">
425 prog1 = Program('prog1.c')
427 print "DEFAULT_TARGETS is now", map(str, DEFAULT_TARGETS)
428 prog2 = Program('prog2.c')
430 print "DEFAULT_TARGETS is now", map(str, DEFAULT_TARGETS)
432 <file name="prog1.c">
435 <file name="prog2.c">
442 Which yields the output:
446 <scons_output example="DEFAULT_TARGETS_2">
447 <scons_output_command>scons</scons_output_command>
452 In practice, this simply means that you
453 need to pay attention to the order in
454 which you call the &Default; function
455 and refer to the &DEFAULT_TARGETS; list,
456 to make sure that you don't examine the
457 list before you've added the default targets
458 you expect to find in it.
467 <title>Getting at the List of Build Targets, Regardless of Origin</title>
471 We've already been introduced to the
472 &COMMAND_LINE_TARGETS; variable,
473 which contains a list of targets specified on the command line,
474 and the &DEFAULT_TARGETS; variable,
475 which contains a list of targets specified
476 via calls to the &Default; method or function.
478 you want a list of whatever targets
479 &SCons; will try to build,
480 regardless of whether the targets came from the
481 command line or a &Default; call.
482 You could code this up by hand, as follows:
487 if COMMAND_LINE_TARGETS:
488 targets = COMMAND_LINE_TARGETS
490 targets = DEFAULT_TARGETS
495 &SCons;, however, provides a convenient
496 &BUILD_TARGETS; variable
497 that eliminates the need for this by-hand manipulation.
498 Essentially, the &BUILD_TARGETS; variable
499 contains a list of the command-line targets,
500 if any were specified,
501 and if no command-line targets were specified,
502 it contains a list of the targets specified
503 via the &Default; method or function.
509 Because &BUILD_TARGETS; may contain a list of &SCons; nodes,
510 you must convert the list elements to strings
511 if you want to print them or look for a specific target name,
512 just like the &DEFAULT_TARGETS; list:
516 <scons_example name="BUILD_TARGETS_1">
517 <file name="SConstruct" printme="1">
518 prog1 = Program('prog1.c')
521 print "BUILD_TARGETS is", map(str, BUILD_TARGETS)
523 <file name="prog1.c">
526 <file name="prog2.c">
533 Notice how the value of &BUILD_TARGETS;
534 changes depending on whether a target is
535 specified on the command line:
539 <scons_output example="BUILD_TARGETS_1">
540 <scons_output_command>scons -Q</scons_output_command>
541 <scons_output_command>scons -Q prog2</scons_output_command>
542 <scons_output_command>scons -Q -c .</scons_output_command>
548 <title>Command-Line <varname>variable</varname>=<varname>value</varname> Build Options</title>
552 You may want to control various aspects
553 of your build by allowing the user
554 to specify <varname>variable</varname>=<varname>value</varname>
555 values on the command line.
556 For example, suppose you
557 want users to be able to
558 build a debug version of a program
559 by running &SCons; as follows:
564 % <userinput>scons -Q debug=1</userinput>
569 &SCons; provides an &ARGUMENTS; dictionary
570 that stores all of the
571 <varname>variable</varname>=<varname>value</varname>
572 assignments from the command line.
573 This allows you to modify
574 aspects of your build in response
575 to specifications on the command line.
576 (Note that unless you want to require
577 that users <emphasis>always</emphasis>
579 you probably want to use
581 <literal>ARGUMENTS.get()</literal> function,
582 which allows you to specify a default value
583 to be used if there is no specification
584 on the command line.)
590 The following code sets the &cv-link-CCFLAGS; construction
591 variable in response to the <varname>debug</varname>
592 flag being set in the &ARGUMENTS; dictionary:
596 <scons_example name="ARGUMENTS">
597 <file name="SConstruct" printme="1">
599 debug = ARGUMENTS.get('debug', 0)
601 env.Append(CCFLAGS = '-g')
602 env.Program('prog.c')
611 This results in the <varname>-g</varname>
612 compiler option being used when
613 <literal>debug=1</literal>
614 is used on the command line:
618 <scons_output example="ARGUMENTS">
619 <scons_output_command>scons -Q debug=0</scons_output_command>
620 <scons_output_command>scons -Q debug=0</scons_output_command>
621 <scons_output_command>scons -Q debug=1</scons_output_command>
622 <scons_output_command>scons -Q debug=1</scons_output_command>
627 Notice that &SCons; keeps track of
628 the last values used to build the object files,
629 and as a result correctly rebuilds
630 the object and executable files
631 only when the value of the <literal>debug</literal>
632 argument has changed.
639 <title>Controlling Command-Line Build Options</title>
643 Being able to use a command-line build option like
644 <literal>debug=1</literal> is handy,
645 but it can be a chore to write specific Python code
646 to recognize each such option
647 and apply the values to a construction variable.
649 &SCons; supports a class to
650 define such build options easily,
651 and a mechanism to apply the
652 build options to a construction environment.
653 This allows you to control how the build options affect
654 construction environments.
660 For example, suppose that you want users to set
661 a &RELEASE; construction variable on the
662 command line whenever the time comes to build
663 a program for release,
664 and that the value of this variable
665 should be added to the command line
666 with the appropriate <literal>-D</literal> option
667 (or other command line option)
668 to pass the value to the C compiler.
669 Here's how you might do that by setting
670 the appropriate value in a dictionary for the
671 &cv-link-CPPDEFINES; construction variable:
675 <scons_example name="Options1">
676 <file name="SConstruct" printme="1">
678 opts.Add('RELEASE', 'Set to 1 to build for release', 0)
679 env = Environment(options = opts,
680 CPPDEFINES={'RELEASE_BUILD' : '${RELEASE}'})
681 env.Program(['foo.c', 'bar.c'])
693 This &SConstruct; file first creates an
695 (the <literal>opts = Options()</literal> call),
696 and then uses the object's &Add;
697 method to indicate that the &RELEASE;
698 option can be set on the command line,
699 and that it's default value will be <literal>0</literal>
700 (the third argument to the &Add; method).
701 The second argument is a line of help text;
702 we'll learn how to use it in the next section.
708 We then pass the created &Options;
709 object as an &options; keyword argument
710 to the &Environment; call
711 used to create the construction environment.
712 This then allows a user to set the
713 &RELEASE; build option on the command line
714 and have the variable show up in
715 the command line used to build each object from
720 <scons_output example="Options1">
721 <scons_output_command>scons -Q RELEASE=1</scons_output_command>
727 <title>Providing Help for Command-Line Build Options</title>
731 To make command-line build options most useful,
732 you ideally want to provide
733 some help text that will describe
734 the available options
735 when the user runs <literal>scons -h</literal>.
736 You could write this text by hand,
737 but &SCons; provides an easier way.
738 &Options; objects support a
739 &GenerateHelpText; method
740 that will, as its name indicates,
741 generate text that describes
742 the various options that
743 have been added to it.
744 You then pass the output from this method to
749 <scons_example name="Options_Help">
750 <file name="SConstruct" printme="1">
751 opts = Options('custom.py')
752 opts.Add('RELEASE', 'Set to 1 to build for release', 0)
753 env = Environment(options = opts)
754 Help(opts.GenerateHelpText(env))
760 &SCons; will now display some useful text
761 when the <literal>-h</literal> option is used:
765 <scons_output example="Options_Help">
766 <scons_output_command>scons -Q -h</scons_output_command>
771 Notice that the help output shows the default value,
772 and the current actual value of the build option.
779 <title>Reading Build Options From a File</title>
783 Being able to use a command-line build option like
784 <literal>debug=1</literal> is handy,
785 but it can be a chore to write specific Python code
786 to recognize each such option
787 and apply the values to a construction variable.
789 &SCons; supports a class to
790 define such build options easily
791 and to read build option values from a file.
792 This allows you to control how the build options affect
793 construction environments.
794 The way you do this is by specifying
795 a file name when you call &Options;,
796 like &custom_py; in the following example:
800 <scons_example name="Options_custom_py_1">
801 <file name="SConstruct" printme="1">
802 opts = Options('custom.py')
803 opts.Add('RELEASE', 'Set to 1 to build for release', 0)
804 env = Environment(options = opts,
805 CPPDEFINES={'RELEASE_BUILD' : '${RELEASE}'})
806 env.Program(['foo.c', 'bar.c'])
807 Help(opts.GenerateHelpText(env))
815 <file name="custom.py">
822 This then allows us to control the &RELEASE;
823 variable by setting it in the &custom_py; file:
827 <scons_example_file example="Options_custom_py_1" name="custom.py"></scons_example_file>
831 Note that this file is actually executed
832 like a Python script.
833 Now when we run &SCons;:
837 <scons_output example="Options_custom_py_1">
838 <scons_output_command>scons -Q</scons_output_command>
843 And if we change the contents of &custom_py; to:
847 <scons_example name="Options_custom_py_2">
848 <file name="SConstruct">
849 opts = Options('custom.py')
850 opts.Add('RELEASE', 'Set to 1 to build for release', 0)
851 env = Environment(options = opts,
852 CPPDEFINES={'RELEASE_BUILD' : '${RELEASE}'})
853 env.Program(['foo.c', 'bar.c'])
854 Help(opts.GenerateHelpText(env))
862 <file name="custom.py" printme="1">
869 The object files are rebuilt appropriately
874 <scons_output example="Options_custom_py_2">
875 <scons_output_command>scons -Q</scons_output_command>
881 <title>Canned Build Options</title>
885 &SCons; provides a number of functions
886 that provide ready-made behaviors
887 for various types of command-line build options.
892 <title>True/False Values: the &BoolOption; Build Option</title>
896 It's often handy to be able to specify an
897 option that controls a simple Boolean variable
898 with a &true; or &false; value.
899 It would be even more handy to accomodate
900 users who have different preferences for how to represent
901 &true; or &false; values.
902 The &BoolOption; function
903 makes it easy to accomodate a variety of
904 common values that represent
911 The &BoolOption; function takes three arguments:
912 the name of the build option,
913 the default value of the build option,
914 and the help string for the option.
915 It then returns appropriate information for
916 passing to the &Add; method of an &Options; object, like so:
920 <scons_example name="BoolOption">
921 <file name="SConstruct" printme="1">
922 opts = Options('custom.py')
923 opts.Add(BoolOption('RELEASE', 'Set to build for release', 0))
924 env = Environment(options = opts,
925 CPPDEFINES={'RELEASE_BUILD' : '${RELEASE}'})
935 With this build option,
936 the &RELEASE; variable can now be enabled by
937 setting it to the value <literal>yes</literal>
938 or <literal>t</literal>:
942 <scons_output example="BoolOption">
943 <scons_output_command>scons -Q RELEASE=yes foo.o</scons_output_command>
946 <scons_output example="BoolOption">
947 <scons_output_command>scons -Q RELEASE=t foo.o</scons_output_command>
952 Other values that equate to &true; include
953 <literal>y</literal>,
954 <literal>1</literal>,
955 <literal>on</literal>
957 <literal>all</literal>.
963 Conversely, &RELEASE; may now be given a &false;
964 value by setting it to
965 <literal>no</literal>
967 <literal>f</literal>:
971 <scons_output example="BoolOption">
972 <scons_output_command>scons -Q RELEASE=no foo.o</scons_output_command>
975 <scons_output example="BoolOption">
976 <scons_output_command>scons -Q RELEASE=f foo.o</scons_output_command>
981 Other values that equate to &true; include
982 <literal>n</literal>,
983 <literal>0</literal>,
984 <literal>off</literal>
986 <literal>none</literal>.
992 Lastly, if a user tries to specify
994 &SCons; supplies an appropriate error message:
998 <scons_output example="BoolOption">
999 <scons_output_command>scons -Q RELEASE=bad_value foo.o</scons_output_command>
1005 <title>Single Value From a List: the &EnumOption; Build Option</title>
1009 Suppose that we want a user to be able to
1010 set a &COLOR; option
1011 that selects a background color to be
1012 displayed by an application,
1013 but that we want to restrict the
1014 choices to a specific set of allowed colors.
1015 This can be set up quite easily
1016 using the &EnumOption;,
1017 which takes a list of &allowed_values
1018 in addition to the variable name,
1020 and help text arguments:
1024 <scons_example name="EnumOption">
1025 <file name="SConstruct" printme="1">
1026 opts = Options('custom.py')
1027 opts.Add(EnumOption('COLOR', 'Set background color', 'red',
1028 allowed_values=('red', 'green', 'blue')))
1029 env = Environment(options = opts,
1030 CPPDEFINES={'COLOR' : '"${COLOR}"'})
1031 env.Program('foo.c')
1040 The user can now explicity set the &COLOR; build option
1041 to any of the specified allowed values:
1045 <scons_output example="EnumOption">
1046 <scons_output_command>scons -Q COLOR=red foo.o</scons_output_command>
1047 <scons_output_command>scons -Q COLOR=blue foo.o</scons_output_command>
1048 <scons_output_command>scons -Q COLOR=green foo.o</scons_output_command>
1053 But, almost more importantly,
1054 an attempt to set &COLOR;
1055 to a value that's not in the list
1056 generates an error message:
1060 <scons_output example="EnumOption">
1061 <scons_output_command>scons -Q COLOR=magenta foo.o</scons_output_command>
1066 The &EnumOption; function also supports a way
1067 to map alternate names to allowed values.
1068 Suppose, for example,
1069 that we want to allow the user
1070 to use the word <literal>navy</literal> as a synonym for
1071 <literal>blue</literal>.
1072 We do this by adding a ↦ dictionary
1073 that will map its key values
1074 to the desired legal value:
1078 <scons_example name="EnumOption_map">
1079 <file name="SConstruct" printme="1">
1080 opts = Options('custom.py')
1081 opts.Add(EnumOption('COLOR', 'Set background color', 'red',
1082 allowed_values=('red', 'green', 'blue'),
1083 map={'navy':'blue'}))
1084 env = Environment(options = opts,
1085 CPPDEFINES={'COLOR' : '"${COLOR}"'})
1086 env.Program('foo.c')
1095 As desired, the user can then use
1096 <literal>navy</literal> on the command line,
1097 and &SCons; will translate it into <literal>blue</literal>
1098 when it comes time to use the &COLOR;
1099 option to build a target:
1103 <scons_output example="EnumOption_map">
1104 <scons_output_command>scons -Q COLOR=navy foo.o</scons_output_command>
1109 By default, when using the &EnumOption; function,
1110 arguments that differ
1111 from the legal values
1113 are treated as illegal values:
1117 <scons_output example="EnumOption">
1118 <scons_output_command>scons -Q COLOR=Red foo.o</scons_output_command>
1119 <scons_output_command>scons -Q COLOR=BLUE foo.o</scons_output_command>
1120 <scons_output_command>scons -Q COLOR=nAvY foo.o</scons_output_command>
1125 The &EnumOption; function can take an additional
1126 &ignorecase; keyword argument that,
1127 when set to <literal>1</literal>,
1128 tells &SCons; to allow case differences
1129 when the values are specified:
1133 <scons_example name="EnumOption_ic1">
1134 <file name="SConstruct" printme="1">
1135 opts = Options('custom.py')
1136 opts.Add(EnumOption('COLOR', 'Set background color', 'red',
1137 allowed_values=('red', 'green', 'blue'),
1138 map={'navy':'blue'},
1140 env = Environment(options = opts,
1141 CPPDEFINES={'COLOR' : '"${COLOR}"'})
1142 env.Program('foo.c')
1151 Which yields the output:
1155 <scons_output example="EnumOption_ic1">
1156 <scons_output_command>scons -Q COLOR=Red foo.o</scons_output_command>
1157 <scons_output_command>scons -Q COLOR=BLUE foo.o</scons_output_command>
1158 <scons_output_command>scons -Q COLOR=nAvY foo.o</scons_output_command>
1159 <scons_output_command>scons -Q COLOR=green foo.o</scons_output_command>
1164 Notice that an &ignorecase; value of <literal>1</literal>
1165 preserves the case-spelling that the user supplied.
1166 If you want &SCons; to translate the names
1168 regardless of the case used by the user,
1169 specify an &ignorecase; value of <literal>2</literal>:
1173 <scons_example name="EnumOption_ic2">
1174 <file name="SConstruct" printme="1">
1175 opts = Options('custom.py')
1176 opts.Add(EnumOption('COLOR', 'Set background color', 'red',
1177 allowed_values=('red', 'green', 'blue'),
1178 map={'navy':'blue'},
1180 env = Environment(options = opts,
1181 CPPDEFINES={'COLOR' : '"${COLOR}"'})
1182 env.Program('foo.c')
1191 Now &SCons; will use values of
1192 <literal>red</literal>,
1193 <literal>green</literal> or
1194 <literal>blue</literal>
1195 regardless of how the user spells
1196 those values on the command line:
1200 <scons_output example="EnumOption_ic2">
1201 <scons_output_command>scons -Q COLOR=Red foo.o</scons_output_command>
1202 <scons_output_command>scons -Q COLOR=nAvY foo.o</scons_output_command>
1203 <scons_output_command>scons -Q COLOR=GREEN foo.o</scons_output_command>
1209 <title>Multiple Values From a List: the &ListOption; Build Option</title>
1213 Another way in which you might want to allow users
1214 to control build option is to
1215 specify a list of one or more legal values.
1216 &SCons; supports this through the &ListOption; function.
1217 If, for example, we want a user to be able to set a
1218 &COLORS; option to one or more of the legal list of values:
1222 <scons_example name="ListOption">
1223 <file name="SConstruct" printme="1">
1224 opts = Options('custom.py')
1225 opts.Add(ListOption('COLORS', 'List of colors', 0,
1226 ['red', 'green', 'blue']))
1227 env = Environment(options = opts,
1228 CPPDEFINES={'COLORS' : '"${COLORS}"'})
1229 env.Program('foo.c')
1238 A user can now specify a comma-separated list
1240 which will get translated into a space-separated
1241 list for passing to the any build commands:
1245 <scons_output example="ListOption">
1246 <scons_output_command>scons -Q COLORS=red,blue foo.o</scons_output_command>
1247 <scons_output_command>scons -Q COLORS=blue,green,red foo.o</scons_output_command>
1252 In addition, the &ListOption; function
1253 allows the user to specify explicit keywords of
1255 to select all of the legal values,
1256 or none of them, respectively:
1260 <scons_output example="ListOption">
1261 <scons_output_command>scons -Q COLORS=all foo.o</scons_output_command>
1262 <scons_output_command>scons -Q COLORS=none foo.o</scons_output_command>
1267 And, of course, an illegal value
1268 still generates an error message:
1272 <scons_output example="ListOption">
1273 <scons_output_command>scons -Q COLORS=magenta foo.o</scons_output_command>
1279 <title>Path Names: the &PathOption; Build Option</title>
1283 &SCons; supports a &PathOption; function
1284 to make it easy to create a build option
1285 to control an expected path name.
1286 If, for example, you need to
1287 define a variable in the preprocessor
1288 that control the location of a
1293 <scons_example name="PathOption">
1294 <file name="SConstruct" printme="1">
1295 opts = Options('custom.py')
1296 opts.Add(PathOption('CONFIG',
1297 'Path to configuration file',
1298 '__ROOT__/etc/my_config'))
1299 env = Environment(options = opts,
1300 CPPDEFINES={'CONFIG_FILE' : '"$CONFIG"'})
1301 env.Program('foo.c')
1306 <file name="__ROOT__/etc/my_config">
1309 <file name="__ROOT__/usr/local/etc/other_config">
1316 This then allows the user to
1317 override the &CONFIG; build option
1318 on the command line as necessary:
1322 <scons_output example="PathOption">
1323 <scons_output_command>scons -Q foo.o</scons_output_command>
1324 <scons_output_command>scons -Q CONFIG=__ROOT__/usr/local/etc/other_config foo.o</scons_output_command>
1329 By default, &PathOption; checks to make sure
1330 that the specified path exists and generates an error if it
1335 <scons_output example="PathOption">
1336 <scons_output_command>scons -Q CONFIG=__ROOT__/does/not/exist foo.o</scons_output_command>
1341 &PathOption; provides a number of methods
1342 that you can use to change this behavior.
1343 If you want to ensure that any specified paths are,
1344 in fact, files and not directories,
1345 use the &PathOption_PathIsFile; method:
1349 <scons_example name="PathIsFile">
1350 <file name="SConstruct" printme="1">
1351 opts = Options('custom.py')
1352 opts.Add(PathOption('CONFIG',
1353 'Path to configuration file',
1354 '__ROOT__/etc/my_config',
1355 PathOption.PathIsFile))
1356 env = Environment(options = opts,
1357 CPPDEFINES={'CONFIG_FILE' : '"$CONFIG"'})
1358 env.Program('foo.c')
1363 <file name="__ROOT__/etc/my_config">
1370 Conversely, to ensure that any specified paths are
1371 directories and not files,
1372 use the &PathOption_PathIsDir; method:
1376 <scons_example name="PathIsDir">
1377 <file name="SConstruct" printme="1">
1378 opts = Options('custom.py')
1379 opts.Add(PathOption('DBDIR',
1380 'Path to database directory',
1381 '__ROOT__/var/my_dbdir',
1382 PathOption.PathIsDir))
1383 env = Environment(options = opts,
1384 CPPDEFINES={'DBDIR' : '"$DBDIR"'})
1385 env.Program('foo.c')
1390 <file name="__ROOT__/var/my_dbdir">
1397 If you want to make sure that any specified paths
1399 and you would like the directory created
1400 if it doesn't already exist,
1401 use the &PathOption_PathIsDirCreate; method:
1405 <scons_example name="PathIsDirCreate">
1406 <file name="SConstruct" printme="1">
1407 opts = Options('custom.py')
1408 opts.Add(PathOption('DBDIR',
1409 'Path to database directory',
1410 '__ROOT__/var/my_dbdir',
1411 PathOption.PathIsDirCreate))
1412 env = Environment(options = opts,
1413 CPPDEFINES={'DBDIR' : '"$DBDIR"'})
1414 env.Program('foo.c')
1419 <file name="__ROOT__/var/my_dbdir">
1426 Lastly, if you don't care whether the path exists,
1427 is a file, or a directory,
1428 use the &PathOption_PathAccept; method
1429 to accept any path that the user supplies:
1433 <scons_example name="PathAccept">
1434 <file name="SConstruct" printme="1">
1435 opts = Options('custom.py')
1436 opts.Add(PathOption('OUTPUT',
1437 'Path to output file or directory',
1439 PathOption.PathAccept))
1440 env = Environment(options = opts,
1441 CPPDEFINES={'OUTPUT' : '"$OUTPUT"'})
1442 env.Program('foo.c')
1452 <title>Enabled/Disabled Path Names: the &PackageOption; Build Option</title>
1456 Sometimes you want to give users
1457 even more control over a path name variable,
1458 allowing them to explicitly enable or
1459 disable the path name
1460 by using <literal>yes</literal> or <literal>no</literal> keywords,
1461 in addition to allow them
1462 to supply an explicit path name.
1463 &SCons; supports the &PackageOption;
1464 function to support this:
1468 <scons_example name="PackageOption">
1469 <file name="SConstruct" printme="1">
1470 opts = Options('custom.py')
1471 opts.Add(PackageOption('PACKAGE',
1473 '__ROOT__/opt/location'))
1474 env = Environment(options = opts,
1475 CPPDEFINES={'PACKAGE' : '"$PACKAGE"'})
1476 env.Program('foo.c')
1481 <file name="__ROOT__/opt/location">
1484 <file name="__ROOT__/usr/local/location">
1491 When the &SConscript; file uses the &PackageOption; funciton,
1492 user can now still use the default
1493 or supply an overriding path name,
1494 but can now explicitly set the
1495 specified variable to a value
1496 that indicates the package should be enabled
1497 (in which case the default should be used)
1502 <scons_output example="PackageOption">
1503 <scons_output_command>scons -Q foo.o</scons_output_command>
1504 <scons_output_command>scons -Q PACKAGE=__ROOT__/usr/local/location foo.o</scons_output_command>
1505 <scons_output_command>scons -Q PACKAGE=yes foo.o</scons_output_command>
1506 <scons_output_command>scons -Q PACKAGE=no foo.o</scons_output_command>
1514 <title>Adding Multiple Command-Line Build Options at Once</title>
1518 Lastly, &SCons; provides a way to add
1519 multiple build options to an &Options object at once.
1520 Instead of having to call the &Add; method
1522 you can call the &AddOptions;
1523 method with a list of build options
1524 to be added to the object.
1525 Each build option is specified
1526 as either a tuple of arguments,
1527 just like you'd pass to the &Add; method itself,
1528 or as a call to one of the canned
1529 functions for pre-packaged command-line build options.
1534 <scons_example name="AddOptions_1">
1535 <file name="SConstruct" printme="1">
1538 ('RELEASE', 'Set to 1 to build for release', 0),
1539 ('CONFIG', 'Configuration file', '/etc/my_config'),
1540 BoolOption('warnings', 'compilation with -Wall and similiar', 1),
1541 EnumOption('debug', 'debug output and symbols', 'no',
1542 allowed_values=('yes', 'no', 'full'),
1543 map={}, ignorecase=0), # case sensitive
1544 ListOption('shared',
1545 'libraries to build as shared libraries',
1547 names = list_of_libs),
1548 PackageOption('x11',
1549 'use X11 installed here (yes = search some places)',
1551 PathOption('qtdir', 'where the root of Qt is installed', qtdir),