cmState.cxx 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2015 Stephen Kelly <[email protected]>
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmState.h"
  11. #include "cmake.h"
  12. #include "cmCacheManager.h"
  13. #include "cmCommand.h"
  14. #include "cmAlgorithms.h"
  15. #include <assert.h>
  16. struct cmState::SnapshotDataType
  17. {
  18. cmState::PositionType CallStackParent;
  19. cmState::PositionType DirectoryParent;
  20. cmState::SnapshotType SnapshotType;
  21. cmLinkedTree<std::string>::iterator ExecutionListFile;
  22. cmLinkedTree<cmState::BuildsystemDirectoryStateType>::iterator
  23. BuildSystemDirectory;
  24. std::string EntryPointCommand;
  25. long EntryPointLine;
  26. std::vector<std::string>::size_type IncludeDirectoryPosition;
  27. std::vector<std::string>::size_type CompileDefinitionsPosition;
  28. std::vector<std::string>::size_type CompileOptionsPosition;
  29. };
  30. struct cmState::BuildsystemDirectoryStateType
  31. {
  32. std::string Location;
  33. std::string OutputLocation;
  34. std::vector<std::string> CurrentSourceDirectoryComponents;
  35. std::vector<std::string> CurrentBinaryDirectoryComponents;
  36. // The top-most directories for relative path conversion. Both the
  37. // source and destination location of a relative path conversion
  38. // must be underneath one of these directories (both under source or
  39. // both under binary) in order for the relative path to be evaluated
  40. // safely by the build tools.
  41. std::string RelativePathTopSource;
  42. std::string RelativePathTopBinary;
  43. std::vector<std::string> IncludeDirectories;
  44. std::vector<cmListFileBacktrace> IncludeDirectoryBacktraces;
  45. std::vector<std::string> CompileDefinitions;
  46. std::vector<cmListFileBacktrace> CompileDefinitionsBacktraces;
  47. std::vector<std::string> CompileOptions;
  48. std::vector<cmListFileBacktrace> CompileOptionsBacktraces;
  49. };
  50. cmState::cmState(cmake* cm)
  51. : CMakeInstance(cm),
  52. IsInTryCompile(false),
  53. WindowsShell(false),
  54. WindowsVSIDE(false),
  55. WatcomWMake(false),
  56. MinGWMake(false),
  57. NMake(false),
  58. MSYSShell(false)
  59. {
  60. }
  61. cmState::~cmState()
  62. {
  63. cmDeleteAll(this->Commands);
  64. }
  65. const char* cmCacheEntryTypes[] =
  66. { "BOOL",
  67. "PATH",
  68. "FILEPATH",
  69. "STRING",
  70. "INTERNAL",
  71. "STATIC",
  72. "UNINITIALIZED",
  73. 0
  74. };
  75. const char*
  76. cmState::CacheEntryTypeToString(cmState::CacheEntryType type)
  77. {
  78. if ( type > 6 )
  79. {
  80. return cmCacheEntryTypes[6];
  81. }
  82. return cmCacheEntryTypes[type];
  83. }
  84. cmState::CacheEntryType
  85. cmState::StringToCacheEntryType(const char* s)
  86. {
  87. int i = 0;
  88. while(cmCacheEntryTypes[i])
  89. {
  90. if(strcmp(s, cmCacheEntryTypes[i]) == 0)
  91. {
  92. return static_cast<cmState::CacheEntryType>(i);
  93. }
  94. ++i;
  95. }
  96. return STRING;
  97. }
  98. bool cmState::IsCacheEntryType(std::string const& key)
  99. {
  100. for(int i=0; cmCacheEntryTypes[i]; ++i)
  101. {
  102. if(strcmp(key.c_str(), cmCacheEntryTypes[i]) == 0)
  103. {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. std::vector<std::string> cmState::GetCacheEntryKeys() const
  110. {
  111. std::vector<std::string> definitions;
  112. definitions.reserve(this->CMakeInstance->GetCacheManager()->GetSize());
  113. cmCacheManager::CacheIterator cit =
  114. this->CMakeInstance->GetCacheManager()->GetCacheIterator();
  115. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  116. {
  117. definitions.push_back(cit.GetName());
  118. }
  119. return definitions;
  120. }
  121. const char* cmState::GetCacheEntryValue(std::string const& key) const
  122. {
  123. cmCacheManager::CacheEntry* e = this->CMakeInstance->GetCacheManager()
  124. ->GetCacheEntry(key);
  125. if (!e)
  126. {
  127. return 0;
  128. }
  129. return e->Value.c_str();
  130. }
  131. const char*
  132. cmState::GetInitializedCacheValue(std::string const& key) const
  133. {
  134. return this->CMakeInstance->GetCacheManager()->GetInitializedCacheValue(key);
  135. }
  136. cmState::CacheEntryType
  137. cmState::GetCacheEntryType(std::string const& key) const
  138. {
  139. cmCacheManager::CacheIterator it =
  140. this->CMakeInstance->GetCacheManager()->GetCacheIterator(key.c_str());
  141. return it.GetType();
  142. }
  143. void cmState::SetCacheEntryValue(std::string const& key,
  144. std::string const& value)
  145. {
  146. this->CMakeInstance->GetCacheManager()->SetCacheEntryValue(key, value);
  147. }
  148. void cmState::SetCacheEntryProperty(std::string const& key,
  149. std::string const& propertyName,
  150. std::string const& value)
  151. {
  152. cmCacheManager::CacheIterator it =
  153. this->CMakeInstance->GetCacheManager()->GetCacheIterator(key.c_str());
  154. it.SetProperty(propertyName, value.c_str());
  155. }
  156. void cmState::SetCacheEntryBoolProperty(std::string const& key,
  157. std::string const& propertyName,
  158. bool value)
  159. {
  160. cmCacheManager::CacheIterator it =
  161. this->CMakeInstance->GetCacheManager()->GetCacheIterator(key.c_str());
  162. it.SetProperty(propertyName, value);
  163. }
  164. const char* cmState::GetCacheEntryProperty(std::string const& key,
  165. std::string const& propertyName)
  166. {
  167. cmCacheManager::CacheIterator it = this->CMakeInstance->GetCacheManager()
  168. ->GetCacheIterator(key.c_str());
  169. if (!it.PropertyExists(propertyName))
  170. {
  171. return 0;
  172. }
  173. return it.GetProperty(propertyName);
  174. }
  175. bool cmState::GetCacheEntryPropertyAsBool(std::string const& key,
  176. std::string const& propertyName)
  177. {
  178. return this->CMakeInstance->GetCacheManager()
  179. ->GetCacheIterator(key.c_str()).GetPropertyAsBool(propertyName);
  180. }
  181. void cmState::AddCacheEntry(const std::string& key, const char* value,
  182. const char* helpString,
  183. cmState::CacheEntryType type)
  184. {
  185. this->CMakeInstance->GetCacheManager()->AddCacheEntry(key, value,
  186. helpString, type);
  187. }
  188. void cmState::RemoveCacheEntry(std::string const& key)
  189. {
  190. this->CMakeInstance->GetCacheManager()->RemoveCacheEntry(key);
  191. }
  192. void cmState::AppendCacheEntryProperty(const std::string& key,
  193. const std::string& property,
  194. const std::string& value,
  195. bool asString)
  196. {
  197. this->CMakeInstance->GetCacheManager()
  198. ->GetCacheIterator(key.c_str()).AppendProperty(property,
  199. value.c_str(),
  200. asString);
  201. }
  202. void cmState::RemoveCacheEntryProperty(std::string const& key,
  203. std::string const& propertyName)
  204. {
  205. this->CMakeInstance->GetCacheManager()
  206. ->GetCacheIterator(key.c_str()).SetProperty(propertyName, (void*)0);
  207. }
  208. cmState::Snapshot cmState::Reset()
  209. {
  210. this->GlobalProperties.clear();
  211. this->PropertyDefinitions.clear();
  212. {
  213. cmLinkedTree<BuildsystemDirectoryStateType>::iterator it =
  214. this->BuildsystemDirectory.Truncate();
  215. it->IncludeDirectories.clear();
  216. it->IncludeDirectoryBacktraces.clear();
  217. it->CompileDefinitions.clear();
  218. it->CompileDefinitionsBacktraces.clear();
  219. it->CompileOptions.clear();
  220. it->CompileOptionsBacktraces.clear();
  221. }
  222. PositionType pos = this->SnapshotData.Truncate();
  223. this->ExecutionListFiles.Truncate();
  224. this->DefineProperty
  225. ("RULE_LAUNCH_COMPILE", cmProperty::DIRECTORY,
  226. "", "", true);
  227. this->DefineProperty
  228. ("RULE_LAUNCH_LINK", cmProperty::DIRECTORY,
  229. "", "", true);
  230. this->DefineProperty
  231. ("RULE_LAUNCH_CUSTOM", cmProperty::DIRECTORY,
  232. "", "", true);
  233. this->DefineProperty
  234. ("RULE_LAUNCH_COMPILE", cmProperty::TARGET,
  235. "", "", true);
  236. this->DefineProperty
  237. ("RULE_LAUNCH_LINK", cmProperty::TARGET,
  238. "", "", true);
  239. this->DefineProperty
  240. ("RULE_LAUNCH_CUSTOM", cmProperty::TARGET,
  241. "", "", true);
  242. return Snapshot(this, pos);
  243. }
  244. void cmState::DefineProperty(const std::string& name,
  245. cmProperty::ScopeType scope,
  246. const char *ShortDescription,
  247. const char *FullDescription,
  248. bool chained)
  249. {
  250. this->PropertyDefinitions[scope].DefineProperty(name,scope,ShortDescription,
  251. FullDescription,
  252. chained);
  253. }
  254. cmPropertyDefinition const* cmState
  255. ::GetPropertyDefinition(const std::string& name,
  256. cmProperty::ScopeType scope) const
  257. {
  258. if (this->IsPropertyDefined(name,scope))
  259. {
  260. cmPropertyDefinitionMap const& defs =
  261. this->PropertyDefinitions.find(scope)->second;
  262. return &defs.find(name)->second;
  263. }
  264. return 0;
  265. }
  266. bool cmState::IsPropertyDefined(const std::string& name,
  267. cmProperty::ScopeType scope) const
  268. {
  269. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it
  270. = this->PropertyDefinitions.find(scope);
  271. if (it == this->PropertyDefinitions.end())
  272. {
  273. return false;
  274. }
  275. return it->second.IsPropertyDefined(name);
  276. }
  277. bool cmState::IsPropertyChained(const std::string& name,
  278. cmProperty::ScopeType scope) const
  279. {
  280. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it
  281. = this->PropertyDefinitions.find(scope);
  282. if (it == this->PropertyDefinitions.end())
  283. {
  284. return false;
  285. }
  286. return it->second.IsPropertyChained(name);
  287. }
  288. void cmState::SetLanguageEnabled(std::string const& l)
  289. {
  290. std::vector<std::string>::iterator it =
  291. std::lower_bound(this->EnabledLanguages.begin(),
  292. this->EnabledLanguages.end(), l);
  293. if (it == this->EnabledLanguages.end() || *it != l)
  294. {
  295. this->EnabledLanguages.insert(it, l);
  296. }
  297. }
  298. bool cmState::GetLanguageEnabled(std::string const& l) const
  299. {
  300. return std::binary_search(this->EnabledLanguages.begin(),
  301. this->EnabledLanguages.end(), l);
  302. }
  303. std::vector<std::string> cmState::GetEnabledLanguages() const
  304. {
  305. return this->EnabledLanguages;
  306. }
  307. void cmState::SetEnabledLanguages(std::vector<std::string> const& langs)
  308. {
  309. this->EnabledLanguages = langs;
  310. }
  311. void cmState::ClearEnabledLanguages()
  312. {
  313. this->EnabledLanguages.clear();
  314. }
  315. bool cmState::GetIsInTryCompile() const
  316. {
  317. return this->IsInTryCompile;
  318. }
  319. void cmState::SetIsInTryCompile(bool b)
  320. {
  321. this->IsInTryCompile = b;
  322. }
  323. void cmState::RenameCommand(std::string const& oldName,
  324. std::string const& newName)
  325. {
  326. // if the command already exists, free the old one
  327. std::string sOldName = cmSystemTools::LowerCase(oldName);
  328. std::string sNewName = cmSystemTools::LowerCase(newName);
  329. std::map<std::string, cmCommand*>::iterator pos =
  330. this->Commands.find(sOldName);
  331. if ( pos == this->Commands.end() )
  332. {
  333. return;
  334. }
  335. cmCommand* cmd = pos->second;
  336. pos = this->Commands.find(sNewName);
  337. if (pos != this->Commands.end())
  338. {
  339. delete pos->second;
  340. this->Commands.erase(pos);
  341. }
  342. this->Commands.insert(std::make_pair(sNewName, cmd));
  343. pos = this->Commands.find(sOldName);
  344. this->Commands.erase(pos);
  345. }
  346. void cmState::AddCommand(cmCommand* command)
  347. {
  348. std::string name = cmSystemTools::LowerCase(command->GetName());
  349. // if the command already exists, free the old one
  350. std::map<std::string, cmCommand*>::iterator pos = this->Commands.find(name);
  351. if (pos != this->Commands.end())
  352. {
  353. delete pos->second;
  354. this->Commands.erase(pos);
  355. }
  356. this->Commands.insert(std::make_pair(name, command));
  357. }
  358. void cmState::RemoveUnscriptableCommands()
  359. {
  360. std::vector<std::string> unscriptableCommands;
  361. for (std::map<std::string, cmCommand*>::iterator
  362. pos = this->Commands.begin();
  363. pos != this->Commands.end(); )
  364. {
  365. if (!pos->second->IsScriptable())
  366. {
  367. delete pos->second;
  368. this->Commands.erase(pos++);
  369. }
  370. else
  371. {
  372. ++pos;
  373. }
  374. }
  375. }
  376. cmCommand* cmState::GetCommand(std::string const& name) const
  377. {
  378. cmCommand* command = 0;
  379. std::string sName = cmSystemTools::LowerCase(name);
  380. std::map<std::string, cmCommand*>::const_iterator pos =
  381. this->Commands.find(sName);
  382. if (pos != this->Commands.end())
  383. {
  384. command = (*pos).second;
  385. }
  386. return command;
  387. }
  388. std::vector<std::string> cmState::GetCommandNames() const
  389. {
  390. std::vector<std::string> commandNames;
  391. commandNames.reserve(this->Commands.size());
  392. std::map<std::string, cmCommand*>::const_iterator cmds
  393. = this->Commands.begin();
  394. for ( ; cmds != this->Commands.end(); ++ cmds )
  395. {
  396. commandNames.push_back(cmds->first);
  397. }
  398. return commandNames;
  399. }
  400. void cmState::RemoveUserDefinedCommands()
  401. {
  402. std::vector<cmCommand*> renamedCommands;
  403. for(std::map<std::string, cmCommand*>::iterator j = this->Commands.begin();
  404. j != this->Commands.end(); )
  405. {
  406. if (j->second->IsA("cmMacroHelperCommand") ||
  407. j->second->IsA("cmFunctionHelperCommand"))
  408. {
  409. delete j->second;
  410. this->Commands.erase(j++);
  411. }
  412. else if (j->first != j->second->GetName())
  413. {
  414. renamedCommands.push_back(j->second);
  415. this->Commands.erase(j++);
  416. }
  417. else
  418. {
  419. ++j;
  420. }
  421. }
  422. for (std::vector<cmCommand*>::const_iterator it = renamedCommands.begin();
  423. it != renamedCommands.end(); ++it)
  424. {
  425. this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
  426. }
  427. }
  428. void cmState::SetGlobalProperty(const std::string& prop, const char* value)
  429. {
  430. this->GlobalProperties.SetProperty(prop, value);
  431. }
  432. void cmState::AppendGlobalProperty(const std::string& prop,
  433. const char* value, bool asString)
  434. {
  435. this->GlobalProperties.AppendProperty(prop, value, asString);
  436. }
  437. const char *cmState::GetGlobalProperty(const std::string& prop)
  438. {
  439. if ( prop == "CACHE_VARIABLES" )
  440. {
  441. std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
  442. this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";").c_str());
  443. }
  444. else if ( prop == "COMMANDS" )
  445. {
  446. std::vector<std::string> commands = this->GetCommandNames();
  447. this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str());
  448. }
  449. else if ( prop == "IN_TRY_COMPILE" )
  450. {
  451. this->SetGlobalProperty("IN_TRY_COMPILE",
  452. this->IsInTryCompile ? "1" : "0");
  453. }
  454. else if ( prop == "ENABLED_LANGUAGES" )
  455. {
  456. std::string langs;
  457. langs = cmJoin(this->EnabledLanguages, ";");
  458. this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
  459. }
  460. #define STRING_LIST_ELEMENT(F) ";" #F
  461. if (prop == "CMAKE_C_KNOWN_FEATURES")
  462. {
  463. return FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT) + 1;
  464. }
  465. if (prop == "CMAKE_CXX_KNOWN_FEATURES")
  466. {
  467. return FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT) + 1;
  468. }
  469. #undef STRING_LIST_ELEMENT
  470. return this->GlobalProperties.GetPropertyValue(prop);
  471. }
  472. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  473. {
  474. return cmSystemTools::IsOn(this->GetGlobalProperty(prop));
  475. }
  476. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  477. {
  478. this->SourceDirectory = sourceDirectory;
  479. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  480. cmSystemTools::SplitPath(
  481. cmSystemTools::CollapseFullPath(this->SourceDirectory),
  482. this->SourceDirectoryComponents);
  483. }
  484. const char* cmState::GetSourceDirectory() const
  485. {
  486. return this->SourceDirectory.c_str();
  487. }
  488. std::vector<std::string> const& cmState::GetSourceDirectoryComponents() const
  489. {
  490. return this->SourceDirectoryComponents;
  491. }
  492. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  493. {
  494. this->BinaryDirectory = binaryDirectory;
  495. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  496. cmSystemTools::SplitPath(
  497. cmSystemTools::CollapseFullPath(this->BinaryDirectory),
  498. this->BinaryDirectoryComponents);
  499. }
  500. void cmState::SetWindowsShell(bool windowsShell)
  501. {
  502. this->WindowsShell = windowsShell;
  503. }
  504. bool cmState::UseWindowsShell() const
  505. {
  506. return this->WindowsShell;
  507. }
  508. void cmState::SetWindowsVSIDE(bool windowsVSIDE)
  509. {
  510. this->WindowsVSIDE = windowsVSIDE;
  511. }
  512. bool cmState::UseWindowsVSIDE() const
  513. {
  514. return this->WindowsVSIDE;
  515. }
  516. void cmState::SetWatcomWMake(bool watcomWMake)
  517. {
  518. this->WatcomWMake = watcomWMake;
  519. }
  520. bool cmState::UseWatcomWMake() const
  521. {
  522. return this->WatcomWMake;
  523. }
  524. void cmState::SetMinGWMake(bool minGWMake)
  525. {
  526. this->MinGWMake = minGWMake;
  527. }
  528. bool cmState::UseMinGWMake() const
  529. {
  530. return this->MinGWMake;
  531. }
  532. void cmState::SetNMake(bool nMake)
  533. {
  534. this->NMake = nMake;
  535. }
  536. bool cmState::UseNMake() const
  537. {
  538. return this->NMake;
  539. }
  540. void cmState::SetMSYSShell(bool mSYSShell)
  541. {
  542. this->MSYSShell = mSYSShell;
  543. }
  544. bool cmState::UseMSYSShell() const
  545. {
  546. return this->MSYSShell;
  547. }
  548. const char* cmState::GetBinaryDirectory() const
  549. {
  550. return this->BinaryDirectory.c_str();
  551. }
  552. std::vector<std::string> const& cmState::GetBinaryDirectoryComponents() const
  553. {
  554. return this->BinaryDirectoryComponents;
  555. }
  556. void cmState::Directory::ComputeRelativePathTopSource()
  557. {
  558. // Relative path conversion inside the source tree is not used to
  559. // construct relative paths passed to build tools so it is safe to use
  560. // even when the source is a network path.
  561. cmState::Snapshot snapshot = this->Snapshot_;
  562. std::vector<cmState::Snapshot> snapshots;
  563. snapshots.push_back(snapshot);
  564. while (true)
  565. {
  566. snapshot = snapshot.GetBuildsystemDirectoryParent();
  567. if (snapshot.IsValid())
  568. {
  569. snapshots.push_back(snapshot);
  570. }
  571. else
  572. {
  573. break;
  574. }
  575. }
  576. std::string result = snapshots.front().GetDirectory().GetCurrentSource();
  577. for (std::vector<cmState::Snapshot>::const_iterator it =
  578. snapshots.begin() + 1; it != snapshots.end(); ++it)
  579. {
  580. std::string currentSource = it->GetDirectory().GetCurrentSource();
  581. if(cmSystemTools::IsSubDirectory(result, currentSource))
  582. {
  583. result = currentSource;
  584. }
  585. }
  586. this->DirectoryState->RelativePathTopSource = result;
  587. }
  588. void cmState::Directory::ComputeRelativePathTopBinary()
  589. {
  590. cmState::Snapshot snapshot = this->Snapshot_;
  591. std::vector<cmState::Snapshot> snapshots;
  592. snapshots.push_back(snapshot);
  593. while (true)
  594. {
  595. snapshot = snapshot.GetBuildsystemDirectoryParent();
  596. if (snapshot.IsValid())
  597. {
  598. snapshots.push_back(snapshot);
  599. }
  600. else
  601. {
  602. break;
  603. }
  604. }
  605. std::string result =
  606. snapshots.front().GetDirectory().GetCurrentBinary();
  607. for (std::vector<cmState::Snapshot>::const_iterator it =
  608. snapshots.begin() + 1; it != snapshots.end(); ++it)
  609. {
  610. std::string currentBinary = it->GetDirectory().GetCurrentBinary();
  611. if(cmSystemTools::IsSubDirectory(result, currentBinary))
  612. {
  613. result = currentBinary;
  614. }
  615. }
  616. // The current working directory on Windows cannot be a network
  617. // path. Therefore relative paths cannot work when the binary tree
  618. // is a network path.
  619. if(result.size() < 2 || result.substr(0, 2) != "//")
  620. {
  621. this->DirectoryState->RelativePathTopBinary = result;
  622. }
  623. else
  624. {
  625. this->DirectoryState->RelativePathTopBinary = "";
  626. }
  627. }
  628. cmState::Snapshot cmState::CreateBaseSnapshot()
  629. {
  630. PositionType pos = this->SnapshotData.Extend(this->SnapshotData.Root());
  631. pos->DirectoryParent = this->SnapshotData.Root();
  632. pos->SnapshotType = BuildsystemDirectoryType;
  633. pos->BuildSystemDirectory =
  634. this->BuildsystemDirectory.Extend(this->BuildsystemDirectory.Root());
  635. pos->ExecutionListFile =
  636. this->ExecutionListFiles.Extend(this->ExecutionListFiles.Root());
  637. pos->IncludeDirectoryPosition = 0;
  638. pos->CompileDefinitionsPosition = 0;
  639. pos->CompileOptionsPosition = 0;
  640. return cmState::Snapshot(this, pos);
  641. }
  642. cmState::Snapshot
  643. cmState::CreateBuildsystemDirectorySnapshot(Snapshot originSnapshot,
  644. std::string const& entryPointCommand,
  645. long entryPointLine)
  646. {
  647. assert(originSnapshot.IsValid());
  648. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position);
  649. pos->CallStackParent = originSnapshot.Position;
  650. pos->EntryPointLine = entryPointLine;
  651. pos->EntryPointCommand = entryPointCommand;
  652. pos->DirectoryParent = originSnapshot.Position;
  653. pos->SnapshotType = BuildsystemDirectoryType;
  654. pos->BuildSystemDirectory =
  655. this->BuildsystemDirectory.Extend(
  656. originSnapshot.Position->BuildSystemDirectory);
  657. pos->ExecutionListFile =
  658. this->ExecutionListFiles.Extend(
  659. originSnapshot.Position->ExecutionListFile);
  660. return cmState::Snapshot(this, pos);
  661. }
  662. cmState::Snapshot
  663. cmState::CreateFunctionCallSnapshot(cmState::Snapshot originSnapshot,
  664. std::string const& entryPointCommand,
  665. long entryPointLine,
  666. std::string const& fileName)
  667. {
  668. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
  669. *originSnapshot.Position);
  670. pos->CallStackParent = originSnapshot.Position;
  671. pos->EntryPointLine = entryPointLine;
  672. pos->EntryPointCommand = entryPointCommand;
  673. pos->SnapshotType = FunctionCallType;
  674. pos->ExecutionListFile = this->ExecutionListFiles.Extend(
  675. originSnapshot.Position->ExecutionListFile, fileName);
  676. return cmState::Snapshot(this, pos);
  677. }
  678. cmState::Snapshot
  679. cmState::CreateMacroCallSnapshot(cmState::Snapshot originSnapshot,
  680. std::string const& entryPointCommand,
  681. long entryPointLine,
  682. std::string const& fileName)
  683. {
  684. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
  685. *originSnapshot.Position);
  686. pos->CallStackParent = originSnapshot.Position;
  687. pos->EntryPointLine = entryPointLine;
  688. pos->EntryPointCommand = entryPointCommand;
  689. pos->SnapshotType = MacroCallType;
  690. pos->ExecutionListFile = this->ExecutionListFiles.Extend(
  691. originSnapshot.Position->ExecutionListFile, fileName);
  692. return cmState::Snapshot(this, pos);
  693. }
  694. cmState::Snapshot
  695. cmState::CreateCallStackSnapshot(cmState::Snapshot originSnapshot,
  696. const std::string& entryPointCommand,
  697. long entryPointLine,
  698. const std::string& fileName)
  699. {
  700. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
  701. *originSnapshot.Position);
  702. pos->CallStackParent = originSnapshot.Position;
  703. pos->EntryPointLine = entryPointLine;
  704. pos->EntryPointCommand = entryPointCommand;
  705. pos->SnapshotType = CallStackType;
  706. pos->ExecutionListFile = this->ExecutionListFiles.Extend(
  707. originSnapshot.Position->ExecutionListFile, fileName);
  708. return cmState::Snapshot(this, pos);
  709. }
  710. cmState::Snapshot
  711. cmState::CreateInlineListFileSnapshot(cmState::Snapshot originSnapshot,
  712. const std::string& entryPointCommand,
  713. long entryPointLine,
  714. const std::string& fileName)
  715. {
  716. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
  717. *originSnapshot.Position);
  718. pos->CallStackParent = originSnapshot.Position;
  719. pos->EntryPointLine = entryPointLine;
  720. pos->EntryPointCommand = entryPointCommand;
  721. pos->SnapshotType = InlineListFileType;
  722. pos->ExecutionListFile = this->ExecutionListFiles.Extend(
  723. originSnapshot.Position->ExecutionListFile, fileName);
  724. return cmState::Snapshot(this, pos);
  725. }
  726. cmState::Snapshot cmState::Pop(cmState::Snapshot originSnapshot)
  727. {
  728. PositionType pos = originSnapshot.Position;
  729. PositionType prevPos = pos;
  730. ++prevPos;
  731. prevPos->IncludeDirectoryPosition =
  732. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  733. prevPos->CompileDefinitionsPosition =
  734. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  735. prevPos->CompileOptionsPosition =
  736. prevPos->BuildSystemDirectory->CompileOptions.size();
  737. if (prevPos == this->SnapshotData.Root())
  738. {
  739. return Snapshot(this, prevPos);
  740. }
  741. return Snapshot(this, originSnapshot.Position->CallStackParent);
  742. }
  743. cmState::Snapshot::Snapshot(cmState* state)
  744. : State(state)
  745. , Position()
  746. {
  747. }
  748. cmState::Snapshot::Snapshot(cmState* state, PositionType position)
  749. : State(state),
  750. Position(position)
  751. {
  752. }
  753. const char* cmState::Directory::GetCurrentSource() const
  754. {
  755. return this->DirectoryState->Location.c_str();
  756. }
  757. void cmState::Directory::SetCurrentSource(std::string const& dir)
  758. {
  759. std::string& loc = this->DirectoryState->Location;
  760. loc = dir;
  761. cmSystemTools::ConvertToUnixSlashes(loc);
  762. loc = cmSystemTools::CollapseFullPath(loc);
  763. cmSystemTools::SplitPath(
  764. loc,
  765. this->DirectoryState->CurrentSourceDirectoryComponents);
  766. this->ComputeRelativePathTopSource();
  767. }
  768. const char* cmState::Directory::GetCurrentBinary() const
  769. {
  770. return this->DirectoryState->OutputLocation.c_str();
  771. }
  772. void cmState::Directory::SetCurrentBinary(std::string const& dir)
  773. {
  774. std::string& loc = this->DirectoryState->OutputLocation;
  775. loc = dir;
  776. cmSystemTools::ConvertToUnixSlashes(loc);
  777. loc = cmSystemTools::CollapseFullPath(loc);
  778. cmSystemTools::SplitPath(
  779. loc,
  780. this->DirectoryState->CurrentBinaryDirectoryComponents);
  781. this->ComputeRelativePathTopBinary();
  782. }
  783. void cmState::Snapshot::SetListFile(const std::string& listfile)
  784. {
  785. *this->Position->ExecutionListFile = listfile;
  786. }
  787. std::vector<std::string> const&
  788. cmState::Directory::GetCurrentSourceComponents() const
  789. {
  790. return this->DirectoryState->CurrentSourceDirectoryComponents;
  791. }
  792. std::vector<std::string> const&
  793. cmState::Directory::GetCurrentBinaryComponents() const
  794. {
  795. return this->DirectoryState->CurrentBinaryDirectoryComponents;
  796. }
  797. const char* cmState::Directory::GetRelativePathTopSource() const
  798. {
  799. return this->DirectoryState->RelativePathTopSource.c_str();
  800. }
  801. const char* cmState::Directory::GetRelativePathTopBinary() const
  802. {
  803. return this->DirectoryState->RelativePathTopBinary.c_str();
  804. }
  805. void cmState::Directory::SetRelativePathTopSource(const char* dir)
  806. {
  807. this->DirectoryState->RelativePathTopSource = dir;
  808. }
  809. void cmState::Directory::SetRelativePathTopBinary(const char* dir)
  810. {
  811. this->DirectoryState->RelativePathTopBinary = dir;
  812. }
  813. std::string cmState::Snapshot::GetExecutionListFile() const
  814. {
  815. return *this->Position->ExecutionListFile;
  816. }
  817. std::string cmState::Snapshot::GetEntryPointCommand() const
  818. {
  819. return this->Position->EntryPointCommand;
  820. }
  821. long cmState::Snapshot::GetEntryPointLine() const
  822. {
  823. return this->Position->EntryPointLine;
  824. }
  825. bool cmState::Snapshot::IsValid() const
  826. {
  827. return this->State && this->Position.IsValid()
  828. ? this->Position != this->State->SnapshotData.Root()
  829. : false;
  830. }
  831. cmState::Snapshot cmState::Snapshot::GetBuildsystemDirectoryParent() const
  832. {
  833. Snapshot snapshot;
  834. if (!this->State || this->Position == this->State->SnapshotData.Root())
  835. {
  836. return snapshot;
  837. }
  838. PositionType parentPos = this->Position->DirectoryParent;
  839. if (parentPos != this->State->SnapshotData.Root())
  840. {
  841. snapshot = Snapshot(this->State, parentPos);
  842. }
  843. return snapshot;
  844. }
  845. cmState::Snapshot cmState::Snapshot::GetCallStackParent() const
  846. {
  847. assert(this->State);
  848. assert(this->Position != this->State->SnapshotData.Root());
  849. Snapshot snapshot;
  850. if (this->Position->SnapshotType == cmState::BuildsystemDirectoryType)
  851. {
  852. return snapshot;
  853. }
  854. PositionType parentPos = this->Position;
  855. ++parentPos;
  856. if (parentPos == this->State->SnapshotData.Root())
  857. {
  858. return snapshot;
  859. }
  860. snapshot = Snapshot(this->State, parentPos);
  861. return snapshot;
  862. }
  863. static const std::string cmPropertySentinal = std::string();
  864. template<typename T, typename U, typename V>
  865. void InitializeContentFromParent(T& parentContent,
  866. T& thisContent,
  867. U& parentBacktraces,
  868. U& thisBacktraces,
  869. V& contentEndPosition)
  870. {
  871. std::vector<std::string>::const_iterator parentBegin =
  872. parentContent.begin();
  873. std::vector<std::string>::const_iterator parentEnd =
  874. parentContent.end();
  875. std::vector<std::string>::const_reverse_iterator parentRbegin =
  876. cmMakeReverseIterator(parentEnd);
  877. std::vector<std::string>::const_reverse_iterator parentRend =
  878. parentContent.rend();
  879. parentRbegin = std::find(parentRbegin, parentRend, cmPropertySentinal);
  880. std::vector<std::string>::const_iterator parentIt = parentRbegin.base();
  881. thisContent = std::vector<std::string>(parentIt, parentEnd);
  882. std::vector<cmListFileBacktrace>::const_iterator btIt =
  883. parentBacktraces.begin() + std::distance(parentBegin, parentIt);
  884. std::vector<cmListFileBacktrace>::const_iterator btEnd =
  885. parentBacktraces.end();
  886. thisBacktraces = std::vector<cmListFileBacktrace>(btIt, btEnd);
  887. contentEndPosition = thisContent.size();
  888. }
  889. void cmState::Snapshot::InitializeFromParent()
  890. {
  891. PositionType parent = this->Position->DirectoryParent;
  892. InitializeContentFromParent(parent->BuildSystemDirectory->IncludeDirectories,
  893. this->Position->BuildSystemDirectory->IncludeDirectories,
  894. parent->BuildSystemDirectory->IncludeDirectoryBacktraces,
  895. this->Position->BuildSystemDirectory->IncludeDirectoryBacktraces,
  896. this->Position->IncludeDirectoryPosition);
  897. InitializeContentFromParent(parent->BuildSystemDirectory->CompileDefinitions,
  898. this->Position->BuildSystemDirectory->CompileDefinitions,
  899. parent->BuildSystemDirectory->CompileDefinitionsBacktraces,
  900. this->Position->BuildSystemDirectory->CompileDefinitionsBacktraces,
  901. this->Position->CompileDefinitionsPosition);
  902. InitializeContentFromParent(parent->BuildSystemDirectory->CompileOptions,
  903. this->Position->BuildSystemDirectory->CompileOptions,
  904. parent->BuildSystemDirectory->CompileOptionsBacktraces,
  905. this->Position->BuildSystemDirectory->CompileOptionsBacktraces,
  906. this->Position->CompileOptionsPosition);
  907. }
  908. cmState* cmState::Snapshot::GetState() const
  909. {
  910. return this->State;
  911. }
  912. cmState::Directory cmState::Snapshot::GetDirectory() const
  913. {
  914. return Directory(this->Position->BuildSystemDirectory, *this);
  915. }
  916. cmState::Directory::Directory(
  917. cmLinkedTree<BuildsystemDirectoryStateType>::iterator iter,
  918. const cmState::Snapshot& snapshot)
  919. : DirectoryState(iter), Snapshot_(snapshot)
  920. {
  921. }
  922. template <typename T, typename U>
  923. cmStringRange GetPropertyContent(T const& content, U contentEndPosition)
  924. {
  925. std::vector<std::string>::const_iterator end =
  926. content.begin() + contentEndPosition;
  927. std::vector<std::string>::const_reverse_iterator rbegin =
  928. cmMakeReverseIterator(end);
  929. rbegin = std::find(rbegin, content.rend(), cmPropertySentinal);
  930. return cmMakeRange(rbegin.base(), end);
  931. }
  932. template <typename T, typename U, typename V>
  933. cmBacktraceRange GetPropertyBacktraces(T const& content,
  934. U const& backtraces,
  935. V contentEndPosition)
  936. {
  937. std::vector<std::string>::const_iterator entryEnd =
  938. content.begin() + contentEndPosition;
  939. std::vector<std::string>::const_reverse_iterator rbegin =
  940. cmMakeReverseIterator(entryEnd);
  941. rbegin = std::find(rbegin, content.rend(), cmPropertySentinal);
  942. std::vector<cmListFileBacktrace>::const_iterator it =
  943. backtraces.begin() + std::distance(content.begin(), rbegin.base());
  944. std::vector<cmListFileBacktrace>::const_iterator end = backtraces.end();
  945. return cmMakeRange(it, end);
  946. }
  947. template <typename T, typename U, typename V>
  948. void AppendEntry(T& content, U& backtraces, V& endContentPosition,
  949. const std::string& vec, const cmListFileBacktrace& lfbt)
  950. {
  951. assert(endContentPosition == content.size());
  952. content.push_back(vec);
  953. backtraces.push_back(lfbt);
  954. endContentPosition = content.size();
  955. }
  956. template <typename T, typename U, typename V>
  957. void SetContent(T& content, U& backtraces, V& endContentPosition,
  958. const std::string& vec, const cmListFileBacktrace& lfbt)
  959. {
  960. assert(endContentPosition == content.size());
  961. content.resize(content.size() + 2);
  962. backtraces.resize(backtraces.size() + 2);
  963. content.back() = vec;
  964. backtraces.back() = lfbt;
  965. endContentPosition = content.size();
  966. }
  967. template <typename T, typename U, typename V>
  968. void ClearContent(T& content, U& backtraces, V& endContentPosition)
  969. {
  970. assert(endContentPosition == content.size());
  971. content.resize(content.size() + 1);
  972. backtraces.resize(backtraces.size() + 1);
  973. endContentPosition = content.size();
  974. }
  975. cmStringRange
  976. cmState::Directory::GetIncludeDirectoriesEntries() const
  977. {
  978. return GetPropertyContent(this->DirectoryState->IncludeDirectories,
  979. this->Snapshot_.Position->IncludeDirectoryPosition);
  980. }
  981. cmBacktraceRange
  982. cmState::Directory::GetIncludeDirectoriesEntryBacktraces() const
  983. {
  984. return GetPropertyBacktraces(this->DirectoryState->IncludeDirectories,
  985. this->DirectoryState->IncludeDirectoryBacktraces,
  986. this->Snapshot_.Position->IncludeDirectoryPosition);
  987. }
  988. void cmState::Directory::AppendIncludeDirectoriesEntry(
  989. const std::string& vec, const cmListFileBacktrace& lfbt)
  990. {
  991. AppendEntry(this->DirectoryState->IncludeDirectories,
  992. this->DirectoryState->IncludeDirectoryBacktraces,
  993. this->Snapshot_.Position->IncludeDirectoryPosition,
  994. vec, lfbt);
  995. }
  996. void cmState::Directory::PrependIncludeDirectoriesEntry(
  997. const std::string& vec, const cmListFileBacktrace& lfbt)
  998. {
  999. std::vector<std::string>::iterator entryEnd =
  1000. this->DirectoryState->IncludeDirectories.begin()
  1001. + this->Snapshot_.Position->IncludeDirectoryPosition;
  1002. std::vector<std::string>::const_reverse_iterator rend =
  1003. this->DirectoryState->IncludeDirectories.rend();
  1004. std::vector<std::string>::reverse_iterator rbegin =
  1005. cmMakeReverseIterator(entryEnd);
  1006. std::vector<std::string>::const_reverse_iterator crbegin = rbegin;
  1007. crbegin = std::find(crbegin, rend, cmPropertySentinal);
  1008. std::vector<std::string>::const_iterator entryIt = crbegin.base();
  1009. std::vector<std::string>::const_iterator entryBegin =
  1010. this->DirectoryState->IncludeDirectories.begin();
  1011. std::vector<cmListFileBacktrace>::iterator btIt =
  1012. this->DirectoryState->IncludeDirectoryBacktraces.begin()
  1013. + std::distance(entryBegin, entryIt);
  1014. this->DirectoryState->IncludeDirectories.insert(rbegin.base(), vec);
  1015. this->DirectoryState->IncludeDirectoryBacktraces.insert(btIt, lfbt);
  1016. this->Snapshot_.Position->IncludeDirectoryPosition =
  1017. this->DirectoryState->IncludeDirectories.size();
  1018. }
  1019. void cmState::Directory::SetIncludeDirectories(
  1020. const std::string& vec, const cmListFileBacktrace& lfbt)
  1021. {
  1022. SetContent(this->DirectoryState->IncludeDirectories,
  1023. this->DirectoryState->IncludeDirectoryBacktraces,
  1024. this->Snapshot_.Position->IncludeDirectoryPosition,
  1025. vec, lfbt);
  1026. }
  1027. void cmState::Directory::ClearIncludeDirectories()
  1028. {
  1029. ClearContent(this->DirectoryState->IncludeDirectories,
  1030. this->DirectoryState->IncludeDirectoryBacktraces,
  1031. this->Snapshot_.Position->IncludeDirectoryPosition);
  1032. }
  1033. cmStringRange cmState::Directory::GetCompileDefinitionsEntries() const
  1034. {
  1035. return GetPropertyContent(this->DirectoryState->CompileDefinitions,
  1036. this->Snapshot_.Position->CompileDefinitionsPosition);
  1037. }
  1038. cmBacktraceRange
  1039. cmState::Directory::GetCompileDefinitionsEntryBacktraces() const
  1040. {
  1041. return GetPropertyBacktraces(this->DirectoryState->CompileDefinitions,
  1042. this->DirectoryState->CompileDefinitionsBacktraces,
  1043. this->Snapshot_.Position->CompileDefinitionsPosition);
  1044. }
  1045. void cmState::Directory::AppendCompileDefinitionsEntry(const std::string& vec,
  1046. const cmListFileBacktrace& lfbt)
  1047. {
  1048. AppendEntry(this->DirectoryState->CompileDefinitions,
  1049. this->DirectoryState->CompileDefinitionsBacktraces,
  1050. this->Snapshot_.Position->CompileDefinitionsPosition,
  1051. vec, lfbt);
  1052. }
  1053. void cmState::Directory::SetCompileDefinitions(const std::string& vec,
  1054. const cmListFileBacktrace& lfbt)
  1055. {
  1056. SetContent(this->DirectoryState->CompileDefinitions,
  1057. this->DirectoryState->CompileDefinitionsBacktraces,
  1058. this->Snapshot_.Position->CompileDefinitionsPosition,
  1059. vec, lfbt);
  1060. }
  1061. void cmState::Directory::ClearCompileDefinitions()
  1062. {
  1063. ClearContent(this->DirectoryState->CompileDefinitions,
  1064. this->DirectoryState->CompileDefinitionsBacktraces,
  1065. this->Snapshot_.Position->CompileDefinitionsPosition);
  1066. }
  1067. cmStringRange cmState::Directory::GetCompileOptionsEntries() const
  1068. {
  1069. return GetPropertyContent(this->DirectoryState->CompileOptions,
  1070. this->Snapshot_.Position->CompileOptionsPosition);
  1071. }
  1072. cmBacktraceRange cmState::Directory::GetCompileOptionsEntryBacktraces() const
  1073. {
  1074. return GetPropertyBacktraces(this->DirectoryState->CompileOptions,
  1075. this->DirectoryState->CompileOptionsBacktraces,
  1076. this->Snapshot_.Position->CompileOptionsPosition);
  1077. }
  1078. void
  1079. cmState::Directory::AppendCompileOptionsEntry(const std::string& vec,
  1080. const cmListFileBacktrace& lfbt)
  1081. {
  1082. AppendEntry(this->DirectoryState->CompileOptions,
  1083. this->DirectoryState->CompileOptionsBacktraces,
  1084. this->Snapshot_.Position->CompileOptionsPosition,
  1085. vec, lfbt);
  1086. }
  1087. void cmState::Directory::SetCompileOptions(const std::string& vec,
  1088. const cmListFileBacktrace& lfbt)
  1089. {
  1090. SetContent(this->DirectoryState->CompileOptions,
  1091. this->DirectoryState->CompileOptionsBacktraces,
  1092. this->Snapshot_.Position->CompileOptionsPosition,
  1093. vec, lfbt);
  1094. }
  1095. void cmState::Directory::ClearCompileOptions()
  1096. {
  1097. ClearContent(this->DirectoryState->CompileOptions,
  1098. this->DirectoryState->CompileOptionsBacktraces,
  1099. this->Snapshot_.Position->CompileOptionsPosition);
  1100. }