hotspot/test/runtime/modules/CompilerUtils.java
author alanb
Fri, 10 Feb 2017 09:03:55 +0000
changeset 43665 4bb003cad9b9
parent 39290 0cc9f5028562
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: lfoltan, acorn, mchung Contributed-by: alan.bateman@oracle.com, george.triantafillou@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39290
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     1
/*
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     4
 *
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     8
 *
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    13
 * accompanied this code).
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    14
 *
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    18
 *
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    21
 * questions.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    22
 */
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    23
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    24
import javax.tools.JavaCompiler;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    25
import javax.tools.StandardJavaFileManager;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    26
import javax.tools.StandardLocation;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    27
import javax.tools.ToolProvider;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    28
import java.io.IOException;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    29
import java.nio.file.Files;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    30
import java.nio.file.Path;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    31
import java.util.Arrays;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    32
import java.util.List;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    33
import java.util.stream.Collectors;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    34
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    35
/**
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    36
 * This class consists exclusively of static utility methods for invoking the
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    37
 * java compiler.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    38
 *
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    39
 * This class will eventually move to jdk.testlibrary.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    40
 */
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    41
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    42
public final class CompilerUtils {
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    43
    private CompilerUtils() { }
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    44
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    45
    /**
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    46
     * Compile all the java sources in {@code <source>/**} to
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    47
     * {@code <destination>/**}. The destination directory will be created if
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    48
     * it doesn't exist.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    49
     *
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    50
     * All warnings/errors emitted by the compiler are output to System.out/err.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    51
     *
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    52
     * @return true if the compilation is successful
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    53
     *
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    54
     * @throws IOException if there is an I/O error scanning the source tree or
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    55
     *                     creating the destination directory
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    56
     */
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    57
    public static boolean compile(Path source, Path destination, String ... options)
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    58
        throws IOException
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    59
    {
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    60
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    61
        StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    62
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    63
        List<Path> sources
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    64
            = Files.find(source, Integer.MAX_VALUE,
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    65
                (file, attrs) -> (file.toString().endsWith(".java")))
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    66
                .collect(Collectors.toList());
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    67
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    68
        Files.createDirectories(destination);
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    69
        jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    70
                                 Arrays.asList(destination));
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    71
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    72
        List<String> opts = Arrays.asList(options);
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    73
        JavaCompiler.CompilationTask task
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    74
            = compiler.getTask(null, jfm, null, opts, null,
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    75
                jfm.getJavaFileObjectsFromPaths(sources));
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    76
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    77
        return task.call();
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    78
    }
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    79
}