cmServerProtocol.cxx 30 KB

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