2 * Copyright 2000-2012 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.jetbrains.jps.cmdline;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.util.io.FileSystemUtil;
20 import com.intellij.openapi.util.io.FileUtil;
21 import io.netty.bootstrap.Bootstrap;
22 import io.netty.channel.*;
23 import io.netty.channel.nio.NioEventLoopGroup;
24 import io.netty.channel.socket.nio.NioSocketChannel;
25 import io.netty.handler.codec.protobuf.ProtobufDecoder;
26 import io.netty.handler.codec.protobuf.ProtobufEncoder;
27 import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
28 import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
29 import org.jetbrains.annotations.Nullable;
30 import org.jetbrains.jps.api.CmdlineProtoUtil;
31 import org.jetbrains.jps.api.CmdlineRemoteProto;
32 import org.jetbrains.jps.builders.BuildTarget;
33 import org.jetbrains.jps.incremental.BuilderRegistry;
34 import org.jetbrains.jps.incremental.MessageHandler;
35 import org.jetbrains.jps.incremental.Utils;
36 import org.jetbrains.jps.incremental.fs.BuildFSState;
37 import org.jetbrains.jps.incremental.messages.BuildMessage;
38 import org.jetbrains.jps.incremental.storage.BuildTargetsState;
39 import org.jetbrains.jps.service.SharedThreadPool;
42 import java.net.InetSocketAddress;
43 import java.util.UUID;
44 import java.util.concurrent.TimeUnit;
47 * @author Eugene Zhuravlev
50 @SuppressWarnings("UseOfSystemOutOrSystemErr")
51 public class BuildMain {
52 private static final String PRELOAD_PROJECT_PATH = "preload.project.path";
53 private static final String PRELOAD_CONFIG_PATH = "preload.config.path";
55 private static final Logger LOG;
57 LogSetup.initLoggers();
58 LOG = Logger.getInstance("#org.jetbrains.jps.cmdline.BuildMain");
61 private static final int HOST_ARG = 0;
62 private static final int PORT_ARG = HOST_ARG + 1;
63 private static final int SESSION_ID_ARG = PORT_ARG + 1;
64 private static final int SYSTEM_DIR_ARG = SESSION_ID_ARG + 1;
66 private static NioEventLoopGroup ourEventLoopGroup;
68 private static PreloadedData ourPreloadedData;
70 public static void main(String[] args){
71 final long processStart = System.currentTimeMillis();
72 final String startMessage = "Build process started. Classpath: " + System.getProperty("java.class.path");
73 System.out.println(startMessage);
74 LOG.info(startMessage);
76 final String host = args[HOST_ARG];
77 final int port = Integer.parseInt(args[PORT_ARG]);
78 final UUID sessionId = UUID.fromString(args[SESSION_ID_ARG]);
79 @SuppressWarnings("ConstantConditions")
80 final File systemDir = new File(FileUtil.toCanonicalPath(args[SYSTEM_DIR_ARG]));
81 Utils.setSystemRoot(systemDir);
83 final long connectStart = System.currentTimeMillis();
84 // IDEA-123132, let's try again
85 for (int attempt = 0; attempt < 3; attempt++) {
87 ourEventLoopGroup = new NioEventLoopGroup(1, SharedThreadPool.getInstance());
90 catch (IllegalStateException e) {
92 printErrorAndExit(host, port, e);
96 LOG.warn("Cannot create event loop, attempt #" + attempt, e);
98 //noinspection BusyWait
99 Thread.sleep(10 * (attempt + 1));
101 catch (InterruptedException ignored) {
107 final Bootstrap bootstrap = new Bootstrap().group(ourEventLoopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer() {
109 protected void initChannel(Channel channel) throws Exception {
110 channel.pipeline().addLast(new ProtobufVarint32FrameDecoder(),
111 new ProtobufDecoder(CmdlineRemoteProto.Message.getDefaultInstance()),
112 new ProtobufVarint32LengthFieldPrepender(),
113 new ProtobufEncoder(),
114 new MyMessageHandler(sessionId));
116 }).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true);
118 final ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)).awaitUninterruptibly();
121 final boolean success = future.isSuccess();
123 LOG.info("Connection to IDE established in " + (System.currentTimeMillis() - connectStart) + " ms");
125 final String projectPathToPreload = System.getProperty(PRELOAD_PROJECT_PATH, null);
126 final String globalsPathToPreload = System.getProperty(PRELOAD_CONFIG_PATH, null);
127 if (projectPathToPreload != null && globalsPathToPreload != null) {
128 final PreloadedData data = new PreloadedData();
129 ourPreloadedData = data;
131 FileSystemUtil.getAttributes(projectPathToPreload); // this will pre-load all FS optimizations
133 final BuildRunner runner = new BuildRunner(new JpsModelLoaderImpl(projectPathToPreload, globalsPathToPreload, null));
134 data.setRunner(runner);
136 final File dataStorageRoot = Utils.getDataStorageRoot(projectPathToPreload);
137 final BuildFSState fsState = new BuildFSState(false);
138 final ProjectDescriptor pd = runner.load(new MessageHandler() {
140 public void processMessage(BuildMessage msg) {
141 data.addMessage(msg);
143 }, dataStorageRoot, fsState);
144 data.setProjectDescriptor(pd);
147 final File fsStateFile = new File(dataStorageRoot, BuildSession.FS_STATE_FILE);
148 final DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fsStateFile)));
150 final int version = in.readInt();
151 if (version == BuildFSState.VERSION) {
152 final long savedOrdinal = in.readLong();
153 final boolean hasWorkToDo = in.readBoolean();// must skip "has-work-to-do" flag
154 fsState.load(in, pd.getModel(), pd.getBuildRootIndex());
155 data.setFsEventOrdinal(savedOrdinal);
156 data.setHasHasWorkToDo(hasWorkToDo);
163 catch (FileNotFoundException ignored) {
165 catch (IOException e) {
166 LOG.info("Error pre-loading FS state", e);
170 // preloading target configurations
171 final BuildTargetsState targetsState = pd.getTargetsState();
172 for (BuildTarget<?> target : pd.getBuildTargetIndex().getAllTargets()) {
173 targetsState.getTargetConfiguration(target);
176 BuilderRegistry.getInstance();
178 LOG.info("Pre-loaded process ready in " + (System.currentTimeMillis() - processStart) + " ms");
180 catch (Throwable e) {
181 LOG.info("Failed to pre-load project " + projectPathToPreload, e);
182 // just failed to preload the project, the situation will be handled later, when real build starts
185 else if (projectPathToPreload != null || globalsPathToPreload != null){
186 LOG.info("Skipping project pre-loading step: both paths to project configuration files and path to global settings must be specified");
188 future.channel().writeAndFlush(CmdlineProtoUtil.toMessage(sessionId, CmdlineProtoUtil.createParamRequest()));
191 printErrorAndExit(host, port, future.cause());
195 private static void printErrorAndExit(String host, int port, Throwable reason) {
196 System.err.println("Error connecting to " + host + ":" + port + "; reason: " + (reason != null ? reason.getMessage() : "unknown"));
197 if (reason != null) {
198 reason.printStackTrace(System.err);
200 System.err.println("Exiting.");
204 private static class MyMessageHandler extends SimpleChannelInboundHandler<CmdlineRemoteProto.Message> {
205 private final UUID mySessionId;
206 private volatile BuildSession mySession;
208 private MyMessageHandler(UUID sessionId) {
209 mySessionId = sessionId;
213 public void channelRead0(final ChannelHandlerContext context, CmdlineRemoteProto.Message message) throws Exception {
214 final CmdlineRemoteProto.Message.Type type = message.getType();
215 final Channel channel = context.channel();
217 if (type == CmdlineRemoteProto.Message.Type.CONTROLLER_MESSAGE) {
218 final CmdlineRemoteProto.Message.ControllerMessage controllerMessage = message.getControllerMessage();
219 switch (controllerMessage.getType()) {
221 case BUILD_PARAMETERS: {
222 if (mySession == null) {
223 final CmdlineRemoteProto.Message.ControllerMessage.FSEvent delta = controllerMessage.hasFsEvent()? controllerMessage.getFsEvent() : null;
224 final BuildSession session = new BuildSession(mySessionId, channel, controllerMessage.getParamsMessage(), delta, ourPreloadedData);
226 SharedThreadPool.getInstance().executeOnPooledThread(new Runnable() {
229 //noinspection finally
245 LOG.info("Cannot start another build session because one is already running");
251 final BuildSession session = mySession;
252 if (session != null) {
253 session.processFSEvent(controllerMessage.getFsEvent());
258 case CONSTANT_SEARCH_RESULT: {
259 final BuildSession session = mySession;
260 if (session != null) {
261 session.processConstantSearchResult(controllerMessage.getConstantSearchResult());
266 case CANCEL_BUILD_COMMAND: {
267 final BuildSession session = mySession;
268 if (session != null) {
272 LOG.info("Build canceled, but no build session is running. Exiting.");
274 final CmdlineRemoteProto.Message.BuilderMessage canceledEvent = CmdlineProtoUtil
275 .createBuildCompletedEvent("build completed", CmdlineRemoteProto.Message.BuilderMessage.BuildEvent.Status.CANCELED);
276 channel.writeAndFlush(CmdlineProtoUtil.toMessage(mySessionId, canceledEvent)).await();
279 catch (Throwable e) {
282 Thread.interrupted(); // to clear 'interrupted' flag
283 final PreloadedData preloaded = ourPreloadedData;
284 final ProjectDescriptor pd = preloaded != null? preloaded.getProjectDescriptor() : null;
295 channel.writeAndFlush(
296 CmdlineProtoUtil.toMessage(mySessionId, CmdlineProtoUtil.createFailure("Unsupported message type: " + type.name(), null)));
300 public void channelInactive(ChannelHandlerContext context) throws Exception {
302 super.channelInactive(context);
305 new Thread("Shutdown thread") {
308 //noinspection finally
310 ourEventLoopGroup.shutdownGracefully(0, 15, TimeUnit.SECONDS);