From: stevenknight Date: Tue, 12 Nov 2002 13:43:54 +0000 (+0000) Subject: Add the ParseConfig() method. (Steve Leblanc) X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=cf55158a08568a5f8323a10c80530dc2a2122767;p=scons.git Add the ParseConfig() method. (Steve Leblanc) git-svn-id: http://scons.tigris.org/svn/scons/trunk@495 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- diff --git a/doc/man/scons.1 b/doc/man/scons.1 index 3d7d0341..928c3612 100644 --- a/doc/man/scons.1 +++ b/doc/man/scons.1 @@ -2344,6 +2344,34 @@ Import("env") Import("env", "variable") .EE +.TP +.RI ParseConfig( env , command ", [" function ]) +Calls the specified +.I function +to modify the specified environment +.I env +as specified by the output of +.I command . +The default +.I function +expects the output of a typical +.I *-config command +(for example, +.BR gtk-config ) +and parses the returned +.BR -L , +.BR -l , +.B -I +and other options +into the +.BR LIBPATH , +.BR LIBS , +.B CPPPATH +and +.B CCFLAGS +variables, +respectively. + .TP .RI Platform( string ) Returns a callable object diff --git a/src/CHANGES.txt b/src/CHANGES.txt index a313b09c..3d8a6b91 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -55,6 +55,9 @@ RELEASE 0.09 - - Refactor option processing to use our own version of Greg Ward's Optik module, modified to run under Python 1.5.2. + - Add a ParseConfig() command to modify an environment based on + parsing output from a *-config command. + From Jeff Petkau: - Fix interpretation of '#/../foo' on Win32 systems. diff --git a/src/engine/SCons/Script/SConscript.py b/src/engine/SCons/Script/SConscript.py index 2802e8fd..3d4a6f7c 100644 --- a/src/engine/SCons/Script/SConscript.py +++ b/src/engine/SCons/Script/SConscript.py @@ -331,6 +331,7 @@ def BuildDefaultGlobals(): globals['Library'] = SCons.Defaults.StaticLibrary globals['Local'] = Local globals['Object'] = SCons.Defaults.StaticObject + globals['Options'] = Options globals['Repository'] = SCons.Node.FS.default_fs.Repository globals['SetBuildSignatureType'] = SetBuildSignatureType globals['SetContentSignatureType'] = SetContentSignatureType @@ -338,6 +339,7 @@ def BuildDefaultGlobals(): globals['StaticObject'] = SCons.Defaults.StaticObject globals['SharedLibrary'] = SCons.Defaults.SharedLibrary globals['SharedObject'] = SCons.Defaults.SharedObject + globals['ParseConfig'] = SCons.Util.ParseConfig globals['Platform'] = SCons.Platform.Platform globals['Program'] = SCons.Defaults.Program globals['Return'] = Return @@ -348,5 +350,4 @@ def BuildDefaultGlobals(): globals['Split'] = SCons.Util.Split globals['Tool'] = SCons.Tool.Tool globals['WhereIs'] = SCons.Util.WhereIs - globals['Options'] = Options return globals diff --git a/src/engine/SCons/Util.py b/src/engine/SCons/Util.py index 3a39c4ec..5c2fd26f 100644 --- a/src/engine/SCons/Util.py +++ b/src/engine/SCons/Util.py @@ -523,3 +523,50 @@ else: if stat.S_IMODE(st[stat.ST_MODE]) & 0111: return f return None + +def ParseConfig(env, command, function=None): + """Use the specified function to parse the output of the command in order + to modify the specified environment. The 'command' can be a string or a + list of strings representing a command and it's arguments. 'Function' is + an optional argument that takes the environment and the output of the + command. If no function is specified, the output will be treated as the + output of a typical 'X-config' command (i.e. gtk-config) and used to set + the CPPPATH, LIBPATH, LIBS, and CCFLAGS variables. + """ + # the default parse function + def parse_conf(env, output): + env_dict = env.Dictionary() + static_libs = [] + + # setup all the dictionary options + if not env_dict.has_key('CPPPATH'): + env_dict['CPPPATH'] = [] + if not env_dict.has_key('LIBPATH'): + env_dict['LIBPATH'] = [] + if not env_dict.has_key('LIBS'): + env_dict['LIBS'] = [] + if not env_dict.has_key('CCFLAGS') or env_dict['CCFLAGS'] == "": + env_dict['CCFLAGS'] = [] + + params = string.split(output) + for arg in params: + switch = arg[0:1] + opt = arg[1:2] + if switch == '-': + if opt == 'L': + env_dict['LIBPATH'].append(arg[2:]) + elif opt == 'l': + env_dict['LIBS'].append(arg[2:]) + elif opt == 'I': + env_dict['CPPPATH'].append(arg[2:]) + else: + env_dict['CCFLAGS'].append(arg) + else: + static_libs.append(arg) + return static_libs + + if function is None: + function = parse_conf + if type(command) is type([]): + command = string.join(command) + return function(env, os.popen(command).read()) diff --git a/test/ParseConfig.py b/test/ParseConfig.py new file mode 100644 index 00000000..1cf52ed8 --- /dev/null +++ b/test/ParseConfig.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# +# Copyright (c) 2001, 2002 Steven Knight +# +# 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. +# + +__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" + +import os +import sys + +import TestSCons + +test = TestSCons.TestSCons() + +test_config = test.workpath('test-config') + +test.write('test-config', """#! /usr/bin/env python +print "-I/usr/include/fum -Ibar -X" +print "-L/usr/fax -Lfoo -lxxx abc" +""") + +test.write('SConstruct', """ +env = Environment() +static_libs = ParseConfig(env, ["%s", "%s", "--libs --cflags"]) +print env['CPPPATH'] +print env['LIBPATH'] +print env['LIBS'] +print env['CCFLAGS'] +print static_libs +""" % (sys.executable, test_config)) + +test.write('SConstruct2', """ +env = Environment() +static_libs = ParseConfig(env, "%s %s --libs --cflags") +print env['CPPPATH'] +print env['LIBPATH'] +print env['LIBS'] +print env['CCFLAGS'] +print static_libs +""" % (sys.executable, test_config)) + +good_stdout = test.wrap_stdout(read_str = """\ +['/usr/include/fum', 'bar'] +['/usr/fax', 'foo'] +['xxx'] +['-X'] +['abc'] +""", build_str = 'scons: "." is up to date.\n') + +test.run(arguments = ".", stdout = good_stdout) + +test.run(arguments = "-f SConstruct2 .", stdout = good_stdout) + +test.pass_test()