cmServerProtocol.cxx 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmServerProtocol.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmExternalMakefileProjectGenerator.h"
  6. #include "cmFileMonitor.h"
  7. #include "cmGeneratorExpression.h"
  8. #include "cmGeneratorTarget.h"
  9. #include "cmGlobalGenerator.h"
  10. #include "cmInstallGenerator.h"
  11. #include "cmInstallTargetGenerator.h"
  12. #include "cmLinkLineComputer.h"
  13. #include "cmListFileCache.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmProperty.h"
  17. #include "cmServer.h"
  18. #include "cmServerDictionary.h"
  19. #include "cmSourceFile.h"
  20. #include "cmState.h"
  21. #include "cmStateDirectory.h"
  22. #include "cmStateSnapshot.h"
  23. #include "cmStateTypes.h"
  24. #include "cmSystemTools.h"
  25. #include "cmTarget.h"
  26. #include "cmTest.h"
  27. #include "cm_uv.h"
  28. #include "cmake.h"
  29. #include <algorithm>
  30. #include <cassert>
  31. #include <cstddef>
  32. #include <functional>
  33. #include <limits>
  34. #include <map>
  35. #include <memory>
  36. #include <set>
  37. #include <string>
  38. #include <unordered_map>
  39. #include <vector>
  40. // Get rid of some windows macros:
  41. #undef max
  42. namespace {
  43. std::vector<std::string> getConfigurations(const cmake* cm)
  44. {
  45. std::vector<std::string> configurations;
  46. auto makefiles = cm->GetGlobalGenerator()->GetMakefiles();
  47. if (makefiles.empty()) {
  48. return configurations;
  49. }
  50. makefiles[0]->GetConfigurations(configurations);
  51. if (configurations.empty()) {
  52. configurations.push_back("");
  53. }
  54. return configurations;
  55. }
  56. bool hasString(const Json::Value& v, const std::string& s)
  57. {
  58. return !v.isNull() &&
  59. std::any_of(v.begin(), v.end(),
  60. [s](const Json::Value& i) { return i.asString() == s; });
  61. }
  62. template <class T>
  63. Json::Value fromStringList(const T& in)
  64. {
  65. Json::Value result = Json::arrayValue;
  66. for (std::string const& i : in) {
  67. result.append(i);
  68. }
  69. return result;
  70. }
  71. std::vector<std::string> toStringList(const Json::Value& in)
  72. {
  73. std::vector<std::string> result;
  74. for (auto const& it : in) {
  75. result.push_back(it.asString());
  76. }
  77. return result;
  78. }
  79. void getCMakeInputs(const cmGlobalGenerator* gg, const std::string& sourceDir,
  80. const std::string& buildDir,
  81. std::vector<std::string>* internalFiles,
  82. std::vector<std::string>* explicitFiles,
  83. std::vector<std::string>* tmpFiles)
  84. {
  85. const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot() + '/';
  86. std::vector<cmMakefile*> const& makefiles = gg->GetMakefiles();
  87. for (cmMakefile const* mf : makefiles) {
  88. for (std::string const& lf : mf->GetListFiles()) {
  89. const std::string startOfFile = lf.substr(0, cmakeRootDir.size());
  90. const bool isInternal = (startOfFile == cmakeRootDir);
  91. const bool isTemporary = !isInternal && (lf.find(buildDir + '/') == 0);
  92. std::string toAdd = lf;
  93. if (!sourceDir.empty()) {
  94. const std::string& relative =
  95. cmSystemTools::RelativePath(sourceDir.c_str(), lf.c_str());
  96. if (toAdd.size() > relative.size()) {
  97. toAdd = relative;
  98. }
  99. }
  100. if (isInternal) {
  101. if (internalFiles) {
  102. internalFiles->push_back(toAdd);
  103. }
  104. } else {
  105. if (isTemporary) {
  106. if (tmpFiles) {
  107. tmpFiles->push_back(toAdd);
  108. }
  109. } else {
  110. if (explicitFiles) {
  111. explicitFiles->push_back(toAdd);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. } // namespace
  119. cmServerRequest::cmServerRequest(cmServer* server, cmConnection* connection,
  120. const std::string& t, const std::string& c,
  121. const Json::Value& d)
  122. : Type(t)
  123. , Cookie(c)
  124. , Data(d)
  125. , Connection(connection)
  126. , m_Server(server)
  127. {
  128. }
  129. void cmServerRequest::ReportProgress(int min, int current, int max,
  130. const std::string& message) const
  131. {
  132. this->m_Server->WriteProgress(*this, min, current, max, message);
  133. }
  134. void cmServerRequest::ReportMessage(const std::string& message,
  135. const std::string& title) const
  136. {
  137. m_Server->WriteMessage(*this, message, title);
  138. }
  139. cmServerResponse cmServerRequest::Reply(const Json::Value& data) const
  140. {
  141. cmServerResponse response(*this);
  142. response.SetData(data);
  143. return response;
  144. }
  145. cmServerResponse cmServerRequest::ReportError(const std::string& message) const
  146. {
  147. cmServerResponse response(*this);
  148. response.SetError(message);
  149. return response;
  150. }
  151. cmServerResponse::cmServerResponse(const cmServerRequest& request)
  152. : Type(request.Type)
  153. , Cookie(request.Cookie)
  154. {
  155. }
  156. void cmServerResponse::SetData(const Json::Value& data)
  157. {
  158. assert(this->m_Payload == PAYLOAD_UNKNOWN);
  159. if (!data[kCOOKIE_KEY].isNull() || !data[kTYPE_KEY].isNull()) {
  160. this->SetError("Response contains cookie or type field.");
  161. return;
  162. }
  163. this->m_Payload = PAYLOAD_DATA;
  164. this->m_Data = data;
  165. }
  166. void cmServerResponse::SetError(const std::string& message)
  167. {
  168. assert(this->m_Payload == PAYLOAD_UNKNOWN);
  169. this->m_Payload = PAYLOAD_ERROR;
  170. this->m_ErrorMessage = message;
  171. }
  172. bool cmServerResponse::IsComplete() const
  173. {
  174. return this->m_Payload != PAYLOAD_UNKNOWN;
  175. }
  176. bool cmServerResponse::IsError() const
  177. {
  178. assert(this->m_Payload != PAYLOAD_UNKNOWN);
  179. return this->m_Payload == PAYLOAD_ERROR;
  180. }
  181. std::string cmServerResponse::ErrorMessage() const
  182. {
  183. if (this->m_Payload == PAYLOAD_ERROR) {
  184. return this->m_ErrorMessage;
  185. }
  186. return std::string();
  187. }
  188. Json::Value cmServerResponse::Data() const
  189. {
  190. assert(this->m_Payload != PAYLOAD_UNKNOWN);
  191. return this->m_Data;
  192. }
  193. bool cmServerProtocol::Activate(cmServer* server,
  194. const cmServerRequest& request,
  195. std::string* errorMessage)
  196. {
  197. assert(server);
  198. this->m_Server = server;
  199. this->m_CMakeInstance = cm::make_unique<cmake>(cmake::RoleProject);
  200. const bool result = this->DoActivate(request, errorMessage);
  201. if (!result) {
  202. this->m_CMakeInstance = nullptr;
  203. }
  204. return result;
  205. }
  206. cmFileMonitor* cmServerProtocol::FileMonitor() const
  207. {
  208. return this->m_Server ? this->m_Server->FileMonitor() : nullptr;
  209. }
  210. void cmServerProtocol::SendSignal(const std::string& name,
  211. const Json::Value& data) const
  212. {
  213. if (this->m_Server) {
  214. this->m_Server->WriteSignal(name, data);
  215. }
  216. }
  217. cmake* cmServerProtocol::CMakeInstance() const
  218. {
  219. return this->m_CMakeInstance.get();
  220. }
  221. bool cmServerProtocol::DoActivate(const cmServerRequest& /*request*/,
  222. std::string* /*errorMessage*/)
  223. {
  224. return true;
  225. }
  226. std::pair<int, int> cmServerProtocol1::ProtocolVersion() const
  227. {
  228. return std::make_pair(1, 2);
  229. }
  230. static void setErrorMessage(std::string* errorMessage, const std::string& text)
  231. {
  232. if (errorMessage) {
  233. *errorMessage = text;
  234. }
  235. }
  236. static bool testHomeDirectory(cmState* state, std::string& value,
  237. std::string* errorMessage)
  238. {
  239. const std::string cachedValue =
  240. std::string(state->GetCacheEntryValue("CMAKE_HOME_DIRECTORY"));
  241. if (value.empty()) {
  242. value = cachedValue;
  243. return true;
  244. }
  245. const std::string suffix = "/CMakeLists.txt";
  246. const std::string cachedValueCML = cachedValue + suffix;
  247. const std::string valueCML = value + suffix;
  248. if (!cmSystemTools::SameFile(valueCML, cachedValueCML)) {
  249. setErrorMessage(errorMessage,
  250. std::string("\"CMAKE_HOME_DIRECTORY\" is set but "
  251. "incompatible with configured "
  252. "source directory value."));
  253. return false;
  254. }
  255. return true;
  256. }
  257. static bool testValue(cmState* state, const std::string& key,
  258. std::string& value, const std::string& keyDescription,
  259. std::string* errorMessage)
  260. {
  261. const char* entry = state->GetCacheEntryValue(key);
  262. const std::string cachedValue =
  263. entry == nullptr ? std::string() : std::string(entry);
  264. if (value.empty()) {
  265. value = cachedValue;
  266. }
  267. if (!cachedValue.empty() && cachedValue != value) {
  268. setErrorMessage(errorMessage, std::string("\"") + key +
  269. "\" is set but incompatible with configured " +
  270. keyDescription + " value.");
  271. return false;
  272. }
  273. return true;
  274. }
  275. bool cmServerProtocol1::DoActivate(const cmServerRequest& request,
  276. std::string* errorMessage)
  277. {
  278. std::string sourceDirectory = request.Data[kSOURCE_DIRECTORY_KEY].asString();
  279. const std::string buildDirectory =
  280. request.Data[kBUILD_DIRECTORY_KEY].asString();
  281. std::string generator = request.Data[kGENERATOR_KEY].asString();
  282. std::string extraGenerator = request.Data[kEXTRA_GENERATOR_KEY].asString();
  283. std::string toolset = request.Data[kTOOLSET_KEY].asString();
  284. std::string platform = request.Data[kPLATFORM_KEY].asString();
  285. if (buildDirectory.empty()) {
  286. setErrorMessage(errorMessage, std::string("\"") + kBUILD_DIRECTORY_KEY +
  287. "\" is missing.");
  288. return false;
  289. }
  290. cmake* cm = CMakeInstance();
  291. if (cmSystemTools::PathExists(buildDirectory)) {
  292. if (!cmSystemTools::FileIsDirectory(buildDirectory)) {
  293. setErrorMessage(errorMessage, std::string("\"") + kBUILD_DIRECTORY_KEY +
  294. "\" exists but is not a directory.");
  295. return false;
  296. }
  297. const std::string cachePath = cm->FindCacheFile(buildDirectory);
  298. if (cm->LoadCache(cachePath)) {
  299. cmState* state = cm->GetState();
  300. // Check generator:
  301. if (!testValue(state, "CMAKE_GENERATOR", generator, "generator",
  302. errorMessage)) {
  303. return false;
  304. }
  305. // check extra generator:
  306. if (!testValue(state, "CMAKE_EXTRA_GENERATOR", extraGenerator,
  307. "extra generator", errorMessage)) {
  308. return false;
  309. }
  310. // check sourcedir:
  311. if (!testHomeDirectory(state, sourceDirectory, errorMessage)) {
  312. return false;
  313. }
  314. // check toolset:
  315. if (!testValue(state, "CMAKE_GENERATOR_TOOLSET", toolset, "toolset",
  316. errorMessage)) {
  317. return false;
  318. }
  319. // check platform:
  320. if (!testValue(state, "CMAKE_GENERATOR_PLATFORM", platform, "platform",
  321. errorMessage)) {
  322. return false;
  323. }
  324. }
  325. }
  326. if (sourceDirectory.empty()) {
  327. setErrorMessage(errorMessage, std::string("\"") + kSOURCE_DIRECTORY_KEY +
  328. "\" is unset but required.");
  329. return false;
  330. }
  331. if (!cmSystemTools::FileIsDirectory(sourceDirectory)) {
  332. setErrorMessage(errorMessage, std::string("\"") + kSOURCE_DIRECTORY_KEY +
  333. "\" is not a directory.");
  334. return false;
  335. }
  336. if (generator.empty()) {
  337. setErrorMessage(errorMessage, std::string("\"") + kGENERATOR_KEY +
  338. "\" is unset but required.");
  339. return false;
  340. }
  341. std::vector<cmake::GeneratorInfo> generators;
  342. cm->GetRegisteredGenerators(generators);
  343. auto baseIt = std::find_if(generators.begin(), generators.end(),
  344. [&generator](const cmake::GeneratorInfo& info) {
  345. return info.name == generator;
  346. });
  347. if (baseIt == generators.end()) {
  348. setErrorMessage(errorMessage, std::string("Generator \"") + generator +
  349. "\" not supported.");
  350. return false;
  351. }
  352. auto extraIt = std::find_if(
  353. generators.begin(), generators.end(),
  354. [&generator, &extraGenerator](const cmake::GeneratorInfo& info) {
  355. return info.baseName == generator && info.extraName == extraGenerator;
  356. });
  357. if (extraIt == generators.end()) {
  358. setErrorMessage(errorMessage,
  359. std::string("The combination of generator \"" + generator +
  360. "\" and extra generator \"" + extraGenerator +
  361. "\" is not supported."));
  362. return false;
  363. }
  364. if (!extraIt->supportsToolset && !toolset.empty()) {
  365. setErrorMessage(errorMessage,
  366. std::string("Toolset was provided but is not supported by "
  367. "the requested generator."));
  368. return false;
  369. }
  370. if (!extraIt->supportsPlatform && !platform.empty()) {
  371. setErrorMessage(errorMessage,
  372. std::string("Platform was provided but is not supported "
  373. "by the requested generator."));
  374. return false;
  375. }
  376. this->GeneratorInfo =
  377. GeneratorInformation(generator, extraGenerator, toolset, platform,
  378. sourceDirectory, buildDirectory);
  379. this->m_State = STATE_ACTIVE;
  380. return true;
  381. }
  382. void cmServerProtocol1::HandleCMakeFileChanges(const std::string& path,
  383. int event, int status)
  384. {
  385. assert(status == 0);
  386. static_cast<void>(status);
  387. if (!m_isDirty) {
  388. m_isDirty = true;
  389. SendSignal(kDIRTY_SIGNAL, Json::objectValue);
  390. }
  391. Json::Value obj = Json::objectValue;
  392. obj[kPATH_KEY] = path;
  393. Json::Value properties = Json::arrayValue;
  394. if (event & UV_RENAME) {
  395. properties.append(kRENAME_PROPERTY_VALUE);
  396. }
  397. if (event & UV_CHANGE) {
  398. properties.append(kCHANGE_PROPERTY_VALUE);
  399. }
  400. obj[kPROPERTIES_KEY] = properties;
  401. SendSignal(kFILE_CHANGE_SIGNAL, obj);
  402. }
  403. const cmServerResponse cmServerProtocol1::Process(
  404. const cmServerRequest& request)
  405. {
  406. assert(this->m_State >= STATE_ACTIVE);
  407. if (request.Type == kCACHE_TYPE) {
  408. return this->ProcessCache(request);
  409. }
  410. if (request.Type == kCMAKE_INPUTS_TYPE) {
  411. return this->ProcessCMakeInputs(request);
  412. }
  413. if (request.Type == kCODE_MODEL_TYPE) {
  414. return this->ProcessCodeModel(request);
  415. }
  416. if (request.Type == kCOMPUTE_TYPE) {
  417. return this->ProcessCompute(request);
  418. }
  419. if (request.Type == kCONFIGURE_TYPE) {
  420. return this->ProcessConfigure(request);
  421. }
  422. if (request.Type == kFILESYSTEM_WATCHERS_TYPE) {
  423. return this->ProcessFileSystemWatchers(request);
  424. }
  425. if (request.Type == kGLOBAL_SETTINGS_TYPE) {
  426. return this->ProcessGlobalSettings(request);
  427. }
  428. if (request.Type == kSET_GLOBAL_SETTINGS_TYPE) {
  429. return this->ProcessSetGlobalSettings(request);
  430. }
  431. if (request.Type == kCTEST_INFO_TYPE) {
  432. return this->ProcessCTests(request);
  433. }
  434. return request.ReportError("Unknown command!");
  435. }
  436. bool cmServerProtocol1::IsExperimental() const
  437. {
  438. return true;
  439. }
  440. cmServerResponse cmServerProtocol1::ProcessCache(
  441. const cmServerRequest& request)
  442. {
  443. cmState* state = this->CMakeInstance()->GetState();
  444. Json::Value result = Json::objectValue;
  445. std::vector<std::string> allKeys = state->GetCacheEntryKeys();
  446. Json::Value list = Json::arrayValue;
  447. std::vector<std::string> keys = toStringList(request.Data[kKEYS_KEY]);
  448. if (keys.empty()) {
  449. keys = allKeys;
  450. } else {
  451. for (auto const& i : keys) {
  452. if (std::find(allKeys.begin(), allKeys.end(), i) == allKeys.end()) {
  453. return request.ReportError("Key \"" + i + "\" not found in cache.");
  454. }
  455. }
  456. }
  457. std::sort(keys.begin(), keys.end());
  458. for (auto const& key : keys) {
  459. Json::Value entry = Json::objectValue;
  460. entry[kKEY_KEY] = key;
  461. entry[kTYPE_KEY] =
  462. cmState::CacheEntryTypeToString(state->GetCacheEntryType(key));
  463. entry[kVALUE_KEY] = state->GetCacheEntryValue(key);
  464. Json::Value props = Json::objectValue;
  465. bool haveProperties = false;
  466. for (auto const& prop : state->GetCacheEntryPropertyList(key)) {
  467. haveProperties = true;
  468. props[prop] = state->GetCacheEntryProperty(key, prop);
  469. }
  470. if (haveProperties) {
  471. entry[kPROPERTIES_KEY] = props;
  472. }
  473. list.append(entry);
  474. }
  475. result[kCACHE_KEY] = list;
  476. return request.Reply(result);
  477. }
  478. cmServerResponse cmServerProtocol1::ProcessCMakeInputs(
  479. const cmServerRequest& request)
  480. {
  481. if (this->m_State < STATE_CONFIGURED) {
  482. return request.ReportError("This instance was not yet configured.");
  483. }
  484. const cmake* cm = this->CMakeInstance();
  485. const cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  486. const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot();
  487. const std::string buildDir = cm->GetHomeOutputDirectory();
  488. const std::string sourceDir = cm->GetHomeDirectory();
  489. Json::Value result = Json::objectValue;
  490. result[kSOURCE_DIRECTORY_KEY] = sourceDir;
  491. result[kCMAKE_ROOT_DIRECTORY_KEY] = cmakeRootDir;
  492. std::vector<std::string> internalFiles;
  493. std::vector<std::string> explicitFiles;
  494. std::vector<std::string> tmpFiles;
  495. getCMakeInputs(gg, sourceDir, buildDir, &internalFiles, &explicitFiles,
  496. &tmpFiles);
  497. Json::Value array = Json::arrayValue;
  498. Json::Value tmp = Json::objectValue;
  499. tmp[kIS_CMAKE_KEY] = true;
  500. tmp[kIS_TEMPORARY_KEY] = false;
  501. tmp[kSOURCES_KEY] = fromStringList(internalFiles);
  502. array.append(tmp);
  503. tmp = Json::objectValue;
  504. tmp[kIS_CMAKE_KEY] = false;
  505. tmp[kIS_TEMPORARY_KEY] = false;
  506. tmp[kSOURCES_KEY] = fromStringList(explicitFiles);
  507. array.append(tmp);
  508. tmp = Json::objectValue;
  509. tmp[kIS_CMAKE_KEY] = false;
  510. tmp[kIS_TEMPORARY_KEY] = true;
  511. tmp[kSOURCES_KEY] = fromStringList(tmpFiles);
  512. array.append(tmp);
  513. result[kBUILD_FILES_KEY] = array;
  514. return request.Reply(result);
  515. }
  516. class LanguageData
  517. {
  518. public:
  519. bool operator==(const LanguageData& other) const;
  520. void SetDefines(const std::set<std::string>& defines);
  521. bool IsGenerated = false;
  522. std::string Language;
  523. std::string Flags;
  524. std::vector<std::string> Defines;
  525. std::vector<std::pair<std::string, bool>> IncludePathList;
  526. };
  527. bool LanguageData::operator==(const LanguageData& other) const
  528. {
  529. return Language == other.Language && Defines == other.Defines &&
  530. Flags == other.Flags && IncludePathList == other.IncludePathList &&
  531. IsGenerated == other.IsGenerated;
  532. }
  533. void LanguageData::SetDefines(const std::set<std::string>& defines)
  534. {
  535. std::vector<std::string> result;
  536. result.reserve(defines.size());
  537. for (std::string const& i : defines) {
  538. result.push_back(i);
  539. }
  540. std::sort(result.begin(), result.end());
  541. Defines = std::move(result);
  542. }
  543. namespace std {
  544. template <>
  545. struct hash<LanguageData>
  546. {
  547. std::size_t operator()(const LanguageData& in) const
  548. {
  549. using std::hash;
  550. size_t result =
  551. hash<std::string>()(in.Language) ^ hash<std::string>()(in.Flags);
  552. for (auto const& i : in.IncludePathList) {
  553. result = result ^ (hash<std::string>()(i.first) ^
  554. (i.second ? std::numeric_limits<size_t>::max() : 0));
  555. }
  556. for (auto const& i : in.Defines) {
  557. result = result ^ hash<std::string>()(i);
  558. }
  559. result =
  560. result ^ (in.IsGenerated ? std::numeric_limits<size_t>::max() : 0);
  561. return result;
  562. }
  563. };
  564. } // namespace std
  565. static Json::Value DumpSourceFileGroup(const LanguageData& data,
  566. const std::vector<std::string>& files,
  567. const std::string& baseDir)
  568. {
  569. Json::Value result = Json::objectValue;
  570. if (!data.Language.empty()) {
  571. result[kLANGUAGE_KEY] = data.Language;
  572. if (!data.Flags.empty()) {
  573. result[kCOMPILE_FLAGS_KEY] = data.Flags;
  574. }
  575. if (!data.IncludePathList.empty()) {
  576. Json::Value includes = Json::arrayValue;
  577. for (auto const& i : data.IncludePathList) {
  578. Json::Value tmp = Json::objectValue;
  579. tmp[kPATH_KEY] = i.first;
  580. if (i.second) {
  581. tmp[kIS_SYSTEM_KEY] = i.second;
  582. }
  583. includes.append(tmp);
  584. }
  585. result[kINCLUDE_PATH_KEY] = includes;
  586. }
  587. if (!data.Defines.empty()) {
  588. result[kDEFINES_KEY] = fromStringList(data.Defines);
  589. }
  590. }
  591. result[kIS_GENERATED_KEY] = data.IsGenerated;
  592. Json::Value sourcesValue = Json::arrayValue;
  593. for (auto const& i : files) {
  594. const std::string relPath =
  595. cmSystemTools::RelativePath(baseDir.c_str(), i.c_str());
  596. sourcesValue.append(relPath.size() < i.size() ? relPath : i);
  597. }
  598. result[kSOURCES_KEY] = sourcesValue;
  599. return result;
  600. }
  601. static Json::Value DumpSourceFilesList(
  602. cmGeneratorTarget* target, const std::string& config,
  603. const std::map<std::string, LanguageData>& languageDataMap)
  604. {
  605. // Collect sourcefile groups:
  606. std::vector<cmSourceFile*> files;
  607. target->GetSourceFiles(files, config);
  608. std::unordered_map<LanguageData, std::vector<std::string>> fileGroups;
  609. for (cmSourceFile* file : files) {
  610. LanguageData fileData;
  611. fileData.Language = file->GetLanguage();
  612. if (!fileData.Language.empty()) {
  613. const LanguageData& ld = languageDataMap.at(fileData.Language);
  614. cmLocalGenerator* lg = target->GetLocalGenerator();
  615. std::string compileFlags = ld.Flags;
  616. if (const char* cflags = file->GetProperty("COMPILE_FLAGS")) {
  617. cmGeneratorExpression ge;
  618. auto cge = ge.Parse(cflags);
  619. const char* processed =
  620. cge->Evaluate(target->GetLocalGenerator(), config);
  621. lg->AppendFlags(compileFlags, processed);
  622. }
  623. fileData.Flags = compileFlags;
  624. fileData.IncludePathList = ld.IncludePathList;
  625. std::set<std::string> defines;
  626. lg->AppendDefines(defines, file->GetProperty("COMPILE_DEFINITIONS"));
  627. const std::string defPropName =
  628. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  629. lg->AppendDefines(defines, file->GetProperty(defPropName));
  630. defines.insert(ld.Defines.begin(), ld.Defines.end());
  631. fileData.SetDefines(defines);
  632. }
  633. fileData.IsGenerated = file->GetPropertyAsBool("GENERATED");
  634. std::vector<std::string>& groupFileList = fileGroups[fileData];
  635. groupFileList.push_back(file->GetFullPath());
  636. }
  637. const std::string baseDir = target->Makefile->GetCurrentSourceDirectory();
  638. Json::Value result = Json::arrayValue;
  639. for (auto const& it : fileGroups) {
  640. Json::Value group = DumpSourceFileGroup(it.first, it.second, baseDir);
  641. if (!group.isNull()) {
  642. result.append(group);
  643. }
  644. }
  645. return result;
  646. }
  647. static Json::Value DumpBacktrace(const cmListFileBacktrace& backtrace)
  648. {
  649. Json::Value result = Json::arrayValue;
  650. cmListFileBacktrace backtraceCopy = backtrace;
  651. while (!backtraceCopy.Top().FilePath.empty()) {
  652. Json::Value entry = Json::objectValue;
  653. entry[kPATH_KEY] = backtraceCopy.Top().FilePath;
  654. if (backtraceCopy.Top().Line) {
  655. entry[kLINE_NUMBER_KEY] = static_cast<int>(backtraceCopy.Top().Line);
  656. }
  657. if (!backtraceCopy.Top().Name.empty()) {
  658. entry[kNAME_KEY] = backtraceCopy.Top().Name;
  659. }
  660. result.append(entry);
  661. backtraceCopy = backtraceCopy.Pop();
  662. }
  663. return result;
  664. }
  665. static void DumpBacktraceRange(Json::Value& result, const std::string& type,
  666. cmBacktraceRange range)
  667. {
  668. for (auto const& bt : range) {
  669. Json::Value obj = Json::objectValue;
  670. obj[kTYPE_KEY] = type;
  671. obj[kBACKTRACE_KEY] = DumpBacktrace(bt);
  672. result.append(obj);
  673. }
  674. }
  675. static Json::Value DumpCTestInfo(cmTest* testInfo)
  676. {
  677. Json::Value result = Json::objectValue;
  678. result[kCTEST_NAME] = testInfo->GetName();
  679. // Concat command entries together. After the first should be the arguments
  680. // for the command
  681. std::string command;
  682. for (auto const& cmd : testInfo->GetCommand()) {
  683. command.append(cmd);
  684. command.append(" ");
  685. }
  686. result[kCTEST_COMMAND] = command;
  687. // Build up the list of properties that may have been specified
  688. Json::Value properties = Json::arrayValue;
  689. for (auto& prop : testInfo->GetProperties()) {
  690. Json::Value entry = Json::objectValue;
  691. entry[kKEY_KEY] = prop.first;
  692. entry[kVALUE_KEY] = prop.second.GetValue();
  693. properties.append(entry);
  694. }
  695. result[kPROPERTIES_KEY] = properties;
  696. // Need backtrace to figure out where this test was originally added
  697. result[kBACKTRACE_KEY] = DumpBacktrace(testInfo->GetBacktrace());
  698. return result;
  699. }
  700. static void DumpMakefileTests(cmMakefile* mf, const std::string& config,
  701. Json::Value* result)
  702. {
  703. std::vector<cmTest*> tests;
  704. mf->GetTests(config, tests);
  705. for (auto test : tests) {
  706. Json::Value tmp = DumpCTestInfo(test);
  707. if (!tmp.isNull()) {
  708. result->append(tmp);
  709. }
  710. }
  711. }
  712. static Json::Value DumpCTestProjectList(const cmake* cm,
  713. std::string const& config)
  714. {
  715. Json::Value result = Json::arrayValue;
  716. auto globalGen = cm->GetGlobalGenerator();
  717. for (const auto& projectIt : globalGen->GetProjectMap()) {
  718. Json::Value pObj = Json::objectValue;
  719. pObj[kNAME_KEY] = projectIt.first;
  720. Json::Value tests = Json::arrayValue;
  721. // Gather tests for every generator
  722. for (const auto& lg : projectIt.second) {
  723. // Make sure they're generated.
  724. lg->GenerateTestFiles();
  725. cmMakefile* mf = lg->GetMakefile();
  726. DumpMakefileTests(mf, config, &tests);
  727. }
  728. pObj[kCTEST_INFO] = tests;
  729. result.append(pObj);
  730. }
  731. return result;
  732. }
  733. static Json::Value DumpCTestConfiguration(const cmake* cm,
  734. const std::string& config)
  735. {
  736. Json::Value result = Json::objectValue;
  737. result[kNAME_KEY] = config;
  738. result[kPROJECTS_KEY] = DumpCTestProjectList(cm, config);
  739. return result;
  740. }
  741. static Json::Value DumpCTestConfigurationsList(const cmake* cm)
  742. {
  743. Json::Value result = Json::arrayValue;
  744. for (const std::string& c : getConfigurations(cm)) {
  745. result.append(DumpCTestConfiguration(cm, c));
  746. }
  747. return result;
  748. }
  749. static Json::Value DumpTarget(cmGeneratorTarget* target,
  750. const std::string& config)
  751. {
  752. cmLocalGenerator* lg = target->GetLocalGenerator();
  753. const cmState* state = lg->GetState();
  754. const cmStateEnums::TargetType type = target->GetType();
  755. const std::string typeName = state->GetTargetTypeName(type);
  756. Json::Value ttl = Json::arrayValue;
  757. ttl.append("EXECUTABLE");
  758. ttl.append("STATIC_LIBRARY");
  759. ttl.append("SHARED_LIBRARY");
  760. ttl.append("MODULE_LIBRARY");
  761. ttl.append("OBJECT_LIBRARY");
  762. ttl.append("UTILITY");
  763. ttl.append("INTERFACE_LIBRARY");
  764. if (!hasString(ttl, typeName) || target->IsImported()) {
  765. return Json::Value();
  766. }
  767. Json::Value result = Json::objectValue;
  768. result[kNAME_KEY] = target->GetName();
  769. result[kIS_GENERATOR_PROVIDED_KEY] =
  770. target->Target->GetIsGeneratorProvided();
  771. result[kTYPE_KEY] = typeName;
  772. result[kSOURCE_DIRECTORY_KEY] = lg->GetCurrentSourceDirectory();
  773. result[kBUILD_DIRECTORY_KEY] = lg->GetCurrentBinaryDirectory();
  774. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  775. return result;
  776. }
  777. result[kFULL_NAME_KEY] = target->GetFullName(config);
  778. if (target->Target->GetHaveInstallRule()) {
  779. result[kHAS_INSTALL_RULE] = true;
  780. Json::Value installPaths = Json::arrayValue;
  781. auto targetGenerators = target->Makefile->GetInstallGenerators();
  782. for (auto installGenerator : targetGenerators) {
  783. auto installTargetGenerator =
  784. dynamic_cast<cmInstallTargetGenerator*>(installGenerator);
  785. if (installTargetGenerator != nullptr &&
  786. installTargetGenerator->GetTarget()->Target == target->Target) {
  787. auto dest = installTargetGenerator->GetDestination(config);
  788. std::string installPath;
  789. if (!dest.empty() && cmSystemTools::FileIsFullPath(dest.c_str())) {
  790. installPath = dest;
  791. } else {
  792. std::string installPrefix =
  793. target->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  794. installPath = installPrefix + '/' + dest;
  795. }
  796. installPaths.append(installPath);
  797. }
  798. }
  799. result[kINSTALL_PATHS] = installPaths;
  800. }
  801. Json::Value crossRefs = Json::objectValue;
  802. crossRefs[kBACKTRACE_KEY] = DumpBacktrace(target->Target->GetBacktrace());
  803. Json::Value statements = Json::arrayValue;
  804. DumpBacktraceRange(statements, "target_compile_definitions",
  805. target->Target->GetCompileDefinitionsBacktraces());
  806. DumpBacktraceRange(statements, "target_include_directories",
  807. target->Target->GetIncludeDirectoriesBacktraces());
  808. DumpBacktraceRange(statements, "target_compile_options",
  809. target->Target->GetCompileOptionsBacktraces());
  810. DumpBacktraceRange(statements, "target_link_libraries",
  811. target->Target->GetLinkImplementationBacktraces());
  812. crossRefs[kRELATED_STATEMENTS_KEY] = std::move(statements);
  813. result[kTARGET_CROSS_REFERENCES_KEY] = std::move(crossRefs);
  814. if (target->HaveWellDefinedOutputFiles()) {
  815. Json::Value artifacts = Json::arrayValue;
  816. artifacts.append(
  817. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact));
  818. if (target->IsDLLPlatform()) {
  819. artifacts.append(
  820. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact));
  821. const cmGeneratorTarget::OutputInfo* output =
  822. target->GetOutputInfo(config);
  823. if (output && !output->PdbDir.empty()) {
  824. artifacts.append(output->PdbDir + '/' + target->GetPDBName(config));
  825. }
  826. }
  827. result[kARTIFACTS_KEY] = artifacts;
  828. result[kLINKER_LANGUAGE_KEY] = target->GetLinkerLanguage(config);
  829. std::string linkLibs;
  830. std::string linkFlags;
  831. std::string linkLanguageFlags;
  832. std::string frameworkPath;
  833. std::string linkPath;
  834. cmLinkLineComputer linkLineComputer(lg,
  835. lg->GetStateSnapshot().GetDirectory());
  836. lg->GetTargetFlags(&linkLineComputer, config, linkLibs, linkLanguageFlags,
  837. linkFlags, frameworkPath, linkPath, target);
  838. linkLibs = cmSystemTools::TrimWhitespace(linkLibs);
  839. linkFlags = cmSystemTools::TrimWhitespace(linkFlags);
  840. linkLanguageFlags = cmSystemTools::TrimWhitespace(linkLanguageFlags);
  841. frameworkPath = cmSystemTools::TrimWhitespace(frameworkPath);
  842. linkPath = cmSystemTools::TrimWhitespace(linkPath);
  843. if (!cmSystemTools::TrimWhitespace(linkLibs).empty()) {
  844. result[kLINK_LIBRARIES_KEY] = linkLibs;
  845. }
  846. if (!cmSystemTools::TrimWhitespace(linkFlags).empty()) {
  847. result[kLINK_FLAGS_KEY] = linkFlags;
  848. }
  849. if (!cmSystemTools::TrimWhitespace(linkLanguageFlags).empty()) {
  850. result[kLINK_LANGUAGE_FLAGS_KEY] = linkLanguageFlags;
  851. }
  852. if (!frameworkPath.empty()) {
  853. result[kFRAMEWORK_PATH_KEY] = frameworkPath;
  854. }
  855. if (!linkPath.empty()) {
  856. result[kLINK_PATH_KEY] = linkPath;
  857. }
  858. const std::string sysroot =
  859. lg->GetMakefile()->GetSafeDefinition("CMAKE_SYSROOT");
  860. if (!sysroot.empty()) {
  861. result[kSYSROOT_KEY] = sysroot;
  862. }
  863. }
  864. std::set<std::string> languages;
  865. target->GetLanguages(languages, config);
  866. std::map<std::string, LanguageData> languageDataMap;
  867. for (std::string const& lang : languages) {
  868. LanguageData& ld = languageDataMap[lang];
  869. ld.Language = lang;
  870. lg->GetTargetCompileFlags(target, config, lang, ld.Flags);
  871. std::set<std::string> defines;
  872. lg->GetTargetDefines(target, config, lang, defines);
  873. ld.SetDefines(defines);
  874. std::vector<std::string> includePathList;
  875. lg->GetIncludeDirectories(includePathList, target, lang, config, true);
  876. for (std::string const& i : includePathList) {
  877. ld.IncludePathList.push_back(
  878. std::make_pair(i, target->IsSystemIncludeDirectory(i, config)));
  879. }
  880. }
  881. Json::Value sourceGroupsValue =
  882. DumpSourceFilesList(target, config, languageDataMap);
  883. if (!sourceGroupsValue.empty()) {
  884. result[kFILE_GROUPS_KEY] = sourceGroupsValue;
  885. }
  886. return result;
  887. }
  888. static Json::Value DumpTargetsList(
  889. const std::vector<cmLocalGenerator*>& generators, const std::string& config)
  890. {
  891. Json::Value result = Json::arrayValue;
  892. std::vector<cmGeneratorTarget*> targetList;
  893. for (auto const& lgIt : generators) {
  894. const auto& list = lgIt->GetGeneratorTargets();
  895. targetList.insert(targetList.end(), list.begin(), list.end());
  896. }
  897. std::sort(targetList.begin(), targetList.end());
  898. for (cmGeneratorTarget* target : targetList) {
  899. Json::Value tmp = DumpTarget(target, config);
  900. if (!tmp.isNull()) {
  901. result.append(tmp);
  902. }
  903. }
  904. return result;
  905. }
  906. static Json::Value DumpProjectList(const cmake* cm, std::string const& config)
  907. {
  908. Json::Value result = Json::arrayValue;
  909. auto globalGen = cm->GetGlobalGenerator();
  910. for (auto const& projectIt : globalGen->GetProjectMap()) {
  911. Json::Value pObj = Json::objectValue;
  912. pObj[kNAME_KEY] = projectIt.first;
  913. // All Projects must have at least one local generator
  914. assert(!projectIt.second.empty());
  915. const cmLocalGenerator* lg = projectIt.second.at(0);
  916. // Project structure information:
  917. const cmMakefile* mf = lg->GetMakefile();
  918. pObj[kMINIMUM_CMAKE_VERSION] =
  919. mf->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  920. pObj[kSOURCE_DIRECTORY_KEY] = mf->GetCurrentSourceDirectory();
  921. pObj[kBUILD_DIRECTORY_KEY] = mf->GetCurrentBinaryDirectory();
  922. pObj[kTARGETS_KEY] = DumpTargetsList(projectIt.second, config);
  923. // For a project-level install rule it might be defined in any of its
  924. // associated generators.
  925. bool hasInstallRule = false;
  926. for (const auto generator : projectIt.second) {
  927. hasInstallRule =
  928. generator->GetMakefile()->GetInstallGenerators().empty() == false;
  929. if (hasInstallRule) {
  930. break;
  931. }
  932. }
  933. pObj[kHAS_INSTALL_RULE] = hasInstallRule;
  934. result.append(pObj);
  935. }
  936. return result;
  937. }
  938. static Json::Value DumpConfiguration(const cmake* cm,
  939. const std::string& config)
  940. {
  941. Json::Value result = Json::objectValue;
  942. result[kNAME_KEY] = config;
  943. result[kPROJECTS_KEY] = DumpProjectList(cm, config);
  944. return result;
  945. }
  946. static Json::Value DumpConfigurationsList(const cmake* cm)
  947. {
  948. Json::Value result = Json::arrayValue;
  949. for (std::string const& c : getConfigurations(cm)) {
  950. result.append(DumpConfiguration(cm, c));
  951. }
  952. return result;
  953. }
  954. cmServerResponse cmServerProtocol1::ProcessCodeModel(
  955. const cmServerRequest& request)
  956. {
  957. if (this->m_State != STATE_COMPUTED) {
  958. return request.ReportError("No build system was generated yet.");
  959. }
  960. Json::Value result = Json::objectValue;
  961. result[kCONFIGURATIONS_KEY] = DumpConfigurationsList(this->CMakeInstance());
  962. return request.Reply(result);
  963. }
  964. cmServerResponse cmServerProtocol1::ProcessCompute(
  965. const cmServerRequest& request)
  966. {
  967. if (this->m_State > STATE_CONFIGURED) {
  968. return request.ReportError("This build system was already generated.");
  969. }
  970. if (this->m_State < STATE_CONFIGURED) {
  971. return request.ReportError("This project was not configured yet.");
  972. }
  973. cmake* cm = this->CMakeInstance();
  974. int ret = cm->Generate();
  975. if (ret < 0) {
  976. return request.ReportError("Failed to compute build system.");
  977. }
  978. m_State = STATE_COMPUTED;
  979. return request.Reply(Json::Value());
  980. }
  981. cmServerResponse cmServerProtocol1::ProcessConfigure(
  982. const cmServerRequest& request)
  983. {
  984. if (this->m_State == STATE_INACTIVE) {
  985. return request.ReportError("This instance is inactive.");
  986. }
  987. FileMonitor()->StopMonitoring();
  988. std::string errorMessage;
  989. cmake* cm = this->CMakeInstance();
  990. this->GeneratorInfo.SetupGenerator(cm, &errorMessage);
  991. if (!errorMessage.empty()) {
  992. return request.ReportError(errorMessage);
  993. }
  994. // Make sure the types of cacheArguments matches (if given):
  995. std::vector<std::string> cacheArgs = { "unused" };
  996. bool cacheArgumentsError = false;
  997. const Json::Value passedArgs = request.Data[kCACHE_ARGUMENTS_KEY];
  998. if (!passedArgs.isNull()) {
  999. if (passedArgs.isString()) {
  1000. cacheArgs.push_back(passedArgs.asString());
  1001. } else if (passedArgs.isArray()) {
  1002. for (auto const& arg : passedArgs) {
  1003. if (!arg.isString()) {
  1004. cacheArgumentsError = true;
  1005. break;
  1006. }
  1007. cacheArgs.push_back(arg.asString());
  1008. }
  1009. } else {
  1010. cacheArgumentsError = true;
  1011. }
  1012. }
  1013. if (cacheArgumentsError) {
  1014. request.ReportError(
  1015. "cacheArguments must be unset, a string or an array of strings.");
  1016. }
  1017. std::string sourceDir = cm->GetHomeDirectory();
  1018. const std::string buildDir = cm->GetHomeOutputDirectory();
  1019. cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  1020. if (buildDir.empty()) {
  1021. return request.ReportError("No build directory set via Handshake.");
  1022. }
  1023. if (cm->LoadCache(buildDir)) {
  1024. // build directory has been set up before
  1025. const char* cachedSourceDir =
  1026. cm->GetState()->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
  1027. if (!cachedSourceDir) {
  1028. return request.ReportError("No CMAKE_HOME_DIRECTORY found in cache.");
  1029. }
  1030. if (sourceDir.empty()) {
  1031. sourceDir = std::string(cachedSourceDir);
  1032. cm->SetHomeDirectory(sourceDir);
  1033. }
  1034. const char* cachedGenerator =
  1035. cm->GetState()->GetInitializedCacheValue("CMAKE_GENERATOR");
  1036. if (cachedGenerator) {
  1037. if (gg && gg->GetName() != cachedGenerator) {
  1038. return request.ReportError("Configured generator does not match with "
  1039. "CMAKE_GENERATOR found in cache.");
  1040. }
  1041. }
  1042. } else {
  1043. // build directory has not been set up before
  1044. if (sourceDir.empty()) {
  1045. return request.ReportError("No sourceDirectory set via "
  1046. "setGlobalSettings and no cache found in "
  1047. "buildDirectory.");
  1048. }
  1049. }
  1050. cmSystemTools::ResetErrorOccuredFlag(); // Reset error state
  1051. if (cm->AddCMakePaths() != 1) {
  1052. return request.ReportError("Failed to set CMake paths.");
  1053. }
  1054. if (!cm->SetCacheArgs(cacheArgs)) {
  1055. return request.ReportError("cacheArguments could not be set.");
  1056. }
  1057. int ret = cm->Configure();
  1058. if (ret < 0) {
  1059. return request.ReportError("Configuration failed.");
  1060. }
  1061. std::vector<std::string> toWatchList;
  1062. getCMakeInputs(gg, std::string(), buildDir, nullptr, &toWatchList, nullptr);
  1063. FileMonitor()->MonitorPaths(toWatchList,
  1064. [this](const std::string& p, int e, int s) {
  1065. this->HandleCMakeFileChanges(p, e, s);
  1066. });
  1067. m_State = STATE_CONFIGURED;
  1068. m_isDirty = false;
  1069. return request.Reply(Json::Value());
  1070. }
  1071. cmServerResponse cmServerProtocol1::ProcessGlobalSettings(
  1072. const cmServerRequest& request)
  1073. {
  1074. cmake* cm = this->CMakeInstance();
  1075. Json::Value obj = Json::objectValue;
  1076. // Capabilities information:
  1077. obj[kCAPABILITIES_KEY] = cm->ReportCapabilitiesJson(true);
  1078. obj[kDEBUG_OUTPUT_KEY] = cm->GetDebugOutput();
  1079. obj[kTRACE_KEY] = cm->GetTrace();
  1080. obj[kTRACE_EXPAND_KEY] = cm->GetTraceExpand();
  1081. obj[kWARN_UNINITIALIZED_KEY] = cm->GetWarnUninitialized();
  1082. obj[kWARN_UNUSED_KEY] = cm->GetWarnUnused();
  1083. obj[kWARN_UNUSED_CLI_KEY] = cm->GetWarnUnusedCli();
  1084. obj[kCHECK_SYSTEM_VARS_KEY] = cm->GetCheckSystemVars();
  1085. obj[kSOURCE_DIRECTORY_KEY] = this->GeneratorInfo.SourceDirectory;
  1086. obj[kBUILD_DIRECTORY_KEY] = this->GeneratorInfo.BuildDirectory;
  1087. // Currently used generator:
  1088. obj[kGENERATOR_KEY] = this->GeneratorInfo.GeneratorName;
  1089. obj[kEXTRA_GENERATOR_KEY] = this->GeneratorInfo.ExtraGeneratorName;
  1090. return request.Reply(obj);
  1091. }
  1092. static void setBool(const cmServerRequest& request, const std::string& key,
  1093. std::function<void(bool)> const& setter)
  1094. {
  1095. if (request.Data[key].isNull()) {
  1096. return;
  1097. }
  1098. setter(request.Data[key].asBool());
  1099. }
  1100. cmServerResponse cmServerProtocol1::ProcessSetGlobalSettings(
  1101. const cmServerRequest& request)
  1102. {
  1103. const std::vector<std::string> boolValues = {
  1104. kDEBUG_OUTPUT_KEY, kTRACE_KEY, kTRACE_EXPAND_KEY,
  1105. kWARN_UNINITIALIZED_KEY, kWARN_UNUSED_KEY, kWARN_UNUSED_CLI_KEY,
  1106. kCHECK_SYSTEM_VARS_KEY
  1107. };
  1108. for (std::string const& i : boolValues) {
  1109. if (!request.Data[i].isNull() && !request.Data[i].isBool()) {
  1110. return request.ReportError("\"" + i +
  1111. "\" must be unset or a bool value.");
  1112. }
  1113. }
  1114. cmake* cm = this->CMakeInstance();
  1115. setBool(request, kDEBUG_OUTPUT_KEY,
  1116. [cm](bool e) { cm->SetDebugOutputOn(e); });
  1117. setBool(request, kTRACE_KEY, [cm](bool e) { cm->SetTrace(e); });
  1118. setBool(request, kTRACE_EXPAND_KEY, [cm](bool e) { cm->SetTraceExpand(e); });
  1119. setBool(request, kWARN_UNINITIALIZED_KEY,
  1120. [cm](bool e) { cm->SetWarnUninitialized(e); });
  1121. setBool(request, kWARN_UNUSED_KEY, [cm](bool e) { cm->SetWarnUnused(e); });
  1122. setBool(request, kWARN_UNUSED_CLI_KEY,
  1123. [cm](bool e) { cm->SetWarnUnusedCli(e); });
  1124. setBool(request, kCHECK_SYSTEM_VARS_KEY,
  1125. [cm](bool e) { cm->SetCheckSystemVars(e); });
  1126. return request.Reply(Json::Value());
  1127. }
  1128. cmServerResponse cmServerProtocol1::ProcessFileSystemWatchers(
  1129. const cmServerRequest& request)
  1130. {
  1131. const cmFileMonitor* const fm = FileMonitor();
  1132. Json::Value result = Json::objectValue;
  1133. Json::Value files = Json::arrayValue;
  1134. for (auto const& f : fm->WatchedFiles()) {
  1135. files.append(f);
  1136. }
  1137. Json::Value directories = Json::arrayValue;
  1138. for (auto const& d : fm->WatchedDirectories()) {
  1139. directories.append(d);
  1140. }
  1141. result[kWATCHED_FILES_KEY] = files;
  1142. result[kWATCHED_DIRECTORIES_KEY] = directories;
  1143. return request.Reply(result);
  1144. }
  1145. cmServerResponse cmServerProtocol1::ProcessCTests(
  1146. const cmServerRequest& request)
  1147. {
  1148. if (this->m_State < STATE_COMPUTED) {
  1149. return request.ReportError("This instance was not yet computed.");
  1150. }
  1151. Json::Value result = Json::objectValue;
  1152. result[kCONFIGURATIONS_KEY] =
  1153. DumpCTestConfigurationsList(this->CMakeInstance());
  1154. return request.Reply(result);
  1155. }
  1156. cmServerProtocol1::GeneratorInformation::GeneratorInformation(
  1157. const std::string& generatorName, const std::string& extraGeneratorName,
  1158. const std::string& toolset, const std::string& platform,
  1159. const std::string& sourceDirectory, const std::string& buildDirectory)
  1160. : GeneratorName(generatorName)
  1161. , ExtraGeneratorName(extraGeneratorName)
  1162. , Toolset(toolset)
  1163. , Platform(platform)
  1164. , SourceDirectory(sourceDirectory)
  1165. , BuildDirectory(buildDirectory)
  1166. {
  1167. }
  1168. void cmServerProtocol1::GeneratorInformation::SetupGenerator(
  1169. cmake* cm, std::string* errorMessage)
  1170. {
  1171. const std::string fullGeneratorName =
  1172. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  1173. GeneratorName, ExtraGeneratorName);
  1174. cm->SetHomeDirectory(SourceDirectory);
  1175. cm->SetHomeOutputDirectory(BuildDirectory);
  1176. cmGlobalGenerator* gg = cm->CreateGlobalGenerator(fullGeneratorName);
  1177. if (!gg) {
  1178. setErrorMessage(
  1179. errorMessage,
  1180. std::string("Could not set up the requested combination of \"") +
  1181. kGENERATOR_KEY + "\" and \"" + kEXTRA_GENERATOR_KEY + "\"");
  1182. return;
  1183. }
  1184. cm->SetGlobalGenerator(gg);
  1185. cm->SetGeneratorToolset(Toolset);
  1186. cm->SetGeneratorPlatform(Platform);
  1187. }