cmMakefile.cxx 73 KB

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