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.

232 lines
8.4 KiB

6 years ago
  1. """SCons.Tool.javac
  2. Tool-specific initialization for javac.
  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. __revision__ = "src/engine/SCons/Tool/javac.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  29. import os
  30. import os.path
  31. import SCons.Action
  32. import SCons.Builder
  33. from SCons.Node.FS import _my_normcase
  34. from SCons.Tool.JavaCommon import parse_java_file
  35. import SCons.Util
  36. def classname(path):
  37. """Turn a string (path name) into a Java class name."""
  38. return os.path.normpath(path).replace(os.sep, '.')
  39. def emit_java_classes(target, source, env):
  40. """Create and return lists of source java files
  41. and their corresponding target class files.
  42. """
  43. java_suffix = env.get('JAVASUFFIX', '.java')
  44. class_suffix = env.get('JAVACLASSSUFFIX', '.class')
  45. target[0].must_be_same(SCons.Node.FS.Dir)
  46. classdir = target[0]
  47. s = source[0].rentry().disambiguate()
  48. if isinstance(s, SCons.Node.FS.File):
  49. sourcedir = s.dir.rdir()
  50. elif isinstance(s, SCons.Node.FS.Dir):
  51. sourcedir = s.rdir()
  52. else:
  53. raise SCons.Errors.UserError("Java source must be File or Dir, not '%s'" % s.__class__)
  54. slist = []
  55. js = _my_normcase(java_suffix)
  56. for entry in source:
  57. entry = entry.rentry().disambiguate()
  58. if isinstance(entry, SCons.Node.FS.File):
  59. slist.append(entry)
  60. elif isinstance(entry, SCons.Node.FS.Dir):
  61. result = SCons.Util.OrderedDict()
  62. dirnode = entry.rdir()
  63. def find_java_files(arg, dirpath, filenames):
  64. java_files = sorted([n for n in filenames
  65. if _my_normcase(n).endswith(js)])
  66. mydir = dirnode.Dir(dirpath)
  67. java_paths = [mydir.File(f) for f in java_files]
  68. for jp in java_paths:
  69. arg[jp] = True
  70. for dirpath, dirnames, filenames in os.walk(dirnode.get_abspath()):
  71. find_java_files(result, dirpath, filenames)
  72. entry.walk(find_java_files, result)
  73. slist.extend(list(result.keys()))
  74. else:
  75. raise SCons.Errors.UserError("Java source must be File or Dir, not '%s'" % entry.__class__)
  76. version = env.get('JAVAVERSION', '1.4')
  77. full_tlist = []
  78. for f in slist:
  79. tlist = []
  80. source_file_based = True
  81. pkg_dir = None
  82. if not f.is_derived():
  83. pkg_dir, classes = parse_java_file(f.rfile().get_abspath(), version)
  84. if classes:
  85. source_file_based = False
  86. if pkg_dir:
  87. d = target[0].Dir(pkg_dir)
  88. p = pkg_dir + os.sep
  89. else:
  90. d = target[0]
  91. p = ''
  92. for c in classes:
  93. t = d.File(c + class_suffix)
  94. t.attributes.java_classdir = classdir
  95. t.attributes.java_sourcedir = sourcedir
  96. t.attributes.java_classname = classname(p + c)
  97. tlist.append(t)
  98. if source_file_based:
  99. base = f.name[:-len(java_suffix)]
  100. if pkg_dir:
  101. t = target[0].Dir(pkg_dir).File(base + class_suffix)
  102. else:
  103. t = target[0].File(base + class_suffix)
  104. t.attributes.java_classdir = classdir
  105. t.attributes.java_sourcedir = f.dir
  106. t.attributes.java_classname = classname(base)
  107. tlist.append(t)
  108. for t in tlist:
  109. t.set_specific_source([f])
  110. full_tlist.extend(tlist)
  111. return full_tlist, slist
  112. JavaAction = SCons.Action.Action('$JAVACCOM', '$JAVACCOMSTR')
  113. JavaBuilder = SCons.Builder.Builder(action = JavaAction,
  114. emitter = emit_java_classes,
  115. target_factory = SCons.Node.FS.Entry,
  116. source_factory = SCons.Node.FS.Entry)
  117. class pathopt(object):
  118. """
  119. Callable object for generating javac-style path options from
  120. a construction variable (e.g. -classpath, -sourcepath).
  121. """
  122. def __init__(self, opt, var, default=None):
  123. self.opt = opt
  124. self.var = var
  125. self.default = default
  126. def __call__(self, target, source, env, for_signature):
  127. path = env[self.var]
  128. if path and not SCons.Util.is_List(path):
  129. path = [path]
  130. if self.default:
  131. default = env[self.default]
  132. if default:
  133. if not SCons.Util.is_List(default):
  134. default = [default]
  135. path = path + default
  136. if path:
  137. return [self.opt, os.pathsep.join(map(str, path))]
  138. else:
  139. return []
  140. def Java(env, target, source, *args, **kw):
  141. """
  142. A pseudo-Builder wrapper around the separate JavaClass{File,Dir}
  143. Builders.
  144. """
  145. if not SCons.Util.is_List(target):
  146. target = [target]
  147. if not SCons.Util.is_List(source):
  148. source = [source]
  149. # Pad the target list with repetitions of the last element in the
  150. # list so we have a target for every source element.
  151. target = target + ([target[-1]] * (len(source) - len(target)))
  152. java_suffix = env.subst('$JAVASUFFIX')
  153. result = []
  154. for t, s in zip(target, source):
  155. if isinstance(s, SCons.Node.FS.Base):
  156. if isinstance(s, SCons.Node.FS.File):
  157. b = env.JavaClassFile
  158. else:
  159. b = env.JavaClassDir
  160. else:
  161. if os.path.isfile(s):
  162. b = env.JavaClassFile
  163. elif os.path.isdir(s):
  164. b = env.JavaClassDir
  165. elif s[-len(java_suffix):] == java_suffix:
  166. b = env.JavaClassFile
  167. else:
  168. b = env.JavaClassDir
  169. result.extend(b(t, s, *args, **kw))
  170. return result
  171. def generate(env):
  172. """Add Builders and construction variables for javac to an Environment."""
  173. java_file = SCons.Tool.CreateJavaFileBuilder(env)
  174. java_class = SCons.Tool.CreateJavaClassFileBuilder(env)
  175. java_class_dir = SCons.Tool.CreateJavaClassDirBuilder(env)
  176. java_class.add_emitter(None, emit_java_classes)
  177. java_class.add_emitter(env.subst('$JAVASUFFIX'), emit_java_classes)
  178. java_class_dir.emitter = emit_java_classes
  179. env.AddMethod(Java)
  180. env['JAVAC'] = 'javac'
  181. env['JAVACFLAGS'] = SCons.Util.CLVar('')
  182. env['JAVABOOTCLASSPATH'] = []
  183. env['JAVACLASSPATH'] = []
  184. env['JAVASOURCEPATH'] = []
  185. env['_javapathopt'] = pathopt
  186. env['_JAVABOOTCLASSPATH'] = '${_javapathopt("-bootclasspath", "JAVABOOTCLASSPATH")} '
  187. env['_JAVACLASSPATH'] = '${_javapathopt("-classpath", "JAVACLASSPATH")} '
  188. env['_JAVASOURCEPATH'] = '${_javapathopt("-sourcepath", "JAVASOURCEPATH", "_JAVASOURCEPATHDEFAULT")} '
  189. env['_JAVASOURCEPATHDEFAULT'] = '${TARGET.attributes.java_sourcedir}'
  190. env['_JAVACCOM'] = '$JAVAC $JAVACFLAGS $_JAVABOOTCLASSPATH $_JAVACLASSPATH -d ${TARGET.attributes.java_classdir} $_JAVASOURCEPATH $SOURCES'
  191. env['JAVACCOM'] = "${TEMPFILE('$_JAVACCOM','$JAVACCOMSTR')}"
  192. env['JAVACLASSSUFFIX'] = '.class'
  193. env['JAVASUFFIX'] = '.java'
  194. def exists(env):
  195. return 1
  196. # Local Variables:
  197. # tab-width:4
  198. # indent-tabs-mode:nil
  199. # End:
  200. # vim: set expandtab tabstop=4 shiftwidth=4: