cmServerProtocol.cxx 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  623. if (const char* coptions = file->GetProperty(COMPILE_OPTIONS)) {
  624. lg->AppendCompileOptions(
  625. compileFlags, genexInterpreter.Evaluate(coptions, COMPILE_OPTIONS));
  626. }
  627. fileData.Flags = compileFlags;
  628. fileData.IncludePathList = ld.IncludePathList;
  629. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  630. std::set<std::string> defines;
  631. if (const char* defs = file->GetProperty(COMPILE_DEFINITIONS)) {
  632. lg->AppendDefines(
  633. defines, genexInterpreter.Evaluate(defs, COMPILE_DEFINITIONS));
  634. }
  635. const std::string defPropName =
  636. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  637. if (const char* config_defs = file->GetProperty(defPropName)) {
  638. lg->AppendDefines(defines, genexInterpreter.Evaluate(
  639. config_defs, COMPILE_DEFINITIONS));
  640. }
  641. defines.insert(ld.Defines.begin(), ld.Defines.end());
  642. fileData.SetDefines(defines);
  643. }
  644. fileData.IsGenerated = file->GetPropertyAsBool("GENERATED");
  645. std::vector<std::string>& groupFileList = fileGroups[fileData];
  646. groupFileList.push_back(file->GetFullPath());
  647. }
  648. const std::string baseDir = target->Makefile->GetCurrentSourceDirectory();
  649. Json::Value result = Json::arrayValue;
  650. for (auto const& it : fileGroups) {
  651. Json::Value group = DumpSourceFileGroup(it.first, it.second, baseDir);
  652. if (!group.isNull()) {
  653. result.append(group);
  654. }
  655. }
  656. return result;
  657. }
  658. static Json::Value DumpCTestInfo(cmLocalGenerator* lg, cmTest* testInfo,
  659. const std::string& config)
  660. {
  661. Json::Value result = Json::objectValue;
  662. result[kCTEST_NAME] = testInfo->GetName();
  663. // Concat command entries together. After the first should be the arguments
  664. // for the command
  665. std::string command;
  666. for (auto const& cmd : testInfo->GetCommand()) {
  667. command.append(cmd);
  668. command.append(" ");
  669. }
  670. // Remove any config specific variables from the output.
  671. cmGeneratorExpression ge;
  672. auto cge = ge.Parse(command.c_str());
  673. const char* processed = cge->Evaluate(lg, config);
  674. result[kCTEST_COMMAND] = processed;
  675. // Build up the list of properties that may have been specified
  676. Json::Value properties = Json::arrayValue;
  677. for (auto& prop : testInfo->GetProperties()) {
  678. Json::Value entry = Json::objectValue;
  679. entry[kKEY_KEY] = prop.first;
  680. // Remove config variables from the value too.
  681. auto cge_value = ge.Parse(prop.second.GetValue());
  682. const char* processed_value = cge_value->Evaluate(lg, config);
  683. entry[kVALUE_KEY] = processed_value;
  684. properties.append(entry);
  685. }
  686. result[kPROPERTIES_KEY] = properties;
  687. return result;
  688. }
  689. static void DumpMakefileTests(cmLocalGenerator* lg, const std::string& config,
  690. Json::Value* result)
  691. {
  692. auto mf = lg->GetMakefile();
  693. std::vector<cmTest*> tests;
  694. mf->GetTests(config, tests);
  695. for (auto test : tests) {
  696. Json::Value tmp = DumpCTestInfo(lg, test, config);
  697. if (!tmp.isNull()) {
  698. result->append(tmp);
  699. }
  700. }
  701. }
  702. static Json::Value DumpCTestProjectList(const cmake* cm,
  703. std::string const& config)
  704. {
  705. Json::Value result = Json::arrayValue;
  706. auto globalGen = cm->GetGlobalGenerator();
  707. for (const auto& projectIt : globalGen->GetProjectMap()) {
  708. Json::Value pObj = Json::objectValue;
  709. pObj[kNAME_KEY] = projectIt.first;
  710. Json::Value tests = Json::arrayValue;
  711. // Gather tests for every generator
  712. for (const auto& lg : projectIt.second) {
  713. // Make sure they're generated.
  714. lg->GenerateTestFiles();
  715. DumpMakefileTests(lg, config, &tests);
  716. }
  717. pObj[kCTEST_INFO] = tests;
  718. result.append(pObj);
  719. }
  720. return result;
  721. }
  722. static Json::Value DumpCTestConfiguration(const cmake* cm,
  723. const std::string& config)
  724. {
  725. Json::Value result = Json::objectValue;
  726. result[kNAME_KEY] = config;
  727. result[kPROJECTS_KEY] = DumpCTestProjectList(cm, config);
  728. return result;
  729. }
  730. static Json::Value DumpCTestConfigurationsList(const cmake* cm)
  731. {
  732. Json::Value result = Json::arrayValue;
  733. for (const std::string& c : getConfigurations(cm)) {
  734. result.append(DumpCTestConfiguration(cm, c));
  735. }
  736. return result;
  737. }
  738. static Json::Value DumpTarget(cmGeneratorTarget* target,
  739. const std::string& config)
  740. {
  741. cmLocalGenerator* lg = target->GetLocalGenerator();
  742. const cmState* state = lg->GetState();
  743. const cmStateEnums::TargetType type = target->GetType();
  744. const std::string typeName = state->GetTargetTypeName(type);
  745. Json::Value ttl = Json::arrayValue;
  746. ttl.append("EXECUTABLE");
  747. ttl.append("STATIC_LIBRARY");
  748. ttl.append("SHARED_LIBRARY");
  749. ttl.append("MODULE_LIBRARY");
  750. ttl.append("OBJECT_LIBRARY");
  751. ttl.append("UTILITY");
  752. ttl.append("INTERFACE_LIBRARY");
  753. if (!hasString(ttl, typeName) || target->IsImported()) {
  754. return Json::Value();
  755. }
  756. Json::Value result = Json::objectValue;
  757. result[kNAME_KEY] = target->GetName();
  758. result[kIS_GENERATOR_PROVIDED_KEY] =
  759. target->Target->GetIsGeneratorProvided();
  760. result[kTYPE_KEY] = typeName;
  761. result[kSOURCE_DIRECTORY_KEY] = lg->GetCurrentSourceDirectory();
  762. result[kBUILD_DIRECTORY_KEY] = lg->GetCurrentBinaryDirectory();
  763. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  764. return result;
  765. }
  766. result[kFULL_NAME_KEY] = target->GetFullName(config);
  767. if (target->Target->GetHaveInstallRule()) {
  768. result[kHAS_INSTALL_RULE] = true;
  769. Json::Value installPaths = Json::arrayValue;
  770. auto targetGenerators = target->Makefile->GetInstallGenerators();
  771. for (auto installGenerator : targetGenerators) {
  772. auto installTargetGenerator =
  773. dynamic_cast<cmInstallTargetGenerator*>(installGenerator);
  774. if (installTargetGenerator != nullptr &&
  775. installTargetGenerator->GetTarget()->Target == target->Target) {
  776. auto dest = installTargetGenerator->GetDestination(config);
  777. std::string installPath;
  778. if (!dest.empty() && cmSystemTools::FileIsFullPath(dest.c_str())) {
  779. installPath = dest;
  780. } else {
  781. std::string installPrefix =
  782. target->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  783. installPath = installPrefix + '/' + dest;
  784. }
  785. installPaths.append(installPath);
  786. }
  787. }
  788. result[kINSTALL_PATHS] = installPaths;
  789. }
  790. if (target->HaveWellDefinedOutputFiles()) {
  791. Json::Value artifacts = Json::arrayValue;
  792. artifacts.append(
  793. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact));
  794. if (target->IsDLLPlatform()) {
  795. artifacts.append(
  796. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact));
  797. const cmGeneratorTarget::OutputInfo* output =
  798. target->GetOutputInfo(config);
  799. if (output && !output->PdbDir.empty()) {
  800. artifacts.append(output->PdbDir + '/' + target->GetPDBName(config));
  801. }
  802. }
  803. result[kARTIFACTS_KEY] = artifacts;
  804. result[kLINKER_LANGUAGE_KEY] = target->GetLinkerLanguage(config);
  805. std::string linkLibs;
  806. std::string linkFlags;
  807. std::string linkLanguageFlags;
  808. std::string frameworkPath;
  809. std::string linkPath;
  810. cmLinkLineComputer linkLineComputer(lg,
  811. lg->GetStateSnapshot().GetDirectory());
  812. lg->GetTargetFlags(&linkLineComputer, config, linkLibs, linkLanguageFlags,
  813. linkFlags, frameworkPath, linkPath, target);
  814. linkLibs = cmSystemTools::TrimWhitespace(linkLibs);
  815. linkFlags = cmSystemTools::TrimWhitespace(linkFlags);
  816. linkLanguageFlags = cmSystemTools::TrimWhitespace(linkLanguageFlags);
  817. frameworkPath = cmSystemTools::TrimWhitespace(frameworkPath);
  818. linkPath = cmSystemTools::TrimWhitespace(linkPath);
  819. if (!cmSystemTools::TrimWhitespace(linkLibs).empty()) {
  820. result[kLINK_LIBRARIES_KEY] = linkLibs;
  821. }
  822. if (!cmSystemTools::TrimWhitespace(linkFlags).empty()) {
  823. result[kLINK_FLAGS_KEY] = linkFlags;
  824. }
  825. if (!cmSystemTools::TrimWhitespace(linkLanguageFlags).empty()) {
  826. result[kLINK_LANGUAGE_FLAGS_KEY] = linkLanguageFlags;
  827. }
  828. if (!frameworkPath.empty()) {
  829. result[kFRAMEWORK_PATH_KEY] = frameworkPath;
  830. }
  831. if (!linkPath.empty()) {
  832. result[kLINK_PATH_KEY] = linkPath;
  833. }
  834. const std::string sysroot =
  835. lg->GetMakefile()->GetSafeDefinition("CMAKE_SYSROOT");
  836. if (!sysroot.empty()) {
  837. result[kSYSROOT_KEY] = sysroot;
  838. }
  839. }
  840. std::set<std::string> languages;
  841. target->GetLanguages(languages, config);
  842. std::map<std::string, LanguageData> languageDataMap;
  843. for (std::string const& lang : languages) {
  844. LanguageData& ld = languageDataMap[lang];
  845. ld.Language = lang;
  846. lg->GetTargetCompileFlags(target, config, lang, ld.Flags);
  847. std::set<std::string> defines;
  848. lg->GetTargetDefines(target, config, lang, defines);
  849. ld.SetDefines(defines);
  850. std::vector<std::string> includePathList;
  851. lg->GetIncludeDirectories(includePathList, target, lang, config, true);
  852. for (std::string const& i : includePathList) {
  853. ld.IncludePathList.push_back(
  854. std::make_pair(i, target->IsSystemIncludeDirectory(i, config)));
  855. }
  856. }
  857. Json::Value sourceGroupsValue =
  858. DumpSourceFilesList(target, config, languageDataMap);
  859. if (!sourceGroupsValue.empty()) {
  860. result[kFILE_GROUPS_KEY] = sourceGroupsValue;
  861. }
  862. return result;
  863. }
  864. static Json::Value DumpTargetsList(
  865. const std::vector<cmLocalGenerator*>& generators, const std::string& config)
  866. {
  867. Json::Value result = Json::arrayValue;
  868. std::vector<cmGeneratorTarget*> targetList;
  869. for (auto const& lgIt : generators) {
  870. const auto& list = lgIt->GetGeneratorTargets();
  871. targetList.insert(targetList.end(), list.begin(), list.end());
  872. }
  873. std::sort(targetList.begin(), targetList.end());
  874. for (cmGeneratorTarget* target : targetList) {
  875. Json::Value tmp = DumpTarget(target, config);
  876. if (!tmp.isNull()) {
  877. result.append(tmp);
  878. }
  879. }
  880. return result;
  881. }
  882. static Json::Value DumpProjectList(const cmake* cm, std::string const& config)
  883. {
  884. Json::Value result = Json::arrayValue;
  885. auto globalGen = cm->GetGlobalGenerator();
  886. for (auto const& projectIt : globalGen->GetProjectMap()) {
  887. Json::Value pObj = Json::objectValue;
  888. pObj[kNAME_KEY] = projectIt.first;
  889. // All Projects must have at least one local generator
  890. assert(!projectIt.second.empty());
  891. const cmLocalGenerator* lg = projectIt.second.at(0);
  892. // Project structure information:
  893. const cmMakefile* mf = lg->GetMakefile();
  894. auto minVersion = mf->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  895. pObj[kMINIMUM_CMAKE_VERSION] = minVersion ? minVersion : "";
  896. pObj[kSOURCE_DIRECTORY_KEY] = mf->GetCurrentSourceDirectory();
  897. pObj[kBUILD_DIRECTORY_KEY] = mf->GetCurrentBinaryDirectory();
  898. pObj[kTARGETS_KEY] = DumpTargetsList(projectIt.second, config);
  899. // For a project-level install rule it might be defined in any of its
  900. // associated generators.
  901. bool hasInstallRule = false;
  902. for (const auto generator : projectIt.second) {
  903. hasInstallRule =
  904. generator->GetMakefile()->GetInstallGenerators().empty() == false;
  905. if (hasInstallRule) {
  906. break;
  907. }
  908. }
  909. pObj[kHAS_INSTALL_RULE] = hasInstallRule;
  910. result.append(pObj);
  911. }
  912. return result;
  913. }
  914. static Json::Value DumpConfiguration(const cmake* cm,
  915. const std::string& config)
  916. {
  917. Json::Value result = Json::objectValue;
  918. result[kNAME_KEY] = config;
  919. result[kPROJECTS_KEY] = DumpProjectList(cm, config);
  920. return result;
  921. }
  922. static Json::Value DumpConfigurationsList(const cmake* cm)
  923. {
  924. Json::Value result = Json::arrayValue;
  925. for (std::string const& c : getConfigurations(cm)) {
  926. result.append(DumpConfiguration(cm, c));
  927. }
  928. return result;
  929. }
  930. cmServerResponse cmServerProtocol1::ProcessCodeModel(
  931. const cmServerRequest& request)
  932. {
  933. if (this->m_State != STATE_COMPUTED) {
  934. return request.ReportError("No build system was generated yet.");
  935. }
  936. Json::Value result = Json::objectValue;
  937. result[kCONFIGURATIONS_KEY] = DumpConfigurationsList(this->CMakeInstance());
  938. return request.Reply(result);
  939. }
  940. cmServerResponse cmServerProtocol1::ProcessCompute(
  941. const cmServerRequest& request)
  942. {
  943. if (this->m_State > STATE_CONFIGURED) {
  944. return request.ReportError("This build system was already generated.");
  945. }
  946. if (this->m_State < STATE_CONFIGURED) {
  947. return request.ReportError("This project was not configured yet.");
  948. }
  949. cmake* cm = this->CMakeInstance();
  950. int ret = cm->Generate();
  951. if (ret < 0) {
  952. return request.ReportError("Failed to compute build system.");
  953. }
  954. m_State = STATE_COMPUTED;
  955. return request.Reply(Json::Value());
  956. }
  957. cmServerResponse cmServerProtocol1::ProcessConfigure(
  958. const cmServerRequest& request)
  959. {
  960. if (this->m_State == STATE_INACTIVE) {
  961. return request.ReportError("This instance is inactive.");
  962. }
  963. FileMonitor()->StopMonitoring();
  964. std::string errorMessage;
  965. cmake* cm = this->CMakeInstance();
  966. this->GeneratorInfo.SetupGenerator(cm, &errorMessage);
  967. if (!errorMessage.empty()) {
  968. return request.ReportError(errorMessage);
  969. }
  970. // Make sure the types of cacheArguments matches (if given):
  971. std::vector<std::string> cacheArgs = { "unused" };
  972. bool cacheArgumentsError = false;
  973. const Json::Value passedArgs = request.Data[kCACHE_ARGUMENTS_KEY];
  974. if (!passedArgs.isNull()) {
  975. if (passedArgs.isString()) {
  976. cacheArgs.push_back(passedArgs.asString());
  977. } else if (passedArgs.isArray()) {
  978. for (auto const& arg : passedArgs) {
  979. if (!arg.isString()) {
  980. cacheArgumentsError = true;
  981. break;
  982. }
  983. cacheArgs.push_back(arg.asString());
  984. }
  985. } else {
  986. cacheArgumentsError = true;
  987. }
  988. }
  989. if (cacheArgumentsError) {
  990. request.ReportError(
  991. "cacheArguments must be unset, a string or an array of strings.");
  992. }
  993. std::string sourceDir = cm->GetHomeDirectory();
  994. const std::string buildDir = cm->GetHomeOutputDirectory();
  995. cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  996. if (buildDir.empty()) {
  997. return request.ReportError("No build directory set via Handshake.");
  998. }
  999. if (cm->LoadCache(buildDir)) {
  1000. // build directory has been set up before
  1001. const char* cachedSourceDir =
  1002. cm->GetState()->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
  1003. if (!cachedSourceDir) {
  1004. return request.ReportError("No CMAKE_HOME_DIRECTORY found in cache.");
  1005. }
  1006. if (sourceDir.empty()) {
  1007. sourceDir = std::string(cachedSourceDir);
  1008. cm->SetHomeDirectory(sourceDir);
  1009. }
  1010. const char* cachedGenerator =
  1011. cm->GetState()->GetInitializedCacheValue("CMAKE_GENERATOR");
  1012. if (cachedGenerator) {
  1013. if (gg && gg->GetName() != cachedGenerator) {
  1014. return request.ReportError("Configured generator does not match with "
  1015. "CMAKE_GENERATOR found in cache.");
  1016. }
  1017. }
  1018. } else {
  1019. // build directory has not been set up before
  1020. if (sourceDir.empty()) {
  1021. return request.ReportError("No sourceDirectory set via "
  1022. "setGlobalSettings and no cache found in "
  1023. "buildDirectory.");
  1024. }
  1025. }
  1026. cmSystemTools::ResetErrorOccuredFlag(); // Reset error state
  1027. if (cm->AddCMakePaths() != 1) {
  1028. return request.ReportError("Failed to set CMake paths.");
  1029. }
  1030. if (!cm->SetCacheArgs(cacheArgs)) {
  1031. return request.ReportError("cacheArguments could not be set.");
  1032. }
  1033. int ret = cm->Configure();
  1034. if (ret < 0) {
  1035. return request.ReportError("Configuration failed.");
  1036. }
  1037. std::vector<std::string> toWatchList;
  1038. getCMakeInputs(gg, std::string(), buildDir, nullptr, &toWatchList, nullptr);
  1039. FileMonitor()->MonitorPaths(toWatchList,
  1040. [this](const std::string& p, int e, int s) {
  1041. this->HandleCMakeFileChanges(p, e, s);
  1042. });
  1043. m_State = STATE_CONFIGURED;
  1044. m_isDirty = false;
  1045. return request.Reply(Json::Value());
  1046. }
  1047. cmServerResponse cmServerProtocol1::ProcessGlobalSettings(
  1048. const cmServerRequest& request)
  1049. {
  1050. cmake* cm = this->CMakeInstance();
  1051. Json::Value obj = Json::objectValue;
  1052. // Capabilities information:
  1053. obj[kCAPABILITIES_KEY] = cm->ReportCapabilitiesJson(true);
  1054. obj[kDEBUG_OUTPUT_KEY] = cm->GetDebugOutput();
  1055. obj[kTRACE_KEY] = cm->GetTrace();
  1056. obj[kTRACE_EXPAND_KEY] = cm->GetTraceExpand();
  1057. obj[kWARN_UNINITIALIZED_KEY] = cm->GetWarnUninitialized();
  1058. obj[kWARN_UNUSED_KEY] = cm->GetWarnUnused();
  1059. obj[kWARN_UNUSED_CLI_KEY] = cm->GetWarnUnusedCli();
  1060. obj[kCHECK_SYSTEM_VARS_KEY] = cm->GetCheckSystemVars();
  1061. obj[kSOURCE_DIRECTORY_KEY] = this->GeneratorInfo.SourceDirectory;
  1062. obj[kBUILD_DIRECTORY_KEY] = this->GeneratorInfo.BuildDirectory;
  1063. // Currently used generator:
  1064. obj[kGENERATOR_KEY] = this->GeneratorInfo.GeneratorName;
  1065. obj[kEXTRA_GENERATOR_KEY] = this->GeneratorInfo.ExtraGeneratorName;
  1066. return request.Reply(obj);
  1067. }
  1068. static void setBool(const cmServerRequest& request, const std::string& key,
  1069. std::function<void(bool)> const& setter)
  1070. {
  1071. if (request.Data[key].isNull()) {
  1072. return;
  1073. }
  1074. setter(request.Data[key].asBool());
  1075. }
  1076. cmServerResponse cmServerProtocol1::ProcessSetGlobalSettings(
  1077. const cmServerRequest& request)
  1078. {
  1079. const std::vector<std::string> boolValues = {
  1080. kDEBUG_OUTPUT_KEY, kTRACE_KEY, kTRACE_EXPAND_KEY,
  1081. kWARN_UNINITIALIZED_KEY, kWARN_UNUSED_KEY, kWARN_UNUSED_CLI_KEY,
  1082. kCHECK_SYSTEM_VARS_KEY
  1083. };
  1084. for (std::string const& i : boolValues) {
  1085. if (!request.Data[i].isNull() && !request.Data[i].isBool()) {
  1086. return request.ReportError("\"" + i +
  1087. "\" must be unset or a bool value.");
  1088. }
  1089. }
  1090. cmake* cm = this->CMakeInstance();
  1091. setBool(request, kDEBUG_OUTPUT_KEY,
  1092. [cm](bool e) { cm->SetDebugOutputOn(e); });
  1093. setBool(request, kTRACE_KEY, [cm](bool e) { cm->SetTrace(e); });
  1094. setBool(request, kTRACE_EXPAND_KEY, [cm](bool e) { cm->SetTraceExpand(e); });
  1095. setBool(request, kWARN_UNINITIALIZED_KEY,
  1096. [cm](bool e) { cm->SetWarnUninitialized(e); });
  1097. setBool(request, kWARN_UNUSED_KEY, [cm](bool e) { cm->SetWarnUnused(e); });
  1098. setBool(request, kWARN_UNUSED_CLI_KEY,
  1099. [cm](bool e) { cm->SetWarnUnusedCli(e); });
  1100. setBool(request, kCHECK_SYSTEM_VARS_KEY,
  1101. [cm](bool e) { cm->SetCheckSystemVars(e); });
  1102. return request.Reply(Json::Value());
  1103. }
  1104. cmServerResponse cmServerProtocol1::ProcessFileSystemWatchers(
  1105. const cmServerRequest& request)
  1106. {
  1107. const cmFileMonitor* const fm = FileMonitor();
  1108. Json::Value result = Json::objectValue;
  1109. Json::Value files = Json::arrayValue;
  1110. for (auto const& f : fm->WatchedFiles()) {
  1111. files.append(f);
  1112. }
  1113. Json::Value directories = Json::arrayValue;
  1114. for (auto const& d : fm->WatchedDirectories()) {
  1115. directories.append(d);
  1116. }
  1117. result[kWATCHED_FILES_KEY] = files;
  1118. result[kWATCHED_DIRECTORIES_KEY] = directories;
  1119. return request.Reply(result);
  1120. }
  1121. cmServerResponse cmServerProtocol1::ProcessCTests(
  1122. const cmServerRequest& request)
  1123. {
  1124. if (this->m_State < STATE_COMPUTED) {
  1125. return request.ReportError("This instance was not yet computed.");
  1126. }
  1127. Json::Value result = Json::objectValue;
  1128. result[kCONFIGURATIONS_KEY] =
  1129. DumpCTestConfigurationsList(this->CMakeInstance());
  1130. return request.Reply(result);
  1131. }
  1132. cmServerProtocol1::GeneratorInformation::GeneratorInformation(
  1133. const std::string& generatorName, const std::string& extraGeneratorName,
  1134. const std::string& toolset, const std::string& platform,
  1135. const std::string& sourceDirectory, const std::string& buildDirectory)
  1136. : GeneratorName(generatorName)
  1137. , ExtraGeneratorName(extraGeneratorName)
  1138. , Toolset(toolset)
  1139. , Platform(platform)
  1140. , SourceDirectory(sourceDirectory)
  1141. , BuildDirectory(buildDirectory)
  1142. {
  1143. }
  1144. void cmServerProtocol1::GeneratorInformation::SetupGenerator(
  1145. cmake* cm, std::string* errorMessage)
  1146. {
  1147. const std::string fullGeneratorName =
  1148. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  1149. GeneratorName, ExtraGeneratorName);
  1150. cm->SetHomeDirectory(SourceDirectory);
  1151. cm->SetHomeOutputDirectory(BuildDirectory);
  1152. cmGlobalGenerator* gg = cm->CreateGlobalGenerator(fullGeneratorName);
  1153. if (!gg) {
  1154. setErrorMessage(
  1155. errorMessage,
  1156. std::string("Could not set up the requested combination of \"") +
  1157. kGENERATOR_KEY + "\" and \"" + kEXTRA_GENERATOR_KEY + "\"");
  1158. return;
  1159. }
  1160. cm->SetGlobalGenerator(gg);
  1161. cm->SetGeneratorToolset(Toolset);
  1162. cm->SetGeneratorPlatform(Platform);
  1163. }