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

Thu, 15 Aug 2013 17:24:35 +0200

author
erikj
date
Thu, 15 Aug 2013 17:24:35 +0200
changeset 1953
71b0089b146f
parent 1648
a03c4a86ea2b
child 2039
0cfd5baa7154
permissions
-rw-r--r--

8015145: Smartjavac needs more flexibility with linking to sources
Reviewed-by: jjg, ohrstrom

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
erikj@1953 252 // Create a map of all source files that are available for linking. Both -src and
erikj@1953 253 // -sourcepath point to such files. It is possible to specify multiple
erikj@1953 254 // -sourcepath options to enable different filtering rules. If the
erikj@1953 255 // filters are the same for multiple sourcepaths, they may be concatenated
erikj@1953 256 // using :(;). Before sending the list of sourcepaths to javac, they are
erikj@1953 257 // all concatenated. The list created here is used by the SmartFileWrapper to
erikj@1953 258 // make sure only the correct sources are actually available.
ohrstrom@1504 259 // We might find more modules here as well.
ohrstrom@1504 260 Map<String,Source> sources_to_link_to = new HashMap<String,Source>();
erikj@1953 261 findFiles(args, "-src", Util.set(".java"), sources_to_link_to, modules, current_module, true);
ohrstrom@1504 262 findFiles(args, "-sourcepath", Util.set(".java"), sources_to_link_to, modules, current_module, true);
erikj@1953 263 // Rewrite the -src option to make it through to the javac instances.
erikj@1953 264 rewriteOptions(args, "-src", "-sourcepath");
ohrstrom@1504 265
ohrstrom@1504 266 // Find all class files allowable for linking.
ohrstrom@1504 267 // And pickup knowledge of all modules found here.
ohrstrom@1504 268 // This cannot currently filter classes inside jar files.
jjg@1648 269 // Map<String,Source> classes_to_link_to = new HashMap<String,Source>();
ohrstrom@1504 270 // findFiles(args, "-classpath", Util.set(".class"), classes_to_link_to, modules, current_module, true);
ohrstrom@1504 271
ohrstrom@1504 272 // Find all module sources allowable for linking.
jjg@1648 273 // Map<String,Source> modules_to_link_to = new HashMap<String,Source>();
jjg@1648 274 // findFiles(args, "-modulepath", Util.set(".class"), modules_to_link_to, modules, current_module, true);
ohrstrom@1504 275
ohrstrom@1504 276 // Add the set of sources to the build database.
ohrstrom@1504 277 javac_state.now().collectPackagesSourcesAndArtifacts(modules);
ohrstrom@1504 278 javac_state.now().checkInternalState("checking sources", false, sources);
ohrstrom@1504 279 javac_state.now().checkInternalState("checking linked sources", true, sources_to_link_to);
ohrstrom@1504 280 javac_state.setVisibleSources(sources_to_link_to);
ohrstrom@1504 281
ohrstrom@1504 282 // If there is any change in the source files, taint packages
ohrstrom@1504 283 // and mark the database in need of saving.
ohrstrom@1504 284 javac_state.checkSourceStatus(false);
ohrstrom@1504 285
ohrstrom@1504 286 // Find all existing artifacts. Their timestamp will match the last modified timestamps stored
ohrstrom@1504 287 // in javac_state, simply because loading of the JavacState will clean out all artifacts
ohrstrom@1504 288 // that do not match the javac_state database.
ohrstrom@1504 289 javac_state.findAllArtifacts();
ohrstrom@1504 290
ohrstrom@1504 291 // Remove unidentified artifacts from the bin, gensrc and header dirs.
ohrstrom@1504 292 // (Unless we allow them to be there.)
ohrstrom@1504 293 // I.e. artifacts that are not known according to the build database (javac_state).
ohrstrom@1504 294 // For examples, files that have been manually copied into these dirs.
ohrstrom@1504 295 // Artifacts with bad timestamps (ie the on disk timestamp does not match the timestamp
ohrstrom@1504 296 // in javac_state) have already been removed when the javac_state was loaded.
ohrstrom@1504 297 if (!findBooleanOption(args, "--permit-unidentified-artifacts")) {
ohrstrom@1504 298 javac_state.removeUnidentifiedArtifacts();
ohrstrom@1504 299 }
ohrstrom@1504 300 // Go through all sources and taint all packages that miss artifacts.
ohrstrom@1504 301 javac_state.taintPackagesThatMissArtifacts();
ohrstrom@1504 302
ohrstrom@1504 303 // Now clean out all known artifacts belonging to tainted packages.
ohrstrom@1504 304 javac_state.deleteClassArtifactsInTaintedPackages();
ohrstrom@1504 305 // Copy files, for example property files, images files, xml files etc etc.
ohrstrom@1504 306 javac_state.performCopying(bin_dir, suffix_rules);
ohrstrom@1504 307 // Translate files, for example compile properties or compile idls.
ohrstrom@1504 308 javac_state.performTranslation(gensrc_dir, suffix_rules);
ohrstrom@1504 309 // Add any potentially generated java sources to the tobe compiled list.
ohrstrom@1504 310 // (Generated sources must always have a package.)
ohrstrom@1504 311 Map<String,Source> generated_sources = new HashMap<String,Source>();
ohrstrom@1504 312 Source.scanRoot(gensrc_dir, Util.set(".java"), null, null, null, null,
ohrstrom@1504 313 generated_sources, modules, current_module, false, true, false);
ohrstrom@1504 314 javac_state.now().collectPackagesSourcesAndArtifacts(modules);
ohrstrom@1504 315 // Recheck the the source files and their timestamps again.
ohrstrom@1504 316 javac_state.checkSourceStatus(true);
ohrstrom@1504 317
ohrstrom@1504 318 // Now do a safety check that the list of source files is identical
ohrstrom@1504 319 // to the list Make believes we are compiling. If we do not get this
ohrstrom@1504 320 // right, then incremental builds will fail with subtility.
ohrstrom@1504 321 // If any difference is detected, then we will fail hard here.
ohrstrom@1504 322 // This is an important safety net.
ohrstrom@1504 323 javac_state.compareWithMakefileList(makefile_source_list);
ohrstrom@1504 324
ohrstrom@1504 325 // Do the compilations, repeatedly until no tainted packages exist.
ohrstrom@1504 326 boolean again;
ohrstrom@1504 327 // Collect the name of all compiled packages.
ohrstrom@1504 328 Set<String> recently_compiled = new HashSet<String>();
ohrstrom@1504 329 boolean[] rc = new boolean[1];
ohrstrom@1504 330 do {
ohrstrom@1504 331 // Clean out artifacts in tainted packages.
ohrstrom@1504 332 javac_state.deleteClassArtifactsInTaintedPackages();
ohrstrom@1504 333 again = javac_state.performJavaCompilations(bin_dir, server_settings, args, recently_compiled, rc);
ohrstrom@1504 334 if (!rc[0]) break;
ohrstrom@1504 335 } while (again);
ohrstrom@1504 336 // Only update the state if the compile went well.
ohrstrom@1504 337 if (rc[0]) {
ohrstrom@1504 338 javac_state.save();
ohrstrom@1504 339 // Collect all the artifacts.
ohrstrom@1504 340 javac_state.now().collectArtifacts(modules);
ohrstrom@1504 341 // Remove artifacts that were generated during the last compile, but not this one.
ohrstrom@1504 342 javac_state.removeSuperfluousArtifacts(recently_compiled);
ohrstrom@1504 343 }
ohrstrom@1504 344 return rc[0] ? 0 : -1;
ohrstrom@1504 345 } catch (ProblemException e) {
ohrstrom@1504 346 Log.error(e.getMessage());
ohrstrom@1504 347 return -1;
ohrstrom@1504 348 } catch (Exception e) {
ohrstrom@1504 349 e.printStackTrace(err);
ohrstrom@1504 350 return -1;
ohrstrom@1504 351 }
ohrstrom@1504 352 }
ohrstrom@1504 353
ohrstrom@1504 354 /**
ohrstrom@1504 355 * Are java source files passed on the command line?
ohrstrom@1504 356 */
ohrstrom@1504 357 private boolean findJavaSourceFiles(String[] args) {
ohrstrom@1504 358 String prev = "";
ohrstrom@1504 359 for (String s : args) {
ohrstrom@1504 360 if (s.endsWith(".java") && !prev.equals("-xf") && !prev.equals("-if")) {
ohrstrom@1504 361 return true;
ohrstrom@1504 362 }
ohrstrom@1504 363 prev = s;
ohrstrom@1504 364 }
ohrstrom@1504 365 return false;
ohrstrom@1504 366 }
ohrstrom@1504 367
ohrstrom@1504 368 /**
ohrstrom@1504 369 * Is an at file passed on the command line?
ohrstrom@1504 370 */
ohrstrom@1504 371 private boolean findAtFile(String[] args) {
ohrstrom@1504 372 for (String s : args) {
ohrstrom@1504 373 if (s.startsWith("@")) {
ohrstrom@1504 374 return true;
ohrstrom@1504 375 }
ohrstrom@1504 376 }
ohrstrom@1504 377 return false;
ohrstrom@1504 378 }
ohrstrom@1504 379
ohrstrom@1504 380 /**
ohrstrom@1504 381 * Find the log level setting.
ohrstrom@1504 382 */
ohrstrom@1504 383 private String findLogLevel(String[] args) {
ohrstrom@1504 384 for (String s : args) {
ohrstrom@1504 385 if (s.startsWith("--log=") && s.length()>6) {
ohrstrom@1504 386 return s.substring(6);
ohrstrom@1504 387 }
ohrstrom@1504 388 if (s.equals("-verbose")) {
ohrstrom@1504 389 return "info";
ohrstrom@1504 390 }
ohrstrom@1504 391 }
ohrstrom@1504 392 return "info";
ohrstrom@1504 393 }
ohrstrom@1504 394
ohrstrom@1504 395 /**
ohrstrom@1504 396 * Remove smart javac wrapper arguments, before feeding
ohrstrom@1504 397 * the args to the plain javac.
ohrstrom@1504 398 */
ohrstrom@1504 399 static String[] removeWrapperArgs(String[] args) {
ohrstrom@1504 400 String[] out = new String[args.length];
ohrstrom@1504 401 // The first source path index is remembered
ohrstrom@1504 402 // here. So that all following can be concatenated to it.
ohrstrom@1504 403 int source_path = -1;
ohrstrom@1504 404 // The same for class path.
ohrstrom@1504 405 int class_path = -1;
ohrstrom@1504 406 // And module path.
ohrstrom@1504 407 int module_path = -1;
ohrstrom@1504 408 int j = 0;
ohrstrom@1504 409 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 410 if (args[i].equals("-src") ||
ohrstrom@1504 411 args[i].equals("-x") ||
ohrstrom@1504 412 args[i].equals("-i") ||
ohrstrom@1504 413 args[i].equals("-xf") ||
ohrstrom@1504 414 args[i].equals("-if") ||
ohrstrom@1504 415 args[i].equals("-copy") ||
ohrstrom@1504 416 args[i].equals("-tr") ||
ohrstrom@1504 417 args[i].equals("-j")) {
ohrstrom@1504 418 // Just skip it and skip following value
ohrstrom@1504 419 i++;
ohrstrom@1504 420 } else if (args[i].startsWith("--server:")) {
ohrstrom@1504 421 // Just skip it.
ohrstrom@1504 422 } else if (args[i].startsWith("--log=")) {
ohrstrom@1504 423 // Just skip it.
ohrstrom@1504 424 } else if (args[i].equals("--permit-unidentified-artifacts")) {
ohrstrom@1504 425 // Just skip it.
ohrstrom@1504 426 } else if (args[i].equals("--permit-sources-without-package")) {
ohrstrom@1504 427 // Just skip it.
ohrstrom@1504 428 } else if (args[i].equals("--compare-found-sources")) {
ohrstrom@1504 429 // Just skip it and skip verify file name
ohrstrom@1504 430 i++;
ohrstrom@1504 431 } else if (args[i].equals("-sourcepath")) {
ohrstrom@1504 432 if (source_path == -1) {
ohrstrom@1504 433 source_path = j;
ohrstrom@1504 434 out[j] = args[i];
ohrstrom@1504 435 out[j+1] = args[i+1];
ohrstrom@1504 436 j+=2;
ohrstrom@1504 437 i++;
ohrstrom@1504 438 } else {
ohrstrom@1504 439 // Skip this and its argument, but
ohrstrom@1504 440 // append argument to found sourcepath.
ohrstrom@1504 441 out[source_path+1] = out[source_path+1]+File.pathSeparatorChar+args[i+1];
ohrstrom@1504 442 i++;
ohrstrom@1504 443 }
ohrstrom@1625 444 } else if (args[i].equals("-classpath") || args[i].equals("-cp")) {
ohrstrom@1504 445 if (class_path == -1) {
ohrstrom@1504 446 class_path = j;
ohrstrom@1504 447 out[j] = args[i];
ohrstrom@1504 448 out[j+1] = args[i+1];
ohrstrom@1504 449 j+=2;
ohrstrom@1504 450 i++;
ohrstrom@1504 451 } else {
ohrstrom@1504 452 // Skip this and its argument, but
ohrstrom@1504 453 // append argument to found sourcepath.
ohrstrom@1504 454 out[class_path+1] = out[class_path+1]+File.pathSeparatorChar+args[i+1];
ohrstrom@1504 455 i++;
ohrstrom@1504 456 }
ohrstrom@1504 457 } else if (args[i].equals("-modulepath")) {
ohrstrom@1504 458 if (module_path == -1) {
ohrstrom@1504 459 module_path = j;
ohrstrom@1504 460 out[j] = args[i];
ohrstrom@1504 461 out[j+1] = args[i+1];
ohrstrom@1504 462 j+=2;
ohrstrom@1504 463 i++;
ohrstrom@1504 464 } else {
ohrstrom@1504 465 // Skip this and its argument, but
ohrstrom@1504 466 // append argument to found sourcepath.
ohrstrom@1504 467 out[module_path+1] = out[module_path+1]+File.pathSeparatorChar+args[i+1];
ohrstrom@1504 468 i++;
ohrstrom@1504 469 }
ohrstrom@1504 470 } else {
ohrstrom@1504 471 // Copy argument.
ohrstrom@1504 472 out[j] = args[i];
ohrstrom@1504 473 j++;
ohrstrom@1504 474 }
ohrstrom@1504 475 }
ohrstrom@1504 476 String[] ret = new String[j];
ohrstrom@1504 477 System.arraycopy(out, 0, ret, 0, j);
ohrstrom@1504 478 return ret;
ohrstrom@1504 479 }
ohrstrom@1504 480
ohrstrom@1504 481 /**
ohrstrom@1504 482 * Make sure directory exist, create it if not.
ohrstrom@1504 483 */
ohrstrom@1504 484 private static boolean makeSureExists(File dir) {
ohrstrom@1504 485 // Make sure the dest directories exist.
ohrstrom@1504 486 if (!dir.exists()) {
ohrstrom@1504 487 if (!dir.mkdirs()) {
ohrstrom@1504 488 Log.error("Could not create the directory "+dir.getPath());
ohrstrom@1504 489 return false;
ohrstrom@1504 490 }
ohrstrom@1504 491 }
ohrstrom@1504 492 return true;
ohrstrom@1504 493 }
ohrstrom@1504 494
ohrstrom@1504 495 /**
ohrstrom@1504 496 * Verify that a package pattern is valid.
ohrstrom@1504 497 */
ohrstrom@1504 498 private static void checkPattern(String s) throws ProblemException {
ohrstrom@1504 499 // Package names like foo.bar.gamma are allowed, and
ohrstrom@1504 500 // package names suffixed with .* like foo.bar.* are
ohrstrom@1504 501 // also allowed.
ohrstrom@1504 502 Pattern p = Pattern.compile("[a-zA-Z_]{1}[a-zA-Z0-9_]*(\\.[a-zA-Z_]{1}[a-zA-Z0-9_]*)*(\\.\\*)?+");
ohrstrom@1504 503 Matcher m = p.matcher(s);
ohrstrom@1504 504 if (!m.matches()) {
ohrstrom@1504 505 throw new ProblemException("The string \""+s+"\" is not a proper package name pattern.");
ohrstrom@1504 506 }
ohrstrom@1504 507 }
ohrstrom@1504 508
ohrstrom@1504 509 /**
ohrstrom@1504 510 * Verify that a translate pattern is valid.
ohrstrom@1504 511 */
ohrstrom@1504 512 private static void checkTranslatePattern(String s) throws ProblemException {
ohrstrom@1504 513 // .prop=com.sun.tools.javac.smart.CompileProperties
ohrstrom@1504 514 // .idl=com.sun.corba.CompileIdl
ohrstrom@1504 515 // .g3=antlr.CompileGrammar,debug=true
ohrstrom@1504 516 Pattern p = Pattern.compile(
ohrstrom@1504 517 "\\.[a-zA-Z_]{1}[a-zA-Z0-9_]*=[a-z_]{1}[a-z0-9_]*(\\.[a-z_]{1}[a-z0-9_]*)*"+
ohrstrom@1504 518 "(\\.[a-zA-Z_]{1}[a-zA-Z0-9_]*)(,.*)?");
ohrstrom@1504 519 Matcher m = p.matcher(s);
ohrstrom@1504 520 if (!m.matches()) {
ohrstrom@1504 521 throw new ProblemException("The string \""+s+"\" is not a proper translate pattern.");
ohrstrom@1504 522 }
ohrstrom@1504 523 }
ohrstrom@1504 524
ohrstrom@1504 525 /**
ohrstrom@1504 526 * Verify that a copy pattern is valid.
ohrstrom@1504 527 */
ohrstrom@1504 528 private static void checkCopyPattern(String s) throws ProblemException {
ohrstrom@1504 529 // .gif
ohrstrom@1504 530 // .html
ohrstrom@1504 531 Pattern p = Pattern.compile(
ohrstrom@1504 532 "\\.[a-zA-Z_]{1}[a-zA-Z0-9_]*");
ohrstrom@1504 533 Matcher m = p.matcher(s);
ohrstrom@1504 534 if (!m.matches()) {
ohrstrom@1504 535 throw new ProblemException("The string \""+s+"\" is not a proper suffix.");
ohrstrom@1504 536 }
ohrstrom@1504 537 }
ohrstrom@1504 538
ohrstrom@1504 539 /**
ohrstrom@1504 540 * Verify that a source file name is valid.
ohrstrom@1504 541 */
ohrstrom@1504 542 private static void checkFilePattern(String s) throws ProblemException {
ohrstrom@1504 543 // File names like foo/bar/gamma/Bar.java are allowed,
ohrstrom@1504 544 // as well as /bar/jndi.properties as well as,
ohrstrom@1504 545 // */bar/Foo.java
ohrstrom@1504 546 Pattern p = null;
ohrstrom@1504 547 if (File.separatorChar == '\\') {
ohrstrom@1504 548 p = Pattern.compile("\\*?(.+\\\\)*.+");
ohrstrom@1504 549 }
ohrstrom@1504 550 else if (File.separatorChar == '/') {
ohrstrom@1504 551 p = Pattern.compile("\\*?(.+/)*.+");
ohrstrom@1504 552 } else {
ohrstrom@1504 553 throw new ProblemException("This platform uses the unsupported "+File.separatorChar+
ohrstrom@1504 554 " as file separator character. Please add support for it!");
ohrstrom@1504 555 }
ohrstrom@1504 556 Matcher m = p.matcher(s);
ohrstrom@1504 557 if (!m.matches()) {
ohrstrom@1504 558 throw new ProblemException("The string \""+s+"\" is not a proper file name.");
ohrstrom@1504 559 }
ohrstrom@1504 560 }
ohrstrom@1504 561
ohrstrom@1504 562 /**
ohrstrom@1504 563 * Scan the arguments to find an option is used.
ohrstrom@1504 564 */
ohrstrom@1504 565 private static boolean hasOption(String[] args, String option) {
ohrstrom@1504 566 for (String a : args) {
ohrstrom@1504 567 if (a.equals(option)) return true;
ohrstrom@1504 568 }
ohrstrom@1504 569 return false;
ohrstrom@1504 570 }
ohrstrom@1504 571
ohrstrom@1504 572 /**
ohrstrom@1504 573 * Check if -implicit is supplied, if so check that it is none.
ohrstrom@1504 574 * If -implicit is not supplied, supply -implicit:none
ohrstrom@1504 575 * Only implicit:none is allowed because otherwise the multicore compilations
ohrstrom@1504 576 * and dependency tracking will be tangled up.
ohrstrom@1504 577 */
ohrstrom@1504 578 private static String[] verifyImplicitOption(String[] args)
ohrstrom@1504 579 throws ProblemException {
ohrstrom@1504 580
ohrstrom@1504 581 boolean foundImplicit = false;
ohrstrom@1504 582 for (String a : args) {
ohrstrom@1504 583 if (a.startsWith("-implicit:")) {
ohrstrom@1504 584 foundImplicit = true;
ohrstrom@1504 585 if (!a.equals("-implicit:none")) {
ohrstrom@1504 586 throw new ProblemException("The only allowed setting for sjavac is -implicit:none, it is also the default.");
ohrstrom@1504 587 }
ohrstrom@1504 588 }
ohrstrom@1504 589 }
ohrstrom@1504 590 if (foundImplicit) {
ohrstrom@1504 591 return args;
ohrstrom@1504 592 }
ohrstrom@1504 593 // -implicit:none not found lets add it.
ohrstrom@1504 594 String[] newargs = new String[args.length+1];
ohrstrom@1504 595 System.arraycopy(args,0, newargs, 0, args.length);
ohrstrom@1504 596 newargs[args.length] = "-implicit:none";
ohrstrom@1504 597 return newargs;
ohrstrom@1504 598 }
ohrstrom@1504 599
ohrstrom@1504 600 /**
ohrstrom@1504 601 * Rewrite a single option into something else.
ohrstrom@1504 602 */
ohrstrom@1504 603 private static void rewriteOptions(String[] args, String option, String new_option) {
ohrstrom@1504 604 for (int i=0; i<args.length; ++i) {
ohrstrom@1504 605 if (args[i].equals(option)) {
ohrstrom@1504 606 args[i] = new_option;
ohrstrom@1504 607 }
ohrstrom@1504 608 }
ohrstrom@1504 609 }
ohrstrom@1504 610
ohrstrom@1504 611 /**
ohrstrom@1504 612 * Scan the arguments to find an option that specifies a directory.
ohrstrom@1504 613 * Create the directory if necessary.
ohrstrom@1504 614 */
ohrstrom@1504 615 private static File findDirectoryOption(String[] args, String option, String name, boolean needed, boolean allow_dups, boolean create)
ohrstrom@1504 616 throws ProblemException, ProblemException {
ohrstrom@1504 617 File dir = null;
ohrstrom@1504 618 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 619 if (args[i].equals(option)) {
ohrstrom@1504 620 if (dir != null) {
ohrstrom@1504 621 throw new ProblemException("You have already specified the "+name+" dir!");
ohrstrom@1504 622 }
ohrstrom@1504 623 if (i+1 >= args.length) {
ohrstrom@1504 624 throw new ProblemException("You have to specify a directory following "+option+".");
ohrstrom@1504 625 }
ohrstrom@1504 626 if (args[i+1].indexOf(File.pathSeparatorChar) != -1) {
ohrstrom@1504 627 throw new ProblemException("You must only specify a single directory for "+option+".");
ohrstrom@1504 628 }
ohrstrom@1504 629 dir = new File(args[i+1]);
ohrstrom@1504 630 if (!dir.exists()) {
ohrstrom@1504 631 if (!create) {
ohrstrom@1504 632 throw new ProblemException("This directory does not exist: "+dir.getPath());
ohrstrom@1504 633 } else
ohrstrom@1504 634 if (!makeSureExists(dir)) {
ohrstrom@1504 635 throw new ProblemException("Cannot create directory "+dir.getPath());
ohrstrom@1504 636 }
ohrstrom@1504 637 }
ohrstrom@1504 638 if (!dir.isDirectory()) {
ohrstrom@1504 639 throw new ProblemException("\""+args[i+1]+"\" is not a directory.");
ohrstrom@1504 640 }
ohrstrom@1504 641 }
ohrstrom@1504 642 }
ohrstrom@1504 643 if (dir == null && needed) {
ohrstrom@1504 644 throw new ProblemException("You have to specify "+option);
ohrstrom@1504 645 }
ohrstrom@1504 646 try {
ohrstrom@1504 647 if (dir != null)
ohrstrom@1504 648 return dir.getCanonicalFile();
ohrstrom@1504 649 } catch (IOException e) {
ohrstrom@1504 650 throw new ProblemException(""+e);
ohrstrom@1504 651 }
ohrstrom@1504 652 return null;
ohrstrom@1504 653 }
ohrstrom@1504 654
ohrstrom@1504 655 /**
ohrstrom@1504 656 * Option is followed by path.
ohrstrom@1504 657 */
ohrstrom@1504 658 private static boolean shouldBeFollowedByPath(String o) {
ohrstrom@1504 659 return o.equals("-s") ||
ohrstrom@1504 660 o.equals("-h") ||
ohrstrom@1504 661 o.equals("-d") ||
ohrstrom@1504 662 o.equals("-sourcepath") ||
ohrstrom@1504 663 o.equals("-classpath") ||
ohrstrom@1625 664 o.equals("-cp") ||
ohrstrom@1504 665 o.equals("-bootclasspath") ||
ohrstrom@1504 666 o.equals("-src");
ohrstrom@1504 667 }
ohrstrom@1504 668
ohrstrom@1504 669 /**
ohrstrom@1504 670 * Add -src before source root directories if not already there.
ohrstrom@1504 671 */
ohrstrom@1504 672 private static String[] addSrcBeforeDirectories(String[] args) {
ohrstrom@1504 673 List<String> newargs = new ArrayList<String>();
ohrstrom@1504 674 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 675 File dir = new File(args[i]);
ohrstrom@1504 676 if (dir.exists() && dir.isDirectory()) {
ohrstrom@1504 677 if (i == 0 || !shouldBeFollowedByPath(args[i-1])) {
ohrstrom@1504 678 newargs.add("-src");
ohrstrom@1504 679 }
ohrstrom@1504 680 }
ohrstrom@1504 681 newargs.add(args[i]);
ohrstrom@1504 682 }
ohrstrom@1504 683 return newargs.toArray(new String[0]);
ohrstrom@1504 684 }
ohrstrom@1504 685
ohrstrom@1504 686 /**
ohrstrom@1504 687 * Check the -src options.
ohrstrom@1504 688 */
ohrstrom@1504 689 private static void checkSrcOption(String[] args)
ohrstrom@1504 690 throws ProblemException {
ohrstrom@1504 691 Set<File> dirs = new HashSet<File>();
ohrstrom@1504 692 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 693 if (args[i].equals("-src")) {
ohrstrom@1504 694 if (i+1 >= args.length) {
ohrstrom@1504 695 throw new ProblemException("You have to specify a directory following -src.");
ohrstrom@1504 696 }
ohrstrom@1504 697 StringTokenizer st = new StringTokenizer(args[i+1], File.pathSeparator);
ohrstrom@1504 698 while (st.hasMoreElements()) {
ohrstrom@1504 699 File dir = new File(st.nextToken());
ohrstrom@1504 700 if (!dir.exists()) {
ohrstrom@1504 701 throw new ProblemException("This directory does not exist: "+dir.getPath());
ohrstrom@1504 702 }
ohrstrom@1504 703 if (!dir.isDirectory()) {
ohrstrom@1504 704 throw new ProblemException("\""+dir.getPath()+"\" is not a directory.");
ohrstrom@1504 705 }
ohrstrom@1504 706 if (dirs.contains(dir)) {
ohrstrom@1504 707 throw new ProblemException("The src directory \""+dir.getPath()+"\" is specified more than once!");
ohrstrom@1504 708 }
ohrstrom@1504 709 dirs.add(dir);
ohrstrom@1504 710 }
ohrstrom@1504 711 }
ohrstrom@1504 712 }
ohrstrom@1504 713 if (dirs.isEmpty()) {
ohrstrom@1504 714 throw new ProblemException("You have to specify -src.");
ohrstrom@1504 715 }
ohrstrom@1504 716 }
ohrstrom@1504 717
ohrstrom@1504 718 /**
ohrstrom@1504 719 * Scan the arguments to find an option that specifies a file.
ohrstrom@1504 720 */
ohrstrom@1504 721 private static File findFileOption(String[] args, String option, String name, boolean needed)
ohrstrom@1504 722 throws ProblemException, ProblemException {
ohrstrom@1504 723 File file = null;
ohrstrom@1504 724 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 725 if (args[i].equals(option)) {
ohrstrom@1504 726 if (file != null) {
ohrstrom@1504 727 throw new ProblemException("You have already specified the "+name+" file!");
ohrstrom@1504 728 }
ohrstrom@1504 729 if (i+1 >= args.length) {
ohrstrom@1504 730 throw new ProblemException("You have to specify a file following "+option+".");
ohrstrom@1504 731 }
ohrstrom@1504 732 file = new File(args[i+1]);
ohrstrom@1504 733 if (file.isDirectory()) {
ohrstrom@1504 734 throw new ProblemException("\""+args[i+1]+"\" is not a file.");
ohrstrom@1504 735 }
ohrstrom@1504 736 if (!file.exists() && needed) {
ohrstrom@1504 737 throw new ProblemException("The file \""+args[i+1]+"\" does not exist.");
ohrstrom@1504 738 }
ohrstrom@1504 739
ohrstrom@1504 740 }
ohrstrom@1504 741 }
ohrstrom@1504 742 if (file == null && needed) {
ohrstrom@1504 743 throw new ProblemException("You have to specify "+option);
ohrstrom@1504 744 }
ohrstrom@1504 745 return file;
ohrstrom@1504 746 }
ohrstrom@1504 747
ohrstrom@1504 748 /**
ohrstrom@1504 749 * Look for a specific switch, return true if found.
ohrstrom@1504 750 */
ohrstrom@1504 751 public static boolean findBooleanOption(String[] args, String option) {
ohrstrom@1504 752 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 753 if (args[i].equals(option)) return true;
ohrstrom@1504 754 }
ohrstrom@1504 755 return false;
ohrstrom@1504 756 }
ohrstrom@1504 757
ohrstrom@1504 758 /**
ohrstrom@1504 759 * Scan the arguments to find an option that specifies a number.
ohrstrom@1504 760 */
ohrstrom@1504 761 public static int findNumberOption(String[] args, String option) {
ohrstrom@1504 762 int rc = 0;
ohrstrom@1504 763 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 764 if (args[i].equals(option)) {
ohrstrom@1504 765 if (args.length > i+1) {
ohrstrom@1504 766 rc = Integer.parseInt(args[i+1]);
ohrstrom@1504 767 }
ohrstrom@1504 768 }
ohrstrom@1504 769 }
ohrstrom@1504 770 return rc;
ohrstrom@1504 771 }
ohrstrom@1504 772
ohrstrom@1504 773 /**
ohrstrom@1504 774 * Scan the arguments to find the option (-tr) that setup translation rules to java source
ohrstrom@1504 775 * from different sources. For example: .properties are translated using CompileProperties
ohrstrom@1504 776 * The found translators are stored as suffix rules.
ohrstrom@1504 777 */
ohrstrom@1504 778 private static void findTranslateOptions(String[] args, Map<String,Transformer> suffix_rules)
ohrstrom@1504 779 throws ProblemException, ProblemException {
ohrstrom@1504 780
ohrstrom@1504 781 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 782 if (args[i].equals("-tr")) {
ohrstrom@1504 783 if (i+1 >= args.length) {
ohrstrom@1504 784 throw new ProblemException("You have to specify a translate rule following -tr.");
ohrstrom@1504 785 }
ohrstrom@1504 786 String s = args[i+1];
ohrstrom@1504 787 checkTranslatePattern(s);
ohrstrom@1504 788 int ep = s.indexOf("=");
ohrstrom@1504 789 String suffix = s.substring(0,ep);
ohrstrom@1504 790 String classname = s.substring(ep+1);
ohrstrom@1504 791 if (suffix_rules.get(suffix) != null) {
ohrstrom@1504 792 throw new ProblemException("You have already specified a "+
ohrstrom@1504 793 "rule for the suffix "+suffix);
ohrstrom@1504 794 }
ohrstrom@1504 795 if (s.equals(".class")) {
ohrstrom@1504 796 throw new ProblemException("You cannot have a translator for .class files!");
ohrstrom@1504 797 }
ohrstrom@1504 798 if (s.equals(".java")) {
ohrstrom@1504 799 throw new ProblemException("You cannot have a translator for .java files!");
ohrstrom@1504 800 }
ohrstrom@1504 801 String extra = null;
ohrstrom@1504 802 int exp = classname.indexOf(",");
ohrstrom@1504 803 if (exp != -1) {
ohrstrom@1504 804 extra = classname.substring(exp+1);
ohrstrom@1504 805 classname = classname.substring(0,exp);
ohrstrom@1504 806 }
ohrstrom@1504 807 try {
ohrstrom@1504 808 Class<?> cl = Class.forName(classname);
ohrstrom@1504 809 Transformer t = (Transformer)cl.newInstance();
ohrstrom@1504 810 t.setExtra(extra);
ohrstrom@1504 811 suffix_rules.put(suffix, t);
ohrstrom@1504 812 }
ohrstrom@1504 813 catch (Exception e) {
ohrstrom@1504 814 throw new ProblemException("Cannot use "+classname+" as a translator!");
ohrstrom@1504 815 }
ohrstrom@1504 816 }
ohrstrom@1504 817 }
ohrstrom@1504 818 }
ohrstrom@1504 819
ohrstrom@1504 820 /**
ohrstrom@1504 821 * Scan the arguments to find the option (-copy) that setup copying rules into the bin dir.
ohrstrom@1504 822 * For example: -copy .html
ohrstrom@1504 823 * The found copiers are stored as suffix rules as well. No translation is done, just copying.
ohrstrom@1504 824 */
ohrstrom@1504 825 private void findCopyOptions(String[] args, Map<String,Transformer> suffix_rules)
ohrstrom@1504 826 throws ProblemException, ProblemException {
ohrstrom@1504 827
ohrstrom@1504 828 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 829 if (args[i].equals("-copy")) {
ohrstrom@1504 830 if (i+1 >= args.length) {
ohrstrom@1504 831 throw new ProblemException("You have to specify a translate rule following -tr.");
ohrstrom@1504 832 }
ohrstrom@1504 833 String s = args[i+1];
ohrstrom@1504 834 checkCopyPattern(s);
ohrstrom@1504 835 if (suffix_rules.get(s) != null) {
ohrstrom@1504 836 throw new ProblemException("You have already specified a "+
ohrstrom@1504 837 "rule for the suffix "+s);
ohrstrom@1504 838 }
ohrstrom@1504 839 if (s.equals(".class")) {
ohrstrom@1504 840 throw new ProblemException("You cannot have a copy rule for .class files!");
ohrstrom@1504 841 }
ohrstrom@1504 842 if (s.equals(".java")) {
ohrstrom@1504 843 throw new ProblemException("You cannot have a copy rule for .java files!");
ohrstrom@1504 844 }
ohrstrom@1504 845 suffix_rules.put(s, javac_state.getCopier());
ohrstrom@1504 846 }
ohrstrom@1504 847 }
ohrstrom@1504 848 }
ohrstrom@1504 849
ohrstrom@1504 850 /**
ohrstrom@1504 851 * Rewrite a / separated path into \ separated, but only
ohrstrom@1504 852 * if we are running on a platform were File.separatorChar=='\', ie winapi.
ohrstrom@1504 853 */
ohrstrom@1504 854 private String fixupSeparator(String p) {
ohrstrom@1504 855 if (File.separatorChar == '/') return p;
ohrstrom@1504 856 return p.replaceAll("/", "\\\\");
ohrstrom@1504 857 }
ohrstrom@1504 858
ohrstrom@1504 859 /**
ohrstrom@1504 860 * Scan the arguments for -i -x -xf -if followed by the option
ohrstrom@1504 861 * -src, -sourcepath, -modulepath or -classpath and produce a map of all the
ohrstrom@1504 862 * files to referenced for that particular option.
ohrstrom@1504 863 *
ohrstrom@1504 864 * Store the found sources and the found modules in the supplied maps.
ohrstrom@1504 865 */
ohrstrom@1504 866 private boolean findFiles(String[] args, String option, Set<String> suffixes,
ohrstrom@1504 867 Map<String,Source> found_files, Map<String, Module> found_modules,
ohrstrom@1504 868 Module current_module, boolean inLinksrc)
ohrstrom@1504 869 throws ProblemException, ProblemException
ohrstrom@1504 870 {
ohrstrom@1504 871 // Track which source roots, source path roots and class path roots have been added.
ohrstrom@1504 872 Set<File> roots = new HashSet<File>();
ohrstrom@1504 873 // Track the current set of package includes,excludes as well as excluded source files,
ohrstrom@1504 874 // to be used in the next -src/-sourcepath/-classpath
ohrstrom@1504 875 List<String> includes = new LinkedList<String>();
ohrstrom@1504 876 List<String> excludes = new LinkedList<String>();
ohrstrom@1504 877 List<String> excludefiles = new LinkedList<String>();
ohrstrom@1504 878 List<String> includefiles = new LinkedList<String>();
ohrstrom@1504 879 // This include is used to find all modules in the source.
ohrstrom@1504 880 List<String> moduleinfo = new LinkedList<String>();
ohrstrom@1504 881 moduleinfo.add("module-info.java");
ohrstrom@1504 882
ohrstrom@1504 883 for (int i = 0; i<args.length; ++i) {
ohrstrom@1504 884 if (args[i].equals("-i")) {
ohrstrom@1504 885 if (i+1 >= args.length) {
ohrstrom@1504 886 throw new ProblemException("You have to specify a package pattern following -i");
ohrstrom@1504 887 }
ohrstrom@1504 888 String incl = args[i+1];
ohrstrom@1504 889 checkPattern(incl);
ohrstrom@1504 890 includes.add(incl);
ohrstrom@1504 891 }
ohrstrom@1504 892 if (args[i].equals("-x")) {
ohrstrom@1504 893 if (i+1 >= args.length) {
ohrstrom@1504 894 throw new ProblemException("You have to specify a package pattern following -x");
ohrstrom@1504 895 }
ohrstrom@1504 896 String excl = args[i+1];
ohrstrom@1504 897 checkPattern(excl);
ohrstrom@1504 898 excludes.add(excl);
ohrstrom@1504 899 }
ohrstrom@1504 900 if (args[i].equals("-xf")) {
ohrstrom@1504 901 if (i+1 >= args.length) {
ohrstrom@1504 902 throw new ProblemException("You have to specify a file following -xf");
ohrstrom@1504 903 }
ohrstrom@1504 904 String exclf = args[i+1];
ohrstrom@1504 905 checkFilePattern(exclf);
ohrstrom@1504 906 exclf = Util.normalizeDriveLetter(exclf);
ohrstrom@1504 907 excludefiles.add(fixupSeparator(exclf));
ohrstrom@1504 908 }
ohrstrom@1504 909 if (args[i].equals("-if")) {
ohrstrom@1504 910 if (i+1 >= args.length) {
ohrstrom@1504 911 throw new ProblemException("You have to specify a file following -xf");
ohrstrom@1504 912 }
ohrstrom@1504 913 String inclf = args[i+1];
ohrstrom@1504 914 checkFilePattern(inclf);
ohrstrom@1504 915 inclf = Util.normalizeDriveLetter(inclf);
ohrstrom@1504 916 includefiles.add(fixupSeparator(inclf));
ohrstrom@1504 917 }
ohrstrom@1504 918 if (args[i].equals(option)) {
ohrstrom@1504 919 if (i+1 >= args.length) {
ohrstrom@1504 920 throw new ProblemException("You have to specify a directory following "+option);
ohrstrom@1504 921 }
ohrstrom@1504 922 String[] root_dirs = args[i+1].split(File.pathSeparator);
ohrstrom@1504 923 for (String r : root_dirs) {
ohrstrom@1504 924 File root = new File(r);
ohrstrom@1504 925 if (!root.isDirectory()) {
ohrstrom@1504 926 throw new ProblemException("\""+r+"\" is not a directory.");
ohrstrom@1504 927 }
ohrstrom@1504 928 try {
ohrstrom@1504 929 root = root.getCanonicalFile();
ohrstrom@1504 930 } catch (IOException e) {
ohrstrom@1504 931 throw new ProblemException(""+e);
ohrstrom@1504 932 }
ohrstrom@1504 933 if (roots.contains(root)) {
ohrstrom@1504 934 throw new ProblemException("\""+r+"\" has already been used for "+option);
ohrstrom@1504 935 }
jjg@1648 936 if (root.equals(bin_dir)) {
ohrstrom@1504 937 throw new ProblemException("\""+r+"\" cannot be used both for "+option+" and -d");
ohrstrom@1504 938 }
jjg@1648 939 if (root.equals(gensrc_dir)) {
ohrstrom@1504 940 throw new ProblemException("\""+r+"\" cannot be used both for "+option+" and -s");
ohrstrom@1504 941 }
jjg@1648 942 if (root.equals(header_dir)) {
ohrstrom@1504 943 throw new ProblemException("\""+r+"\" cannot be used both for "+option+" and -h");
ohrstrom@1504 944 }
ohrstrom@1504 945 roots.add(root);
ohrstrom@1504 946 Source.scanRoot(root, suffixes, excludes, includes, excludefiles, includefiles,
ohrstrom@1504 947 found_files, found_modules, current_module,
ohrstrom@1504 948 findBooleanOption(args, "--permit-sources-without-package"),
ohrstrom@1504 949 false, inLinksrc);
ohrstrom@1504 950 }
ohrstrom@1504 951 }
ohrstrom@1504 952 if (args[i].equals("-src") ||
ohrstrom@1504 953 args[i].equals("-sourcepath") ||
ohrstrom@1504 954 args[i].equals("-modulepath") ||
ohrstrom@1625 955 args[i].equals("-classpath") ||
ohrstrom@1625 956 args[i].equals("-cp"))
ohrstrom@1504 957 {
ohrstrom@1504 958 // Reset the includes,excludes and excludefiles after they have been used.
ohrstrom@1504 959 includes = new LinkedList<String>();
ohrstrom@1504 960 excludes = new LinkedList<String>();
ohrstrom@1504 961 excludefiles = new LinkedList<String>();
ohrstrom@1504 962 includefiles = new LinkedList<String>();
ohrstrom@1504 963 }
ohrstrom@1504 964 }
ohrstrom@1504 965 return true;
ohrstrom@1504 966 }
ohrstrom@1504 967
ohrstrom@1504 968 }
ohrstrom@1504 969

mercurial