Search in sources :

Example 1 with FileSystemCompiler

use of org.codehaus.groovy.tools.FileSystemCompiler in project groovy by apache.

the class Groovyc method extractJointOptions.

/**
 * If {@code groovyc} task includes a nested {@code javac} task, check for
 * shareable configuration.  {@code FileSystemCompiler} supports several
 * command-line arguments for configuring joint compilation:
 * <ul>
 * <li><tt>-j</tt> enables joint compile
 * <li><tt>-F</tt> is used to pass flags
 * <li><tt>-J</tt> is used to pass name=value pairs
 * </ul>
 * Joint compilation options are transferred from {@link FileSystemCompiler}
 * to {@link CompilerConfiguration}'s jointCompileOptions property.  Flags
 * are saved to key "flags" (with the inclusion of "parameters" if enabled
 * on groovyc), pairs are saved to key "namedValues" and the key "memStub"
 * may also be set to {@link Boolean#TRUE} to influence joint compilation.
 *
 * @see org.codehaus.groovy.tools.javac.JavacJavaCompiler
 * @see javax.tools.JavaCompiler
 */
private List<String> extractJointOptions(Path classpath) {
    List<String> jointOptions = new ArrayList<>();
    if (!jointCompilation)
        return jointOptions;
    // map "debug" and "debuglevel" to "-Fg"
    if (javac.getDebug()) {
        jointOptions.add("-Fg" + Optional.ofNullable(javac.getDebugLevel()).map(level -> ":" + level).orElse(""));
    } else {
        jointOptions.add("-Fg:none");
    }
    // map "deprecation" to "-Fdeprecation"
    if (javac.getDeprecation()) {
        jointOptions.add("-Fdeprecation");
    }
    // map "nowarn" to "-Fnowarn"
    if (javac.getNowarn()) {
        jointOptions.add("-Fnowarn");
    }
    // map "verbose" to "-Fverbose"
    if (javac.getVerbose()) {
        jointOptions.add("-Fverbose");
    }
    RuntimeConfigurable rc = javac.getRuntimeConfigurableWrapper();
    for (Map.Entry<String, Object> e : rc.getAttributeMap().entrySet()) {
        String key = e.getKey();
        if (key.equals("depend") || key.equals("encoding") || key.equals("extdirs") || key.equals("nativeheaderdir") || key.equals("release") || key.equals("source") || key.equals("target")) {
            switch(key) {
                case "nativeheaderdir":
                    key = "h";
                    break;
                case "release":
                    // to get "--" when passed to javac
                    key = "-" + key;
                    break;
                default:
            }
            // map "depend", "encoding", etc. to "-Jkey=val"
            jointOptions.add("-J" + key + "=" + getProject().replaceProperties(e.getValue().toString()));
        } else if (key.contains("classpath")) {
            if (key.startsWith("boot")) {
                // map "bootclasspath" or "bootclasspathref" to "-Jbootclasspath="
                jointOptions.add("-Jbootclasspath=" + javac.getBootclasspath());
            } else {
                // map "classpath" or "classpathref" to "--classpath"
                classpath.add(javac.getClasspath());
            }
        } else if (key.contains("module") && key.contains("path")) {
            if (key.startsWith("upgrade")) {
                // map "upgrademodulepath" or "upgrademodulepathref" to "-J-upgrade-module-path="
                jointOptions.add("-J-upgrade-module-path=" + javac.getUpgrademodulepath());
            } else if (key.contains("source")) {
                // map "modulesourcepath" or "modulesourcepathref" to "-J-module-source-path="
                jointOptions.add("-J-module-source-path=" + javac.getModulesourcepath());
            } else {
                // map "modulepath" or "modulepathref" to "-J-module-path="
                jointOptions.add("-J-module-path=" + javac.getModulepath());
            }
        } else if (!key.contains("debug") && !key.equals("deprecation") && !key.equals("nowarn") && !key.equals("verbose")) {
            log.warn("The option " + key + " cannot be set on the contained <javac> element. The option will be ignored.");
        }
    // TODO: defaultexcludes, excludes(file)?, includes(file)?, includeDestClasses, tempdir
    }
    // can be multiple of them) for additional options to be passed to javac.
    for (RuntimeConfigurable childrc : Collections.list(rc.getChildren())) {
        if (childrc.getElementTag().equals("compilerarg")) {
            for (Map.Entry<String, Object> e : childrc.getAttributeMap().entrySet()) {
                String key = e.getKey();
                if (key.equals("value")) {
                    String value = getProject().replaceProperties(e.getValue().toString());
                    StringTokenizer st = new StringTokenizer(value, " ");
                    while (st.hasMoreTokens()) {
                        String option = st.nextToken();
                        // GROOVY-5063: map "-Werror", etc. to "-FWerror"
                        jointOptions.add(option.replaceFirst("^-(W|X|proc:)", "-F$1"));
                    }
                }
            }
        }
    }
    return jointOptions;
}
Also used : Arrays(java.util.Arrays) AntClassLoader(org.apache.tools.ant.AntClassLoader) Execute(org.apache.tools.ant.taskdefs.Execute) URISyntaxException(java.net.URISyntaxException) Javac(org.apache.tools.ant.taskdefs.Javac) FileSystemCompiler(org.codehaus.groovy.tools.FileSystemCompiler) JavaAwareCompilationUnit(org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit) ParseTreeVisitor(org.antlr.v4.runtime.tree.ParseTreeVisitor) Path(org.apache.tools.ant.types.Path) ArrayList(java.util.ArrayList) SourceFileScanner(org.apache.tools.ant.util.SourceFileScanner) Charset(java.nio.charset.Charset) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) StringTokenizer(java.util.StringTokenizer) Map(java.util.Map) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) DefaultGroovyStaticMethods(org.codehaus.groovy.runtime.DefaultGroovyStaticMethods) URI(java.net.URI) ClassVisitor(org.objectweb.asm.ClassVisitor) CommandLine(picocli.CommandLine) LinkedHashSet(java.util.LinkedHashSet) PrintWriter(java.io.PrintWriter) GlobPatternMapper(org.apache.tools.ant.util.GlobPatternMapper) FileWriter(java.io.FileWriter) DefaultGroovyMethods(org.codehaus.groovy.runtime.DefaultGroovyMethods) Set(java.util.Set) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) MatchingTask(org.apache.tools.ant.taskdefs.MatchingTask) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) PrivilegedAction(java.security.PrivilegedAction) File(java.io.File) GroovyBugError(org.codehaus.groovy.GroovyBugError) List(java.util.List) RuntimeConfigurable(org.apache.tools.ant.RuntimeConfigurable) SourceExtensionHandler(org.codehaus.groovy.control.SourceExtensionHandler) VMPluginFactory(org.codehaus.groovy.vmplugin.VMPluginFactory) Writer(java.io.Writer) Optional(java.util.Optional) GroovyClassLoader(groovy.lang.GroovyClassLoader) Collections(java.util.Collections) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Reference(org.apache.tools.ant.types.Reference) ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) StringTokenizer(java.util.StringTokenizer) RuntimeConfigurable(org.apache.tools.ant.RuntimeConfigurable) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 2 with FileSystemCompiler

use of org.codehaus.groovy.tools.FileSystemCompiler in project freeplane by freeplane.

the class ScriptCompiler method compile.

private static void compile(File dir, File[] files) {
    try {
        final CompilerConfiguration compilerConfiguration = GroovyScript.createCompilerConfiguration();
        compilerConfiguration.setTargetDirectory(dir);
        final CompilationUnit unit = new CompilationUnit(compilerConfiguration, null, new GroovyClassLoader(ScriptingEngine.class.getClassLoader()));
        new FileSystemCompiler(compilerConfiguration, unit).compile(files);
        LogUtils.info("compiled in " + dir + ": " + createNameList(files));
    } catch (Exception e) {
        LogUtils.severe("error compiling in " + dir + createNameList(files), e);
    }
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) GroovyClassLoader(groovy.lang.GroovyClassLoader) FileSystemCompiler(org.codehaus.groovy.tools.FileSystemCompiler) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)2 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)2 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Charset (java.nio.charset.Charset)1 PrivilegedAction (java.security.PrivilegedAction)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1