1. Merge the options into the environment: mergeflags Function
The construction environment of scons has a mergeflags method, which combines the dictionary of a value into the construction environment. Mergeflags treats each value in the dictionary as an option list. If an option already exists in the construction environment variable, mergeflags will not set this option again.
When merging options to any variable named in the path, mergeflags keeps the options at the leftmost end. This should be in the directory path list, where the first appearance is dominant. When merging options to any other variable names, mergeflags keeps the options on the right side, because the final display in the command line option list is dominant.
ENV = environment ()
Env. append (ccflags = '-Option-O3-O1 ')
Flags = {'ccflags ':'-whatever-O3 '}
Env. mergeflags (flags)
Print env ['ccflags ']
% Scons-Q
['-Option','-O1 ','-whatever ','-O3 ']
Scons: '.' is up to date.
In the above example, the default value of $ ccflags is an internal scons object, which will automatically convert the specified option into a string and add it to the list.
ENV = environment ()
Env. append (cpppath = ['/include', '/usr/local/include', '/usr/include'])
Flags = {'cppath': ['/usr/opt/include', '/usr/local/include']}
Env. mergeflags (flags)
Print env ['cppath']
% Scons-Q
['/Include', '/usr/local/include', '/usr/include', '/usr/opt/include']
Scons: '.' is up to date.
In the above example, the default value of $ cpppath is a python list, so we must specify the value as a list in the dictionary passed to the mergeflags function.
If it is not to pass a dictionary but other things, the parseflags method will be called to convert it into a dictionary:
ENV = environment ()
Env. append (ccflags = '-Option-O3-O1 ')
Env. append (cpppath = ['/include', '/usr/local/include', '/usr/include'])
Env. mergeflags ('-whatever-I/usr/opt/include-O3-I/usr/local/include ')
Print env ['ccflags ']
Print env ['cppath']
% Scons-Q
['-Option','-O1 ','-whatever ','-O3 ']
['/Include', '/usr/local/include', '/usr/include', '/usr/opt/include']
Scons: '.' is up to date.
In the preceding example, the parseflags method has assigned the option sorting value to the corresponding variable, and then returns a dictionary to the mergeflags method.
2. Separate compilation parameters into variables: parseflags Function
During program compilation, scons has a confusing array of construction variables for different types of options. For a special option, you may not know which variable to use.
The scons construction environment has a parseflags method that accepts a set of command line options and then distributes them to the appropriate construction variables.
Parseflags returns a dictionary containing the construction variable and value. Normally, this dictionary will be passed to the mergeflags method and the options will be merged into the construction environment. However, if you want to provide additional functions, this dictionary can be edited.
ENV = environment ()
D = env. parseflags ("-I/opt/include-L/opt/lib-lfoo ")
For K, V in sorted (D. Items ()):
If V:
Print K, V
Env. mergeflags (d)
Env. Program ('f1. C ')
% Scons-Q
Cpppath ['/opt/include']
Libpath ['/opt/lib']
Libs ['foo']
CC-O f1.o-c-I/opt/include f1.c
CC-O F1 f1.o-L/opt/lib-lfoo
If flags is used for GCC, unacknowledged flags will be placed in $ ccflags for compiling C and C ++:
ENV = environment ()
D = env. parseflags ("-whatever ")
For K, V in sorted (D. Items ()):
If V:
Print K, V
Env. mergeflags (d)
Env. Program ('f1. C ')
% Scons-Q
Ccflags-whatever
CC-O f1.o-C-whatever f1.c
CC-O F1 f1.o
Parseflags also accepts a string list as input:
ENV = environment ()
D = env. parseflags (["-I/opt/include", ["-l/opt/lib", "-lfoo"])
For K, V in sorted (D. Items ()):
If V:
Print K, V
Env. mergeflags (d)
Env. Program ('f1. C ')
% Scons-Q
Cpppath ['/opt/include']
Libpath ['/opt/lib']
Libs ['foo']
CC-O f1.o-c-I/opt/include f1.c
CC-O F1 f1.o-L/opt/lib-lfoo
If a string starts! The string is passed to shell for execution. Command output is parsed:
ENV = environment ()
D = env. parseflags (["! Echo-I/opt/include ","! Echo-L/opt/lib ","-lfoo "])
For K, V in sorted (D. Items ()):
If V:
Print K, V
Env. mergeflags (d)
Env. Program ('f1. C ')
% Scons-Q
Cpppath ['/opt/include']
Libpath ['/opt/lib']
Libs ['foo']
CC-O f1.o-c-I/opt/include f1.c
CC-O F1 f1.o-L/opt/lib-lfoo
Parseflags regularly updates new options.
3. Find the installed Library Information: parseconfig Function
It is very complicated to configure the correct options to compile programs that reference other libraries, especially shared libraries. To help solve this problem, many tools whose names end with config return the command line options that need to use these libraries; for example, the command line option of a library named lib will be found by a tool called Lib-config.
A recent convention is that these options are available through the generic PKG-config tool, which has a general framework for error handling, therefore, all the package creators need to provide a string set for their special packages.
The scons construction environment has a parseconfig method, which executes * config and configures the appropriate construction Variable Based on the command line options returned by a specific command.
ENV = environment ()
Env ['cppath'] = ['/lib/compat']
Env. parseconfig ("PKG-config X11 -- cflags -- Libs ")
Print env ['cppath']
Scons will execute a specific command string, parse the result flags, and then add the flags to the appropriate environment variable:
% Scons-Q
['/Lib/compat','/usr/X11/include ']
Scons: '.' is up to date.
In the above example, scons adds the include directory to cpppath.
Note that the mergeflags method is used to merge options with existing options. Each option appears only once in the Construction variable:
ENV = environment ()
Env. parseconfig ("PKG-config X11 -- cflags -- Libs ")
Env. parseconfig ("PKG-config X11 -- cflags -- Libs ")
Print env ['cppath']
% Scons-Q
['/Usr/X11/include']
Scons: '.' is up to date.