cmServerProtocol.cxx 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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 "cmExternalMakefileProjectGenerator.h"
  5. #include "cmFileMonitor.h"
  6. #include "cmGeneratorTarget.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmLinkLineComputer.h"
  9. #include "cmListFileCache.h"
  10. #include "cmLocalGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmServer.h"
  13. #include "cmServerDictionary.h"
  14. #include "cmSourceFile.h"
  15. #include "cmSystemTools.h"
  16. #include "cmake.h"
  17. #include "cmServerDictionary.h"
  18. #if defined(CMAKE_BUILD_WITH_CMAKE)
  19. #include "cm_jsoncpp_reader.h"
  20. #include "cm_jsoncpp_value.h"
  21. #endif
  22. #include <algorithm>
  23. #include <string>
  24. #include <vector>
  25. // Get rid of some windows macros:
  26. #undef max
  27. namespace {
  28. static std::vector<std::string> getConfigurations(const cmake* cm)
  29. {
  30. std::vector<std::string> configurations;
  31. auto makefiles = cm->GetGlobalGenerator()->GetMakefiles();
  32. if (makefiles.empty()) {
  33. return configurations;
  34. }
  35. makefiles[0]->GetConfigurations(configurations);
  36. if (configurations.empty()) {
  37. configurations.push_back("");
  38. }
  39. return configurations;
  40. }
  41. static bool hasString(const Json::Value& v, const std::string& s)
  42. {
  43. return !v.isNull() &&
  44. std::find_if(v.begin(), v.end(), [s](const Json::Value& i) {
  45. return i.asString() == s;
  46. }) != v.end();
  47. }
  48. template <class T>
  49. static Json::Value fromStringList(const T& in)
  50. {
  51. Json::Value result = Json::arrayValue;
  52. for (const std::string& i : in) {
  53. result.append(i);
  54. }
  55. return result;
  56. }
  57. static std::vector<std::string> toStringList(const Json::Value& in)
  58. {
  59. std::vector<std::string> result;
  60. for (const auto& it : in) {
  61. result.push_back(it.asString());
  62. }
  63. return result;
  64. }
  65. static void getCMakeInputs(const cmGlobalGenerator* gg,
  66. const std::string& sourceDir,
  67. const std::string& buildDir,
  68. std::vector<std::string>* internalFiles,
  69. std::vector<std::string>* explicitFiles,
  70. std::vector<std::string>* tmpFiles)
  71. {
  72. const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot() + '/';
  73. const std::vector<cmMakefile*> makefiles = gg->GetMakefiles();
  74. for (auto it = makefiles.begin(); it != makefiles.end(); ++it) {
  75. const std::vector<std::string> listFiles = (*it)->GetListFiles();
  76. for (auto jt = listFiles.begin(); jt != listFiles.end(); ++jt) {
  77. const std::string startOfFile = jt->substr(0, cmakeRootDir.size());
  78. const bool isInternal = (startOfFile == cmakeRootDir);
  79. const bool isTemporary = !isInternal && (jt->find(buildDir + '/') == 0);
  80. std::string toAdd = *jt;
  81. if (!sourceDir.empty()) {
  82. const std::string& relative =
  83. cmSystemTools::RelativePath(sourceDir.c_str(), jt->c_str());
  84. if (toAdd.size() > relative.size()) {
  85. toAdd = relative;
  86. }
  87. }
  88. if (isInternal) {
  89. if (internalFiles) {
  90. internalFiles->push_back(toAdd);
  91. }
  92. } else {
  93. if (isTemporary) {
  94. if (tmpFiles) {
  95. tmpFiles->push_back(toAdd);
  96. }
  97. } else {
  98. if (explicitFiles) {
  99. explicitFiles->push_back(toAdd);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. } // namespace
  107. cmServerRequest::cmServerRequest(cmServer* server, const std::string& t,
  108. const std::string& c, const Json::Value& d)
  109. : Type(t)
  110. , Cookie(c)
  111. , Data(d)
  112. , m_Server(server)
  113. {
  114. }
  115. void cmServerRequest::ReportProgress(int min, int current, int max,
  116. const std::string& message) const
  117. {
  118. this->m_Server->WriteProgress(*this, min, current, max, message);
  119. }
  120. void cmServerRequest::ReportMessage(const std::string& message,
  121. const std::string& title) const
  122. {
  123. m_Server->WriteMessage(*this, message, title);
  124. }
  125. cmServerResponse cmServerRequest::Reply(const Json::Value& data) const
  126. {
  127. cmServerResponse response(*this);
  128. response.SetData(data);
  129. return response;
  130. }
  131. cmServerResponse cmServerRequest::ReportError(const std::string& message) const
  132. {
  133. cmServerResponse response(*this);
  134. response.SetError(message);
  135. return response;
  136. }
  137. cmServerResponse::cmServerResponse(const cmServerRequest& request)
  138. : Type(request.Type)
  139. , Cookie(request.Cookie)
  140. {
  141. }
  142. void cmServerResponse::SetData(const Json::Value& data)
  143. {
  144. assert(this->m_Payload == PAYLOAD_UNKNOWN);
  145. if (!data[kCOOKIE_KEY].isNull() || !data[kTYPE_KEY].isNull()) {
  146. this->SetError("Response contains cookie or type field.");
  147. return;
  148. }
  149. this->m_Payload = PAYLOAD_DATA;
  150. this->m_Data = data;
  151. }
  152. void cmServerResponse::SetError(const std::string& message)
  153. {
  154. assert(this->m_Payload == PAYLOAD_UNKNOWN);
  155. this->m_Payload = PAYLOAD_ERROR;
  156. this->m_ErrorMessage = message;
  157. }
  158. bool cmServerResponse::IsComplete() const
  159. {
  160. return this->m_Payload != PAYLOAD_UNKNOWN;
  161. }
  162. bool cmServerResponse::IsError() const
  163. {
  164. assert(this->m_Payload != PAYLOAD_UNKNOWN);
  165. return this->m_Payload == PAYLOAD_ERROR;
  166. }
  167. std::string cmServerResponse::ErrorMessage() const
  168. {
  169. if (this->m_Payload == PAYLOAD_ERROR) {
  170. return this->m_ErrorMessage;
  171. }
  172. return std::string();
  173. }
  174. Json::Value cmServerResponse::Data() const
  175. {
  176. assert(this->m_Payload != PAYLOAD_UNKNOWN);
  177. return this->m_Data;
  178. }
  179. bool cmServerProtocol::Activate(cmServer* server,
  180. const cmServerRequest& request,
  181. std::string* errorMessage)
  182. {
  183. assert(server);
  184. this->m_Server = server;
  185. this->m_CMakeInstance = std::make_unique<cmake>();
  186. const bool result = this->DoActivate(request, errorMessage);
  187. if (!result) {
  188. this->m_CMakeInstance = CM_NULLPTR;
  189. }
  190. return result;
  191. }
  192. cmFileMonitor* cmServerProtocol::FileMonitor() const
  193. {
  194. return this->m_Server ? this->m_Server->FileMonitor() : nullptr;
  195. }
  196. void cmServerProtocol::SendSignal(const std::string& name,
  197. const Json::Value& data) const
  198. {
  199. if (this->m_Server) {
  200. this->m_Server->WriteSignal(name, data);
  201. }
  202. }
  203. cmake* cmServerProtocol::CMakeInstance() const
  204. {
  205. return this->m_CMakeInstance.get();
  206. }
  207. bool cmServerProtocol::DoActivate(const cmServerRequest& /*request*/,
  208. std::string* /*errorMessage*/)
  209. {
  210. return true;
  211. }
  212. std::pair<int, int> cmServerProtocol1_0::ProtocolVersion() const
  213. {
  214. return std::make_pair(1, 0);
  215. }
  216. bool cmServerProtocol1_0::DoActivate(const cmServerRequest& request,
  217. std::string* errorMessage)
  218. {
  219. std::string sourceDirectory = request.Data[kSOURCE_DIRECTORY_KEY].asString();
  220. const std::string buildDirectory =
  221. request.Data[kBUILD_DIRECTORY_KEY].asString();
  222. std::string generator = request.Data[kGENERATOR_KEY].asString();
  223. std::string extraGenerator = request.Data[kEXTRA_GENERATOR_KEY].asString();
  224. if (buildDirectory.empty()) {
  225. if (errorMessage) {
  226. *errorMessage =
  227. std::string("\"") + kBUILD_DIRECTORY_KEY + "\" is missing.";
  228. }
  229. return false;
  230. }
  231. cmake* cm = CMakeInstance();
  232. if (cmSystemTools::PathExists(buildDirectory)) {
  233. if (!cmSystemTools::FileIsDirectory(buildDirectory)) {
  234. if (errorMessage) {
  235. *errorMessage = std::string("\"") + kBUILD_DIRECTORY_KEY +
  236. "\" exists but is not a directory.";
  237. }
  238. return false;
  239. }
  240. const std::string cachePath = cm->FindCacheFile(buildDirectory);
  241. if (cm->LoadCache(cachePath)) {
  242. cmState* state = cm->GetState();
  243. // Check generator:
  244. const std::string cachedGenerator =
  245. std::string(state->GetCacheEntryValue("CMAKE_GENERATOR"));
  246. if (cachedGenerator.empty() && generator.empty()) {
  247. if (errorMessage) {
  248. *errorMessage =
  249. std::string("\"") + kGENERATOR_KEY + "\" is required but unset.";
  250. }
  251. return false;
  252. }
  253. if (generator.empty()) {
  254. generator = cachedGenerator;
  255. }
  256. if (generator != cachedGenerator) {
  257. if (errorMessage) {
  258. *errorMessage = std::string("\"") + kGENERATOR_KEY +
  259. "\" set but incompatible with configured generator.";
  260. }
  261. return false;
  262. }
  263. // check extra generator:
  264. const std::string cachedExtraGenerator =
  265. std::string(state->GetCacheEntryValue("CMAKE_EXTRA_GENERATOR"));
  266. if (!cachedExtraGenerator.empty() && !extraGenerator.empty() &&
  267. cachedExtraGenerator != extraGenerator) {
  268. if (errorMessage) {
  269. *errorMessage = std::string("\"") + kEXTRA_GENERATOR_KEY +
  270. "\" is set but incompatible with configured extra generator.";
  271. }
  272. return false;
  273. }
  274. if (extraGenerator.empty()) {
  275. extraGenerator = cachedExtraGenerator;
  276. }
  277. // check sourcedir:
  278. const std::string cachedSourceDirectory =
  279. std::string(state->GetCacheEntryValue("CMAKE_HOME_DIRECTORY"));
  280. if (!cachedSourceDirectory.empty() && !sourceDirectory.empty() &&
  281. cachedSourceDirectory != sourceDirectory) {
  282. if (errorMessage) {
  283. *errorMessage = std::string("\"") + kSOURCE_DIRECTORY_KEY +
  284. "\" is set but incompatible with configured source directory.";
  285. }
  286. return false;
  287. }
  288. if (sourceDirectory.empty()) {
  289. sourceDirectory = cachedSourceDirectory;
  290. }
  291. }
  292. }
  293. if (sourceDirectory.empty()) {
  294. if (errorMessage) {
  295. *errorMessage = std::string("\"") + kSOURCE_DIRECTORY_KEY +
  296. "\" is unset but required.";
  297. }
  298. return false;
  299. }
  300. if (!cmSystemTools::FileIsDirectory(sourceDirectory)) {
  301. if (errorMessage) {
  302. *errorMessage =
  303. std::string("\"") + kSOURCE_DIRECTORY_KEY + "\" is not a directory.";
  304. }
  305. return false;
  306. }
  307. if (generator.empty()) {
  308. if (errorMessage) {
  309. *errorMessage =
  310. std::string("\"") + kGENERATOR_KEY + "\" is unset but required.";
  311. }
  312. return false;
  313. }
  314. const std::string fullGeneratorName =
  315. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  316. generator, extraGenerator);
  317. cmGlobalGenerator* gg = cm->CreateGlobalGenerator(fullGeneratorName);
  318. if (!gg) {
  319. if (errorMessage) {
  320. *errorMessage =
  321. std::string("Could not set up the requested combination of \"") +
  322. kGENERATOR_KEY + "\" and \"" + kEXTRA_GENERATOR_KEY + "\"";
  323. }
  324. return false;
  325. }
  326. cm->SetGlobalGenerator(gg);
  327. cm->SetHomeDirectory(sourceDirectory);
  328. cm->SetHomeOutputDirectory(buildDirectory);
  329. this->m_State = STATE_ACTIVE;
  330. return true;
  331. }
  332. void cmServerProtocol1_0::HandleCMakeFileChanges(const std::string& path,
  333. int event, int status)
  334. {
  335. assert(status == 0);
  336. static_cast<void>(status);
  337. if (!m_isDirty) {
  338. m_isDirty = true;
  339. SendSignal(kDIRTY_SIGNAL, Json::objectValue);
  340. }
  341. Json::Value obj = Json::objectValue;
  342. obj[kPATH_KEY] = path;
  343. Json::Value properties = Json::arrayValue;
  344. if (event & UV_RENAME) {
  345. properties.append(kRENAME_PROPERTY_VALUE);
  346. }
  347. if (event & UV_CHANGE) {
  348. properties.append(kCHANGE_PROPERTY_VALUE);
  349. }
  350. obj[kPROPERTIES_KEY] = properties;
  351. SendSignal(kFILE_CHANGE_SIGNAL, obj);
  352. }
  353. const cmServerResponse cmServerProtocol1_0::Process(
  354. const cmServerRequest& request)
  355. {
  356. assert(this->m_State >= STATE_ACTIVE);
  357. if (request.Type == kCACHE_TYPE) {
  358. return this->ProcessCache(request);
  359. }
  360. if (request.Type == kCMAKE_INPUTS_TYPE) {
  361. return this->ProcessCMakeInputs(request);
  362. }
  363. if (request.Type == kCODE_MODEL_TYPE) {
  364. return this->ProcessCodeModel(request);
  365. }
  366. if (request.Type == kCOMPUTE_TYPE) {
  367. return this->ProcessCompute(request);
  368. }
  369. if (request.Type == kCONFIGURE_TYPE) {
  370. return this->ProcessConfigure(request);
  371. }
  372. if (request.Type == kFILESYSTEM_WATCHERS_TYPE) {
  373. return this->ProcessFileSystemWatchers(request);
  374. }
  375. if (request.Type == kGLOBAL_SETTINGS_TYPE) {
  376. return this->ProcessGlobalSettings(request);
  377. }
  378. if (request.Type == kSET_GLOBAL_SETTINGS_TYPE) {
  379. return this->ProcessSetGlobalSettings(request);
  380. }
  381. return request.ReportError("Unknown command!");
  382. }
  383. bool cmServerProtocol1_0::IsExperimental() const
  384. {
  385. return true;
  386. }
  387. cmServerResponse cmServerProtocol1_0::ProcessCache(
  388. const cmServerRequest& request)
  389. {
  390. if (this->m_State < STATE_CONFIGURED) {
  391. return request.ReportError("This project was not configured yet.");
  392. }
  393. cmState* state = this->CMakeInstance()->GetState();
  394. Json::Value result = Json::objectValue;
  395. std::vector<std::string> allKeys = state->GetCacheEntryKeys();
  396. Json::Value list = Json::arrayValue;
  397. std::vector<std::string> keys = toStringList(request.Data[kKEYS_KEY]);
  398. if (keys.empty()) {
  399. keys = allKeys;
  400. } else {
  401. for (auto i : keys) {
  402. if (std::find_if(allKeys.begin(), allKeys.end(),
  403. [i](const std::string& j) { return i == j; }) ==
  404. allKeys.end()) {
  405. return request.ReportError("Key \"" + i + "\" not found in cache.");
  406. }
  407. }
  408. }
  409. std::sort(keys.begin(), keys.end());
  410. for (auto key : keys) {
  411. Json::Value entry = Json::objectValue;
  412. entry[kKEY_KEY] = key;
  413. entry[kTYPE_KEY] =
  414. cmState::CacheEntryTypeToString(state->GetCacheEntryType(key));
  415. entry[kVALUE_KEY] = state->GetCacheEntryValue(key);
  416. Json::Value props = Json::objectValue;
  417. bool haveProperties = false;
  418. for (auto prop : state->GetCacheEntryPropertyList(key)) {
  419. haveProperties = true;
  420. props[prop] = state->GetCacheEntryProperty(key, prop);
  421. }
  422. if (haveProperties) {
  423. entry[kPROPERTIES_KEY] = props;
  424. }
  425. list.append(entry);
  426. }
  427. result[kCACHE_KEY] = list;
  428. return request.Reply(result);
  429. }
  430. cmServerResponse cmServerProtocol1_0::ProcessCMakeInputs(
  431. const cmServerRequest& request)
  432. {
  433. if (this->m_State < STATE_CONFIGURED) {
  434. return request.ReportError("This instance was not yet configured.");
  435. }
  436. const cmake* cm = this->CMakeInstance();
  437. const cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  438. const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot();
  439. const std::string buildDir = cm->GetHomeOutputDirectory();
  440. const std::string sourceDir = cm->GetHomeDirectory();
  441. Json::Value result = Json::objectValue;
  442. result[kSOURCE_DIRECTORY_KEY] = sourceDir;
  443. result[kCMAKE_ROOT_DIRECTORY_KEY] = cmakeRootDir;
  444. std::vector<std::string> internalFiles;
  445. std::vector<std::string> explicitFiles;
  446. std::vector<std::string> tmpFiles;
  447. getCMakeInputs(gg, sourceDir, buildDir, &internalFiles, &explicitFiles,
  448. &tmpFiles);
  449. Json::Value array = Json::arrayValue;
  450. Json::Value tmp = Json::objectValue;
  451. tmp[kIS_CMAKE_KEY] = true;
  452. tmp[kIS_TEMPORARY_KEY] = false;
  453. tmp[kSOURCES_KEY] = fromStringList(internalFiles);
  454. array.append(tmp);
  455. tmp = Json::objectValue;
  456. tmp[kIS_CMAKE_KEY] = false;
  457. tmp[kIS_TEMPORARY_KEY] = false;
  458. tmp[kSOURCES_KEY] = fromStringList(explicitFiles);
  459. array.append(tmp);
  460. tmp = Json::objectValue;
  461. tmp[kIS_CMAKE_KEY] = false;
  462. tmp[kIS_TEMPORARY_KEY] = true;
  463. tmp[kSOURCES_KEY] = fromStringList(tmpFiles);
  464. array.append(tmp);
  465. result[kBUILD_FILES_KEY] = array;
  466. return request.Reply(result);
  467. }
  468. class LanguageData
  469. {
  470. public:
  471. bool operator==(const LanguageData& other) const;
  472. void SetDefines(const std::set<std::string>& defines);
  473. bool IsGenerated = false;
  474. std::string Language;
  475. std::string Flags;
  476. std::vector<std::string> Defines;
  477. std::vector<std::pair<std::string, bool> > IncludePathList;
  478. };
  479. bool LanguageData::operator==(const LanguageData& other) const
  480. {
  481. return Language == other.Language && Defines == other.Defines &&
  482. Flags == other.Flags && IncludePathList == other.IncludePathList &&
  483. IsGenerated == other.IsGenerated;
  484. }
  485. void LanguageData::SetDefines(const std::set<std::string>& defines)
  486. {
  487. std::vector<std::string> result;
  488. for (auto i : defines) {
  489. result.push_back(i);
  490. }
  491. std::sort(result.begin(), result.end());
  492. Defines = result;
  493. }
  494. namespace std {
  495. template <>
  496. struct hash<LanguageData>
  497. {
  498. std::size_t operator()(const LanguageData& in) const
  499. {
  500. using std::hash;
  501. size_t result =
  502. hash<std::string>()(in.Language) ^ hash<std::string>()(in.Flags);
  503. for (auto i : in.IncludePathList) {
  504. result = result ^ (hash<std::string>()(i.first) ^
  505. (i.second ? std::numeric_limits<size_t>::max() : 0));
  506. }
  507. for (auto i : in.Defines) {
  508. result = result ^ hash<std::string>()(i);
  509. }
  510. result =
  511. result ^ (in.IsGenerated ? std::numeric_limits<size_t>::max() : 0);
  512. return result;
  513. }
  514. };
  515. } // namespace std
  516. static Json::Value DumpSourceFileGroup(const LanguageData& data,
  517. const std::vector<std::string>& files,
  518. const std::string& baseDir)
  519. {
  520. Json::Value result = Json::objectValue;
  521. if (!data.Language.empty()) {
  522. result[kLANGUAGE_KEY] = data.Language;
  523. if (!data.Flags.empty()) {
  524. result[kCOMPILE_FLAGS_KEY] = data.Flags;
  525. }
  526. if (!data.IncludePathList.empty()) {
  527. Json::Value includes = Json::arrayValue;
  528. for (auto i : data.IncludePathList) {
  529. Json::Value tmp = Json::objectValue;
  530. tmp[kPATH_KEY] = i.first;
  531. if (i.second) {
  532. tmp[kIS_SYSTEM_KEY] = i.second;
  533. }
  534. includes.append(tmp);
  535. }
  536. result[kINCLUDE_PATH_KEY] = includes;
  537. }
  538. if (!data.Defines.empty()) {
  539. result[kDEFINES_KEY] = fromStringList(data.Defines);
  540. }
  541. }
  542. result[kIS_GENERATED_KEY] = data.IsGenerated;
  543. Json::Value sourcesValue = Json::arrayValue;
  544. for (auto i : files) {
  545. const std::string relPath =
  546. cmSystemTools::RelativePath(baseDir.c_str(), i.c_str());
  547. sourcesValue.append(relPath.size() < i.size() ? relPath : i);
  548. }
  549. result[kSOURCES_KEY] = sourcesValue;
  550. return result;
  551. }
  552. static Json::Value DumpSourceFilesList(
  553. cmGeneratorTarget* target, const std::string& config,
  554. const std::map<std::string, LanguageData>& languageDataMap)
  555. {
  556. // Collect sourcefile groups:
  557. std::vector<cmSourceFile*> files;
  558. target->GetSourceFiles(files, config);
  559. std::unordered_map<LanguageData, std::vector<std::string> > fileGroups;
  560. for (cmSourceFile* file : files) {
  561. LanguageData fileData;
  562. fileData.Language = file->GetLanguage();
  563. if (!fileData.Language.empty()) {
  564. const LanguageData& ld = languageDataMap.at(fileData.Language);
  565. cmLocalGenerator* lg = target->GetLocalGenerator();
  566. std::string compileFlags = ld.Flags;
  567. lg->AppendFlags(compileFlags, file->GetProperty("COMPILE_FLAGS"));
  568. fileData.Flags = compileFlags;
  569. fileData.IncludePathList = ld.IncludePathList;
  570. std::set<std::string> defines;
  571. lg->AppendDefines(defines, file->GetProperty("COMPILE_DEFINITIONS"));
  572. const std::string defPropName =
  573. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  574. lg->AppendDefines(defines, file->GetProperty(defPropName));
  575. defines.insert(ld.Defines.begin(), ld.Defines.end());
  576. fileData.SetDefines(defines);
  577. }
  578. fileData.IsGenerated = file->GetPropertyAsBool("GENERATED");
  579. std::vector<std::string>& groupFileList = fileGroups[fileData];
  580. groupFileList.push_back(file->GetFullPath());
  581. }
  582. const std::string baseDir = target->Makefile->GetCurrentSourceDirectory();
  583. Json::Value result = Json::arrayValue;
  584. for (auto it = fileGroups.begin(); it != fileGroups.end(); ++it) {
  585. Json::Value group = DumpSourceFileGroup(it->first, it->second, baseDir);
  586. if (!group.isNull()) {
  587. result.append(group);
  588. }
  589. }
  590. return result;
  591. }
  592. static Json::Value DumpTarget(cmGeneratorTarget* target,
  593. const std::string& config)
  594. {
  595. cmLocalGenerator* lg = target->GetLocalGenerator();
  596. const cmState* state = lg->GetState();
  597. const cmStateEnums::TargetType type = target->GetType();
  598. const std::string typeName = state->GetTargetTypeName(type);
  599. Json::Value ttl = Json::arrayValue;
  600. ttl.append("EXECUTABLE");
  601. ttl.append("STATIC_LIBRARY");
  602. ttl.append("SHARED_LIBRARY");
  603. ttl.append("MODULE_LIBRARY");
  604. ttl.append("OBJECT_LIBRARY");
  605. ttl.append("UTILITY");
  606. ttl.append("INTERFACE_LIBRARY");
  607. if (!hasString(ttl, typeName) || target->IsImported()) {
  608. return Json::Value();
  609. }
  610. Json::Value result = Json::objectValue;
  611. result[kNAME_KEY] = target->GetName();
  612. result[kTYPE_KEY] = typeName;
  613. result[kFULL_NAME_KEY] = target->GetFullName(config);
  614. result[kSOURCE_DIRECTORY_KEY] = lg->GetCurrentSourceDirectory();
  615. result[kBUILD_DIRECTORY_KEY] = lg->GetCurrentBinaryDirectory();
  616. if (target->HaveWellDefinedOutputFiles()) {
  617. Json::Value artifacts = Json::arrayValue;
  618. artifacts.append(target->GetFullPath(config, false));
  619. if (target->IsDLLPlatform()) {
  620. artifacts.append(target->GetFullPath(config, true));
  621. const cmGeneratorTarget::OutputInfo* output =
  622. target->GetOutputInfo(config);
  623. if (output && !output->PdbDir.empty()) {
  624. artifacts.append(output->PdbDir + '/' + target->GetPDBName(config));
  625. }
  626. }
  627. result[kARTIFACTS_KEY] = artifacts;
  628. result[kLINKER_LANGUAGE_KEY] = target->GetLinkerLanguage(config);
  629. std::string linkLibs;
  630. std::string linkFlags;
  631. std::string linkLanguageFlags;
  632. std::string frameworkPath;
  633. std::string linkPath;
  634. cmLinkLineComputer linkLineComputer(lg,
  635. lg->GetStateSnapshot().GetDirectory());
  636. lg->GetTargetFlags(&linkLineComputer, config, linkLibs, linkLanguageFlags,
  637. linkFlags, frameworkPath, linkPath, target);
  638. linkLibs = cmSystemTools::TrimWhitespace(linkLibs);
  639. linkFlags = cmSystemTools::TrimWhitespace(linkFlags);
  640. linkLanguageFlags = cmSystemTools::TrimWhitespace(linkLanguageFlags);
  641. frameworkPath = cmSystemTools::TrimWhitespace(frameworkPath);
  642. linkPath = cmSystemTools::TrimWhitespace(linkPath);
  643. if (!cmSystemTools::TrimWhitespace(linkLibs).empty()) {
  644. result[kLINK_LIBRARIES_KEY] = linkLibs;
  645. }
  646. if (!cmSystemTools::TrimWhitespace(linkFlags).empty()) {
  647. result[kLINK_FLAGS_KEY] = linkFlags;
  648. }
  649. if (!cmSystemTools::TrimWhitespace(linkLanguageFlags).empty()) {
  650. result[kLINK_LANGUAGE_FLAGS_KEY] = linkLanguageFlags;
  651. }
  652. if (!frameworkPath.empty()) {
  653. result[kFRAMEWORK_PATH_KEY] = frameworkPath;
  654. }
  655. if (!linkPath.empty()) {
  656. result[kLINK_PATH_KEY] = linkPath;
  657. }
  658. const std::string sysroot =
  659. lg->GetMakefile()->GetSafeDefinition("CMAKE_SYSROOT");
  660. if (!sysroot.empty()) {
  661. result[kSYSROOT_KEY] = sysroot;
  662. }
  663. }
  664. std::set<std::string> languages;
  665. target->GetLanguages(languages, config);
  666. std::map<std::string, LanguageData> languageDataMap;
  667. for (auto lang : languages) {
  668. LanguageData& ld = languageDataMap[lang];
  669. ld.Language = lang;
  670. lg->GetTargetCompileFlags(target, config, lang, ld.Flags);
  671. std::set<std::string> defines;
  672. lg->GetTargetDefines(target, config, lang, defines);
  673. ld.SetDefines(defines);
  674. std::vector<std::string> includePathList;
  675. lg->GetIncludeDirectories(includePathList, target, lang, config, true);
  676. for (auto i : includePathList) {
  677. ld.IncludePathList.push_back(
  678. std::make_pair(i, target->IsSystemIncludeDirectory(i, config)));
  679. }
  680. }
  681. Json::Value sourceGroupsValue =
  682. DumpSourceFilesList(target, config, languageDataMap);
  683. if (!sourceGroupsValue.empty()) {
  684. result[kFILE_GROUPS_KEY] = sourceGroupsValue;
  685. }
  686. return result;
  687. }
  688. static Json::Value DumpTargetsList(
  689. const std::vector<cmLocalGenerator*>& generators, const std::string& config)
  690. {
  691. Json::Value result = Json::arrayValue;
  692. std::vector<cmGeneratorTarget*> targetList;
  693. for (const auto& lgIt : generators) {
  694. auto list = lgIt->GetGeneratorTargets();
  695. targetList.insert(targetList.end(), list.begin(), list.end());
  696. }
  697. std::sort(targetList.begin(), targetList.end());
  698. for (cmGeneratorTarget* target : targetList) {
  699. Json::Value tmp = DumpTarget(target, config);
  700. if (!tmp.isNull()) {
  701. result.append(tmp);
  702. }
  703. }
  704. return result;
  705. }
  706. static Json::Value DumpProjectList(const cmake* cm, const std::string config)
  707. {
  708. Json::Value result = Json::arrayValue;
  709. auto globalGen = cm->GetGlobalGenerator();
  710. for (const auto& projectIt : globalGen->GetProjectMap()) {
  711. Json::Value pObj = Json::objectValue;
  712. pObj[kNAME_KEY] = projectIt.first;
  713. // All Projects must have at least one local generator
  714. assert(!projectIt.second.empty());
  715. const cmLocalGenerator* lg = projectIt.second.at(0);
  716. // Project structure information:
  717. const cmMakefile* mf = lg->GetMakefile();
  718. pObj[kSOURCE_DIRECTORY_KEY] = mf->GetCurrentSourceDirectory();
  719. pObj[kBUILD_DIRECTORY_KEY] = mf->GetCurrentBinaryDirectory();
  720. pObj[kTARGETS_KEY] = DumpTargetsList(projectIt.second, config);
  721. result.append(pObj);
  722. }
  723. return result;
  724. }
  725. static Json::Value DumpConfiguration(const cmake* cm,
  726. const std::string& config)
  727. {
  728. Json::Value result = Json::objectValue;
  729. result[kNAME_KEY] = config;
  730. result[kPROJECTS_KEY] = DumpProjectList(cm, config);
  731. return result;
  732. }
  733. static Json::Value DumpConfigurationsList(const cmake* cm)
  734. {
  735. Json::Value result = Json::arrayValue;
  736. for (const std::string& c : getConfigurations(cm)) {
  737. result.append(DumpConfiguration(cm, c));
  738. }
  739. return result;
  740. }
  741. cmServerResponse cmServerProtocol1_0::ProcessCodeModel(
  742. const cmServerRequest& request)
  743. {
  744. if (this->m_State != STATE_COMPUTED) {
  745. return request.ReportError("No build system was generated yet.");
  746. }
  747. Json::Value result = Json::objectValue;
  748. result[kCONFIGURATIONS_KEY] = DumpConfigurationsList(this->CMakeInstance());
  749. return request.Reply(result);
  750. }
  751. cmServerResponse cmServerProtocol1_0::ProcessCompute(
  752. const cmServerRequest& request)
  753. {
  754. if (this->m_State > STATE_CONFIGURED) {
  755. return request.ReportError("This build system was already generated.");
  756. }
  757. if (this->m_State < STATE_CONFIGURED) {
  758. return request.ReportError("This project was not configured yet.");
  759. }
  760. cmake* cm = this->CMakeInstance();
  761. int ret = cm->Generate();
  762. if (ret < 0) {
  763. return request.ReportError("Failed to compute build system.");
  764. }
  765. m_State = STATE_COMPUTED;
  766. return request.Reply(Json::Value());
  767. }
  768. cmServerResponse cmServerProtocol1_0::ProcessConfigure(
  769. const cmServerRequest& request)
  770. {
  771. if (this->m_State == STATE_INACTIVE) {
  772. return request.ReportError("This instance is inactive.");
  773. }
  774. FileMonitor()->StopMonitoring();
  775. // Make sure the types of cacheArguments matches (if given):
  776. std::vector<std::string> cacheArgs;
  777. bool cacheArgumentsError = false;
  778. const Json::Value passedArgs = request.Data[kCACHE_ARGUMENTS_KEY];
  779. if (!passedArgs.isNull()) {
  780. if (passedArgs.isString()) {
  781. cacheArgs.push_back(passedArgs.asString());
  782. } else if (passedArgs.isArray()) {
  783. for (auto i = passedArgs.begin(); i != passedArgs.end(); ++i) {
  784. if (!i->isString()) {
  785. cacheArgumentsError = true;
  786. break;
  787. }
  788. cacheArgs.push_back(i->asString());
  789. }
  790. } else {
  791. cacheArgumentsError = true;
  792. }
  793. }
  794. if (cacheArgumentsError) {
  795. request.ReportError(
  796. "cacheArguments must be unset, a string or an array of strings.");
  797. }
  798. cmake* cm = this->CMakeInstance();
  799. std::string sourceDir = cm->GetHomeDirectory();
  800. const std::string buildDir = cm->GetHomeOutputDirectory();
  801. cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  802. if (buildDir.empty()) {
  803. return request.ReportError(
  804. "No build directory set via setGlobalSettings.");
  805. }
  806. if (cm->LoadCache(buildDir)) {
  807. // build directory has been set up before
  808. const char* cachedSourceDir =
  809. cm->GetState()->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
  810. if (!cachedSourceDir) {
  811. return request.ReportError("No CMAKE_HOME_DIRECTORY found in cache.");
  812. }
  813. if (sourceDir.empty()) {
  814. sourceDir = std::string(cachedSourceDir);
  815. cm->SetHomeDirectory(sourceDir);
  816. }
  817. const char* cachedGenerator =
  818. cm->GetState()->GetInitializedCacheValue("CMAKE_GENERATOR");
  819. if (cachedGenerator) {
  820. if (gg && gg->GetName() != cachedGenerator) {
  821. return request.ReportError("Configured generator does not match with "
  822. "CMAKE_GENERATOR found in cache.");
  823. }
  824. }
  825. } else {
  826. // build directory has not been set up before
  827. if (sourceDir.empty()) {
  828. return request.ReportError("No sourceDirectory set via "
  829. "setGlobalSettings and no cache found in "
  830. "buildDirectory.");
  831. }
  832. }
  833. if (cm->AddCMakePaths() != 1) {
  834. return request.ReportError("Failed to set CMake paths.");
  835. }
  836. if (!cm->SetCacheArgs(cacheArgs)) {
  837. return request.ReportError("cacheArguments could not be set.");
  838. }
  839. int ret = cm->Configure();
  840. if (ret < 0) {
  841. return request.ReportError("Configuration failed.");
  842. }
  843. std::vector<std::string> toWatchList;
  844. getCMakeInputs(gg, std::string(), buildDir, nullptr, &toWatchList, nullptr);
  845. FileMonitor()->MonitorPaths(toWatchList,
  846. [this](const std::string& p, int e, int s) {
  847. this->HandleCMakeFileChanges(p, e, s);
  848. });
  849. m_State = STATE_CONFIGURED;
  850. m_isDirty = false;
  851. return request.Reply(Json::Value());
  852. }
  853. cmServerResponse cmServerProtocol1_0::ProcessGlobalSettings(
  854. const cmServerRequest& request)
  855. {
  856. cmake* cm = this->CMakeInstance();
  857. Json::Value obj = Json::objectValue;
  858. // Capabilities information:
  859. obj[kCAPABILITIES_KEY] = cm->ReportCapabilitiesJson(true);
  860. obj[kDEBUG_OUTPUT_KEY] = cm->GetDebugOutput();
  861. obj[kTRACE_KEY] = cm->GetTrace();
  862. obj[kTRACE_EXPAND_KEY] = cm->GetTraceExpand();
  863. obj[kWARN_UNINITIALIZED_KEY] = cm->GetWarnUninitialized();
  864. obj[kWARN_UNUSED_KEY] = cm->GetWarnUnused();
  865. obj[kWARN_UNUSED_CLI_KEY] = cm->GetWarnUnusedCli();
  866. obj[kCHECK_SYSTEM_VARS_KEY] = cm->GetCheckSystemVars();
  867. obj[kSOURCE_DIRECTORY_KEY] = cm->GetHomeDirectory();
  868. obj[kBUILD_DIRECTORY_KEY] = cm->GetHomeOutputDirectory();
  869. // Currently used generator:
  870. cmGlobalGenerator* gen = cm->GetGlobalGenerator();
  871. obj[kGENERATOR_KEY] = gen ? gen->GetName() : std::string();
  872. obj[kEXTRA_GENERATOR_KEY] =
  873. gen ? gen->GetExtraGeneratorName() : std::string();
  874. return request.Reply(obj);
  875. }
  876. static void setBool(const cmServerRequest& request, const std::string& key,
  877. std::function<void(bool)> setter)
  878. {
  879. if (request.Data[key].isNull()) {
  880. return;
  881. }
  882. setter(request.Data[key].asBool());
  883. }
  884. cmServerResponse cmServerProtocol1_0::ProcessSetGlobalSettings(
  885. const cmServerRequest& request)
  886. {
  887. const std::vector<std::string> boolValues = {
  888. kDEBUG_OUTPUT_KEY, kTRACE_KEY, kTRACE_EXPAND_KEY,
  889. kWARN_UNINITIALIZED_KEY, kWARN_UNUSED_KEY, kWARN_UNUSED_CLI_KEY,
  890. kCHECK_SYSTEM_VARS_KEY
  891. };
  892. for (auto i : boolValues) {
  893. if (!request.Data[i].isNull() && !request.Data[i].isBool()) {
  894. return request.ReportError("\"" + i +
  895. "\" must be unset or a bool value.");
  896. }
  897. }
  898. cmake* cm = this->CMakeInstance();
  899. setBool(request, kDEBUG_OUTPUT_KEY,
  900. [cm](bool e) { cm->SetDebugOutputOn(e); });
  901. setBool(request, kTRACE_KEY, [cm](bool e) { cm->SetTrace(e); });
  902. setBool(request, kTRACE_EXPAND_KEY, [cm](bool e) { cm->SetTraceExpand(e); });
  903. setBool(request, kWARN_UNINITIALIZED_KEY,
  904. [cm](bool e) { cm->SetWarnUninitialized(e); });
  905. setBool(request, kWARN_UNUSED_KEY, [cm](bool e) { cm->SetWarnUnused(e); });
  906. setBool(request, kWARN_UNUSED_CLI_KEY,
  907. [cm](bool e) { cm->SetWarnUnusedCli(e); });
  908. setBool(request, kCHECK_SYSTEM_VARS_KEY,
  909. [cm](bool e) { cm->SetCheckSystemVars(e); });
  910. return request.Reply(Json::Value());
  911. }
  912. cmServerResponse cmServerProtocol1_0::ProcessFileSystemWatchers(
  913. const cmServerRequest& request)
  914. {
  915. const cmFileMonitor* const fm = FileMonitor();
  916. Json::Value result = Json::objectValue;
  917. Json::Value files = Json::arrayValue;
  918. for (const auto& f : fm->WatchedFiles()) {
  919. files.append(f);
  920. }
  921. Json::Value directories = Json::arrayValue;
  922. for (const auto& d : fm->WatchedDirectories()) {
  923. directories.append(d);
  924. }
  925. result[kWATCHED_FILES_KEY] = files;
  926. result[kWATCHED_DIRECTORIES_KEY] = directories;
  927. return request.Reply(result);
  928. }