cmServerProtocol.cxx 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmServerProtocol.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmExternalMakefileProjectGenerator.h"
  6. #include "cmFileMonitor.h"
  7. #include "cmGeneratorExpression.h"
  8. #include "cmGeneratorTarget.h"
  9. #include "cmGlobalGenerator.h"
  10. #include "cmInstallGenerator.h"
  11. #include "cmInstallTargetGenerator.h"
  12. #include "cmLinkLineComputer.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmProperty.h"
  16. #include "cmServer.h"
  17. #include "cmServerDictionary.h"
  18. #include "cmSourceFile.h"
  19. #include "cmState.h"
  20. #include "cmStateDirectory.h"
  21. #include "cmStateSnapshot.h"
  22. #include "cmStateTypes.h"
  23. #include "cmSystemTools.h"
  24. #include "cmTarget.h"
  25. #include "cmTest.h"
  26. #include "cm_uv.h"
  27. #include "cmake.h"
  28. #include <algorithm>
  29. #include <cassert>
  30. #include <cstddef>
  31. #include <functional>
  32. #include <limits>
  33. #include <map>
  34. #include <memory>
  35. #include <set>
  36. #include <string>
  37. #include <unordered_map>
  38. #include <vector>
  39. // Get rid of some windows macros:
  40. #undef max
  41. namespace {
  42. std::vector<std::string> getConfigurations(const cmake* cm)
  43. {
  44. std::vector<std::string> configurations;
  45. auto makefiles = cm->GetGlobalGenerator()->GetMakefiles();
  46. if (makefiles.empty()) {
  47. return configurations;
  48. }
  49. makefiles[0]->GetConfigurations(configurations);
  50. if (configurations.empty()) {
  51. configurations.push_back("");
  52. }
  53. return configurations;
  54. }
  55. bool hasString(const Json::Value& v, const std::string& s)
  56. {
  57. return !v.isNull() &&
  58. std::any_of(v.begin(), v.end(),
  59. [s](const Json::Value& i) { return i.asString() == s; });
  60. }
  61. template <class T>
  62. Json::Value fromStringList(const T& in)
  63. {
  64. Json::Value result = Json::arrayValue;
  65. for (std::string const& i : in) {
  66. result.append(i);
  67. }
  68. return result;
  69. }
  70. std::vector<std::string> toStringList(const Json::Value& in)
  71. {
  72. std::vector<std::string> result;
  73. for (auto const& it : in) {
  74. result.push_back(it.asString());
  75. }
  76. return result;
  77. }
  78. void getCMakeInputs(const cmGlobalGenerator* gg, const std::string& sourceDir,
  79. const std::string& buildDir,
  80. std::vector<std::string>* internalFiles,
  81. std::vector<std::string>* explicitFiles,
  82. std::vector<std::string>* tmpFiles)
  83. {
  84. const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot() + '/';
  85. std::vector<cmMakefile*> const& makefiles = gg->GetMakefiles();
  86. for (cmMakefile const* mf : makefiles) {
  87. for (std::string const& lf : mf->GetListFiles()) {
  88. const std::string startOfFile = lf.substr(0, cmakeRootDir.size());
  89. const bool isInternal = (startOfFile == cmakeRootDir);
  90. const bool isTemporary = !isInternal && (lf.find(buildDir + '/') == 0);
  91. std::string toAdd = lf;
  92. if (!sourceDir.empty()) {
  93. const std::string& relative =
  94. cmSystemTools::RelativePath(sourceDir.c_str(), lf.c_str());
  95. if (toAdd.size() > relative.size()) {
  96. toAdd = relative;
  97. }
  98. }
  99. if (isInternal) {
  100. if (internalFiles) {
  101. internalFiles->push_back(toAdd);
  102. }
  103. } else {
  104. if (isTemporary) {
  105. if (tmpFiles) {
  106. tmpFiles->push_back(toAdd);
  107. }
  108. } else {
  109. if (explicitFiles) {
  110. explicitFiles->push_back(toAdd);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. } // namespace
  118. cmServerRequest::cmServerRequest(cmServer* server, cmConnection* connection,
  119. const std::string& t, const std::string& c,
  120. const Json::Value& d)
  121. : Type(t)
  122. , Cookie(c)
  123. , Data(d)
  124. , Connection(connection)
  125. , m_Server(server)
  126. {
  127. }
  128. void cmServerRequest::ReportProgress(int min, int current, int max,
  129. const std::string& message) const
  130. {
  131. this->m_Server->WriteProgress(*this, min, current, max, message);
  132. }
  133. void cmServerRequest::ReportMessage(const std::string& message,
  134. const std::string& title) const
  135. {
  136. m_Server->WriteMessage(*this, message, title);
  137. }
  138. cmServerResponse cmServerRequest::Reply(const Json::Value& data) const
  139. {
  140. cmServerResponse response(*this);
  141. response.SetData(data);
  142. return response;
  143. }
  144. cmServerResponse cmServerRequest::ReportError(const std::string& message) const
  145. {
  146. cmServerResponse response(*this);
  147. response.SetError(message);
  148. return response;
  149. }
  150. cmServerResponse::cmServerResponse(const cmServerRequest& request)
  151. : Type(request.Type)
  152. , Cookie(request.Cookie)
  153. {
  154. }
  155. void cmServerResponse::SetData(const Json::Value& data)
  156. {
  157. assert(this->m_Payload == PAYLOAD_UNKNOWN);
  158. if (!data[kCOOKIE_KEY].isNull() || !data[kTYPE_KEY].isNull()) {
  159. this->SetError("Response contains cookie or type field.");
  160. return;
  161. }
  162. this->m_Payload = PAYLOAD_DATA;
  163. this->m_Data = data;
  164. }
  165. void cmServerResponse::SetError(const std::string& message)
  166. {
  167. assert(this->m_Payload == PAYLOAD_UNKNOWN);
  168. this->m_Payload = PAYLOAD_ERROR;
  169. this->m_ErrorMessage = message;
  170. }
  171. bool cmServerResponse::IsComplete() const
  172. {
  173. return this->m_Payload != PAYLOAD_UNKNOWN;
  174. }
  175. bool cmServerResponse::IsError() const
  176. {
  177. assert(this->m_Payload != PAYLOAD_UNKNOWN);
  178. return this->m_Payload == PAYLOAD_ERROR;
  179. }
  180. std::string cmServerResponse::ErrorMessage() const
  181. {
  182. if (this->m_Payload == PAYLOAD_ERROR) {
  183. return this->m_ErrorMessage;
  184. }
  185. return std::string();
  186. }
  187. Json::Value cmServerResponse::Data() const
  188. {
  189. assert(this->m_Payload != PAYLOAD_UNKNOWN);
  190. return this->m_Data;
  191. }
  192. bool cmServerProtocol::Activate(cmServer* server,
  193. const cmServerRequest& request,
  194. std::string* errorMessage)
  195. {
  196. assert(server);
  197. this->m_Server = server;
  198. this->m_CMakeInstance = cm::make_unique<cmake>(cmake::RoleProject);
  199. const bool result = this->DoActivate(request, errorMessage);
  200. if (!result) {
  201. this->m_CMakeInstance = nullptr;
  202. }
  203. return result;
  204. }
  205. cmFileMonitor* cmServerProtocol::FileMonitor() const
  206. {
  207. return this->m_Server ? this->m_Server->FileMonitor() : nullptr;
  208. }
  209. void cmServerProtocol::SendSignal(const std::string& name,
  210. const Json::Value& data) const
  211. {
  212. if (this->m_Server) {
  213. this->m_Server->WriteSignal(name, data);
  214. }
  215. }
  216. cmake* cmServerProtocol::CMakeInstance() const
  217. {
  218. return this->m_CMakeInstance.get();
  219. }
  220. bool cmServerProtocol::DoActivate(const cmServerRequest& /*request*/,
  221. std::string* /*errorMessage*/)
  222. {
  223. return true;
  224. }
  225. std::pair<int, int> cmServerProtocol1::ProtocolVersion() const
  226. {
  227. return std::make_pair(1, 2);
  228. }
  229. static void setErrorMessage(std::string* errorMessage, const std::string& text)
  230. {
  231. if (errorMessage) {
  232. *errorMessage = text;
  233. }
  234. }
  235. static bool testHomeDirectory(cmState* state, std::string& value,
  236. std::string* errorMessage)
  237. {
  238. const std::string cachedValue =
  239. std::string(state->GetCacheEntryValue("CMAKE_HOME_DIRECTORY"));
  240. if (value.empty()) {
  241. value = cachedValue;
  242. return true;
  243. }
  244. const std::string suffix = "/CMakeLists.txt";
  245. const std::string cachedValueCML = cachedValue + suffix;
  246. const std::string valueCML = value + suffix;
  247. if (!cmSystemTools::SameFile(valueCML, cachedValueCML)) {
  248. setErrorMessage(errorMessage,
  249. std::string("\"CMAKE_HOME_DIRECTORY\" is set but "
  250. "incompatible with configured "
  251. "source directory value."));
  252. return false;
  253. }
  254. return true;
  255. }
  256. static bool testValue(cmState* state, const std::string& key,
  257. std::string& value, const std::string& keyDescription,
  258. std::string* errorMessage)
  259. {
  260. const char* entry = state->GetCacheEntryValue(key);
  261. const std::string cachedValue =
  262. entry == nullptr ? std::string() : std::string(entry);
  263. if (value.empty()) {
  264. value = cachedValue;
  265. }
  266. if (!cachedValue.empty() && cachedValue != value) {
  267. setErrorMessage(errorMessage, std::string("\"") + key +
  268. "\" is set but incompatible with configured " +
  269. keyDescription + " value.");
  270. return false;
  271. }
  272. return true;
  273. }
  274. bool cmServerProtocol1::DoActivate(const cmServerRequest& request,
  275. std::string* errorMessage)
  276. {
  277. std::string sourceDirectory = request.Data[kSOURCE_DIRECTORY_KEY].asString();
  278. const std::string buildDirectory =
  279. request.Data[kBUILD_DIRECTORY_KEY].asString();
  280. std::string generator = request.Data[kGENERATOR_KEY].asString();
  281. std::string extraGenerator = request.Data[kEXTRA_GENERATOR_KEY].asString();
  282. std::string toolset = request.Data[kTOOLSET_KEY].asString();
  283. std::string platform = request.Data[kPLATFORM_KEY].asString();
  284. if (buildDirectory.empty()) {
  285. setErrorMessage(errorMessage, std::string("\"") + kBUILD_DIRECTORY_KEY +
  286. "\" is missing.");
  287. return false;
  288. }
  289. cmake* cm = CMakeInstance();
  290. if (cmSystemTools::PathExists(buildDirectory)) {
  291. if (!cmSystemTools::FileIsDirectory(buildDirectory)) {
  292. setErrorMessage(errorMessage, std::string("\"") + kBUILD_DIRECTORY_KEY +
  293. "\" exists but is not a directory.");
  294. return false;
  295. }
  296. const std::string cachePath = cm->FindCacheFile(buildDirectory);
  297. if (cm->LoadCache(cachePath)) {
  298. cmState* state = cm->GetState();
  299. // Check generator:
  300. if (!testValue(state, "CMAKE_GENERATOR", generator, "generator",
  301. errorMessage)) {
  302. return false;
  303. }
  304. // check extra generator:
  305. if (!testValue(state, "CMAKE_EXTRA_GENERATOR", extraGenerator,
  306. "extra generator", errorMessage)) {
  307. return false;
  308. }
  309. // check sourcedir:
  310. if (!testHomeDirectory(state, sourceDirectory, errorMessage)) {
  311. return false;
  312. }
  313. // check toolset:
  314. if (!testValue(state, "CMAKE_GENERATOR_TOOLSET", toolset, "toolset",
  315. errorMessage)) {
  316. return false;
  317. }
  318. // check platform:
  319. if (!testValue(state, "CMAKE_GENERATOR_PLATFORM", platform, "platform",
  320. errorMessage)) {
  321. return false;
  322. }
  323. }
  324. }
  325. if (sourceDirectory.empty()) {
  326. setErrorMessage(errorMessage, std::string("\"") + kSOURCE_DIRECTORY_KEY +
  327. "\" is unset but required.");
  328. return false;
  329. }
  330. if (!cmSystemTools::FileIsDirectory(sourceDirectory)) {
  331. setErrorMessage(errorMessage, std::string("\"") + kSOURCE_DIRECTORY_KEY +
  332. "\" is not a directory.");
  333. return false;
  334. }
  335. if (generator.empty()) {
  336. setErrorMessage(errorMessage, std::string("\"") + kGENERATOR_KEY +
  337. "\" is unset but required.");
  338. return false;
  339. }
  340. std::vector<cmake::GeneratorInfo> generators;
  341. cm->GetRegisteredGenerators(generators);
  342. auto baseIt = std::find_if(generators.begin(), generators.end(),
  343. [&generator](const cmake::GeneratorInfo& info) {
  344. return info.name == generator;
  345. });
  346. if (baseIt == generators.end()) {
  347. setErrorMessage(errorMessage, std::string("Generator \"") + generator +
  348. "\" not supported.");
  349. return false;
  350. }
  351. auto extraIt = std::find_if(
  352. generators.begin(), generators.end(),
  353. [&generator, &extraGenerator](const cmake::GeneratorInfo& info) {
  354. return info.baseName == generator && info.extraName == extraGenerator;
  355. });
  356. if (extraIt == generators.end()) {
  357. setErrorMessage(errorMessage,
  358. std::string("The combination of generator \"" + generator +
  359. "\" and extra generator \"" + extraGenerator +
  360. "\" is not supported."));
  361. return false;
  362. }
  363. if (!extraIt->supportsToolset && !toolset.empty()) {
  364. setErrorMessage(errorMessage,
  365. std::string("Toolset was provided but is not supported by "
  366. "the requested generator."));
  367. return false;
  368. }
  369. if (!extraIt->supportsPlatform && !platform.empty()) {
  370. setErrorMessage(errorMessage,
  371. std::string("Platform was provided but is not supported "
  372. "by the requested generator."));
  373. return false;
  374. }
  375. this->GeneratorInfo =
  376. GeneratorInformation(generator, extraGenerator, toolset, platform,
  377. sourceDirectory, buildDirectory);
  378. this->m_State = STATE_ACTIVE;
  379. return true;
  380. }
  381. void cmServerProtocol1::HandleCMakeFileChanges(const std::string& path,
  382. int event, int status)
  383. {
  384. assert(status == 0);
  385. static_cast<void>(status);
  386. if (!m_isDirty) {
  387. m_isDirty = true;
  388. SendSignal(kDIRTY_SIGNAL, Json::objectValue);
  389. }
  390. Json::Value obj = Json::objectValue;
  391. obj[kPATH_KEY] = path;
  392. Json::Value properties = Json::arrayValue;
  393. if (event & UV_RENAME) {
  394. properties.append(kRENAME_PROPERTY_VALUE);
  395. }
  396. if (event & UV_CHANGE) {
  397. properties.append(kCHANGE_PROPERTY_VALUE);
  398. }
  399. obj[kPROPERTIES_KEY] = properties;
  400. SendSignal(kFILE_CHANGE_SIGNAL, obj);
  401. }
  402. const cmServerResponse cmServerProtocol1::Process(
  403. const cmServerRequest& request)
  404. {
  405. assert(this->m_State >= STATE_ACTIVE);
  406. if (request.Type == kCACHE_TYPE) {
  407. return this->ProcessCache(request);
  408. }
  409. if (request.Type == kCMAKE_INPUTS_TYPE) {
  410. return this->ProcessCMakeInputs(request);
  411. }
  412. if (request.Type == kCODE_MODEL_TYPE) {
  413. return this->ProcessCodeModel(request);
  414. }
  415. if (request.Type == kCOMPUTE_TYPE) {
  416. return this->ProcessCompute(request);
  417. }
  418. if (request.Type == kCONFIGURE_TYPE) {
  419. return this->ProcessConfigure(request);
  420. }
  421. if (request.Type == kFILESYSTEM_WATCHERS_TYPE) {
  422. return this->ProcessFileSystemWatchers(request);
  423. }
  424. if (request.Type == kGLOBAL_SETTINGS_TYPE) {
  425. return this->ProcessGlobalSettings(request);
  426. }
  427. if (request.Type == kSET_GLOBAL_SETTINGS_TYPE) {
  428. return this->ProcessSetGlobalSettings(request);
  429. }
  430. if (request.Type == kCTEST_INFO_TYPE) {
  431. return this->ProcessCTests(request);
  432. }
  433. return request.ReportError("Unknown command!");
  434. }
  435. bool cmServerProtocol1::IsExperimental() const
  436. {
  437. return true;
  438. }
  439. cmServerResponse cmServerProtocol1::ProcessCache(
  440. const cmServerRequest& request)
  441. {
  442. cmState* state = this->CMakeInstance()->GetState();
  443. Json::Value result = Json::objectValue;
  444. std::vector<std::string> allKeys = state->GetCacheEntryKeys();
  445. Json::Value list = Json::arrayValue;
  446. std::vector<std::string> keys = toStringList(request.Data[kKEYS_KEY]);
  447. if (keys.empty()) {
  448. keys = allKeys;
  449. } else {
  450. for (auto const& i : keys) {
  451. if (std::find(allKeys.begin(), allKeys.end(), i) == allKeys.end()) {
  452. return request.ReportError("Key \"" + i + "\" not found in cache.");
  453. }
  454. }
  455. }
  456. std::sort(keys.begin(), keys.end());
  457. for (auto const& key : keys) {
  458. Json::Value entry = Json::objectValue;
  459. entry[kKEY_KEY] = key;
  460. entry[kTYPE_KEY] =
  461. cmState::CacheEntryTypeToString(state->GetCacheEntryType(key));
  462. entry[kVALUE_KEY] = state->GetCacheEntryValue(key);
  463. Json::Value props = Json::objectValue;
  464. bool haveProperties = false;
  465. for (auto const& prop : state->GetCacheEntryPropertyList(key)) {
  466. haveProperties = true;
  467. props[prop] = state->GetCacheEntryProperty(key, prop);
  468. }
  469. if (haveProperties) {
  470. entry[kPROPERTIES_KEY] = props;
  471. }
  472. list.append(entry);
  473. }
  474. result[kCACHE_KEY] = list;
  475. return request.Reply(result);
  476. }
  477. cmServerResponse cmServerProtocol1::ProcessCMakeInputs(
  478. const cmServerRequest& request)
  479. {
  480. if (this->m_State < STATE_CONFIGURED) {
  481. return request.ReportError("This instance was not yet configured.");
  482. }
  483. const cmake* cm = this->CMakeInstance();
  484. const cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  485. const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot();
  486. const std::string buildDir = cm->GetHomeOutputDirectory();
  487. const std::string sourceDir = cm->GetHomeDirectory();
  488. Json::Value result = Json::objectValue;
  489. result[kSOURCE_DIRECTORY_KEY] = sourceDir;
  490. result[kCMAKE_ROOT_DIRECTORY_KEY] = cmakeRootDir;
  491. std::vector<std::string> internalFiles;
  492. std::vector<std::string> explicitFiles;
  493. std::vector<std::string> tmpFiles;
  494. getCMakeInputs(gg, sourceDir, buildDir, &internalFiles, &explicitFiles,
  495. &tmpFiles);
  496. Json::Value array = Json::arrayValue;
  497. Json::Value tmp = Json::objectValue;
  498. tmp[kIS_CMAKE_KEY] = true;
  499. tmp[kIS_TEMPORARY_KEY] = false;
  500. tmp[kSOURCES_KEY] = fromStringList(internalFiles);
  501. array.append(tmp);
  502. tmp = Json::objectValue;
  503. tmp[kIS_CMAKE_KEY] = false;
  504. tmp[kIS_TEMPORARY_KEY] = false;
  505. tmp[kSOURCES_KEY] = fromStringList(explicitFiles);
  506. array.append(tmp);
  507. tmp = Json::objectValue;
  508. tmp[kIS_CMAKE_KEY] = false;
  509. tmp[kIS_TEMPORARY_KEY] = true;
  510. tmp[kSOURCES_KEY] = fromStringList(tmpFiles);
  511. array.append(tmp);
  512. result[kBUILD_FILES_KEY] = array;
  513. return request.Reply(result);
  514. }
  515. class LanguageData
  516. {
  517. public:
  518. bool operator==(const LanguageData& other) const;
  519. void SetDefines(const std::set<std::string>& defines);
  520. bool IsGenerated = false;
  521. std::string Language;
  522. std::string Flags;
  523. std::vector<std::string> Defines;
  524. std::vector<std::pair<std::string, bool>> IncludePathList;
  525. };
  526. bool LanguageData::operator==(const LanguageData& other) const
  527. {
  528. return Language == other.Language && Defines == other.Defines &&
  529. Flags == other.Flags && IncludePathList == other.IncludePathList &&
  530. IsGenerated == other.IsGenerated;
  531. }
  532. void LanguageData::SetDefines(const std::set<std::string>& defines)
  533. {
  534. std::vector<std::string> result;
  535. result.reserve(defines.size());
  536. for (std::string const& i : defines) {
  537. result.push_back(i);
  538. }
  539. std::sort(result.begin(), result.end());
  540. Defines = std::move(result);
  541. }
  542. namespace std {
  543. template <>
  544. struct hash<LanguageData>
  545. {
  546. std::size_t operator()(const LanguageData& in) const
  547. {
  548. using std::hash;
  549. size_t result =
  550. hash<std::string>()(in.Language) ^ hash<std::string>()(in.Flags);
  551. for (auto const& i : in.IncludePathList) {
  552. result = result ^ (hash<std::string>()(i.first) ^
  553. (i.second ? std::numeric_limits<size_t>::max() : 0));
  554. }
  555. for (auto const& i : in.Defines) {
  556. result = result ^ hash<std::string>()(i);
  557. }
  558. result =
  559. result ^ (in.IsGenerated ? std::numeric_limits<size_t>::max() : 0);
  560. return result;
  561. }
  562. };
  563. } // namespace std
  564. static Json::Value DumpSourceFileGroup(const LanguageData& data,
  565. const std::vector<std::string>& files,
  566. const std::string& baseDir)
  567. {
  568. Json::Value result = Json::objectValue;
  569. if (!data.Language.empty()) {
  570. result[kLANGUAGE_KEY] = data.Language;
  571. if (!data.Flags.empty()) {
  572. result[kCOMPILE_FLAGS_KEY] = data.Flags;
  573. }
  574. if (!data.IncludePathList.empty()) {
  575. Json::Value includes = Json::arrayValue;
  576. for (auto const& i : data.IncludePathList) {
  577. Json::Value tmp = Json::objectValue;
  578. tmp[kPATH_KEY] = i.first;
  579. if (i.second) {
  580. tmp[kIS_SYSTEM_KEY] = i.second;
  581. }
  582. includes.append(tmp);
  583. }
  584. result[kINCLUDE_PATH_KEY] = includes;
  585. }
  586. if (!data.Defines.empty()) {
  587. result[kDEFINES_KEY] = fromStringList(data.Defines);
  588. }
  589. }
  590. result[kIS_GENERATED_KEY] = data.IsGenerated;
  591. Json::Value sourcesValue = Json::arrayValue;
  592. for (auto const& i : files) {
  593. const std::string relPath =
  594. cmSystemTools::RelativePath(baseDir.c_str(), i.c_str());
  595. sourcesValue.append(relPath.size() < i.size() ? relPath : i);
  596. }
  597. result[kSOURCES_KEY] = sourcesValue;
  598. return result;
  599. }
  600. static Json::Value DumpSourceFilesList(
  601. cmGeneratorTarget* target, const std::string& config,
  602. const std::map<std::string, LanguageData>& languageDataMap)
  603. {
  604. // Collect sourcefile groups:
  605. std::vector<cmSourceFile*> files;
  606. target->GetSourceFiles(files, config);
  607. std::unordered_map<LanguageData, std::vector<std::string>> fileGroups;
  608. for (cmSourceFile* file : files) {
  609. LanguageData fileData;
  610. fileData.Language = file->GetLanguage();
  611. if (!fileData.Language.empty()) {
  612. const LanguageData& ld = languageDataMap.at(fileData.Language);
  613. cmLocalGenerator* lg = target->GetLocalGenerator();
  614. cmGeneratorExpressionInterpreter genexInterpreter(
  615. lg, target, config, target->GetName(), fileData.Language);
  616. std::string compileFlags = ld.Flags;
  617. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  618. if (const char* cflags = file->GetProperty(COMPILE_FLAGS)) {
  619. lg->AppendFlags(compileFlags,
  620. genexInterpreter.Evaluate(cflags, COMPILE_FLAGS));
  621. }
  622. fileData.Flags = compileFlags;
  623. fileData.IncludePathList = ld.IncludePathList;
  624. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  625. std::set<std::string> defines;
  626. if (const char* defs = file->GetProperty(COMPILE_DEFINITIONS)) {
  627. lg->AppendDefines(
  628. defines, genexInterpreter.Evaluate(defs, COMPILE_DEFINITIONS));
  629. }
  630. const std::string defPropName =
  631. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  632. if (const char* config_defs = file->GetProperty(defPropName)) {
  633. lg->AppendDefines(defines, genexInterpreter.Evaluate(
  634. config_defs, COMPILE_DEFINITIONS));
  635. }
  636. defines.insert(ld.Defines.begin(), ld.Defines.end());
  637. fileData.SetDefines(defines);
  638. }
  639. fileData.IsGenerated = file->GetPropertyAsBool("GENERATED");
  640. std::vector<std::string>& groupFileList = fileGroups[fileData];
  641. groupFileList.push_back(file->GetFullPath());
  642. }
  643. const std::string baseDir = target->Makefile->GetCurrentSourceDirectory();
  644. Json::Value result = Json::arrayValue;
  645. for (auto const& it : fileGroups) {
  646. Json::Value group = DumpSourceFileGroup(it.first, it.second, baseDir);
  647. if (!group.isNull()) {
  648. result.append(group);
  649. }
  650. }
  651. return result;
  652. }
  653. static Json::Value DumpCTestInfo(cmLocalGenerator* lg, cmTest* testInfo,
  654. const std::string& config)
  655. {
  656. Json::Value result = Json::objectValue;
  657. result[kCTEST_NAME] = testInfo->GetName();
  658. // Concat command entries together. After the first should be the arguments
  659. // for the command
  660. std::string command;
  661. for (auto const& cmd : testInfo->GetCommand()) {
  662. command.append(cmd);
  663. command.append(" ");
  664. }
  665. // Remove any config specific variables from the output.
  666. cmGeneratorExpression ge;
  667. auto cge = ge.Parse(command.c_str());
  668. const char* processed = cge->Evaluate(lg, config);
  669. result[kCTEST_COMMAND] = processed;
  670. // Build up the list of properties that may have been specified
  671. Json::Value properties = Json::arrayValue;
  672. for (auto& prop : testInfo->GetProperties()) {
  673. Json::Value entry = Json::objectValue;
  674. entry[kKEY_KEY] = prop.first;
  675. // Remove config variables from the value too.
  676. auto cge_value = ge.Parse(prop.second.GetValue());
  677. const char* processed_value = cge_value->Evaluate(lg, config);
  678. entry[kVALUE_KEY] = processed_value;
  679. properties.append(entry);
  680. }
  681. result[kPROPERTIES_KEY] = properties;
  682. return result;
  683. }
  684. static void DumpMakefileTests(cmLocalGenerator* lg, const std::string& config,
  685. Json::Value* result)
  686. {
  687. auto mf = lg->GetMakefile();
  688. std::vector<cmTest*> tests;
  689. mf->GetTests(config, tests);
  690. for (auto test : tests) {
  691. Json::Value tmp = DumpCTestInfo(lg, test, config);
  692. if (!tmp.isNull()) {
  693. result->append(tmp);
  694. }
  695. }
  696. }
  697. static Json::Value DumpCTestProjectList(const cmake* cm,
  698. std::string const& config)
  699. {
  700. Json::Value result = Json::arrayValue;
  701. auto globalGen = cm->GetGlobalGenerator();
  702. for (const auto& projectIt : globalGen->GetProjectMap()) {
  703. Json::Value pObj = Json::objectValue;
  704. pObj[kNAME_KEY] = projectIt.first;
  705. Json::Value tests = Json::arrayValue;
  706. // Gather tests for every generator
  707. for (const auto& lg : projectIt.second) {
  708. // Make sure they're generated.
  709. lg->GenerateTestFiles();
  710. DumpMakefileTests(lg, config, &tests);
  711. }
  712. pObj[kCTEST_INFO] = tests;
  713. result.append(pObj);
  714. }
  715. return result;
  716. }
  717. static Json::Value DumpCTestConfiguration(const cmake* cm,
  718. const std::string& config)
  719. {
  720. Json::Value result = Json::objectValue;
  721. result[kNAME_KEY] = config;
  722. result[kPROJECTS_KEY] = DumpCTestProjectList(cm, config);
  723. return result;
  724. }
  725. static Json::Value DumpCTestConfigurationsList(const cmake* cm)
  726. {
  727. Json::Value result = Json::arrayValue;
  728. for (const std::string& c : getConfigurations(cm)) {
  729. result.append(DumpCTestConfiguration(cm, c));
  730. }
  731. return result;
  732. }
  733. static Json::Value DumpTarget(cmGeneratorTarget* target,
  734. const std::string& config)
  735. {
  736. cmLocalGenerator* lg = target->GetLocalGenerator();
  737. const cmState* state = lg->GetState();
  738. const cmStateEnums::TargetType type = target->GetType();
  739. const std::string typeName = state->GetTargetTypeName(type);
  740. Json::Value ttl = Json::arrayValue;
  741. ttl.append("EXECUTABLE");
  742. ttl.append("STATIC_LIBRARY");
  743. ttl.append("SHARED_LIBRARY");
  744. ttl.append("MODULE_LIBRARY");
  745. ttl.append("OBJECT_LIBRARY");
  746. ttl.append("UTILITY");
  747. ttl.append("INTERFACE_LIBRARY");
  748. if (!hasString(ttl, typeName) || target->IsImported()) {
  749. return Json::Value();
  750. }
  751. Json::Value result = Json::objectValue;
  752. result[kNAME_KEY] = target->GetName();
  753. result[kIS_GENERATOR_PROVIDED_KEY] =
  754. target->Target->GetIsGeneratorProvided();
  755. result[kTYPE_KEY] = typeName;
  756. result[kSOURCE_DIRECTORY_KEY] = lg->GetCurrentSourceDirectory();
  757. result[kBUILD_DIRECTORY_KEY] = lg->GetCurrentBinaryDirectory();
  758. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  759. return result;
  760. }
  761. result[kFULL_NAME_KEY] = target->GetFullName(config);
  762. if (target->Target->GetHaveInstallRule()) {
  763. result[kHAS_INSTALL_RULE] = true;
  764. Json::Value installPaths = Json::arrayValue;
  765. auto targetGenerators = target->Makefile->GetInstallGenerators();
  766. for (auto installGenerator : targetGenerators) {
  767. auto installTargetGenerator =
  768. dynamic_cast<cmInstallTargetGenerator*>(installGenerator);
  769. if (installTargetGenerator != nullptr &&
  770. installTargetGenerator->GetTarget()->Target == target->Target) {
  771. auto dest = installTargetGenerator->GetDestination(config);
  772. std::string installPath;
  773. if (!dest.empty() && cmSystemTools::FileIsFullPath(dest.c_str())) {
  774. installPath = dest;
  775. } else {
  776. std::string installPrefix =
  777. target->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  778. installPath = installPrefix + '/' + dest;
  779. }
  780. installPaths.append(installPath);
  781. }
  782. }
  783. result[kINSTALL_PATHS] = installPaths;
  784. }
  785. if (target->HaveWellDefinedOutputFiles()) {
  786. Json::Value artifacts = Json::arrayValue;
  787. artifacts.append(
  788. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact));
  789. if (target->IsDLLPlatform()) {
  790. artifacts.append(
  791. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact));
  792. const cmGeneratorTarget::OutputInfo* output =
  793. target->GetOutputInfo(config);
  794. if (output && !output->PdbDir.empty()) {
  795. artifacts.append(output->PdbDir + '/' + target->GetPDBName(config));
  796. }
  797. }
  798. result[kARTIFACTS_KEY] = artifacts;
  799. result[kLINKER_LANGUAGE_KEY] = target->GetLinkerLanguage(config);
  800. std::string linkLibs;
  801. std::string linkFlags;
  802. std::string linkLanguageFlags;
  803. std::string frameworkPath;
  804. std::string linkPath;
  805. cmLinkLineComputer linkLineComputer(lg,
  806. lg->GetStateSnapshot().GetDirectory());
  807. lg->GetTargetFlags(&linkLineComputer, config, linkLibs, linkLanguageFlags,
  808. linkFlags, frameworkPath, linkPath, target);
  809. linkLibs = cmSystemTools::TrimWhitespace(linkLibs);
  810. linkFlags = cmSystemTools::TrimWhitespace(linkFlags);
  811. linkLanguageFlags = cmSystemTools::TrimWhitespace(linkLanguageFlags);
  812. frameworkPath = cmSystemTools::TrimWhitespace(frameworkPath);
  813. linkPath = cmSystemTools::TrimWhitespace(linkPath);
  814. if (!cmSystemTools::TrimWhitespace(linkLibs).empty()) {
  815. result[kLINK_LIBRARIES_KEY] = linkLibs;
  816. }
  817. if (!cmSystemTools::TrimWhitespace(linkFlags).empty()) {
  818. result[kLINK_FLAGS_KEY] = linkFlags;
  819. }
  820. if (!cmSystemTools::TrimWhitespace(linkLanguageFlags).empty()) {
  821. result[kLINK_LANGUAGE_FLAGS_KEY] = linkLanguageFlags;
  822. }
  823. if (!frameworkPath.empty()) {
  824. result[kFRAMEWORK_PATH_KEY] = frameworkPath;
  825. }
  826. if (!linkPath.empty()) {
  827. result[kLINK_PATH_KEY] = linkPath;
  828. }
  829. const std::string sysroot =
  830. lg->GetMakefile()->GetSafeDefinition("CMAKE_SYSROOT");
  831. if (!sysroot.empty()) {
  832. result[kSYSROOT_KEY] = sysroot;
  833. }
  834. }
  835. std::set<std::string> languages;
  836. target->GetLanguages(languages, config);
  837. std::map<std::string, LanguageData> languageDataMap;
  838. for (std::string const& lang : languages) {
  839. LanguageData& ld = languageDataMap[lang];
  840. ld.Language = lang;
  841. lg->GetTargetCompileFlags(target, config, lang, ld.Flags);
  842. std::set<std::string> defines;
  843. lg->GetTargetDefines(target, config, lang, defines);
  844. ld.SetDefines(defines);
  845. std::vector<std::string> includePathList;
  846. lg->GetIncludeDirectories(includePathList, target, lang, config, true);
  847. for (std::string const& i : includePathList) {
  848. ld.IncludePathList.push_back(
  849. std::make_pair(i, target->IsSystemIncludeDirectory(i, config)));
  850. }
  851. }
  852. Json::Value sourceGroupsValue =
  853. DumpSourceFilesList(target, config, languageDataMap);
  854. if (!sourceGroupsValue.empty()) {
  855. result[kFILE_GROUPS_KEY] = sourceGroupsValue;
  856. }
  857. return result;
  858. }
  859. static Json::Value DumpTargetsList(
  860. const std::vector<cmLocalGenerator*>& generators, const std::string& config)
  861. {
  862. Json::Value result = Json::arrayValue;
  863. std::vector<cmGeneratorTarget*> targetList;
  864. for (auto const& lgIt : generators) {
  865. const auto& list = lgIt->GetGeneratorTargets();
  866. targetList.insert(targetList.end(), list.begin(), list.end());
  867. }
  868. std::sort(targetList.begin(), targetList.end());
  869. for (cmGeneratorTarget* target : targetList) {
  870. Json::Value tmp = DumpTarget(target, config);
  871. if (!tmp.isNull()) {
  872. result.append(tmp);
  873. }
  874. }
  875. return result;
  876. }
  877. static Json::Value DumpProjectList(const cmake* cm, std::string const& config)
  878. {
  879. Json::Value result = Json::arrayValue;
  880. auto globalGen = cm->GetGlobalGenerator();
  881. for (auto const& projectIt : globalGen->GetProjectMap()) {
  882. Json::Value pObj = Json::objectValue;
  883. pObj[kNAME_KEY] = projectIt.first;
  884. // All Projects must have at least one local generator
  885. assert(!projectIt.second.empty());
  886. const cmLocalGenerator* lg = projectIt.second.at(0);
  887. // Project structure information:
  888. const cmMakefile* mf = lg->GetMakefile();
  889. auto minVersion = mf->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  890. pObj[kMINIMUM_CMAKE_VERSION] = minVersion ? minVersion : "";
  891. pObj[kSOURCE_DIRECTORY_KEY] = mf->GetCurrentSourceDirectory();
  892. pObj[kBUILD_DIRECTORY_KEY] = mf->GetCurrentBinaryDirectory();
  893. pObj[kTARGETS_KEY] = DumpTargetsList(projectIt.second, config);
  894. // For a project-level install rule it might be defined in any of its
  895. // associated generators.
  896. bool hasInstallRule = false;
  897. for (const auto generator : projectIt.second) {
  898. hasInstallRule =
  899. generator->GetMakefile()->GetInstallGenerators().empty() == false;
  900. if (hasInstallRule) {
  901. break;
  902. }
  903. }
  904. pObj[kHAS_INSTALL_RULE] = hasInstallRule;
  905. result.append(pObj);
  906. }
  907. return result;
  908. }
  909. static Json::Value DumpConfiguration(const cmake* cm,
  910. const std::string& config)
  911. {
  912. Json::Value result = Json::objectValue;
  913. result[kNAME_KEY] = config;
  914. result[kPROJECTS_KEY] = DumpProjectList(cm, config);
  915. return result;
  916. }
  917. static Json::Value DumpConfigurationsList(const cmake* cm)
  918. {
  919. Json::Value result = Json::arrayValue;
  920. for (std::string const& c : getConfigurations(cm)) {
  921. result.append(DumpConfiguration(cm, c));
  922. }
  923. return result;
  924. }
  925. cmServerResponse cmServerProtocol1::ProcessCodeModel(
  926. const cmServerRequest& request)
  927. {
  928. if (this->m_State != STATE_COMPUTED) {
  929. return request.ReportError("No build system was generated yet.");
  930. }
  931. Json::Value result = Json::objectValue;
  932. result[kCONFIGURATIONS_KEY] = DumpConfigurationsList(this->CMakeInstance());
  933. return request.Reply(result);
  934. }
  935. cmServerResponse cmServerProtocol1::ProcessCompute(
  936. const cmServerRequest& request)
  937. {
  938. if (this->m_State > STATE_CONFIGURED) {
  939. return request.ReportError("This build system was already generated.");
  940. }
  941. if (this->m_State < STATE_CONFIGURED) {
  942. return request.ReportError("This project was not configured yet.");
  943. }
  944. cmake* cm = this->CMakeInstance();
  945. int ret = cm->Generate();
  946. if (ret < 0) {
  947. return request.ReportError("Failed to compute build system.");
  948. }
  949. m_State = STATE_COMPUTED;
  950. return request.Reply(Json::Value());
  951. }
  952. cmServerResponse cmServerProtocol1::ProcessConfigure(
  953. const cmServerRequest& request)
  954. {
  955. if (this->m_State == STATE_INACTIVE) {
  956. return request.ReportError("This instance is inactive.");
  957. }
  958. FileMonitor()->StopMonitoring();
  959. std::string errorMessage;
  960. cmake* cm = this->CMakeInstance();
  961. this->GeneratorInfo.SetupGenerator(cm, &errorMessage);
  962. if (!errorMessage.empty()) {
  963. return request.ReportError(errorMessage);
  964. }
  965. // Make sure the types of cacheArguments matches (if given):
  966. std::vector<std::string> cacheArgs = { "unused" };
  967. bool cacheArgumentsError = false;
  968. const Json::Value passedArgs = request.Data[kCACHE_ARGUMENTS_KEY];
  969. if (!passedArgs.isNull()) {
  970. if (passedArgs.isString()) {
  971. cacheArgs.push_back(passedArgs.asString());
  972. } else if (passedArgs.isArray()) {
  973. for (auto const& arg : passedArgs) {
  974. if (!arg.isString()) {
  975. cacheArgumentsError = true;
  976. break;
  977. }
  978. cacheArgs.push_back(arg.asString());
  979. }
  980. } else {
  981. cacheArgumentsError = true;
  982. }
  983. }
  984. if (cacheArgumentsError) {
  985. request.ReportError(
  986. "cacheArguments must be unset, a string or an array of strings.");
  987. }
  988. std::string sourceDir = cm->GetHomeDirectory();
  989. const std::string buildDir = cm->GetHomeOutputDirectory();
  990. cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  991. if (buildDir.empty()) {
  992. return request.ReportError("No build directory set via Handshake.");
  993. }
  994. if (cm->LoadCache(buildDir)) {
  995. // build directory has been set up before
  996. const char* cachedSourceDir =
  997. cm->GetState()->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
  998. if (!cachedSourceDir) {
  999. return request.ReportError("No CMAKE_HOME_DIRECTORY found in cache.");
  1000. }
  1001. if (sourceDir.empty()) {
  1002. sourceDir = std::string(cachedSourceDir);
  1003. cm->SetHomeDirectory(sourceDir);
  1004. }
  1005. const char* cachedGenerator =
  1006. cm->GetState()->GetInitializedCacheValue("CMAKE_GENERATOR");
  1007. if (cachedGenerator) {
  1008. if (gg && gg->GetName() != cachedGenerator) {
  1009. return request.ReportError("Configured generator does not match with "
  1010. "CMAKE_GENERATOR found in cache.");
  1011. }
  1012. }
  1013. } else {
  1014. // build directory has not been set up before
  1015. if (sourceDir.empty()) {
  1016. return request.ReportError("No sourceDirectory set via "
  1017. "setGlobalSettings and no cache found in "
  1018. "buildDirectory.");
  1019. }
  1020. }
  1021. cmSystemTools::ResetErrorOccuredFlag(); // Reset error state
  1022. if (cm->AddCMakePaths() != 1) {
  1023. return request.ReportError("Failed to set CMake paths.");
  1024. }
  1025. if (!cm->SetCacheArgs(cacheArgs)) {
  1026. return request.ReportError("cacheArguments could not be set.");
  1027. }
  1028. int ret = cm->Configure();
  1029. if (ret < 0) {
  1030. return request.ReportError("Configuration failed.");
  1031. }
  1032. std::vector<std::string> toWatchList;
  1033. getCMakeInputs(gg, std::string(), buildDir, nullptr, &toWatchList, nullptr);
  1034. FileMonitor()->MonitorPaths(toWatchList,
  1035. [this](const std::string& p, int e, int s) {
  1036. this->HandleCMakeFileChanges(p, e, s);
  1037. });
  1038. m_State = STATE_CONFIGURED;
  1039. m_isDirty = false;
  1040. return request.Reply(Json::Value());
  1041. }
  1042. cmServerResponse cmServerProtocol1::ProcessGlobalSettings(
  1043. const cmServerRequest& request)
  1044. {
  1045. cmake* cm = this->CMakeInstance();
  1046. Json::Value obj = Json::objectValue;
  1047. // Capabilities information:
  1048. obj[kCAPABILITIES_KEY] = cm->ReportCapabilitiesJson(true);
  1049. obj[kDEBUG_OUTPUT_KEY] = cm->GetDebugOutput();
  1050. obj[kTRACE_KEY] = cm->GetTrace();
  1051. obj[kTRACE_EXPAND_KEY] = cm->GetTraceExpand();
  1052. obj[kWARN_UNINITIALIZED_KEY] = cm->GetWarnUninitialized();
  1053. obj[kWARN_UNUSED_KEY] = cm->GetWarnUnused();
  1054. obj[kWARN_UNUSED_CLI_KEY] = cm->GetWarnUnusedCli();
  1055. obj[kCHECK_SYSTEM_VARS_KEY] = cm->GetCheckSystemVars();
  1056. obj[kSOURCE_DIRECTORY_KEY] = this->GeneratorInfo.SourceDirectory;
  1057. obj[kBUILD_DIRECTORY_KEY] = this->GeneratorInfo.BuildDirectory;
  1058. // Currently used generator:
  1059. obj[kGENERATOR_KEY] = this->GeneratorInfo.GeneratorName;
  1060. obj[kEXTRA_GENERATOR_KEY] = this->GeneratorInfo.ExtraGeneratorName;
  1061. return request.Reply(obj);
  1062. }
  1063. static void setBool(const cmServerRequest& request, const std::string& key,
  1064. std::function<void(bool)> const& setter)
  1065. {
  1066. if (request.Data[key].isNull()) {
  1067. return;
  1068. }
  1069. setter(request.Data[key].asBool());
  1070. }
  1071. cmServerResponse cmServerProtocol1::ProcessSetGlobalSettings(
  1072. const cmServerRequest& request)
  1073. {
  1074. const std::vector<std::string> boolValues = {
  1075. kDEBUG_OUTPUT_KEY, kTRACE_KEY, kTRACE_EXPAND_KEY,
  1076. kWARN_UNINITIALIZED_KEY, kWARN_UNUSED_KEY, kWARN_UNUSED_CLI_KEY,
  1077. kCHECK_SYSTEM_VARS_KEY
  1078. };
  1079. for (std::string const& i : boolValues) {
  1080. if (!request.Data[i].isNull() && !request.Data[i].isBool()) {
  1081. return request.ReportError("\"" + i +
  1082. "\" must be unset or a bool value.");
  1083. }
  1084. }
  1085. cmake* cm = this->CMakeInstance();
  1086. setBool(request, kDEBUG_OUTPUT_KEY,
  1087. [cm](bool e) { cm->SetDebugOutputOn(e); });
  1088. setBool(request, kTRACE_KEY, [cm](bool e) { cm->SetTrace(e); });
  1089. setBool(request, kTRACE_EXPAND_KEY, [cm](bool e) { cm->SetTraceExpand(e); });
  1090. setBool(request, kWARN_UNINITIALIZED_KEY,
  1091. [cm](bool e) { cm->SetWarnUninitialized(e); });
  1092. setBool(request, kWARN_UNUSED_KEY, [cm](bool e) { cm->SetWarnUnused(e); });
  1093. setBool(request, kWARN_UNUSED_CLI_KEY,
  1094. [cm](bool e) { cm->SetWarnUnusedCli(e); });
  1095. setBool(request, kCHECK_SYSTEM_VARS_KEY,
  1096. [cm](bool e) { cm->SetCheckSystemVars(e); });
  1097. return request.Reply(Json::Value());
  1098. }
  1099. cmServerResponse cmServerProtocol1::ProcessFileSystemWatchers(
  1100. const cmServerRequest& request)
  1101. {
  1102. const cmFileMonitor* const fm = FileMonitor();
  1103. Json::Value result = Json::objectValue;
  1104. Json::Value files = Json::arrayValue;
  1105. for (auto const& f : fm->WatchedFiles()) {
  1106. files.append(f);
  1107. }
  1108. Json::Value directories = Json::arrayValue;
  1109. for (auto const& d : fm->WatchedDirectories()) {
  1110. directories.append(d);
  1111. }
  1112. result[kWATCHED_FILES_KEY] = files;
  1113. result[kWATCHED_DIRECTORIES_KEY] = directories;
  1114. return request.Reply(result);
  1115. }
  1116. cmServerResponse cmServerProtocol1::ProcessCTests(
  1117. const cmServerRequest& request)
  1118. {
  1119. if (this->m_State < STATE_COMPUTED) {
  1120. return request.ReportError("This instance was not yet computed.");
  1121. }
  1122. Json::Value result = Json::objectValue;
  1123. result[kCONFIGURATIONS_KEY] =
  1124. DumpCTestConfigurationsList(this->CMakeInstance());
  1125. return request.Reply(result);
  1126. }
  1127. cmServerProtocol1::GeneratorInformation::GeneratorInformation(
  1128. const std::string& generatorName, const std::string& extraGeneratorName,
  1129. const std::string& toolset, const std::string& platform,
  1130. const std::string& sourceDirectory, const std::string& buildDirectory)
  1131. : GeneratorName(generatorName)
  1132. , ExtraGeneratorName(extraGeneratorName)
  1133. , Toolset(toolset)
  1134. , Platform(platform)
  1135. , SourceDirectory(sourceDirectory)
  1136. , BuildDirectory(buildDirectory)
  1137. {
  1138. }
  1139. void cmServerProtocol1::GeneratorInformation::SetupGenerator(
  1140. cmake* cm, std::string* errorMessage)
  1141. {
  1142. const std::string fullGeneratorName =
  1143. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  1144. GeneratorName, ExtraGeneratorName);
  1145. cm->SetHomeDirectory(SourceDirectory);
  1146. cm->SetHomeOutputDirectory(BuildDirectory);
  1147. cmGlobalGenerator* gg = cm->CreateGlobalGenerator(fullGeneratorName);
  1148. if (!gg) {
  1149. setErrorMessage(
  1150. errorMessage,
  1151. std::string("Could not set up the requested combination of \"") +
  1152. kGENERATOR_KEY + "\" and \"" + kEXTRA_GENERATOR_KEY + "\"");
  1153. return;
  1154. }
  1155. cm->SetGlobalGenerator(gg);
  1156. cm->SetGeneratorToolset(Toolset);
  1157. cm->SetGeneratorPlatform(Platform);
  1158. }