cmServerProtocol.cxx 42 KB

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