cmServerProtocol.cxx 47 KB

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