Anonymous avatar Anonymous committed f057439

Add support for SCONSFLAGS.

Comments (0)

Files changed (4)

doc/man/main.sgml

 
     <!ENTITY desc SYSTEM "desc.sgml">
     <!ENTITY options SYSTEM "options.sgml">
+    <!ENTITY variables SYSTEM "variables.sgml">
 ]>
 
 <refentry id="scons">
   </refsect1>
 
   <refsect1>
+    <title>Environment Variables</title>
+    &variables;
+  </refsect1>
+
+  <refsect1>
     <title>See Also</title>
     <para>
     </para>

doc/man/variables.sgml

+<!--
+
+  Copyright 2001 Steven Knight
+
+-- >
+
+<para>
+
+  &scons; supports the following environment variables to affect
+  its behavior:
+
+</para>
+
+<variablelist>
+
+  <varlistentry>
+    <term>
+      &SCONSFLAGS;
+    </term>
+    <listitem>
+      <para>
+
+        &SCons; reads and interprets command-line options from this
+        environment variable before interpreting the options actually
+        supplied on the command line.  Where there's a conflict, options
+        from the command line overrule options from the &SCONSFLAGS;
+        environment variable.  Unknown options and other errors in the
+        &SCONSFLAGS; variable are ignored with a warning, but do not
+        halt &SCons;.
+
+      </para>
+    </listitem>
+  </varlistentry>
+
+</variablelist>

src/script/scons.py

 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 import getopt
+import os
 import os.path
 import string
 import sys
 # Global variables
 
 default_targets = []
-local_help = None
+include_dirs = []
+help_option = None
 num_jobs = 1
-Scripts = []
-include_dirs = []
+scripts = []
 
 # utility functions
 
     sys.stderr.write("\nSCons error: %s\n" % value)
     sys.stderr.write('File "%s", line %d, in %s\n' % (filename, lineno, routine))
 
+def _scons_user_warning(e):
+    """Handle user warnings. Print out a message and a description of
+    the warning, along with the line number and routine where it occured.
+    """
+    etype, value, tb = sys.exc_info()
+    while tb.tb_next is not None:
+        tb = tb.tb_next
+    lineno = traceback.tb_lineno(tb)
+    filename = tb.tb_frame.f_code.co_filename
+    routine = tb.tb_frame.f_code.co_name
+    sys.stderr.write("\nSCons warning: %s\n" % e)
+    sys.stderr.write('File "%s", line %d, in %s\n' % (filename, lineno, routine))
+
 def _scons_other_errors():
     """Handle all errors but user errors. Print out a message telling
     the user what to do in this case and print a normal trace.
 
 
 def Conscript(filename):
-    global Scripts
-    Scripts.append(filename)
+    global scripts
+    scripts.append(filename)
 
 def Default(*targets):
     for t in targets:
 	    default_targets.append(s)
 
 def Help(text):
-    global local_help
-    if local_help:
+    global help_option
+    if help_option == 'h':
 	print text
 	print "Use scons -H for help about command-line options."
 	sys.exit(0)
 	help = "Environment variables override makefiles.")
 
     def opt_f(opt, arg):
-	global Scripts
-	Scripts.append(arg)
+	global scripts
+	scripts.append(arg)
 
     Option(func = opt_f,
 	short = 'f', long = ['file', 'makefile', 'sconstruct'], arg = 'FILE',
 	help = "Read FILE as the top-level SConstruct file.")
 
     def opt_help(opt, arg):
-	global local_help
-	local_help = 1
+	global help_option
+	help_option = 'h'
 
     Option(func = opt_help,
 	short = 'h', long = ['help'],
 	help = "Print defined help message, or this one.")
 
     def opt_help_options(opt, arg):
-	PrintUsage()
-	sys.exit(0)
+	global help_option
+	help_option = 'H'
 
     Option(func = opt_help_options,
 	short = 'H', long = ['help-options'],
 	try:
             num_jobs = int(arg)
 	except:
-            PrintUsage()
+            print UsageString()
             sys.exit(1)
 
 	if num_jobs <= 0:
-            PrintUsage()
+            print UsageString()
             sys.exit(1)
 
     Option(func = opt_j,
 
 
 
-def PrintUsage():
-    print "Usage: scons [OPTION] [TARGET] ..."
-    print "Options:"
-    for o in option_list:
-	if o.helpline:
-	    print o.helpline
+def UsageString():
+    help_opts = filter(lambda x: x.helpline, option_list)
+    s = "Usage: scons [OPTION] [TARGET] ...\n" + "Options:\n" + \
+	string.join(map(lambda x: x.helpline, help_opts), "\n") + "\n"
+    return s
 
 
 
 def main():
-    global Scripts, local_help, num_jobs
+    global scripts, help_option, num_jobs
+
+    # It looks like 2.0 changed the name of the exception class
+    # raised by getopt.
+    try:
+	getopt_err = getopt.GetoptError
+    except:
+	getopt_err = getopt.error
 
     try:
-	cmd_opts, targets = getopt.getopt(sys.argv[1:], short_opts, long_opts)
-#   except getopt.GetoptError, x:
-    except:
-        #print x
-        PrintUsage()
-        sys.exit(1)
+	cmd_opts, t = getopt.getopt(string.split(os.environ['SCONSFLAGS']),
+					  short_opts, long_opts)
+    except KeyError:
+	# It's all right if there's no SCONSFLAGS environment variable.
+	pass
+    except getopt_err, x:
+	_scons_user_warning("SCONSFLAGS " + x)
+    else:
+	for opt, arg in cmd_opts:
+	    opt_func[opt](opt, arg)
+
+    cmd_opts, targets = getopt.getopt(sys.argv[1:], short_opts, long_opts)
 
     for opt, arg in cmd_opts:
 	opt_func[opt](opt, arg)
 
-    if not Scripts:
+    if not scripts:
         for file in ['SConstruct', 'Sconstruct', 'sconstruct']:
             if os.path.isfile(file):
-                Scripts.append(file)
+                scripts.append(file)
                 break
 
-    if not Scripts:
-	if local_help:
-	    # There's no SConstruct, but they specified -h.  Give them
-	    # the options usage before we try to read it and fail.
-	    PrintUsage()
+    if help_option == 'H':
+	print UsageString()
+	sys.exit(0)
+
+    if not scripts:
+	if help_option == 'h':
+	    # There's no SConstruct, but they specified either -h or
+	    # -H.  Give them the options usage now, before we fail
+	    # trying to read a non-existent SConstruct file.
+	    print UsageString()
 	    sys.exit(0)
 	else:
 	    raise UserError, "No SConstruct file found."
 
     sys.path = include_dirs + sys.path
 
-    while Scripts:
-        file, Scripts = Scripts[0], Scripts[1:]
+    while scripts:
+        file, scripts = scripts[0], scripts[1:]
 	if file == "-":
 	    exec sys.stdin in globals()
 	else:
 	    else:
 		exec f in globals()
 
-    if local_help:
+    if help_option == 'h':
 	# They specified -h, but there was no Help() inside the
 	# SConscript files.  Give them the options usage.
-	PrintUsage()
+	print UsageString()
 	sys.exit(0)
 
     if not targets:

test/SCONSFLAGS.py

+#!/usr/bin/env python
+
+__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+
+import os
+import string
+import TestSCons
+
+test = TestSCons.TestSCons()
+
+wpath = test.workpath()
+
+test.write('SConstruct', r"""
+Help("Help text.\n")
+""")
+
+expect = "Help text.\n\nUse scons -H for help about command-line options.\n"
+
+os.environ['SCONSFLAGS'] = ''
+
+test.run(arguments = '-h', stdout = expect)
+
+os.environ['SCONSFLAGS'] = '-h'
+
+test.run(stdout = expect)
+
+test.run(arguments = "-H")
+
+test.fail_test(string.find(test.stdout(), 'Help text.') >= 0)
+test.fail_test(string.find(test.stdout(), '-H, --help-options') == -1)
+
+os.environ['SCONSFLAGS'] = '-Z'
+
+test.run(arguments = "-H", stderr = r"""
+SCons warning: SCONSFLAGS option -Z not recognized
+File "[^"]*", line \d+, in \S+
+""")
+
+test.fail_test(string.find(test.stdout(), '-H, --help-options') == -1)
+
+test.pass_test()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.