Accumulated documentation changes.
[scons.git] / src / engine / SCons / Tool / yacc.py
index 0c80cf83910bf5a46d3b1b9dfbbf5059a73c8c5d..da88aa223d8841e6648cdb4886d1db2236d2d07b 100644 (file)
@@ -33,18 +33,45 @@ selection method.
 
 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
+import os.path
+
+import SCons.Defaults
 import SCons.Tool
+import SCons.Util
+
+YaccAction = SCons.Action.Action("$YACCCOM", "$YACCCOMSTR")
+
+def _yaccEmitter(target, source, env, ysuf, hsuf):
+    # If -d is specified on the command line, yacc will emit a .h
+    # or .hpp file as well as a .c or .cpp file, depending on whether
+    # the input file is a .y or .yy, respectively.
+    if len(source) and '-d' in SCons.Util.CLVar(env.subst("$YACCFLAGS")):
+        base, ext = os.path.splitext(SCons.Util.to_String(source[0]))
+        if ext == ysuf:
+            base, ext = os.path.splitext(SCons.Util.to_String(target[0]))
+            target.append(base + hsuf)
+    return (target, source)
+
+def yEmitter(target, source, env):
+    return _yaccEmitter(target, source, env, '.y', '.h')
+
+def yyEmitter(target, source, env):
+    return _yaccEmitter(target, source, env, '.yy', '.hpp')
 
 def generate(env):
     """Add Builders and construction variables for yacc to an Environment."""
     c_file, cxx_file = SCons.Tool.createCFileBuilders(env)
     
-    c_file.add_action('.y', '$YACCCOM')
-    cxx_file.add_action('.yy', '$YACCCOM')
+    c_file.add_action('.y', YaccAction)
+    c_file.add_action('.yacc', YaccAction)
+    cxx_file.add_action('.yy', YaccAction)
+    c_file.add_emitter('.y', yEmitter)
+    c_file.add_emitter('.yacc', yEmitter)
+    cxx_file.add_emitter('.yy', yyEmitter)
 
-    env['YACC']      = 'yacc'
-    env['YACCFLAGS'] = ''
+    env['YACC']      = env.Detect('bison') or 'yacc'
+    env['YACCFLAGS'] = SCons.Util.CLVar('')
     env['YACCCOM']   = '$YACC $YACCFLAGS -o $TARGET $SOURCES'
 
 def exists(env):
-    return env.Detect('yacc')
+    return env.Detect(['bison', 'yacc'])