cmMakefile.cxx 69 KB

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