3 Tool-specific initialization for javac.
5 There normally shouldn't be any need to import this module directly.
6 It will usually be imported through the generic SCons.Tool.Tool()
14 # Permission is hereby granted, free of charge, to any person obtaining
15 # a copy of this software and associated documentation files (the
16 # "Software"), to deal in the Software without restriction, including
17 # without limitation the rights to use, copy, modify, merge, publish,
18 # distribute, sublicense, and/or sell copies of the Software, and to
19 # permit persons to whom the Software is furnished to do so, subject to
20 # the following conditions:
22 # The above copyright notice and this permission notice shall be included
23 # in all copies or substantial portions of the Software.
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
26 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
27 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 from __future__ import generators ### KEEP FOR COMPATIBILITY FIXERS
35 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
42 from SCons.Node.FS import _my_normcase
43 from SCons.Tool.JavaCommon import parse_java_file
47 """Turn a string (path name) into a Java class name."""
48 return os.path.normpath(path).replace(os.sep, '.')
50 def emit_java_classes(target, source, env):
51 """Create and return lists of source java files
52 and their corresponding target class files.
54 java_suffix = env.get('JAVASUFFIX', '.java')
55 class_suffix = env.get('JAVACLASSSUFFIX', '.class')
57 target[0].must_be_same(SCons.Node.FS.Dir)
60 s = source[0].rentry().disambiguate()
61 if isinstance(s, SCons.Node.FS.File):
62 sourcedir = s.dir.rdir()
63 elif isinstance(s, SCons.Node.FS.Dir):
66 raise SCons.Errors.UserError("Java source must be File or Dir, not '%s'" % s.__class__)
69 js = _my_normcase(java_suffix)
71 entry = entry.rentry().disambiguate()
72 if isinstance(entry, SCons.Node.FS.File):
74 elif isinstance(entry, SCons.Node.FS.Dir):
75 result = SCons.Util.OrderedDict()
76 def visit(arg, dirname, names, dirnode=entry.rdir()):
77 java_files = [n for n in names if _my_normcase(n[-len(js):]) == js]
78 # The on-disk entries come back in arbitrary order. Sort
79 # them so our target and source lists are determinate.
81 mydir = dirnode.Dir(dirname)
82 java_paths = [mydir.File(f) for f in java_files]
86 os.path.walk(entry.rdir().get_abspath(), visit, result)
87 entry.walk(visit, result)
89 slist.extend(result.keys())
91 raise SCons.Errors.UserError("Java source must be File or Dir, not '%s'" % entry.__class__)
93 version = env.get('JAVAVERSION', '1.4')
97 source_file_based = True
99 if not f.is_derived():
100 pkg_dir, classes = parse_java_file(f.rfile().get_abspath(), version)
102 source_file_based = False
104 d = target[0].Dir(pkg_dir)
110 t = d.File(c + class_suffix)
111 t.attributes.java_classdir = classdir
112 t.attributes.java_sourcedir = sourcedir
113 t.attributes.java_classname = classname(p + c)
116 if source_file_based:
117 base = f.name[:-len(java_suffix)]
119 t = target[0].Dir(pkg_dir).File(base + class_suffix)
121 t = target[0].File(base + class_suffix)
122 t.attributes.java_classdir = classdir
123 t.attributes.java_sourcedir = f.dir
124 t.attributes.java_classname = classname(base)
128 t.set_specific_source([f])
130 full_tlist.extend(tlist)
132 return full_tlist, slist
134 JavaAction = SCons.Action.Action('$JAVACCOM', '$JAVACCOMSTR')
136 JavaBuilder = SCons.Builder.Builder(action = JavaAction,
137 emitter = emit_java_classes,
138 target_factory = SCons.Node.FS.Entry,
139 source_factory = SCons.Node.FS.Entry)
143 Callable object for generating javac-style path options from
144 a construction variable (e.g. -classpath, -sourcepath).
146 def __init__(self, opt, var, default=None):
149 self.default = default
151 def __call__(self, target, source, env, for_signature):
153 if path and not SCons.Util.is_List(path):
156 path = path + [ env[self.default] ]
158 return [self.opt, os.pathsep.join(path)]
159 #return self.opt + " " + os.pathsep.join(path)
164 def Java(env, target, source, *args, **kw):
166 A pseudo-Builder wrapper around the separate JavaClass{File,Dir}
169 if not SCons.Util.is_List(target):
171 if not SCons.Util.is_List(source):
174 # Pad the target list with repetitions of the last element in the
175 # list so we have a target for every source element.
176 target = target + ([target[-1]] * (len(source) - len(target)))
178 java_suffix = env.subst('$JAVASUFFIX')
181 for t, s in zip(target, source):
182 if isinstance(s, SCons.Node.FS.Base):
183 if isinstance(s, SCons.Node.FS.File):
184 b = env.JavaClassFile
188 if os.path.isfile(s):
189 b = env.JavaClassFile
190 elif os.path.isdir(s):
192 elif s[-len(java_suffix):] == java_suffix:
193 b = env.JavaClassFile
196 result.extend(b(t, s, *args, **kw))
201 """Add Builders and construction variables for javac to an Environment."""
202 java_file = SCons.Tool.CreateJavaFileBuilder(env)
203 java_class = SCons.Tool.CreateJavaClassFileBuilder(env)
204 java_class_dir = SCons.Tool.CreateJavaClassDirBuilder(env)
205 java_class.add_emitter(None, emit_java_classes)
206 java_class.add_emitter(env.subst('$JAVASUFFIX'), emit_java_classes)
207 java_class_dir.emitter = emit_java_classes
211 env['JAVAC'] = 'javac'
212 env['JAVACFLAGS'] = SCons.Util.CLVar('')
213 env['JAVABOOTCLASSPATH'] = []
214 env['JAVACLASSPATH'] = []
215 env['JAVASOURCEPATH'] = []
216 env['_javapathopt'] = pathopt
217 env['_JAVABOOTCLASSPATH'] = '${_javapathopt("-bootclasspath", "JAVABOOTCLASSPATH")} '
218 env['_JAVACLASSPATH'] = '${_javapathopt("-classpath", "JAVACLASSPATH")} '
219 env['_JAVASOURCEPATH'] = '${_javapathopt("-sourcepath", "JAVASOURCEPATH", "_JAVASOURCEPATHDEFAULT")} '
220 env['_JAVASOURCEPATHDEFAULT'] = '${TARGET.attributes.java_sourcedir}'
221 env['_JAVACCOM'] = '$JAVAC $JAVACFLAGS $_JAVABOOTCLASSPATH $_JAVACLASSPATH -d ${TARGET.attributes.java_classdir} $_JAVASOURCEPATH $SOURCES'
222 env['JAVACCOM'] = "${TEMPFILE('$_JAVACCOM')}"
223 env['JAVACLASSSUFFIX'] = '.class'
224 env['JAVASUFFIX'] = '.java'
231 # indent-tabs-mode:nil
233 # vim: set expandtab tabstop=4 shiftwidth=4: