Chapter 7 of scons User Guide: Environment

Source: Internet
Author: User

An environment is a set of values that can affect how a program executes. There are three different types of environments in scons:


External Environment: external environment refers to the set of variables in the user environment when the user runs scons. These variables can be obtained through the OS. Environ Dictionary of Python IN THE sconscript file.
Construction Environment: A constructor is a unique object created in an sconscript file, this object contains some values that can affect the actions of scons when compiling a target and decide to compile the target file from that source. A powerful function of scons is to create multiple constructor environments, including a new custom constructor from an existing constructor environment.
Execution environment: an execution environment is the value set by scons when executing an external command to compile one or more target files. This is different from the external environment. Unlike make, scons does not automatically copy or import values between different environments. This is a deliberate design option, ensuring that compilation can always be repeated regardless of the value of the user's external environment. This avoids compilation issues. For example, a custom setting enables a different compiler or compilation option to successfully compile the developer's local code, however, after checked-in, the compilation fails because different environment variable settings are used.
7.1 When scons is executed using the value from the external environment, the value of the external environment is obtained through the OS. Environ dictionary. This means that you need to add an import OS statement in any sconscript file that you want to use the external environment: Import OS
7.2. Constructing an environment in a large and complex system, it is rare that all software is compiled in the same way. For example, different source files may require different compilation options, or different executable programs need to link different libraries. Scons allows you to create and configure multiple constructor environments to control and meet different compilation requirements.
7.2.1 create a constructor environment: environment function a constructor environment is created by the Environment method: ENV = environment () by default, scons initializes each new constructor environment based on a variable set of tools in your system. When initializing a constructor environment, you can set the constructor variables of the environment to control how a constructor is compiled. Example: Import OS Env = environment (Cc = 'gcc ', ccflags ='-O2 ') ENV. Program ('foo. C ')
7.2.2 obtain values from a constructor. You can use the python dictionary to obtain a single constructor: ENV = environment () print "cc is :", env ['cc'] A constructor environment is actually an object with methods. If you want to directly access the dictionary of the constructor, you can use the dictionary method: ENV = environment (FOO = 'foo', bar = 'bar') dict = Env. dictionary () for key in ['objsuffix ', 'libsuffix', 'progsuffix ']: Print "Key = % s, value = % s" % (key, dict [Key]) If you want to loop and print all the variables in the constructor environment: ENV = environment () for item in sorted (Env. dictionary (). items (): Print "Construction variable = '% s', value =' % S'" % item
7.2.3 extend the value from a constructor environment: The SUBST method is used to obtain information from the constructor environment. For example, ENV = environment () print "cc is:", Env. the advantage of using SUBST to expand a string is that the constructor variable in the result is re-expanded until it cannot be expanded. For example, if you get $ cccom: ENV = environment (ccflags = '-dfoo') print "cccom is:", ENV ['cccom'] will print the $ cccom: % scons-Q cccom is: $ CC $ ccflags $ cppflags $ _ cppdefflags $ _ cppincflags-c-o $ target $ sources scons :'. is up to date. call the SUBST method to obtain $ cccom: ENV = environment (ccflags = '-dfoo') print "cccom is:", Env. SUBST ('$ cccom') Will recursively extend all the construction variables: % scons-Q cccom is: gcc-dfoo-c-o scons :'. is up to date.
7.2.4. If a constructor variable is extended, it is extended to a ''Null String by default, which does not cause scons failure. ENV = environment () print "value is:", ENV. SUBST ('-> $ missing <-')
% Scons-Q value is:-> <-scons: '.' is up to date. Using the allowsubstexception function will change the default behavior. An exception occurs when the value is extended. allowsubstexceptions controls which exception is fatal and which one is allowed to occur safely. By default, the nameerror and indexerror exceptions are allowed. If you need to name all the constructors, call allowsubstanctions: allowsubstanctions () ENV = environment () print "value is:", ENV. SUBST ('-> $ missing <-')
% Scons-Q value is: scons: *** nameerror 'missing' trying to evaluate '$ missing'

File "/home/My/project/sconstruct", line 3, in <module>

You can also use $ {...} to construct the variable syntax to allow other exceptions. For example, the following code allows division by zero: allowsubstexceptions (indexerror, nameerror, zerodivisionerror)

ENV = environment ()
Print "value is:", ENV. SUBST ('->$ {1/0} <-')

% Scons-Q
Value is:-> <-
Scons: '.' is up to date.


7.2.5 control the default constructor environment: The defaultenvironment function uses a default constructor for all the builders we have introduced, such as program and library. You can control the settings of the default constructor environment and use the defaultenvironment function: defaultenvironment (Cc = '/usr/local/bin/GCC, all calls to program or object will use/usr/local/bin/GCC to compile the target file. Note that defaultenvironment returns the initialized default constructor environment object, which can be operated like other constructor environments. So the following code is equivalent to the above example: ENV = defaultenvironment () env ['cc'] = '/usr/local/bin/GCC' the common function of defaultenvironment is to accelerate scons initialization. To make most of the default configurations work, scons searches for compilers and other tools installed on the local system. This search process takes time. If you know which compiler or tool you want to configure, you can control the search process executed by scons to initialize the default Build Environment by specifying some specific tool modules: ENV = defaultenvironment (tools = ['gcc ', 'gnulink'], Cc = '/usr/local/bin/GCC ') the above example tells scons to show that the default environment configuration is set using the GNU Compiler and the GNU linker, And the/usr/local/bin/GCC compiler is used.
7.2.6. The true advantage of constructing environments with multiple constructor environments is that you can create many different constructor environments you need, each Build Environment corresponds to a different way to compile a part of the software or other files. For example, if we need to compile a program with-O2 and compile another program with-G, we can do the following: OPT = environment (ccflags = '-O2 ') dbg = environment (ccflags = '-G') OPT. program ('foo', 'foo. c') dbg. program ('bar', 'bar. C ')
% Scons-q cc-o Bar. o-C-g bar. c CC-o Bar. o cc-O Foo. o-C-O2 Foo. c CC-O Foo. o we can even use multiple constructor environments to compile multiple versions of a program: OPT = environment (ccflags = '-O2') dbg = environment (ccflags = '-G ') OPT. program ('foo', 'foo. c') dbg. program ('foo', 'foo. C ') at this time, scons will have an error: % scons-Q

Scons: *** two environments with different actions were specified for the same target: Foo. o
File "/home/My/project/sconstruct", line 6, in <module>

This is because both program calls implicitly tell scons to generate a target file named Foo. O. Scons cannot determine their priority, so an error is reported. To solve this problem, we should specify that each environment will. C compiled into the target file of different names: 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)
7.2.7. Copy the constructor environment: Sometimes you want to share the same value for one or more variables in more than one constructor environment. When creating a constructor environment, instead of repeatedly setting all the shared variables, you can use the clone method to create a copy of the constructor environment. Environment creates a constructor. The clone method creates a constructor by assigning values to the constructor variables and reloads the values of the constructor environments. For example, suppose we want to use GCC to create three versions of a program, one optimized version, one debug version, and one other version. We can create a basic construction environment to set $ CC to GCC, and then create two copies: 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)
7.2.8. replace value: replace method you can use the replace method to replace an existing constructor: ENV = environment (ccflags = '-ddefine1'); ENV. replace (ccflags = '-ddefine2'); ENV. program ('foo. C ') You can safely call the replace method for non-existent constructors: ENV = environment () ENV. replace (new_variable = 'xyable') print "new_variable =", ENV ['new _ variable'] In this example, the constructor variable is added to the constructor environment: % scons-Q new_variable = xyw.scons :'. is up to date. variables are not extended. When the constructor environment is used to compile the target file, the calls of the scons function and method are unordered. The final replacement may be used to compile all target files, regardless of whether the replace method is called before and after the compilation method: ENV = environment (ccflags = '-ddefine1') print "ccflags =", ENV ['ccflags '] Env. program ("foo. C ") ENV. replace (ccflags = '-ddefin2') print "ccflags =", ENV ['ccflags '] Env. program ("bar. C ") % scons

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. o
CC-O Foo. O-C-ddefine2 Foo. c
CC-O Foo. o
Scons: done building targets.

Because replacement occurs when the sconscript file is read, the $ ccflags variable has been set to-ddefine2 during Foo. O compilation, even if the relapce method is called after the sconscript file.
7.2.9. Set the value when no value is defined: setdefault method sometimes a constructor variable should be set to a value only when the constructor does not define this variable. You can use the setdefault method, which is similar to the set_default method in the python dictionary: Env. setdefault (special_flag = '-extra-option') is useful when you write your own tool module to apply variables to the build environment.
7.2.10. append to the end of a value: append method you can append a value to an existing constructor. Use the append method: ENV = environment (ccflags = ['-dmy_value']). env. append (ccflags = ['-dlast']) ENV. program ('foo. C ') When scons compiles the target file, the-dmy_value and-dlast two flags will be applied: % scons-q cc-O Foo. o-C-dmy_value-dlast Foo. c CC-O Foo. O if the constructor variable does not exist, the append method will create it.
7.2.11 append a unique value: the appendunique method adds a new value only when an existing constructor does not contain a value. You can use the appendunique method: ENV. appendunique (ccflags = ['-G']). In the preceding example,-G is added only when $ ccflags does not contain-G.
7.2.12. append a value at the starting position of the value: For an existing constructor variable, you can use the prepend method to append a value to the starting position of its value. ENV = environment (ccflags = ['-dmy_value']) ENV. prepend (ccflags = ['-dfirst']) ENV. Program ('foo. C ')
% Scons-q cc-O Foo. O-C-dfirst-dmy_value Foo. c CC-O Foo. O if the constructor variable does not exist, the prepend method will create it.
7.2.13 append a unique value to the front: The prependunique method is appended to the front only when the value of a constructor does not include the value to be added, you can use the prependunique method; ENV. prependunique (ccflags = ['-G'])
7.3. control command execution environment when scons compiles a target file, it will not use the same external environment you use to execute scons to execute some commands. It uses $ ENV to construct a variable to execute commands as an external environment. The most important embodiment of this behavior is the path environment variable, which determines where the operating system will look for commands and tools, different from the external environment you use to call scons. This means that scons will not be able to find all the tools you execute in the command line. The default value of the PATH environment variable is/usr/local/bin:/usr/bin. If you want to execute any commands that are not in these default locations, you need to set the path in the $ ENV dictionary in your constructor environment. The simplest way is to initialize these values when creating the construction environment: Path = ['/usr/local/bin ', '/usr/bin'] Env = environment (ENV = {'path': path}) in this way, assign a dictionary value to $ ENV constructor to completely reset the external environment, therefore, when an external command is executed, the set variable is only the value of path. If you want to use the remaining values in $ ENV, just set the path value, you can do this: env ['env'] ['path'] = ['/usr/local/bin','/bin ', '/usr/bin'] note that scons allows you to use a string to define the directory in the path. The path is separated by the path separator: env ['env'] ['path'] = '/usr/local/bin:/usr/bin'
7.3.1 obtain the PATH value from the external environment. You may want to obtain the external path as the command execution environment. You can use. environ's path value to initialize the PATH variable: Import OS Env = environment (ENV = {'path': OS. environ ['path']}) You can set the entire external environment: Import OS Env = environment (ENV = OS. environ)
7.3.2 a common requirement for adding the PATH value in the execution environment is to add one or more custom directories to the PATH variable. ENV = environment (ENV = OS. environ) ENV. prependenvpath ('path', '/usr/local/bin') ENV. appendenvpath ('lib', '/usr/local/lib ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.