cmServerProtocol.cxx 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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, lf);
  95. if (toAdd.size() > relative.size()) {
  96. toAdd = relative;
  97. }
  98. }
  99. if (isInternal) {
  100. if (internalFiles) {
  101. internalFiles->push_back(std::move(toAdd));
  102. }
  103. } else {
  104. if (isTemporary) {
  105. if (tmpFiles) {
  106. tmpFiles->push_back(std::move(toAdd));
  107. }
  108. } else {
  109. if (explicitFiles) {
  110. explicitFiles->push_back(std::move(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 getOrTestHomeDirectory(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 getOrTestValue(cmState* state, const std::string& key,
  257. std::string& value,
  258. const std::string& keyDescription,
  259. std::string* errorMessage)
  260. {
  261. const char* entry = state->GetCacheEntryValue(key);
  262. const std::string cachedValue =
  263. entry == nullptr ? std::string() : std::string(entry);
  264. if (value.empty()) {
  265. value = cachedValue;
  266. }
  267. if (!cachedValue.empty() && cachedValue != value) {
  268. setErrorMessage(errorMessage, std::string("\"") + key +
  269. "\" is set but incompatible with configured " +
  270. keyDescription + " value.");
  271. return false;
  272. }
  273. return true;
  274. }
  275. bool cmServerProtocol1::DoActivate(const cmServerRequest& request,
  276. std::string* errorMessage)
  277. {
  278. std::string sourceDirectory = request.Data[kSOURCE_DIRECTORY_KEY].asString();
  279. const std::string buildDirectory =
  280. request.Data[kBUILD_DIRECTORY_KEY].asString();
  281. std::string generator = request.Data[kGENERATOR_KEY].asString();
  282. std::string extraGenerator = request.Data[kEXTRA_GENERATOR_KEY].asString();
  283. std::string toolset = request.Data[kTOOLSET_KEY].asString();
  284. std::string platform = request.Data[kPLATFORM_KEY].asString();
  285. if (buildDirectory.empty()) {
  286. setErrorMessage(errorMessage, std::string("\"") + kBUILD_DIRECTORY_KEY +
  287. "\" is missing.");
  288. return false;
  289. }
  290. cmake* cm = CMakeInstance();
  291. if (cmSystemTools::PathExists(buildDirectory)) {
  292. if (!cmSystemTools::FileIsDirectory(buildDirectory)) {
  293. setErrorMessage(errorMessage, std::string("\"") + kBUILD_DIRECTORY_KEY +
  294. "\" exists but is not a directory.");
  295. return false;
  296. }
  297. const std::string cachePath = cm->FindCacheFile(buildDirectory);
  298. if (cm->LoadCache(cachePath)) {
  299. cmState* state = cm->GetState();
  300. // Check generator:
  301. if (!getOrTestValue(state, "CMAKE_GENERATOR", generator, "generator",
  302. errorMessage)) {
  303. return false;
  304. }
  305. // check extra generator:
  306. if (!getOrTestValue(state, "CMAKE_EXTRA_GENERATOR", extraGenerator,
  307. "extra generator", errorMessage)) {
  308. return false;
  309. }
  310. // check sourcedir:
  311. if (!getOrTestHomeDirectory(state, sourceDirectory, errorMessage)) {
  312. return false;
  313. }
  314. // check toolset:
  315. if (!getOrTestValue(state, "CMAKE_GENERATOR_TOOLSET", toolset, "toolset",
  316. errorMessage)) {
  317. return false;
  318. }
  319. // check platform:
  320. if (!getOrTestValue(state, "CMAKE_GENERATOR_PLATFORM", platform,
  321. "platform", errorMessage)) {
  322. return false;
  323. }
  324. }
  325. }
  326. if (sourceDirectory.empty()) {
  327. setErrorMessage(errorMessage, std::string("\"") + kSOURCE_DIRECTORY_KEY +
  328. "\" is unset but required.");
  329. return false;
  330. }
  331. if (!cmSystemTools::FileIsDirectory(sourceDirectory)) {
  332. setErrorMessage(errorMessage, std::string("\"") + kSOURCE_DIRECTORY_KEY +
  333. "\" is not a directory.");
  334. return false;
  335. }
  336. if (generator.empty()) {
  337. setErrorMessage(errorMessage, std::string("\"") + kGENERATOR_KEY +
  338. "\" is unset but required.");
  339. return false;
  340. }
  341. std::vector<cmake::GeneratorInfo> generators;
  342. cm->GetRegisteredGenerators(generators);
  343. auto baseIt = std::find_if(generators.begin(), generators.end(),
  344. [&generator](const cmake::GeneratorInfo& info) {
  345. return info.name == generator;
  346. });
  347. if (baseIt == generators.end()) {
  348. setErrorMessage(errorMessage, std::string("Generator \"") + generator +
  349. "\" not supported.");
  350. return false;
  351. }
  352. auto extraIt = std::find_if(
  353. generators.begin(), generators.end(),
  354. [&generator, &extraGenerator](const cmake::GeneratorInfo& info) {
  355. return info.baseName == generator && info.extraName == extraGenerator;
  356. });
  357. if (extraIt == generators.end()) {
  358. setErrorMessage(errorMessage,
  359. std::string("The combination of generator \"" + generator +
  360. "\" and extra generator \"" + extraGenerator +
  361. "\" is not supported."));
  362. return false;
  363. }
  364. if (!extraIt->supportsToolset && !toolset.empty()) {
  365. setErrorMessage(errorMessage,
  366. std::string("Toolset was provided but is not supported by "
  367. "the requested generator."));
  368. return false;
  369. }
  370. if (!extraIt->supportsPlatform && !platform.empty()) {
  371. setErrorMessage(errorMessage,
  372. std::string("Platform was provided but is not supported "
  373. "by the requested generator."));
  374. return false;
  375. }
  376. this->GeneratorInfo =
  377. GeneratorInformation(generator, extraGenerator, toolset, platform,
  378. sourceDirectory, buildDirectory);
  379. this->m_State = STATE_ACTIVE;
  380. return true;
  381. }
  382. void cmServerProtocol1::HandleCMakeFileChanges(const std::string& path,
  383. int event, int status)
  384. {
  385. assert(status == 0);
  386. static_cast<void>(status);
  387. if (!m_isDirty) {
  388. m_isDirty = true;
  389. SendSignal(kDIRTY_SIGNAL, Json::objectValue);
  390. }
  391. Json::Value obj = Json::objectValue;
  392. obj[kPATH_KEY] = path;
  393. Json::Value properties = Json::arrayValue;
  394. if (event & UV_RENAME) {
  395. properties.append(kRENAME_PROPERTY_VALUE);
  396. }
  397. if (event & UV_CHANGE) {
  398. properties.append(kCHANGE_PROPERTY_VALUE);
  399. }
  400. obj[kPROPERTIES_KEY] = properties;
  401. SendSignal(kFILE_CHANGE_SIGNAL, obj);
  402. }
  403. const cmServerResponse cmServerProtocol1::Process(
  404. const cmServerRequest& request)
  405. {
  406. assert(this->m_State >= STATE_ACTIVE);
  407. if (request.Type == kCACHE_TYPE) {
  408. return this->ProcessCache(request);
  409. }
  410. if (request.Type == kCMAKE_INPUTS_TYPE) {
  411. return this->ProcessCMakeInputs(request);
  412. }
  413. if (request.Type == kCODE_MODEL_TYPE) {
  414. return this->ProcessCodeModel(request);
  415. }
  416. if (request.Type == kCOMPUTE_TYPE) {
  417. return this->ProcessCompute(request);
  418. }
  419. if (request.Type == kCONFIGURE_TYPE) {
  420. return this->ProcessConfigure(request);
  421. }
  422. if (request.Type == kFILESYSTEM_WATCHERS_TYPE) {
  423. return this->ProcessFileSystemWatchers(request);
  424. }
  425. if (request.Type == kGLOBAL_SETTINGS_TYPE) {
  426. return this->ProcessGlobalSettings(request);
  427. }
  428. if (request.Type == kSET_GLOBAL_SETTINGS_TYPE) {
  429. return this->ProcessSetGlobalSettings(request);
  430. }
  431. if (request.Type == kCTEST_INFO_TYPE) {
  432. return this->ProcessCTests(request);
  433. }
  434. return request.ReportError("Unknown command!");
  435. }
  436. bool cmServerProtocol1::IsExperimental() const
  437. {
  438. return true;
  439. }
  440. cmServerResponse cmServerProtocol1::ProcessCache(
  441. const cmServerRequest& request)
  442. {
  443. cmState* state = this->CMakeInstance()->GetState();
  444. Json::Value result = Json::objectValue;
  445. std::vector<std::string> allKeys = state->GetCacheEntryKeys();
  446. Json::Value list = Json::arrayValue;
  447. std::vector<std::string> keys = toStringList(request.Data[kKEYS_KEY]);
  448. if (keys.empty()) {
  449. keys = allKeys;
  450. } else {
  451. for (auto const& i : keys) {
  452. if (std::find(allKeys.begin(), allKeys.end(), i) == allKeys.end()) {
  453. return request.ReportError("Key \"" + i + "\" not found in cache.");
  454. }
  455. }
  456. }
  457. std::sort(keys.begin(), keys.end());
  458. for (auto const& key : keys) {
  459. Json::Value entry = Json::objectValue;
  460. entry[kKEY_KEY] = key;
  461. entry[kTYPE_KEY] =
  462. cmState::CacheEntryTypeToString(state->GetCacheEntryType(key));
  463. entry[kVALUE_KEY] = state->GetCacheEntryValue(key);
  464. Json::Value props = Json::objectValue;
  465. bool haveProperties = false;
  466. for (auto const& prop : state->GetCacheEntryPropertyList(key)) {
  467. haveProperties = true;
  468. props[prop] = state->GetCacheEntryProperty(key, prop);
  469. }
  470. if (haveProperties) {
  471. entry[kPROPERTIES_KEY] = props;
  472. }
  473. list.append(entry);
  474. }
  475. result[kCACHE_KEY] = list;
  476. return request.Reply(result);
  477. }
  478. cmServerResponse cmServerProtocol1::ProcessCMakeInputs(
  479. const cmServerRequest& request)
  480. {
  481. if (this->m_State < STATE_CONFIGURED) {
  482. return request.ReportError("This instance was not yet configured.");
  483. }
  484. const cmake* cm = this->CMakeInstance();
  485. const cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  486. const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot();
  487. const std::string& buildDir = cm->GetHomeOutputDirectory();
  488. const std::string& sourceDir = cm->GetHomeDirectory();
  489. Json::Value result = Json::objectValue;
  490. result[kSOURCE_DIRECTORY_KEY] = sourceDir;
  491. result[kCMAKE_ROOT_DIRECTORY_KEY] = cmakeRootDir;
  492. std::vector<std::string> internalFiles;
  493. std::vector<std::string> explicitFiles;
  494. std::vector<std::string> tmpFiles;
  495. getCMakeInputs(gg, sourceDir, buildDir, &internalFiles, &explicitFiles,
  496. &tmpFiles);
  497. Json::Value array = Json::arrayValue;
  498. Json::Value tmp = Json::objectValue;
  499. tmp[kIS_CMAKE_KEY] = true;
  500. tmp[kIS_TEMPORARY_KEY] = false;
  501. tmp[kSOURCES_KEY] = fromStringList(internalFiles);
  502. array.append(tmp);
  503. tmp = Json::objectValue;
  504. tmp[kIS_CMAKE_KEY] = false;
  505. tmp[kIS_TEMPORARY_KEY] = false;
  506. tmp[kSOURCES_KEY] = fromStringList(explicitFiles);
  507. array.append(tmp);
  508. tmp = Json::objectValue;
  509. tmp[kIS_CMAKE_KEY] = false;
  510. tmp[kIS_TEMPORARY_KEY] = true;
  511. tmp[kSOURCES_KEY] = fromStringList(tmpFiles);
  512. array.append(tmp);
  513. result[kBUILD_FILES_KEY] = array;
  514. return request.Reply(result);
  515. }
  516. class LanguageData
  517. {
  518. public:
  519. bool operator==(const LanguageData& other) const;
  520. void SetDefines(const std::set<std::string>& defines);
  521. bool IsGenerated = false;
  522. std::string Language;
  523. std::string Flags;
  524. std::vector<std::string> Defines;
  525. std::vector<std::pair<std::string, bool>> IncludePathList;
  526. };
  527. bool LanguageData::operator==(const LanguageData& other) const
  528. {
  529. return Language == other.Language && Defines == other.Defines &&
  530. Flags == other.Flags && IncludePathList == other.IncludePathList &&
  531. IsGenerated == other.IsGenerated;
  532. }
  533. void LanguageData::SetDefines(const std::set<std::string>& defines)
  534. {
  535. std::vector<std::string> result;
  536. result.reserve(defines.size());
  537. for (std::string const& i : defines) {
  538. result.push_back(i);
  539. }
  540. std::sort(result.begin(), result.end());
  541. Defines = std::move(result);
  542. }
  543. namespace std {
  544. template <>
  545. struct hash<LanguageData>
  546. {
  547. std::size_t operator()(const LanguageData& in) const
  548. {
  549. using std::hash;
  550. size_t result =
  551. hash<std::string>()(in.Language) ^ hash<std::string>()(in.Flags);
  552. for (auto const& i : in.IncludePathList) {
  553. result = result ^ (hash<std::string>()(i.first) ^
  554. (i.second ? std::numeric_limits<size_t>::max() : 0));
  555. }
  556. for (auto const& i : in.Defines) {
  557. result = result ^ hash<std::string>()(i);
  558. }
  559. result =
  560. result ^ (in.IsGenerated ? std::numeric_limits<size_t>::max() : 0);
  561. return result;
  562. }
  563. };
  564. } // namespace std
  565. static Json::Value DumpSourceFileGroup(const LanguageData& data,
  566. const std::vector<std::string>& files,
  567. const std::string& baseDir)
  568. {
  569. Json::Value result = Json::objectValue;
  570. if (!data.Language.empty()) {
  571. result[kLANGUAGE_KEY] = data.Language;
  572. if (!data.Flags.empty()) {
  573. result[kCOMPILE_FLAGS_KEY] = data.Flags;
  574. }
  575. if (!data.IncludePathList.empty()) {
  576. Json::Value includes = Json::arrayValue;
  577. for (auto const& i : data.IncludePathList) {
  578. Json::Value tmp = Json::objectValue;
  579. tmp[kPATH_KEY] = i.first;
  580. if (i.second) {
  581. tmp[kIS_SYSTEM_KEY] = i.second;
  582. }
  583. includes.append(tmp);
  584. }
  585. result[kINCLUDE_PATH_KEY] = includes;
  586. }
  587. if (!data.Defines.empty()) {
  588. result[kDEFINES_KEY] = fromStringList(data.Defines);
  589. }
  590. }
  591. result[kIS_GENERATED_KEY] = data.IsGenerated;
  592. Json::Value sourcesValue = Json::arrayValue;
  593. for (auto const& i : files) {
  594. const std::string relPath = cmSystemTools::RelativePath(baseDir, i);
  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. // Add include directories from source file properties.
  629. std::vector<std::string> includes;
  630. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  631. if (const char* cincludes = file->GetProperty(INCLUDE_DIRECTORIES)) {
  632. const char* evaluatedIncludes =
  633. genexInterpreter.Evaluate(cincludes, INCLUDE_DIRECTORIES);
  634. lg->AppendIncludeDirectories(includes, evaluatedIncludes, *file);
  635. for (const auto& include : includes) {
  636. fileData.IncludePathList.push_back(
  637. std::make_pair(include, target->IsSystemIncludeDirectory(
  638. include, config, fileData.Language)));
  639. }
  640. }
  641. fileData.IncludePathList.insert(fileData.IncludePathList.end(),
  642. ld.IncludePathList.begin(),
  643. ld.IncludePathList.end());
  644. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  645. std::set<std::string> defines;
  646. if (const char* defs = file->GetProperty(COMPILE_DEFINITIONS)) {
  647. lg->AppendDefines(
  648. defines, genexInterpreter.Evaluate(defs, COMPILE_DEFINITIONS));
  649. }
  650. const std::string defPropName =
  651. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  652. if (const char* config_defs = file->GetProperty(defPropName)) {
  653. lg->AppendDefines(defines, genexInterpreter.Evaluate(
  654. config_defs, COMPILE_DEFINITIONS));
  655. }
  656. defines.insert(ld.Defines.begin(), ld.Defines.end());
  657. fileData.SetDefines(defines);
  658. }
  659. fileData.IsGenerated = file->GetPropertyAsBool("GENERATED");
  660. std::vector<std::string>& groupFileList = fileGroups[fileData];
  661. groupFileList.push_back(file->GetFullPath());
  662. }
  663. const std::string baseDir = target->Makefile->GetCurrentSourceDirectory();
  664. Json::Value result = Json::arrayValue;
  665. for (auto const& it : fileGroups) {
  666. Json::Value group = DumpSourceFileGroup(it.first, it.second, baseDir);
  667. if (!group.isNull()) {
  668. result.append(group);
  669. }
  670. }
  671. return result;
  672. }
  673. static Json::Value DumpCTestInfo(cmLocalGenerator* lg, cmTest* testInfo,
  674. const std::string& config)
  675. {
  676. Json::Value result = Json::objectValue;
  677. result[kCTEST_NAME] = testInfo->GetName();
  678. // Concat command entries together. After the first should be the arguments
  679. // for the command
  680. std::string command;
  681. for (auto const& cmd : testInfo->GetCommand()) {
  682. command.append(cmd);
  683. command.append(" ");
  684. }
  685. // Remove any config specific variables from the output.
  686. cmGeneratorExpression ge;
  687. auto cge = ge.Parse(command.c_str());
  688. const char* processed = cge->Evaluate(lg, config);
  689. result[kCTEST_COMMAND] = processed;
  690. // Build up the list of properties that may have been specified
  691. Json::Value properties = Json::arrayValue;
  692. for (auto& prop : testInfo->GetProperties()) {
  693. Json::Value entry = Json::objectValue;
  694. entry[kKEY_KEY] = prop.first;
  695. // Remove config variables from the value too.
  696. auto cge_value = ge.Parse(prop.second.GetValue());
  697. const char* processed_value = cge_value->Evaluate(lg, config);
  698. entry[kVALUE_KEY] = processed_value;
  699. properties.append(entry);
  700. }
  701. result[kPROPERTIES_KEY] = properties;
  702. return result;
  703. }
  704. static void DumpMakefileTests(cmLocalGenerator* lg, const std::string& config,
  705. Json::Value* result)
  706. {
  707. auto mf = lg->GetMakefile();
  708. std::vector<cmTest*> tests;
  709. mf->GetTests(config, tests);
  710. for (auto test : tests) {
  711. Json::Value tmp = DumpCTestInfo(lg, test, config);
  712. if (!tmp.isNull()) {
  713. result->append(tmp);
  714. }
  715. }
  716. }
  717. static Json::Value DumpCTestProjectList(const cmake* cm,
  718. std::string const& config)
  719. {
  720. Json::Value result = Json::arrayValue;
  721. auto globalGen = cm->GetGlobalGenerator();
  722. for (const auto& projectIt : globalGen->GetProjectMap()) {
  723. Json::Value pObj = Json::objectValue;
  724. pObj[kNAME_KEY] = projectIt.first;
  725. Json::Value tests = Json::arrayValue;
  726. // Gather tests for every generator
  727. for (const auto& lg : projectIt.second) {
  728. // Make sure they're generated.
  729. lg->GenerateTestFiles();
  730. DumpMakefileTests(lg, config, &tests);
  731. }
  732. pObj[kCTEST_INFO] = tests;
  733. result.append(pObj);
  734. }
  735. return result;
  736. }
  737. static Json::Value DumpCTestConfiguration(const cmake* cm,
  738. const std::string& config)
  739. {
  740. Json::Value result = Json::objectValue;
  741. result[kNAME_KEY] = config;
  742. result[kPROJECTS_KEY] = DumpCTestProjectList(cm, config);
  743. return result;
  744. }
  745. static Json::Value DumpCTestConfigurationsList(const cmake* cm)
  746. {
  747. Json::Value result = Json::arrayValue;
  748. for (const std::string& c : getConfigurations(cm)) {
  749. result.append(DumpCTestConfiguration(cm, c));
  750. }
  751. return result;
  752. }
  753. static Json::Value DumpTarget(cmGeneratorTarget* target,
  754. const std::string& config)
  755. {
  756. cmLocalGenerator* lg = target->GetLocalGenerator();
  757. const cmState* state = lg->GetState();
  758. const cmStateEnums::TargetType type = target->GetType();
  759. const std::string typeName = state->GetTargetTypeName(type);
  760. Json::Value ttl = Json::arrayValue;
  761. ttl.append("EXECUTABLE");
  762. ttl.append("STATIC_LIBRARY");
  763. ttl.append("SHARED_LIBRARY");
  764. ttl.append("MODULE_LIBRARY");
  765. ttl.append("OBJECT_LIBRARY");
  766. ttl.append("UTILITY");
  767. ttl.append("INTERFACE_LIBRARY");
  768. if (!hasString(ttl, typeName) || target->IsImported()) {
  769. return Json::Value();
  770. }
  771. Json::Value result = Json::objectValue;
  772. result[kNAME_KEY] = target->GetName();
  773. result[kIS_GENERATOR_PROVIDED_KEY] =
  774. target->Target->GetIsGeneratorProvided();
  775. result[kTYPE_KEY] = typeName;
  776. result[kSOURCE_DIRECTORY_KEY] = lg->GetCurrentSourceDirectory();
  777. result[kBUILD_DIRECTORY_KEY] = lg->GetCurrentBinaryDirectory();
  778. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  779. return result;
  780. }
  781. result[kFULL_NAME_KEY] = target->GetFullName(config);
  782. if (target->Target->GetHaveInstallRule()) {
  783. result[kHAS_INSTALL_RULE] = true;
  784. Json::Value installPaths = Json::arrayValue;
  785. auto targetGenerators = target->Makefile->GetInstallGenerators();
  786. for (auto installGenerator : targetGenerators) {
  787. auto installTargetGenerator =
  788. dynamic_cast<cmInstallTargetGenerator*>(installGenerator);
  789. if (installTargetGenerator != nullptr &&
  790. installTargetGenerator->GetTarget()->Target == target->Target) {
  791. auto dest = installTargetGenerator->GetDestination(config);
  792. std::string installPath;
  793. if (!dest.empty() && cmSystemTools::FileIsFullPath(dest)) {
  794. installPath = dest;
  795. } else {
  796. std::string installPrefix =
  797. target->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  798. installPath = installPrefix + '/' + dest;
  799. }
  800. installPaths.append(installPath);
  801. }
  802. }
  803. result[kINSTALL_PATHS] = installPaths;
  804. }
  805. if (target->HaveWellDefinedOutputFiles()) {
  806. Json::Value artifacts = Json::arrayValue;
  807. artifacts.append(
  808. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact));
  809. if (target->IsDLLPlatform()) {
  810. artifacts.append(
  811. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact));
  812. const cmGeneratorTarget::OutputInfo* output =
  813. target->GetOutputInfo(config);
  814. if (output && !output->PdbDir.empty()) {
  815. artifacts.append(output->PdbDir + '/' + target->GetPDBName(config));
  816. }
  817. }
  818. result[kARTIFACTS_KEY] = artifacts;
  819. result[kLINKER_LANGUAGE_KEY] = target->GetLinkerLanguage(config);
  820. std::string linkLibs;
  821. std::string linkFlags;
  822. std::string linkLanguageFlags;
  823. std::string frameworkPath;
  824. std::string linkPath;
  825. cmLinkLineComputer linkLineComputer(lg,
  826. lg->GetStateSnapshot().GetDirectory());
  827. lg->GetTargetFlags(&linkLineComputer, config, linkLibs, linkLanguageFlags,
  828. linkFlags, frameworkPath, linkPath, target);
  829. linkLibs = cmSystemTools::TrimWhitespace(linkLibs);
  830. linkFlags = cmSystemTools::TrimWhitespace(linkFlags);
  831. linkLanguageFlags = cmSystemTools::TrimWhitespace(linkLanguageFlags);
  832. frameworkPath = cmSystemTools::TrimWhitespace(frameworkPath);
  833. linkPath = cmSystemTools::TrimWhitespace(linkPath);
  834. if (!cmSystemTools::TrimWhitespace(linkLibs).empty()) {
  835. result[kLINK_LIBRARIES_KEY] = linkLibs;
  836. }
  837. if (!cmSystemTools::TrimWhitespace(linkFlags).empty()) {
  838. result[kLINK_FLAGS_KEY] = linkFlags;
  839. }
  840. if (!cmSystemTools::TrimWhitespace(linkLanguageFlags).empty()) {
  841. result[kLINK_LANGUAGE_FLAGS_KEY] = linkLanguageFlags;
  842. }
  843. if (!frameworkPath.empty()) {
  844. result[kFRAMEWORK_PATH_KEY] = frameworkPath;
  845. }
  846. if (!linkPath.empty()) {
  847. result[kLINK_PATH_KEY] = linkPath;
  848. }
  849. const std::string sysroot =
  850. lg->GetMakefile()->GetSafeDefinition("CMAKE_SYSROOT");
  851. if (!sysroot.empty()) {
  852. result[kSYSROOT_KEY] = sysroot;
  853. }
  854. }
  855. std::set<std::string> languages;
  856. target->GetLanguages(languages, config);
  857. std::map<std::string, LanguageData> languageDataMap;
  858. for (std::string const& lang : languages) {
  859. LanguageData& ld = languageDataMap[lang];
  860. ld.Language = lang;
  861. lg->GetTargetCompileFlags(target, config, lang, ld.Flags);
  862. std::set<std::string> defines;
  863. lg->GetTargetDefines(target, config, lang, defines);
  864. ld.SetDefines(defines);
  865. std::vector<std::string> includePathList;
  866. lg->GetIncludeDirectories(includePathList, target, lang, config, true);
  867. for (std::string const& i : includePathList) {
  868. ld.IncludePathList.push_back(
  869. std::make_pair(i, target->IsSystemIncludeDirectory(i, config, lang)));
  870. }
  871. }
  872. Json::Value sourceGroupsValue =
  873. DumpSourceFilesList(target, config, languageDataMap);
  874. if (!sourceGroupsValue.empty()) {
  875. result[kFILE_GROUPS_KEY] = sourceGroupsValue;
  876. }
  877. return result;
  878. }
  879. static Json::Value DumpTargetsList(
  880. const std::vector<cmLocalGenerator*>& generators, const std::string& config)
  881. {
  882. Json::Value result = Json::arrayValue;
  883. std::vector<cmGeneratorTarget*> targetList;
  884. for (auto const& lgIt : generators) {
  885. const auto& list = lgIt->GetGeneratorTargets();
  886. targetList.insert(targetList.end(), list.begin(), list.end());
  887. }
  888. std::sort(targetList.begin(), targetList.end());
  889. for (cmGeneratorTarget* target : targetList) {
  890. Json::Value tmp = DumpTarget(target, config);
  891. if (!tmp.isNull()) {
  892. result.append(tmp);
  893. }
  894. }
  895. return result;
  896. }
  897. static Json::Value DumpProjectList(const cmake* cm, std::string const& config)
  898. {
  899. Json::Value result = Json::arrayValue;
  900. auto globalGen = cm->GetGlobalGenerator();
  901. for (auto const& projectIt : globalGen->GetProjectMap()) {
  902. Json::Value pObj = Json::objectValue;
  903. pObj[kNAME_KEY] = projectIt.first;
  904. // All Projects must have at least one local generator
  905. assert(!projectIt.second.empty());
  906. const cmLocalGenerator* lg = projectIt.second.at(0);
  907. // Project structure information:
  908. const cmMakefile* mf = lg->GetMakefile();
  909. auto minVersion = mf->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  910. pObj[kMINIMUM_CMAKE_VERSION] = minVersion ? minVersion : "";
  911. pObj[kSOURCE_DIRECTORY_KEY] = mf->GetCurrentSourceDirectory();
  912. pObj[kBUILD_DIRECTORY_KEY] = mf->GetCurrentBinaryDirectory();
  913. pObj[kTARGETS_KEY] = DumpTargetsList(projectIt.second, config);
  914. // For a project-level install rule it might be defined in any of its
  915. // associated generators.
  916. bool hasInstallRule = false;
  917. for (const auto generator : projectIt.second) {
  918. hasInstallRule =
  919. generator->GetMakefile()->GetInstallGenerators().empty() == false;
  920. if (hasInstallRule) {
  921. break;
  922. }
  923. }
  924. pObj[kHAS_INSTALL_RULE] = hasInstallRule;
  925. result.append(pObj);
  926. }
  927. return result;
  928. }
  929. static Json::Value DumpConfiguration(const cmake* cm,
  930. const std::string& config)
  931. {
  932. Json::Value result = Json::objectValue;
  933. result[kNAME_KEY] = config;
  934. result[kPROJECTS_KEY] = DumpProjectList(cm, config);
  935. return result;
  936. }
  937. static Json::Value DumpConfigurationsList(const cmake* cm)
  938. {
  939. Json::Value result = Json::arrayValue;
  940. for (std::string const& c : getConfigurations(cm)) {
  941. result.append(DumpConfiguration(cm, c));
  942. }
  943. return result;
  944. }
  945. cmServerResponse cmServerProtocol1::ProcessCodeModel(
  946. const cmServerRequest& request)
  947. {
  948. if (this->m_State != STATE_COMPUTED) {
  949. return request.ReportError("No build system was generated yet.");
  950. }
  951. Json::Value result = Json::objectValue;
  952. result[kCONFIGURATIONS_KEY] = DumpConfigurationsList(this->CMakeInstance());
  953. return request.Reply(result);
  954. }
  955. cmServerResponse cmServerProtocol1::ProcessCompute(
  956. const cmServerRequest& request)
  957. {
  958. if (this->m_State > STATE_CONFIGURED) {
  959. return request.ReportError("This build system was already generated.");
  960. }
  961. if (this->m_State < STATE_CONFIGURED) {
  962. return request.ReportError("This project was not configured yet.");
  963. }
  964. cmake* cm = this->CMakeInstance();
  965. int ret = cm->Generate();
  966. if (ret < 0) {
  967. return request.ReportError("Failed to compute build system.");
  968. }
  969. m_State = STATE_COMPUTED;
  970. return request.Reply(Json::Value());
  971. }
  972. cmServerResponse cmServerProtocol1::ProcessConfigure(
  973. const cmServerRequest& request)
  974. {
  975. if (this->m_State == STATE_INACTIVE) {
  976. return request.ReportError("This instance is inactive.");
  977. }
  978. FileMonitor()->StopMonitoring();
  979. std::string errorMessage;
  980. cmake* cm = this->CMakeInstance();
  981. this->GeneratorInfo.SetupGenerator(cm, &errorMessage);
  982. if (!errorMessage.empty()) {
  983. return request.ReportError(errorMessage);
  984. }
  985. // Make sure the types of cacheArguments matches (if given):
  986. std::vector<std::string> cacheArgs = { "unused" };
  987. bool cacheArgumentsError = false;
  988. const Json::Value passedArgs = request.Data[kCACHE_ARGUMENTS_KEY];
  989. if (!passedArgs.isNull()) {
  990. if (passedArgs.isString()) {
  991. cacheArgs.push_back(passedArgs.asString());
  992. } else if (passedArgs.isArray()) {
  993. for (auto const& arg : passedArgs) {
  994. if (!arg.isString()) {
  995. cacheArgumentsError = true;
  996. break;
  997. }
  998. cacheArgs.push_back(arg.asString());
  999. }
  1000. } else {
  1001. cacheArgumentsError = true;
  1002. }
  1003. }
  1004. if (cacheArgumentsError) {
  1005. request.ReportError(
  1006. "cacheArguments must be unset, a string or an array of strings.");
  1007. }
  1008. std::string sourceDir = cm->GetHomeDirectory();
  1009. const std::string buildDir = cm->GetHomeOutputDirectory();
  1010. cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  1011. if (buildDir.empty()) {
  1012. return request.ReportError("No build directory set via Handshake.");
  1013. }
  1014. if (cm->LoadCache(buildDir)) {
  1015. // build directory has been set up before
  1016. const char* cachedSourceDir =
  1017. cm->GetState()->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
  1018. if (!cachedSourceDir) {
  1019. return request.ReportError("No CMAKE_HOME_DIRECTORY found in cache.");
  1020. }
  1021. if (sourceDir.empty()) {
  1022. sourceDir = std::string(cachedSourceDir);
  1023. cm->SetHomeDirectory(sourceDir);
  1024. }
  1025. const char* cachedGenerator =
  1026. cm->GetState()->GetInitializedCacheValue("CMAKE_GENERATOR");
  1027. if (cachedGenerator) {
  1028. if (gg && gg->GetName() != cachedGenerator) {
  1029. return request.ReportError("Configured generator does not match with "
  1030. "CMAKE_GENERATOR found in cache.");
  1031. }
  1032. }
  1033. } else {
  1034. // build directory has not been set up before
  1035. if (sourceDir.empty()) {
  1036. return request.ReportError("No sourceDirectory set via "
  1037. "setGlobalSettings and no cache found in "
  1038. "buildDirectory.");
  1039. }
  1040. }
  1041. cmSystemTools::ResetErrorOccuredFlag(); // Reset error state
  1042. if (cm->AddCMakePaths() != 1) {
  1043. return request.ReportError("Failed to set CMake paths.");
  1044. }
  1045. if (!cm->SetCacheArgs(cacheArgs)) {
  1046. return request.ReportError("cacheArguments could not be set.");
  1047. }
  1048. int ret = cm->Configure();
  1049. if (ret < 0) {
  1050. return request.ReportError("Configuration failed.");
  1051. }
  1052. std::vector<std::string> toWatchList;
  1053. getCMakeInputs(gg, std::string(), buildDir, nullptr, &toWatchList, nullptr);
  1054. FileMonitor()->MonitorPaths(toWatchList,
  1055. [this](const std::string& p, int e, int s) {
  1056. this->HandleCMakeFileChanges(p, e, s);
  1057. });
  1058. m_State = STATE_CONFIGURED;
  1059. m_isDirty = false;
  1060. return request.Reply(Json::Value());
  1061. }
  1062. cmServerResponse cmServerProtocol1::ProcessGlobalSettings(
  1063. const cmServerRequest& request)
  1064. {
  1065. cmake* cm = this->CMakeInstance();
  1066. Json::Value obj = Json::objectValue;
  1067. // Capabilities information:
  1068. obj[kCAPABILITIES_KEY] = cm->ReportCapabilitiesJson(true);
  1069. obj[kDEBUG_OUTPUT_KEY] = cm->GetDebugOutput();
  1070. obj[kTRACE_KEY] = cm->GetTrace();
  1071. obj[kTRACE_EXPAND_KEY] = cm->GetTraceExpand();
  1072. obj[kWARN_UNINITIALIZED_KEY] = cm->GetWarnUninitialized();
  1073. obj[kWARN_UNUSED_KEY] = cm->GetWarnUnused();
  1074. obj[kWARN_UNUSED_CLI_KEY] = cm->GetWarnUnusedCli();
  1075. obj[kCHECK_SYSTEM_VARS_KEY] = cm->GetCheckSystemVars();
  1076. obj[kSOURCE_DIRECTORY_KEY] = this->GeneratorInfo.SourceDirectory;
  1077. obj[kBUILD_DIRECTORY_KEY] = this->GeneratorInfo.BuildDirectory;
  1078. // Currently used generator:
  1079. obj[kGENERATOR_KEY] = this->GeneratorInfo.GeneratorName;
  1080. obj[kEXTRA_GENERATOR_KEY] = this->GeneratorInfo.ExtraGeneratorName;
  1081. return request.Reply(obj);
  1082. }
  1083. static void setBool(const cmServerRequest& request, const std::string& key,
  1084. std::function<void(bool)> const& setter)
  1085. {
  1086. if (request.Data[key].isNull()) {
  1087. return;
  1088. }
  1089. setter(request.Data[key].asBool());
  1090. }
  1091. cmServerResponse cmServerProtocol1::ProcessSetGlobalSettings(
  1092. const cmServerRequest& request)
  1093. {
  1094. const std::vector<std::string> boolValues = {
  1095. kDEBUG_OUTPUT_KEY, kTRACE_KEY, kTRACE_EXPAND_KEY,
  1096. kWARN_UNINITIALIZED_KEY, kWARN_UNUSED_KEY, kWARN_UNUSED_CLI_KEY,
  1097. kCHECK_SYSTEM_VARS_KEY
  1098. };
  1099. for (std::string const& i : boolValues) {
  1100. if (!request.Data[i].isNull() && !request.Data[i].isBool()) {
  1101. return request.ReportError("\"" + i +
  1102. "\" must be unset or a bool value.");
  1103. }
  1104. }
  1105. cmake* cm = this->CMakeInstance();
  1106. setBool(request, kDEBUG_OUTPUT_KEY,
  1107. [cm](bool e) { cm->SetDebugOutputOn(e); });
  1108. setBool(request, kTRACE_KEY, [cm](bool e) { cm->SetTrace(e); });
  1109. setBool(request, kTRACE_EXPAND_KEY, [cm](bool e) { cm->SetTraceExpand(e); });
  1110. setBool(request, kWARN_UNINITIALIZED_KEY,
  1111. [cm](bool e) { cm->SetWarnUninitialized(e); });
  1112. setBool(request, kWARN_UNUSED_KEY, [cm](bool e) { cm->SetWarnUnused(e); });
  1113. setBool(request, kWARN_UNUSED_CLI_KEY,
  1114. [cm](bool e) { cm->SetWarnUnusedCli(e); });
  1115. setBool(request, kCHECK_SYSTEM_VARS_KEY,
  1116. [cm](bool e) { cm->SetCheckSystemVars(e); });
  1117. return request.Reply(Json::Value());
  1118. }
  1119. cmServerResponse cmServerProtocol1::ProcessFileSystemWatchers(
  1120. const cmServerRequest& request)
  1121. {
  1122. const cmFileMonitor* const fm = FileMonitor();
  1123. Json::Value result = Json::objectValue;
  1124. Json::Value files = Json::arrayValue;
  1125. for (auto const& f : fm->WatchedFiles()) {
  1126. files.append(f);
  1127. }
  1128. Json::Value directories = Json::arrayValue;
  1129. for (auto const& d : fm->WatchedDirectories()) {
  1130. directories.append(d);
  1131. }
  1132. result[kWATCHED_FILES_KEY] = files;
  1133. result[kWATCHED_DIRECTORIES_KEY] = directories;
  1134. return request.Reply(result);
  1135. }
  1136. cmServerResponse cmServerProtocol1::ProcessCTests(
  1137. const cmServerRequest& request)
  1138. {
  1139. if (this->m_State < STATE_COMPUTED) {
  1140. return request.ReportError("This instance was not yet computed.");
  1141. }
  1142. Json::Value result = Json::objectValue;
  1143. result[kCONFIGURATIONS_KEY] =
  1144. DumpCTestConfigurationsList(this->CMakeInstance());
  1145. return request.Reply(result);
  1146. }
  1147. cmServerProtocol1::GeneratorInformation::GeneratorInformation(
  1148. const std::string& generatorName, const std::string& extraGeneratorName,
  1149. const std::string& toolset, const std::string& platform,
  1150. const std::string& sourceDirectory, const std::string& buildDirectory)
  1151. : GeneratorName(generatorName)
  1152. , ExtraGeneratorName(extraGeneratorName)
  1153. , Toolset(toolset)
  1154. , Platform(platform)
  1155. , SourceDirectory(sourceDirectory)
  1156. , BuildDirectory(buildDirectory)
  1157. {
  1158. }
  1159. void cmServerProtocol1::GeneratorInformation::SetupGenerator(
  1160. cmake* cm, std::string* errorMessage)
  1161. {
  1162. const std::string fullGeneratorName =
  1163. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  1164. GeneratorName, ExtraGeneratorName);
  1165. cm->SetHomeDirectory(SourceDirectory);
  1166. cm->SetHomeOutputDirectory(BuildDirectory);
  1167. cmGlobalGenerator* gg = cm->CreateGlobalGenerator(fullGeneratorName);
  1168. if (!gg) {
  1169. setErrorMessage(
  1170. errorMessage,
  1171. std::string("Could not set up the requested combination of \"") +
  1172. kGENERATOR_KEY + "\" and \"" + kEXTRA_GENERATOR_KEY + "\"");
  1173. return;
  1174. }
  1175. cm->SetGlobalGenerator(gg);
  1176. cm->SetGeneratorToolset(Toolset);
  1177. cm->SetGeneratorPlatform(Platform);
  1178. }