cmServerProtocol.cxx 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  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 "cmListFileCache.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmProperty.h"
  17. #include "cmServer.h"
  18. #include "cmServerDictionary.h"
  19. #include "cmSourceFile.h"
  20. #include "cmState.h"
  21. #include "cmStateDirectory.h"
  22. #include "cmStateSnapshot.h"
  23. #include "cmStateTypes.h"
  24. #include "cmSystemTools.h"
  25. #include "cmTarget.h"
  26. #include "cmTest.h"
  27. #include "cm_uv.h"
  28. #include "cmake.h"
  29. #include <algorithm>
  30. #include <cassert>
  31. #include <cstddef>
  32. #include <functional>
  33. #include <limits>
  34. #include <map>
  35. #include <memory>
  36. #include <set>
  37. #include <string>
  38. #include <unordered_map>
  39. #include <vector>
  40. // Get rid of some windows macros:
  41. #undef max
  42. namespace {
  43. std::vector<std::string> getConfigurations(const cmake* cm)
  44. {
  45. std::vector<std::string> configurations;
  46. auto makefiles = cm->GetGlobalGenerator()->GetMakefiles();
  47. if (makefiles.empty()) {
  48. return configurations;
  49. }
  50. makefiles[0]->GetConfigurations(configurations);
  51. if (configurations.empty()) {
  52. configurations.push_back("");
  53. }
  54. return configurations;
  55. }
  56. bool hasString(const Json::Value& v, const std::string& s)
  57. {
  58. return !v.isNull() &&
  59. std::any_of(v.begin(), v.end(),
  60. [s](const Json::Value& i) { return i.asString() == s; });
  61. }
  62. template <class T>
  63. Json::Value fromStringList(const T& in)
  64. {
  65. Json::Value result = Json::arrayValue;
  66. for (std::string const& i : in) {
  67. result.append(i);
  68. }
  69. return result;
  70. }
  71. std::vector<std::string> toStringList(const Json::Value& in)
  72. {
  73. std::vector<std::string> result;
  74. for (auto const& it : in) {
  75. result.push_back(it.asString());
  76. }
  77. return result;
  78. }
  79. void getCMakeInputs(const cmGlobalGenerator* gg, const std::string& sourceDir,
  80. const std::string& buildDir,
  81. std::vector<std::string>* internalFiles,
  82. std::vector<std::string>* explicitFiles,
  83. std::vector<std::string>* tmpFiles)
  84. {
  85. const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot() + '/';
  86. std::vector<cmMakefile*> const& makefiles = gg->GetMakefiles();
  87. for (cmMakefile const* mf : makefiles) {
  88. for (std::string const& lf : mf->GetListFiles()) {
  89. const std::string startOfFile = lf.substr(0, cmakeRootDir.size());
  90. const bool isInternal = (startOfFile == cmakeRootDir);
  91. const bool isTemporary = !isInternal && (lf.find(buildDir + '/') == 0);
  92. std::string toAdd = lf;
  93. if (!sourceDir.empty()) {
  94. const std::string& relative =
  95. cmSystemTools::RelativePath(sourceDir.c_str(), lf.c_str());
  96. if (toAdd.size() > relative.size()) {
  97. toAdd = relative;
  98. }
  99. }
  100. if (isInternal) {
  101. if (internalFiles) {
  102. internalFiles->push_back(toAdd);
  103. }
  104. } else {
  105. if (isTemporary) {
  106. if (tmpFiles) {
  107. tmpFiles->push_back(toAdd);
  108. }
  109. } else {
  110. if (explicitFiles) {
  111. explicitFiles->push_back(toAdd);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. } // namespace
  119. cmServerRequest::cmServerRequest(cmServer* server, cmConnection* connection,
  120. const std::string& t, const std::string& c,
  121. const Json::Value& d)
  122. : Type(t)
  123. , Cookie(c)
  124. , Data(d)
  125. , Connection(connection)
  126. , m_Server(server)
  127. {
  128. }
  129. void cmServerRequest::ReportProgress(int min, int current, int max,
  130. const std::string& message) const
  131. {
  132. this->m_Server->WriteProgress(*this, min, current, max, message);
  133. }
  134. void cmServerRequest::ReportMessage(const std::string& message,
  135. const std::string& title) const
  136. {
  137. m_Server->WriteMessage(*this, message, title);
  138. }
  139. cmServerResponse cmServerRequest::Reply(const Json::Value& data) const
  140. {
  141. cmServerResponse response(*this);
  142. response.SetData(data);
  143. return response;
  144. }
  145. cmServerResponse cmServerRequest::ReportError(const std::string& message) const
  146. {
  147. cmServerResponse response(*this);
  148. response.SetError(message);
  149. return response;
  150. }
  151. cmServerResponse::cmServerResponse(const cmServerRequest& request)
  152. : Type(request.Type)
  153. , Cookie(request.Cookie)
  154. {
  155. }
  156. void cmServerResponse::SetData(const Json::Value& data)
  157. {
  158. assert(this->m_Payload == PAYLOAD_UNKNOWN);
  159. if (!data[kCOOKIE_KEY].isNull() || !data[kTYPE_KEY].isNull()) {
  160. this->SetError("Response contains cookie or type field.");
  161. return;
  162. }
  163. this->m_Payload = PAYLOAD_DATA;
  164. this->m_Data = data;
  165. }
  166. void cmServerResponse::SetError(const std::string& message)
  167. {
  168. assert(this->m_Payload == PAYLOAD_UNKNOWN);
  169. this->m_Payload = PAYLOAD_ERROR;
  170. this->m_ErrorMessage = message;
  171. }
  172. bool cmServerResponse::IsComplete() const
  173. {
  174. return this->m_Payload != PAYLOAD_UNKNOWN;
  175. }
  176. bool cmServerResponse::IsError() const
  177. {
  178. assert(this->m_Payload != PAYLOAD_UNKNOWN);
  179. return this->m_Payload == PAYLOAD_ERROR;
  180. }
  181. std::string cmServerResponse::ErrorMessage() const
  182. {
  183. if (this->m_Payload == PAYLOAD_ERROR) {
  184. return this->m_ErrorMessage;
  185. }
  186. return std::string();
  187. }
  188. Json::Value cmServerResponse::Data() const
  189. {
  190. assert(this->m_Payload != PAYLOAD_UNKNOWN);
  191. return this->m_Data;
  192. }
  193. bool cmServerProtocol::Activate(cmServer* server,
  194. const cmServerRequest& request,
  195. std::string* errorMessage)
  196. {
  197. assert(server);
  198. this->m_Server = server;
  199. this->m_CMakeInstance = cm::make_unique<cmake>(cmake::RoleProject);
  200. const bool result = this->DoActivate(request, errorMessage);
  201. if (!result) {
  202. this->m_CMakeInstance = nullptr;
  203. }
  204. return result;
  205. }
  206. cmFileMonitor* cmServerProtocol::FileMonitor() const
  207. {
  208. return this->m_Server ? this->m_Server->FileMonitor() : nullptr;
  209. }
  210. void cmServerProtocol::SendSignal(const std::string& name,
  211. const Json::Value& data) const
  212. {
  213. if (this->m_Server) {
  214. this->m_Server->WriteSignal(name, data);
  215. }
  216. }
  217. cmake* cmServerProtocol::CMakeInstance() const
  218. {
  219. return this->m_CMakeInstance.get();
  220. }
  221. bool cmServerProtocol::DoActivate(const cmServerRequest& /*request*/,
  222. std::string* /*errorMessage*/)
  223. {
  224. return true;
  225. }
  226. std::pair<int, int> cmServerProtocol1::ProtocolVersion() const
  227. {
  228. return std::make_pair(1, 2);
  229. }
  230. static void setErrorMessage(std::string* errorMessage, const std::string& text)
  231. {
  232. if (errorMessage) {
  233. *errorMessage = text;
  234. }
  235. }
  236. static bool testHomeDirectory(cmState* state, std::string& value,
  237. std::string* errorMessage)
  238. {
  239. const std::string cachedValue =
  240. std::string(state->GetCacheEntryValue("CMAKE_HOME_DIRECTORY"));
  241. if (value.empty()) {
  242. value = cachedValue;
  243. return true;
  244. }
  245. const std::string suffix = "/CMakeLists.txt";
  246. const std::string cachedValueCML = cachedValue + suffix;
  247. const std::string valueCML = value + suffix;
  248. if (!cmSystemTools::SameFile(valueCML, cachedValueCML)) {
  249. setErrorMessage(errorMessage,
  250. std::string("\"CMAKE_HOME_DIRECTORY\" is set but "
  251. "incompatible with configured "
  252. "source directory value."));
  253. return false;
  254. }
  255. return true;
  256. }
  257. static bool testValue(cmState* state, const std::string& key,
  258. std::string& value, 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 (!testValue(state, "CMAKE_GENERATOR", generator, "generator",
  302. errorMessage)) {
  303. return false;
  304. }
  305. // check extra generator:
  306. if (!testValue(state, "CMAKE_EXTRA_GENERATOR", extraGenerator,
  307. "extra generator", errorMessage)) {
  308. return false;
  309. }
  310. // check sourcedir:
  311. if (!testHomeDirectory(state, sourceDirectory, errorMessage)) {
  312. return false;
  313. }
  314. // check toolset:
  315. if (!testValue(state, "CMAKE_GENERATOR_TOOLSET", toolset, "toolset",
  316. errorMessage)) {
  317. return false;
  318. }
  319. // check platform:
  320. if (!testValue(state, "CMAKE_GENERATOR_PLATFORM", platform, "platform",
  321. 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 =
  595. cmSystemTools::RelativePath(baseDir.c_str(), i.c_str());
  596. sourcesValue.append(relPath.size() < i.size() ? relPath : i);
  597. }
  598. result[kSOURCES_KEY] = sourcesValue;
  599. return result;
  600. }
  601. static Json::Value DumpSourceFilesList(
  602. cmGeneratorTarget* target, const std::string& config,
  603. const std::map<std::string, LanguageData>& languageDataMap)
  604. {
  605. // Collect sourcefile groups:
  606. std::vector<cmSourceFile*> files;
  607. target->GetSourceFiles(files, config);
  608. cmGeneratorExpressionInterpreter genexInterpreter(
  609. target->GetLocalGenerator(), target, config);
  610. std::unordered_map<LanguageData, std::vector<std::string>> fileGroups;
  611. for (cmSourceFile* file : files) {
  612. LanguageData fileData;
  613. fileData.Language = file->GetLanguage();
  614. if (!fileData.Language.empty()) {
  615. const LanguageData& ld = languageDataMap.at(fileData.Language);
  616. cmLocalGenerator* lg = target->GetLocalGenerator();
  617. std::string compileFlags = ld.Flags;
  618. if (const char* cflags = file->GetProperty("COMPILE_FLAGS")) {
  619. lg->AppendFlags(compileFlags, genexInterpreter.Evaluate(cflags));
  620. }
  621. fileData.Flags = compileFlags;
  622. fileData.IncludePathList = ld.IncludePathList;
  623. std::set<std::string> defines;
  624. lg->AppendDefines(defines, file->GetProperty("COMPILE_DEFINITIONS"));
  625. const std::string defPropName =
  626. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  627. lg->AppendDefines(defines, file->GetProperty(defPropName));
  628. defines.insert(ld.Defines.begin(), ld.Defines.end());
  629. fileData.SetDefines(defines);
  630. }
  631. fileData.IsGenerated = file->GetPropertyAsBool("GENERATED");
  632. std::vector<std::string>& groupFileList = fileGroups[fileData];
  633. groupFileList.push_back(file->GetFullPath());
  634. }
  635. const std::string baseDir = target->Makefile->GetCurrentSourceDirectory();
  636. Json::Value result = Json::arrayValue;
  637. for (auto const& it : fileGroups) {
  638. Json::Value group = DumpSourceFileGroup(it.first, it.second, baseDir);
  639. if (!group.isNull()) {
  640. result.append(group);
  641. }
  642. }
  643. return result;
  644. }
  645. static Json::Value DumpBacktrace(const cmListFileBacktrace& backtrace)
  646. {
  647. Json::Value result = Json::arrayValue;
  648. cmListFileBacktrace backtraceCopy = backtrace;
  649. while (!backtraceCopy.Top().FilePath.empty()) {
  650. Json::Value entry = Json::objectValue;
  651. entry[kPATH_KEY] = backtraceCopy.Top().FilePath;
  652. if (backtraceCopy.Top().Line) {
  653. entry[kLINE_NUMBER_KEY] = static_cast<int>(backtraceCopy.Top().Line);
  654. }
  655. if (!backtraceCopy.Top().Name.empty()) {
  656. entry[kNAME_KEY] = backtraceCopy.Top().Name;
  657. }
  658. result.append(entry);
  659. backtraceCopy = backtraceCopy.Pop();
  660. }
  661. return result;
  662. }
  663. static void DumpBacktraceRange(Json::Value& result, const std::string& type,
  664. cmBacktraceRange range)
  665. {
  666. for (auto const& bt : range) {
  667. Json::Value obj = Json::objectValue;
  668. obj[kTYPE_KEY] = type;
  669. obj[kBACKTRACE_KEY] = DumpBacktrace(bt);
  670. result.append(obj);
  671. }
  672. }
  673. static Json::Value DumpCTestInfo(cmTest* testInfo)
  674. {
  675. Json::Value result = Json::objectValue;
  676. result[kCTEST_NAME] = testInfo->GetName();
  677. // Concat command entries together. After the first should be the arguments
  678. // for the command
  679. std::string command;
  680. for (auto const& cmd : testInfo->GetCommand()) {
  681. command.append(cmd);
  682. command.append(" ");
  683. }
  684. result[kCTEST_COMMAND] = command;
  685. // Build up the list of properties that may have been specified
  686. Json::Value properties = Json::arrayValue;
  687. for (auto& prop : testInfo->GetProperties()) {
  688. Json::Value entry = Json::objectValue;
  689. entry[kKEY_KEY] = prop.first;
  690. entry[kVALUE_KEY] = prop.second.GetValue();
  691. properties.append(entry);
  692. }
  693. result[kPROPERTIES_KEY] = properties;
  694. // Need backtrace to figure out where this test was originally added
  695. result[kBACKTRACE_KEY] = DumpBacktrace(testInfo->GetBacktrace());
  696. return result;
  697. }
  698. static void DumpMakefileTests(cmMakefile* mf, const std::string& config,
  699. Json::Value* result)
  700. {
  701. std::vector<cmTest*> tests;
  702. mf->GetTests(config, tests);
  703. for (auto test : tests) {
  704. Json::Value tmp = DumpCTestInfo(test);
  705. if (!tmp.isNull()) {
  706. result->append(tmp);
  707. }
  708. }
  709. }
  710. static Json::Value DumpCTestProjectList(const cmake* cm,
  711. std::string const& config)
  712. {
  713. Json::Value result = Json::arrayValue;
  714. auto globalGen = cm->GetGlobalGenerator();
  715. for (const auto& projectIt : globalGen->GetProjectMap()) {
  716. Json::Value pObj = Json::objectValue;
  717. pObj[kNAME_KEY] = projectIt.first;
  718. Json::Value tests = Json::arrayValue;
  719. // Gather tests for every generator
  720. for (const auto& lg : projectIt.second) {
  721. // Make sure they're generated.
  722. lg->GenerateTestFiles();
  723. cmMakefile* mf = lg->GetMakefile();
  724. DumpMakefileTests(mf, config, &tests);
  725. }
  726. pObj[kCTEST_INFO] = tests;
  727. result.append(pObj);
  728. }
  729. return result;
  730. }
  731. static Json::Value DumpCTestConfiguration(const cmake* cm,
  732. const std::string& config)
  733. {
  734. Json::Value result = Json::objectValue;
  735. result[kNAME_KEY] = config;
  736. result[kPROJECTS_KEY] = DumpCTestProjectList(cm, config);
  737. return result;
  738. }
  739. static Json::Value DumpCTestConfigurationsList(const cmake* cm)
  740. {
  741. Json::Value result = Json::arrayValue;
  742. for (const std::string& c : getConfigurations(cm)) {
  743. result.append(DumpCTestConfiguration(cm, c));
  744. }
  745. return result;
  746. }
  747. static Json::Value DumpTarget(cmGeneratorTarget* target,
  748. const std::string& config)
  749. {
  750. cmLocalGenerator* lg = target->GetLocalGenerator();
  751. const cmState* state = lg->GetState();
  752. const cmStateEnums::TargetType type = target->GetType();
  753. const std::string typeName = state->GetTargetTypeName(type);
  754. Json::Value ttl = Json::arrayValue;
  755. ttl.append("EXECUTABLE");
  756. ttl.append("STATIC_LIBRARY");
  757. ttl.append("SHARED_LIBRARY");
  758. ttl.append("MODULE_LIBRARY");
  759. ttl.append("OBJECT_LIBRARY");
  760. ttl.append("UTILITY");
  761. ttl.append("INTERFACE_LIBRARY");
  762. if (!hasString(ttl, typeName) || target->IsImported()) {
  763. return Json::Value();
  764. }
  765. Json::Value result = Json::objectValue;
  766. result[kNAME_KEY] = target->GetName();
  767. result[kIS_GENERATOR_PROVIDED_KEY] =
  768. target->Target->GetIsGeneratorProvided();
  769. result[kTYPE_KEY] = typeName;
  770. result[kSOURCE_DIRECTORY_KEY] = lg->GetCurrentSourceDirectory();
  771. result[kBUILD_DIRECTORY_KEY] = lg->GetCurrentBinaryDirectory();
  772. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  773. return result;
  774. }
  775. result[kFULL_NAME_KEY] = target->GetFullName(config);
  776. if (target->Target->GetHaveInstallRule()) {
  777. result[kHAS_INSTALL_RULE] = true;
  778. Json::Value installPaths = Json::arrayValue;
  779. auto targetGenerators = target->Makefile->GetInstallGenerators();
  780. for (auto installGenerator : targetGenerators) {
  781. auto installTargetGenerator =
  782. dynamic_cast<cmInstallTargetGenerator*>(installGenerator);
  783. if (installTargetGenerator != nullptr &&
  784. installTargetGenerator->GetTarget()->Target == target->Target) {
  785. auto dest = installTargetGenerator->GetDestination(config);
  786. std::string installPath;
  787. if (!dest.empty() && cmSystemTools::FileIsFullPath(dest.c_str())) {
  788. installPath = dest;
  789. } else {
  790. std::string installPrefix =
  791. target->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  792. installPath = installPrefix + '/' + dest;
  793. }
  794. installPaths.append(installPath);
  795. }
  796. }
  797. result[kINSTALL_PATHS] = installPaths;
  798. }
  799. Json::Value crossRefs = Json::objectValue;
  800. crossRefs[kBACKTRACE_KEY] = DumpBacktrace(target->Target->GetBacktrace());
  801. Json::Value statements = Json::arrayValue;
  802. DumpBacktraceRange(statements, "target_compile_definitions",
  803. target->Target->GetCompileDefinitionsBacktraces());
  804. DumpBacktraceRange(statements, "target_include_directories",
  805. target->Target->GetIncludeDirectoriesBacktraces());
  806. DumpBacktraceRange(statements, "target_compile_options",
  807. target->Target->GetCompileOptionsBacktraces());
  808. DumpBacktraceRange(statements, "target_link_libraries",
  809. target->Target->GetLinkImplementationBacktraces());
  810. crossRefs[kRELATED_STATEMENTS_KEY] = std::move(statements);
  811. result[kTARGET_CROSS_REFERENCES_KEY] = std::move(crossRefs);
  812. if (target->HaveWellDefinedOutputFiles()) {
  813. Json::Value artifacts = Json::arrayValue;
  814. artifacts.append(
  815. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact));
  816. if (target->IsDLLPlatform()) {
  817. artifacts.append(
  818. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact));
  819. const cmGeneratorTarget::OutputInfo* output =
  820. target->GetOutputInfo(config);
  821. if (output && !output->PdbDir.empty()) {
  822. artifacts.append(output->PdbDir + '/' + target->GetPDBName(config));
  823. }
  824. }
  825. result[kARTIFACTS_KEY] = artifacts;
  826. result[kLINKER_LANGUAGE_KEY] = target->GetLinkerLanguage(config);
  827. std::string linkLibs;
  828. std::string linkFlags;
  829. std::string linkLanguageFlags;
  830. std::string frameworkPath;
  831. std::string linkPath;
  832. cmLinkLineComputer linkLineComputer(lg,
  833. lg->GetStateSnapshot().GetDirectory());
  834. lg->GetTargetFlags(&linkLineComputer, config, linkLibs, linkLanguageFlags,
  835. linkFlags, frameworkPath, linkPath, target);
  836. linkLibs = cmSystemTools::TrimWhitespace(linkLibs);
  837. linkFlags = cmSystemTools::TrimWhitespace(linkFlags);
  838. linkLanguageFlags = cmSystemTools::TrimWhitespace(linkLanguageFlags);
  839. frameworkPath = cmSystemTools::TrimWhitespace(frameworkPath);
  840. linkPath = cmSystemTools::TrimWhitespace(linkPath);
  841. if (!cmSystemTools::TrimWhitespace(linkLibs).empty()) {
  842. result[kLINK_LIBRARIES_KEY] = linkLibs;
  843. }
  844. if (!cmSystemTools::TrimWhitespace(linkFlags).empty()) {
  845. result[kLINK_FLAGS_KEY] = linkFlags;
  846. }
  847. if (!cmSystemTools::TrimWhitespace(linkLanguageFlags).empty()) {
  848. result[kLINK_LANGUAGE_FLAGS_KEY] = linkLanguageFlags;
  849. }
  850. if (!frameworkPath.empty()) {
  851. result[kFRAMEWORK_PATH_KEY] = frameworkPath;
  852. }
  853. if (!linkPath.empty()) {
  854. result[kLINK_PATH_KEY] = linkPath;
  855. }
  856. const std::string sysroot =
  857. lg->GetMakefile()->GetSafeDefinition("CMAKE_SYSROOT");
  858. if (!sysroot.empty()) {
  859. result[kSYSROOT_KEY] = sysroot;
  860. }
  861. }
  862. std::set<std::string> languages;
  863. target->GetLanguages(languages, config);
  864. std::map<std::string, LanguageData> languageDataMap;
  865. for (std::string const& lang : languages) {
  866. LanguageData& ld = languageDataMap[lang];
  867. ld.Language = lang;
  868. lg->GetTargetCompileFlags(target, config, lang, ld.Flags);
  869. std::set<std::string> defines;
  870. lg->GetTargetDefines(target, config, lang, defines);
  871. ld.SetDefines(defines);
  872. std::vector<std::string> includePathList;
  873. lg->GetIncludeDirectories(includePathList, target, lang, config, true);
  874. for (std::string const& i : includePathList) {
  875. ld.IncludePathList.push_back(
  876. std::make_pair(i, target->IsSystemIncludeDirectory(i, config)));
  877. }
  878. }
  879. Json::Value sourceGroupsValue =
  880. DumpSourceFilesList(target, config, languageDataMap);
  881. if (!sourceGroupsValue.empty()) {
  882. result[kFILE_GROUPS_KEY] = sourceGroupsValue;
  883. }
  884. return result;
  885. }
  886. static Json::Value DumpTargetsList(
  887. const std::vector<cmLocalGenerator*>& generators, const std::string& config)
  888. {
  889. Json::Value result = Json::arrayValue;
  890. std::vector<cmGeneratorTarget*> targetList;
  891. for (auto const& lgIt : generators) {
  892. const auto& list = lgIt->GetGeneratorTargets();
  893. targetList.insert(targetList.end(), list.begin(), list.end());
  894. }
  895. std::sort(targetList.begin(), targetList.end());
  896. for (cmGeneratorTarget* target : targetList) {
  897. Json::Value tmp = DumpTarget(target, config);
  898. if (!tmp.isNull()) {
  899. result.append(tmp);
  900. }
  901. }
  902. return result;
  903. }
  904. static Json::Value DumpProjectList(const cmake* cm, std::string const& config)
  905. {
  906. Json::Value result = Json::arrayValue;
  907. auto globalGen = cm->GetGlobalGenerator();
  908. for (auto const& projectIt : globalGen->GetProjectMap()) {
  909. Json::Value pObj = Json::objectValue;
  910. pObj[kNAME_KEY] = projectIt.first;
  911. // All Projects must have at least one local generator
  912. assert(!projectIt.second.empty());
  913. const cmLocalGenerator* lg = projectIt.second.at(0);
  914. // Project structure information:
  915. const cmMakefile* mf = lg->GetMakefile();
  916. pObj[kMINIMUM_CMAKE_VERSION] =
  917. mf->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  918. pObj[kSOURCE_DIRECTORY_KEY] = mf->GetCurrentSourceDirectory();
  919. pObj[kBUILD_DIRECTORY_KEY] = mf->GetCurrentBinaryDirectory();
  920. pObj[kTARGETS_KEY] = DumpTargetsList(projectIt.second, config);
  921. // For a project-level install rule it might be defined in any of its
  922. // associated generators.
  923. bool hasInstallRule = false;
  924. for (const auto generator : projectIt.second) {
  925. hasInstallRule =
  926. generator->GetMakefile()->GetInstallGenerators().empty() == false;
  927. if (hasInstallRule) {
  928. break;
  929. }
  930. }
  931. pObj[kHAS_INSTALL_RULE] = hasInstallRule;
  932. result.append(pObj);
  933. }
  934. return result;
  935. }
  936. static Json::Value DumpConfiguration(const cmake* cm,
  937. const std::string& config)
  938. {
  939. Json::Value result = Json::objectValue;
  940. result[kNAME_KEY] = config;
  941. result[kPROJECTS_KEY] = DumpProjectList(cm, config);
  942. return result;
  943. }
  944. static Json::Value DumpConfigurationsList(const cmake* cm)
  945. {
  946. Json::Value result = Json::arrayValue;
  947. for (std::string const& c : getConfigurations(cm)) {
  948. result.append(DumpConfiguration(cm, c));
  949. }
  950. return result;
  951. }
  952. cmServerResponse cmServerProtocol1::ProcessCodeModel(
  953. const cmServerRequest& request)
  954. {
  955. if (this->m_State != STATE_COMPUTED) {
  956. return request.ReportError("No build system was generated yet.");
  957. }
  958. Json::Value result = Json::objectValue;
  959. result[kCONFIGURATIONS_KEY] = DumpConfigurationsList(this->CMakeInstance());
  960. return request.Reply(result);
  961. }
  962. cmServerResponse cmServerProtocol1::ProcessCompute(
  963. const cmServerRequest& request)
  964. {
  965. if (this->m_State > STATE_CONFIGURED) {
  966. return request.ReportError("This build system was already generated.");
  967. }
  968. if (this->m_State < STATE_CONFIGURED) {
  969. return request.ReportError("This project was not configured yet.");
  970. }
  971. cmake* cm = this->CMakeInstance();
  972. int ret = cm->Generate();
  973. if (ret < 0) {
  974. return request.ReportError("Failed to compute build system.");
  975. }
  976. m_State = STATE_COMPUTED;
  977. return request.Reply(Json::Value());
  978. }
  979. cmServerResponse cmServerProtocol1::ProcessConfigure(
  980. const cmServerRequest& request)
  981. {
  982. if (this->m_State == STATE_INACTIVE) {
  983. return request.ReportError("This instance is inactive.");
  984. }
  985. FileMonitor()->StopMonitoring();
  986. std::string errorMessage;
  987. cmake* cm = this->CMakeInstance();
  988. this->GeneratorInfo.SetupGenerator(cm, &errorMessage);
  989. if (!errorMessage.empty()) {
  990. return request.ReportError(errorMessage);
  991. }
  992. // Make sure the types of cacheArguments matches (if given):
  993. std::vector<std::string> cacheArgs = { "unused" };
  994. bool cacheArgumentsError = false;
  995. const Json::Value passedArgs = request.Data[kCACHE_ARGUMENTS_KEY];
  996. if (!passedArgs.isNull()) {
  997. if (passedArgs.isString()) {
  998. cacheArgs.push_back(passedArgs.asString());
  999. } else if (passedArgs.isArray()) {
  1000. for (auto const& arg : passedArgs) {
  1001. if (!arg.isString()) {
  1002. cacheArgumentsError = true;
  1003. break;
  1004. }
  1005. cacheArgs.push_back(arg.asString());
  1006. }
  1007. } else {
  1008. cacheArgumentsError = true;
  1009. }
  1010. }
  1011. if (cacheArgumentsError) {
  1012. request.ReportError(
  1013. "cacheArguments must be unset, a string or an array of strings.");
  1014. }
  1015. std::string sourceDir = cm->GetHomeDirectory();
  1016. const std::string buildDir = cm->GetHomeOutputDirectory();
  1017. cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  1018. if (buildDir.empty()) {
  1019. return request.ReportError("No build directory set via Handshake.");
  1020. }
  1021. if (cm->LoadCache(buildDir)) {
  1022. // build directory has been set up before
  1023. const char* cachedSourceDir =
  1024. cm->GetState()->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
  1025. if (!cachedSourceDir) {
  1026. return request.ReportError("No CMAKE_HOME_DIRECTORY found in cache.");
  1027. }
  1028. if (sourceDir.empty()) {
  1029. sourceDir = std::string(cachedSourceDir);
  1030. cm->SetHomeDirectory(sourceDir);
  1031. }
  1032. const char* cachedGenerator =
  1033. cm->GetState()->GetInitializedCacheValue("CMAKE_GENERATOR");
  1034. if (cachedGenerator) {
  1035. if (gg && gg->GetName() != cachedGenerator) {
  1036. return request.ReportError("Configured generator does not match with "
  1037. "CMAKE_GENERATOR found in cache.");
  1038. }
  1039. }
  1040. } else {
  1041. // build directory has not been set up before
  1042. if (sourceDir.empty()) {
  1043. return request.ReportError("No sourceDirectory set via "
  1044. "setGlobalSettings and no cache found in "
  1045. "buildDirectory.");
  1046. }
  1047. }
  1048. cmSystemTools::ResetErrorOccuredFlag(); // Reset error state
  1049. if (cm->AddCMakePaths() != 1) {
  1050. return request.ReportError("Failed to set CMake paths.");
  1051. }
  1052. if (!cm->SetCacheArgs(cacheArgs)) {
  1053. return request.ReportError("cacheArguments could not be set.");
  1054. }
  1055. int ret = cm->Configure();
  1056. if (ret < 0) {
  1057. return request.ReportError("Configuration failed.");
  1058. }
  1059. std::vector<std::string> toWatchList;
  1060. getCMakeInputs(gg, std::string(), buildDir, nullptr, &toWatchList, nullptr);
  1061. FileMonitor()->MonitorPaths(toWatchList,
  1062. [this](const std::string& p, int e, int s) {
  1063. this->HandleCMakeFileChanges(p, e, s);
  1064. });
  1065. m_State = STATE_CONFIGURED;
  1066. m_isDirty = false;
  1067. return request.Reply(Json::Value());
  1068. }
  1069. cmServerResponse cmServerProtocol1::ProcessGlobalSettings(
  1070. const cmServerRequest& request)
  1071. {
  1072. cmake* cm = this->CMakeInstance();
  1073. Json::Value obj = Json::objectValue;
  1074. // Capabilities information:
  1075. obj[kCAPABILITIES_KEY] = cm->ReportCapabilitiesJson(true);
  1076. obj[kDEBUG_OUTPUT_KEY] = cm->GetDebugOutput();
  1077. obj[kTRACE_KEY] = cm->GetTrace();
  1078. obj[kTRACE_EXPAND_KEY] = cm->GetTraceExpand();
  1079. obj[kWARN_UNINITIALIZED_KEY] = cm->GetWarnUninitialized();
  1080. obj[kWARN_UNUSED_KEY] = cm->GetWarnUnused();
  1081. obj[kWARN_UNUSED_CLI_KEY] = cm->GetWarnUnusedCli();
  1082. obj[kCHECK_SYSTEM_VARS_KEY] = cm->GetCheckSystemVars();
  1083. obj[kSOURCE_DIRECTORY_KEY] = this->GeneratorInfo.SourceDirectory;
  1084. obj[kBUILD_DIRECTORY_KEY] = this->GeneratorInfo.BuildDirectory;
  1085. // Currently used generator:
  1086. obj[kGENERATOR_KEY] = this->GeneratorInfo.GeneratorName;
  1087. obj[kEXTRA_GENERATOR_KEY] = this->GeneratorInfo.ExtraGeneratorName;
  1088. return request.Reply(obj);
  1089. }
  1090. static void setBool(const cmServerRequest& request, const std::string& key,
  1091. std::function<void(bool)> const& setter)
  1092. {
  1093. if (request.Data[key].isNull()) {
  1094. return;
  1095. }
  1096. setter(request.Data[key].asBool());
  1097. }
  1098. cmServerResponse cmServerProtocol1::ProcessSetGlobalSettings(
  1099. const cmServerRequest& request)
  1100. {
  1101. const std::vector<std::string> boolValues = {
  1102. kDEBUG_OUTPUT_KEY, kTRACE_KEY, kTRACE_EXPAND_KEY,
  1103. kWARN_UNINITIALIZED_KEY, kWARN_UNUSED_KEY, kWARN_UNUSED_CLI_KEY,
  1104. kCHECK_SYSTEM_VARS_KEY
  1105. };
  1106. for (std::string const& i : boolValues) {
  1107. if (!request.Data[i].isNull() && !request.Data[i].isBool()) {
  1108. return request.ReportError("\"" + i +
  1109. "\" must be unset or a bool value.");
  1110. }
  1111. }
  1112. cmake* cm = this->CMakeInstance();
  1113. setBool(request, kDEBUG_OUTPUT_KEY,
  1114. [cm](bool e) { cm->SetDebugOutputOn(e); });
  1115. setBool(request, kTRACE_KEY, [cm](bool e) { cm->SetTrace(e); });
  1116. setBool(request, kTRACE_EXPAND_KEY, [cm](bool e) { cm->SetTraceExpand(e); });
  1117. setBool(request, kWARN_UNINITIALIZED_KEY,
  1118. [cm](bool e) { cm->SetWarnUninitialized(e); });
  1119. setBool(request, kWARN_UNUSED_KEY, [cm](bool e) { cm->SetWarnUnused(e); });
  1120. setBool(request, kWARN_UNUSED_CLI_KEY,
  1121. [cm](bool e) { cm->SetWarnUnusedCli(e); });
  1122. setBool(request, kCHECK_SYSTEM_VARS_KEY,
  1123. [cm](bool e) { cm->SetCheckSystemVars(e); });
  1124. return request.Reply(Json::Value());
  1125. }
  1126. cmServerResponse cmServerProtocol1::ProcessFileSystemWatchers(
  1127. const cmServerRequest& request)
  1128. {
  1129. const cmFileMonitor* const fm = FileMonitor();
  1130. Json::Value result = Json::objectValue;
  1131. Json::Value files = Json::arrayValue;
  1132. for (auto const& f : fm->WatchedFiles()) {
  1133. files.append(f);
  1134. }
  1135. Json::Value directories = Json::arrayValue;
  1136. for (auto const& d : fm->WatchedDirectories()) {
  1137. directories.append(d);
  1138. }
  1139. result[kWATCHED_FILES_KEY] = files;
  1140. result[kWATCHED_DIRECTORIES_KEY] = directories;
  1141. return request.Reply(result);
  1142. }
  1143. cmServerResponse cmServerProtocol1::ProcessCTests(
  1144. const cmServerRequest& request)
  1145. {
  1146. if (this->m_State < STATE_COMPUTED) {
  1147. return request.ReportError("This instance was not yet computed.");
  1148. }
  1149. Json::Value result = Json::objectValue;
  1150. result[kCONFIGURATIONS_KEY] =
  1151. DumpCTestConfigurationsList(this->CMakeInstance());
  1152. return request.Reply(result);
  1153. }
  1154. cmServerProtocol1::GeneratorInformation::GeneratorInformation(
  1155. const std::string& generatorName, const std::string& extraGeneratorName,
  1156. const std::string& toolset, const std::string& platform,
  1157. const std::string& sourceDirectory, const std::string& buildDirectory)
  1158. : GeneratorName(generatorName)
  1159. , ExtraGeneratorName(extraGeneratorName)
  1160. , Toolset(toolset)
  1161. , Platform(platform)
  1162. , SourceDirectory(sourceDirectory)
  1163. , BuildDirectory(buildDirectory)
  1164. {
  1165. }
  1166. void cmServerProtocol1::GeneratorInformation::SetupGenerator(
  1167. cmake* cm, std::string* errorMessage)
  1168. {
  1169. const std::string fullGeneratorName =
  1170. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  1171. GeneratorName, ExtraGeneratorName);
  1172. cm->SetHomeDirectory(SourceDirectory);
  1173. cm->SetHomeOutputDirectory(BuildDirectory);
  1174. cmGlobalGenerator* gg = cm->CreateGlobalGenerator(fullGeneratorName);
  1175. if (!gg) {
  1176. setErrorMessage(
  1177. errorMessage,
  1178. std::string("Could not set up the requested combination of \"") +
  1179. kGENERATOR_KEY + "\" and \"" + kEXTRA_GENERATOR_KEY + "\"");
  1180. return;
  1181. }
  1182. cm->SetGlobalGenerator(gg);
  1183. cm->SetGeneratorToolset(Toolset);
  1184. cm->SetGeneratorPlatform(Platform);
  1185. }