cmServerProtocol.cxx 42 KB

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