cmServerProtocol.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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 "cmCacheManager.h"
  5. #include "cmExternalMakefileProjectGenerator.h"
  6. #include "cmFileMonitor.h"
  7. #include "cmGeneratorTarget.h"
  8. #include "cmGlobalGenerator.h"
  9. #include "cmLinkLineComputer.h"
  10. #include "cmListFileCache.h"
  11. #include "cmLocalGenerator.h"
  12. #include "cmMakefile.h"
  13. #include "cmServer.h"
  14. #include "cmServerDictionary.h"
  15. #include "cmSourceFile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmake.h"
  18. #include "cmServerDictionary.h"
  19. #if defined(CMAKE_BUILD_WITH_CMAKE)
  20. #include "cm_jsoncpp_reader.h"
  21. #include "cm_jsoncpp_value.h"
  22. #endif
  23. #include <algorithm>
  24. #include <string>
  25. #include <vector>
  26. // Get rid of some windows macros:
  27. #undef max
  28. namespace {
  29. static std::vector<std::string> getConfigurations(const cmake* cm)
  30. {
  31. std::vector<std::string> configurations;
  32. auto makefiles = cm->GetGlobalGenerator()->GetMakefiles();
  33. if (makefiles.empty()) {
  34. return configurations;
  35. }
  36. makefiles[0]->GetConfigurations(configurations);
  37. if (configurations.empty())
  38. configurations.push_back("");
  39. return configurations;
  40. }
  41. static bool hasString(const Json::Value& v, const std::string& s)
  42. {
  43. return !v.isNull() &&
  44. std::find_if(v.begin(), v.end(), [s](const Json::Value& i) {
  45. return i.asString() == s;
  46. }) != v.end();
  47. }
  48. template <class T>
  49. static Json::Value fromStringList(const T& in)
  50. {
  51. Json::Value result = Json::arrayValue;
  52. for (const std::string& i : in) {
  53. result.append(i);
  54. }
  55. return result;
  56. }
  57. static std::vector<std::string> toStringList(const Json::Value& in)
  58. {
  59. std::vector<std::string> result;
  60. for (const auto& it : in) {
  61. result.push_back(it.asString());
  62. }
  63. return result;
  64. }
  65. static void getCMakeInputs(const cmGlobalGenerator* gg,
  66. const std::string& sourceDir,
  67. const std::string& buildDir,
  68. std::vector<std::string>* internalFiles,
  69. std::vector<std::string>* explicitFiles,
  70. std::vector<std::string>* tmpFiles)
  71. {
  72. const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot() + '/';
  73. const std::vector<cmMakefile*> makefiles = gg->GetMakefiles();
  74. for (auto it = makefiles.begin(); it != makefiles.end(); ++it) {
  75. const std::vector<std::string> listFiles = (*it)->GetListFiles();
  76. for (auto jt = listFiles.begin(); jt != listFiles.end(); ++jt) {
  77. const std::string startOfFile = jt->substr(0, cmakeRootDir.size());
  78. const bool isInternal = (startOfFile == cmakeRootDir);
  79. const bool isTemporary = !isInternal && (jt->find(buildDir + '/') == 0);
  80. std::string toAdd = *jt;
  81. if (!sourceDir.empty()) {
  82. const std::string& relative =
  83. cmSystemTools::RelativePath(sourceDir.c_str(), jt->c_str());
  84. if (toAdd.size() > relative.size())
  85. toAdd = relative;
  86. }
  87. if (isInternal) {
  88. if (internalFiles)
  89. internalFiles->push_back(toAdd);
  90. } else {
  91. if (isTemporary) {
  92. if (tmpFiles)
  93. tmpFiles->push_back(toAdd);
  94. } else {
  95. if (explicitFiles)
  96. explicitFiles->push_back(toAdd);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. } // namespace
  103. cmServerRequest::cmServerRequest(cmServer* server, const std::string& t,
  104. const std::string& c, const Json::Value& d)
  105. : Type(t)
  106. , Cookie(c)
  107. , Data(d)
  108. , m_Server(server)
  109. {
  110. }
  111. void cmServerRequest::ReportProgress(int min, int current, int max,
  112. const std::string& message) const
  113. {
  114. this->m_Server->WriteProgress(*this, min, current, max, message);
  115. }
  116. void cmServerRequest::ReportMessage(const std::string& message,
  117. const std::string& title) const
  118. {
  119. m_Server->WriteMessage(*this, message, title);
  120. }
  121. cmServerResponse cmServerRequest::Reply(const Json::Value& data) const
  122. {
  123. cmServerResponse response(*this);
  124. response.SetData(data);
  125. return response;
  126. }
  127. cmServerResponse cmServerRequest::ReportError(const std::string& message) const
  128. {
  129. cmServerResponse response(*this);
  130. response.SetError(message);
  131. return response;
  132. }
  133. cmServerResponse::cmServerResponse(const cmServerRequest& request)
  134. : Type(request.Type)
  135. , Cookie(request.Cookie)
  136. {
  137. }
  138. void cmServerResponse::SetData(const Json::Value& data)
  139. {
  140. assert(this->m_Payload == PAYLOAD_UNKNOWN);
  141. if (!data[kCOOKIE_KEY].isNull() || !data[kTYPE_KEY].isNull()) {
  142. this->SetError("Response contains cookie or type field.");
  143. return;
  144. }
  145. this->m_Payload = PAYLOAD_DATA;
  146. this->m_Data = data;
  147. }
  148. void cmServerResponse::SetError(const std::string& message)
  149. {
  150. assert(this->m_Payload == PAYLOAD_UNKNOWN);
  151. this->m_Payload = PAYLOAD_ERROR;
  152. this->m_ErrorMessage = message;
  153. }
  154. bool cmServerResponse::IsComplete() const
  155. {
  156. return this->m_Payload != PAYLOAD_UNKNOWN;
  157. }
  158. bool cmServerResponse::IsError() const
  159. {
  160. assert(this->m_Payload != PAYLOAD_UNKNOWN);
  161. return this->m_Payload == PAYLOAD_ERROR;
  162. }
  163. std::string cmServerResponse::ErrorMessage() const
  164. {
  165. if (this->m_Payload == PAYLOAD_ERROR) {
  166. return this->m_ErrorMessage;
  167. }
  168. return std::string();
  169. }
  170. Json::Value cmServerResponse::Data() const
  171. {
  172. assert(this->m_Payload != PAYLOAD_UNKNOWN);
  173. return this->m_Data;
  174. }
  175. bool cmServerProtocol::Activate(cmServer* server,
  176. const cmServerRequest& request,
  177. std::string* errorMessage)
  178. {
  179. assert(server);
  180. this->m_Server = server;
  181. this->m_CMakeInstance = std::make_unique<cmake>();
  182. const bool result = this->DoActivate(request, errorMessage);
  183. if (!result) {
  184. this->m_CMakeInstance = CM_NULLPTR;
  185. }
  186. return result;
  187. }
  188. cmFileMonitor* cmServerProtocol::FileMonitor() const
  189. {
  190. return this->m_Server ? this->m_Server->FileMonitor() : nullptr;
  191. }
  192. void cmServerProtocol::SendSignal(const std::string& name,
  193. const Json::Value& data) const
  194. {
  195. if (this->m_Server) {
  196. this->m_Server->WriteSignal(name, data);
  197. }
  198. }
  199. cmake* cmServerProtocol::CMakeInstance() const
  200. {
  201. return this->m_CMakeInstance.get();
  202. }
  203. bool cmServerProtocol::DoActivate(const cmServerRequest& /*request*/,
  204. std::string* /*errorMessage*/)
  205. {
  206. return true;
  207. }
  208. std::pair<int, int> cmServerProtocol1_0::ProtocolVersion() const
  209. {
  210. return std::make_pair(1, 0);
  211. }
  212. bool cmServerProtocol1_0::DoActivate(const cmServerRequest& request,
  213. std::string* errorMessage)
  214. {
  215. std::string sourceDirectory = request.Data[kSOURCE_DIRECTORY_KEY].asString();
  216. const std::string buildDirectory =
  217. request.Data[kBUILD_DIRECTORY_KEY].asString();
  218. std::string generator = request.Data[kGENERATOR_KEY].asString();
  219. std::string extraGenerator = request.Data[kEXTRA_GENERATOR_KEY].asString();
  220. if (buildDirectory.empty()) {
  221. if (errorMessage) {
  222. *errorMessage =
  223. std::string("\"") + kBUILD_DIRECTORY_KEY + "\" is missing.";
  224. }
  225. return false;
  226. }
  227. cmake* cm = CMakeInstance();
  228. if (cmSystemTools::PathExists(buildDirectory)) {
  229. if (!cmSystemTools::FileIsDirectory(buildDirectory)) {
  230. if (errorMessage) {
  231. *errorMessage = std::string("\"") + kBUILD_DIRECTORY_KEY +
  232. "\" exists but is not a directory.";
  233. }
  234. return false;
  235. }
  236. const std::string cachePath = cm->FindCacheFile(buildDirectory);
  237. if (cm->LoadCache(cachePath)) {
  238. cmState* state = cm->GetState();
  239. // Check generator:
  240. const std::string cachedGenerator =
  241. std::string(state->GetCacheEntryValue("CMAKE_GENERATOR"));
  242. if (cachedGenerator.empty() && generator.empty()) {
  243. if (errorMessage) {
  244. *errorMessage =
  245. std::string("\"") + kGENERATOR_KEY + "\" is required but unset.";
  246. }
  247. return false;
  248. }
  249. if (generator.empty()) {
  250. generator = cachedGenerator;
  251. }
  252. if (generator != cachedGenerator) {
  253. if (errorMessage) {
  254. *errorMessage = std::string("\"") + kGENERATOR_KEY +
  255. "\" set but incompatible with configured generator.";
  256. }
  257. return false;
  258. }
  259. // check extra generator:
  260. const std::string cachedExtraGenerator =
  261. std::string(state->GetCacheEntryValue("CMAKE_EXTRA_GENERATOR"));
  262. if (!cachedExtraGenerator.empty() && !extraGenerator.empty() &&
  263. cachedExtraGenerator != extraGenerator) {
  264. if (errorMessage) {
  265. *errorMessage = std::string("\"") + kEXTRA_GENERATOR_KEY +
  266. "\" is set but incompatible with configured extra generator.";
  267. }
  268. return false;
  269. }
  270. if (extraGenerator.empty()) {
  271. extraGenerator = cachedExtraGenerator;
  272. }
  273. // check sourcedir:
  274. const std::string cachedSourceDirectory =
  275. std::string(state->GetCacheEntryValue("CMAKE_HOME_DIRECTORY"));
  276. if (!cachedSourceDirectory.empty() && !sourceDirectory.empty() &&
  277. cachedSourceDirectory != sourceDirectory) {
  278. if (errorMessage) {
  279. *errorMessage = std::string("\"") + kSOURCE_DIRECTORY_KEY +
  280. "\" is set but incompatible with configured source directory.";
  281. }
  282. return false;
  283. }
  284. if (sourceDirectory.empty()) {
  285. sourceDirectory = cachedSourceDirectory;
  286. }
  287. }
  288. }
  289. if (sourceDirectory.empty()) {
  290. if (errorMessage) {
  291. *errorMessage = std::string("\"") + kSOURCE_DIRECTORY_KEY +
  292. "\" is unset but required.";
  293. }
  294. return false;
  295. }
  296. if (!cmSystemTools::FileIsDirectory(sourceDirectory)) {
  297. if (errorMessage) {
  298. *errorMessage =
  299. std::string("\"") + kSOURCE_DIRECTORY_KEY + "\" is not a directory.";
  300. }
  301. return false;
  302. }
  303. if (generator.empty()) {
  304. if (errorMessage) {
  305. *errorMessage =
  306. std::string("\"") + kGENERATOR_KEY + "\" is unset but required.";
  307. }
  308. return false;
  309. }
  310. const std::string fullGeneratorName =
  311. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  312. generator, extraGenerator);
  313. cmGlobalGenerator* gg = cm->CreateGlobalGenerator(fullGeneratorName);
  314. if (!gg) {
  315. if (errorMessage) {
  316. *errorMessage =
  317. std::string("Could not set up the requested combination of \"") +
  318. kGENERATOR_KEY + "\" and \"" + kEXTRA_GENERATOR_KEY + "\"";
  319. }
  320. return false;
  321. }
  322. cm->SetGlobalGenerator(gg);
  323. cm->SetHomeDirectory(sourceDirectory);
  324. cm->SetHomeOutputDirectory(buildDirectory);
  325. this->m_State = STATE_ACTIVE;
  326. return true;
  327. }
  328. void cmServerProtocol1_0::HandleCMakeFileChanges(const std::string& path,
  329. int event, int status)
  330. {
  331. assert(status == 0);
  332. static_cast<void>(status);
  333. if (!m_isDirty) {
  334. m_isDirty = true;
  335. SendSignal(kDIRTY_SIGNAL, Json::objectValue);
  336. }
  337. Json::Value obj = Json::objectValue;
  338. obj[kPATH_KEY] = path;
  339. Json::Value properties = Json::arrayValue;
  340. if (event & UV_RENAME) {
  341. properties.append(kRENAME_PROPERTY_VALUE);
  342. }
  343. if (event & UV_CHANGE) {
  344. properties.append(kCHANGE_PROPERTY_VALUE);
  345. }
  346. obj[kPROPERTIES_KEY] = properties;
  347. SendSignal(kFILE_CHANGE_SIGNAL, obj);
  348. }
  349. const cmServerResponse cmServerProtocol1_0::Process(
  350. const cmServerRequest& request)
  351. {
  352. assert(this->m_State >= STATE_ACTIVE);
  353. if (request.Type == kCACHE_TYPE) {
  354. return this->ProcessCache(request);
  355. }
  356. if (request.Type == kCMAKE_INPUTS_TYPE) {
  357. return this->ProcessCMakeInputs(request);
  358. }
  359. if (request.Type == kCODE_MODEL_TYPE) {
  360. return this->ProcessCodeModel(request);
  361. }
  362. if (request.Type == kCOMPUTE_TYPE) {
  363. return this->ProcessCompute(request);
  364. }
  365. if (request.Type == kCONFIGURE_TYPE) {
  366. return this->ProcessConfigure(request);
  367. }
  368. if (request.Type == kFILESYSTEM_WATCHERS_TYPE) {
  369. return this->ProcessFileSystemWatchers(request);
  370. }
  371. if (request.Type == kGLOBAL_SETTINGS_TYPE) {
  372. return this->ProcessGlobalSettings(request);
  373. }
  374. if (request.Type == kSET_GLOBAL_SETTINGS_TYPE) {
  375. return this->ProcessSetGlobalSettings(request);
  376. }
  377. return request.ReportError("Unknown command!");
  378. }
  379. bool cmServerProtocol1_0::IsExperimental() const
  380. {
  381. return true;
  382. }
  383. cmServerResponse cmServerProtocol1_0::ProcessCache(
  384. const cmServerRequest& request)
  385. {
  386. if (this->m_State < STATE_CONFIGURED) {
  387. return request.ReportError("This project was not configured yet.");
  388. }
  389. cmState* state = this->CMakeInstance()->GetState();
  390. Json::Value result = Json::objectValue;
  391. std::vector<std::string> allKeys = state->GetCacheEntryKeys();
  392. Json::Value list = Json::arrayValue;
  393. std::vector<std::string> keys = toStringList(request.Data[kKEYS_KEY]);
  394. if (keys.empty()) {
  395. keys = allKeys;
  396. } else {
  397. for (auto i : keys) {
  398. if (std::find_if(allKeys.begin(), allKeys.end(),
  399. [i](const std::string& j) { return i == j; }) ==
  400. allKeys.end()) {
  401. return request.ReportError("Key \"" + i + "\" not found in cache.");
  402. }
  403. }
  404. }
  405. std::sort(keys.begin(), keys.end());
  406. for (auto key : keys) {
  407. Json::Value entry = Json::objectValue;
  408. entry[kKEY_KEY] = key;
  409. entry[kTYPE_KEY] =
  410. cmState::CacheEntryTypeToString(state->GetCacheEntryType(key));
  411. entry[kVALUE_KEY] = state->GetCacheEntryValue(key);
  412. Json::Value props = Json::objectValue;
  413. bool haveProperties = false;
  414. for (auto prop : state->GetCacheEntryPropertyList(key)) {
  415. haveProperties = true;
  416. props[prop] = state->GetCacheEntryProperty(key, prop);
  417. }
  418. if (haveProperties) {
  419. entry[kPROPERTIES_KEY] = props;
  420. }
  421. list.append(entry);
  422. }
  423. result[kCACHE_KEY] = list;
  424. return request.Reply(result);
  425. }
  426. cmServerResponse cmServerProtocol1_0::ProcessCMakeInputs(
  427. const cmServerRequest& request)
  428. {
  429. if (this->m_State < STATE_CONFIGURED) {
  430. return request.ReportError("This instance was not yet configured.");
  431. }
  432. const cmake* cm = this->CMakeInstance();
  433. const cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  434. const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot();
  435. const std::string buildDir = cm->GetHomeOutputDirectory();
  436. const std::string sourceDir = cm->GetHomeDirectory();
  437. Json::Value result = Json::objectValue;
  438. result[kSOURCE_DIRECTORY_KEY] = sourceDir;
  439. result[kCMAKE_ROOT_DIRECTORY_KEY] = cmakeRootDir;
  440. std::vector<std::string> internalFiles;
  441. std::vector<std::string> explicitFiles;
  442. std::vector<std::string> tmpFiles;
  443. getCMakeInputs(gg, sourceDir, buildDir, &internalFiles, &explicitFiles,
  444. &tmpFiles);
  445. Json::Value array = Json::arrayValue;
  446. Json::Value tmp = Json::objectValue;
  447. tmp[kIS_CMAKE_KEY] = true;
  448. tmp[kIS_TEMPORARY_KEY] = false;
  449. tmp[kSOURCES_KEY] = fromStringList(internalFiles);
  450. array.append(tmp);
  451. tmp = Json::objectValue;
  452. tmp[kIS_CMAKE_KEY] = false;
  453. tmp[kIS_TEMPORARY_KEY] = false;
  454. tmp[kSOURCES_KEY] = fromStringList(explicitFiles);
  455. array.append(tmp);
  456. tmp = Json::objectValue;
  457. tmp[kIS_CMAKE_KEY] = false;
  458. tmp[kIS_TEMPORARY_KEY] = true;
  459. tmp[kSOURCES_KEY] = fromStringList(tmpFiles);
  460. array.append(tmp);
  461. result[kBUILD_FILES_KEY] = array;
  462. return request.Reply(result);
  463. }
  464. class LanguageData
  465. {
  466. public:
  467. bool operator==(const LanguageData& other) const;
  468. void SetDefines(const std::set<std::string>& defines);
  469. bool IsGenerated = false;
  470. std::string Language;
  471. std::string Flags;
  472. std::vector<std::string> Defines;
  473. std::vector<std::pair<std::string, bool> > IncludePathList;
  474. };
  475. bool LanguageData::operator==(const LanguageData& other) const
  476. {
  477. return Language == other.Language && Defines == other.Defines &&
  478. Flags == other.Flags && IncludePathList == other.IncludePathList &&
  479. IsGenerated == other.IsGenerated;
  480. }
  481. void LanguageData::SetDefines(const std::set<std::string>& defines)
  482. {
  483. std::vector<std::string> result;
  484. for (auto i : defines) {
  485. result.push_back(i);
  486. }
  487. std::sort(result.begin(), result.end());
  488. Defines = result;
  489. }
  490. namespace std {
  491. template <>
  492. struct hash<LanguageData>
  493. {
  494. std::size_t operator()(const LanguageData& in) const
  495. {
  496. using std::hash;
  497. size_t result =
  498. hash<std::string>()(in.Language) ^ hash<std::string>()(in.Flags);
  499. for (auto i : in.IncludePathList) {
  500. result = result ^ (hash<std::string>()(i.first) ^
  501. (i.second ? std::numeric_limits<size_t>::max() : 0));
  502. }
  503. for (auto i : in.Defines) {
  504. result = result ^ hash<std::string>()(i);
  505. }
  506. result =
  507. result ^ (in.IsGenerated ? std::numeric_limits<size_t>::max() : 0);
  508. return result;
  509. }
  510. };
  511. } // namespace std
  512. static Json::Value DumpSourceFileGroup(const LanguageData& data,
  513. const std::vector<std::string>& files,
  514. const std::string& baseDir)
  515. {
  516. Json::Value result = Json::objectValue;
  517. if (!data.Language.empty()) {
  518. result[kLANGUAGE_KEY] = data.Language;
  519. if (!data.Flags.empty()) {
  520. result[kCOMPILE_FLAGS_KEY] = data.Flags;
  521. }
  522. if (!data.IncludePathList.empty()) {
  523. Json::Value includes = Json::arrayValue;
  524. for (auto i : data.IncludePathList) {
  525. Json::Value tmp = Json::objectValue;
  526. tmp[kPATH_KEY] = i.first;
  527. if (i.second) {
  528. tmp[kIS_SYSTEM_KEY] = i.second;
  529. }
  530. includes.append(tmp);
  531. }
  532. result[kINCLUDE_PATH_KEY] = includes;
  533. }
  534. if (!data.Defines.empty()) {
  535. result[kDEFINES_KEY] = fromStringList(data.Defines);
  536. }
  537. }
  538. result[kIS_GENERATED_KEY] = data.IsGenerated;
  539. Json::Value sourcesValue = Json::arrayValue;
  540. for (auto i : files) {
  541. const std::string relPath =
  542. cmSystemTools::RelativePath(baseDir.c_str(), i.c_str());
  543. sourcesValue.append(relPath.size() < i.size() ? relPath : i);
  544. }
  545. result[kSOURCES_KEY] = sourcesValue;
  546. return result;
  547. }
  548. static Json::Value DumpSourceFilesList(
  549. cmGeneratorTarget* target, const std::string& config,
  550. const std::map<std::string, LanguageData>& languageDataMap)
  551. {
  552. // Collect sourcefile groups:
  553. std::vector<cmSourceFile*> files;
  554. target->GetSourceFiles(files, config);
  555. std::unordered_map<LanguageData, std::vector<std::string> > fileGroups;
  556. for (cmSourceFile* file : files) {
  557. LanguageData fileData;
  558. fileData.Language = file->GetLanguage();
  559. if (!fileData.Language.empty()) {
  560. const LanguageData& ld = languageDataMap.at(fileData.Language);
  561. cmLocalGenerator* lg = target->GetLocalGenerator();
  562. std::string compileFlags = ld.Flags;
  563. lg->AppendFlags(compileFlags, file->GetProperty("COMPILE_FLAGS"));
  564. fileData.Flags = compileFlags;
  565. fileData.IncludePathList = ld.IncludePathList;
  566. std::set<std::string> defines;
  567. lg->AppendDefines(defines, file->GetProperty("COMPILE_DEFINITIONS"));
  568. const std::string defPropName =
  569. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  570. lg->AppendDefines(defines, file->GetProperty(defPropName));
  571. defines.insert(ld.Defines.begin(), ld.Defines.end());
  572. fileData.SetDefines(defines);
  573. }
  574. fileData.IsGenerated = file->GetPropertyAsBool("GENERATED");
  575. std::vector<std::string>& groupFileList = fileGroups[fileData];
  576. groupFileList.push_back(file->GetFullPath());
  577. }
  578. const std::string baseDir = target->Makefile->GetCurrentSourceDirectory();
  579. Json::Value result = Json::arrayValue;
  580. for (auto it = fileGroups.begin(); it != fileGroups.end(); ++it) {
  581. Json::Value group = DumpSourceFileGroup(it->first, it->second, baseDir);
  582. if (!group.isNull())
  583. result.append(group);
  584. }
  585. return result;
  586. }
  587. static Json::Value DumpTarget(cmGeneratorTarget* target,
  588. const std::string& config)
  589. {
  590. cmLocalGenerator* lg = target->GetLocalGenerator();
  591. const cmState* state = lg->GetState();
  592. const cmState::TargetType type = target->GetType();
  593. const std::string typeName = state->GetTargetTypeName(type);
  594. Json::Value ttl = Json::arrayValue;
  595. ttl.append("EXECUTABLE");
  596. ttl.append("STATIC_LIBRARY");
  597. ttl.append("SHARED_LIBRARY");
  598. ttl.append("MODULE_LIBRARY");
  599. ttl.append("OBJECT_LIBRARY");
  600. ttl.append("UTILITY");
  601. ttl.append("INTERFACE_LIBRARY");
  602. if (!hasString(ttl, typeName) || target->IsImported()) {
  603. return Json::Value();
  604. }
  605. Json::Value result = Json::objectValue;
  606. result[kNAME_KEY] = target->GetName();
  607. result[kTYPE_KEY] = typeName;
  608. result[kFULL_NAME_KEY] = target->GetFullName(config);
  609. result[kSOURCE_DIRECTORY_KEY] = lg->GetCurrentSourceDirectory();
  610. result[kBUILD_DIRECTORY_KEY] = lg->GetCurrentBinaryDirectory();
  611. if (target->HaveWellDefinedOutputFiles()) {
  612. Json::Value artifacts = Json::arrayValue;
  613. artifacts.append(target->GetFullPath(config, false));
  614. if (target->IsDLLPlatform()) {
  615. artifacts.append(target->GetFullPath(config, true));
  616. const cmGeneratorTarget::OutputInfo* output =
  617. target->GetOutputInfo(config);
  618. if (output && !output->PdbDir.empty()) {
  619. artifacts.append(output->PdbDir + '/' + target->GetPDBName(config));
  620. }
  621. }
  622. result[kARTIFACTS_KEY] = artifacts;
  623. result[kLINKER_LANGUAGE_KEY] = target->GetLinkerLanguage(config);
  624. std::string linkLibs;
  625. std::string linkFlags;
  626. std::string linkLanguageFlags;
  627. std::string frameworkPath;
  628. std::string linkPath;
  629. cmLinkLineComputer linkLineComputer(lg,
  630. lg->GetStateSnapshot().GetDirectory());
  631. lg->GetTargetFlags(&linkLineComputer, config, linkLibs, linkLanguageFlags,
  632. linkFlags, frameworkPath, linkPath, target);
  633. linkLibs = cmSystemTools::TrimWhitespace(linkLibs);
  634. linkFlags = cmSystemTools::TrimWhitespace(linkFlags);
  635. linkLanguageFlags = cmSystemTools::TrimWhitespace(linkLanguageFlags);
  636. frameworkPath = cmSystemTools::TrimWhitespace(frameworkPath);
  637. linkPath = cmSystemTools::TrimWhitespace(linkPath);
  638. if (!cmSystemTools::TrimWhitespace(linkLibs).empty()) {
  639. result[kLINK_LIBRARIES_KEY] = linkLibs;
  640. }
  641. if (!cmSystemTools::TrimWhitespace(linkFlags).empty()) {
  642. result[kLINK_FLAGS_KEY] = linkFlags;
  643. }
  644. if (!cmSystemTools::TrimWhitespace(linkLanguageFlags).empty()) {
  645. result[kLINK_LANGUAGE_FLAGS_KEY] = linkLanguageFlags;
  646. }
  647. if (!frameworkPath.empty()) {
  648. result[kFRAMEWORK_PATH_KEY] = frameworkPath;
  649. }
  650. if (!linkPath.empty()) {
  651. result[kLINK_PATH_KEY] = linkPath;
  652. }
  653. const std::string sysroot =
  654. lg->GetMakefile()->GetSafeDefinition("CMAKE_SYSROOT");
  655. if (!sysroot.empty()) {
  656. result[kSYSROOT_KEY] = sysroot;
  657. }
  658. }
  659. std::set<std::string> languages;
  660. target->GetLanguages(languages, config);
  661. std::map<std::string, LanguageData> languageDataMap;
  662. for (auto lang : languages) {
  663. LanguageData& ld = languageDataMap[lang];
  664. ld.Language = lang;
  665. lg->GetTargetCompileFlags(target, config, lang, ld.Flags);
  666. std::set<std::string> defines;
  667. lg->GetTargetDefines(target, config, lang, defines);
  668. ld.SetDefines(defines);
  669. std::vector<std::string> includePathList;
  670. lg->GetIncludeDirectories(includePathList, target, lang, config, true);
  671. for (auto i : includePathList) {
  672. ld.IncludePathList.push_back(
  673. std::make_pair(i, target->IsSystemIncludeDirectory(i, config)));
  674. }
  675. }
  676. Json::Value sourceGroupsValue =
  677. DumpSourceFilesList(target, config, languageDataMap);
  678. if (!sourceGroupsValue.empty()) {
  679. result[kFILE_GROUPS_KEY] = sourceGroupsValue;
  680. }
  681. return result;
  682. }
  683. static Json::Value DumpTargetsList(
  684. const std::vector<cmLocalGenerator*>& generators, const std::string& config)
  685. {
  686. Json::Value result = Json::arrayValue;
  687. std::vector<cmGeneratorTarget*> targetList;
  688. for (const auto& lgIt : generators) {
  689. auto list = lgIt->GetGeneratorTargets();
  690. targetList.insert(targetList.end(), list.begin(), list.end());
  691. }
  692. std::sort(targetList.begin(), targetList.end());
  693. for (cmGeneratorTarget* target : targetList) {
  694. Json::Value tmp = DumpTarget(target, config);
  695. if (!tmp.isNull()) {
  696. result.append(tmp);
  697. }
  698. }
  699. return result;
  700. }
  701. static Json::Value DumpProjectList(const cmake* cm, const std::string config)
  702. {
  703. Json::Value result = Json::arrayValue;
  704. auto globalGen = cm->GetGlobalGenerator();
  705. for (const auto& projectIt : globalGen->GetProjectMap()) {
  706. Json::Value pObj = Json::objectValue;
  707. pObj[kNAME_KEY] = projectIt.first;
  708. assert(projectIt.second.size() >
  709. 0); // All Projects must have at least one local generator
  710. const cmLocalGenerator* lg = projectIt.second.at(0);
  711. // Project structure information:
  712. const cmMakefile* mf = lg->GetMakefile();
  713. pObj[kSOURCE_DIRECTORY_KEY] = mf->GetCurrentSourceDirectory();
  714. pObj[kBUILD_DIRECTORY_KEY] = mf->GetCurrentBinaryDirectory();
  715. pObj[kTARGETS_KEY] = DumpTargetsList(projectIt.second, config);
  716. result.append(pObj);
  717. }
  718. return result;
  719. }
  720. static Json::Value DumpConfiguration(const cmake* cm,
  721. const std::string& config)
  722. {
  723. Json::Value result = Json::objectValue;
  724. result[kNAME_KEY] = config;
  725. result[kPROJECTS_KEY] = DumpProjectList(cm, config);
  726. return result;
  727. }
  728. static Json::Value DumpConfigurationsList(const cmake* cm)
  729. {
  730. Json::Value result = Json::arrayValue;
  731. for (const std::string& c : getConfigurations(cm)) {
  732. result.append(DumpConfiguration(cm, c));
  733. }
  734. return result;
  735. }
  736. cmServerResponse cmServerProtocol1_0::ProcessCodeModel(
  737. const cmServerRequest& request)
  738. {
  739. if (this->m_State != STATE_COMPUTED) {
  740. return request.ReportError("No build system was generated yet.");
  741. }
  742. Json::Value result = Json::objectValue;
  743. result[kCONFIGURATIONS_KEY] = DumpConfigurationsList(this->CMakeInstance());
  744. return request.Reply(result);
  745. }
  746. cmServerResponse cmServerProtocol1_0::ProcessCompute(
  747. const cmServerRequest& request)
  748. {
  749. if (this->m_State > STATE_CONFIGURED) {
  750. return request.ReportError("This build system was already generated.");
  751. }
  752. if (this->m_State < STATE_CONFIGURED) {
  753. return request.ReportError("This project was not configured yet.");
  754. }
  755. cmake* cm = this->CMakeInstance();
  756. int ret = cm->Generate();
  757. if (ret < 0) {
  758. return request.ReportError("Failed to compute build system.");
  759. }
  760. m_State = STATE_COMPUTED;
  761. return request.Reply(Json::Value());
  762. }
  763. cmServerResponse cmServerProtocol1_0::ProcessConfigure(
  764. const cmServerRequest& request)
  765. {
  766. if (this->m_State == STATE_INACTIVE) {
  767. return request.ReportError("This instance is inactive.");
  768. }
  769. FileMonitor()->StopMonitoring();
  770. // Make sure the types of cacheArguments matches (if given):
  771. std::vector<std::string> cacheArgs;
  772. bool cacheArgumentsError = false;
  773. const Json::Value passedArgs = request.Data[kCACHE_ARGUMENTS_KEY];
  774. if (!passedArgs.isNull()) {
  775. if (passedArgs.isString()) {
  776. cacheArgs.push_back(passedArgs.asString());
  777. } else if (passedArgs.isArray()) {
  778. for (auto i = passedArgs.begin(); i != passedArgs.end(); ++i) {
  779. if (!i->isString()) {
  780. cacheArgumentsError = true;
  781. break;
  782. }
  783. cacheArgs.push_back(i->asString());
  784. }
  785. } else {
  786. cacheArgumentsError = true;
  787. }
  788. }
  789. if (cacheArgumentsError) {
  790. request.ReportError(
  791. "cacheArguments must be unset, a string or an array of strings.");
  792. }
  793. cmake* cm = this->CMakeInstance();
  794. std::string sourceDir = cm->GetHomeDirectory();
  795. const std::string buildDir = cm->GetHomeOutputDirectory();
  796. cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  797. if (buildDir.empty()) {
  798. return request.ReportError(
  799. "No build directory set via setGlobalSettings.");
  800. }
  801. if (cm->LoadCache(buildDir)) {
  802. // build directory has been set up before
  803. const char* cachedSourceDir =
  804. cm->GetState()->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
  805. if (!cachedSourceDir) {
  806. return request.ReportError("No CMAKE_HOME_DIRECTORY found in cache.");
  807. }
  808. if (sourceDir.empty()) {
  809. sourceDir = std::string(cachedSourceDir);
  810. cm->SetHomeDirectory(sourceDir);
  811. }
  812. const char* cachedGenerator =
  813. cm->GetState()->GetInitializedCacheValue("CMAKE_GENERATOR");
  814. if (cachedGenerator) {
  815. if (gg && gg->GetName() != cachedGenerator) {
  816. return request.ReportError("Configured generator does not match with "
  817. "CMAKE_GENERATOR found in cache.");
  818. }
  819. }
  820. } else {
  821. // build directory has not been set up before
  822. if (sourceDir.empty()) {
  823. return request.ReportError("No sourceDirectory set via "
  824. "setGlobalSettings and no cache found in "
  825. "buildDirectory.");
  826. }
  827. }
  828. if (cm->AddCMakePaths() != 1) {
  829. return request.ReportError("Failed to set CMake paths.");
  830. }
  831. if (!cm->SetCacheArgs(cacheArgs)) {
  832. return request.ReportError("cacheArguments could not be set.");
  833. }
  834. int ret = cm->Configure();
  835. if (ret < 0) {
  836. return request.ReportError("Configuration failed.");
  837. }
  838. std::vector<std::string> toWatchList;
  839. getCMakeInputs(gg, std::string(), buildDir, nullptr, &toWatchList, nullptr);
  840. FileMonitor()->MonitorPaths(toWatchList,
  841. [this](const std::string& p, int e, int s) {
  842. this->HandleCMakeFileChanges(p, e, s);
  843. });
  844. m_State = STATE_CONFIGURED;
  845. m_isDirty = false;
  846. return request.Reply(Json::Value());
  847. }
  848. cmServerResponse cmServerProtocol1_0::ProcessGlobalSettings(
  849. const cmServerRequest& request)
  850. {
  851. cmake* cm = this->CMakeInstance();
  852. Json::Value obj = Json::objectValue;
  853. // Capabilities information:
  854. obj[kCAPABILITIES_KEY] = cm->ReportCapabilitiesJson(true);
  855. obj[kDEBUG_OUTPUT_KEY] = cm->GetDebugOutput();
  856. obj[kTRACE_KEY] = cm->GetTrace();
  857. obj[kTRACE_EXPAND_KEY] = cm->GetTraceExpand();
  858. obj[kWARN_UNINITIALIZED_KEY] = cm->GetWarnUninitialized();
  859. obj[kWARN_UNUSED_KEY] = cm->GetWarnUnused();
  860. obj[kWARN_UNUSED_CLI_KEY] = cm->GetWarnUnusedCli();
  861. obj[kCHECK_SYSTEM_VARS_KEY] = cm->GetCheckSystemVars();
  862. obj[kSOURCE_DIRECTORY_KEY] = cm->GetHomeDirectory();
  863. obj[kBUILD_DIRECTORY_KEY] = cm->GetHomeOutputDirectory();
  864. // Currently used generator:
  865. cmGlobalGenerator* gen = cm->GetGlobalGenerator();
  866. obj[kGENERATOR_KEY] = gen ? gen->GetName() : std::string();
  867. obj[kEXTRA_GENERATOR_KEY] =
  868. gen ? gen->GetExtraGeneratorName() : std::string();
  869. return request.Reply(obj);
  870. }
  871. static void setBool(const cmServerRequest& request, const std::string& key,
  872. std::function<void(bool)> setter)
  873. {
  874. if (request.Data[key].isNull()) {
  875. return;
  876. }
  877. setter(request.Data[key].asBool());
  878. }
  879. cmServerResponse cmServerProtocol1_0::ProcessSetGlobalSettings(
  880. const cmServerRequest& request)
  881. {
  882. const std::vector<std::string> boolValues = {
  883. kDEBUG_OUTPUT_KEY, kTRACE_KEY, kTRACE_EXPAND_KEY,
  884. kWARN_UNINITIALIZED_KEY, kWARN_UNUSED_KEY, kWARN_UNUSED_CLI_KEY,
  885. kCHECK_SYSTEM_VARS_KEY
  886. };
  887. for (auto i : boolValues) {
  888. if (!request.Data[i].isNull() && !request.Data[i].isBool()) {
  889. return request.ReportError("\"" + i +
  890. "\" must be unset or a bool value.");
  891. }
  892. }
  893. cmake* cm = this->CMakeInstance();
  894. setBool(request, kDEBUG_OUTPUT_KEY,
  895. [cm](bool e) { cm->SetDebugOutputOn(e); });
  896. setBool(request, kTRACE_KEY, [cm](bool e) { cm->SetTrace(e); });
  897. setBool(request, kTRACE_EXPAND_KEY, [cm](bool e) { cm->SetTraceExpand(e); });
  898. setBool(request, kWARN_UNINITIALIZED_KEY,
  899. [cm](bool e) { cm->SetWarnUninitialized(e); });
  900. setBool(request, kWARN_UNUSED_KEY, [cm](bool e) { cm->SetWarnUnused(e); });
  901. setBool(request, kWARN_UNUSED_CLI_KEY,
  902. [cm](bool e) { cm->SetWarnUnusedCli(e); });
  903. setBool(request, kCHECK_SYSTEM_VARS_KEY,
  904. [cm](bool e) { cm->SetCheckSystemVars(e); });
  905. return request.Reply(Json::Value());
  906. }
  907. cmServerResponse cmServerProtocol1_0::ProcessFileSystemWatchers(
  908. const cmServerRequest& request)
  909. {
  910. const cmFileMonitor* const fm = FileMonitor();
  911. Json::Value result = Json::objectValue;
  912. Json::Value files = Json::arrayValue;
  913. for (const auto& f : fm->WatchedFiles()) {
  914. files.append(f);
  915. }
  916. Json::Value directories = Json::arrayValue;
  917. for (const auto& d : fm->WatchedDirectories()) {
  918. directories.append(d);
  919. }
  920. result[kWATCHED_FILES_KEY] = files;
  921. result[kWATCHED_DIRECTORIES_KEY] = directories;
  922. return request.Reply(result);
  923. }