Source

SCons / doc / user / environments.xml

Full commit
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
<!--

  __COPYRIGHT__

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-->

<!--

=head1 More on construction environments

As previously mentioned, a B<construction environment> is an object that
has a set of keyword/value pairs and a set of methods, and which is used
to tell Cons how target files should be built.  This section describes
how Cons uses and expands construction environment values to control its
build behavior.

=head2 Construction variable expansion

Construction variables from a construction environment are expanded
by preceding the keyword with a C<%> (percent sign):

  Construction variables:
	XYZZY => 'abracadabra',

  The string:  "The magic word is:  %XYZZY!"
  expands to:  "The magic word is:  abracadabra!"

A construction variable name may be surrounded by C<{> and C<}> (curly
braces), which are stripped as part of the expansion.  This can
sometimes be necessary to separate a variable expansion from trailing
alphanumeric characters:

  Construction variables:
	OPT    => 'value1',
	OPTION => 'value2',

  The string:  "%OPT %{OPT}ION %OPTION %{OPTION}"
  expands to:  "value1 value1ION value2 value2"

Construction variable expansion is recursive, that is, a string
containing C<%->expansions after substitution will be re-expanded until
no further substitutions can be made:

  Construction variables:
	STRING => 'The result is:  %FOO',
	FOO    => '%BAR',
	BAR    => 'final value',

  The string:  "The string says:  %STRING"
  expands to:  "The string says:  The result is:  final value"

If a construction variable is not defined in an environment, then the
null string is substituted:

  Construction variables:
	FOO => 'value1',
	BAR => 'value2',

  The string:  "%FOO <%NO_VARIABLE> %BAR"
  expands to:  "value1 <> value2"

A doubled C<%%> will be replaced by a single C<%>:

  The string:  "Here is a percent sign:  %%"
  expands to:  "Here is a percent sign: %"

=head2 Default construction variables

When you specify no arguments when creating a new construction
environment:

  $env = new cons();

Cons creates a reference to a new, default construction
environment. This contains a number of construction variables and some
methods. At the present writing, the default construction variables on a
UNIX system are:

  CC            => 'cc',
  CFLAGS        => '',
  CCCOM         => '%CC %CFLAGS %_IFLAGS -c %< -o %>',
  CXX           => '%CC',
  CXXFLAGS      => '%CFLAGS',
  CXXCOM        => '%CXX %CXXFLAGS %_IFLAGS -c %< -o %>',
  INCDIRPREFIX  => '-I',
  INCDIRSUFFIX  => '',
  LINK          => '%CXX',
  LINKCOM       => '%LINK %LDFLAGS -o %> %< %_LDIRS %LIBS',
  LINKMODULECOM => '%LD -r -o %> %<',
  LIBDIRPREFIX  => '-L',
  LIBDIRSUFFIX  => '',
  AR		=> 'ar',
  ARFLAGS	=> 'r',
  ARCOM		=> ['%AR %ARFLAGS %> %<', '%RANLIB %>'],
  RANLIB	=> 'ranlib',
  AS		=> 'as',
  ASFLAGS	=> '',
  ASCOM		=> '%AS %ASFLAGS %< -o %>',
  LD		=> 'ld',
  LDFLAGS	=> '',
  PREFLIB	=> 'lib',
  SUFLIB	=> '.a',
  SUFLIBS	=> '.so:.a',
  SUFOBJ	=> '.o',
  SIGNATURE     => [ '*' => 'build' ],
  ENV		=> { 'PATH' => '/bin:/usr/bin' },


And on a Windows system (Windows NT), the default construction variables
are (unless the default rule style is set using the B<DefaultRules>
method):

  CC		=> 'cl',
  CFLAGS	=> '/nologo',
  CCCOM		=> '%CC %CFLAGS %_IFLAGS /c %< /Fo%>',
  CXXCOM        => '%CXX %CXXFLAGS %_IFLAGS /c %< /Fo%>',
  INCDIRPREFIX  => '/I',
  INCDIRSUFFIX  => '',
  LINK          => 'link',
  LINKCOM       => '%LINK %LDFLAGS /out:%> %< %_LDIRS %LIBS',
  LINKMODULECOM => '%LD /r /o %> %<',
  LIBDIRPREFIX  => '/LIBPATH:',
  LIBDIRSUFFIX  => '',
  AR            => 'lib',
  ARFLAGS       => '/nologo ',
  ARCOM         => "%AR %ARFLAGS /out:%> %<",
  RANLIB        => '',
  LD            => 'link',
  LDFLAGS       => '/nologo ',
  PREFLIB       => '',
  SUFEXE	=> '.exe',
  SUFLIB	=> '.lib',
  SUFLIBS	=> '.dll:.lib',
  SUFOBJ	=> '.obj',
  SIGNATURE     => [ '*' => 'build' ],

These variables are used by the various methods associated with the
environment. In particular, any method that ultimately invokes an external
command will substitute these variables into the final command, as
appropriate. For example, the C<Objects> method takes a number of source
files and arranges to derive, if necessary, the corresponding object
files:

  Objects $env 'foo.c', 'bar.c';

This will arrange to produce, if necessary, F<foo.o> and F<bar.o>. The
command invoked is simply C<%CCCOM>, which expands, through substitution,
to the appropriate external command required to build each object. The
substitution rules will be discussed in detail in the next section.

The construction variables are also used for other purposes. For example,
C<CPPPATH> is used to specify a colon-separated path of include
directories. These are intended to be passed to the C preprocessor and are
also used by the C-file scanning machinery to determine the dependencies
involved in a C Compilation.

Variables beginning with underscore are created by various methods,
and should normally be considered ``internal'' variables. For example,
when a method is called which calls for the creation of an object from
a C source, the variable C<_IFLAGS> is created: this corresponds to the
C<-I> switches required by the C compiler to represent the directories
specified by C<CPPPATH>.

Note that, for any particular environment, the value of a variable is set
once, and then never reset (to change a variable, you must create a new
environment. Methods are provided for copying existing environments for this
purpose). Some internal variables, such as C<_IFLAGS> are created on demand,
but once set, they remain fixed for the life of the environment.

The C<CFLAGS>, C<LDFLAGS>, and C<ARFLAGS> variables all supply a place
for passing options to the compiler, loader, and archiver, respectively.

The C<INCDIRPREFIX> and C<INCDIRSUFFIX> variables specify option
strings to be appended to the beginning and end, respectively, of each
include directory so that the compiler knows where to find F<.h> files.
Similarly, the C<LIBDIRPREFIX> and C<LIBDIRSUFFIX> variables specify the
option string to be appended to the beginning of and end, respectively,
of each directory that the linker should search for libraries.

Another variable, C<ENV>, is used to determine the system environment during
the execution of an external command. By default, the only environment
variable that is set is C<PATH>, which is the execution path for a UNIX
command. For the utmost reproducibility, you should really arrange to set
your own execution path, in your top-level F<Construct> file (or perhaps by
importing an appropriate construction package with the Perl C<use>
command). The default variables are intended to get you off the ground.

=head2 Expanding variables in construction commands

Within a construction command, construction variables will be expanded
according to the rules described above.  In addition to normal variable
expansion from the construction environment, construction commands also
expand the following pseudo-variables to insert the specific input and
output files in the command line that will be executed:

=over 10

=item %>

The target file name.  In a multi-target command, this expands to the
first target mentioned.)

=item %0

Same as C<%E<gt>>.

=item %1, %2, ..., %9

These refer to the first through ninth input file, respectively.

=item %E<lt>

The full set of input file names. If any of these have been used
anywhere else in the current command line (via C<%1>, C<%2>, etc.), then
those will be deleted from the list provided by C<%E<lt>>. Consider the
following command found in a F<Conscript> file in the F<test> directory:

  Command $env 'tgt', qw(foo bar baz), qq(
	echo %< -i %1 > %>
	echo %< -i %2 >> %>
	echo %< -i %3 >> %>
  );

If F<tgt> needed to be updated, then this would result in the execution of
the following commands, assuming that no remapping has been established for
the F<test> directory:

  echo test/bar test/baz -i test/foo > test/tgt
  echo test/foo test/baz -i test/bar >> test/tgt
  echo test/foo test/bar -i test/baz >> test/tgt

=back

Any of the above pseudo-variables may be followed immediately by one of
the following suffixes to select a portion of the expanded path name:

  :a    the absolute path to the file name
  :b    the directory plus the file name stripped of any suffix
  :d    the directory
  :f    the file name
  :s    the file name suffix
  :F    the file name stripped of any suffix
  :S    the absolute path path to a Linked source file

Continuing with the above example, C<%E<lt>:f> would expand to C<foo bar baz>,
and C<%E<gt>:d> would expand to C<test>.

There are additional C<%> elements which affect the command line(s):

=over 10

=item %[ %]

It is possible to programmatically rewrite part of the command by
enclosing part of it between C<%[> and C<%]>.  This will call the
construction variable named as the first word enclosed in the brackets
as a Perl code reference; the results of this call will be used to
replace the contents of the brackets in the command line.  For example,
given an existing input file named F<tgt.in>:

  @keywords = qw(foo bar baz);
  $env = new cons(X_COMMA => sub { join(",", @_) });
  Command $env 'tgt', 'tgt.in', qq(
	echo '# Keywords: %[X_COMMA @keywords %]' > %>
	cat %< >> %>
  );

This will execute:

  echo '# Keywords: foo,bar,baz' > tgt
  cat tgt.in >> tgt

=item %( %)

Cons includes the text of the command line in the MD5 signature for a
build, so that targets get rebuilt if you change the command line (to
add or remove an option, for example).  Command-line text in between
C<%(> and C<%)>, however, will be ignored for MD5 signature calculation.

Internally, Cons uses C<%(> and C<%)> around include and library
directory options (C<-I> and C<-L> on UNIX systems, C</I> and
C</LIBPATH> on Windows NT) to avoid rebuilds just because the directory
list changes.  Rebuilds occur only if the changed directory list causes
any included I<files> to change, and a changed include file is detected
by the MD5 signature calculation on the actual file contents.

=back

XXX DESCRIBE THE Literal() FUNCTION, TOO XXX

=head2 Expanding construction variables in file names

Cons expands construction variables in the source and target file names
passed to the various construction methods according to the expansion
rules described above:

  $env = new cons(
	DESTDIR	=>	'programs',
	SRCDIR	=>	'src',
  );
  Program $env '%DESTDIR/hello', '%SRCDIR/hello.c';

This allows for flexible configuration, through the construction
environment, of directory names, suffixes, etc.

=head1 Default construction methods

The list of default construction methods includes the following:


=head2 The C<new> constructor

The C<new> method is a Perl object constructor. That is, it is not invoked
via a reference to an existing construction environment B<reference>, but,
rather statically, using the name of the Perl B<package> where the
constructor is defined. The method is invoked like this:

  $env = new cons(<overrides>);

The environment you get back is blessed into the package C<cons>, which
means that it will have associated with it the default methods described
below. Individual construction variables can be overridden by providing
name/value pairs in an override list. Note that to override any command
environment variable (i.e. anything under C<ENV>), you will have to override
all of them. You can get around this difficulty by using the C<copy> method
on an existing construction environment.


=head2 The C<clone> method

The C<clone> method creates a clone of an existing construction environment,
and can be called as in the following example:

  $env2 = $env1->clone(<overrides>);

You can provide overrides in the usual manner to create a different
environment from the original. If you just want a new name for the same
environment (which may be helpful when exporting environments to existing
components), you can just use simple assignment.


=head2 The C<copy> method

The C<copy> method extracts the externally defined construction variables
from an environment and returns them as a list of name/value
pairs. Overrides can also be provided, in which case, the overridden values
will be returned, as appropriate. The returned list can be assigned to a
hash, as shown in the prototype, below, but it can also be manipulated in
other ways:

  %env = $env1->copy(<overrides>);

The value of C<ENV>, which is itself a hash, is also copied to a new hash,
so this may be changed without fear of affecting the original
environment. So, for example, if you really want to override just the
C<PATH> variable in the default environment, you could do the following:

  %cons = new cons()->copy();
  $cons{ENV}{PATH} = "<your path here>";
  $cons = new cons(%cons);

This will leave anything else that might be in the default execution
environment undisturbed.

-->

 <para>

   It is rare that all of the software in a large,
   complicated system needs to be built the same way.
   For example, different source files may need different options
   enabled on the command line,
   or different executable programs need to be linked
   with different libraries.
   &SCons; accommodates these different build
   requirements by allowing you to create and
   configure multiple &consenvs;
   that control how the software is built.
   Technically, a &consenv; is an object
   that has a number of associated
   &consvars;, each with a name and a value.
   (A construction environment also has an attached
   set of &Builder; methods,
   about which we'll learn more later.)

 </para>

 <para>

   A &consenv; is created by the &Environment; method:

 </para>

  <programlisting>
    env = Environment()
  </programlisting>

 <para>

   By default, &SCons; initializes every
   new construction environment
   with a set of &consvars;
   based on the tools that it finds on your system,
   plus the default set of builder methods
   necessary for using those tools.
   The construction variables
   are initialized with values describing
   the C compiler,
   the Fortran compiler,
   the linker,
   etc.,
   as well as the command lines to invoke them.

 </para>

 <para>

   When you initialize a construction environment
   you can set the values of the
   environment's &consvars;
   to control how a program is built.
   For example:

 </para>

  <programlisting>
    env = Environment(CC = 'gcc',
                      CCFLAGS = '-O2')

    env.Program('foo.c')
  </programlisting>

 <para>

   The construction environment in this example
   is still initialized with the same default
   construction variable values,
   except that the user has explicitly specified use of the
   GNU C compiler &gcc;,
   and further specifies that the <literal>-O2</literal>
   (optimization level two)
   flag should be used when compiling the object file.
   In other words, the explicit initializations of
   &cv-link-CC; and &cv-link-CCFLAGS;
   override the default values in the newly-created
   construction environment.
   So a run from this example would look like:

 </para>

 <screen>
    % <userinput>scons -Q</userinput>
    gcc -o foo.o -c -O2 foo.c
    gcc -o foo foo.o
 </screen>

 <section>
 <title>Multiple &ConsEnvs;</title>

   <para>

   The real advantage of construction environments
   is that you can create as many different construction
   environments as you need,
   each tailored to a different way to build
   some piece of software or other file.
   If, for example, we need to build
   one program with the <literal>-O2</literal> flag
   and another with the <literal>-g</literal> (debug) flag,
   we would do this like so:

   </para>

   <programlisting>
      opt = Environment(CCFLAGS = '-O2')
      dbg = Environment(CCFLAGS = '-g')

      opt.Program('foo', 'foo.c')

      dbg.Program('bar', 'bar.c')
   </programlisting>

   <screen>
      % <userinput>scons -Q</userinput>
      cc -o bar.o -c -g bar.c
      cc -o bar bar.o
      cc -o foo.o -c -O2 foo.c
      cc -o foo foo.o
   </screen>

   <para>

   We can even use multiple construction environments to build
   multiple versions of a single program.
   If you do this by simply trying to use the
   &b-link-Program; builder with both environments, though,
   like this:

   </para>

   <programlisting>
      opt = Environment(CCFLAGS = '-O2')
      dbg = Environment(CCFLAGS = '-g')

      opt.Program('foo', 'foo.c')

      dbg.Program('foo', 'foo.c')
   </programlisting>

   <para>

   Then &SCons; generates the following error:

   </para>

   <screen>
      % <userinput>scons -Q</userinput>
      
      scons: *** Two environments with different actions were specified for the same target: foo.o
      File "/home/my/project/SConstruct", line 6, in &lt;module&gt;
   </screen>

   <para>

   This is because the two &b-Program; calls have
   each implicitly told &SCons; to generate an object file named
   <filename>foo.o</filename>,
   one with a &cv-link-CCFLAGS; value of
   <literal>-O2</literal>
   and one with a &cv-link-CCFLAGS; value of
   <literal>-g</literal>.
   &SCons; can't just decide that one of them
   should take precedence over the other,
   so it generates the error.
   To avoid this problem,
   we must explicitly specify
   that each environment compile
   <filename>foo.c</filename>
   to a separately-named object file
   using the &b-link-Object; builder, like so:

   </para>

   <programlisting>
      opt = Environment(CCFLAGS = '-O2')
      dbg = Environment(CCFLAGS = '-g')

      o = opt.Object('foo-opt', 'foo.c')
      opt.Program(o)

      d = dbg.Object('foo-dbg', 'foo.c')
      dbg.Program(d)
   </programlisting>

   <para>

   Notice that each call to the &b-Object; builder
   returns a value,
   an internal &SCons; object that
   represents the object file that will be built.
   We then use that object
   as input to the &b-Program; builder.
   This avoids having to specify explicitly
   the object file name in multiple places,
   and makes for a compact, readable
   &SConstruct; file.
   Our &SCons; output then looks like:

   </para>

   <screen>
      % <userinput>scons -Q</userinput>
      cc -o foo-dbg.o -c -g foo.c
      cc -o foo-dbg foo-dbg.o
      cc -o foo-opt.o -c -O2 foo.c
      cc -o foo-opt foo-opt.o
   </screen>

 </section>

 <section>
 <title>Copying &ConsEnvs;</title>

   <para>

   Sometimes you want more than one construction environment
   to share the same values for one or more variables.
   Rather than always having to repeat all of the common
   variables when you create each construction environment,
   you can use the &Clone; method
   to create a copy of a construction environment.

   </para>

   <para>

   Like the &Environment; call that creates a construction environment,
   the &Clone; method takes &consvar; assignments,
   which will override the values in the copied construction environment.
   For example, suppose we want to use &gcc;
   to create three versions of a program,
   one optimized, one debug, and one with neither.
   We could do this by creating a "base" construction environment
   that sets &cv-link-CC; to &gcc;,
   and then creating two copies,
   one which sets &cv-link-CCFLAGS; for optimization
   and the other which sets &cv-CCFLAGS; for debugging:

   </para>

   <programlisting>
      env = Environment(CC = 'gcc')
      opt = env.Clone(CCFLAGS = '-O2')
      dbg = env.Clone(CCFLAGS = '-g')

      env.Program('foo', 'foo.c')

      o = opt.Object('foo-opt', 'foo.c')
      opt.Program(o)

      d = dbg.Object('foo-dbg', 'foo.c')
      dbg.Program(d)
   </programlisting>

   <para>

   Then our output would look like:

   </para>

   <screen>
      % <userinput>scons -Q</userinput>
      gcc -o foo.o -c foo.c
      gcc -o foo foo.o
      gcc -o foo-dbg.o -c -g foo.c
      gcc -o foo-dbg foo-dbg.o
      gcc -o foo-opt.o -c -O2 foo.c
      gcc -o foo-opt foo-opt.o
   </screen>

 </section>

 <section>
 <title>Fetching Values From a &ConsEnv;</title>

   <para>

   You can fetch individual construction variables
   using the normal syntax
   for accessing individual named items in a Python dictionary:

   </para>

   <programlisting>
      env = Environment()
      print "CC is:", env['CC']
   </programlisting>

   <para>

   This example &SConstruct; file doesn't build anything,
   but because it's actually a Python script,
   it will print the value of &cv-link-CC; for us:

   </para>

   <screen>
      % <userinput>scons -Q</userinput>
      CC is: cc
      scons: `.' is up to date.
   </screen>

   <para>

   A construction environment, however,
   is actually an object with associated methods, etc.
   If you want to have direct access to only the
   dictionary of construction variables,
   you can fetch this using the &Dictionary; method:

   </para>

   <programlisting>
      env = Environment(FOO = 'foo', BAR = 'bar')
      dict = env.Dictionary()
      for key in ['OBJSUFFIX', 'LIBSUFFIX', 'PROGSUFFIX']:
          print "key = %s, value = %s" % (key, dict[key])
   </programlisting>

   <para>

   This &SConstruct; file
   will print the specified dictionary items for us on POSIX
   systems as follows:

   </para>

   <screen>
      % <userinput>scons -Q</userinput>
      key = OBJSUFFIX, value = .o
      key = LIBSUFFIX, value = .a
      key = PROGSUFFIX, value = 
      scons: `.' is up to date.
   </screen>

   <para>

   And on Windows:

   </para>

   <screen>
      C:\><userinput>scons -Q</userinput>
      key = OBJSUFFIX, value = .obj
      key = LIBSUFFIX, value = .lib
      key = PROGSUFFIX, value = .exe
      scons: `.' is up to date.
   </screen>

   <para>

   If you want to loop through and print the values of
   all of the construction variables in a construction environment,
   the Python code to do that in sorted order might look something like:

   </para>

   <programlisting>
      env = Environment()
      dict = env.Dictionary()
      keys = dict.keys()
      keys.sort()
      for key in keys:
          print "construction variable = '%s', value = '%s'" % (key, dict[key])
   </programlisting>

 </section>

 <section>
 <title>Expanding Values From a &ConsEnv;</title>

   <para>

   Another way to get information from
   a construction environment.
   is to use the &subst; method
   on a string containing $-expansions
   of construction variable names.
   As a simple example,
   the example from the previous
   section that used
   <literal>env['CC']</literal>
   to fetch the value of &cv-link-CC;
   could also be written as:

   </para>

   <programlisting>
     env = Environment()
     print "CC is:", env.subst('$CC')
   </programlisting>

   <para>

   The real advantage of using
   &subst; to expand strings is
   that construction variables
   in the result get
   re-expanded until
   there are no expansions left in the string.
   So a simple fetch of a value like
   &cv-link-CCCOM;:

   </para>

   <programlisting>
     env = Environment(CCFLAGS = '-DFOO')
     print "CCCOM is:", env['CCCOM']
   </programlisting>

   <para>

   Will print the unexpanded value of &cv-CCCOM;,
   showing us the construction
   variables that still need to be expanded:

   </para>

   <screen>
     % <userinput>scons -Q</userinput>
     CCCOM is: $CC $CCFLAGS $CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS -c -o $TARGET $SOURCES
     scons: `.' is up to date.
   </screen>

   <para>

   Calling the &subst; method on <varname>$CCOM</varname>,
   however:

   </para>

   <programlisting>
     env = Environment(CCFLAGS = '-DFOO')
     print "CCCOM is:", env.subst('$CCCOM')
   </programlisting>

   <para>

   Will recursively expand all of
   the $-prefixed construction variables,
   showing us the final output:

   </para>

   <screen>
     % <userinput>scons -Q</userinput>
     CCCOM is: gcc -DFOO -c -o
     scons: `.' is up to date.
   </screen>

   <para>

   (Note that because we're not expanding this
   in the context of building something
   there are no target or source files
   for &cv-link-TARGET; and &cv-link-SOURCES; to expand.)

   </para>

 </section>

 <section>
 <title>Modifying a &ConsEnv;</title>

   <para>

   &SCons; provides various methods that
   support modifying existing values in a construction environment.

   </para>

   <section>
   <title>Replacing Values in a &ConsEnv;</title>

     <para>

     You can replace existing construction variable values
     using the &Replace; method:

     </para>

     <programlisting>
        env = Environment(CCFLAGS = '-DDEFINE1')
        env.Replace(CCFLAGS = '-DDEFINE2')
        env.Program('foo.c')
     </programlisting>

     <para>

     The replacing value
     (<literal>-DDEFINE2</literal> in the above example)
     completely replaces the value in the
     construction environment:

     </para>

     <screen>
        % <userinput>scons -Q</userinput>
        cc -o foo.o -c -DDEFINE2 foo.c
        cc -o foo foo.o
     </screen>

     <para>

     You can safely call &Replace;
     for construction variables that
     don't exist in the construction environment:

     </para>

     <programlisting>
        env = Environment()
        env.Replace(NEW_VARIABLE = 'xyzzy')
        print "NEW_VARIABLE =", env['NEW_VARIABLE']
     </programlisting>

     <para>

     In this case,
     the construction variable simply
     gets added to the construction environment:

     </para>

     <screen>
        % <userinput>scons -Q</userinput>
        NEW_VARIABLE = xyzzy
        scons: `.' is up to date.
     </screen>

     <para>

     Because the variables
     aren't expanded until the construction environment
     is actually used to build the targets,
     and because &SCons; function and method calls
     are order-independent,
     the last replacement "wins"
     and is used to build all targets,
     regardless of the order in which
     the calls to Replace() are
     interspersed with calls to
     builder methods:

     </para>

     <programlisting>
        env = Environment(CCFLAGS = '-DDEFINE1')
        print "CCFLAGS =", env['CCFLAGS']
        env.Program('foo.c')

        env.Replace(CCFLAGS = '-DDEFINE2')
        print "CCFLAGS =", env['CCFLAGS']
        env.Program('bar.c')
     </programlisting>

     <para>

     The timing of when the replacement
     actually occurs relative
     to when the targets get built
     becomes apparent
     if we run &scons; without the <literal>-Q</literal>
     option:

     </para>

     <screen>
        % <userinput>scons</userinput>
        scons: Reading SConscript files ...
        CCFLAGS = -DDEFINE1
        CCFLAGS = -DDEFINE2
        scons: done reading SConscript files.
        scons: Building targets ...
        cc -o bar.o -c -DDEFINE2 bar.c
        cc -o bar bar.o
        cc -o foo.o -c -DDEFINE2 foo.c
        cc -o foo foo.o
        scons: done building targets.
     </screen>

     <para>

     Because the replacement occurs while
     the &SConscript; files are being read,
     the &cv-link-CCFLAGS;
     variable has already been set to
     <literal>-DDEFINE2</literal>
     by the time the &foo_o; target is built,
     even though the call to the &Replace;
     method does not occur until later in
     the &SConscript; file.

     </para>

   </section>

   <!--

   <section>
   <title>Setting Values Only If They're Not Already Defined</title>

     <para>

     XXX SetDefault()

     </para>

   </section>

   -->

   <section>
   <title>Appending to the End of Values in a &ConsEnv;</title>

     <para>

     You can append a value to
     an existing construction variable
     using the &Append; method:

     </para>

     <programlisting>
        env = Environment(CCFLAGS = '-DMY_VALUE')
        env.Append(CCFLAGS = ' -DLAST')
        env.Program('foo.c')
     </programlisting>

     <para>

     &SCons; then supplies both the <literal>-DMY_VALUE</literal> and
     <literal>-DLAST</literal> flags when compiling the object file:

     </para>

     <screen>
        % <userinput>scons -Q</userinput>
        cc -o foo.o -c -DMY_VALUE -DLAST foo.c
        cc -o foo foo.o
     </screen>

     <para>

     If the construction variable doesn't already exist,
     the &Append; method will create it:

     </para>

     <programlisting>
        env = Environment()
        env.Append(NEW_VARIABLE = 'added')
        print "NEW_VARIABLE =", env['NEW_VARIABLE']
     </programlisting>

     <para>

     Which yields:

     </para>

     <screen>
        % <userinput>scons -Q</userinput>
        NEW_VARIABLE = added
        scons: `.' is up to date.
     </screen>

     <!--

     XXX AppendUnique()

     -->

   </section>

   <section>
   <title>Appending to the Beginning of Values in a &ConsEnv;</title>

     <para>

     You can append a value to the beginning of
     an existing construction variable
     using the &Prepend; method:

     </para>

     <programlisting>
        env = Environment(CCFLAGS = '-DMY_VALUE')
        env.Prepend(CCFLAGS = '-DFIRST ')
        env.Program('foo.c')
     </programlisting>

     <para>

     &SCons; then supplies both the <literal>-DFIRST</literal> and
     <literal>-DMY_VALUE</literal> flags when compiling the object file:

     </para>

     <screen>
        % <userinput>scons -Q</userinput>
        cc -o foo.o -c -DFIRST -DMY_VALUE foo.c
        cc -o foo foo.o
     </screen>

     <para>

     If the construction variable doesn't already exist,
     the &Prepend; method will create it:

     </para>

     <programlisting>
        env = Environment()
        env.Prepend(NEW_VARIABLE = 'added')
        print "NEW_VARIABLE =", env['NEW_VARIABLE']
     </programlisting>

     <para>

     Which yields:

     </para>

     <screen>
        % <userinput>scons -Q</userinput>
        NEW_VARIABLE = added
        scons: `.' is up to date.
     </screen>

     <!--

     XXX PrependUnique()

     -->

   </section>

   <!--

   <section>
   <title>Adding to Values in the Execution Environment</title>

     <para>

     XXX AppendENVPath()

     XXX PrependENVPath()

     </para>

   </section>

   -->

 </section>