You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.5 KiB

6 years ago
  1. """SCons.Tool.yacc
  2. Tool-specific initialization for yacc.
  3. There normally shouldn't be any need to import this module directly.
  4. It will usually be imported through the generic SCons.Tool.Tool()
  5. selection method.
  6. """
  7. #
  8. # Copyright (c) 2001 - 2017 The SCons Foundation
  9. #
  10. # Permission is hereby granted, free of charge, to any person obtaining
  11. # a copy of this software and associated documentation files (the
  12. # "Software"), to deal in the Software without restriction, including
  13. # without limitation the rights to use, copy, modify, merge, publish,
  14. # distribute, sublicense, and/or sell copies of the Software, and to
  15. # permit persons to whom the Software is furnished to do so, subject to
  16. # the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included
  19. # in all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  22. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  23. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. #
  29. __revision__ = "src/engine/SCons/Tool/yacc.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  30. import os.path
  31. import SCons.Defaults
  32. import SCons.Tool
  33. import SCons.Util
  34. YaccAction = SCons.Action.Action("$YACCCOM", "$YACCCOMSTR")
  35. def _yaccEmitter(target, source, env, ysuf, hsuf):
  36. yaccflags = env.subst("$YACCFLAGS", target=target, source=source)
  37. flags = SCons.Util.CLVar(yaccflags)
  38. targetBase, targetExt = os.path.splitext(SCons.Util.to_String(target[0]))
  39. if '.ym' in ysuf: # If using Objective-C
  40. target = [targetBase + ".m"] # the extension is ".m".
  41. # If -d is specified on the command line, yacc will emit a .h
  42. # or .hpp file with the same name as the .c or .cpp output file.
  43. if '-d' in flags:
  44. target.append(targetBase + env.subst(hsuf, target=target, source=source))
  45. # If -g is specified on the command line, yacc will emit a .vcg
  46. # file with the same base name as the .y, .yacc, .ym or .yy file.
  47. if "-g" in flags:
  48. base, ext = os.path.splitext(SCons.Util.to_String(source[0]))
  49. target.append(base + env.subst("$YACCVCGFILESUFFIX"))
  50. # If -v is specified yacc will create the output debug file
  51. # which is not really source for any process, but should
  52. # be noted and also be cleaned
  53. # Bug #2558
  54. if "-v" in flags:
  55. env.SideEffect(targetBase+'.output',target[0])
  56. env.Clean(target[0],targetBase+'.output')
  57. # With --defines and --graph, the name of the file is totally defined
  58. # in the options.
  59. fileGenOptions = ["--defines=", "--graph="]
  60. for option in flags:
  61. for fileGenOption in fileGenOptions:
  62. l = len(fileGenOption)
  63. if option[:l] == fileGenOption:
  64. # A file generating option is present, so add the file
  65. # name to the list of targets.
  66. fileName = option[l:].strip()
  67. target.append(fileName)
  68. return (target, source)
  69. def yEmitter(target, source, env):
  70. return _yaccEmitter(target, source, env, ['.y', '.yacc'], '$YACCHFILESUFFIX')
  71. def ymEmitter(target, source, env):
  72. return _yaccEmitter(target, source, env, ['.ym'], '$YACCHFILESUFFIX')
  73. def yyEmitter(target, source, env):
  74. return _yaccEmitter(target, source, env, ['.yy'], '$YACCHXXFILESUFFIX')
  75. def generate(env):
  76. """Add Builders and construction variables for yacc to an Environment."""
  77. c_file, cxx_file = SCons.Tool.createCFileBuilders(env)
  78. # C
  79. c_file.add_action('.y', YaccAction)
  80. c_file.add_emitter('.y', yEmitter)
  81. c_file.add_action('.yacc', YaccAction)
  82. c_file.add_emitter('.yacc', yEmitter)
  83. # Objective-C
  84. c_file.add_action('.ym', YaccAction)
  85. c_file.add_emitter('.ym', ymEmitter)
  86. # C++
  87. cxx_file.add_action('.yy', YaccAction)
  88. cxx_file.add_emitter('.yy', yyEmitter)
  89. env['YACC'] = env.Detect('bison') or 'yacc'
  90. env['YACCFLAGS'] = SCons.Util.CLVar('')
  91. env['YACCCOM'] = '$YACC $YACCFLAGS -o $TARGET $SOURCES'
  92. env['YACCHFILESUFFIX'] = '.h'
  93. env['YACCHXXFILESUFFIX'] = '.hpp'
  94. env['YACCVCGFILESUFFIX'] = '.vcg'
  95. def exists(env):
  96. return env.Detect(['bison', 'yacc'])
  97. # Local Variables:
  98. # tab-width:4
  99. # indent-tabs-mode:nil
  100. # End:
  101. # vim: set expandtab tabstop=4 shiftwidth=4: