cmState.cxx 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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 "cmDefinitions.h"
  16. #include <assert.h>
  17. struct cmState::SnapshotDataType
  18. {
  19. cmState::PositionType ScopeParent;
  20. cmState::PositionType DirectoryParent;
  21. cmLinkedTree<cmState::PolicyStackEntry>::iterator Policies;
  22. cmLinkedTree<cmState::PolicyStackEntry>::iterator PolicyRoot;
  23. cmLinkedTree<cmState::PolicyStackEntry>::iterator PolicyScope;
  24. cmState::SnapshotType SnapshotType;
  25. cmLinkedTree<std::string>::iterator ExecutionListFile;
  26. cmLinkedTree<cmState::BuildsystemDirectoryStateType>::iterator
  27. BuildSystemDirectory;
  28. cmLinkedTree<cmDefinitions>::iterator Vars;
  29. cmLinkedTree<cmDefinitions>::iterator Root;
  30. cmLinkedTree<cmDefinitions>::iterator Parent;
  31. std::string EntryPointCommand;
  32. long EntryPointLine;
  33. std::vector<std::string>::size_type IncludeDirectoryPosition;
  34. std::vector<std::string>::size_type CompileDefinitionsPosition;
  35. std::vector<std::string>::size_type CompileOptionsPosition;
  36. };
  37. struct cmState::PolicyStackEntry: public cmPolicies::PolicyMap
  38. {
  39. typedef cmPolicies::PolicyMap derived;
  40. PolicyStackEntry(bool w = false): derived(), Weak(w) {}
  41. PolicyStackEntry(derived const& d, bool w): derived(d), Weak(w) {}
  42. PolicyStackEntry(PolicyStackEntry const& r): derived(r), Weak(r.Weak) {}
  43. bool Weak;
  44. };
  45. struct cmState::BuildsystemDirectoryStateType
  46. {
  47. cmState::PositionType DirectoryEnd;
  48. std::string Location;
  49. std::string OutputLocation;
  50. std::vector<std::string> CurrentSourceDirectoryComponents;
  51. std::vector<std::string> CurrentBinaryDirectoryComponents;
  52. // The top-most directories for relative path conversion. Both the
  53. // source and destination location of a relative path conversion
  54. // must be underneath one of these directories (both under source or
  55. // both under binary) in order for the relative path to be evaluated
  56. // safely by the build tools.
  57. std::string RelativePathTopSource;
  58. std::string RelativePathTopBinary;
  59. std::vector<std::string> IncludeDirectories;
  60. std::vector<cmListFileBacktrace> IncludeDirectoryBacktraces;
  61. std::vector<std::string> CompileDefinitions;
  62. std::vector<cmListFileBacktrace> CompileDefinitionsBacktraces;
  63. std::vector<std::string> CompileOptions;
  64. std::vector<cmListFileBacktrace> CompileOptionsBacktraces;
  65. cmPropertyMap Properties;
  66. };
  67. cmState::cmState(cmake* cm)
  68. : CMakeInstance(cm),
  69. IsInTryCompile(false),
  70. WindowsShell(false),
  71. WindowsVSIDE(false),
  72. WatcomWMake(false),
  73. MinGWMake(false),
  74. NMake(false),
  75. MSYSShell(false)
  76. {
  77. }
  78. cmState::~cmState()
  79. {
  80. cmDeleteAll(this->Commands);
  81. }
  82. const char* cmCacheEntryTypes[] =
  83. { "BOOL",
  84. "PATH",
  85. "FILEPATH",
  86. "STRING",
  87. "INTERNAL",
  88. "STATIC",
  89. "UNINITIALIZED",
  90. 0
  91. };
  92. const char*
  93. cmState::CacheEntryTypeToString(cmState::CacheEntryType type)
  94. {
  95. if ( type > 6 )
  96. {
  97. return cmCacheEntryTypes[6];
  98. }
  99. return cmCacheEntryTypes[type];
  100. }
  101. cmState::CacheEntryType
  102. cmState::StringToCacheEntryType(const char* s)
  103. {
  104. int i = 0;
  105. while(cmCacheEntryTypes[i])
  106. {
  107. if(strcmp(s, cmCacheEntryTypes[i]) == 0)
  108. {
  109. return static_cast<cmState::CacheEntryType>(i);
  110. }
  111. ++i;
  112. }
  113. return STRING;
  114. }
  115. bool cmState::IsCacheEntryType(std::string const& key)
  116. {
  117. for(int i=0; cmCacheEntryTypes[i]; ++i)
  118. {
  119. if(strcmp(key.c_str(), cmCacheEntryTypes[i]) == 0)
  120. {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. std::vector<std::string> cmState::GetCacheEntryKeys() const
  127. {
  128. std::vector<std::string> definitions;
  129. definitions.reserve(this->CMakeInstance->GetCacheManager()->GetSize());
  130. cmCacheManager::CacheIterator cit =
  131. this->CMakeInstance->GetCacheManager()->GetCacheIterator();
  132. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  133. {
  134. definitions.push_back(cit.GetName());
  135. }
  136. return definitions;
  137. }
  138. const char* cmState::GetCacheEntryValue(std::string const& key) const
  139. {
  140. cmCacheManager::CacheEntry* e = this->CMakeInstance->GetCacheManager()
  141. ->GetCacheEntry(key);
  142. if (!e)
  143. {
  144. return 0;
  145. }
  146. return e->Value.c_str();
  147. }
  148. const char*
  149. cmState::GetInitializedCacheValue(std::string const& key) const
  150. {
  151. return this->CMakeInstance->GetCacheManager()->GetInitializedCacheValue(key);
  152. }
  153. cmState::CacheEntryType
  154. cmState::GetCacheEntryType(std::string const& key) const
  155. {
  156. cmCacheManager::CacheIterator it =
  157. this->CMakeInstance->GetCacheManager()->GetCacheIterator(key.c_str());
  158. return it.GetType();
  159. }
  160. void cmState::SetCacheEntryValue(std::string const& key,
  161. std::string const& value)
  162. {
  163. this->CMakeInstance->GetCacheManager()->SetCacheEntryValue(key, value);
  164. }
  165. void cmState::SetCacheEntryProperty(std::string const& key,
  166. std::string const& propertyName,
  167. std::string const& value)
  168. {
  169. cmCacheManager::CacheIterator it =
  170. this->CMakeInstance->GetCacheManager()->GetCacheIterator(key.c_str());
  171. it.SetProperty(propertyName, value.c_str());
  172. }
  173. void cmState::SetCacheEntryBoolProperty(std::string const& key,
  174. std::string const& propertyName,
  175. bool value)
  176. {
  177. cmCacheManager::CacheIterator it =
  178. this->CMakeInstance->GetCacheManager()->GetCacheIterator(key.c_str());
  179. it.SetProperty(propertyName, value);
  180. }
  181. const char* cmState::GetCacheEntryProperty(std::string const& key,
  182. std::string const& propertyName)
  183. {
  184. cmCacheManager::CacheIterator it = this->CMakeInstance->GetCacheManager()
  185. ->GetCacheIterator(key.c_str());
  186. if (!it.PropertyExists(propertyName))
  187. {
  188. return 0;
  189. }
  190. return it.GetProperty(propertyName);
  191. }
  192. bool cmState::GetCacheEntryPropertyAsBool(std::string const& key,
  193. std::string const& propertyName)
  194. {
  195. return this->CMakeInstance->GetCacheManager()
  196. ->GetCacheIterator(key.c_str()).GetPropertyAsBool(propertyName);
  197. }
  198. void cmState::AddCacheEntry(const std::string& key, const char* value,
  199. const char* helpString,
  200. cmState::CacheEntryType type)
  201. {
  202. this->CMakeInstance->GetCacheManager()->AddCacheEntry(key, value,
  203. helpString, type);
  204. }
  205. void cmState::RemoveCacheEntry(std::string const& key)
  206. {
  207. this->CMakeInstance->GetCacheManager()->RemoveCacheEntry(key);
  208. }
  209. void cmState::AppendCacheEntryProperty(const std::string& key,
  210. const std::string& property,
  211. const std::string& value,
  212. bool asString)
  213. {
  214. this->CMakeInstance->GetCacheManager()
  215. ->GetCacheIterator(key.c_str()).AppendProperty(property,
  216. value.c_str(),
  217. asString);
  218. }
  219. void cmState::RemoveCacheEntryProperty(std::string const& key,
  220. std::string const& propertyName)
  221. {
  222. this->CMakeInstance->GetCacheManager()
  223. ->GetCacheIterator(key.c_str()).SetProperty(propertyName, (void*)0);
  224. }
  225. cmState::Snapshot cmState::Reset()
  226. {
  227. this->GlobalProperties.clear();
  228. this->PropertyDefinitions.clear();
  229. PositionType pos = this->SnapshotData.Truncate();
  230. this->ExecutionListFiles.Truncate();
  231. {
  232. cmLinkedTree<BuildsystemDirectoryStateType>::iterator it =
  233. this->BuildsystemDirectory.Truncate();
  234. it->IncludeDirectories.clear();
  235. it->IncludeDirectoryBacktraces.clear();
  236. it->CompileDefinitions.clear();
  237. it->CompileDefinitionsBacktraces.clear();
  238. it->CompileOptions.clear();
  239. it->CompileOptionsBacktraces.clear();
  240. it->DirectoryEnd = pos;
  241. it->Properties.clear();
  242. }
  243. this->PolicyStack.Clear();
  244. pos->Policies = this->PolicyStack.Root();
  245. pos->PolicyRoot = this->PolicyStack.Root();
  246. pos->PolicyScope = this->PolicyStack.Root();
  247. assert(pos->Policies.IsValid());
  248. assert(pos->PolicyRoot.IsValid());
  249. this->VarTree.Clear();
  250. pos->Vars = this->VarTree.Extend(this->VarTree.Root());
  251. pos->Parent = this->VarTree.Root();
  252. pos->Root = this->VarTree.Root();
  253. this->DefineProperty
  254. ("RULE_LAUNCH_COMPILE", cmProperty::DIRECTORY,
  255. "", "", true);
  256. this->DefineProperty
  257. ("RULE_LAUNCH_LINK", cmProperty::DIRECTORY,
  258. "", "", true);
  259. this->DefineProperty
  260. ("RULE_LAUNCH_CUSTOM", cmProperty::DIRECTORY,
  261. "", "", true);
  262. this->DefineProperty
  263. ("RULE_LAUNCH_COMPILE", cmProperty::TARGET,
  264. "", "", true);
  265. this->DefineProperty
  266. ("RULE_LAUNCH_LINK", cmProperty::TARGET,
  267. "", "", true);
  268. this->DefineProperty
  269. ("RULE_LAUNCH_CUSTOM", cmProperty::TARGET,
  270. "", "", true);
  271. return Snapshot(this, pos);
  272. }
  273. void cmState::DefineProperty(const std::string& name,
  274. cmProperty::ScopeType scope,
  275. const char *ShortDescription,
  276. const char *FullDescription,
  277. bool chained)
  278. {
  279. this->PropertyDefinitions[scope].DefineProperty(name,scope,ShortDescription,
  280. FullDescription,
  281. chained);
  282. }
  283. cmPropertyDefinition const* cmState
  284. ::GetPropertyDefinition(const std::string& name,
  285. cmProperty::ScopeType scope) const
  286. {
  287. if (this->IsPropertyDefined(name,scope))
  288. {
  289. cmPropertyDefinitionMap const& defs =
  290. this->PropertyDefinitions.find(scope)->second;
  291. return &defs.find(name)->second;
  292. }
  293. return 0;
  294. }
  295. bool cmState::IsPropertyDefined(const std::string& name,
  296. cmProperty::ScopeType scope) const
  297. {
  298. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it
  299. = this->PropertyDefinitions.find(scope);
  300. if (it == this->PropertyDefinitions.end())
  301. {
  302. return false;
  303. }
  304. return it->second.IsPropertyDefined(name);
  305. }
  306. bool cmState::IsPropertyChained(const std::string& name,
  307. cmProperty::ScopeType scope) const
  308. {
  309. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it
  310. = this->PropertyDefinitions.find(scope);
  311. if (it == this->PropertyDefinitions.end())
  312. {
  313. return false;
  314. }
  315. return it->second.IsPropertyChained(name);
  316. }
  317. void cmState::SetLanguageEnabled(std::string const& l)
  318. {
  319. std::vector<std::string>::iterator it =
  320. std::lower_bound(this->EnabledLanguages.begin(),
  321. this->EnabledLanguages.end(), l);
  322. if (it == this->EnabledLanguages.end() || *it != l)
  323. {
  324. this->EnabledLanguages.insert(it, l);
  325. }
  326. }
  327. bool cmState::GetLanguageEnabled(std::string const& l) const
  328. {
  329. return std::binary_search(this->EnabledLanguages.begin(),
  330. this->EnabledLanguages.end(), l);
  331. }
  332. std::vector<std::string> cmState::GetEnabledLanguages() const
  333. {
  334. return this->EnabledLanguages;
  335. }
  336. void cmState::SetEnabledLanguages(std::vector<std::string> const& langs)
  337. {
  338. this->EnabledLanguages = langs;
  339. }
  340. void cmState::ClearEnabledLanguages()
  341. {
  342. this->EnabledLanguages.clear();
  343. }
  344. bool cmState::GetIsInTryCompile() const
  345. {
  346. return this->IsInTryCompile;
  347. }
  348. void cmState::SetIsInTryCompile(bool b)
  349. {
  350. this->IsInTryCompile = b;
  351. }
  352. void cmState::RenameCommand(std::string const& oldName,
  353. std::string const& newName)
  354. {
  355. // if the command already exists, free the old one
  356. std::string sOldName = cmSystemTools::LowerCase(oldName);
  357. std::string sNewName = cmSystemTools::LowerCase(newName);
  358. std::map<std::string, cmCommand*>::iterator pos =
  359. this->Commands.find(sOldName);
  360. if ( pos == this->Commands.end() )
  361. {
  362. return;
  363. }
  364. cmCommand* cmd = pos->second;
  365. pos = this->Commands.find(sNewName);
  366. if (pos != this->Commands.end())
  367. {
  368. delete pos->second;
  369. this->Commands.erase(pos);
  370. }
  371. this->Commands.insert(std::make_pair(sNewName, cmd));
  372. pos = this->Commands.find(sOldName);
  373. this->Commands.erase(pos);
  374. }
  375. void cmState::AddCommand(cmCommand* command)
  376. {
  377. std::string name = cmSystemTools::LowerCase(command->GetName());
  378. // if the command already exists, free the old one
  379. std::map<std::string, cmCommand*>::iterator pos = this->Commands.find(name);
  380. if (pos != this->Commands.end())
  381. {
  382. delete pos->second;
  383. this->Commands.erase(pos);
  384. }
  385. this->Commands.insert(std::make_pair(name, command));
  386. }
  387. void cmState::RemoveUnscriptableCommands()
  388. {
  389. std::vector<std::string> unscriptableCommands;
  390. for (std::map<std::string, cmCommand*>::iterator
  391. pos = this->Commands.begin();
  392. pos != this->Commands.end(); )
  393. {
  394. if (!pos->second->IsScriptable())
  395. {
  396. delete pos->second;
  397. this->Commands.erase(pos++);
  398. }
  399. else
  400. {
  401. ++pos;
  402. }
  403. }
  404. }
  405. cmCommand* cmState::GetCommand(std::string const& name) const
  406. {
  407. cmCommand* command = 0;
  408. std::string sName = cmSystemTools::LowerCase(name);
  409. std::map<std::string, cmCommand*>::const_iterator pos =
  410. this->Commands.find(sName);
  411. if (pos != this->Commands.end())
  412. {
  413. command = (*pos).second;
  414. }
  415. return command;
  416. }
  417. std::vector<std::string> cmState::GetCommandNames() const
  418. {
  419. std::vector<std::string> commandNames;
  420. commandNames.reserve(this->Commands.size());
  421. std::map<std::string, cmCommand*>::const_iterator cmds
  422. = this->Commands.begin();
  423. for ( ; cmds != this->Commands.end(); ++ cmds )
  424. {
  425. commandNames.push_back(cmds->first);
  426. }
  427. return commandNames;
  428. }
  429. void cmState::RemoveUserDefinedCommands()
  430. {
  431. std::vector<cmCommand*> renamedCommands;
  432. for(std::map<std::string, cmCommand*>::iterator j = this->Commands.begin();
  433. j != this->Commands.end(); )
  434. {
  435. if (j->second->IsA("cmMacroHelperCommand") ||
  436. j->second->IsA("cmFunctionHelperCommand"))
  437. {
  438. delete j->second;
  439. this->Commands.erase(j++);
  440. }
  441. else if (j->first != j->second->GetName())
  442. {
  443. renamedCommands.push_back(j->second);
  444. this->Commands.erase(j++);
  445. }
  446. else
  447. {
  448. ++j;
  449. }
  450. }
  451. for (std::vector<cmCommand*>::const_iterator it = renamedCommands.begin();
  452. it != renamedCommands.end(); ++it)
  453. {
  454. this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
  455. }
  456. }
  457. void cmState::SetGlobalProperty(const std::string& prop, const char* value)
  458. {
  459. this->GlobalProperties.SetProperty(prop, value);
  460. }
  461. void cmState::AppendGlobalProperty(const std::string& prop,
  462. const char* value, bool asString)
  463. {
  464. this->GlobalProperties.AppendProperty(prop, value, asString);
  465. }
  466. const char *cmState::GetGlobalProperty(const std::string& prop)
  467. {
  468. if ( prop == "CACHE_VARIABLES" )
  469. {
  470. std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
  471. this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";").c_str());
  472. }
  473. else if ( prop == "COMMANDS" )
  474. {
  475. std::vector<std::string> commands = this->GetCommandNames();
  476. this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str());
  477. }
  478. else if ( prop == "IN_TRY_COMPILE" )
  479. {
  480. this->SetGlobalProperty("IN_TRY_COMPILE",
  481. this->IsInTryCompile ? "1" : "0");
  482. }
  483. else if ( prop == "ENABLED_LANGUAGES" )
  484. {
  485. std::string langs;
  486. langs = cmJoin(this->EnabledLanguages, ";");
  487. this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
  488. }
  489. #define STRING_LIST_ELEMENT(F) ";" #F
  490. if (prop == "CMAKE_C_KNOWN_FEATURES")
  491. {
  492. return FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT) + 1;
  493. }
  494. if (prop == "CMAKE_CXX_KNOWN_FEATURES")
  495. {
  496. return FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT) + 1;
  497. }
  498. #undef STRING_LIST_ELEMENT
  499. return this->GlobalProperties.GetPropertyValue(prop);
  500. }
  501. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  502. {
  503. return cmSystemTools::IsOn(this->GetGlobalProperty(prop));
  504. }
  505. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  506. {
  507. this->SourceDirectory = sourceDirectory;
  508. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  509. cmSystemTools::SplitPath(
  510. cmSystemTools::CollapseFullPath(this->SourceDirectory),
  511. this->SourceDirectoryComponents);
  512. }
  513. const char* cmState::GetSourceDirectory() const
  514. {
  515. return this->SourceDirectory.c_str();
  516. }
  517. std::vector<std::string> const& cmState::GetSourceDirectoryComponents() const
  518. {
  519. return this->SourceDirectoryComponents;
  520. }
  521. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  522. {
  523. this->BinaryDirectory = binaryDirectory;
  524. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  525. cmSystemTools::SplitPath(
  526. cmSystemTools::CollapseFullPath(this->BinaryDirectory),
  527. this->BinaryDirectoryComponents);
  528. }
  529. void cmState::SetWindowsShell(bool windowsShell)
  530. {
  531. this->WindowsShell = windowsShell;
  532. }
  533. bool cmState::UseWindowsShell() const
  534. {
  535. return this->WindowsShell;
  536. }
  537. void cmState::SetWindowsVSIDE(bool windowsVSIDE)
  538. {
  539. this->WindowsVSIDE = windowsVSIDE;
  540. }
  541. bool cmState::UseWindowsVSIDE() const
  542. {
  543. return this->WindowsVSIDE;
  544. }
  545. void cmState::SetWatcomWMake(bool watcomWMake)
  546. {
  547. this->WatcomWMake = watcomWMake;
  548. }
  549. bool cmState::UseWatcomWMake() const
  550. {
  551. return this->WatcomWMake;
  552. }
  553. void cmState::SetMinGWMake(bool minGWMake)
  554. {
  555. this->MinGWMake = minGWMake;
  556. }
  557. bool cmState::UseMinGWMake() const
  558. {
  559. return this->MinGWMake;
  560. }
  561. void cmState::SetNMake(bool nMake)
  562. {
  563. this->NMake = nMake;
  564. }
  565. bool cmState::UseNMake() const
  566. {
  567. return this->NMake;
  568. }
  569. void cmState::SetMSYSShell(bool mSYSShell)
  570. {
  571. this->MSYSShell = mSYSShell;
  572. }
  573. bool cmState::UseMSYSShell() const
  574. {
  575. return this->MSYSShell;
  576. }
  577. const char* cmState::GetBinaryDirectory() const
  578. {
  579. return this->BinaryDirectory.c_str();
  580. }
  581. std::vector<std::string> const& cmState::GetBinaryDirectoryComponents() const
  582. {
  583. return this->BinaryDirectoryComponents;
  584. }
  585. void cmState::Directory::ComputeRelativePathTopSource()
  586. {
  587. // Relative path conversion inside the source tree is not used to
  588. // construct relative paths passed to build tools so it is safe to use
  589. // even when the source is a network path.
  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 = snapshots.front().GetDirectory().GetCurrentSource();
  606. for (std::vector<cmState::Snapshot>::const_iterator it =
  607. snapshots.begin() + 1; it != snapshots.end(); ++it)
  608. {
  609. std::string currentSource = it->GetDirectory().GetCurrentSource();
  610. if(cmSystemTools::IsSubDirectory(result, currentSource))
  611. {
  612. result = currentSource;
  613. }
  614. }
  615. this->DirectoryState->RelativePathTopSource = result;
  616. }
  617. void cmState::Directory::ComputeRelativePathTopBinary()
  618. {
  619. cmState::Snapshot snapshot = this->Snapshot_;
  620. std::vector<cmState::Snapshot> snapshots;
  621. snapshots.push_back(snapshot);
  622. while (true)
  623. {
  624. snapshot = snapshot.GetBuildsystemDirectoryParent();
  625. if (snapshot.IsValid())
  626. {
  627. snapshots.push_back(snapshot);
  628. }
  629. else
  630. {
  631. break;
  632. }
  633. }
  634. std::string result =
  635. snapshots.front().GetDirectory().GetCurrentBinary();
  636. for (std::vector<cmState::Snapshot>::const_iterator it =
  637. snapshots.begin() + 1; it != snapshots.end(); ++it)
  638. {
  639. std::string currentBinary = it->GetDirectory().GetCurrentBinary();
  640. if(cmSystemTools::IsSubDirectory(result, currentBinary))
  641. {
  642. result = currentBinary;
  643. }
  644. }
  645. // The current working directory on Windows cannot be a network
  646. // path. Therefore relative paths cannot work when the binary tree
  647. // is a network path.
  648. if(result.size() < 2 || result.substr(0, 2) != "//")
  649. {
  650. this->DirectoryState->RelativePathTopBinary = result;
  651. }
  652. else
  653. {
  654. this->DirectoryState->RelativePathTopBinary = "";
  655. }
  656. }
  657. cmState::Snapshot cmState::CreateBaseSnapshot()
  658. {
  659. PositionType pos = this->SnapshotData.Extend(this->SnapshotData.Root());
  660. pos->DirectoryParent = this->SnapshotData.Root();
  661. pos->ScopeParent = this->SnapshotData.Root();
  662. pos->SnapshotType = BaseType;
  663. pos->BuildSystemDirectory =
  664. this->BuildsystemDirectory.Extend(this->BuildsystemDirectory.Root());
  665. pos->ExecutionListFile =
  666. this->ExecutionListFiles.Extend(this->ExecutionListFiles.Root());
  667. pos->IncludeDirectoryPosition = 0;
  668. pos->CompileDefinitionsPosition = 0;
  669. pos->CompileOptionsPosition = 0;
  670. pos->BuildSystemDirectory->DirectoryEnd = pos;
  671. pos->Policies = this->PolicyStack.Root();
  672. pos->PolicyRoot = this->PolicyStack.Root();
  673. pos->PolicyScope = this->PolicyStack.Root();
  674. assert(pos->Policies.IsValid());
  675. assert(pos->PolicyRoot.IsValid());
  676. pos->Vars = this->VarTree.Extend(this->VarTree.Root());
  677. assert(pos->Vars.IsValid());
  678. pos->Parent = this->VarTree.Root();
  679. pos->Root = this->VarTree.Root();
  680. return cmState::Snapshot(this, pos);
  681. }
  682. cmState::Snapshot
  683. cmState::CreateBuildsystemDirectorySnapshot(Snapshot originSnapshot,
  684. std::string const& entryPointCommand,
  685. long entryPointLine)
  686. {
  687. assert(originSnapshot.IsValid());
  688. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position);
  689. pos->EntryPointLine = entryPointLine;
  690. pos->EntryPointCommand = entryPointCommand;
  691. pos->DirectoryParent = originSnapshot.Position;
  692. pos->ScopeParent = originSnapshot.Position;
  693. pos->SnapshotType = BuildsystemDirectoryType;
  694. pos->BuildSystemDirectory =
  695. this->BuildsystemDirectory.Extend(
  696. originSnapshot.Position->BuildSystemDirectory);
  697. pos->ExecutionListFile =
  698. this->ExecutionListFiles.Extend(
  699. originSnapshot.Position->ExecutionListFile);
  700. pos->BuildSystemDirectory->DirectoryEnd = pos;
  701. pos->Policies = originSnapshot.Position->Policies;
  702. pos->PolicyRoot = originSnapshot.Position->Policies;
  703. pos->PolicyScope = originSnapshot.Position->Policies;
  704. assert(pos->Policies.IsValid());
  705. assert(pos->PolicyRoot.IsValid());
  706. cmLinkedTree<cmDefinitions>::iterator origin =
  707. originSnapshot.Position->Vars;
  708. pos->Parent = origin;
  709. pos->Root = origin;
  710. pos->Vars = this->VarTree.Extend(origin);
  711. return cmState::Snapshot(this, pos);
  712. }
  713. cmState::Snapshot
  714. cmState::CreateFunctionCallSnapshot(cmState::Snapshot originSnapshot,
  715. std::string const& entryPointCommand,
  716. long entryPointLine,
  717. std::string const& fileName)
  718. {
  719. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
  720. *originSnapshot.Position);
  721. pos->ScopeParent = originSnapshot.Position;
  722. pos->EntryPointLine = entryPointLine;
  723. pos->EntryPointCommand = entryPointCommand;
  724. pos->SnapshotType = FunctionCallType;
  725. pos->ExecutionListFile = this->ExecutionListFiles.Extend(
  726. originSnapshot.Position->ExecutionListFile, fileName);
  727. pos->BuildSystemDirectory->DirectoryEnd = pos;
  728. pos->PolicyScope = originSnapshot.Position->Policies;
  729. assert(originSnapshot.Position->Vars.IsValid());
  730. cmLinkedTree<cmDefinitions>::iterator origin =
  731. originSnapshot.Position->Vars;
  732. pos->Parent = origin;
  733. pos->Vars = this->VarTree.Extend(origin);
  734. return cmState::Snapshot(this, pos);
  735. }
  736. cmState::Snapshot
  737. cmState::CreateMacroCallSnapshot(cmState::Snapshot originSnapshot,
  738. std::string const& entryPointCommand,
  739. long entryPointLine,
  740. std::string const& fileName)
  741. {
  742. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
  743. *originSnapshot.Position);
  744. pos->EntryPointLine = entryPointLine;
  745. pos->EntryPointCommand = entryPointCommand;
  746. pos->SnapshotType = MacroCallType;
  747. pos->ExecutionListFile = this->ExecutionListFiles.Extend(
  748. originSnapshot.Position->ExecutionListFile, fileName);
  749. assert(originSnapshot.Position->Vars.IsValid());
  750. pos->BuildSystemDirectory->DirectoryEnd = pos;
  751. pos->PolicyScope = originSnapshot.Position->Policies;
  752. return cmState::Snapshot(this, pos);
  753. }
  754. cmState::Snapshot
  755. cmState::CreateCallStackSnapshot(cmState::Snapshot originSnapshot,
  756. const std::string& entryPointCommand,
  757. long entryPointLine,
  758. const std::string& fileName)
  759. {
  760. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
  761. *originSnapshot.Position);
  762. pos->EntryPointLine = entryPointLine;
  763. pos->EntryPointCommand = entryPointCommand;
  764. pos->SnapshotType = CallStackType;
  765. pos->ExecutionListFile = this->ExecutionListFiles.Extend(
  766. originSnapshot.Position->ExecutionListFile, fileName);
  767. assert(originSnapshot.Position->Vars.IsValid());
  768. pos->BuildSystemDirectory->DirectoryEnd = pos;
  769. pos->PolicyScope = originSnapshot.Position->Policies;
  770. return cmState::Snapshot(this, pos);
  771. }
  772. cmState::Snapshot
  773. cmState::CreateVariableScopeSnapshot(cmState::Snapshot originSnapshot,
  774. std::string const& entryPointCommand,
  775. long entryPointLine)
  776. {
  777. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
  778. *originSnapshot.Position);
  779. pos->ScopeParent = originSnapshot.Position;
  780. pos->EntryPointLine = entryPointLine;
  781. pos->EntryPointCommand = entryPointCommand;
  782. pos->SnapshotType = VariableScopeType;
  783. assert(originSnapshot.Position->Vars.IsValid());
  784. cmLinkedTree<cmDefinitions>::iterator origin =
  785. originSnapshot.Position->Vars;
  786. pos->Parent = origin;
  787. pos->Vars = this->VarTree.Extend(origin);
  788. assert(pos->Vars.IsValid());
  789. return cmState::Snapshot(this, pos);
  790. }
  791. cmState::Snapshot
  792. cmState::CreateInlineListFileSnapshot(cmState::Snapshot originSnapshot,
  793. const std::string& entryPointCommand,
  794. long entryPointLine,
  795. const std::string& fileName)
  796. {
  797. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
  798. *originSnapshot.Position);
  799. pos->EntryPointLine = entryPointLine;
  800. pos->EntryPointCommand = entryPointCommand;
  801. pos->SnapshotType = InlineListFileType;
  802. pos->ExecutionListFile = this->ExecutionListFiles.Extend(
  803. originSnapshot.Position->ExecutionListFile, fileName);
  804. pos->BuildSystemDirectory->DirectoryEnd = pos;
  805. pos->PolicyScope = originSnapshot.Position->Policies;
  806. return cmState::Snapshot(this, pos);
  807. }
  808. cmState::Snapshot
  809. cmState::CreatePolicyScopeSnapshot(cmState::Snapshot originSnapshot)
  810. {
  811. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
  812. *originSnapshot.Position);
  813. pos->SnapshotType = PolicyScopeType;
  814. pos->BuildSystemDirectory->DirectoryEnd = pos;
  815. pos->PolicyScope = originSnapshot.Position->Policies;
  816. return cmState::Snapshot(this, pos);
  817. }
  818. cmState::Snapshot cmState::Pop(cmState::Snapshot originSnapshot)
  819. {
  820. PositionType pos = originSnapshot.Position;
  821. PositionType prevPos = pos;
  822. ++prevPos;
  823. prevPos->IncludeDirectoryPosition =
  824. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  825. prevPos->CompileDefinitionsPosition =
  826. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  827. prevPos->CompileOptionsPosition =
  828. prevPos->BuildSystemDirectory->CompileOptions.size();
  829. prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
  830. return Snapshot(this, prevPos);
  831. }
  832. cmState::Snapshot::Snapshot(cmState* state)
  833. : State(state)
  834. , Position()
  835. {
  836. }
  837. cmState::Snapshot::Snapshot(cmState* state, PositionType position)
  838. : State(state),
  839. Position(position)
  840. {
  841. }
  842. cmState::SnapshotType cmState::Snapshot::GetType() const
  843. {
  844. return this->Position->SnapshotType;
  845. }
  846. const char* cmState::Directory::GetCurrentSource() const
  847. {
  848. return this->DirectoryState->Location.c_str();
  849. }
  850. void cmState::Directory::SetCurrentSource(std::string const& dir)
  851. {
  852. std::string& loc = this->DirectoryState->Location;
  853. loc = dir;
  854. cmSystemTools::ConvertToUnixSlashes(loc);
  855. loc = cmSystemTools::CollapseFullPath(loc);
  856. cmSystemTools::SplitPath(
  857. loc,
  858. this->DirectoryState->CurrentSourceDirectoryComponents);
  859. this->ComputeRelativePathTopSource();
  860. }
  861. const char* cmState::Directory::GetCurrentBinary() const
  862. {
  863. return this->DirectoryState->OutputLocation.c_str();
  864. }
  865. void cmState::Directory::SetCurrentBinary(std::string const& dir)
  866. {
  867. std::string& loc = this->DirectoryState->OutputLocation;
  868. loc = dir;
  869. cmSystemTools::ConvertToUnixSlashes(loc);
  870. loc = cmSystemTools::CollapseFullPath(loc);
  871. cmSystemTools::SplitPath(
  872. loc,
  873. this->DirectoryState->CurrentBinaryDirectoryComponents);
  874. this->ComputeRelativePathTopBinary();
  875. }
  876. void cmState::Snapshot::SetListFile(const std::string& listfile)
  877. {
  878. *this->Position->ExecutionListFile = listfile;
  879. }
  880. std::vector<std::string> const&
  881. cmState::Directory::GetCurrentSourceComponents() const
  882. {
  883. return this->DirectoryState->CurrentSourceDirectoryComponents;
  884. }
  885. std::vector<std::string> const&
  886. cmState::Directory::GetCurrentBinaryComponents() const
  887. {
  888. return this->DirectoryState->CurrentBinaryDirectoryComponents;
  889. }
  890. const char* cmState::Directory::GetRelativePathTopSource() const
  891. {
  892. return this->DirectoryState->RelativePathTopSource.c_str();
  893. }
  894. const char* cmState::Directory::GetRelativePathTopBinary() const
  895. {
  896. return this->DirectoryState->RelativePathTopBinary.c_str();
  897. }
  898. void cmState::Directory::SetRelativePathTopSource(const char* dir)
  899. {
  900. this->DirectoryState->RelativePathTopSource = dir;
  901. }
  902. void cmState::Directory::SetRelativePathTopBinary(const char* dir)
  903. {
  904. this->DirectoryState->RelativePathTopBinary = dir;
  905. }
  906. std::string cmState::Snapshot::GetExecutionListFile() const
  907. {
  908. return *this->Position->ExecutionListFile;
  909. }
  910. std::string cmState::Snapshot::GetEntryPointCommand() const
  911. {
  912. return this->Position->EntryPointCommand;
  913. }
  914. long cmState::Snapshot::GetEntryPointLine() const
  915. {
  916. return this->Position->EntryPointLine;
  917. }
  918. bool cmState::Snapshot::IsValid() const
  919. {
  920. return this->State && this->Position.IsValid()
  921. ? this->Position != this->State->SnapshotData.Root()
  922. : false;
  923. }
  924. cmState::Snapshot cmState::Snapshot::GetBuildsystemDirectoryParent() const
  925. {
  926. Snapshot snapshot;
  927. if (!this->State || this->Position == this->State->SnapshotData.Root())
  928. {
  929. return snapshot;
  930. }
  931. PositionType parentPos = this->Position->DirectoryParent;
  932. if (parentPos != this->State->SnapshotData.Root())
  933. {
  934. snapshot = Snapshot(this->State, parentPos);
  935. }
  936. return snapshot;
  937. }
  938. cmState::Snapshot cmState::Snapshot::GetCallStackParent() const
  939. {
  940. assert(this->State);
  941. assert(this->Position != this->State->SnapshotData.Root());
  942. Snapshot snapshot;
  943. PositionType parentPos = this->Position;
  944. while(parentPos->SnapshotType == cmState::PolicyScopeType)
  945. {
  946. ++parentPos;
  947. }
  948. if (parentPos->SnapshotType == cmState::BuildsystemDirectoryType
  949. || parentPos->SnapshotType == cmState::BaseType)
  950. {
  951. return snapshot;
  952. }
  953. ++parentPos;
  954. while(parentPos->SnapshotType == cmState::PolicyScopeType)
  955. {
  956. ++parentPos;
  957. }
  958. if (parentPos == this->State->SnapshotData.Root())
  959. {
  960. return snapshot;
  961. }
  962. snapshot = Snapshot(this->State, parentPos);
  963. return snapshot;
  964. }
  965. void cmState::Snapshot::PushPolicy(cmPolicies::PolicyMap entry, bool weak)
  966. {
  967. PositionType pos = this->Position;
  968. pos->Policies =
  969. this->State->PolicyStack.Extend(pos->Policies,
  970. PolicyStackEntry(entry, weak));
  971. }
  972. bool cmState::Snapshot::PopPolicy()
  973. {
  974. PositionType pos = this->Position;
  975. if (pos->Policies == pos->PolicyScope)
  976. {
  977. return false;
  978. }
  979. ++pos->Policies;
  980. return true;
  981. }
  982. bool cmState::Snapshot::CanPopPolicyScope()
  983. {
  984. return this->Position->Policies == this->Position->PolicyScope;
  985. }
  986. void cmState::Snapshot::SetPolicy(cmPolicies::PolicyID id,
  987. cmPolicies::PolicyStatus status)
  988. {
  989. // Update the policy stack from the top to the top-most strong entry.
  990. bool previous_was_weak = true;
  991. for(cmLinkedTree<PolicyStackEntry>::iterator psi = this->Position->Policies;
  992. previous_was_weak && psi != this->Position->PolicyRoot; ++psi)
  993. {
  994. psi->Set(id, status);
  995. previous_was_weak = psi->Weak;
  996. }
  997. }
  998. cmPolicies::PolicyStatus
  999. cmState::Snapshot::GetPolicy(cmPolicies::PolicyID id) const
  1000. {
  1001. cmPolicies::PolicyStatus status = cmPolicies::GetPolicyStatus(id);
  1002. if(status == cmPolicies::REQUIRED_ALWAYS ||
  1003. status == cmPolicies::REQUIRED_IF_USED)
  1004. {
  1005. return status;
  1006. }
  1007. cmLinkedTree<BuildsystemDirectoryStateType>::iterator dir =
  1008. this->Position->BuildSystemDirectory;
  1009. while (true)
  1010. {
  1011. assert(dir.IsValid());
  1012. cmLinkedTree<PolicyStackEntry>::iterator leaf =
  1013. dir->DirectoryEnd->Policies;
  1014. cmLinkedTree<PolicyStackEntry>::iterator root =
  1015. dir->DirectoryEnd->PolicyRoot;
  1016. for( ; leaf != root; ++leaf)
  1017. {
  1018. if(leaf->IsDefined(id))
  1019. {
  1020. status = leaf->Get(id);
  1021. return status;
  1022. }
  1023. }
  1024. cmState::PositionType e = dir->DirectoryEnd;
  1025. cmState::PositionType p = e->DirectoryParent;
  1026. if (p == this->State->SnapshotData.Root())
  1027. {
  1028. break;
  1029. }
  1030. dir = p->BuildSystemDirectory;
  1031. }
  1032. return status;
  1033. }
  1034. bool cmState::Snapshot::HasDefinedPolicyCMP0011()
  1035. {
  1036. return !this->Position->Policies->IsEmpty();
  1037. }
  1038. const char* cmState::Snapshot::GetDefinition(std::string const& name) const
  1039. {
  1040. assert(this->Position->Vars.IsValid());
  1041. return cmDefinitions::Get(name, this->Position->Vars,
  1042. this->Position->Root);
  1043. }
  1044. bool cmState::Snapshot::IsInitialized(std::string const& name) const
  1045. {
  1046. return cmDefinitions::HasKey(name, this->Position->Vars,
  1047. this->Position->Root);
  1048. }
  1049. void cmState::Snapshot::SetDefinition(std::string const& name,
  1050. std::string const& value)
  1051. {
  1052. this->Position->Vars->Set(name, value.c_str());
  1053. }
  1054. void cmState::Snapshot::RemoveDefinition(std::string const& name)
  1055. {
  1056. this->Position->Vars->Set(name, 0);
  1057. }
  1058. std::vector<std::string> cmState::Snapshot::UnusedKeys() const
  1059. {
  1060. return this->Position->Vars->UnusedKeys();
  1061. }
  1062. std::vector<std::string> cmState::Snapshot::ClosureKeys() const
  1063. {
  1064. return cmDefinitions::ClosureKeys(this->Position->Vars,
  1065. this->Position->Root);
  1066. }
  1067. bool cmState::Snapshot::RaiseScope(std::string const& var, const char* varDef)
  1068. {
  1069. if(this->Position->ScopeParent == this->Position->DirectoryParent)
  1070. {
  1071. Snapshot parentDir = this->GetBuildsystemDirectoryParent();
  1072. if(!parentDir.IsValid())
  1073. {
  1074. return false;
  1075. }
  1076. // Update the definition in the parent directory top scope. This
  1077. // directory's scope was initialized by the closure of the parent
  1078. // scope, so we do not need to localize the definition first.
  1079. if (varDef)
  1080. {
  1081. parentDir.SetDefinition(var, varDef);
  1082. }
  1083. else
  1084. {
  1085. parentDir.RemoveDefinition(var);
  1086. }
  1087. return true;
  1088. }
  1089. // First localize the definition in the current scope.
  1090. cmDefinitions::Raise(var, this->Position->Vars,
  1091. this->Position->Root);
  1092. // Now update the definition in the parent scope.
  1093. this->Position->Parent->Set(var, varDef);
  1094. return true;
  1095. }
  1096. static const std::string cmPropertySentinal = std::string();
  1097. template<typename T, typename U, typename V>
  1098. void InitializeContentFromParent(T& parentContent,
  1099. T& thisContent,
  1100. U& parentBacktraces,
  1101. U& thisBacktraces,
  1102. V& contentEndPosition)
  1103. {
  1104. std::vector<std::string>::const_iterator parentBegin =
  1105. parentContent.begin();
  1106. std::vector<std::string>::const_iterator parentEnd =
  1107. parentContent.end();
  1108. std::vector<std::string>::const_reverse_iterator parentRbegin =
  1109. cmMakeReverseIterator(parentEnd);
  1110. std::vector<std::string>::const_reverse_iterator parentRend =
  1111. parentContent.rend();
  1112. parentRbegin = std::find(parentRbegin, parentRend, cmPropertySentinal);
  1113. std::vector<std::string>::const_iterator parentIt = parentRbegin.base();
  1114. thisContent = std::vector<std::string>(parentIt, parentEnd);
  1115. std::vector<cmListFileBacktrace>::const_iterator btIt =
  1116. parentBacktraces.begin() + std::distance(parentBegin, parentIt);
  1117. std::vector<cmListFileBacktrace>::const_iterator btEnd =
  1118. parentBacktraces.end();
  1119. thisBacktraces = std::vector<cmListFileBacktrace>(btIt, btEnd);
  1120. contentEndPosition = thisContent.size();
  1121. }
  1122. void cmState::Snapshot::InitializeFromParent()
  1123. {
  1124. PositionType parent = this->Position->DirectoryParent;
  1125. assert(this->Position->Vars.IsValid());
  1126. assert(parent->Vars.IsValid());
  1127. *this->Position->Vars =
  1128. cmDefinitions::MakeClosure(parent->Vars, parent->Root);
  1129. InitializeContentFromParent(parent->BuildSystemDirectory->IncludeDirectories,
  1130. this->Position->BuildSystemDirectory->IncludeDirectories,
  1131. parent->BuildSystemDirectory->IncludeDirectoryBacktraces,
  1132. this->Position->BuildSystemDirectory->IncludeDirectoryBacktraces,
  1133. this->Position->IncludeDirectoryPosition);
  1134. InitializeContentFromParent(parent->BuildSystemDirectory->CompileDefinitions,
  1135. this->Position->BuildSystemDirectory->CompileDefinitions,
  1136. parent->BuildSystemDirectory->CompileDefinitionsBacktraces,
  1137. this->Position->BuildSystemDirectory->CompileDefinitionsBacktraces,
  1138. this->Position->CompileDefinitionsPosition);
  1139. InitializeContentFromParent(parent->BuildSystemDirectory->CompileOptions,
  1140. this->Position->BuildSystemDirectory->CompileOptions,
  1141. parent->BuildSystemDirectory->CompileOptionsBacktraces,
  1142. this->Position->BuildSystemDirectory->CompileOptionsBacktraces,
  1143. this->Position->CompileOptionsPosition);
  1144. }
  1145. cmState* cmState::Snapshot::GetState() const
  1146. {
  1147. return this->State;
  1148. }
  1149. cmState::Directory cmState::Snapshot::GetDirectory() const
  1150. {
  1151. return Directory(this->Position->BuildSystemDirectory, *this);
  1152. }
  1153. cmState::Directory::Directory(
  1154. cmLinkedTree<BuildsystemDirectoryStateType>::iterator iter,
  1155. const cmState::Snapshot& snapshot)
  1156. : DirectoryState(iter), Snapshot_(snapshot)
  1157. {
  1158. }
  1159. template <typename T, typename U>
  1160. cmStringRange GetPropertyContent(T const& content, U contentEndPosition)
  1161. {
  1162. std::vector<std::string>::const_iterator end =
  1163. content.begin() + contentEndPosition;
  1164. std::vector<std::string>::const_reverse_iterator rbegin =
  1165. cmMakeReverseIterator(end);
  1166. rbegin = std::find(rbegin, content.rend(), cmPropertySentinal);
  1167. return cmMakeRange(rbegin.base(), end);
  1168. }
  1169. template <typename T, typename U, typename V>
  1170. cmBacktraceRange GetPropertyBacktraces(T const& content,
  1171. U const& backtraces,
  1172. V contentEndPosition)
  1173. {
  1174. std::vector<std::string>::const_iterator entryEnd =
  1175. content.begin() + contentEndPosition;
  1176. std::vector<std::string>::const_reverse_iterator rbegin =
  1177. cmMakeReverseIterator(entryEnd);
  1178. rbegin = std::find(rbegin, content.rend(), cmPropertySentinal);
  1179. std::vector<cmListFileBacktrace>::const_iterator it =
  1180. backtraces.begin() + std::distance(content.begin(), rbegin.base());
  1181. std::vector<cmListFileBacktrace>::const_iterator end = backtraces.end();
  1182. return cmMakeRange(it, end);
  1183. }
  1184. template <typename T, typename U, typename V>
  1185. void AppendEntry(T& content, U& backtraces, V& endContentPosition,
  1186. const std::string& vec, const cmListFileBacktrace& lfbt)
  1187. {
  1188. assert(endContentPosition == content.size());
  1189. content.push_back(vec);
  1190. backtraces.push_back(lfbt);
  1191. endContentPosition = content.size();
  1192. }
  1193. template <typename T, typename U, typename V>
  1194. void SetContent(T& content, U& backtraces, V& endContentPosition,
  1195. const std::string& vec, const cmListFileBacktrace& lfbt)
  1196. {
  1197. assert(endContentPosition == content.size());
  1198. content.resize(content.size() + 2);
  1199. backtraces.resize(backtraces.size() + 2);
  1200. content.back() = vec;
  1201. backtraces.back() = lfbt;
  1202. endContentPosition = content.size();
  1203. }
  1204. template <typename T, typename U, typename V>
  1205. void ClearContent(T& content, U& backtraces, V& endContentPosition)
  1206. {
  1207. assert(endContentPosition == content.size());
  1208. content.resize(content.size() + 1);
  1209. backtraces.resize(backtraces.size() + 1);
  1210. endContentPosition = content.size();
  1211. }
  1212. cmStringRange
  1213. cmState::Directory::GetIncludeDirectoriesEntries() const
  1214. {
  1215. return GetPropertyContent(this->DirectoryState->IncludeDirectories,
  1216. this->Snapshot_.Position->IncludeDirectoryPosition);
  1217. }
  1218. cmBacktraceRange
  1219. cmState::Directory::GetIncludeDirectoriesEntryBacktraces() const
  1220. {
  1221. return GetPropertyBacktraces(this->DirectoryState->IncludeDirectories,
  1222. this->DirectoryState->IncludeDirectoryBacktraces,
  1223. this->Snapshot_.Position->IncludeDirectoryPosition);
  1224. }
  1225. void cmState::Directory::AppendIncludeDirectoriesEntry(
  1226. const std::string& vec, const cmListFileBacktrace& lfbt)
  1227. {
  1228. AppendEntry(this->DirectoryState->IncludeDirectories,
  1229. this->DirectoryState->IncludeDirectoryBacktraces,
  1230. this->Snapshot_.Position->IncludeDirectoryPosition,
  1231. vec, lfbt);
  1232. }
  1233. void cmState::Directory::PrependIncludeDirectoriesEntry(
  1234. const std::string& vec, const cmListFileBacktrace& lfbt)
  1235. {
  1236. std::vector<std::string>::iterator entryEnd =
  1237. this->DirectoryState->IncludeDirectories.begin()
  1238. + this->Snapshot_.Position->IncludeDirectoryPosition;
  1239. std::vector<std::string>::reverse_iterator rend =
  1240. this->DirectoryState->IncludeDirectories.rend();
  1241. std::vector<std::string>::reverse_iterator rbegin =
  1242. cmMakeReverseIterator(entryEnd);
  1243. rbegin = std::find(rbegin, rend, cmPropertySentinal);
  1244. std::vector<std::string>::iterator entryIt = rbegin.base();
  1245. std::vector<std::string>::iterator entryBegin =
  1246. this->DirectoryState->IncludeDirectories.begin();
  1247. std::vector<cmListFileBacktrace>::iterator btIt =
  1248. this->DirectoryState->IncludeDirectoryBacktraces.begin()
  1249. + std::distance(entryBegin, entryIt);
  1250. this->DirectoryState->IncludeDirectories.insert(entryIt, vec);
  1251. this->DirectoryState->IncludeDirectoryBacktraces.insert(btIt, lfbt);
  1252. this->Snapshot_.Position->IncludeDirectoryPosition =
  1253. this->DirectoryState->IncludeDirectories.size();
  1254. }
  1255. void cmState::Directory::SetIncludeDirectories(
  1256. const std::string& vec, const cmListFileBacktrace& lfbt)
  1257. {
  1258. SetContent(this->DirectoryState->IncludeDirectories,
  1259. this->DirectoryState->IncludeDirectoryBacktraces,
  1260. this->Snapshot_.Position->IncludeDirectoryPosition,
  1261. vec, lfbt);
  1262. }
  1263. void cmState::Directory::ClearIncludeDirectories()
  1264. {
  1265. ClearContent(this->DirectoryState->IncludeDirectories,
  1266. this->DirectoryState->IncludeDirectoryBacktraces,
  1267. this->Snapshot_.Position->IncludeDirectoryPosition);
  1268. }
  1269. cmStringRange cmState::Directory::GetCompileDefinitionsEntries() const
  1270. {
  1271. return GetPropertyContent(this->DirectoryState->CompileDefinitions,
  1272. this->Snapshot_.Position->CompileDefinitionsPosition);
  1273. }
  1274. cmBacktraceRange
  1275. cmState::Directory::GetCompileDefinitionsEntryBacktraces() const
  1276. {
  1277. return GetPropertyBacktraces(this->DirectoryState->CompileDefinitions,
  1278. this->DirectoryState->CompileDefinitionsBacktraces,
  1279. this->Snapshot_.Position->CompileDefinitionsPosition);
  1280. }
  1281. void cmState::Directory::AppendCompileDefinitionsEntry(const std::string& vec,
  1282. const cmListFileBacktrace& lfbt)
  1283. {
  1284. AppendEntry(this->DirectoryState->CompileDefinitions,
  1285. this->DirectoryState->CompileDefinitionsBacktraces,
  1286. this->Snapshot_.Position->CompileDefinitionsPosition,
  1287. vec, lfbt);
  1288. }
  1289. void cmState::Directory::SetCompileDefinitions(const std::string& vec,
  1290. const cmListFileBacktrace& lfbt)
  1291. {
  1292. SetContent(this->DirectoryState->CompileDefinitions,
  1293. this->DirectoryState->CompileDefinitionsBacktraces,
  1294. this->Snapshot_.Position->CompileDefinitionsPosition,
  1295. vec, lfbt);
  1296. }
  1297. void cmState::Directory::ClearCompileDefinitions()
  1298. {
  1299. ClearContent(this->DirectoryState->CompileDefinitions,
  1300. this->DirectoryState->CompileDefinitionsBacktraces,
  1301. this->Snapshot_.Position->CompileDefinitionsPosition);
  1302. }
  1303. cmStringRange cmState::Directory::GetCompileOptionsEntries() const
  1304. {
  1305. return GetPropertyContent(this->DirectoryState->CompileOptions,
  1306. this->Snapshot_.Position->CompileOptionsPosition);
  1307. }
  1308. cmBacktraceRange cmState::Directory::GetCompileOptionsEntryBacktraces() const
  1309. {
  1310. return GetPropertyBacktraces(this->DirectoryState->CompileOptions,
  1311. this->DirectoryState->CompileOptionsBacktraces,
  1312. this->Snapshot_.Position->CompileOptionsPosition);
  1313. }
  1314. void
  1315. cmState::Directory::AppendCompileOptionsEntry(const std::string& vec,
  1316. const cmListFileBacktrace& lfbt)
  1317. {
  1318. AppendEntry(this->DirectoryState->CompileOptions,
  1319. this->DirectoryState->CompileOptionsBacktraces,
  1320. this->Snapshot_.Position->CompileOptionsPosition,
  1321. vec, lfbt);
  1322. }
  1323. void cmState::Directory::SetCompileOptions(const std::string& vec,
  1324. const cmListFileBacktrace& lfbt)
  1325. {
  1326. SetContent(this->DirectoryState->CompileOptions,
  1327. this->DirectoryState->CompileOptionsBacktraces,
  1328. this->Snapshot_.Position->CompileOptionsPosition,
  1329. vec, lfbt);
  1330. }
  1331. void cmState::Directory::ClearCompileOptions()
  1332. {
  1333. ClearContent(this->DirectoryState->CompileOptions,
  1334. this->DirectoryState->CompileOptionsBacktraces,
  1335. this->Snapshot_.Position->CompileOptionsPosition);
  1336. }
  1337. bool cmState::Snapshot::StrictWeakOrder::operator()(
  1338. const cmState::Snapshot& lhs, const cmState::Snapshot& rhs) const
  1339. {
  1340. return lhs.Position.StrictWeakOrdered(rhs.Position);
  1341. }
  1342. void cmState::Directory::SetProperty(const std::string& prop,
  1343. const char* value,
  1344. cmListFileBacktrace lfbt)
  1345. {
  1346. if (prop == "INCLUDE_DIRECTORIES")
  1347. {
  1348. if (!value)
  1349. {
  1350. this->ClearIncludeDirectories();
  1351. return;
  1352. }
  1353. this->SetIncludeDirectories(value, lfbt);
  1354. return;
  1355. }
  1356. if (prop == "COMPILE_OPTIONS")
  1357. {
  1358. if (!value)
  1359. {
  1360. this->ClearCompileOptions();
  1361. return;
  1362. }
  1363. this->SetCompileOptions(value, lfbt);
  1364. return;
  1365. }
  1366. if (prop == "COMPILE_DEFINITIONS")
  1367. {
  1368. if (!value)
  1369. {
  1370. this->ClearCompileDefinitions();
  1371. return;
  1372. }
  1373. this->SetCompileDefinitions(value, lfbt);
  1374. return;
  1375. }
  1376. this->DirectoryState->Properties.SetProperty(prop, value);
  1377. }
  1378. void cmState::Directory::AppendProperty(const std::string& prop,
  1379. const char* value,
  1380. bool asString,
  1381. cmListFileBacktrace lfbt)
  1382. {
  1383. if (prop == "INCLUDE_DIRECTORIES")
  1384. {
  1385. this->AppendIncludeDirectoriesEntry(value, lfbt);
  1386. return;
  1387. }
  1388. if (prop == "COMPILE_OPTIONS")
  1389. {
  1390. this->AppendCompileOptionsEntry(value, lfbt);
  1391. return;
  1392. }
  1393. if (prop == "COMPILE_DEFINITIONS")
  1394. {
  1395. this->AppendCompileDefinitionsEntry(value, lfbt);
  1396. return;
  1397. }
  1398. this->DirectoryState->Properties.AppendProperty(prop, value, asString);
  1399. }
  1400. const char*cmState::Directory::GetProperty(const std::string& prop) const
  1401. {
  1402. const bool chain = this->Snapshot_.State->
  1403. IsPropertyChained(prop, cmProperty::DIRECTORY);
  1404. return this->GetProperty(prop, chain);
  1405. }
  1406. const char*
  1407. cmState::Directory::GetProperty(const std::string& prop, bool chain) const
  1408. {
  1409. static std::string output;
  1410. output = "";
  1411. if (prop == "PARENT_DIRECTORY")
  1412. {
  1413. cmState::Snapshot parent =
  1414. this->Snapshot_.GetBuildsystemDirectoryParent();
  1415. if(parent.IsValid())
  1416. {
  1417. return parent.GetDirectory().GetCurrentSource();
  1418. }
  1419. return "";
  1420. }
  1421. else if (prop == "LISTFILE_STACK")
  1422. {
  1423. std::vector<std::string> listFiles;
  1424. cmState::Snapshot snp = this->Snapshot_;
  1425. while (snp.IsValid())
  1426. {
  1427. listFiles.push_back(snp.GetExecutionListFile());
  1428. snp = snp.GetCallStackParent();
  1429. }
  1430. std::reverse(listFiles.begin(), listFiles.end());
  1431. output = cmJoin(listFiles, ";");
  1432. return output.c_str();
  1433. }
  1434. else if ( prop == "CACHE_VARIABLES" )
  1435. {
  1436. output = cmJoin(this->Snapshot_.State->GetCacheEntryKeys(), ";");
  1437. return output.c_str();
  1438. }
  1439. else if (prop == "VARIABLES")
  1440. {
  1441. std::vector<std::string> res = this->Snapshot_.ClosureKeys();
  1442. std::vector<std::string> cacheKeys =
  1443. this->Snapshot_.State->GetCacheEntryKeys();
  1444. res.insert(res.end(), cacheKeys.begin(), cacheKeys.end());
  1445. std::sort(res.begin(), res.end());
  1446. output = cmJoin(res, ";");
  1447. return output.c_str();
  1448. }
  1449. else if (prop == "INCLUDE_DIRECTORIES")
  1450. {
  1451. output = cmJoin(this->GetIncludeDirectoriesEntries(), ";");
  1452. return output.c_str();
  1453. }
  1454. else if (prop == "COMPILE_OPTIONS")
  1455. {
  1456. output = cmJoin(this->GetCompileOptionsEntries(), ";");
  1457. return output.c_str();
  1458. }
  1459. else if (prop == "COMPILE_DEFINITIONS")
  1460. {
  1461. output = cmJoin(this->GetCompileDefinitionsEntries(), ";");
  1462. return output.c_str();
  1463. }
  1464. const char *retVal = this->DirectoryState->Properties.GetPropertyValue(prop);
  1465. if (!retVal && chain)
  1466. {
  1467. Snapshot parentSnapshot = this->Snapshot_.GetBuildsystemDirectoryParent();
  1468. if (parentSnapshot.IsValid())
  1469. {
  1470. return parentSnapshot.GetDirectory().GetProperty(prop, chain);
  1471. }
  1472. return this->Snapshot_.State->GetGlobalProperty(prop);
  1473. }
  1474. return retVal;
  1475. }
  1476. bool cmState::Directory::GetPropertyAsBool(const std::string& prop) const
  1477. {
  1478. return cmSystemTools::IsOn(this->GetProperty(prop));
  1479. }
  1480. std::vector<std::string> cmState::Directory::GetPropertyKeys() const
  1481. {
  1482. std::vector<std::string> keys;
  1483. keys.reserve(this->DirectoryState->Properties.size());
  1484. for(cmPropertyMap::const_iterator it =
  1485. this->DirectoryState->Properties.begin();
  1486. it != this->DirectoryState->Properties.end(); ++it)
  1487. {
  1488. keys.push_back(it->first);
  1489. }
  1490. return keys;
  1491. }