cmServerProtocol.cxx 42 KB

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