From cc4bf014a6ea5d5c4d4ff7c6039f387adcc3d762 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Tue, 15 Sep 2020 11:20:27 -0600 Subject: Tweaks to variant dir documentation [skip appveyor] Manpage/shared VariantDir function wording tries to make more clear that build happens in the variant. Other tweaking. User guide variant description similarly updated. A (worded) example of why one might want variants is added. Formerly separate chapter doc/user/variants.xml is now included as a section in the previous chapter. An additional example is enabled (was in the file but commented out), which some introductiory wording. The introduction to the other sample in the file is also built up slightly. Dropped embedded comments that were the old Cons documentation for these topics. Signed-off-by: Mats Wichmann --- SCons/Environment.xml | 91 ++++++--------- doc/generated/examples/separate_ex1_1.xml | 2 + doc/user/main.xml | 1 - doc/user/separate.xml | 179 ++++++++++-------------------- doc/user/variants.xml | 70 ++++++------ 5 files changed, 132 insertions(+), 211 deletions(-) diff --git a/SCons/Environment.xml b/SCons/Environment.xml index ac40f74..13d0754 100644 --- a/SCons/Environment.xml +++ b/SCons/Environment.xml @@ -3171,8 +3171,8 @@ def create(target, source, env): # A function that will write a 'prefix=$SOURCE' # string into the file name specified as the # $TARGET. - f = open(str(target[0]), 'wb') - f.write('prefix=' + source[0].get_contents()) + with open(str(target[0]), 'wb') as f: + f.write('prefix=' + source[0].get_contents()) # Fetch the prefix= argument, if any, from the command # line, and use /usr/local as the default. @@ -3206,90 +3206,75 @@ env.UpdateValue(target = Value(output), source = Value(input)) -Use the -&f-VariantDir; -function to create a copy of your sources in another location: -if a name under -variant_dir -is not found but exists under -src_dir, -the file or directory is copied to -variant_dir. -Target files can be built in a different directory -than the original sources by simply refering to the sources (and targets) -within the variant tree. - - - +Sets up an alternate build location. +When building in the variant_dir, +&SCons; backfills as needed with files from src_dir +to create a complete build directory. &f-VariantDir; can be called multiple times with the same src_dir to set up multiple builds with different options -(variants). +(variants). + + + The -src_dir -location must be in or underneath the SConstruct file's directory, and -variant_dir +variant +location must be in or underneath the project top directory, +and src_dir may not be underneath -src_dir. - +variant_dir. -The default behavior is for -&scons; -to physically duplicate the source files in the variant tree. +By default, &SCons; +physically duplicates the source files and SConscript files +as needed into the variant tree. Thus, a build performed in the variant tree is guaranteed to be identical to a build performed in the source tree even if intermediate source files are generated during the build, -or preprocessors or other scanners search for included files +or if preprocessors or other scanners search for included files relative to the source file, -or individual compilers or other invoked tools are hard-coded +or if individual compilers or other invoked tools are hard-coded to put derived files in the same directory as source files. +Only the files &SCons; calculates are needed for the build are +duplicated into variant_dir. If possible on the platform, -the duplication is performed by linking rather than copying; -see also the +the duplication is performed by linking rather than copying. +This behavior is affected by the command-line option. -Moreover, only the files needed for the build are duplicated; -files and directories that are not used are not present in -variant_dir. -Duplicating the source tree may be disabled by setting the -duplicate +Physically duplicating the source files may be disabled by setting the +duplicate argument to -0 -(zero). +False. This will cause -&scons; +&SCons; to invoke Builders using the path names of source files in src_dir and the path names of derived files within variant_dir. -This is always more efficient than -duplicate=1, -and is usually safe for most builds -(but see above for cases that may cause problems). +This is more efficient than +duplicate=True, +and is safe for most builds; +revert to True +if it causes problems. -Note that &f-VariantDir; -works most naturally with a subsidiary SConscript file. -However, you would then call the subsidiary SConscript file -not in the source directory, but in the +works most naturally with used with a subsidiary SConscript file. +The subsidiary SConscript file is called as if it +were in variant_dir, regardless of the value of -duplicate. +duplicate. This is how you tell &scons; which variant of a source tree to build: @@ -3319,15 +3304,11 @@ Examples: # use names in the build directory, not the source directory VariantDir('build', 'src', duplicate=0) Program('build/prog', 'build/source.c') - - # this builds both the source and docs in a separate subtree VariantDir('build', '.', duplicate=0) SConscript(dirs=['build/src','build/doc']) - - # same as previous example, but only uses SConscript SConscript(dirs='src', variant_dir='build/src', duplicate=0) SConscript(dirs='doc', variant_dir='build/doc', duplicate=0) diff --git a/doc/generated/examples/separate_ex1_1.xml b/doc/generated/examples/separate_ex1_1.xml index 6efc16f..803bc4f 100644 --- a/doc/generated/examples/separate_ex1_1.xml +++ b/doc/generated/examples/separate_ex1_1.xml @@ -3,6 +3,8 @@ SConscript hello.c % scons -Q cc -o build/hello.o -c build/hello.c cc -o build/hello build/hello.o +% ls src +SConscript hello.c % ls build SConscript hello hello.c hello.o diff --git a/doc/user/main.xml b/doc/user/main.xml index a19df62..494a208 100644 --- a/doc/user/main.xml +++ b/doc/user/main.xml @@ -105,7 +105,6 @@ - diff --git a/doc/user/separate.xml b/doc/user/separate.xml index 748a124..a000260 100644 --- a/doc/user/separate.xml +++ b/doc/user/separate.xml @@ -2,7 +2,7 @@ %scons; - + %builders-mod; @@ -17,7 +17,7 @@ xmlns="http://www.scons.org/dbxsd/v1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.scons.org/dbxsd/v1.0 http://www.scons.org/dbxsd/v1.0/scons.xsd"> -Separating Source and Build Directories +Separating Source and Build Trees: Variant Directories - + - It's often useful to keep any built files completely - separate from the source files. - In &SCons;, this is usually done by creating one or more separate - variant directory trees + You can enable this separation by creating one or more + variant directory trees that are used to hold the built objects files, libraries, and executable programs, etc. for a specific flavor, or variant, of build. &SCons; provides two ways to do this, - one through the &SConscript; function that we've already seen, - and the second through a more flexible &VariantDir; function. + one through the &f-link-SConscript; function that we've already seen, + and the second through a more flexible &f-link-VariantDir; function. - One historical note: the &VariantDir; function + Historical note: the &VariantDir; function used to be called &BuildDir;, a name which was removed because the &SCons; functionality - differs from the model of a "build directory" - implemented by other build systems like the GNU Autotools. + differs from a familiar model of a "build directory" + implemented by other build systems like GNU Autotools. + You might still find references to the old name on + the Internet in postings about &SCons;, but it no longer works. @@ -162,11 +96,11 @@ program using the F path name. The most straightforward way to establish a variant directory tree - uses the fact that the usual way to + relies the fact that the usual way to set up a build hierarchy is to have an - &SConscript; file in the source subdirectory. - If you then pass a &variant_dir; argument to the - &SConscript; function call: + SConscript file in the source subdirectory. + If you pass a &variant_dir; argument to the + &f-link-SConscript; function call: @@ -193,20 +127,25 @@ int main() { printf("Hello, world!\n"); } ls src scons -Q + ls src ls build - But wait a minute--what's going on here? - &SCons; created the object file + No files were built in &src;, they went to &build;. + The build output might show a bit of a surprise: + the object file build/hello.o - in the &build; subdirectory, + and the executable file + build/hello + were built in the &build; subdirectory, as expected. But even though our &hello_c; file lives in the &src; subdirectory, &SCons; has actually compiled a build/hello.c file - to create the object file. + to create the object file, + and that file is now seen in &build;. @@ -215,7 +154,7 @@ int main() { printf("Hello, world!\n"); } What's happened is that &SCons; has duplicated the &hello_c; file from the &src; subdirectory to the &build; subdirectory, - and built the program from there. + and built the program from there (it also duplicated &SConscript;). The next section explains why &SCons; does this. @@ -227,13 +166,13 @@ int main() { printf("Hello, world!\n"); } - &SCons; duplicates source files in variant directory trees - because it's the most straightforward way to guarantee a correct build - regardless of include-file directory paths, - relative references between files, - or tool support for putting files in different locations, - and the &SCons; philosophy is to, by default, - guarantee a correct build in all cases. + The main thing to understand is that when you set up a variant directory, + &SCons; performs the build in that directory. + It turns out it's easiest to ensure where build products end up + by just building in place. + Since the build is happening in a place different from where the + sources are, the most straightforward way to guarantee a correct build + is for &SCons; to copy them there. @@ -312,13 +251,13 @@ int main() { printf("Hello, world!\n"); } duplicating the source files and everything will work just fine. You can disable the default &SCons; behavior - by specifying duplicate=0 + by specifying duplicate=False when you call the &SConscript; function: -SConscript('src/SConscript', variant_dir='build', duplicate=0) +SConscript('src/SConscript', variant_dir='build', duplicate=False) @@ -395,14 +334,14 @@ int main() { printf("Hello, world!\n"); } - You can specify the same duplicate=0 argument + You can specify the same duplicate=False argument that you can specify for an &SConscript; call: -VariantDir('build', 'src', duplicate=0) +VariantDir('build', 'src', duplicate=False) env = Environment() env.Program('build/hello.c') @@ -432,8 +371,10 @@ int main() { printf("Hello, world!\n"); } Even when using the &VariantDir; function, - it's much more natural to use it with - a subsidiary &SConscript; file. + it's more natural to use it with + a subsidiary &SConscript; file, + because then you don't have to adjust your individual + build instructions to use the variant directory path. For example, if the src/SConscript looks like this: @@ -490,7 +431,7 @@ int main() { printf("Hello, world!\n"); } - The &Glob; file name pattern matching function + The &f-link-Glob; file name pattern matching function works just as usual when using &VariantDir;. For example, if the src/SConscript @@ -558,4 +499,6 @@ const char * f2(); --> + + diff --git a/doc/user/variants.xml b/doc/user/variants.xml index 0c83b04..6cf8c3d 100644 --- a/doc/user/variants.xml +++ b/doc/user/variants.xml @@ -13,11 +13,11 @@ %variables-mod; ]> - -Variant Builds +Variant Build Examples - - The &variant_dir; keyword argument of @@ -76,9 +52,15 @@ is pretty smart about rebuilding things when you change options. variant builds using &SCons;. Suppose, for example, that we want to build a program for both Windows and Linux platforms, - but that we want to build it in a shared directory + but that we want to build it in directory on a network share with separate side-by-side build directories for the Windows and Linux versions of the program. + We have to do a little bit of work to construct paths, + to make sure unwanted location dependencies don't creep in. + The top-relative path reference can be useful here. + To avoid writing conditional code based on platform, + we can build the variant_dir + path dynamically: @@ -90,13 +72,15 @@ include = "#export/$PLATFORM/include" lib = "#export/$PLATFORM/lib" bin = "#export/$PLATFORM/bin" -env = Environment(PLATFORM = platform, - BINDIR = bin, - INCDIR = include, - LIBDIR = lib, - CPPPATH = [include], - LIBPATH = [lib], - LIBS = 'world') +env = Environment( + PLATFORM=platform, + BINDIR=bin, + INCDIR=include, + LIBDIR=lib, + CPPPATH=[include], + LIBPATH=[lib], + LIBS='world', +) Export('env') @@ -155,20 +139,32 @@ int world() { printf "world.c\n"; } scons -Q OS=windows - - + -- cgit v0.12