# Merging Options into the Environment: the &MergeFlags; Function

&SCons; &consenvs; have a &f-link-MergeFlags; method that merges values from a passed-in argument into the &consenv;. If the argument is a dictionary, &MergeFlags; treats each value in the dictionary as a list of options you would pass to a command (such as a compiler or linker). &MergeFlags; will not duplicate an option if it already exists in the &consvar;. If the argument is a string, &MergeFlags; calls the &f-link-ParseFlags; method to burst it out into a dictionary first, then acts on the result.

&MergeFlags; tries to be intelligent about merging options, knowing that different &consvars; may have different needs. When merging options to any variable whose name ends in `PATH`, &MergeFlags; keeps the leftmost occurrence of the option, because in typical lists of directory paths, the first occurrence "wins." When merging options to any other variable name, &MergeFlags; keeps the rightmost occurrence of the option, because in a list of typical command-line options, the last occurrence "wins."

env = Environment() env.Append(CCFLAGS='-option -O3 -O1') flags = {'CCFLAGS': '-whatever -O3'} env.MergeFlags(flags) print("CCFLAGS:", env['CCFLAGS'])
scons -Q

Note that the default value for &cv-link-CCFLAGS; is an internal &SCons; object which automatically converts the options you specify as a string into a list.

env = Environment() env.Append(CPPPATH=['/include', '/usr/local/include', '/usr/include']) flags = {'CPPPATH': ['/usr/opt/include', '/usr/local/include']} env.MergeFlags(flags) print("CPPPATH:", env['CPPPATH'])
scons -Q

Note that the default value for &cv-link-CPPPATH; is a normal &Python; list, so you should give its values as a list in the dictionary you pass to the &MergeFlags; function.

If &MergeFlags; is passed anything other than a dictionary, it calls the &ParseFlags; method 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("CCFLAGS:", env['CCFLAGS']) print("CPPPATH:", env['CPPPATH'])
scons -Q

In the combined example above, &ParseFlags; has sorted the options into their corresponding variables and returned a dictionary for &MergeFlags; to apply to the &consvars; in the specified &consenv;.