cmServerProtocol.cxx 22.5 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3 4
#include "cmServerProtocol.h"

5
#include "cmAlgorithms.h"
6
#include "cmExternalMakefileProjectGenerator.h"
7
#include "cmFileMonitor.h"
8
#include "cmGlobalGenerator.h"
9
#include "cmJsonObjectDictionary.h"
10
#include "cmJsonObjects.h"
11
#include "cmServer.h"
12
#include "cmServerDictionary.h"
13
#include "cmState.h"
14
#include "cmSystemTools.h"
15
#include "cm_uv.h"
16 17
#include "cmake.h"

18
#include <algorithm>
19 20
#include <cassert>
#include <functional>
21
#include <memory>
22
#include <string>
wahikihiki's avatar
wahikihiki committed
23
#include <utility>
24 25 26 27 28 29 30
#include <vector>

// Get rid of some windows macros:
#undef max

namespace {

31
std::vector<std::string> toStringList(const Json::Value& in)
32 33
{
  std::vector<std::string> result;
34
  for (auto const& it : in) {
35 36 37 38 39
    result.push_back(it.asString());
  }
  return result;
}

40 41
} // namespace

42
cmServerRequest::cmServerRequest(cmServer* server, cmConnection* connection,
wahikihiki's avatar
wahikihiki committed
43 44 45 46
                                 std::string t, std::string c, Json::Value d)
  : Type(std::move(t))
  , Cookie(std::move(c))
  , Data(std::move(d))
47
  , Connection(connection)
48 49 50 51 52 53 54 55 56 57
  , 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);
}

58 59 60 61 62 63
void cmServerRequest::ReportMessage(const std::string& message,
                                    const std::string& title) const
{
  m_Server->WriteMessage(*this, message, title);
}

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
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
{
115
  if (this->m_Payload == PAYLOAD_ERROR) {
116
    return this->m_ErrorMessage;
117 118
  }
  return std::string();
119 120 121 122 123 124 125 126
}

Json::Value cmServerResponse::Data() const
{
  assert(this->m_Payload != PAYLOAD_UNKNOWN);
  return this->m_Data;
}

127 128
bool cmServerProtocol::Activate(cmServer* server,
                                const cmServerRequest& request,
129 130
                                std::string* errorMessage)
{
131 132
  assert(server);
  this->m_Server = server;
133 134
  this->m_CMakeInstance =
    cm::make_unique<cmake>(cmake::RoleProject, cmState::Project);
135
  const bool result = this->DoActivate(request, errorMessage);
136
  if (!result) {
Daniel Pfeifer's avatar
Daniel Pfeifer committed
137
    this->m_CMakeInstance = nullptr;
138
  }
139 140 141
  return result;
}

142 143 144 145 146
cmFileMonitor* cmServerProtocol::FileMonitor() const
{
  return this->m_Server ? this->m_Server->FileMonitor() : nullptr;
}

147 148 149
void cmServerProtocol::SendSignal(const std::string& name,
                                  const Json::Value& data) const
{
150
  if (this->m_Server) {
151
    this->m_Server->WriteSignal(name, data);
152
  }
153 154
}

155 156 157 158 159 160 161 162 163 164
cmake* cmServerProtocol::CMakeInstance() const
{
  return this->m_CMakeInstance.get();
}

bool cmServerProtocol::DoActivate(const cmServerRequest& /*request*/,
                                  std::string* /*errorMessage*/)
{
  return true;
}
165

166
std::pair<int, int> cmServerProtocol1::ProtocolVersion() const
167
{
168
  return std::make_pair(1, 2);
169 170
}

171 172 173 174 175 176 177
static void setErrorMessage(std::string* errorMessage, const std::string& text)
{
  if (errorMessage) {
    *errorMessage = text;
  }
}

178 179
static bool getOrTestHomeDirectory(cmState* state, std::string& value,
                                   std::string* errorMessage)
180 181 182
{
  const std::string cachedValue =
    std::string(state->GetCacheEntryValue("CMAKE_HOME_DIRECTORY"));
183 184 185 186
  if (value.empty()) {
    value = cachedValue;
    return true;
  }
187 188 189 190 191 192 193 194 195 196 197 198 199
  const std::string suffix = "/CMakeLists.txt";
  const std::string cachedValueCML = cachedValue + suffix;
  const std::string valueCML = value + suffix;
  if (!cmSystemTools::SameFile(valueCML, cachedValueCML)) {
    setErrorMessage(errorMessage,
                    std::string("\"CMAKE_HOME_DIRECTORY\" is set but "
                                "incompatible with configured "
                                "source directory value."));
    return false;
  }
  return true;
}

200 201 202 203
static bool getOrTestValue(cmState* state, const std::string& key,
                           std::string& value,
                           const std::string& keyDescription,
                           std::string* errorMessage)
204
{
205 206 207
  const char* entry = state->GetCacheEntryValue(key);
  const std::string cachedValue =
    entry == nullptr ? std::string() : std::string(entry);
208 209 210 211
  if (value.empty()) {
    value = cachedValue;
  }
  if (!cachedValue.empty() && cachedValue != value) {
212 213
    setErrorMessage(errorMessage,
                    std::string("\"") + key +
214
                      "\" is set but incompatible with configured " +
215
                      keyDescription + " value.");
216 217 218 219 220
    return false;
  }
  return true;
}

221 222
bool cmServerProtocol1::DoActivate(const cmServerRequest& request,
                                   std::string* errorMessage)
223 224
{
  std::string sourceDirectory = request.Data[kSOURCE_DIRECTORY_KEY].asString();
225
  std::string buildDirectory = request.Data[kBUILD_DIRECTORY_KEY].asString();
226 227
  std::string generator = request.Data[kGENERATOR_KEY].asString();
  std::string extraGenerator = request.Data[kEXTRA_GENERATOR_KEY].asString();
228 229
  std::string toolset = request.Data[kTOOLSET_KEY].asString();
  std::string platform = request.Data[kPLATFORM_KEY].asString();
230

231 232 233 234 235 236 237 238 239 240
  // normalize source and build directory
  if (!sourceDirectory.empty()) {
    sourceDirectory = cmSystemTools::CollapseFullPath(sourceDirectory);
    cmSystemTools::ConvertToUnixSlashes(sourceDirectory);
  }
  if (!buildDirectory.empty()) {
    buildDirectory = cmSystemTools::CollapseFullPath(buildDirectory);
    cmSystemTools::ConvertToUnixSlashes(buildDirectory);
  }

241
  if (buildDirectory.empty()) {
242 243
    setErrorMessage(errorMessage,
                    std::string("\"") + kBUILD_DIRECTORY_KEY +
244
                      "\" is missing.");
245 246
    return false;
  }
247

248 249 250
  cmake* cm = CMakeInstance();
  if (cmSystemTools::PathExists(buildDirectory)) {
    if (!cmSystemTools::FileIsDirectory(buildDirectory)) {
251 252
      setErrorMessage(errorMessage,
                      std::string("\"") + kBUILD_DIRECTORY_KEY +
253
                        "\" exists but is not a directory.");
254 255 256
      return false;
    }

257
    const std::string cachePath = cmake::FindCacheFile(buildDirectory);
258 259 260 261
    if (cm->LoadCache(cachePath)) {
      cmState* state = cm->GetState();

      // Check generator:
262 263
      if (!getOrTestValue(state, "CMAKE_GENERATOR", generator, "generator",
                          errorMessage)) {
264 265 266 267
        return false;
      }

      // check extra generator:
268 269
      if (!getOrTestValue(state, "CMAKE_EXTRA_GENERATOR", extraGenerator,
                          "extra generator", errorMessage)) {
270 271 272 273
        return false;
      }

      // check sourcedir:
274
      if (!getOrTestHomeDirectory(state, sourceDirectory, errorMessage)) {
275 276
        return false;
      }
277 278

      // check toolset:
279 280
      if (!getOrTestValue(state, "CMAKE_GENERATOR_TOOLSET", toolset, "toolset",
                          errorMessage)) {
281 282 283 284
        return false;
      }

      // check platform:
285 286
      if (!getOrTestValue(state, "CMAKE_GENERATOR_PLATFORM", platform,
                          "platform", errorMessage)) {
287
        return false;
288 289 290 291 292
      }
    }
  }

  if (sourceDirectory.empty()) {
293 294
    setErrorMessage(errorMessage,
                    std::string("\"") + kSOURCE_DIRECTORY_KEY +
295
                      "\" is unset but required.");
296 297 298
    return false;
  }
  if (!cmSystemTools::FileIsDirectory(sourceDirectory)) {
299 300
    setErrorMessage(errorMessage,
                    std::string("\"") + kSOURCE_DIRECTORY_KEY +
301
                      "\" is not a directory.");
302 303 304
    return false;
  }
  if (generator.empty()) {
305 306
    setErrorMessage(errorMessage,
                    std::string("\"") + kGENERATOR_KEY +
307 308 309 310 311 312 313 314 315 316 317
                      "\" is unset but required.");
    return false;
  }

  std::vector<cmake::GeneratorInfo> generators;
  cm->GetRegisteredGenerators(generators);
  auto baseIt = std::find_if(generators.begin(), generators.end(),
                             [&generator](const cmake::GeneratorInfo& info) {
                               return info.name == generator;
                             });
  if (baseIt == generators.end()) {
318 319
    setErrorMessage(errorMessage,
                    std::string("Generator \"") + generator +
320 321 322 323 324 325 326 327 328 329 330 331 332
                      "\" not supported.");
    return false;
  }
  auto extraIt = std::find_if(
    generators.begin(), generators.end(),
    [&generator, &extraGenerator](const cmake::GeneratorInfo& info) {
      return info.baseName == generator && info.extraName == extraGenerator;
    });
  if (extraIt == generators.end()) {
    setErrorMessage(errorMessage,
                    std::string("The combination of generator \"" + generator +
                                "\" and extra generator \"" + extraGenerator +
                                "\" is not supported."));
333 334
    return false;
  }
335 336 337 338 339 340 341 342 343 344
  if (!extraIt->supportsToolset && !toolset.empty()) {
    setErrorMessage(errorMessage,
                    std::string("Toolset was provided but is not supported by "
                                "the requested generator."));
    return false;
  }
  if (!extraIt->supportsPlatform && !platform.empty()) {
    setErrorMessage(errorMessage,
                    std::string("Platform was provided but is not supported "
                                "by the requested generator."));
345 346 347
    return false;
  }

348 349 350
  this->GeneratorInfo =
    GeneratorInformation(generator, extraGenerator, toolset, platform,
                         sourceDirectory, buildDirectory);
351 352 353 354 355

  this->m_State = STATE_ACTIVE;
  return true;
}

356 357
void cmServerProtocol1::HandleCMakeFileChanges(const std::string& path,
                                               int event, int status)
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
{
  assert(status == 0);
  static_cast<void>(status);

  if (!m_isDirty) {
    m_isDirty = true;
    SendSignal(kDIRTY_SIGNAL, Json::objectValue);
  }
  Json::Value obj = Json::objectValue;
  obj[kPATH_KEY] = path;
  Json::Value properties = Json::arrayValue;
  if (event & UV_RENAME) {
    properties.append(kRENAME_PROPERTY_VALUE);
  }
  if (event & UV_CHANGE) {
    properties.append(kCHANGE_PROPERTY_VALUE);
  }

  obj[kPROPERTIES_KEY] = properties;
  SendSignal(kFILE_CHANGE_SIGNAL, obj);
}

380
const cmServerResponse cmServerProtocol1::Process(
381 382 383 384
  const cmServerRequest& request)
{
  assert(this->m_State >= STATE_ACTIVE);

385 386 387
  if (request.Type == kCACHE_TYPE) {
    return this->ProcessCache(request);
  }
388 389 390
  if (request.Type == kCMAKE_INPUTS_TYPE) {
    return this->ProcessCMakeInputs(request);
  }
391 392 393
  if (request.Type == kCODE_MODEL_TYPE) {
    return this->ProcessCodeModel(request);
  }
394 395 396
  if (request.Type == kCOMPUTE_TYPE) {
    return this->ProcessCompute(request);
  }
397 398 399
  if (request.Type == kCONFIGURE_TYPE) {
    return this->ProcessConfigure(request);
  }
400 401 402
  if (request.Type == kFILESYSTEM_WATCHERS_TYPE) {
    return this->ProcessFileSystemWatchers(request);
  }
403 404 405
  if (request.Type == kGLOBAL_SETTINGS_TYPE) {
    return this->ProcessGlobalSettings(request);
  }
406 407 408
  if (request.Type == kSET_GLOBAL_SETTINGS_TYPE) {
    return this->ProcessSetGlobalSettings(request);
  }
409 410 411
  if (request.Type == kCTEST_INFO_TYPE) {
    return this->ProcessCTests(request);
  }
412

413 414
  return request.ReportError("Unknown command!");
}
415

416
bool cmServerProtocol1::IsExperimental() const
417 418 419
{
  return true;
}
420

421
cmServerResponse cmServerProtocol1::ProcessCache(
422 423 424 425 426 427 428 429 430 431 432 433 434
  const cmServerRequest& request)
{
  cmState* state = this->CMakeInstance()->GetState();

  Json::Value result = Json::objectValue;

  std::vector<std::string> allKeys = state->GetCacheEntryKeys();

  Json::Value list = Json::arrayValue;
  std::vector<std::string> keys = toStringList(request.Data[kKEYS_KEY]);
  if (keys.empty()) {
    keys = allKeys;
  } else {
435
    for (auto const& i : keys) {
436
      if (std::find(allKeys.begin(), allKeys.end(), i) == allKeys.end()) {
437 438 439 440 441
        return request.ReportError("Key \"" + i + "\" not found in cache.");
      }
    }
  }
  std::sort(keys.begin(), keys.end());
442
  for (auto const& key : keys) {
443 444 445 446 447 448 449 450
    Json::Value entry = Json::objectValue;
    entry[kKEY_KEY] = key;
    entry[kTYPE_KEY] =
      cmState::CacheEntryTypeToString(state->GetCacheEntryType(key));
    entry[kVALUE_KEY] = state->GetCacheEntryValue(key);

    Json::Value props = Json::objectValue;
    bool haveProperties = false;
451
    for (auto const& prop : state->GetCacheEntryPropertyList(key)) {
452 453 454 455 456 457 458 459 460 461 462 463 464 465
      haveProperties = true;
      props[prop] = state->GetCacheEntryProperty(key, prop);
    }
    if (haveProperties) {
      entry[kPROPERTIES_KEY] = props;
    }

    list.append(entry);
  }

  result[kCACHE_KEY] = list;
  return request.Reply(result);
}

466 467 468 469 470 471
cmServerResponse cmServerProtocol1::ProcessCMakeInputs(
  const cmServerRequest& request)
{
  if (this->m_State < STATE_CONFIGURED) {
    return request.ReportError("This instance was not yet configured.");
  }
472

473 474 475 476 477 478 479
  const cmake* cm = this->CMakeInstance();
  const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot();
  const std::string& sourceDir = cm->GetHomeDirectory();

  Json::Value result = Json::objectValue;
  result[kSOURCE_DIRECTORY_KEY] = sourceDir;
  result[kCMAKE_ROOT_DIRECTORY_KEY] = cmakeRootDir;
480
  result[kBUILD_FILES_KEY] = cmDumpCMakeInputs(cm);
481 482 483
  return request.Reply(result);
}

484
cmServerResponse cmServerProtocol1::ProcessCodeModel(
485 486 487 488 489 490
  const cmServerRequest& request)
{
  if (this->m_State != STATE_COMPUTED) {
    return request.ReportError("No build system was generated yet.");
  }

491
  return request.Reply(cmDumpCodeModel(this->CMakeInstance()));
492 493
}

494
cmServerResponse cmServerProtocol1::ProcessCompute(
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
  const cmServerRequest& request)
{
  if (this->m_State > STATE_CONFIGURED) {
    return request.ReportError("This build system was already generated.");
  }
  if (this->m_State < STATE_CONFIGURED) {
    return request.ReportError("This project was not configured yet.");
  }

  cmake* cm = this->CMakeInstance();
  int ret = cm->Generate();

  if (ret < 0) {
    return request.ReportError("Failed to compute build system.");
  }
  m_State = STATE_COMPUTED;
  return request.Reply(Json::Value());
}

514
cmServerResponse cmServerProtocol1::ProcessConfigure(
515 516 517 518 519 520
  const cmServerRequest& request)
{
  if (this->m_State == STATE_INACTIVE) {
    return request.ReportError("This instance is inactive.");
  }

521 522
  FileMonitor()->StopMonitoring();

523 524 525 526 527 528 529
  std::string errorMessage;
  cmake* cm = this->CMakeInstance();
  this->GeneratorInfo.SetupGenerator(cm, &errorMessage);
  if (!errorMessage.empty()) {
    return request.ReportError(errorMessage);
  }

530
  // Make sure the types of cacheArguments matches (if given):
531
  std::vector<std::string> cacheArgs = { "unused" };
532 533 534 535 536 537
  bool cacheArgumentsError = false;
  const Json::Value passedArgs = request.Data[kCACHE_ARGUMENTS_KEY];
  if (!passedArgs.isNull()) {
    if (passedArgs.isString()) {
      cacheArgs.push_back(passedArgs.asString());
    } else if (passedArgs.isArray()) {
538 539
      for (auto const& arg : passedArgs) {
        if (!arg.isString()) {
540 541 542
          cacheArgumentsError = true;
          break;
        }
543
        cacheArgs.push_back(arg.asString());
544 545 546 547 548 549 550 551 552 553 554 555 556
      }
    } else {
      cacheArgumentsError = true;
    }
  }
  if (cacheArgumentsError) {
    request.ReportError(
      "cacheArguments must be unset, a string or an array of strings.");
  }

  std::string sourceDir = cm->GetHomeDirectory();
  const std::string buildDir = cm->GetHomeOutputDirectory();

557 558
  cmGlobalGenerator* gg = cm->GetGlobalGenerator();

559
  if (buildDir.empty()) {
560
    return request.ReportError("No build directory set via Handshake.");
561 562 563 564
  }

  if (cm->LoadCache(buildDir)) {
    // build directory has been set up before
565
    const std::string* cachedSourceDir =
566 567 568 569 570
      cm->GetState()->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
    if (!cachedSourceDir) {
      return request.ReportError("No CMAKE_HOME_DIRECTORY found in cache.");
    }
    if (sourceDir.empty()) {
571
      sourceDir = *cachedSourceDir;
572 573 574
      cm->SetHomeDirectory(sourceDir);
    }

575
    const std::string* cachedGenerator =
576 577
      cm->GetState()->GetInitializedCacheValue("CMAKE_GENERATOR");
    if (cachedGenerator) {
578
      if (gg && gg->GetName() != *cachedGenerator) {
579 580 581 582 583 584 585 586 587 588 589 590 591
        return request.ReportError("Configured generator does not match with "
                                   "CMAKE_GENERATOR found in cache.");
      }
    }
  } else {
    // build directory has not been set up before
    if (sourceDir.empty()) {
      return request.ReportError("No sourceDirectory set via "
                                 "setGlobalSettings and no cache found in "
                                 "buildDirectory.");
    }
  }

592 593
  cmSystemTools::ResetErrorOccuredFlag(); // Reset error state

594 595 596 597 598 599 600 601 602 603 604 605
  if (cm->AddCMakePaths() != 1) {
    return request.ReportError("Failed to set CMake paths.");
  }

  if (!cm->SetCacheArgs(cacheArgs)) {
    return request.ReportError("cacheArguments could not be set.");
  }

  int ret = cm->Configure();
  if (ret < 0) {
    return request.ReportError("Configuration failed.");
  }
606 607

  std::vector<std::string> toWatchList;
608 609
  cmGetCMakeInputs(gg, std::string(), buildDir, nullptr, &toWatchList,
                   nullptr);
610 611 612 613 614 615

  FileMonitor()->MonitorPaths(toWatchList,
                              [this](const std::string& p, int e, int s) {
                                this->HandleCMakeFileChanges(p, e, s);
                              });

616
  m_State = STATE_CONFIGURED;
617
  m_isDirty = false;
618 619 620
  return request.Reply(Json::Value());
}

621
cmServerResponse cmServerProtocol1::ProcessGlobalSettings(
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
  const cmServerRequest& request)
{
  cmake* cm = this->CMakeInstance();
  Json::Value obj = Json::objectValue;

  // Capabilities information:
  obj[kCAPABILITIES_KEY] = cm->ReportCapabilitiesJson(true);

  obj[kDEBUG_OUTPUT_KEY] = cm->GetDebugOutput();
  obj[kTRACE_KEY] = cm->GetTrace();
  obj[kTRACE_EXPAND_KEY] = cm->GetTraceExpand();
  obj[kWARN_UNINITIALIZED_KEY] = cm->GetWarnUninitialized();
  obj[kWARN_UNUSED_KEY] = cm->GetWarnUnused();
  obj[kWARN_UNUSED_CLI_KEY] = cm->GetWarnUnusedCli();
  obj[kCHECK_SYSTEM_VARS_KEY] = cm->GetCheckSystemVars();

638 639
  obj[kSOURCE_DIRECTORY_KEY] = this->GeneratorInfo.SourceDirectory;
  obj[kBUILD_DIRECTORY_KEY] = this->GeneratorInfo.BuildDirectory;
640 641

  // Currently used generator:
642 643
  obj[kGENERATOR_KEY] = this->GeneratorInfo.GeneratorName;
  obj[kEXTRA_GENERATOR_KEY] = this->GeneratorInfo.ExtraGeneratorName;
644 645 646

  return request.Reply(obj);
}
647 648

static void setBool(const cmServerRequest& request, const std::string& key,
Daniel Pfeifer's avatar
Daniel Pfeifer committed
649
                    std::function<void(bool)> const& setter)
650 651 652 653 654 655 656
{
  if (request.Data[key].isNull()) {
    return;
  }
  setter(request.Data[key].asBool());
}

657
cmServerResponse cmServerProtocol1::ProcessSetGlobalSettings(
658 659 660 661 662 663 664
  const cmServerRequest& request)
{
  const std::vector<std::string> boolValues = {
    kDEBUG_OUTPUT_KEY,       kTRACE_KEY,       kTRACE_EXPAND_KEY,
    kWARN_UNINITIALIZED_KEY, kWARN_UNUSED_KEY, kWARN_UNUSED_CLI_KEY,
    kCHECK_SYSTEM_VARS_KEY
  };
665
  for (std::string const& i : boolValues) {
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
    if (!request.Data[i].isNull() && !request.Data[i].isBool()) {
      return request.ReportError("\"" + i +
                                 "\" must be unset or a bool value.");
    }
  }

  cmake* cm = this->CMakeInstance();

  setBool(request, kDEBUG_OUTPUT_KEY,
          [cm](bool e) { cm->SetDebugOutputOn(e); });
  setBool(request, kTRACE_KEY, [cm](bool e) { cm->SetTrace(e); });
  setBool(request, kTRACE_EXPAND_KEY, [cm](bool e) { cm->SetTraceExpand(e); });
  setBool(request, kWARN_UNINITIALIZED_KEY,
          [cm](bool e) { cm->SetWarnUninitialized(e); });
  setBool(request, kWARN_UNUSED_KEY, [cm](bool e) { cm->SetWarnUnused(e); });
  setBool(request, kWARN_UNUSED_CLI_KEY,
          [cm](bool e) { cm->SetWarnUnusedCli(e); });
  setBool(request, kCHECK_SYSTEM_VARS_KEY,
          [cm](bool e) { cm->SetCheckSystemVars(e); });

  return request.Reply(Json::Value());
}
688

689
cmServerResponse cmServerProtocol1::ProcessFileSystemWatchers(
690 691 692 693 694
  const cmServerRequest& request)
{
  const cmFileMonitor* const fm = FileMonitor();
  Json::Value result = Json::objectValue;
  Json::Value files = Json::arrayValue;
695
  for (auto const& f : fm->WatchedFiles()) {
696 697 698
    files.append(f);
  }
  Json::Value directories = Json::arrayValue;
699
  for (auto const& d : fm->WatchedDirectories()) {
700 701 702 703 704 705 706
    directories.append(d);
  }
  result[kWATCHED_FILES_KEY] = files;
  result[kWATCHED_DIRECTORIES_KEY] = directories;

  return request.Reply(result);
}
707

708 709 710 711 712 713 714
cmServerResponse cmServerProtocol1::ProcessCTests(
  const cmServerRequest& request)
{
  if (this->m_State < STATE_COMPUTED) {
    return request.ReportError("This instance was not yet computed.");
  }

715
  return request.Reply(cmDumpCTestInfo(this->CMakeInstance()));
716 717
}

718
cmServerProtocol1::GeneratorInformation::GeneratorInformation(
wahikihiki's avatar
wahikihiki committed
719 720 721 722 723 724 725 726 727
  std::string generatorName, std::string extraGeneratorName,
  std::string toolset, std::string platform, std::string sourceDirectory,
  std::string buildDirectory)
  : GeneratorName(std::move(generatorName))
  , ExtraGeneratorName(std::move(extraGeneratorName))
  , Toolset(std::move(toolset))
  , Platform(std::move(platform))
  , SourceDirectory(std::move(sourceDirectory))
  , BuildDirectory(std::move(buildDirectory))
728 729 730
{
}

731
void cmServerProtocol1::GeneratorInformation::SetupGenerator(
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
  cmake* cm, std::string* errorMessage)
{
  const std::string fullGeneratorName =
    cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
      GeneratorName, ExtraGeneratorName);

  cm->SetHomeDirectory(SourceDirectory);
  cm->SetHomeOutputDirectory(BuildDirectory);

  cmGlobalGenerator* gg = cm->CreateGlobalGenerator(fullGeneratorName);
  if (!gg) {
    setErrorMessage(
      errorMessage,
      std::string("Could not set up the requested combination of \"") +
        kGENERATOR_KEY + "\" and \"" + kEXTRA_GENERATOR_KEY + "\"");
    return;
  }

  cm->SetGlobalGenerator(gg);

  cm->SetGeneratorToolset(Toolset);
  cm->SetGeneratorPlatform(Platform);
}