src/share/classes/com/sun/tools/sjavac/Main.java

Tue, 19 Mar 2013 17:04:03 -0700

author
jjg
date
Tue, 19 Mar 2013 17:04:03 -0700
changeset 1648
a03c4a86ea2b
parent 1625
fbb6e470079d
child 1953
71b0089b146f
permissions
-rw-r--r--

8010361: fix some langtools findbugs issues
Reviewed-by: darcy

ohrstrom@1504 1 /*
jjg@1648 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
ohrstrom@1504 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@1504 4 *
ohrstrom@1504 5 * This code is free software; you can redistribute it and/or modify it
ohrstrom@1504 6 * under the terms of the GNU General Public License version 2 only, as
ohrstrom@1504 7 * published by the Free Software Foundation. Oracle designates this
ohrstrom@1504 8 * particular file as subject to the "Classpath" exception as provided
ohrstrom@1504 9 * by Oracle in the LICENSE file that accompanied this code.
ohrstrom@1504 10 *
ohrstrom@1504 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@1504 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@1504 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@1504 14 * version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@1504 15 * accompanied this code).
ohrstrom@1504 16 *
ohrstrom@1504 17 * You should have received a copy of the GNU General Public License version
ohrstrom@1504 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@1504 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@1504 20 *
ohrstrom@1504 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@1504 22 * or visit www.oracle.com if you need additional information or have any
ohrstrom@1504 23 * questions.
ohrstrom@1504 24 */
ohrstrom@1504 25
ohrstrom@1504 26 package com.sun.tools.sjavac;
ohrstrom@1504 27
ohrstrom@1504 28 import java.io.File;
ohrstrom@1504 29 import java.io.IOException;
ohrstrom@1504 30 import java.io.PrintStream;
ohrstrom@1504 31 import java.util.*;
jjg@1648 32 import java.util.regex.Matcher;
jjg@1648 33 import java.util.regex.Pattern;
jjg@1648 34
jjg@1648 35 import com.sun.tools.sjavac.server.JavacServer;
ohrstrom@1504 36
ohrstrom@1504 37 /**
ohrstrom@1504 38 * The main class of the smart javac wrapper tool.
ohrstrom@1504 39 *
ohrstrom@1504 40 * <p><b>This is NOT part of any supported API.
ohrstrom@1504 41 * If you write code that depends on this, you do so at your own
ohrstrom@1504 42 * risk. This code and its internal interfaces are subject to change
ohrstrom@1504 43 * or deletion without notice.</b></p>
ohrstrom@1504 44 */
ohrstrom@1504 45 public class Main {
ohrstrom@1504 46
ohrstrom@1504 47 /* This is a smart javac wrapper primarily used when building the OpenJDK,
ohrstrom@1504 48 though other projects are welcome to use it too. But please be aware
ohrstrom@1504 49 that it is not an official api and will change in the future.
ohrstrom@1504 50 (We really mean it!)
ohrstrom@1504 51
ohrstrom@1504 52 Goals:
ohrstrom@1504 53
ohrstrom@1504 54 ** Create a state file, containing information about the build, so
ohrstrom@1504 55 that incremental builds only rebuild what is necessary. Also the
ohrstrom@1504 56 state file can be used by make/ant to detect when to trigger
ohrstrom@1504 57 a call to the smart javac wrapper.
ohrstrom@1504 58
ohrstrom@1504 59 This file is called bin/javac_state (assuming that you specified "-d bin")
ohrstrom@1504 60 Thus the simplest makefile is:
ohrstrom@1504 61
ohrstrom@1504 62 SJAVAC=java -cp .../tools.jar com.sun.tools.sjavac.Main
ohrstrom@1504 63 SRCS=$(shell find src -name "*.java")
ohrstrom@1504 64 bin/javac_state : $(SRCS)
ohrstrom@1504 65 $(SJAVAC) src -d bin
ohrstrom@1504 66
ohrstrom@1504 67 This makefile will run very fast and detect properly when Java code needs to
ohrstrom@1504 68 be recompiled. The smart javac wrapper will then use the information in java_state
ohrstrom@1504 69 to do an efficient incremental compile.
ohrstrom@1504 70
ohrstrom@1504 71 Previously it was near enough impossible to write an efficient makefile for Java
ohrstrom@1504 72 with support for incremental builds and dependency tracking.
ohrstrom@1504 73
ohrstrom@1504 74 ** Separate java sources to be compiled from java
ohrstrom@1504 75 sources used >only< for linking. The options:
ohrstrom@1504 76
ohrstrom@1504 77 "dir" points to root dir with sources to be compiled
ohrstrom@1504 78 "-sourcepath dir" points to root dir with sources used only for linking
ohrstrom@1504 79 "-classpath dir" points to dir with classes used only for linking (as before)
ohrstrom@1504 80
ohrstrom@1504 81 ** Use all cores for compilation by default.
ohrstrom@1504 82 "-j 4" limit the number of cores to 4.
ohrstrom@1504 83 For the moment, the sjavac server additionally limits the number of cores to three.
ohrstrom@1504 84 This will improve in the future when more sharing is performed between concurrent JavaCompilers.
ohrstrom@1504 85
ohrstrom@1504 86 ** Basic translation support from other sources to java, and then compilation of the generated java.
ohrstrom@1504 87 This functionality might be moved into annotation processors instead.
ohrstrom@1504 88 Again this is driven by the OpenJDK sources where properties and a few other types of files
ohrstrom@1504 89 are converted into Java sources regularily. The javac_state embraces copy and tr, and perform
ohrstrom@1504 90 incremental recompiles and copying for these as well. META-INF will be a special copy rule
ohrstrom@1504 91 that will copy any files found below any META-INF dir in src to the bin/META-INF dir.
ohrstrom@1504 92 "-copy .gif"
ohrstrom@1504 93 "-copy META-INF"
ohrstrom@1504 94 "-tr .prop=com.sun.tools.javac.smart.CompileProperties
ohrstrom@1504 95 "-tr .propp=com.sun.tools.javac.smart.CompileProperties,java.util.ListResourceBundle
ohrstrom@1504 96 "-tr .proppp=com.sun.tools.javac.smart.CompileProperties,sun.util.resources.LocaleNamesBundle
ohrstrom@1504 97
ohrstrom@1504 98 ** Control which classes in the src,sourcepath and classpath that javac is allowed to see.
ohrstrom@1504 99 Again, this is necessary to deal with the source code structure of the OpenJDK which is
ohrstrom@1504 100 intricate (read messy).
ohrstrom@1504 101
ohrstrom@1504 102 "-i tools.*" to include the tools package and all its subpackages in the build.
ohrstrom@1504 103 "-x tools.net.aviancarrier.*" to exclude the aviancarrier package and all its sources and subpackages.
ohrstrom@1504 104 "-x tools.net.drums" to exclude the drums package only, keep its subpackages.
ohrstrom@1504 105 "-xf tools/net/Bar.java" // Do not compile this file...
ohrstrom@1504 106 "-xf *Bor.java" // Do not compile Bor.java wherever it is found, BUT do compile ABor.java!
ohrstrom@1504 107 "-if tools/net/Bor.java" // Only compile this file...odd, but sometimes used.
ohrstrom@1504 108
ohrstrom@1504 109 ** The smart javac wrapper is driven by the modification time on the source files and compared
ohrstrom@1504 110 to the modification times written into the javac_state file.
ohrstrom@1504 111
ohrstrom@1504 112 It does not compare the modification time of the source with the modification time of the artifact.
ohrstrom@1504 113 However it will detect if the modification time of an artifact has changed compared to the java_state,
ohrstrom@1504 114 and this will trigger a delete of the artifact and a subsequent recompile of the source.
ohrstrom@1504 115
ohrstrom@1504 116 The smart javac wrapper is not a generic makefile/ant system. Its purpose is to compile java source
ohrstrom@1504 117 as the final step before the output dir is finalized and immediately jared, or jmodded. The output
ohrstrom@1504 118 dir should be considered opaque. Do not write into the outputdir yourself!
ohrstrom@1504 119 Any artifacts found in the outputdir that javac_state does not know of, will be deleted!
ohrstrom@1504 120 This can however be prevented, using the switch --permit-unidentified-artifacts
ohrstrom@1504 121 This switch is necessary when build the OpenJDK because its makefiles still write directly to
ohrstrom@1504 122 the output classes dirs.
ohrstrom@1504 123
ohrstrom@1504 124 Any makefile/ant rules that want to put contents into the outputdir should put the content
ohrstrom@1504 125 in one of several source roots. Static content that is under version control, can be put in the same source
ohrstrom@1504 126 code tree as the Java sources. Dynamic content that is generated by make/ant on the fly, should
ohrstrom@1504 127 be put in a separate gensrc_stuff root. The smart javac wrapper call will then take the arguments:
ohrstrom@1504 128 "gensrc_stuff src -d bin"
ohrstrom@1504 129
ohrstrom@1504 130 The command line:
ohrstrom@1504 131 java -cp tools.jar com.sun.tools.sjavac.Main \
ohrstrom@1504 132 -i "com.bar.*" -x "com.bar.foo.*" \
ohrstrom@1504 133 first_root \
ohrstrom@1504 134 -i "com.bar.foo.*" \
ohrstrom@1504 135 second_root \
ohrstrom@1504 136 -x "org.net.*" \
ohrstrom@1504 137 -sourcepath link_root_sources \
ohrstrom@1504 138 -classpath link_root_classes \
ohrstrom@1504 139 -d bin
ohrstrom@1504 140
ohrstrom@1504 141 Will compile all sources for package com.bar and its subpackages, found below first_root,
ohrstrom@1504 142 except the package com.bar.foo (and its subpackages), for which the sources are picked
ohrstrom@1504 143 from second_root instead. It will link against classes in link_root_classes and against
ohrstrom@1504 144 sources in link_root_sources, but will not see (try to link against) sources matching org.net.*
ohrstrom@1504 145 but will link against org.net* classes (if they exist) in link_root_classes.
ohrstrom@1504 146
ohrstrom@1504 147 (If you want a set of complex filter rules to be applied to several source directories, without
ohrstrom@1504 148 having to repeat the the filter rules for each root. You can use the explicit -src option. For example:
ohrstrom@1504 149 sjavac -x "com.foo.*" -src root1:root2:root3 )
ohrstrom@1504 150
ohrstrom@1504 151 The resulting classes are written into bin.
ohrstrom@1504 152 */
ohrstrom@1504 153
ohrstrom@1504 154 // This is the final destination for classes and copied files.
ohrstrom@1504 155 private File bin_dir;
ohrstrom@1504 156 // This is where the annotation process will put generated sources.
ohrstrom@1504 157 private File gensrc_dir;
ohrstrom@1504 158 // This is where javac -h puts the generated c-header files.
ohrstrom@1504 159 private File header_dir;
ohrstrom@1504 160
ohrstrom@1504 161 // This file contains the list of sources genereated by the makefile.
ohrstrom@1504 162 // We double check that our calculated list of sources matches this list,
ohrstrom@1504 163 // if not, then we terminate with an error!
ohrstrom@1504 164 private File makefile_source_list;
ohrstrom@1504 165 // The challenging task to manage an incremental build is done by javac_state.
ohrstrom@1504 166 private JavacState javac_state;
ohrstrom@1504 167
ohrstrom@1504 168 // The suffix rules tells you for example, that .java files should be compiled,
ohrstrom@1504 169 // and .html files should be copied and .properties files be translated.
ohrstrom@1504 170 Map<String,Transformer> suffix_rules;
ohrstrom@1504 171
ohrstrom@1504 172 public static void main(String... args) {
ohrstrom@1504 173 if (args.length > 0 && args[0].startsWith("--startserver:")) {
ohrstrom@1504 174 if (args.length>1) {
ohrstrom@1504 175 Log.error("When spawning a background server, only a single --startserver argument is allowed.");
ohrstrom@1504 176 return;
ohrstrom@1504 177 }
ohrstrom@1504 178 // Spawn a background server.
ohrstrom@1504 179 int rc = JavacServer.startServer(args[0], System.err);
ohrstrom@1504 180 System.exit(rc);
ohrstrom@1504 181 }
ohrstrom@1504 182 Main main = new Main();
ohrstrom@1504 183 int rc = main.go(args, System.out, System.err);
ohrstrom@1504 184 // Remove the portfile, but only if this background=false was used.
ohrstrom@1504 185 JavacServer.cleanup(args);
ohrstrom@1504 186 System.exit(rc);
ohrstrom@1504 187 }
ohrstrom@1504 188
ohrstrom@1504 189 private void printHelp() {
ohrstrom@1504 190 System.out.println("Usage: sjavac <options>\n"+
ohrstrom@1504 191 "where required options are:\n"+
ohrstrom@1504 192 "dir Compile all sources in dir recursively\n"+
ohrstrom@1504 193 "-d dir Store generated classes here and the javac_state file\n"+
ohrstrom@1504 194 "--server:portfile=/tmp/abc Use a background sjavac server\n\n"+
ohrstrom@1504 195 "All other arguments as javac, except -implicit:none which is forced by default.\n"+
ohrstrom@1504 196 "No java source files can be supplied on the command line, nor can an @file be supplied.\n\n"+
ohrstrom@1504 197 "Warning!\n"+
ohrstrom@1504 198 "This tool might disappear at any time, and its command line options might change at any time!");
ohrstrom@1504 199 }
ohrstrom@1504 200
ohrstrom@1504 201 public int go(String[] args, PrintStream out, PrintStream err) {
ohrstrom@1504 202 try {
ohrstrom@1504 203 if (args.length == 0 || findJavaSourceFiles(args) || findAtFile(args) || null==Util.findServerSettings(args)) {
ohrstrom@1504 204 printHelp();
ohrstrom@1504 205 return 0;
ohrstrom@1504 206 }
ohrstrom@1504 207
ohrstrom@1504 208 Log.setLogLevel(findLogLevel(args), out, err);
ohrstrom@1504 209 String server_settings = Util.findServerSettings(args);
ohrstrom@1504 210 args = verifyImplicitOption(args);
ohrstrom@1504 211 // Find the source root directories, and add the -src option before these, if not there already.
ohrstrom@1504 212 args = addSrcBeforeDirectories(args);
ohrstrom@1504 213 // Check that there is at least one -src supplied.
ohrstrom@1504 214 checkSrcOption(args);
ohrstrom@1504 215 // Check that there is one -d supplied.
ohrstrom@1504 216 bin_dir = findDirectoryOption(args,"-d","output", true, false, true);
ohrstrom@1504 217 gensrc_dir = findDirectoryOption(args,"-s","gensrc", false, false, true);
ohrstrom@1504 218 header_dir = findDirectoryOption(args,"-h","headers", false, false, true);
ohrstrom@1504 219 makefile_source_list = findFileOption(args,"--compare-found-sources","makefile source list", false);
ohrstrom@1504 220
ohrstrom@1504 221 // Load the prev build state database.
ohrstrom@1504 222 javac_state = JavacState.load(args, bin_dir, gensrc_dir, header_dir,
ohrstrom@1504 223 findBooleanOption(args, "--permit-unidentified-artifacts"), out, err);
ohrstrom@1504 224
ohrstrom@1504 225 // Setup the suffix rules from the command line.
ohrstrom@1504 226 suffix_rules = javac_state.getJavaSuffixRule();
ohrstrom@1504 227 findTranslateOptions(args, suffix_rules);
ohrstrom@1504 228 if (suffix_rules.keySet().size() > 1 && gensrc_dir == null) {
ohrstrom@1504 229 Log.error("You have translators but no gensrc dir (-s) specified!");
ohrstrom@1504 230 return -1;
ohrstrom@1504 231 }
ohrstrom@1504 232 findCopyOptions(args, suffix_rules);
ohrstrom@1504 233
ohrstrom@1504 234 // All found modules are put here.
ohrstrom@1504 235 Map<String,Module> modules = new HashMap<String,Module>();
ohrstrom@1504 236 // We start out in the legacy empty no-name module.
ohrstrom@1504 237 // As soon as we stumble on a module-info.java file we change to that module.
ohrstrom@1504 238 Module current_module = new Module("", "");
ohrstrom@1504 239 modules.put("", current_module);
ohrstrom@1504 240
ohrstrom@1504 241 // Find all sources, use the suffix rules to know which files are sources.
ohrstrom@1504 242 Map<String,Source> sources = new HashMap<String,Source>();
ohrstrom@1504 243 // Find the files, this will automatically populate the found modules
ohrstrom@1504 244 // with found packages where the sources are found!
ohrstrom@1504 245 findFiles(args, "-src", suffix_rules.keySet(), sources, modules, current_module, false);
ohrstrom@1504 246
ohrstrom@1504 247 if (sources.isEmpty()) {
ohrstrom@1504 248 Log.error("Found nothing to compile!");
ohrstrom@1504 249 return -1;
ohrstrom@1504 250 }
ohrstrom@1504 251
ohrstrom@1504 252 // Find all source files allowable for linking.
ohrstrom@1504 253 // We might find more modules here as well.
ohrstrom@1504 254 Map<String,Source> sources_to_link_to = new HashMap<String,Source>();
ohrstrom@1504 255 // Always reuse -src for linking as well! This means that we might
ohrstrom@1504 256 // get two -sourcepath on the commandline after the rewrite, which is
ohrstrom@1504 257 // fine. We can have as many as we like. You need to have separate -src/-sourcepath/-classpath
ohrstrom@1504 258 // if you need different filtering rules for different roots. If you have the same filtering
ohrstrom@1504 259 // rules for all sourcepath roots, you can concatenate them using :(;) as before.
ohrstrom@1504 260 rewriteOptions(args, "-src", "-sourcepath");
ohrstrom@1504 261 findFiles(args, "-sourcepath", Util.set(".java"), sources_to_link_to, modules, current_module, true);
ohrstrom@1504 262
ohrstrom@1504 263 // Find all class files allowable for linking.
ohrstrom@1504 264 // And pickup knowledge of all modules found here.
ohrstrom@1504 265 // This cannot currently filter classes inside jar files.
jjg@1648 266 // Map<String,Source> classes_to_link_to = new HashMap<String,Source>();
ohrstrom@1504 267 // findFiles(args, "-classpath", Util.set(".class"), classes_to_link_to, modules, current_module, true);
ohrstrom@1504 268
ohrstrom@1504 269 // Find all module sources allowable for linking.
jjg@1648 270 // Map<String,Source> modules_to_link_to = new HashMap<String,Source>();
jjg@1648 271 // findFiles(args, "-modulepath", Util.set(".class"), modules_to_link_to, modules, current_module, true);
ohrstrom@1504 272
ohrstrom@1504 273 // Add the set of sources to the build database.
ohrstrom@1504 274 javac_state.now().collectPackagesSourcesAndArtifacts(modules);
ohrstrom@1504 275 javac_state.now().checkInternalState("checking sources", false, sources);
ohrstrom@1504 276 javac_state.now().checkInternalState("checking linked sources", true, sources_to_link_to);
ohrstrom@1504 277 javac_state.setVisibleSources(sources_to_link_to);
ohrstrom@1504 278
ohrstrom@1504 279 // If there is any change in the source files, taint packages
ohrstrom@1504 280 // and mark the database in need of saving.
ohrstrom@1504 281 javac_state.checkSourceStatus(false);
ohrstrom@1504 282
ohrstrom@1504 283 // Find all existing artifacts. Their timestamp will match the last modified timestamps stored
ohrstrom@1504 284 // in javac_state, simply because loading of the JavacState will clean out all artifacts
ohrstrom@1504 285 // that do not match the javac_state database.
ohrstrom@1504 286 javac_state.findAllArtifacts();
ohrstrom@1504 287
ohrstrom@1504 288 // Remove unidentified artifacts from the bin, gensrc and header dirs.
ohrstrom@1504 289 // (Unless we allow them to be there.)
ohrstrom@1504 290 // I.e. artifacts that are not known according to the build database (javac_state).
ohrstrom@1504 291 // For examples, files that have been manually copied into these dirs.
ohrstrom@1504 292 // Artifacts with bad timestamps (ie the on disk timestamp does not match the timestamp
ohrstrom@1504 293 // in javac_state) have already been removed when the javac_state was loaded.
ohrstrom@1504 294 if (!findBooleanOption(args, "--permit-unidentified-artifacts")) {
ohrstrom@1504 295 javac_state.removeUnidentifiedArtifacts();
ohrstrom@1504 296 }
ohrstrom@1504 297 // Go through all sources and taint all packages that miss artifacts.
ohrstrom@1504 298 javac_state.taintPackagesThatMissArtifacts();
ohrstrom@1504 299
ohrstrom@1504 300 // Now clean out all known artifacts belonging to tainted packages.
ohrstrom@1504 301 javac_state.deleteClassArtifactsInTaintedPackages();
ohrstrom@1504 302 // Copy files, for example property files, images files, xml files etc etc.
ohrstrom@1504 303 javac_state.performCopying(bin_dir, suffix_rules);
ohrstrom@1504 304 // Translate files, for example compile properties or compile idls.
ohrstrom@1504 305 javac_state.performTranslation(gensrc_dir, suffix_rules);
ohrstrom@1504 306 // Add any potentially generated java sources to the tobe compiled list.
ohrstrom@1504 307 // (Generated sources must always have a package.)
ohrstrom@1504 308 Map<String,Source> generated_sources = new HashMap<String,Source>();
ohrstrom@1504 309 Source.scanRoot(gensrc_dir, Util.set(".java"), null, null, null, null,
ohrstrom@1504 310 generated_sources, modules, current_module, false, true, false);
ohrstrom@1504 311 javac_state.now().collectPackagesSourcesAndArtifacts(modules);
ohrstrom@1504 312 // Recheck the the source files and their timestamps again.
ohrstrom@1504 313 javac_state.checkSourceStatus(true);
ohrstrom@1504 314
ohrstrom@1504 315 // Now do a safety check that the list of source files is identical
ohrstrom@1504 316 // to the list Make believes we are compiling. If we do not get this
ohrstrom@1504 317 // right, then incremental builds will fail with subtility.
ohrstrom@1504 318 // If any difference is detected, then we will fail hard here.
ohrstrom@1504 319 // This is an important safety net.
ohrstrom@1504 320 javac_state.compareWithMakefileList(makefile_source_list);
ohrstrom@1504 321
ohrstrom@1504 322 // Do the compilations, repeatedly until no tainted packages exist.
ohrstrom@1504 323 boolean again;
ohrstrom@1504 324 // Collect the name of all compiled packages.
ohrstrom@1504 325 Set<String> recently_compiled = new HashSet<String>();
ohrstrom@1504 326 boolean[] rc = new boolean[1];
ohrstrom@1504 327 do {
ohrstrom@1504 328 // Clean out artifacts in tainted packages.
ohrstrom@1504 329 javac_state.deleteClassArtifactsInTaintedPackages();
ohrstrom@1504 330 again = javac_state.performJavaCompilations(bin_dir, server_settings, args, recently_compiled, rc);
ohrstrom@1504 331 if (!rc[0]) break;
ohrstrom@1504 332 } while (again);
ohrstrom@1504 333 // Only update the state if the compile went well.
ohrstrom@1504 334 if (rc[0]) {
ohrstrom@1504 335 javac_state.save();
ohrstrom@1504 336 // Collect all the artifacts.
ohrstrom@1504 337 javac_state.now().collectArtifacts(modules);
ohrstrom@1504 338 // Remove artifacts that were generated during the last compile, but not this one.
ohrstrom@1504 339 javac_state.removeSuperfluousArtifacts(recently_compiled);
ohrstrom@1504 340 }
ohrstrom@1504 341 return rc[0] ? 0 : -1;
ohrstrom@1504 342 } catch (ProblemException e) {
ohrstrom@1504 343 Log.error(e.getMessage());
ohrstrom@1504 344 return -1;
ohrstrom@1504 345 } catch (Exception e) {
ohrstrom@1504 346 e.printStackTrace(err);
ohrstrom@1504 347 return -1;
ohrstrom@1504 348 }
ohrstrom@1504 349 }
ohrstrom@1504 350
ohrstrom@1504 351 /**
ohrstrom@1504 352 * Are java source files passed on the command line?
ohrstrom@1504 353 */
ohrstrom@1504 354 private boolean findJavaSourceFiles(String[] args) {
ohrstrom@1504 355 String prev = "";
ohrstrom@1504 356 for (String s : args) {
ohrstrom@1504 357 if (s.endsWith(".java") && !prev.equals("-xf") && !prev.equals("-if")) {
ohrstrom@1504 358 return true;
ohrstrom@1504 359 }
ohrstrom@1504 360 prev = s;
ohrstrom@1504 361 }
ohrstrom@1504 362 return false;
ohrstrom@1504 363 }
ohrstrom@1504 364
ohrstrom@1504 365 /**
ohrstrom@1504 366 * Is an at file passed on the command line?
ohrstrom@1504 367 */
ohrstrom@1504 368 private boolean findAtFile(String[] args) {
ohrstrom@1504 369 for (String s : args) {
ohrstrom@1504 370 if (s.startsWith("@")) {
ohrstrom@1504 371 return true;
ohrstrom@1504 372 }
ohrstrom@1504 373 }
ohrstrom@1504 374 return false;
ohrstrom@1504 375 }
ohrstrom@1504 376
ohrstrom@1504 377 /**
ohrstrom@1504 378 * Find the log level setting.
ohrstrom@1504 379 */
ohrstrom@1504 380 private String findLogLevel(String[] args) {
ohrstrom@1504 381 for (String s : args) {
ohrstrom@1504 382 if (s.startsWith("--log=") && s.length()>6) {
ohrstrom@1504 383 return s.substring(6);
ohrstrom@1504 384 }
ohrstrom@1504 385 if (s.equals("-verbose")) {
ohrstrom@1504 386 return "info";
ohrstrom@1504 387 }
ohrstrom@1504 388 }
ohrstrom@1504 389 return "info";
ohrstrom@1504 390 }
ohrstrom@1504 391
ohrstrom@1504 392 /**
ohrstrom@1504 393 * Remove smart javac wrapper arguments, before feeding
ohrstrom@1504 394 * the args to the plain javac.
ohrstrom@1504 395 */
ohrstrom@1504 396 static String[] removeWrapperArgs(String[] args) {
ohrstrom@1504 397 String[] out = new String[args.length];
ohrstrom@1504 398 // The first source path index is remembered
ohrstrom@1504 399 // here. So that all following can be concatenated to it.
ohrstrom@1504 400 int source_path = -1;
ohrstrom@1504 401 // The same for class path.
ohrstrom@1504 402 int class_path = -1;
ohrstrom@1504 403 // And module path.
ohrstrom@1504 404 int module_path = -1;
ohrstrom@1504 405 int j = 0;
ohrstrom@1504 406 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 407 if (args[i].equals("-src") ||
ohrstrom@1504 408 args[i].equals("-x") ||
ohrstrom@1504 409 args[i].equals("-i") ||
ohrstrom@1504 410 args[i].equals("-xf") ||
ohrstrom@1504 411 args[i].equals("-if") ||
ohrstrom@1504 412 args[i].equals("-copy") ||
ohrstrom@1504 413 args[i].equals("-tr") ||
ohrstrom@1504 414 args[i].equals("-j")) {
ohrstrom@1504 415 // Just skip it and skip following value
ohrstrom@1504 416 i++;
ohrstrom@1504 417 } else if (args[i].startsWith("--server:")) {
ohrstrom@1504 418 // Just skip it.
ohrstrom@1504 419 } else if (args[i].startsWith("--log=")) {
ohrstrom@1504 420 // Just skip it.
ohrstrom@1504 421 } else if (args[i].equals("--permit-unidentified-artifacts")) {
ohrstrom@1504 422 // Just skip it.
ohrstrom@1504 423 } else if (args[i].equals("--permit-sources-without-package")) {
ohrstrom@1504 424 // Just skip it.
ohrstrom@1504 425 } else if (args[i].equals("--compare-found-sources")) {
ohrstrom@1504 426 // Just skip it and skip verify file name
ohrstrom@1504 427 i++;
ohrstrom@1504 428 } else if (args[i].equals("-sourcepath")) {
ohrstrom@1504 429 if (source_path == -1) {
ohrstrom@1504 430 source_path = j;
ohrstrom@1504 431 out[j] = args[i];
ohrstrom@1504 432 out[j+1] = args[i+1];
ohrstrom@1504 433 j+=2;
ohrstrom@1504 434 i++;
ohrstrom@1504 435 } else {
ohrstrom@1504 436 // Skip this and its argument, but
ohrstrom@1504 437 // append argument to found sourcepath.
ohrstrom@1504 438 out[source_path+1] = out[source_path+1]+File.pathSeparatorChar+args[i+1];
ohrstrom@1504 439 i++;
ohrstrom@1504 440 }
ohrstrom@1625 441 } else if (args[i].equals("-classpath") || args[i].equals("-cp")) {
ohrstrom@1504 442 if (class_path == -1) {
ohrstrom@1504 443 class_path = j;
ohrstrom@1504 444 out[j] = args[i];
ohrstrom@1504 445 out[j+1] = args[i+1];
ohrstrom@1504 446 j+=2;
ohrstrom@1504 447 i++;
ohrstrom@1504 448 } else {
ohrstrom@1504 449 // Skip this and its argument, but
ohrstrom@1504 450 // append argument to found sourcepath.
ohrstrom@1504 451 out[class_path+1] = out[class_path+1]+File.pathSeparatorChar+args[i+1];
ohrstrom@1504 452 i++;
ohrstrom@1504 453 }
ohrstrom@1504 454 } else if (args[i].equals("-modulepath")) {
ohrstrom@1504 455 if (module_path == -1) {
ohrstrom@1504 456 module_path = j;
ohrstrom@1504 457 out[j] = args[i];
ohrstrom@1504 458 out[j+1] = args[i+1];
ohrstrom@1504 459 j+=2;
ohrstrom@1504 460 i++;
ohrstrom@1504 461 } else {
ohrstrom@1504 462 // Skip this and its argument, but
ohrstrom@1504 463 // append argument to found sourcepath.
ohrstrom@1504 464 out[module_path+1] = out[module_path+1]+File.pathSeparatorChar+args[i+1];
ohrstrom@1504 465 i++;
ohrstrom@1504 466 }
ohrstrom@1504 467 } else {
ohrstrom@1504 468 // Copy argument.
ohrstrom@1504 469 out[j] = args[i];
ohrstrom@1504 470 j++;
ohrstrom@1504 471 }
ohrstrom@1504 472 }
ohrstrom@1504 473 String[] ret = new String[j];
ohrstrom@1504 474 System.arraycopy(out, 0, ret, 0, j);
ohrstrom@1504 475 return ret;
ohrstrom@1504 476 }
ohrstrom@1504 477
ohrstrom@1504 478 /**
ohrstrom@1504 479 * Make sure directory exist, create it if not.
ohrstrom@1504 480 */
ohrstrom@1504 481 private static boolean makeSureExists(File dir) {
ohrstrom@1504 482 // Make sure the dest directories exist.
ohrstrom@1504 483 if (!dir.exists()) {
ohrstrom@1504 484 if (!dir.mkdirs()) {
ohrstrom@1504 485 Log.error("Could not create the directory "+dir.getPath());
ohrstrom@1504 486 return false;
ohrstrom@1504 487 }
ohrstrom@1504 488 }
ohrstrom@1504 489 return true;
ohrstrom@1504 490 }
ohrstrom@1504 491
ohrstrom@1504 492 /**
ohrstrom@1504 493 * Verify that a package pattern is valid.
ohrstrom@1504 494 */
ohrstrom@1504 495 private static void checkPattern(String s) throws ProblemException {
ohrstrom@1504 496 // Package names like foo.bar.gamma are allowed, and
ohrstrom@1504 497 // package names suffixed with .* like foo.bar.* are
ohrstrom@1504 498 // also allowed.
ohrstrom@1504 499 Pattern p = Pattern.compile("[a-zA-Z_]{1}[a-zA-Z0-9_]*(\\.[a-zA-Z_]{1}[a-zA-Z0-9_]*)*(\\.\\*)?+");
ohrstrom@1504 500 Matcher m = p.matcher(s);
ohrstrom@1504 501 if (!m.matches()) {
ohrstrom@1504 502 throw new ProblemException("The string \""+s+"\" is not a proper package name pattern.");
ohrstrom@1504 503 }
ohrstrom@1504 504 }
ohrstrom@1504 505
ohrstrom@1504 506 /**
ohrstrom@1504 507 * Verify that a translate pattern is valid.
ohrstrom@1504 508 */
ohrstrom@1504 509 private static void checkTranslatePattern(String s) throws ProblemException {
ohrstrom@1504 510 // .prop=com.sun.tools.javac.smart.CompileProperties
ohrstrom@1504 511 // .idl=com.sun.corba.CompileIdl
ohrstrom@1504 512 // .g3=antlr.CompileGrammar,debug=true
ohrstrom@1504 513 Pattern p = Pattern.compile(
ohrstrom@1504 514 "\\.[a-zA-Z_]{1}[a-zA-Z0-9_]*=[a-z_]{1}[a-z0-9_]*(\\.[a-z_]{1}[a-z0-9_]*)*"+
ohrstrom@1504 515 "(\\.[a-zA-Z_]{1}[a-zA-Z0-9_]*)(,.*)?");
ohrstrom@1504 516 Matcher m = p.matcher(s);
ohrstrom@1504 517 if (!m.matches()) {
ohrstrom@1504 518 throw new ProblemException("The string \""+s+"\" is not a proper translate pattern.");
ohrstrom@1504 519 }
ohrstrom@1504 520 }
ohrstrom@1504 521
ohrstrom@1504 522 /**
ohrstrom@1504 523 * Verify that a copy pattern is valid.
ohrstrom@1504 524 */
ohrstrom@1504 525 private static void checkCopyPattern(String s) throws ProblemException {
ohrstrom@1504 526 // .gif
ohrstrom@1504 527 // .html
ohrstrom@1504 528 Pattern p = Pattern.compile(
ohrstrom@1504 529 "\\.[a-zA-Z_]{1}[a-zA-Z0-9_]*");
ohrstrom@1504 530 Matcher m = p.matcher(s);
ohrstrom@1504 531 if (!m.matches()) {
ohrstrom@1504 532 throw new ProblemException("The string \""+s+"\" is not a proper suffix.");
ohrstrom@1504 533 }
ohrstrom@1504 534 }
ohrstrom@1504 535
ohrstrom@1504 536 /**
ohrstrom@1504 537 * Verify that a source file name is valid.
ohrstrom@1504 538 */
ohrstrom@1504 539 private static void checkFilePattern(String s) throws ProblemException {
ohrstrom@1504 540 // File names like foo/bar/gamma/Bar.java are allowed,
ohrstrom@1504 541 // as well as /bar/jndi.properties as well as,
ohrstrom@1504 542 // */bar/Foo.java
ohrstrom@1504 543 Pattern p = null;
ohrstrom@1504 544 if (File.separatorChar == '\\') {
ohrstrom@1504 545 p = Pattern.compile("\\*?(.+\\\\)*.+");
ohrstrom@1504 546 }
ohrstrom@1504 547 else if (File.separatorChar == '/') {
ohrstrom@1504 548 p = Pattern.compile("\\*?(.+/)*.+");
ohrstrom@1504 549 } else {
ohrstrom@1504 550 throw new ProblemException("This platform uses the unsupported "+File.separatorChar+
ohrstrom@1504 551 " as file separator character. Please add support for it!");
ohrstrom@1504 552 }
ohrstrom@1504 553 Matcher m = p.matcher(s);
ohrstrom@1504 554 if (!m.matches()) {
ohrstrom@1504 555 throw new ProblemException("The string \""+s+"\" is not a proper file name.");
ohrstrom@1504 556 }
ohrstrom@1504 557 }
ohrstrom@1504 558
ohrstrom@1504 559 /**
ohrstrom@1504 560 * Scan the arguments to find an option is used.
ohrstrom@1504 561 */
ohrstrom@1504 562 private static boolean hasOption(String[] args, String option) {
ohrstrom@1504 563 for (String a : args) {
ohrstrom@1504 564 if (a.equals(option)) return true;
ohrstrom@1504 565 }
ohrstrom@1504 566 return false;
ohrstrom@1504 567 }
ohrstrom@1504 568
ohrstrom@1504 569 /**
ohrstrom@1504 570 * Check if -implicit is supplied, if so check that it is none.
ohrstrom@1504 571 * If -implicit is not supplied, supply -implicit:none
ohrstrom@1504 572 * Only implicit:none is allowed because otherwise the multicore compilations
ohrstrom@1504 573 * and dependency tracking will be tangled up.
ohrstrom@1504 574 */
ohrstrom@1504 575 private static String[] verifyImplicitOption(String[] args)
ohrstrom@1504 576 throws ProblemException {
ohrstrom@1504 577
ohrstrom@1504 578 boolean foundImplicit = false;
ohrstrom@1504 579 for (String a : args) {
ohrstrom@1504 580 if (a.startsWith("-implicit:")) {
ohrstrom@1504 581 foundImplicit = true;
ohrstrom@1504 582 if (!a.equals("-implicit:none")) {
ohrstrom@1504 583 throw new ProblemException("The only allowed setting for sjavac is -implicit:none, it is also the default.");
ohrstrom@1504 584 }
ohrstrom@1504 585 }
ohrstrom@1504 586 }
ohrstrom@1504 587 if (foundImplicit) {
ohrstrom@1504 588 return args;
ohrstrom@1504 589 }
ohrstrom@1504 590 // -implicit:none not found lets add it.
ohrstrom@1504 591 String[] newargs = new String[args.length+1];
ohrstrom@1504 592 System.arraycopy(args,0, newargs, 0, args.length);
ohrstrom@1504 593 newargs[args.length] = "-implicit:none";
ohrstrom@1504 594 return newargs;
ohrstrom@1504 595 }
ohrstrom@1504 596
ohrstrom@1504 597 /**
ohrstrom@1504 598 * Rewrite a single option into something else.
ohrstrom@1504 599 */
ohrstrom@1504 600 private static void rewriteOptions(String[] args, String option, String new_option) {
ohrstrom@1504 601 for (int i=0; i<args.length; ++i) {
ohrstrom@1504 602 if (args[i].equals(option)) {
ohrstrom@1504 603 args[i] = new_option;
ohrstrom@1504 604 }
ohrstrom@1504 605 }
ohrstrom@1504 606 }
ohrstrom@1504 607
ohrstrom@1504 608 /**
ohrstrom@1504 609 * Scan the arguments to find an option that specifies a directory.
ohrstrom@1504 610 * Create the directory if necessary.
ohrstrom@1504 611 */
ohrstrom@1504 612 private static File findDirectoryOption(String[] args, String option, String name, boolean needed, boolean allow_dups, boolean create)
ohrstrom@1504 613 throws ProblemException, ProblemException {
ohrstrom@1504 614 File dir = null;
ohrstrom@1504 615 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 616 if (args[i].equals(option)) {
ohrstrom@1504 617 if (dir != null) {
ohrstrom@1504 618 throw new ProblemException("You have already specified the "+name+" dir!");
ohrstrom@1504 619 }
ohrstrom@1504 620 if (i+1 >= args.length) {
ohrstrom@1504 621 throw new ProblemException("You have to specify a directory following "+option+".");
ohrstrom@1504 622 }
ohrstrom@1504 623 if (args[i+1].indexOf(File.pathSeparatorChar) != -1) {
ohrstrom@1504 624 throw new ProblemException("You must only specify a single directory for "+option+".");
ohrstrom@1504 625 }
ohrstrom@1504 626 dir = new File(args[i+1]);
ohrstrom@1504 627 if (!dir.exists()) {
ohrstrom@1504 628 if (!create) {
ohrstrom@1504 629 throw new ProblemException("This directory does not exist: "+dir.getPath());
ohrstrom@1504 630 } else
ohrstrom@1504 631 if (!makeSureExists(dir)) {
ohrstrom@1504 632 throw new ProblemException("Cannot create directory "+dir.getPath());
ohrstrom@1504 633 }
ohrstrom@1504 634 }
ohrstrom@1504 635 if (!dir.isDirectory()) {
ohrstrom@1504 636 throw new ProblemException("\""+args[i+1]+"\" is not a directory.");
ohrstrom@1504 637 }
ohrstrom@1504 638 }
ohrstrom@1504 639 }
ohrstrom@1504 640 if (dir == null && needed) {
ohrstrom@1504 641 throw new ProblemException("You have to specify "+option);
ohrstrom@1504 642 }
ohrstrom@1504 643 try {
ohrstrom@1504 644 if (dir != null)
ohrstrom@1504 645 return dir.getCanonicalFile();
ohrstrom@1504 646 } catch (IOException e) {
ohrstrom@1504 647 throw new ProblemException(""+e);
ohrstrom@1504 648 }
ohrstrom@1504 649 return null;
ohrstrom@1504 650 }
ohrstrom@1504 651
ohrstrom@1504 652 /**
ohrstrom@1504 653 * Option is followed by path.
ohrstrom@1504 654 */
ohrstrom@1504 655 private static boolean shouldBeFollowedByPath(String o) {
ohrstrom@1504 656 return o.equals("-s") ||
ohrstrom@1504 657 o.equals("-h") ||
ohrstrom@1504 658 o.equals("-d") ||
ohrstrom@1504 659 o.equals("-sourcepath") ||
ohrstrom@1504 660 o.equals("-classpath") ||
ohrstrom@1625 661 o.equals("-cp") ||
ohrstrom@1504 662 o.equals("-bootclasspath") ||
ohrstrom@1504 663 o.equals("-src");
ohrstrom@1504 664 }
ohrstrom@1504 665
ohrstrom@1504 666 /**
ohrstrom@1504 667 * Add -src before source root directories if not already there.
ohrstrom@1504 668 */
ohrstrom@1504 669 private static String[] addSrcBeforeDirectories(String[] args) {
ohrstrom@1504 670 List<String> newargs = new ArrayList<String>();
ohrstrom@1504 671 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 672 File dir = new File(args[i]);
ohrstrom@1504 673 if (dir.exists() && dir.isDirectory()) {
ohrstrom@1504 674 if (i == 0 || !shouldBeFollowedByPath(args[i-1])) {
ohrstrom@1504 675 newargs.add("-src");
ohrstrom@1504 676 }
ohrstrom@1504 677 }
ohrstrom@1504 678 newargs.add(args[i]);
ohrstrom@1504 679 }
ohrstrom@1504 680 return newargs.toArray(new String[0]);
ohrstrom@1504 681 }
ohrstrom@1504 682
ohrstrom@1504 683 /**
ohrstrom@1504 684 * Check the -src options.
ohrstrom@1504 685 */
ohrstrom@1504 686 private static void checkSrcOption(String[] args)
ohrstrom@1504 687 throws ProblemException {
ohrstrom@1504 688 Set<File> dirs = new HashSet<File>();
ohrstrom@1504 689 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 690 if (args[i].equals("-src")) {
ohrstrom@1504 691 if (i+1 >= args.length) {
ohrstrom@1504 692 throw new ProblemException("You have to specify a directory following -src.");
ohrstrom@1504 693 }
ohrstrom@1504 694 StringTokenizer st = new StringTokenizer(args[i+1], File.pathSeparator);
ohrstrom@1504 695 while (st.hasMoreElements()) {
ohrstrom@1504 696 File dir = new File(st.nextToken());
ohrstrom@1504 697 if (!dir.exists()) {
ohrstrom@1504 698 throw new ProblemException("This directory does not exist: "+dir.getPath());
ohrstrom@1504 699 }
ohrstrom@1504 700 if (!dir.isDirectory()) {
ohrstrom@1504 701 throw new ProblemException("\""+dir.getPath()+"\" is not a directory.");
ohrstrom@1504 702 }
ohrstrom@1504 703 if (dirs.contains(dir)) {
ohrstrom@1504 704 throw new ProblemException("The src directory \""+dir.getPath()+"\" is specified more than once!");
ohrstrom@1504 705 }
ohrstrom@1504 706 dirs.add(dir);
ohrstrom@1504 707 }
ohrstrom@1504 708 }
ohrstrom@1504 709 }
ohrstrom@1504 710 if (dirs.isEmpty()) {
ohrstrom@1504 711 throw new ProblemException("You have to specify -src.");
ohrstrom@1504 712 }
ohrstrom@1504 713 }
ohrstrom@1504 714
ohrstrom@1504 715 /**
ohrstrom@1504 716 * Scan the arguments to find an option that specifies a file.
ohrstrom@1504 717 */
ohrstrom@1504 718 private static File findFileOption(String[] args, String option, String name, boolean needed)
ohrstrom@1504 719 throws ProblemException, ProblemException {
ohrstrom@1504 720 File file = null;
ohrstrom@1504 721 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 722 if (args[i].equals(option)) {
ohrstrom@1504 723 if (file != null) {
ohrstrom@1504 724 throw new ProblemException("You have already specified the "+name+" file!");
ohrstrom@1504 725 }
ohrstrom@1504 726 if (i+1 >= args.length) {
ohrstrom@1504 727 throw new ProblemException("You have to specify a file following "+option+".");
ohrstrom@1504 728 }
ohrstrom@1504 729 file = new File(args[i+1]);
ohrstrom@1504 730 if (file.isDirectory()) {
ohrstrom@1504 731 throw new ProblemException("\""+args[i+1]+"\" is not a file.");
ohrstrom@1504 732 }
ohrstrom@1504 733 if (!file.exists() && needed) {
ohrstrom@1504 734 throw new ProblemException("The file \""+args[i+1]+"\" does not exist.");
ohrstrom@1504 735 }
ohrstrom@1504 736
ohrstrom@1504 737 }
ohrstrom@1504 738 }
ohrstrom@1504 739 if (file == null && needed) {
ohrstrom@1504 740 throw new ProblemException("You have to specify "+option);
ohrstrom@1504 741 }
ohrstrom@1504 742 return file;
ohrstrom@1504 743 }
ohrstrom@1504 744
ohrstrom@1504 745 /**
ohrstrom@1504 746 * Look for a specific switch, return true if found.
ohrstrom@1504 747 */
ohrstrom@1504 748 public static boolean findBooleanOption(String[] args, String option) {
ohrstrom@1504 749 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 750 if (args[i].equals(option)) return true;
ohrstrom@1504 751 }
ohrstrom@1504 752 return false;
ohrstrom@1504 753 }
ohrstrom@1504 754
ohrstrom@1504 755 /**
ohrstrom@1504 756 * Scan the arguments to find an option that specifies a number.
ohrstrom@1504 757 */
ohrstrom@1504 758 public static int findNumberOption(String[] args, String option) {
ohrstrom@1504 759 int rc = 0;
ohrstrom@1504 760 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 761 if (args[i].equals(option)) {
ohrstrom@1504 762 if (args.length > i+1) {
ohrstrom@1504 763 rc = Integer.parseInt(args[i+1]);
ohrstrom@1504 764 }
ohrstrom@1504 765 }
ohrstrom@1504 766 }
ohrstrom@1504 767 return rc;
ohrstrom@1504 768 }
ohrstrom@1504 769
ohrstrom@1504 770 /**
ohrstrom@1504 771 * Scan the arguments to find the option (-tr) that setup translation rules to java source
ohrstrom@1504 772 * from different sources. For example: .properties are translated using CompileProperties
ohrstrom@1504 773 * The found translators are stored as suffix rules.
ohrstrom@1504 774 */
ohrstrom@1504 775 private static void findTranslateOptions(String[] args, Map<String,Transformer> suffix_rules)
ohrstrom@1504 776 throws ProblemException, ProblemException {
ohrstrom@1504 777
ohrstrom@1504 778 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 779 if (args[i].equals("-tr")) {
ohrstrom@1504 780 if (i+1 >= args.length) {
ohrstrom@1504 781 throw new ProblemException("You have to specify a translate rule following -tr.");
ohrstrom@1504 782 }
ohrstrom@1504 783 String s = args[i+1];
ohrstrom@1504 784 checkTranslatePattern(s);
ohrstrom@1504 785 int ep = s.indexOf("=");
ohrstrom@1504 786 String suffix = s.substring(0,ep);
ohrstrom@1504 787 String classname = s.substring(ep+1);
ohrstrom@1504 788 if (suffix_rules.get(suffix) != null) {
ohrstrom@1504 789 throw new ProblemException("You have already specified a "+
ohrstrom@1504 790 "rule for the suffix "+suffix);
ohrstrom@1504 791 }
ohrstrom@1504 792 if (s.equals(".class")) {
ohrstrom@1504 793 throw new ProblemException("You cannot have a translator for .class files!");
ohrstrom@1504 794 }
ohrstrom@1504 795 if (s.equals(".java")) {
ohrstrom@1504 796 throw new ProblemException("You cannot have a translator for .java files!");
ohrstrom@1504 797 }
ohrstrom@1504 798 String extra = null;
ohrstrom@1504 799 int exp = classname.indexOf(",");
ohrstrom@1504 800 if (exp != -1) {
ohrstrom@1504 801 extra = classname.substring(exp+1);
ohrstrom@1504 802 classname = classname.substring(0,exp);
ohrstrom@1504 803 }
ohrstrom@1504 804 try {
ohrstrom@1504 805 Class<?> cl = Class.forName(classname);
ohrstrom@1504 806 Transformer t = (Transformer)cl.newInstance();
ohrstrom@1504 807 t.setExtra(extra);
ohrstrom@1504 808 suffix_rules.put(suffix, t);
ohrstrom@1504 809 }
ohrstrom@1504 810 catch (Exception e) {
ohrstrom@1504 811 throw new ProblemException("Cannot use "+classname+" as a translator!");
ohrstrom@1504 812 }
ohrstrom@1504 813 }
ohrstrom@1504 814 }
ohrstrom@1504 815 }
ohrstrom@1504 816
ohrstrom@1504 817 /**
ohrstrom@1504 818 * Scan the arguments to find the option (-copy) that setup copying rules into the bin dir.
ohrstrom@1504 819 * For example: -copy .html
ohrstrom@1504 820 * The found copiers are stored as suffix rules as well. No translation is done, just copying.
ohrstrom@1504 821 */
ohrstrom@1504 822 private void findCopyOptions(String[] args, Map<String,Transformer> suffix_rules)
ohrstrom@1504 823 throws ProblemException, ProblemException {
ohrstrom@1504 824
ohrstrom@1504 825 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 826 if (args[i].equals("-copy")) {
ohrstrom@1504 827 if (i+1 >= args.length) {
ohrstrom@1504 828 throw new ProblemException("You have to specify a translate rule following -tr.");
ohrstrom@1504 829 }
ohrstrom@1504 830 String s = args[i+1];
ohrstrom@1504 831 checkCopyPattern(s);
ohrstrom@1504 832 if (suffix_rules.get(s) != null) {
ohrstrom@1504 833 throw new ProblemException("You have already specified a "+
ohrstrom@1504 834 "rule for the suffix "+s);
ohrstrom@1504 835 }
ohrstrom@1504 836 if (s.equals(".class")) {
ohrstrom@1504 837 throw new ProblemException("You cannot have a copy rule for .class files!");
ohrstrom@1504 838 }
ohrstrom@1504 839 if (s.equals(".java")) {
ohrstrom@1504 840 throw new ProblemException("You cannot have a copy rule for .java files!");
ohrstrom@1504 841 }
ohrstrom@1504 842 suffix_rules.put(s, javac_state.getCopier());
ohrstrom@1504 843 }
ohrstrom@1504 844 }
ohrstrom@1504 845 }
ohrstrom@1504 846
ohrstrom@1504 847 /**
ohrstrom@1504 848 * Rewrite a / separated path into \ separated, but only
ohrstrom@1504 849 * if we are running on a platform were File.separatorChar=='\', ie winapi.
ohrstrom@1504 850 */
ohrstrom@1504 851 private String fixupSeparator(String p) {
ohrstrom@1504 852 if (File.separatorChar == '/') return p;
ohrstrom@1504 853 return p.replaceAll("/", "\\\\");
ohrstrom@1504 854 }
ohrstrom@1504 855
ohrstrom@1504 856 /**
ohrstrom@1504 857 * Scan the arguments for -i -x -xf -if followed by the option
ohrstrom@1504 858 * -src, -sourcepath, -modulepath or -classpath and produce a map of all the
ohrstrom@1504 859 * files to referenced for that particular option.
ohrstrom@1504 860 *
ohrstrom@1504 861 * Store the found sources and the found modules in the supplied maps.
ohrstrom@1504 862 */
ohrstrom@1504 863 private boolean findFiles(String[] args, String option, Set<String> suffixes,
ohrstrom@1504 864 Map<String,Source> found_files, Map<String, Module> found_modules,
ohrstrom@1504 865 Module current_module, boolean inLinksrc)
ohrstrom@1504 866 throws ProblemException, ProblemException
ohrstrom@1504 867 {
ohrstrom@1504 868 // Track which source roots, source path roots and class path roots have been added.
ohrstrom@1504 869 Set<File> roots = new HashSet<File>();
ohrstrom@1504 870 // Track the current set of package includes,excludes as well as excluded source files,
ohrstrom@1504 871 // to be used in the next -src/-sourcepath/-classpath
ohrstrom@1504 872 List<String> includes = new LinkedList<String>();
ohrstrom@1504 873 List<String> excludes = new LinkedList<String>();
ohrstrom@1504 874 List<String> excludefiles = new LinkedList<String>();
ohrstrom@1504 875 List<String> includefiles = new LinkedList<String>();
ohrstrom@1504 876 // This include is used to find all modules in the source.
ohrstrom@1504 877 List<String> moduleinfo = new LinkedList<String>();
ohrstrom@1504 878 moduleinfo.add("module-info.java");
ohrstrom@1504 879
ohrstrom@1504 880 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 881 if (args[i].equals("-i")) {
ohrstrom@1504 882 if (i+1 >= args.length) {
ohrstrom@1504 883 throw new ProblemException("You have to specify a package pattern following -i");
ohrstrom@1504 884 }
ohrstrom@1504 885 String incl = args[i+1];
ohrstrom@1504 886 checkPattern(incl);
ohrstrom@1504 887 includes.add(incl);
ohrstrom@1504 888 }
ohrstrom@1504 889 if (args[i].equals("-x")) {
ohrstrom@1504 890 if (i+1 >= args.length) {
ohrstrom@1504 891 throw new ProblemException("You have to specify a package pattern following -x");
ohrstrom@1504 892 }
ohrstrom@1504 893 String excl = args[i+1];
ohrstrom@1504 894 checkPattern(excl);
ohrstrom@1504 895 excludes.add(excl);
ohrstrom@1504 896 }
ohrstrom@1504 897 if (args[i].equals("-xf")) {
ohrstrom@1504 898 if (i+1 >= args.length) {
ohrstrom@1504 899 throw new ProblemException("You have to specify a file following -xf");
ohrstrom@1504 900 }
ohrstrom@1504 901 String exclf = args[i+1];
ohrstrom@1504 902 checkFilePattern(exclf);
ohrstrom@1504 903 exclf = Util.normalizeDriveLetter(exclf);
ohrstrom@1504 904 excludefiles.add(fixupSeparator(exclf));
ohrstrom@1504 905 }
ohrstrom@1504 906 if (args[i].equals("-if")) {
ohrstrom@1504 907 if (i+1 >= args.length) {
ohrstrom@1504 908 throw new ProblemException("You have to specify a file following -xf");
ohrstrom@1504 909 }
ohrstrom@1504 910 String inclf = args[i+1];
ohrstrom@1504 911 checkFilePattern(inclf);
ohrstrom@1504 912 inclf = Util.normalizeDriveLetter(inclf);
ohrstrom@1504 913 includefiles.add(fixupSeparator(inclf));
ohrstrom@1504 914 }
ohrstrom@1504 915 if (args[i].equals(option)) {
ohrstrom@1504 916 if (i+1 >= args.length) {
ohrstrom@1504 917 throw new ProblemException("You have to specify a directory following "+option);
ohrstrom@1504 918 }
ohrstrom@1504 919 String[] root_dirs = args[i+1].split(File.pathSeparator);
ohrstrom@1504 920 for (String r : root_dirs) {
ohrstrom@1504 921 File root = new File(r);
ohrstrom@1504 922 if (!root.isDirectory()) {
ohrstrom@1504 923 throw new ProblemException("\""+r+"\" is not a directory.");
ohrstrom@1504 924 }
ohrstrom@1504 925 try {
ohrstrom@1504 926 root = root.getCanonicalFile();
ohrstrom@1504 927 } catch (IOException e) {
ohrstrom@1504 928 throw new ProblemException(""+e);
ohrstrom@1504 929 }
ohrstrom@1504 930 if (roots.contains(root)) {
ohrstrom@1504 931 throw new ProblemException("\""+r+"\" has already been used for "+option);
ohrstrom@1504 932 }
jjg@1648 933 if (root.equals(bin_dir)) {
ohrstrom@1504 934 throw new ProblemException("\""+r+"\" cannot be used both for "+option+" and -d");
ohrstrom@1504 935 }
jjg@1648 936 if (root.equals(gensrc_dir)) {
ohrstrom@1504 937 throw new ProblemException("\""+r+"\" cannot be used both for "+option+" and -s");
ohrstrom@1504 938 }
jjg@1648 939 if (root.equals(header_dir)) {
ohrstrom@1504 940 throw new ProblemException("\""+r+"\" cannot be used both for "+option+" and -h");
ohrstrom@1504 941 }
ohrstrom@1504 942 roots.add(root);
ohrstrom@1504 943 Source.scanRoot(root, suffixes, excludes, includes, excludefiles, includefiles,
ohrstrom@1504 944 found_files, found_modules, current_module,
ohrstrom@1504 945 findBooleanOption(args, "--permit-sources-without-package"),
ohrstrom@1504 946 false, inLinksrc);
ohrstrom@1504 947 }
ohrstrom@1504 948 }
ohrstrom@1504 949 if (args[i].equals("-src") ||
ohrstrom@1504 950 args[i].equals("-sourcepath") ||
ohrstrom@1504 951 args[i].equals("-modulepath") ||
ohrstrom@1625 952 args[i].equals("-classpath") ||
ohrstrom@1625 953 args[i].equals("-cp"))
ohrstrom@1504 954 {
ohrstrom@1504 955 // Reset the includes,excludes and excludefiles after they have been used.
ohrstrom@1504 956 includes = new LinkedList<String>();
ohrstrom@1504 957 excludes = new LinkedList<String>();
ohrstrom@1504 958 excludefiles = new LinkedList<String>();
ohrstrom@1504 959 includefiles = new LinkedList<String>();
ohrstrom@1504 960 }
ohrstrom@1504 961 }
ohrstrom@1504 962 return true;
ohrstrom@1504 963 }
ohrstrom@1504 964
ohrstrom@1504 965 }
ohrstrom@1504 966

mercurial