Add the ParseConfig() method. (Steve Leblanc)
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Tue, 12 Nov 2002 13:43:54 +0000 (13:43 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Tue, 12 Nov 2002 13:43:54 +0000 (13:43 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@495 fdb21ef1-2011-0410-befe-b5e4ea1792b1

doc/man/scons.1
src/CHANGES.txt
src/engine/SCons/Script/SConscript.py
src/engine/SCons/Util.py
test/ParseConfig.py [new file with mode: 0644]

index 3d7d0341eb5e08e7adbe728e0318159954f09362..928c361258dfbbfc10b771f07203f7bbfd57bbce 100644 (file)
@@ -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
index a313b09ca35a8387ef26b75122a02753fddaa80a..3d8a6b918448671a2c7cba1591e5e94cd5bd5c78 100644 (file)
@@ -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.
index 2802e8fd89ec4296745f92bdc621002dd01ebe81..3d4a6f7caaeb4a0991b4890e5bc5b524e4ebb205 100644 (file)
@@ -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
index 3a39c4ecafb945bf6e152d5d0f2a8492bdd11661..5c2fd26f13213bf26b5af339f845ed8a7d5873ce 100644 (file)
@@ -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 (file)
index 0000000..1cf52ed
--- /dev/null
@@ -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()