cmMakefile.cxx 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmMakefile.h"
  14. #include "cmCommand.h"
  15. #include "cmSourceFile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmGlobalGenerator.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmCommands.h"
  20. #include "cmCacheManager.h"
  21. #include "cmFunctionBlocker.h"
  22. #include "cmListFileCache.h"
  23. #include "cmVariableWatch.h"
  24. #include "cmake.h"
  25. #include <stdlib.h> // required for atoi
  26. #include <cmsys/RegularExpression.hxx>
  27. // default is not to be building executables
  28. cmMakefile::cmMakefile()
  29. {
  30. // Setup the default include file regular expression (match everything).
  31. m_IncludeFileRegularExpression = "^.*$";
  32. // Setup the default include complaint regular expression (match nothing).
  33. m_ComplainFileRegularExpression = "^$";
  34. // Source and header file extensions that we can handle
  35. // The "c" extension MUST precede the "C" extension.
  36. m_SourceFileExtensions.push_back( "c" );
  37. m_SourceFileExtensions.push_back( "C" );
  38. m_SourceFileExtensions.push_back( "c++" );
  39. m_SourceFileExtensions.push_back( "cc" );
  40. m_SourceFileExtensions.push_back( "cpp" );
  41. m_SourceFileExtensions.push_back( "cxx" );
  42. m_SourceFileExtensions.push_back( "m" );
  43. m_SourceFileExtensions.push_back( "M" );
  44. m_SourceFileExtensions.push_back( "mm" );
  45. m_HeaderFileExtensions.push_back( "h" );
  46. m_HeaderFileExtensions.push_back( "h++" );
  47. m_HeaderFileExtensions.push_back( "hm" );
  48. m_HeaderFileExtensions.push_back( "hpp" );
  49. m_HeaderFileExtensions.push_back( "hxx" );
  50. m_HeaderFileExtensions.push_back( "in" );
  51. m_HeaderFileExtensions.push_back( "txx" );
  52. m_DefineFlags = " ";
  53. m_LocalGenerator = 0;
  54. this->AddSourceGroup("", "^.*$");
  55. this->AddSourceGroup("Source Files",
  56. "\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|rc|def|r|odl|idl|hpj|bat)$");
  57. this->AddSourceGroup("Header Files", "\\.(h|h\\+\\+|hm|hpp|hxx|in|txx|inl)$");
  58. this->AddSourceGroup("CMake Rules", "\\.rule$");
  59. this->AddDefaultDefinitions();
  60. }
  61. const char* cmMakefile::GetReleaseVersion()
  62. {
  63. #if CMake_VERSION_MINOR & 1
  64. return "development";
  65. #else
  66. # if CMake_VERSION_PATCH == 0
  67. return "beta";
  68. # else
  69. return "patch " CMAKE_TO_STRING(CMake_VERSION_PATCH);
  70. # endif
  71. #endif
  72. }
  73. unsigned int cmMakefile::GetCacheMajorVersion()
  74. {
  75. if(!this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
  76. {
  77. return 0;
  78. }
  79. return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"));
  80. }
  81. unsigned int cmMakefile::GetCacheMinorVersion()
  82. {
  83. if(!this->GetCacheManager()->GetCacheValue("Cmake_Cache_MINOR_VERSION"))
  84. {
  85. return 0;
  86. }
  87. return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"));
  88. }
  89. cmMakefile::~cmMakefile()
  90. {
  91. for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
  92. i != m_SourceFiles.end(); ++i)
  93. {
  94. delete *i;
  95. }
  96. for(unsigned int i=0; i < m_UsedCommands.size(); i++)
  97. {
  98. delete m_UsedCommands[i];
  99. }
  100. for(DataMap::const_iterator d = m_DataMap.begin();
  101. d != m_DataMap.end(); ++d)
  102. {
  103. if(d->second)
  104. {
  105. delete d->second;
  106. }
  107. }
  108. std::list<cmFunctionBlocker *>::iterator pos;
  109. for (pos = m_FunctionBlockers.begin();
  110. pos != m_FunctionBlockers.end(); ++pos)
  111. {
  112. cmFunctionBlocker* b = *pos;
  113. delete b;
  114. }
  115. m_FunctionBlockers.clear();
  116. }
  117. void cmMakefile::PrintStringVector(const char* s, const std::vector<std::string>& v) const
  118. {
  119. std::cout << s << ": ( \n";
  120. for(std::vector<std::string>::const_iterator i = v.begin();
  121. i != v.end(); ++i)
  122. {
  123. std::cout << (*i).c_str() << " ";
  124. }
  125. std::cout << " )\n";
  126. }
  127. // call print on all the classes in the makefile
  128. void cmMakefile::Print() const
  129. {
  130. // print the class lists
  131. std::cout << "classes:\n";
  132. std::cout << " m_Targets: ";
  133. for (cmTargets::const_iterator l = m_Targets.begin();
  134. l != m_Targets.end(); l++)
  135. {
  136. std::cout << l->first << std::endl;
  137. }
  138. std::cout << " m_CurrentOutputDirectory; " <<
  139. m_CurrentOutputDirectory.c_str() << std::endl;
  140. std::cout << " m_StartOutputDirectory; " <<
  141. m_StartOutputDirectory.c_str() << std::endl;
  142. std::cout << " m_HomeOutputDirectory; " <<
  143. m_HomeOutputDirectory.c_str() << std::endl;
  144. std::cout << " m_cmCurrentDirectory; " <<
  145. m_cmCurrentDirectory.c_str() << std::endl;
  146. std::cout << " m_cmStartDirectory; " <<
  147. m_cmStartDirectory.c_str() << std::endl;
  148. std::cout << " m_cmHomeDirectory; " <<
  149. m_cmHomeDirectory.c_str() << std::endl;
  150. std::cout << " m_ProjectName; " << m_ProjectName.c_str() << std::endl;
  151. this->PrintStringVector("m_SubDirectories ", m_SubDirectories);
  152. this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
  153. this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
  154. for( std::vector<cmSourceGroup>::const_iterator i = m_SourceGroups.begin();
  155. i != m_SourceGroups.end(); ++i)
  156. {
  157. std::cout << "Source Group: " << i->GetName() << std::endl;
  158. }
  159. }
  160. bool cmMakefile::CommandExists(const char* name) const
  161. {
  162. return this->GetCMakeInstance()->CommandExists(name);
  163. }
  164. bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff)
  165. {
  166. bool result = true;
  167. // quick return if blocked
  168. if(this->IsFunctionBlocked(lff))
  169. {
  170. // No error.
  171. return result;
  172. }
  173. std::string name = lff.m_Name;
  174. // execute the command
  175. cmCommand *rm =
  176. this->GetCMakeInstance()->GetCommand(name.c_str());
  177. if(rm)
  178. {
  179. const char* versionValue
  180. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  181. int major = 0;
  182. int minor = 0;
  183. if ( versionValue )
  184. {
  185. sscanf(versionValue, "%d.%d", &major, &minor);
  186. }
  187. if ( rm->IsDeprecated(major, minor) )
  188. {
  189. cmOStringStream error;
  190. error << "Error in cmake code at\n"
  191. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  192. << rm->GetError();
  193. cmSystemTools::Error(error.str().c_str());
  194. return false;
  195. }
  196. cmCommand* usedCommand = rm->Clone();
  197. usedCommand->SetMakefile(this);
  198. bool keepCommand = false;
  199. if(usedCommand->GetEnabled() && !cmSystemTools::GetFatalErrorOccured() &&
  200. (!this->GetCMakeInstance()->GetScriptMode() ||
  201. usedCommand->IsScriptable()))
  202. {
  203. // if not running in inherit mode or
  204. // if the command is inherited then InitialPass it.
  205. if(!m_Inheriting || usedCommand->IsInherited())
  206. {
  207. if(!usedCommand->InvokeInitialPass(lff.m_Arguments))
  208. {
  209. cmOStringStream error;
  210. error << "Error in cmake code at\n"
  211. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  212. << usedCommand->GetError();
  213. cmSystemTools::Error(error.str().c_str());
  214. result = false;
  215. if ( this->GetCMakeInstance()->GetScriptMode() )
  216. {
  217. cmSystemTools::SetFatalErrorOccured();
  218. }
  219. }
  220. else
  221. {
  222. // use the command
  223. keepCommand = true;
  224. m_UsedCommands.push_back(usedCommand);
  225. }
  226. }
  227. }
  228. else if ( this->GetCMakeInstance()->GetScriptMode() && !usedCommand->IsScriptable() )
  229. {
  230. cmOStringStream error;
  231. error << "Error in cmake code at\n"
  232. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  233. << "Command " << usedCommand->GetName() << " not scriptable" << std::endl;
  234. cmSystemTools::Error(error.str().c_str());
  235. result = false;
  236. cmSystemTools::SetFatalErrorOccured();
  237. }
  238. // if the Cloned command was not used
  239. // then delete it
  240. if(!keepCommand)
  241. {
  242. delete usedCommand;
  243. }
  244. }
  245. else
  246. {
  247. if(!cmSystemTools::GetFatalErrorOccured())
  248. {
  249. cmOStringStream error;
  250. error << "Error in cmake code at\n"
  251. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  252. << "Unknown CMake command \"" << lff.m_Name.c_str() << "\".";
  253. cmSystemTools::Error(error.str().c_str());
  254. result = false;
  255. }
  256. }
  257. return result;
  258. }
  259. // Parse the given CMakeLists.txt file into a list of classes.
  260. // Reads in current CMakeLists file and all parent CMakeLists files
  261. // executing all inherited commands in the parents
  262. //
  263. // if external is non-zero, this means that we have branched to grab some
  264. // commands from a remote list-file (that is, the equivalent of a
  265. // #include has been called). We DO NOT look at the parents of this
  266. // list-file, and for all other purposes, the name of this list-file
  267. // is "filename" and not "external".
  268. bool cmMakefile::ReadListFile(const char* filename_in, const char* external_in)
  269. {
  270. // used to watch for blockers going out of scope
  271. // e.g. mismatched IF statement
  272. std::set<cmFunctionBlocker *> originalBlockers;
  273. const char* external = 0;
  274. std::string external_abs;
  275. const char* filename = filename_in;
  276. std::string filename_abs;
  277. if (external_in)
  278. {
  279. external_abs =
  280. cmSystemTools::CollapseFullPath(external_in,
  281. m_cmCurrentDirectory.c_str());
  282. external = external_abs.c_str();
  283. if (filename_in)
  284. {
  285. filename_abs =
  286. cmSystemTools::CollapseFullPath(filename_in,
  287. m_cmCurrentDirectory.c_str());
  288. filename = filename_abs.c_str();
  289. }
  290. }
  291. // keep track of the current file being read
  292. if (filename)
  293. {
  294. if(m_cmCurrentListFile != filename)
  295. {
  296. m_cmCurrentListFile = filename;
  297. }
  298. // loop over current function blockers and record them
  299. std::list<cmFunctionBlocker *>::iterator pos;
  300. for (pos = m_FunctionBlockers.begin();
  301. pos != m_FunctionBlockers.end(); ++pos)
  302. {
  303. originalBlockers.insert(*pos);
  304. }
  305. }
  306. // if this is not a remote makefile
  307. // (if it were, this would be called from the "filename" call,
  308. // rather than the "external" call)
  309. if (!external)
  310. {
  311. // is there a parent CMakeLists file that does not go beyond the
  312. // Home directory? if so recurse and read in that List file
  313. std::string parentList = this->GetParentListFileName(filename);
  314. if (parentList != "")
  315. {
  316. std::string srcdir = this->GetCurrentDirectory();
  317. std::string bindir = this->GetCurrentOutputDirectory();
  318. std::string::size_type pos = parentList.rfind('/');
  319. this->SetCurrentDirectory(parentList.substr(0, pos).c_str());
  320. this->SetCurrentOutputDirectory((m_HomeOutputDirectory +
  321. parentList.substr(m_cmHomeDirectory.size(),
  322. pos - m_cmHomeDirectory.size())).c_str());
  323. // if not found, oops
  324. if(pos == std::string::npos)
  325. {
  326. cmSystemTools::Error("Trailing slash not found");
  327. }
  328. this->ReadListFile(parentList.c_str());
  329. // restore the current directory
  330. this->SetCurrentDirectory(srcdir.c_str());
  331. this->SetCurrentOutputDirectory(bindir.c_str());
  332. }
  333. }
  334. // are we at the start CMakeLists file or are we processing a parent
  335. // lists file
  336. //
  337. // this might, or might not be true, irrespective if we are
  338. // off looking at an external makefile.
  339. m_Inheriting = (m_cmCurrentDirectory != m_cmStartDirectory);
  340. // Now read the input file
  341. const char *filenametoread= filename;
  342. if( external)
  343. {
  344. filenametoread= external;
  345. }
  346. // try to see if the list file is the top most
  347. // list file for a project, and if it is, then it
  348. // must have a project command. If there is not
  349. // one, then cmake will provide one via the
  350. // cmListFileCache class.
  351. bool requireProjectCommand = false;
  352. if(!external && m_cmCurrentDirectory == m_cmHomeDirectory)
  353. {
  354. if(cmSystemTools::LowerCase(
  355. cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
  356. {
  357. requireProjectCommand = true;
  358. }
  359. }
  360. cmListFile* lf =
  361. cmListFileCache::GetInstance()->GetFileCache(filenametoread,
  362. requireProjectCommand);
  363. if(!lf)
  364. {
  365. return false;
  366. }
  367. // add this list file to the list of dependencies
  368. m_ListFiles.push_back( filenametoread);
  369. const size_t numberFunctions = lf->m_Functions.size();
  370. for(size_t i =0; i < numberFunctions; ++i)
  371. {
  372. this->ExecuteCommand(lf->m_Functions[i]);
  373. }
  374. // send scope ended to and funciton blockers
  375. if (filename)
  376. {
  377. // loop over all function blockers to see if any block this command
  378. std::list<cmFunctionBlocker *>::iterator pos;
  379. for (pos = m_FunctionBlockers.begin();
  380. pos != m_FunctionBlockers.end(); ++pos)
  381. {
  382. // if this blocker was not in the original then send a
  383. // scope ended message
  384. if (originalBlockers.find(*pos) == originalBlockers.end())
  385. {
  386. (*pos)->ScopeEnded(*this);
  387. }
  388. }
  389. }
  390. return true;
  391. }
  392. void cmMakefile::AddCommand(cmCommand* wg)
  393. {
  394. this->GetCMakeInstance()->AddCommand(wg);
  395. }
  396. // Set the make file
  397. void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
  398. {
  399. m_LocalGenerator = lg;
  400. }
  401. void cmMakefile::FinalPass()
  402. {
  403. // do all the variable expansions here
  404. this->ExpandVariables();
  405. // give all the commands a chance to do something
  406. // after the file has been parsed before generation
  407. for(std::vector<cmCommand*>::iterator i = m_UsedCommands.begin();
  408. i != m_UsedCommands.end(); ++i)
  409. {
  410. (*i)->FinalPass();
  411. }
  412. }
  413. // Generate the output file
  414. void cmMakefile::ConfigureFinalPass()
  415. {
  416. this->FinalPass();
  417. const char* versionValue
  418. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  419. bool oldVersion = (!versionValue || atof(versionValue) < 1.4);
  420. // merge libraries
  421. for (cmTargets::iterator l = m_Targets.begin();
  422. l != m_Targets.end(); l++)
  423. {
  424. l->second.GenerateSourceFilesFromSourceLists(*this);
  425. // pick up any LINK_LIBRARIES that were added after the target
  426. if(oldVersion)
  427. {
  428. this->AddGlobalLinkInformation(l->first.c_str(), l->second);
  429. }
  430. l->second.AnalyzeLibDependencies(*this);
  431. }
  432. }
  433. // this is the old style signature, we convert to new style
  434. void cmMakefile::AddCustomCommand(const char* source,
  435. const char* command,
  436. const std::vector<std::string>& commandArgs,
  437. const std::vector<std::string>& depends,
  438. const std::vector<std::string>& outputs,
  439. const char *target,
  440. const char *comment)
  441. {
  442. if (strcmp(source,target))
  443. {
  444. // what a pain, for backwards compatibility we will try to
  445. // convert this to an output based rule... so for each output..
  446. for(std::vector<std::string>::const_iterator d = outputs.begin();
  447. d != outputs.end(); ++d)
  448. {
  449. // if this looks like a real file then use is as the main depend
  450. cmsys::RegularExpression SourceFiles("\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|rc|def|r|odl|idl|hpj|bat|h|h\\+\\+|hm|hpp|hxx|in|txx|inl)$");
  451. if (SourceFiles.find(source))
  452. {
  453. this->AddCustomCommandToOutput(d->c_str(), command, commandArgs,
  454. source, depends, comment);
  455. }
  456. // otherwise do not use a main depend
  457. else
  458. {
  459. std::vector<std::string> depends2 = depends;
  460. depends2.push_back(source);
  461. this->AddCustomCommandToOutput(d->c_str(), command, commandArgs,
  462. 0, depends2, comment);
  463. }
  464. // add the output to the target?
  465. std::string sname = *d;
  466. sname += ".rule";
  467. this->ExpandVariablesInString(sname);
  468. // if the rule was added to the source,
  469. // then add the source to the target
  470. if (!this->GetSource(sname.c_str()))
  471. {
  472. if (m_Targets.find(target) != m_Targets.end())
  473. {
  474. m_Targets[target].GetSourceLists().push_back(source);
  475. }
  476. else
  477. {
  478. cmSystemTools::Error("Attempt to add a custom rule to a target that does not exist yet for target ", target);
  479. return;
  480. }
  481. }
  482. }
  483. }
  484. else
  485. {
  486. this->AddCustomCommandToTarget(target, command, commandArgs,
  487. cmTarget::POST_BUILD, comment, depends);
  488. }
  489. }
  490. void cmMakefile::AddCustomCommand(const char* source,
  491. const char* command,
  492. const std::vector<std::string>& commandArgs,
  493. const std::vector<std::string>& depends,
  494. const char* output,
  495. const char *target)
  496. {
  497. std::vector<std::string> outputs;
  498. outputs.push_back(output);
  499. this->AddCustomCommand(source, command, commandArgs, depends,
  500. outputs, target);
  501. }
  502. void cmMakefile::
  503. AddCustomCommandToOutput(const char* outputIn,
  504. const char* inCommand,
  505. const std::vector<std::string>& commandArgs,
  506. const char *main_dependency,
  507. const std::vector<std::string>& depends,
  508. const char *comment,
  509. bool replace)
  510. {
  511. std::string expandC;
  512. std::string combinedArgs;
  513. std::string command = inCommand;
  514. // process the command's string
  515. this->ExpandVariablesInString(command);
  516. command = cmSystemTools::EscapeSpaces(command.c_str());
  517. unsigned int i;
  518. for (i = 0; i < commandArgs.size(); ++i)
  519. {
  520. expandC = commandArgs[i].c_str();
  521. this->ExpandVariablesInString(expandC);
  522. combinedArgs += cmSystemTools::EscapeSpaces(expandC.c_str());
  523. combinedArgs += " ";
  524. }
  525. cmSourceFile *file = 0;
  526. // setup the output name and make sure we expand any variables
  527. std::string output = outputIn;
  528. this->ExpandVariablesInString(output);
  529. std::string outName = output;
  530. outName += ".rule";
  531. // setup the main dependency name and expand vars of course
  532. std::string mainDepend;
  533. if (main_dependency && main_dependency[0] != '\0')
  534. {
  535. mainDepend = main_dependency;
  536. this->ExpandVariablesInString(mainDepend);
  537. }
  538. // OK this rule will be placed on a generated output file unless the main
  539. // depednency was specified.
  540. if (main_dependency && main_dependency[0] != '\0')
  541. {
  542. file = this->GetSource(mainDepend.c_str());
  543. if (file && file->GetCustomCommand() && !replace)
  544. {
  545. cmCustomCommand* cc = file->GetCustomCommand();
  546. // if the command and args are the same
  547. // as the command already there, then silently skip
  548. // this add command
  549. if(cc->IsEquivalent(command.c_str(), combinedArgs.c_str()))
  550. {
  551. return;
  552. }
  553. // generate a source instead
  554. file = 0;
  555. }
  556. else
  557. {
  558. file = this->GetOrCreateSource(mainDepend.c_str());
  559. }
  560. }
  561. if (!file)
  562. {
  563. file = this->GetSource(outName.c_str());
  564. if (file && file->GetCustomCommand() && !replace)
  565. {
  566. cmCustomCommand* cc = file->GetCustomCommand();
  567. // if the command and args are the same
  568. // as the command already there, then silently skip
  569. // this add command
  570. if(cc->IsEquivalent(command.c_str(), combinedArgs.c_str()))
  571. {
  572. return;
  573. }
  574. // produce error if two different commands are given to produce
  575. // the same output
  576. cmSystemTools::Error("Attempt to add a custom rule to an output that already"
  577. " has a custom rule. For output: ", outputIn);
  578. return;
  579. }
  580. // create a cmSourceFile for the output
  581. file = this->GetOrCreateSource(outName.c_str(), true);
  582. // always mark as generated
  583. file->SetProperty("GENERATED","1");
  584. }
  585. // always create the output and mark it generated
  586. cmSourceFile *out = this->GetOrCreateSource(output.c_str(), true);
  587. out->SetProperty("GENERATED","1");
  588. std::vector<std::string> depends2(depends);
  589. if (main_dependency && main_dependency[0] != '\0')
  590. {
  591. depends2.push_back(mainDepend.c_str());
  592. }
  593. cmCustomCommand *cc =
  594. new cmCustomCommand(command.c_str(),combinedArgs.c_str(),depends2,
  595. output.c_str());
  596. if ( comment && comment[0] )
  597. {
  598. cc->SetComment(comment);
  599. }
  600. if (file->GetCustomCommand())
  601. {
  602. delete file->GetCustomCommand();
  603. }
  604. file->SetCustomCommand(cc);
  605. }
  606. void cmMakefile::
  607. AddCustomCommandToTarget(const char* target, const char* command,
  608. const std::vector<std::string>& commandArgs,
  609. cmTarget::CustomCommandType type,
  610. const char *comment)
  611. {
  612. std::vector<std::string> empty;
  613. this->AddCustomCommandToTarget(target,command,commandArgs,type,
  614. comment, empty);
  615. }
  616. void cmMakefile::
  617. AddCustomCommandToTarget(const char* target, const char* command,
  618. const std::vector<std::string>& commandArgs,
  619. cmTarget::CustomCommandType type,
  620. const char *comment,
  621. const std::vector<std::string>& depends)
  622. {
  623. // find the target,
  624. if (m_Targets.find(target) != m_Targets.end())
  625. {
  626. std::string expandC = command;
  627. this->ExpandVariablesInString(expandC);
  628. std::string c = cmSystemTools::EscapeSpaces(expandC.c_str());
  629. std::string combinedArgs;
  630. unsigned int i;
  631. for (i = 0; i < commandArgs.size(); ++i)
  632. {
  633. expandC = commandArgs[i].c_str();
  634. this->ExpandVariablesInString(expandC);
  635. combinedArgs += cmSystemTools::EscapeSpaces(expandC.c_str());
  636. combinedArgs += " ";
  637. }
  638. cmCustomCommand cc(c.c_str(),combinedArgs.c_str(),depends,0);
  639. if ( comment && comment[0] )
  640. {
  641. cc.SetComment(comment);
  642. }
  643. switch (type)
  644. {
  645. case cmTarget::PRE_BUILD:
  646. m_Targets[target].GetPreBuildCommands().push_back(cc);
  647. break;
  648. case cmTarget::PRE_LINK:
  649. m_Targets[target].GetPreLinkCommands().push_back(cc);
  650. break;
  651. case cmTarget::POST_BUILD:
  652. m_Targets[target].GetPostBuildCommands().push_back(cc);
  653. break;
  654. }
  655. std::string cacheCommand = command;
  656. this->ExpandVariablesInString(cacheCommand);
  657. if(this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()))
  658. {
  659. m_Targets[target].AddUtility(
  660. this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()));
  661. }
  662. }
  663. }
  664. void cmMakefile::AddDefineFlag(const char* flag)
  665. {
  666. m_DefineFlags += " ";
  667. m_DefineFlags += flag;
  668. }
  669. void cmMakefile::AddLinkLibrary(const char* lib, cmTarget::LinkLibraryType llt)
  670. {
  671. m_LinkLibraries.push_back(
  672. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  673. }
  674. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  675. const char* lib,
  676. cmTarget::LinkLibraryType llt)
  677. {
  678. cmTargets::iterator i = m_Targets.find(target);
  679. if ( i != m_Targets.end())
  680. {
  681. i->second.AddLinkLibrary( *this, target, lib, llt );
  682. }
  683. else
  684. {
  685. cmSystemTools::Error("Attempt to add link libraries to non-existant target: ", target, " for lib ", lib);
  686. }
  687. }
  688. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  689. const char* d)
  690. {
  691. cmTargets::iterator i = m_Targets.find(target);
  692. if ( i != m_Targets.end())
  693. {
  694. i->second.AddLinkDirectory( d );
  695. }
  696. else
  697. {
  698. cmSystemTools::Error("Attempt to add link directories to non-existant target: ",
  699. target, " for directory ", d);
  700. }
  701. }
  702. void cmMakefile::AddLinkLibrary(const char* lib)
  703. {
  704. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  705. }
  706. void cmMakefile::AddLinkDirectory(const char* dir)
  707. {
  708. // Don't add a link directory that is already present. Yes, this
  709. // linear search results in n^2 behavior, but n won't be getting
  710. // much bigger than 20. We cannot use a set because of order
  711. // dependency of the link search path.
  712. // remove trailing slashes
  713. if(dir && dir[strlen(dir)-1] == '/')
  714. {
  715. std::string newdir = dir;
  716. newdir = newdir.substr(0, newdir.size()-1);
  717. if(std::find(m_LinkDirectories.begin(),
  718. m_LinkDirectories.end(), newdir.c_str()) == m_LinkDirectories.end())
  719. {
  720. m_LinkDirectories.push_back(newdir);
  721. }
  722. }
  723. else
  724. {
  725. if(std::find(m_LinkDirectories.begin(),
  726. m_LinkDirectories.end(), dir) == m_LinkDirectories.end())
  727. {
  728. m_LinkDirectories.push_back(dir);
  729. }
  730. }
  731. }
  732. void cmMakefile::AddSubDirectory(const char* sub)
  733. {
  734. // make sure it isn't already there
  735. if (std::find(m_SubDirectories.begin(),
  736. m_SubDirectories.end(), sub) == m_SubDirectories.end())
  737. {
  738. m_SubDirectories.push_back(sub);
  739. }
  740. }
  741. void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
  742. {
  743. // Don't add an include directory that is already present. Yes,
  744. // this linear search results in n^2 behavior, but n won't be
  745. // getting much bigger than 20. We cannot use a set because of
  746. // order dependency of the include path.
  747. if(std::find(m_IncludeDirectories.begin(),
  748. m_IncludeDirectories.end(), inc) == m_IncludeDirectories.end())
  749. {
  750. if (before)
  751. {
  752. // WARNING: this *is* expensive (linear time) since it's a vector
  753. m_IncludeDirectories.insert(m_IncludeDirectories.begin(), inc);
  754. }
  755. else
  756. {
  757. m_IncludeDirectories.push_back(inc);
  758. }
  759. }
  760. }
  761. void cmMakefile::AddDefinition(const char* name, const char* value)
  762. {
  763. if (!value )
  764. {
  765. return;
  766. }
  767. m_TemporaryDefinitionKey = name;
  768. m_Definitions[m_TemporaryDefinitionKey] = value;
  769. cmVariableWatch* vv = this->GetVariableWatch();
  770. if ( vv )
  771. {
  772. vv->VariableAccessed(m_TemporaryDefinitionKey,
  773. cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  774. }
  775. }
  776. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  777. const char* doc,
  778. cmCacheManager::CacheEntryType type)
  779. {
  780. const char* val = value;
  781. cmCacheManager::CacheIterator it =
  782. this->GetCacheManager()->GetCacheIterator(name);
  783. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  784. it.Initialized())
  785. {
  786. val = it.GetValue();
  787. if ( type == cmCacheManager::PATH || type == cmCacheManager::FILEPATH )
  788. {
  789. std::vector<std::string>::size_type cc;
  790. std::vector<std::string> files;
  791. std::string nvalue = "";
  792. cmSystemTools::ExpandListArgument(val, files);
  793. for ( cc = 0; cc < files.size(); cc ++ )
  794. {
  795. files[cc] = cmSystemTools::CollapseFullPath(files[cc].c_str());
  796. if ( cc > 0 )
  797. {
  798. nvalue += ";";
  799. }
  800. nvalue += files[cc];
  801. }
  802. this->GetCacheManager()->AddCacheEntry(name, nvalue.c_str(), doc, type);
  803. val = it.GetValue();
  804. }
  805. }
  806. this->GetCacheManager()->AddCacheEntry(name, val, doc, type);
  807. this->AddDefinition(name, val);
  808. }
  809. void cmMakefile::AddDefinition(const char* name, bool value)
  810. {
  811. if(value)
  812. {
  813. m_Definitions.erase( DefinitionMap::key_type(name));
  814. m_Definitions.insert(DefinitionMap::value_type(name, "ON"));
  815. }
  816. else
  817. {
  818. m_Definitions.erase( DefinitionMap::key_type(name));
  819. m_Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  820. }
  821. cmVariableWatch* vv = this->GetVariableWatch();
  822. if ( vv )
  823. {
  824. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  825. }
  826. }
  827. void cmMakefile::AddCacheDefinition(const char* name, bool value, const char* doc)
  828. {
  829. bool val = value;
  830. cmCacheManager::CacheIterator it =
  831. this->GetCacheManager()->GetCacheIterator(name);
  832. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  833. it.Initialized())
  834. {
  835. val = it.GetValueAsBool();
  836. }
  837. this->GetCacheManager()->AddCacheEntry(name, val, doc);
  838. this->AddDefinition(name, val);
  839. }
  840. void cmMakefile::RemoveDefinition(const char* name)
  841. {
  842. m_Definitions.erase(DefinitionMap::key_type(name));
  843. cmVariableWatch* vv = this->GetVariableWatch();
  844. if ( vv )
  845. {
  846. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS);
  847. }
  848. }
  849. void cmMakefile::SetProjectName(const char* p)
  850. {
  851. m_ProjectName = p;
  852. }
  853. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  854. {
  855. // for these targets do not add anything
  856. switch(target.GetType())
  857. {
  858. case cmTarget::UTILITY:
  859. case cmTarget::INSTALL_FILES:
  860. case cmTarget::INSTALL_PROGRAMS:
  861. return;
  862. default:;
  863. }
  864. std::vector<std::string>::iterator j;
  865. for(j = m_LinkDirectories.begin();
  866. j != m_LinkDirectories.end(); ++j)
  867. {
  868. target.AddLinkDirectory(j->c_str());
  869. }
  870. target.MergeLinkLibraries( *this, name, m_LinkLibraries );
  871. }
  872. void cmMakefile::AddLibrary(const char* lname, int shared,
  873. const std::vector<std::string> &srcs)
  874. {
  875. cmTarget target;
  876. switch (shared)
  877. {
  878. case 0:
  879. target.SetType(cmTarget::STATIC_LIBRARY);
  880. break;
  881. case 1:
  882. target.SetType(cmTarget::SHARED_LIBRARY);
  883. break;
  884. case 2:
  885. target.SetType(cmTarget::MODULE_LIBRARY);
  886. break;
  887. default:
  888. target.SetType(cmTarget::STATIC_LIBRARY);
  889. }
  890. // Clear its dependencies. Otherwise, dependencies might persist
  891. // over changes in CMakeLists.txt, making the information stale and
  892. // hence useless.
  893. target.ClearDependencyInformation( *this, lname );
  894. target.SetInAll(true);
  895. target.GetSourceLists() = srcs;
  896. this->AddGlobalLinkInformation(lname, target);
  897. m_Targets.insert(cmTargets::value_type(lname,target));
  898. // Add an entry into the cache
  899. std::string libPath = lname;
  900. libPath += "_CMAKE_PATH";
  901. this->GetCacheManager()->
  902. AddCacheEntry(libPath.c_str(),
  903. this->GetCurrentOutputDirectory(),
  904. "Path to a library", cmCacheManager::INTERNAL);
  905. // Add an entry into the cache
  906. std::string ltname = lname;
  907. ltname += "_LIBRARY_TYPE";
  908. switch (shared)
  909. {
  910. case 0:
  911. this->GetCacheManager()->AddCacheEntry(ltname.c_str(),"STATIC",
  912. "Whether a library is static, shared or module.",
  913. cmCacheManager::INTERNAL);
  914. break;
  915. case 1:
  916. this->GetCacheManager()->
  917. AddCacheEntry(ltname.c_str(),
  918. "SHARED",
  919. "Whether a library is static, shared or module.",
  920. cmCacheManager::INTERNAL);
  921. break;
  922. case 2:
  923. this->GetCacheManager()->
  924. AddCacheEntry(ltname.c_str(),
  925. "MODULE",
  926. "Whether a library is static, shared or module.",
  927. cmCacheManager::INTERNAL);
  928. break;
  929. default:
  930. this->GetCacheManager()->
  931. AddCacheEntry(ltname.c_str(),
  932. "STATIC",
  933. "Whether a library is static, shared or module.",
  934. cmCacheManager::INTERNAL);
  935. }
  936. }
  937. void cmMakefile::AddExecutable(const char *exeName,
  938. const std::vector<std::string> &srcs)
  939. {
  940. this->AddExecutable(exeName,srcs,false);
  941. }
  942. void cmMakefile::AddExecutable(const char *exeName,
  943. const std::vector<std::string> &srcs,
  944. bool win32)
  945. {
  946. cmTarget target;
  947. if (win32)
  948. {
  949. target.SetType(cmTarget::WIN32_EXECUTABLE);
  950. }
  951. else
  952. {
  953. target.SetType(cmTarget::EXECUTABLE);
  954. }
  955. target.SetInAll(true);
  956. target.GetSourceLists() = srcs;
  957. this->AddGlobalLinkInformation(exeName, target);
  958. m_Targets.insert(cmTargets::value_type(exeName,target));
  959. // Add an entry into the cache
  960. std::string exePath = exeName;
  961. exePath += "_CMAKE_PATH";
  962. this->GetCacheManager()->
  963. AddCacheEntry(exePath.c_str(),
  964. this->GetCurrentOutputDirectory(),
  965. "Path to an executable", cmCacheManager::INTERNAL);
  966. }
  967. void cmMakefile::AddUtilityCommand(const char* utilityName,
  968. const char* command,
  969. const char* arguments,
  970. bool all,
  971. const std::vector<std::string> &depends)
  972. {
  973. std::vector<std::string> empty;
  974. this->AddUtilityCommand(utilityName,command,arguments,all,
  975. depends, empty);
  976. }
  977. void cmMakefile::AddUtilityCommand(const char* utilityName,
  978. const char* command,
  979. const char* arguments,
  980. bool all,
  981. const std::vector<std::string> &dep,
  982. const std::vector<std::string> &out)
  983. {
  984. cmTarget target;
  985. target.SetType(cmTarget::UTILITY);
  986. target.SetInAll(all);
  987. if (out.size() > 1)
  988. {
  989. cmSystemTools::Error(
  990. "Utility targets can only have one output. For utilityNamed: ",
  991. utilityName);
  992. return;
  993. }
  994. if (out.size())
  995. {
  996. cmCustomCommand cc(command, arguments, dep, out[0].c_str());
  997. target.GetPostBuildCommands().push_back(cc);
  998. }
  999. else
  1000. {
  1001. cmCustomCommand cc(command, arguments, dep, (const char *)0);
  1002. target.GetPostBuildCommands().push_back(cc);
  1003. }
  1004. m_Targets.insert(cmTargets::value_type(utilityName,target));
  1005. }
  1006. cmSourceFile *cmMakefile::GetSourceFileWithOutput(const char *cname)
  1007. {
  1008. std::string name = cname;
  1009. // look through all the source files that have custom commands
  1010. // and see if the custom command has the passed source file as an output
  1011. // keep in mind the possible .rule extension that may be tacked on
  1012. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1013. i != m_SourceFiles.end(); ++i)
  1014. {
  1015. // does this source file have a custom command?
  1016. if ((*i)->GetCustomCommand())
  1017. {
  1018. // is the output of the custom command match the source files name
  1019. const std::string &out = (*i)->GetCustomCommand()->GetOutput();
  1020. if (out.rfind(name) != out.npos &&
  1021. out.rfind(name) == out.size() - name.size())
  1022. {
  1023. return *i;
  1024. }
  1025. }
  1026. }
  1027. // otherwise return NULL
  1028. return 0;
  1029. }
  1030. cmSourceGroup* cmMakefile::GetSourceGroup(const char* name)
  1031. {
  1032. // First see if the group exists. If so, replace its regular expression.
  1033. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  1034. sg != m_SourceGroups.end(); ++sg)
  1035. {
  1036. std::string sgName = sg->GetName();
  1037. if(sgName == name)
  1038. {
  1039. return &(*sg);
  1040. }
  1041. }
  1042. return 0;
  1043. }
  1044. void cmMakefile::AddSourceGroup(const char* name, const char* regex)
  1045. {
  1046. // First see if the group exists. If so, replace its regular expression.
  1047. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  1048. sg != m_SourceGroups.end(); ++sg)
  1049. {
  1050. std::string sgName = sg->GetName();
  1051. if(sgName == name)
  1052. {
  1053. if ( regex )
  1054. {
  1055. // We only want to set the regular expression. If there are already
  1056. // source files in the group, we don't want to remove them.
  1057. sg->SetGroupRegex(regex);
  1058. }
  1059. return;
  1060. }
  1061. }
  1062. // The group doesn't exist. Add it.
  1063. m_SourceGroups.push_back(cmSourceGroup(name, regex));
  1064. }
  1065. void cmMakefile::AddExtraDirectory(const char* dir)
  1066. {
  1067. m_AuxSourceDirectories.push_back(dir);
  1068. }
  1069. // return the file name for the parent CMakeLists file to the
  1070. // one passed in. Zero is returned if the CMakeLists file is the
  1071. // one in the home directory or if for some reason a parent cmake lists
  1072. // file cannot be found.
  1073. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  1074. {
  1075. // extract the directory name
  1076. std::string parentFile;
  1077. std::string listsDir = currentFileName;
  1078. std::string::size_type pos = listsDir.rfind('/');
  1079. // if we could not find the directory return 0
  1080. if(pos == std::string::npos)
  1081. {
  1082. return parentFile;
  1083. }
  1084. listsDir = listsDir.substr(0, pos);
  1085. // if we are in the home directory then stop, return 0
  1086. if(m_cmHomeDirectory == listsDir)
  1087. {
  1088. return parentFile;
  1089. }
  1090. // is there a parent directory we can check
  1091. pos = listsDir.rfind('/');
  1092. // if we could not find the directory return 0
  1093. if(pos == std::string::npos)
  1094. {
  1095. return parentFile;
  1096. }
  1097. listsDir = listsDir.substr(0, pos);
  1098. // is there a CMakeLists.txt file in the parent directory ?
  1099. parentFile = listsDir;
  1100. parentFile += "/CMakeLists.txt";
  1101. while(!cmSystemTools::FileExists(parentFile.c_str()))
  1102. {
  1103. // There is no CMakeLists.txt file in the parent directory. This
  1104. // can occur when coming out of a subdirectory resulting from a
  1105. // SUBDIRS(Foo/Bar) command (coming out of Bar into Foo). Try
  1106. // walking up until a CMakeLists.txt is found or the home
  1107. // directory is hit.
  1108. // if we are in the home directory then stop, return 0
  1109. if(m_cmHomeDirectory == listsDir) { return ""; }
  1110. // is there a parent directory we can check
  1111. pos = listsDir.rfind('/');
  1112. // if we could not find the directory return 0
  1113. if(pos == std::string::npos) { return ""; }
  1114. listsDir = listsDir.substr(0, pos);
  1115. parentFile = listsDir;
  1116. parentFile += "/CMakeLists.txt";
  1117. }
  1118. return parentFile;
  1119. }
  1120. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  1121. // include and library directories.
  1122. void cmMakefile::ExpandVariables()
  1123. {
  1124. // Now expand variables in the include and link strings
  1125. for(std::vector<std::string>::iterator d = m_IncludeDirectories.begin();
  1126. d != m_IncludeDirectories.end(); ++d)
  1127. {
  1128. this->ExpandVariablesInString(*d);
  1129. }
  1130. for(std::vector<std::string>::iterator d = m_LinkDirectories.begin();
  1131. d != m_LinkDirectories.end(); ++d)
  1132. {
  1133. this->ExpandVariablesInString(*d);
  1134. }
  1135. for(cmTarget::LinkLibraries::iterator l = m_LinkLibraries.begin();
  1136. l != m_LinkLibraries.end(); ++l)
  1137. {
  1138. this->ExpandVariablesInString(l->first);
  1139. }
  1140. }
  1141. void cmMakefile::ExpandVariablesInCustomCommands()
  1142. {
  1143. // do source files
  1144. for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
  1145. i != m_SourceFiles.end(); ++i)
  1146. {
  1147. cmCustomCommand *cc = (*i)->GetCustomCommand();
  1148. if (cc)
  1149. {
  1150. cc->ExpandVariables(*this);
  1151. }
  1152. }
  1153. // now do targets
  1154. std::vector<cmCustomCommand>::iterator ic;
  1155. for (cmTargets::iterator l = m_Targets.begin();
  1156. l != m_Targets.end(); l++)
  1157. {
  1158. for (ic = l->second.GetPreBuildCommands().begin();
  1159. ic != l->second.GetPreBuildCommands().end(); ++ic)
  1160. {
  1161. ic->ExpandVariables(*this);
  1162. }
  1163. for (ic = l->second.GetPreLinkCommands().begin();
  1164. ic != l->second.GetPreLinkCommands().end(); ++ic)
  1165. {
  1166. ic->ExpandVariables(*this);
  1167. }
  1168. for (ic = l->second.GetPostBuildCommands().begin();
  1169. ic != l->second.GetPostBuildCommands().end(); ++ic)
  1170. {
  1171. ic->ExpandVariables(*this);
  1172. }
  1173. }
  1174. }
  1175. bool cmMakefile::IsOn(const char* name) const
  1176. {
  1177. const char* value = this->GetDefinition(name);
  1178. return cmSystemTools::IsOn(value);
  1179. }
  1180. const char* cmMakefile::GetDefinition(const char* name) const
  1181. {
  1182. const char* def = 0;
  1183. DefinitionMap::const_iterator pos = m_Definitions.find(name);
  1184. if(pos != m_Definitions.end())
  1185. {
  1186. def = (*pos).second.c_str();
  1187. }
  1188. else
  1189. {
  1190. def = this->GetCacheManager()->GetCacheValue(name);
  1191. }
  1192. cmVariableWatch* vv = this->GetVariableWatch();
  1193. if ( vv )
  1194. {
  1195. if ( def )
  1196. {
  1197. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS);
  1198. }
  1199. else
  1200. {
  1201. // are unknown access allowed
  1202. DefinitionMap::const_iterator pos2 =
  1203. m_Definitions.find("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  1204. if (pos2 != m_Definitions.end() &&
  1205. cmSystemTools::IsOn((*pos2).second.c_str()))
  1206. {
  1207. vv->VariableAccessed(name,
  1208. cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS);
  1209. }
  1210. else
  1211. {
  1212. vv->VariableAccessed(name, cmVariableWatch::
  1213. UNKNOWN_VARIABLE_READ_ACCESS);
  1214. }
  1215. }
  1216. }
  1217. return def;
  1218. }
  1219. std::vector<std::string> cmMakefile::GetDefinitions(int cacheonly /* = 0 */) const
  1220. {
  1221. std::map<std::string, int> definitions;
  1222. if ( !cacheonly )
  1223. {
  1224. DefinitionMap::const_iterator it;
  1225. for ( it = m_Definitions.begin(); it != m_Definitions.end(); it ++ )
  1226. {
  1227. definitions[it->first] = 1;
  1228. }
  1229. }
  1230. cmCacheManager::CacheIterator cit = this->GetCacheManager()->GetCacheIterator();
  1231. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  1232. {
  1233. definitions[cit.GetName()] = 1;
  1234. }
  1235. std::vector<std::string> res;
  1236. std::map<std::string, int>::iterator fit;
  1237. for ( fit = definitions.begin(); fit != definitions.end(); fit ++ )
  1238. {
  1239. res.push_back(fit->first);
  1240. }
  1241. return res;
  1242. }
  1243. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  1244. {
  1245. return this->ExpandVariablesInString(source, false);
  1246. }
  1247. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  1248. bool escapeQuotes,
  1249. bool atOnly) const
  1250. {
  1251. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  1252. // with GetDefinition(), if not found in the map, nothing is expanded.
  1253. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  1254. // the current environment variables.
  1255. // start by look for $ or @ in the string
  1256. std::string::size_type markerPos;
  1257. if(atOnly)
  1258. {
  1259. markerPos = source.find_first_of("@");
  1260. }
  1261. else
  1262. {
  1263. markerPos = source.find_first_of("$@");
  1264. }
  1265. // if not found, or found as the last character, then leave quickly as
  1266. // nothing needs to be expanded
  1267. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  1268. {
  1269. return source.c_str();
  1270. }
  1271. // current position
  1272. std::string::size_type currentPos =0; // start at 0
  1273. std::string result; // string with replacements
  1274. // go until the the end of the string
  1275. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  1276. {
  1277. // grab string from currentPos to the start of the variable
  1278. // and add it to the result
  1279. result += source.substr(currentPos, markerPos - currentPos);
  1280. char endVariableMarker; // what is the end of the variable @ or }
  1281. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  1282. if(!atOnly && source[markerPos] == '$')
  1283. {
  1284. // ${var} case
  1285. if(source[markerPos+1] == '{')
  1286. {
  1287. endVariableMarker = '}';
  1288. markerStartSize = 2;
  1289. }
  1290. // $ENV{var} case
  1291. else if(markerPos+4 < source.size() &&
  1292. source[markerPos+4] == '{' &&
  1293. !source.substr(markerPos+1, 3).compare("ENV"))
  1294. {
  1295. endVariableMarker = '}';
  1296. markerStartSize = 5;
  1297. }
  1298. else
  1299. {
  1300. // bogus $ with no { so add $ to result and move on
  1301. result += '$'; // add bogus $ back into string
  1302. currentPos = markerPos+1; // move on
  1303. endVariableMarker = ' '; // set end var to space so we can tell bogus
  1304. }
  1305. }
  1306. else
  1307. {
  1308. // @VAR case
  1309. endVariableMarker = '@';
  1310. }
  1311. // if it was a valid variable (started with @ or ${ or $ENV{ )
  1312. if(endVariableMarker != ' ')
  1313. {
  1314. markerPos += markerStartSize; // move past marker
  1315. // find the end variable marker starting at the markerPos
  1316. std::string::size_type endVariablePos =
  1317. source.find(endVariableMarker, markerPos);
  1318. if(endVariablePos == std::string::npos)
  1319. {
  1320. // no end marker found so add the bogus start
  1321. if(endVariableMarker == '@')
  1322. {
  1323. result += '@';
  1324. }
  1325. else
  1326. {
  1327. result += (markerStartSize == 5 ? "$ENV{" : "${");
  1328. }
  1329. currentPos = markerPos;
  1330. }
  1331. else
  1332. {
  1333. // good variable remove it
  1334. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  1335. bool found = false;
  1336. if (markerStartSize == 5) // $ENV{
  1337. {
  1338. char *ptr = getenv(var.c_str());
  1339. if (ptr)
  1340. {
  1341. if (escapeQuotes)
  1342. {
  1343. result += cmSystemTools::EscapeQuotes(ptr);
  1344. }
  1345. else
  1346. {
  1347. result += ptr;
  1348. }
  1349. found = true;
  1350. }
  1351. }
  1352. else
  1353. {
  1354. const char* lookup = this->GetDefinition(var.c_str());
  1355. if(lookup)
  1356. {
  1357. if (escapeQuotes)
  1358. {
  1359. result += cmSystemTools::EscapeQuotes(lookup);
  1360. }
  1361. else
  1362. {
  1363. result += lookup;
  1364. }
  1365. found = true;
  1366. }
  1367. }
  1368. // if found add to result, if not, then it gets blanked
  1369. if (!found)
  1370. {
  1371. // if no definition is found then add the var back
  1372. if(endVariableMarker == '@')
  1373. {
  1374. result += "@";
  1375. result += var;
  1376. result += "@";
  1377. }
  1378. }
  1379. // lookup var, and replace it
  1380. currentPos = endVariablePos+1;
  1381. }
  1382. }
  1383. if(atOnly)
  1384. {
  1385. markerPos = source.find_first_of("@", currentPos);
  1386. }
  1387. else
  1388. {
  1389. markerPos = source.find_first_of("$@", currentPos);
  1390. }
  1391. }
  1392. result += source.substr(currentPos); // pick up the rest of the string
  1393. source = result;
  1394. return source.c_str();
  1395. }
  1396. void cmMakefile::RemoveVariablesInString(std::string& source,
  1397. bool atOnly) const
  1398. {
  1399. if(!atOnly)
  1400. {
  1401. cmsys::RegularExpression var("(\\${[A-Za-z_0-9]*})");
  1402. while (var.find(source))
  1403. {
  1404. source.erase(var.start(),var.end() - var.start());
  1405. }
  1406. }
  1407. if(!atOnly)
  1408. {
  1409. cmsys::RegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  1410. while (varb.find(source))
  1411. {
  1412. source.erase(varb.start(),varb.end() - varb.start());
  1413. }
  1414. }
  1415. cmsys::RegularExpression var2("(@[A-Za-z_0-9]*@)");
  1416. while (var2.find(source))
  1417. {
  1418. source.erase(var2.start(),var2.end() - var2.start());
  1419. }
  1420. }
  1421. /**
  1422. * Add the default definitions to the makefile. These values must not
  1423. * be dependent on anything that isn't known when this cmMakefile instance
  1424. * is constructed.
  1425. */
  1426. void cmMakefile::AddDefaultDefinitions()
  1427. {
  1428. #if defined(_WIN32) || defined(__CYGWIN__)
  1429. this->AddDefinition("WIN32", "1");
  1430. #else
  1431. this->AddDefinition("UNIX", "1");
  1432. #endif
  1433. // Cygwin is more like unix so enable the unix commands
  1434. #if defined(__CYGWIN__)
  1435. this->AddDefinition("UNIX", "1");
  1436. this->AddDefinition("CYGWIN", "1");
  1437. #endif
  1438. #if defined(__APPLE__)
  1439. this->AddDefinition("APPLE", "1");
  1440. #endif
  1441. char temp[1024];
  1442. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1443. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1444. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1445. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1446. }
  1447. /**
  1448. * Find a source group whose regular expression matches the filename
  1449. * part of the given source name. Search backward through the list of
  1450. * source groups, and take the first matching group found. This way
  1451. * non-inherited SOURCE_GROUP commands will have precedence over
  1452. * inherited ones.
  1453. */
  1454. cmSourceGroup&
  1455. cmMakefile::FindSourceGroup(const char* source,
  1456. std::vector<cmSourceGroup> &groups)
  1457. {
  1458. // First search for a group that lists the file explicitly.
  1459. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1460. sg != groups.rend(); ++sg)
  1461. {
  1462. if(sg->MatchesFiles(source))
  1463. {
  1464. return *sg;
  1465. }
  1466. }
  1467. // Now search for a group whose regex matches the file.
  1468. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1469. sg != groups.rend(); ++sg)
  1470. {
  1471. if(sg->MatchesRegex(source))
  1472. {
  1473. return *sg;
  1474. }
  1475. }
  1476. // Shouldn't get here, but just in case, return the default group.
  1477. return groups.front();
  1478. }
  1479. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff)
  1480. {
  1481. // if there are no blockers get out of here
  1482. if (m_FunctionBlockers.begin() == m_FunctionBlockers.end())
  1483. {
  1484. return false;
  1485. }
  1486. // loop over all function blockers to see if any block this command
  1487. // evaluate in reverse, this is critical for balanced IF statements etc
  1488. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1489. for (pos = m_FunctionBlockers.rbegin();
  1490. pos != m_FunctionBlockers.rend(); ++pos)
  1491. {
  1492. if((*pos)->IsFunctionBlocked(lff, *this))
  1493. {
  1494. return true;
  1495. }
  1496. }
  1497. return false;
  1498. }
  1499. void cmMakefile::ExpandArguments(
  1500. std::vector<cmListFileArgument> const& inArgs,
  1501. std::vector<std::string>& outArgs)
  1502. {
  1503. std::vector<cmListFileArgument>::const_iterator i;
  1504. std::string value;
  1505. outArgs.reserve(inArgs.size());
  1506. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  1507. {
  1508. // Expand the variables in the argument.
  1509. value = i->Value;
  1510. this->ExpandVariablesInString(value);
  1511. // If the argument is quoted, it should be one argument.
  1512. // Otherwise, it may be a list of arguments.
  1513. if(i->Quoted)
  1514. {
  1515. outArgs.push_back(value);
  1516. }
  1517. else
  1518. {
  1519. cmSystemTools::ExpandListArgument(value, outArgs);
  1520. }
  1521. }
  1522. }
  1523. void cmMakefile::RemoveFunctionBlocker(const cmListFileFunction& lff)
  1524. {
  1525. // loop over all function blockers to see if any block this command
  1526. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1527. for (pos = m_FunctionBlockers.rbegin();
  1528. pos != m_FunctionBlockers.rend(); ++pos)
  1529. {
  1530. if ((*pos)->ShouldRemove(lff, *this))
  1531. {
  1532. cmFunctionBlocker* b = *pos;
  1533. m_FunctionBlockers.remove(b);
  1534. delete b;
  1535. break;
  1536. }
  1537. }
  1538. return;
  1539. }
  1540. void cmMakefile::SetHomeDirectory(const char* dir)
  1541. {
  1542. m_cmHomeDirectory = dir;
  1543. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1544. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1545. }
  1546. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1547. {
  1548. m_HomeOutputDirectory = lib;
  1549. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1550. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1551. }
  1552. /**
  1553. * Register the given cmData instance with its own name.
  1554. */
  1555. void cmMakefile::RegisterData(cmData* data)
  1556. {
  1557. std::string name = data->GetName();
  1558. DataMap::const_iterator d = m_DataMap.find(name);
  1559. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1560. {
  1561. delete d->second;
  1562. }
  1563. m_DataMap[name] = data;
  1564. }
  1565. /**
  1566. * Register the given cmData instance with the given name. This can be used
  1567. * to register a NULL pointer.
  1568. */
  1569. void cmMakefile::RegisterData(const char* name, cmData* data)
  1570. {
  1571. DataMap::const_iterator d = m_DataMap.find(name);
  1572. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1573. {
  1574. delete d->second;
  1575. }
  1576. m_DataMap[name] = data;
  1577. }
  1578. /**
  1579. * Lookup a cmData instance previously registered with the given name. If
  1580. * the instance cannot be found, return NULL.
  1581. */
  1582. cmData* cmMakefile::LookupData(const char* name) const
  1583. {
  1584. DataMap::const_iterator d = m_DataMap.find(name);
  1585. if(d != m_DataMap.end())
  1586. {
  1587. return d->second;
  1588. }
  1589. else
  1590. {
  1591. return 0;
  1592. }
  1593. }
  1594. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  1595. {
  1596. // if the source is provided with a full path use it, otherwise
  1597. // by default it is in the current source dir
  1598. std::string path = cmSystemTools::GetFilenamePath(sourceName);
  1599. if (path.empty())
  1600. {
  1601. path = this->GetCurrentDirectory();
  1602. }
  1603. std::string sname =
  1604. cmSystemTools::GetFilenameWithoutLastExtension(sourceName);
  1605. // compute the extension
  1606. std::string ext
  1607. = cmSystemTools::GetFilenameLastExtension(sourceName);
  1608. if ( ext.length() && ext[0] == '.' )
  1609. {
  1610. ext = ext.substr(1);
  1611. }
  1612. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1613. i != m_SourceFiles.end(); ++i)
  1614. {
  1615. if ((*i)->GetSourceName() == sname &&
  1616. cmSystemTools::GetFilenamePath((*i)->GetFullPath()) == path &&
  1617. (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1618. {
  1619. return *i;
  1620. }
  1621. }
  1622. // geeze, if it wasn't found maybe it is listed under the output dir
  1623. if (!cmSystemTools::GetFilenamePath(sourceName).empty())
  1624. {
  1625. return 0;
  1626. }
  1627. path = this->GetCurrentOutputDirectory();
  1628. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1629. i != m_SourceFiles.end(); ++i)
  1630. {
  1631. if ((*i)->GetSourceName() == sname &&
  1632. cmSystemTools::GetFilenamePath((*i)->GetFullPath()) == path &&
  1633. (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1634. {
  1635. return *i;
  1636. }
  1637. }
  1638. return 0;
  1639. }
  1640. cmSourceFile* cmMakefile::GetOrCreateSource(const char* sourceName,
  1641. bool generated)
  1642. {
  1643. // make it a full path first
  1644. std::string src = sourceName;
  1645. bool relative = !cmSystemTools::FileIsFullPath(sourceName);
  1646. std::string srcTreeFile = this->GetCurrentDirectory();
  1647. srcTreeFile += "/";
  1648. srcTreeFile += sourceName;
  1649. if(relative)
  1650. {
  1651. src = srcTreeFile;
  1652. }
  1653. // check to see if it exists
  1654. cmSourceFile* ret = this->GetSource(src.c_str());
  1655. if (ret)
  1656. {
  1657. return ret;
  1658. }
  1659. // OK a source file object doesn't exist for the source
  1660. // maybe we made a bad call on assuming it was in the src tree
  1661. std::string buildTreeFile = this->GetCurrentOutputDirectory();
  1662. buildTreeFile += "/";
  1663. buildTreeFile += sourceName;
  1664. if (relative)
  1665. {
  1666. src = buildTreeFile;
  1667. ret = this->GetSource(src.c_str());
  1668. if (ret)
  1669. {
  1670. return ret;
  1671. }
  1672. // if it has not been marked generated check to see if it exists in the
  1673. // src tree
  1674. if(!generated)
  1675. {
  1676. // see if the file is in the source tree, otherwise assume it
  1677. // is in the binary tree
  1678. if (cmSystemTools::FileExists(srcTreeFile.c_str()) &&
  1679. !cmSystemTools::FileIsDirectory(srcTreeFile.c_str()))
  1680. {
  1681. src = srcTreeFile;
  1682. }
  1683. else
  1684. {
  1685. if ( cmSystemTools::GetFilenameLastExtension(srcTreeFile.c_str()).size() == 0)
  1686. {
  1687. if (cmSystemTools::DoesFileExistWithExtensions(
  1688. srcTreeFile.c_str(), this->GetSourceExtensions()))
  1689. {
  1690. src = srcTreeFile;
  1691. }
  1692. else if (cmSystemTools::DoesFileExistWithExtensions(
  1693. srcTreeFile.c_str(), this->GetHeaderExtensions()))
  1694. {
  1695. src = srcTreeFile;
  1696. }
  1697. }
  1698. }
  1699. }
  1700. }
  1701. // a cmSourceFile instance does not exist yet so we must create one
  1702. // go back to looking in the source directory for it
  1703. // we must create one
  1704. cmSourceFile file;
  1705. std::string path = cmSystemTools::GetFilenamePath(src);
  1706. if(generated)
  1707. {
  1708. std::string ext = cmSystemTools::GetFilenameLastExtension(src);
  1709. std::string name_no_ext = cmSystemTools::GetFilenameName(src.c_str());
  1710. name_no_ext = name_no_ext.substr(0, name_no_ext.length()-ext.length());
  1711. if ( ext.length() && ext[0] == '.' )
  1712. {
  1713. ext = ext.substr(1);
  1714. }
  1715. file.SetName(name_no_ext.c_str(), path.c_str(), ext.c_str(), false);
  1716. }
  1717. else
  1718. {
  1719. file.SetName(cmSystemTools::GetFilenameName(src.c_str()).c_str(),
  1720. path.c_str(),
  1721. this->GetSourceExtensions(),
  1722. this->GetHeaderExtensions());
  1723. }
  1724. // add the source file to the makefile
  1725. this->AddSource(file);
  1726. src = file.GetFullPath();
  1727. ret = this->GetSource(src.c_str());
  1728. if (!ret)
  1729. {
  1730. cmSystemTools::Error(
  1731. "CMake failed to properly look up cmSourceFile: ", sourceName);
  1732. }
  1733. return ret;
  1734. }
  1735. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  1736. {
  1737. // check to see if it exists
  1738. cmSourceFile* ret = this->GetSource(sf.GetFullPath().c_str());
  1739. if(ret)
  1740. {
  1741. return ret;
  1742. }
  1743. ret = new cmSourceFile(sf);
  1744. m_SourceFiles.push_back(ret);
  1745. return ret;
  1746. }
  1747. void cmMakefile::EnableLanguage(const char* lang)
  1748. {
  1749. m_LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this);
  1750. }
  1751. void cmMakefile::ExpandSourceListArguments(
  1752. std::vector<std::string> const& arguments,
  1753. std::vector<std::string>& newargs, unsigned int start)
  1754. {
  1755. // first figure out if we need to handle version 1.2 style source lists
  1756. int oldVersion = 1;
  1757. const char* versionValue
  1758. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  1759. if (versionValue && atof(versionValue) > 1.2)
  1760. {
  1761. oldVersion = 0;
  1762. }
  1763. // now expand the args
  1764. unsigned int i;
  1765. for(i = 0; i < arguments.size(); ++i)
  1766. {
  1767. // is the arg defined ?, if so use the def
  1768. const char *def = this->GetDefinition(arguments[i].c_str());
  1769. if (def && oldVersion && i >= start)
  1770. {
  1771. // Definition lookup could result in a list that needs to be
  1772. // expanded.
  1773. cmSystemTools::ExpandListArgument(def, newargs);
  1774. }
  1775. else
  1776. {
  1777. // List expansion will have been done already.
  1778. newargs.push_back(arguments[i]);
  1779. }
  1780. }
  1781. }
  1782. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  1783. const char *projectName, const char *targetName,
  1784. const std::vector<std::string> *cmakeArgs,
  1785. std::string *output)
  1786. {
  1787. // does the binary directory exist ? If not create it...
  1788. if (!cmSystemTools::FileIsDirectory(bindir))
  1789. {
  1790. cmSystemTools::MakeDirectory(bindir);
  1791. }
  1792. // change to the tests directory and run cmake
  1793. // use the cmake object instead of calling cmake
  1794. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1795. cmSystemTools::ChangeDirectory(bindir);
  1796. // make sure the same generator is used
  1797. // use this program as the cmake to be run, it should not
  1798. // be run that way but the cmake object requires a vailid path
  1799. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  1800. cmake cm;
  1801. cm.SetIsInTryCompile(true);
  1802. cmGlobalGenerator *gg =
  1803. cm.CreateGlobalGenerator(m_LocalGenerator->GetGlobalGenerator()->GetName());
  1804. if (!gg)
  1805. {
  1806. cmSystemTools::Error(
  1807. "Internal CMake error, TryCompile bad GlobalGenerator");
  1808. // return to the original directory
  1809. cmSystemTools::ChangeDirectory(cwd.c_str());
  1810. return 1;
  1811. }
  1812. cm.SetGlobalGenerator(gg);
  1813. // do a configure
  1814. cm.SetHomeDirectory(srcdir);
  1815. cm.SetHomeOutputDirectory(bindir);
  1816. cm.SetStartDirectory(srcdir);
  1817. cm.SetStartOutputDirectory(bindir);
  1818. cm.SetCMakeCommand(cmakeCommand.c_str());
  1819. cm.LoadCache();
  1820. // if cmake args were provided then pass them in
  1821. if (cmakeArgs)
  1822. {
  1823. cm.SetCacheArgs(*cmakeArgs);
  1824. }
  1825. // to save time we pass the EnableLanguage info directly
  1826. gg->EnableLanguagesFromGenerator(m_LocalGenerator->GetGlobalGenerator());
  1827. if (cm.Configure() != 0)
  1828. {
  1829. cmSystemTools::Error(
  1830. "Internal CMake error, TryCompile configure of cmake failed");
  1831. // return to the original directory
  1832. cmSystemTools::ChangeDirectory(cwd.c_str());
  1833. return 1;
  1834. }
  1835. if (cm.Generate() != 0)
  1836. {
  1837. cmSystemTools::Error(
  1838. "Internal CMake error, TryCompile generation of cmake failed");
  1839. // return to the original directory
  1840. cmSystemTools::ChangeDirectory(cwd.c_str());
  1841. return 1;
  1842. }
  1843. // finally call the generator to actually build the resulting project
  1844. int ret =
  1845. m_LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  1846. projectName,
  1847. targetName,
  1848. output);
  1849. cmSystemTools::ChangeDirectory(cwd.c_str());
  1850. return ret;
  1851. }
  1852. cmake *cmMakefile::GetCMakeInstance() const
  1853. {
  1854. if ( m_LocalGenerator && m_LocalGenerator->GetGlobalGenerator() )
  1855. {
  1856. return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  1857. }
  1858. return 0;
  1859. }
  1860. cmVariableWatch *cmMakefile::GetVariableWatch() const
  1861. {
  1862. if ( this->GetCMakeInstance() &&
  1863. this->GetCMakeInstance()->GetVariableWatch() )
  1864. {
  1865. return this->GetCMakeInstance()->GetVariableWatch();
  1866. }
  1867. return 0;
  1868. }
  1869. void cmMakefile::AddMacro(const char* name, const char* signature)
  1870. {
  1871. if ( !name || !signature )
  1872. {
  1873. return;
  1874. }
  1875. m_MacrosMap[name] = signature;
  1876. }
  1877. void cmMakefile::GetListOfMacros(std::string& macros)
  1878. {
  1879. StringStringMap::iterator it;
  1880. macros = "";
  1881. int cc = 0;
  1882. for ( it = m_MacrosMap.begin(); it != m_MacrosMap.end(); ++it )
  1883. {
  1884. if ( cc > 0 )
  1885. {
  1886. macros += ";";
  1887. }
  1888. macros += it->first;
  1889. cc ++;
  1890. }
  1891. }
  1892. cmCacheManager *cmMakefile::GetCacheManager() const
  1893. {
  1894. return this->GetCMakeInstance()->GetCacheManager();
  1895. }
  1896. bool cmMakefile::GetLocal() const
  1897. {
  1898. return this->GetCMakeInstance()->GetLocal();
  1899. }
  1900. void cmMakefile::DisplayStatus(const char* message, float s)
  1901. {
  1902. this->GetLocalGenerator()->GetGlobalGenerator()
  1903. ->GetCMakeInstance()->UpdateProgress(message, s);
  1904. }
  1905. /**
  1906. * Find the library with the given name. Searches the given path and then
  1907. * the system search path. Returns the full path to the library if it is
  1908. * found. Otherwise, the empty string is returned.
  1909. */
  1910. std::string cmMakefile::FindLibrary(const char* name,
  1911. const std::vector<std::string>& userPaths)
  1912. {
  1913. // See if the executable exists as written.
  1914. if(cmSystemTools::FileExists(name))
  1915. {
  1916. return cmSystemTools::CollapseFullPath(name);
  1917. }
  1918. // Add the system search path to our path.
  1919. std::vector<std::string> path = userPaths;
  1920. cmSystemTools::GetPath(path);
  1921. // Add some lib directories specific to compilers, depending on the
  1922. // current generator, so that library that might have been stored here
  1923. // can be found too.
  1924. // i.e. Microsoft Visual Studio or .Net: path to compiler/../Lib
  1925. // Borland: path to compiler/../Lib
  1926. const char* genName = this->GetDefinition("CMAKE_GENERATOR");
  1927. if (genName)
  1928. {
  1929. if (!strcmp(genName, "NMake Makefiles") ||
  1930. !strcmp(genName, "Visual Studio 6"))
  1931. {
  1932. const char* compiler = this->GetDefinition("CMAKE_CXX_COMPILER");
  1933. if (compiler)
  1934. {
  1935. std::string compiler_path = cmSystemTools::FindProgram(compiler);
  1936. if (compiler_path.size())
  1937. {
  1938. std::string lib_path =
  1939. cmSystemTools::GetFilenamePath(
  1940. cmSystemTools::GetFilenamePath(compiler_path)) + "/Lib";
  1941. path.push_back(lib_path);
  1942. }
  1943. }
  1944. }
  1945. else if (!strcmp(genName, "Visual Studio 7"))
  1946. {
  1947. // It is likely that the compiler won't be in the path for .Net, but
  1948. // we know where devenv is.
  1949. const char* devenv = this->GetDefinition("MICROSOFT_DEVENV");
  1950. if (devenv)
  1951. {
  1952. std::string devenv_path = cmSystemTools::FindProgram(devenv);
  1953. if (devenv_path.size())
  1954. {
  1955. std::string vc7_path =
  1956. cmSystemTools::GetFilenamePath(
  1957. cmSystemTools::GetFilenamePath(
  1958. cmSystemTools::GetFilenamePath(devenv_path))) + "/Vc7";
  1959. path.push_back(vc7_path + "/lib");
  1960. path.push_back(vc7_path + "/PlatformSDK/lib");
  1961. }
  1962. }
  1963. }
  1964. else if (!strcmp(genName, "Borland Makefiles"))
  1965. {
  1966. const char* bcb_bin_path = this->GetDefinition("BCB_BIN_PATH");
  1967. if (bcb_bin_path)
  1968. {
  1969. std::string lib_path =
  1970. cmSystemTools::GetFilenamePath(bcb_bin_path) + "/Lib";
  1971. path.push_back(lib_path);
  1972. }
  1973. }
  1974. }
  1975. return cmSystemTools::FindLibrary(name, path);
  1976. }