langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/SjavacImpl.java
author vasya
Mon, 14 Dec 2015 20:18:19 +0100
changeset 34752 9c262a013456
parent 34560 b6a567b677f7
child 34991 ff8be37d1164
permissions -rw-r--r--
8145342: Some copyright notices are inconsistently and ill formatted Summary: Adjusted formatting of copyright notices. Reviewed-by: darcy, alundblad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25605
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     1
/*
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27388
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
25605
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     4
 *
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    10
 *
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    15
 * accompanied this code).
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    16
 *
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    20
 *
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    23
 * questions.
c2d72342fe19 8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents: 25299
diff changeset
    24
 */
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 34560
diff changeset
    25
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    26
package com.sun.tools.sjavac.comp;
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    27
27388
d694da45bd7a 8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents: 26992
diff changeset
    28
import java.io.IOException;
32542
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    29
import java.io.PrintWriter;
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    30
import java.io.Writer;
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    31
import java.nio.file.Files;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    32
import java.nio.file.Path;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    33
import java.util.ArrayList;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    34
import java.util.Collections;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    35
import java.util.HashMap;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    36
import java.util.HashSet;
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    37
import java.util.List;
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    38
import java.util.Map;
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    39
import java.util.Set;
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
    40
import java.util.stream.Stream;
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    41
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
    42
import com.sun.tools.javac.file.JavacFileManager;
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
    43
import com.sun.tools.javac.main.Main;
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
    44
import com.sun.tools.javac.util.Context;
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    45
import com.sun.tools.sjavac.JavacState;
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27388
diff changeset
    46
import com.sun.tools.sjavac.Log;
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    47
import com.sun.tools.sjavac.Module;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    48
import com.sun.tools.sjavac.ProblemException;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    49
import com.sun.tools.sjavac.Source;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    50
import com.sun.tools.sjavac.Transformer;
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents: 26107
diff changeset
    51
import com.sun.tools.sjavac.Util;
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
    52
import com.sun.tools.sjavac.options.Option;
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    53
import com.sun.tools.sjavac.options.Options;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    54
import com.sun.tools.sjavac.options.SourceLocation;
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
    55
import com.sun.tools.sjavac.server.Sjavac;
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    56
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
    57
import javax.tools.JavaFileManager;
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
    58
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
    59
/**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
    60
 * The sjavac implementation that interacts with javac and performs the actual
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
    61
 * compilation.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
    62
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
    63
 *  <p><b>This is NOT part of any supported API.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
    64
 *  If you write code that depends on this, you do so at your own risk.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
    65
 *  This code and its internal interfaces are subject to change or
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
    66
 *  deletion without notice.</b>
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
    67
 */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
    68
public class SjavacImpl implements Sjavac {
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    69
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    70
    @Override
32542
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    71
    public int compile(String[] args, Writer out, Writer err) {
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    72
        Options options;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    73
        try {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    74
            options = Options.parseArgs(args);
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    75
        } catch (IllegalArgumentException e) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    76
            Log.error(e.getMessage());
32542
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    77
            return RC_FATAL;
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    78
        }
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    79
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    80
        Log.setLogLevel(options.getLogLevel());
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    81
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    82
        if (!validateOptions(options))
32542
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    83
            return RC_FATAL;
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
    84
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    85
        if (!createIfMissing(options.getDestDir()))
32542
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    86
            return RC_FATAL;
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    87
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
    88
        Path stateDir = options.getStateDir();
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
    89
        if (stateDir != null && !createIfMissing(options.getStateDir()))
32542
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    90
            return RC_FATAL;
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    91
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    92
        Path gensrc = options.getGenSrcDir();
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    93
        if (gensrc != null && !createIfMissing(gensrc))
32542
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    94
            return RC_FATAL;
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27388
diff changeset
    95
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    96
        Path hdrdir = options.getHeaderDir();
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    97
        if (hdrdir != null && !createIfMissing(hdrdir))
32542
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    98
            return RC_FATAL;
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    99
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   100
        if (stateDir == null) {
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   101
            // Prepare context. Direct logging to our byte array stream.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   102
            Context context = new Context();
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   103
            PrintWriter writer = new PrintWriter(err);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   104
            com.sun.tools.javac.util.Log.preRegister(context, writer);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   105
            JavacFileManager.preRegister(context);
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   106
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   107
            // Prepare arguments
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   108
            String[] passThroughArgs = Stream.of(args)
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   109
                                             .filter(arg -> !arg.startsWith(Option.SERVER.arg))
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   110
                                             .toArray(String[]::new);
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   111
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   112
            // Compile
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   113
            com.sun.tools.javac.main.Main compiler = new com.sun.tools.javac.main.Main("javac", writer);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   114
            Main.Result result = compiler.compile(passThroughArgs, context);
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   115
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   116
            // Clean up
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   117
            JavaFileManager fileManager = context.get(JavaFileManager.class);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   118
            if (fileManager instanceof JavacFileManager) {
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 33553
diff changeset
   119
                try {
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 33553
diff changeset
   120
                    ((JavacFileManager) fileManager).close();
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 33553
diff changeset
   121
                } catch (IOException e) {
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 33553
diff changeset
   122
                    return RC_FATAL;
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 33553
diff changeset
   123
                }
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   124
            }
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   125
            return result.exitCode;
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   126
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   127
        } else {
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   128
            // Load the prev build state database.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   129
            JavacState javac_state = JavacState.load(options, out, err);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   130
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   131
            // Setup the suffix rules from the command line.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   132
            Map<String, Transformer> suffixRules = new HashMap<>();
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27388
diff changeset
   133
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   134
            // Handling of .java-compilation
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   135
            suffixRules.putAll(javac_state.getJavaSuffixRule());
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   136
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   137
            // Handling of -copy and -tr
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   138
            suffixRules.putAll(options.getTranslationRules());
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   139
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   140
            // All found modules are put here.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   141
            Map<String,Module> modules = new HashMap<>();
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   142
            // We start out in the legacy empty no-name module.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   143
            // As soon as we stumble on a module-info.java file we change to that module.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   144
            Module current_module = new Module("", "");
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   145
            modules.put("", current_module);
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   146
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   147
            // Find all sources, use the suffix rules to know which files are sources.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   148
            Map<String,Source> sources = new HashMap<>();
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents: 26107
diff changeset
   149
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   150
            // Find the files, this will automatically populate the found modules
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   151
            // with found packages where the sources are found!
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   152
            findSourceFiles(options.getSources(),
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   153
                            suffixRules.keySet(),
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   154
                            sources,
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   155
                            modules,
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   156
                            current_module,
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   157
                            options.isDefaultPackagePermitted(),
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   158
                            false);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   159
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   160
            if (sources.isEmpty()) {
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   161
                Log.error("Found nothing to compile!");
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   162
                return RC_FATAL;
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   163
            }
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   164
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   165
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   166
            // Create a map of all source files that are available for linking. Both -src and
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   167
            // -sourcepath point to such files. It is possible to specify multiple
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   168
            // -sourcepath options to enable different filtering rules. If the
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   169
            // filters are the same for multiple sourcepaths, they may be concatenated
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   170
            // using :(;). Before sending the list of sourcepaths to javac, they are
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   171
            // all concatenated. The list created here is used by the SmartFileWrapper to
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   172
            // make sure only the correct sources are actually available.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   173
            // We might find more modules here as well.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   174
            Map<String,Source> sources_to_link_to = new HashMap<>();
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27388
diff changeset
   175
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   176
            List<SourceLocation> sourceResolutionLocations = new ArrayList<>();
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   177
            sourceResolutionLocations.addAll(options.getSources());
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   178
            sourceResolutionLocations.addAll(options.getSourceSearchPaths());
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   179
            findSourceFiles(sourceResolutionLocations,
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   180
                            Collections.singleton(".java"),
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   181
                            sources_to_link_to,
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   182
                            modules,
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   183
                            current_module,
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   184
                            options.isDefaultPackagePermitted(),
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   185
                            true);
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   186
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   187
            // Add the set of sources to the build database.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   188
            javac_state.now().flattenPackagesSourcesAndArtifacts(modules);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   189
            javac_state.now().checkInternalState("checking sources", false, sources);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   190
            javac_state.now().checkInternalState("checking linked sources", true, sources_to_link_to);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   191
            javac_state.setVisibleSources(sources_to_link_to);
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   192
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   193
            int round = 0;
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   194
            printRound(round);
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   195
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   196
            // If there is any change in the source files, taint packages
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   197
            // and mark the database in need of saving.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   198
            javac_state.checkSourceStatus(false);
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   199
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   200
            // Find all existing artifacts. Their timestamp will match the last modified timestamps stored
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   201
            // in javac_state, simply because loading of the JavacState will clean out all artifacts
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   202
            // that do not match the javac_state database.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   203
            javac_state.findAllArtifacts();
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   204
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   205
            // Remove unidentified artifacts from the bin, gensrc and header dirs.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   206
            // (Unless we allow them to be there.)
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   207
            // I.e. artifacts that are not known according to the build database (javac_state).
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   208
            // For examples, files that have been manually copied into these dirs.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   209
            // Artifacts with bad timestamps (ie the on disk timestamp does not match the timestamp
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   210
            // in javac_state) have already been removed when the javac_state was loaded.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   211
            if (!options.areUnidentifiedArtifactsPermitted()) {
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   212
                javac_state.removeUnidentifiedArtifacts();
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   213
            }
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   214
            // Go through all sources and taint all packages that miss artifacts.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   215
            javac_state.taintPackagesThatMissArtifacts();
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   216
33553
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   217
            try {
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   218
                // Check recorded classpath public apis. Taint packages that depend on
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   219
                // classpath classes whose public apis have changed.
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   220
                javac_state.taintPackagesDependingOnChangedClasspathPackages();
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   221
33553
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   222
                // Now clean out all known artifacts belonging to tainted packages.
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   223
                javac_state.deleteClassArtifactsInTaintedPackages();
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   224
                // Copy files, for example property files, images files, xml files etc etc.
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   225
                javac_state.performCopying(Util.pathToFile(options.getDestDir()), suffixRules);
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   226
                // Translate files, for example compile properties or compile idls.
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   227
                javac_state.performTranslation(Util.pathToFile(gensrc), suffixRules);
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   228
                // Add any potentially generated java sources to the tobe compiled list.
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   229
                // (Generated sources must always have a package.)
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 32799
diff changeset
   230
                Map<String,Source> generated_sources = new HashMap<>();
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   231
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   232
                Source.scanRoot(Util.pathToFile(options.getGenSrcDir()), Util.set(".java"), null, null, null, null,
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   233
                        generated_sources, modules, current_module, false, true, false);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   234
                javac_state.now().flattenPackagesSourcesAndArtifacts(modules);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   235
                // Recheck the the source files and their timestamps again.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   236
                javac_state.checkSourceStatus(true);
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   237
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   238
                // Now do a safety check that the list of source files is identical
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   239
                // to the list Make believes we are compiling. If we do not get this
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   240
                // right, then incremental builds will fail with subtility.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   241
                // If any difference is detected, then we will fail hard here.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   242
                // This is an important safety net.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   243
                javac_state.compareWithMakefileList(Util.pathToFile(options.getSourceReferenceList()));
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   244
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   245
                // Do the compilations, repeatedly until no tainted packages exist.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   246
                boolean again;
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   247
                // Collect the name of all compiled packages.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   248
                Set<String> recently_compiled = new HashSet<>();
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   249
                boolean[] rc = new boolean[1];
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   250
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   251
                CompilationService compilationService = new CompilationService();
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   252
                do {
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   253
                    if (round > 0)
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   254
                        printRound(round);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   255
                    // Clean out artifacts in tainted packages.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   256
                    javac_state.deleteClassArtifactsInTaintedPackages();
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   257
                    again = javac_state.performJavaCompilations(compilationService, options, recently_compiled, rc);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   258
                    if (!rc[0]) {
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   259
                        Log.debug("Compilation failed.");
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   260
                        break;
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   261
                    }
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   262
                    if (!again) {
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   263
                        Log.debug("Nothing left to do.");
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   264
                    }
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   265
                    round++;
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   266
                } while (again);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   267
                Log.debug("No need to do another round.");
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   268
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   269
                // Only update the state if the compile went well.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   270
                if (rc[0]) {
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   271
                    javac_state.save();
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   272
                    // Reflatten only the artifacts.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   273
                    javac_state.now().flattenArtifacts(modules);
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   274
                    // Remove artifacts that were generated during the last compile, but not this one.
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   275
                    javac_state.removeSuperfluousArtifacts(recently_compiled);
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   276
                }
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   277
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   278
                return rc[0] ? RC_OK : RC_FATAL;
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   279
            } catch (ProblemException e) {
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   280
                Log.error(e.getMessage());
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   281
                return RC_FATAL;
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   282
            } catch (Exception e) {
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   283
                e.printStackTrace(new PrintWriter(err));
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   284
                return RC_FATAL;
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   285
            }
26992
92e69fa21956 8059349: Public API scanning should be implemented in the form of a TaskListener
alundblad
parents: 26991
diff changeset
   286
        }
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   287
    }
26088
f479ca655ba1 8054474: Add --state-dir=bar to sjavac
ohrstrom
parents: 25605
diff changeset
   288
f479ca655ba1 8054474: Add --state-dir=bar to sjavac
ohrstrom
parents: 25605
diff changeset
   289
    @Override
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
   290
    public void shutdown() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
   291
        // Nothing to clean up
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
   292
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 26088
diff changeset
   293
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   294
    private static boolean validateOptions(Options options) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   295
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   296
        String err = null;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   297
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   298
        if (options.getDestDir() == null) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   299
            err = "Please specify output directory.";
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   300
        } else if (options.isJavaFilesAmongJavacArgs()) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   301
            err = "Sjavac does not handle explicit compilation of single .java files.";
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   302
        } else if (options.getServerConf() == null) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   303
            err = "No server configuration provided.";
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   304
        } else if (!options.getImplicitPolicy().equals("none")) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   305
            err = "The only allowed setting for sjavac is -implicit:none";
32799
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   306
        } else if (options.getSources().isEmpty() && options.getStateDir() != null) {
ee577901f4bb 8135131: Enable thin server mode in Sjavac
alundblad
parents: 32542
diff changeset
   307
            err = "You have to specify -src when using --state-dir.";
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   308
        } else if (options.getTranslationRules().size() > 1
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   309
                && options.getGenSrcDir() == null) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   310
            err = "You have translators but no gensrc dir (-s) specified!";
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   311
        }
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   312
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   313
        if (err != null)
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   314
            Log.error(err);
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   315
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   316
        return err == null;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   317
26088
f479ca655ba1 8054474: Add --state-dir=bar to sjavac
ohrstrom
parents: 25605
diff changeset
   318
    }
f479ca655ba1 8054474: Add --state-dir=bar to sjavac
ohrstrom
parents: 25605
diff changeset
   319
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   320
    private static boolean createIfMissing(Path dir) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   321
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   322
        if (Files.isDirectory(dir))
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   323
            return true;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   324
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   325
        if (Files.exists(dir)) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   326
            Log.error(dir + " is not a directory.");
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   327
            return false;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   328
        }
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   329
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   330
        try {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   331
            Files.createDirectories(dir);
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   332
        } catch (IOException e) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   333
            Log.error("Could not create directory: " + e.getMessage());
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   334
            return false;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   335
        }
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   336
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   337
        return true;
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   338
    }
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   339
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   340
    /** Find source files in the given source locations. */
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   341
    public static void findSourceFiles(List<SourceLocation> sourceLocations,
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   342
                                       Set<String> sourceTypes,
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   343
                                       Map<String,Source> foundFiles,
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   344
                                       Map<String, Module> foundModules,
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   345
                                       Module currentModule,
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   346
                                       boolean permitSourcesInDefaultPackage,
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   347
                                       boolean inLinksrc) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   348
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   349
        for (SourceLocation source : sourceLocations) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   350
            source.findSourceFiles(sourceTypes,
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   351
                                   foundFiles,
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   352
                                   foundModules,
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   353
                                   currentModule,
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   354
                                   permitSourcesInDefaultPackage,
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   355
                                   inLinksrc);
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27388
diff changeset
   356
        }
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 27388
diff changeset
   357
    }
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   358
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   359
    private static void printRound(int round) {
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   360
        Log.debug("****************************************");
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   361
        Log.debug("* Round " + round + "                              *");
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   362
        Log.debug("****************************************");
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
   363
    }
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff changeset
   364
}