cmServerProtocol.cxx 40 KB

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