cmServerProtocol.cxx 43 KB

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