PageRenderTime 41ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/subprojects/language-java/src/main/java/org/gradle/api/internal/tasks/compile/CommandLineJavaCompilerArgumentsGenerator.java

https://github.com/andrewhj-mn/gradle
Java | 76 lines | 49 code | 7 blank | 20 comment | 5 complexity | 7b69546f03cdc21781e96bb02afc233c MD5 | raw file
  1. /*
  2. * Copyright 2012 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.gradle.api.internal.tasks.compile;
  17. import com.google.common.collect.Iterables;
  18. import org.gradle.api.UncheckedIOException;
  19. import org.gradle.platform.base.internal.toolchain.ArgCollector;
  20. import org.gradle.platform.base.internal.toolchain.ArgWriter;
  21. import java.io.*;
  22. import java.util.Collections;
  23. import java.util.List;
  24. public class CommandLineJavaCompilerArgumentsGenerator implements CompileSpecToArguments<JavaCompileSpec>, Serializable {
  25. public void collectArguments(JavaCompileSpec spec, ArgCollector collector) {
  26. for (String arg : generate(spec)) {
  27. collector.args(arg);
  28. }
  29. }
  30. public Iterable<String> generate(JavaCompileSpec spec) {
  31. List<String> launcherOptions = new JavaCompilerArgumentsBuilder(spec).includeLauncherOptions(true).includeMainOptions(false).includeClasspath(false).build();
  32. List<String> remainingArgs = new JavaCompilerArgumentsBuilder(spec).includeSourceFiles(true).build();
  33. Iterable<String> allArgs = Iterables.concat(launcherOptions, remainingArgs);
  34. if (exceedsWindowsCommandLineLengthLimit(allArgs)) {
  35. return Iterables.concat(launcherOptions, shortenArgs(spec.getTempDir(), remainingArgs));
  36. }
  37. return allArgs;
  38. }
  39. private boolean exceedsWindowsCommandLineLengthLimit(Iterable<String> args) {
  40. int length = 0;
  41. for (String arg : args) {
  42. length += arg.length() + 1;
  43. // limit is 2047 on older Windows systems, and 8191 on newer ones
  44. // http://support.microsoft.com/kb/830473
  45. // let's play it safe, no need to optimize
  46. if (length > 1500) { return true; }
  47. }
  48. return false;
  49. }
  50. private Iterable<String> shortenArgs(File tempDir, List<String> args) {
  51. File file = new File(tempDir, "java-compiler-args.txt");
  52. // for command file format, see http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html#commandlineargfile
  53. // use platform character and line encoding
  54. try {
  55. PrintWriter writer = new PrintWriter(new FileWriter(file));
  56. try {
  57. ArgWriter argWriter = ArgWriter.unixStyle(writer);
  58. for (String arg : args) {
  59. argWriter.args(arg);
  60. }
  61. } finally {
  62. writer.close();
  63. }
  64. } catch (IOException e) {
  65. throw new UncheckedIOException(e);
  66. }
  67. return Collections.singleton("@" + file.getPath());
  68. }
  69. }