Commit b13d3e0d authored by Tobias Hunger's avatar Tobias Hunger Committed by Brad King

cmake-server: Bare-bones server implementation

Adds a bare-bones cmake-server implementation and makes it possible
to start that with "cmake -E server".

Communication happens via stdin/stdout for now.

Protocol is based on Json objects surrounded by magic strings
("[== CMake Server ==[" and "]== CMake Server ==]"), which simplifies
Json parsing significantly.

This patch also defines an interface used to implement different
versions of the protocol spoken by the server, but does not include
any protocol implementaiton.
parent cd049f01
Pipeline #26670 passed with stage
......@@ -702,6 +702,18 @@ endif()
# setup some Testing support (a macro defined in this file)
CMAKE_SETUP_TESTING()
# Check whether to build server mode or not:
set(CMake_HAVE_SERVER_MODE 0)
if(NOT CMake_TEST_EXTERNAL_CMAKE AND NOT CMAKE_BOOTSTRAP AND CMAKE_USE_LIBUV)
list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_auto_type CMake_HAVE_CXX_AUTO_TYPE)
list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_range_for CMake_HAVE_CXX_RANGE_FOR)
if(CMake_HAVE_CXX_AUTO_TYPE AND CMake_HAVE_CXX_RANGE_FOR)
if(CMake_HAVE_CXX_MAKE_UNIQUE)
set(CMake_HAVE_SERVER_MODE 1)
endif()
endif()
endif()
if(NOT CMake_TEST_EXTERNAL_CMAKE)
if(NOT CMake_VERSION_IS_RELEASE)
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND
......
......@@ -786,6 +786,17 @@ add_executable(cmake cmakemain.cxx cmcmd.cxx cmcmd.h ${MANIFEST_FILE})
list(APPEND _tools cmake)
target_link_libraries(cmake CMakeLib)
if(CMake_HAVE_SERVER_MODE)
add_library(CMakeServerLib
cmServer.cxx cmServer.h
cmServerProtocol.cxx cmServerProtocol.h
)
target_link_libraries(CMakeServerLib CMakeLib)
set_property(SOURCE cmcmd.cxx APPEND PROPERTY COMPILE_DEFINITIONS HAVE_SERVER_MODE=1)
target_link_libraries(cmake CMakeServerLib)
endif()
# Build CTest executable
add_executable(ctest ctest.cxx ${MANIFEST_FILE})
list(APPEND _tools ctest)
......
This diff is collapsed.
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2015 Stephen Kelly <steveire@gmail.com>
Copyright 2016 Tobias Hunger <tobias.hunger@qt.io>
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#pragma once
#include "cmListFileCache.h"
#include "cmState.h"
#if defined(CMAKE_BUILD_WITH_CMAKE)
#include "cm_jsoncpp_value.h"
#include "cm_uv.h"
#endif
#include <string>
#include <vector>
class cmServerProtocol;
class cmServerRequest;
class cmServerResponse;
class cmServer
{
public:
cmServer();
~cmServer();
void Serve();
// for callbacks:
void PopOne();
void handleData(std::string const& data);
private:
void RegisterProtocol(cmServerProtocol* protocol);
// Handle requests:
cmServerResponse SetProtocolVersion(const cmServerRequest& request);
void PrintHello() const;
// Write responses:
void WriteProgress(const cmServerRequest& request, int min, int current,
int max, const std::string& message) const;
void WriteResponse(const cmServerResponse& response) const;
void WriteParseError(const std::string& message) const;
void WriteJsonObject(Json::Value const& jsonValue) const;
static cmServerProtocol* FindMatchingProtocol(
const std::vector<cmServerProtocol*>& protocols, int major, int minor);
cmServerProtocol* Protocol = nullptr;
std::vector<cmServerProtocol*> SupportedProtocols;
std::vector<std::string> Queue;
std::string DataBuffer;
std::string JsonData;
uv_loop_t* Loop = nullptr;
typedef union
{
uv_tty_t tty;
uv_pipe_t pipe;
} InOutUnion;
InOutUnion Input;
InOutUnion Output;
uv_stream_t* InputStream = nullptr;
uv_stream_t* OutputStream = nullptr;
mutable bool Writing = false;
friend class cmServerRequest;
};
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2016 Tobias Hunger <tobias.hunger@qt.io>
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#include "cmServerProtocol.h"
#include "cmExternalMakefileProjectGenerator.h"
#include "cmServer.h"
#include "cmake.h"
#if defined(CMAKE_BUILD_WITH_CMAKE)
#include "cm_jsoncpp_reader.h"
#include "cm_jsoncpp_value.h"
#endif
namespace {
// Vocabulary:
const std::string kCOOKIE_KEY = "cookie";
const std::string kTYPE_KEY = "type";
} // namespace
cmServerRequest::cmServerRequest(cmServer* server, const std::string& t,
const std::string& c, const Json::Value& d)
: Type(t)
, Cookie(c)
, Data(d)
, m_Server(server)
{
}
void cmServerRequest::ReportProgress(int min, int current, int max,
const std::string& message) const
{
this->m_Server->WriteProgress(*this, min, current, max, message);
}
cmServerResponse cmServerRequest::Reply(const Json::Value& data) const
{
cmServerResponse response(*this);
response.SetData(data);
return response;
}
cmServerResponse cmServerRequest::ReportError(const std::string& message) const
{
cmServerResponse response(*this);
response.SetError(message);
return response;
}
cmServerResponse::cmServerResponse(const cmServerRequest& request)
: Type(request.Type)
, Cookie(request.Cookie)
{
}
void cmServerResponse::SetData(const Json::Value& data)
{
assert(this->m_Payload == PAYLOAD_UNKNOWN);
if (!data[kCOOKIE_KEY].isNull() || !data[kTYPE_KEY].isNull()) {
this->SetError("Response contains cookie or type field.");
return;
}
this->m_Payload = PAYLOAD_DATA;
this->m_Data = data;
}
void cmServerResponse::SetError(const std::string& message)
{
assert(this->m_Payload == PAYLOAD_UNKNOWN);
this->m_Payload = PAYLOAD_ERROR;
this->m_ErrorMessage = message;
}
bool cmServerResponse::IsComplete() const
{
return this->m_Payload != PAYLOAD_UNKNOWN;
}
bool cmServerResponse::IsError() const
{
assert(this->m_Payload != PAYLOAD_UNKNOWN);
return this->m_Payload == PAYLOAD_ERROR;
}
std::string cmServerResponse::ErrorMessage() const
{
if (this->m_Payload == PAYLOAD_ERROR)
return this->m_ErrorMessage;
else
return std::string();
}
Json::Value cmServerResponse::Data() const
{
assert(this->m_Payload != PAYLOAD_UNKNOWN);
return this->m_Data;
}
bool cmServerProtocol::Activate(const cmServerRequest& request,
std::string* errorMessage)
{
this->m_CMakeInstance = std::make_unique<cmake>();
const bool result = this->DoActivate(request, errorMessage);
if (!result)
this->m_CMakeInstance = CM_NULLPTR;
return result;
}
cmake* cmServerProtocol::CMakeInstance() const
{
return this->m_CMakeInstance.get();
}
bool cmServerProtocol::DoActivate(const cmServerRequest& /*request*/,
std::string* /*errorMessage*/)
{
return true;
}
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2016 Tobias Hunger <tobias.hunger@qt.io>
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#pragma once
#include "cmListFileCache.h"
#if defined(CMAKE_BUILD_WITH_CMAKE)
#include "cm_jsoncpp_writer.h"
#endif
#include <memory>
#include <string>
class cmake;
class cmServer;
class cmServerRequest;
class cmServerResponse
{
public:
explicit cmServerResponse(const cmServerRequest& request);
void SetData(const Json::Value& data);
void SetError(const std::string& message);
bool IsComplete() const;
bool IsError() const;
std::string ErrorMessage() const;
Json::Value Data() const;
const std::string Type;
const std::string Cookie;
private:
enum PayLoad
{
PAYLOAD_UNKNOWN,
PAYLOAD_ERROR,
PAYLOAD_DATA
};
PayLoad m_Payload = PAYLOAD_UNKNOWN;
std::string m_ErrorMessage;
Json::Value m_Data;
};
class cmServerRequest
{
public:
void ReportProgress(int min, int current, int max,
const std::string& message) const;
cmServerResponse Reply(const Json::Value& data) const;
cmServerResponse ReportError(const std::string& message) const;
const std::string Type;
const std::string Cookie;
const Json::Value Data;
private:
cmServerRequest(cmServer* server, const std::string& t, const std::string& c,
const Json::Value& d);
cmServer* m_Server;
friend class cmServer;
};
class cmServerProtocol
{
public:
virtual ~cmServerProtocol() {}
virtual std::pair<int, int> ProtocolVersion() const = 0;
virtual const cmServerResponse Process(const cmServerRequest& request) = 0;
bool Activate(const cmServerRequest& request, std::string* errorMessage);
protected:
cmake* CMakeInstance() const;
// Implement protocol specific activation tasks here. Called from Activate().
virtual bool DoActivate(const cmServerRequest& request,
std::string* errorMessage);
private:
std::unique_ptr<cmake> m_CMakeInstance;
};
......@@ -23,6 +23,10 @@
#include "cm_auto_ptr.hxx"
#include "cmake.h"
#if defined(HAVE_SERVER_MODE) && HAVE_SERVER_MODE
#include "cmServer.h"
#endif
#if defined(CMAKE_BUILD_WITH_CMAKE)
#include "cmDependsFortran.h" // For -E cmake_copy_f90_mod callback.
#endif
......@@ -91,6 +95,7 @@ void CMakeCommandUsage(const char* program)
<< " remove_directory dir - remove a directory and its contents\n"
<< " rename oldname newname - rename a file or directory "
"(on one volume)\n"
<< " server - start cmake in server mode\n"
<< " sleep <number>... - sleep for given number of seconds\n"
<< " tar [cxt][vf][zjJ] file.tar [file/dir1 file/dir2 ...]\n"
<< " - create or extract a tar or zip archive\n"
......@@ -907,6 +912,19 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
#endif
}
return 0;
} else if (args[1] == "server") {
if (args.size() > 2) {
cmSystemTools::Error("Too many arguments to start server mode");
return 1;
}
#if defined(HAVE_SERVER_MODE) && HAVE_SERVER_MODE
cmServer server;
server.Serve();
return 0;
#else
cmSystemTools::Error("CMake was not built with server mode enabled");
return 1;
#endif
}
#if defined(CMAKE_BUILD_WITH_CMAKE)
......
^CMake Error: Too many arguments to start server mode$
......@@ -12,6 +12,7 @@ run_cmake_command(E_capabilities ${CMAKE_COMMAND} -E capabilities)
run_cmake_command(E_capabilities-arg ${CMAKE_COMMAND} -E capabilities --extra-arg)
run_cmake_command(E_echo_append ${CMAKE_COMMAND} -E echo_append)
run_cmake_command(E_rename-no-arg ${CMAKE_COMMAND} -E rename)
run_cmake_command(E_server-arg ${CMAKE_COMMAND} -E server --extra-arg)
run_cmake_command(E_touch_nocreate-no-arg ${CMAKE_COMMAND} -E touch_nocreate)
run_cmake_command(E_time ${CMAKE_COMMAND} -E time ${CMAKE_COMMAND} -E echo "hello world")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment