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()
12 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 The SCons Foundation
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 __revision__ = "src/engine/SCons/Tool/javac.py 5023 2010/06/14 22:05:46 scons"
40 from SCons.Node.FS import _my_normcase
41 from SCons.Tool.JavaCommon import parse_java_file
45 """Turn a string (path name) into a Java class name."""
46 return os.path.normpath(path).replace(os.sep, '.')
48 def emit_java_classes(target, source, env):
49 """Create and return lists of source java files
50 and their corresponding target class files.
52 java_suffix = env.get('JAVASUFFIX', '.java')
53 class_suffix = env.get('JAVACLASSSUFFIX', '.class')
55 target[0].must_be_same(SCons.Node.FS.Dir)
58 s = source[0].rentry().disambiguate()
59 if isinstance(s, SCons.Node.FS.File):
60 sourcedir = s.dir.rdir()
61 elif isinstance(s, SCons.Node.FS.Dir):
64 raise SCons.Errors.UserError("Java source must be File or Dir, not '%s'" % s.__class__)
67 js = _my_normcase(java_suffix)
69 entry = entry.rentry().disambiguate()
70 if isinstance(entry, SCons.Node.FS.File):
72 elif isinstance(entry, SCons.Node.FS.Dir):
73 result = SCons.Util.OrderedDict()
74 dirnode = entry.rdir()
75 def find_java_files(arg, dirpath, filenames):
76 java_files = sorted([n for n in filenames
77 if _my_normcase(n).endswith(js)])
78 mydir = dirnode.Dir(dirpath)
79 java_paths = [mydir.File(f) for f in java_files]
82 for dirpath, dirnames, filenames in os.walk(dirnode.get_abspath()):
83 find_java_files(result, dirpath, filenames)
84 entry.walk(find_java_files, result)
86 slist.extend(list(result.keys()))
88 raise SCons.Errors.UserError("Java source must be File or Dir, not '%s'" % entry.__class__)
90 version = env.get('JAVAVERSION', '1.4')
94 source_file_based = True
96 if not f.is_derived():
97 pkg_dir, classes = parse_java_file(f.rfile().get_abspath(), version)
99 source_file_based = False
101 d = target[0].Dir(pkg_dir)
107 t = d.File(c + class_suffix)
108 t.attributes.java_classdir = classdir
109 t.attributes.java_sourcedir = sourcedir
110 t.attributes.java_classname = classname(p + c)
113 if source_file_based:
114 base = f.name[:-len(java_suffix)]
116 t = target[0].Dir(pkg_dir).File(base + class_suffix)
118 t = target[0].File(base + class_suffix)
119 t.attributes.java_classdir = classdir
120 t.attributes.java_sourcedir = f.dir
121 t.attributes.java_classname = classname(base)
125 t.set_specific_source([f])
127 full_tlist.extend(tlist)
129 return full_tlist, slist
131 JavaAction = SCons.Action.Action('$JAVACCOM', '$JAVACCOMSTR')
133 JavaBuilder = SCons.Builder.Builder(action = JavaAction,
134 emitter = emit_java_classes,
135 target_factory = SCons.Node.FS.Entry,
136 source_factory = SCons.Node.FS.Entry)
138 class pathopt(object):
140 Callable object for generating javac-style path options from
141 a construction variable (e.g. -classpath, -sourcepath).
143 def __init__(self, opt, var, default=None):
146 self.default = default
148 def __call__(self, target, source, env, for_signature):
150 if path and not SCons.Util.is_List(path):
153 path = path + [ env[self.default] ]
155 return [self.opt, os.pathsep.join(path)]
156 #return self.opt + " " + os.pathsep.join(path)
161 def Java(env, target, source, *args, **kw):
163 A pseudo-Builder wrapper around the separate JavaClass{File,Dir}
166 if not SCons.Util.is_List(target):
168 if not SCons.Util.is_List(source):
171 # Pad the target list with repetitions of the last element in the
172 # list so we have a target for every source element.
173 target = target + ([target[-1]] * (len(source) - len(target)))
175 java_suffix = env.subst('$JAVASUFFIX')
178 for t, s in zip(target, source):
179 if isinstance(s, SCons.Node.FS.Base):
180 if isinstance(s, SCons.Node.FS.File):
181 b = env.JavaClassFile
185 if os.path.isfile(s):
186 b = env.JavaClassFile
187 elif os.path.isdir(s):
189 elif s[-len(java_suffix):] == java_suffix:
190 b = env.JavaClassFile
193 result.extend(b(t, s, *args, **kw))
198 """Add Builders and construction variables for javac to an Environment."""
199 java_file = SCons.Tool.CreateJavaFileBuilder(env)
200 java_class = SCons.Tool.CreateJavaClassFileBuilder(env)
201 java_class_dir = SCons.Tool.CreateJavaClassDirBuilder(env)
202 java_class.add_emitter(None, emit_java_classes)
203 java_class.add_emitter(env.subst('$JAVASUFFIX'), emit_java_classes)
204 java_class_dir.emitter = emit_java_classes
208 env['JAVAC'] = 'javac'
209 env['JAVACFLAGS'] = SCons.Util.CLVar('')
210 env['JAVABOOTCLASSPATH'] = []
211 env['JAVACLASSPATH'] = []
212 env['JAVASOURCEPATH'] = []
213 env['_javapathopt'] = pathopt
214 env['_JAVABOOTCLASSPATH'] = '${_javapathopt("-bootclasspath", "JAVABOOTCLASSPATH")} '
215 env['_JAVACLASSPATH'] = '${_javapathopt("-classpath", "JAVACLASSPATH")} '
216 env['_JAVASOURCEPATH'] = '${_javapathopt("-sourcepath", "JAVASOURCEPATH", "_JAVASOURCEPATHDEFAULT")} '
217 env['_JAVASOURCEPATHDEFAULT'] = '${TARGET.attributes.java_sourcedir}'
218 env['_JAVACCOM'] = '$JAVAC $JAVACFLAGS $_JAVABOOTCLASSPATH $_JAVACLASSPATH -d ${TARGET.attributes.java_classdir} $_JAVASOURCEPATH $SOURCES'
219 env['JAVACCOM'] = "${TEMPFILE('$_JAVACCOM')}"
220 env['JAVACLASSSUFFIX'] = '.class'
221 env['JAVASUFFIX'] = '.java'
228 # indent-tabs-mode:nil
230 # vim: set expandtab tabstop=4 shiftwidth=4: