cmMakefile.cxx 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  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. if(std::find(m_IncludeDirectories.begin(),
  804. m_IncludeDirectories.end(), inc) == m_IncludeDirectories.end())
  805. {
  806. if (before)
  807. {
  808. // WARNING: this *is* expensive (linear time) since it's a vector
  809. m_IncludeDirectories.insert(m_IncludeDirectories.begin(), inc);
  810. }
  811. else
  812. {
  813. m_IncludeDirectories.push_back(inc);
  814. }
  815. }
  816. }
  817. void cmMakefile::AddDefinition(const char* name, const char* value)
  818. {
  819. if (!value )
  820. {
  821. return;
  822. }
  823. m_TemporaryDefinitionKey = name;
  824. m_Definitions[m_TemporaryDefinitionKey] = value;
  825. #ifdef CMAKE_BUILD_WITH_CMAKE
  826. cmVariableWatch* vv = this->GetVariableWatch();
  827. if ( vv )
  828. {
  829. vv->VariableAccessed(m_TemporaryDefinitionKey,
  830. cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  831. }
  832. #endif
  833. }
  834. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  835. const char* doc,
  836. cmCacheManager::CacheEntryType type)
  837. {
  838. const char* val = value;
  839. cmCacheManager::CacheIterator it =
  840. this->GetCacheManager()->GetCacheIterator(name);
  841. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  842. it.Initialized())
  843. {
  844. val = it.GetValue();
  845. if ( type == cmCacheManager::PATH || type == cmCacheManager::FILEPATH )
  846. {
  847. std::vector<std::string>::size_type cc;
  848. std::vector<std::string> files;
  849. std::string nvalue = "";
  850. cmSystemTools::ExpandListArgument(val, files);
  851. for ( cc = 0; cc < files.size(); cc ++ )
  852. {
  853. files[cc] = cmSystemTools::CollapseFullPath(files[cc].c_str());
  854. if ( cc > 0 )
  855. {
  856. nvalue += ";";
  857. }
  858. nvalue += files[cc];
  859. }
  860. this->GetCacheManager()->AddCacheEntry(name, nvalue.c_str(), doc, type);
  861. val = it.GetValue();
  862. }
  863. }
  864. this->GetCacheManager()->AddCacheEntry(name, val, doc, type);
  865. this->AddDefinition(name, val);
  866. }
  867. void cmMakefile::AddDefinition(const char* name, bool value)
  868. {
  869. if(value)
  870. {
  871. m_Definitions.erase( DefinitionMap::key_type(name));
  872. m_Definitions.insert(DefinitionMap::value_type(name, "ON"));
  873. }
  874. else
  875. {
  876. m_Definitions.erase( DefinitionMap::key_type(name));
  877. m_Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  878. }
  879. #ifdef CMAKE_BUILD_WITH_CMAKE
  880. cmVariableWatch* vv = this->GetVariableWatch();
  881. if ( vv )
  882. {
  883. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  884. }
  885. #endif
  886. }
  887. void cmMakefile::AddCacheDefinition(const char* name, bool value, const char* doc)
  888. {
  889. bool val = value;
  890. cmCacheManager::CacheIterator it =
  891. this->GetCacheManager()->GetCacheIterator(name);
  892. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  893. it.Initialized())
  894. {
  895. val = it.GetValueAsBool();
  896. }
  897. this->GetCacheManager()->AddCacheEntry(name, val, doc);
  898. this->AddDefinition(name, val);
  899. }
  900. void cmMakefile::RemoveDefinition(const char* name)
  901. {
  902. m_Definitions.erase(DefinitionMap::key_type(name));
  903. #ifdef CMAKE_BUILD_WITH_CMAKE
  904. cmVariableWatch* vv = this->GetVariableWatch();
  905. if ( vv )
  906. {
  907. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS);
  908. }
  909. #endif
  910. }
  911. void cmMakefile::SetProjectName(const char* p)
  912. {
  913. if(m_ProjectName.size())
  914. {
  915. m_ParentProjects.push_back(m_ProjectName);
  916. }
  917. m_ProjectName = p;
  918. }
  919. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  920. {
  921. // for these targets do not add anything
  922. switch(target.GetType())
  923. {
  924. case cmTarget::UTILITY:
  925. case cmTarget::INSTALL_FILES:
  926. case cmTarget::INSTALL_PROGRAMS:
  927. return;
  928. default:;
  929. }
  930. std::vector<std::string>::iterator j;
  931. for(j = m_LinkDirectories.begin();
  932. j != m_LinkDirectories.end(); ++j)
  933. {
  934. target.AddLinkDirectory(j->c_str());
  935. }
  936. target.MergeLinkLibraries( *this, name, m_LinkLibraries );
  937. }
  938. void cmMakefile::AddLibrary(const char* lname, int shared,
  939. const std::vector<std::string> &srcs)
  940. {
  941. cmTarget target;
  942. switch (shared)
  943. {
  944. case 0:
  945. target.SetType(cmTarget::STATIC_LIBRARY, lname);
  946. break;
  947. case 1:
  948. target.SetType(cmTarget::SHARED_LIBRARY, lname);
  949. break;
  950. case 2:
  951. target.SetType(cmTarget::MODULE_LIBRARY, lname);
  952. break;
  953. default:
  954. target.SetType(cmTarget::STATIC_LIBRARY, lname);
  955. }
  956. // Clear its dependencies. Otherwise, dependencies might persist
  957. // over changes in CMakeLists.txt, making the information stale and
  958. // hence useless.
  959. target.ClearDependencyInformation( *this, lname );
  960. target.SetInAll(true);
  961. target.GetSourceLists() = srcs;
  962. this->AddGlobalLinkInformation(lname, target);
  963. m_Targets.insert(cmTargets::value_type(lname,target));
  964. // Add an entry into the cache
  965. std::string libPath = lname;
  966. libPath += "_CMAKE_PATH";
  967. this->GetCacheManager()->
  968. AddCacheEntry(libPath.c_str(),
  969. this->GetCurrentOutputDirectory(),
  970. "Path to a library", cmCacheManager::INTERNAL);
  971. // Add an entry into the cache
  972. std::string ltname = lname;
  973. ltname += "_LIBRARY_TYPE";
  974. switch (shared)
  975. {
  976. case 0:
  977. this->GetCacheManager()->AddCacheEntry(ltname.c_str(),"STATIC",
  978. "Whether a library is static, shared or module.",
  979. cmCacheManager::INTERNAL);
  980. break;
  981. case 1:
  982. this->GetCacheManager()->
  983. AddCacheEntry(ltname.c_str(),
  984. "SHARED",
  985. "Whether a library is static, shared or module.",
  986. cmCacheManager::INTERNAL);
  987. break;
  988. case 2:
  989. this->GetCacheManager()->
  990. AddCacheEntry(ltname.c_str(),
  991. "MODULE",
  992. "Whether a library is static, shared or module.",
  993. cmCacheManager::INTERNAL);
  994. break;
  995. default:
  996. this->GetCacheManager()->
  997. AddCacheEntry(ltname.c_str(),
  998. "STATIC",
  999. "Whether a library is static, shared or module.",
  1000. cmCacheManager::INTERNAL);
  1001. }
  1002. }
  1003. cmTarget* cmMakefile::AddExecutable(const char *exeName,
  1004. const std::vector<std::string> &srcs)
  1005. {
  1006. cmTarget target;
  1007. target.SetType(cmTarget::EXECUTABLE, exeName);
  1008. target.SetInAll(true);
  1009. target.GetSourceLists() = srcs;
  1010. this->AddGlobalLinkInformation(exeName, target);
  1011. cmTargets::iterator it =
  1012. m_Targets.insert(cmTargets::value_type(exeName,target)).first;
  1013. // Add an entry into the cache
  1014. std::string exePath = exeName;
  1015. exePath += "_CMAKE_PATH";
  1016. this->GetCacheManager()->
  1017. AddCacheEntry(exePath.c_str(),
  1018. this->GetCurrentOutputDirectory(),
  1019. "Path to an executable", cmCacheManager::INTERNAL);
  1020. return &it->second;
  1021. }
  1022. void cmMakefile::AddUtilityCommand(const char* utilityName,
  1023. const char* command,
  1024. const char* arguments,
  1025. bool all,
  1026. const std::vector<std::string> &depends)
  1027. {
  1028. std::vector<std::string> empty;
  1029. this->AddUtilityCommand(utilityName,command,arguments,all,
  1030. depends, empty);
  1031. }
  1032. void cmMakefile::AddUtilityCommand(const char* utilityName,
  1033. const char* command,
  1034. const char* arguments,
  1035. bool all,
  1036. const std::vector<std::string> &dep,
  1037. const std::vector<std::string> &out)
  1038. {
  1039. cmTarget target;
  1040. target.SetType(cmTarget::UTILITY, utilityName);
  1041. target.SetInAll(all);
  1042. if (out.size() > 1)
  1043. {
  1044. cmSystemTools::Error(
  1045. "Utility targets can only have one output. For utilityNamed: ",
  1046. utilityName);
  1047. return;
  1048. }
  1049. if (out.size())
  1050. {
  1051. cmCustomCommand cc(command, arguments, dep, out[0].c_str());
  1052. target.GetPostBuildCommands().push_back(cc);
  1053. }
  1054. else
  1055. {
  1056. cmCustomCommand cc(command, arguments, dep, (const char *)0);
  1057. target.GetPostBuildCommands().push_back(cc);
  1058. }
  1059. m_Targets.insert(cmTargets::value_type(utilityName,target));
  1060. }
  1061. cmSourceFile *cmMakefile::GetSourceFileWithOutput(const char *cname)
  1062. {
  1063. std::string name = cname;
  1064. // look through all the source files that have custom commands
  1065. // and see if the custom command has the passed source file as an output
  1066. // keep in mind the possible .rule extension that may be tacked on
  1067. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1068. i != m_SourceFiles.end(); ++i)
  1069. {
  1070. // does this source file have a custom command?
  1071. if ((*i)->GetCustomCommand())
  1072. {
  1073. // is the output of the custom command match the source files name
  1074. const std::string &out = (*i)->GetCustomCommand()->GetOutput();
  1075. if (out.rfind(name) != out.npos &&
  1076. out.rfind(name) == out.size() - name.size())
  1077. {
  1078. return *i;
  1079. }
  1080. }
  1081. }
  1082. // otherwise return NULL
  1083. return 0;
  1084. }
  1085. cmSourceGroup* cmMakefile::GetSourceGroup(const char* name)
  1086. {
  1087. // First see if the group exists. If so, replace its regular expression.
  1088. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  1089. sg != m_SourceGroups.end(); ++sg)
  1090. {
  1091. std::string sgName = sg->GetName();
  1092. if(sgName == name)
  1093. {
  1094. return &(*sg);
  1095. }
  1096. }
  1097. return 0;
  1098. }
  1099. void cmMakefile::AddSourceGroup(const char* name, const char* regex)
  1100. {
  1101. // First see if the group exists. If so, replace its regular expression.
  1102. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  1103. sg != m_SourceGroups.end(); ++sg)
  1104. {
  1105. std::string sgName = sg->GetName();
  1106. if(sgName == name)
  1107. {
  1108. if ( regex )
  1109. {
  1110. // We only want to set the regular expression. If there are already
  1111. // source files in the group, we don't want to remove them.
  1112. sg->SetGroupRegex(regex);
  1113. }
  1114. return;
  1115. }
  1116. }
  1117. // The group doesn't exist. Add it.
  1118. m_SourceGroups.push_back(cmSourceGroup(name, regex));
  1119. }
  1120. void cmMakefile::AddExtraDirectory(const char* dir)
  1121. {
  1122. m_AuxSourceDirectories.push_back(dir);
  1123. }
  1124. // return the file name for the parent CMakeLists file to the
  1125. // one passed in. Zero is returned if the CMakeLists file is the
  1126. // one in the home directory or if for some reason a parent cmake lists
  1127. // file cannot be found.
  1128. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  1129. {
  1130. // extract the directory name
  1131. std::string parentFile;
  1132. std::string listsDir = currentFileName;
  1133. std::string::size_type pos = listsDir.rfind('/');
  1134. // if we could not find the directory return 0
  1135. if(pos == std::string::npos)
  1136. {
  1137. return parentFile;
  1138. }
  1139. listsDir = listsDir.substr(0, pos);
  1140. // if we are in the home directory then stop, return 0
  1141. if(m_cmHomeDirectory == listsDir)
  1142. {
  1143. return parentFile;
  1144. }
  1145. // is there a parent directory we can check
  1146. pos = listsDir.rfind('/');
  1147. // if we could not find the directory return 0
  1148. if(pos == std::string::npos)
  1149. {
  1150. return parentFile;
  1151. }
  1152. listsDir = listsDir.substr(0, pos);
  1153. // is there a CMakeLists.txt file in the parent directory ?
  1154. parentFile = listsDir;
  1155. parentFile += "/CMakeLists.txt";
  1156. while(!cmSystemTools::FileExists(parentFile.c_str()))
  1157. {
  1158. // There is no CMakeLists.txt file in the parent directory. This
  1159. // can occur when coming out of a subdirectory resulting from a
  1160. // SUBDIRS(Foo/Bar) command (coming out of Bar into Foo). Try
  1161. // walking up until a CMakeLists.txt is found or the home
  1162. // directory is hit.
  1163. // if we are in the home directory then stop, return 0
  1164. if(m_cmHomeDirectory == listsDir) { return ""; }
  1165. // is there a parent directory we can check
  1166. pos = listsDir.rfind('/');
  1167. // if we could not find the directory return 0
  1168. if(pos == std::string::npos) { return ""; }
  1169. listsDir = listsDir.substr(0, pos);
  1170. parentFile = listsDir;
  1171. parentFile += "/CMakeLists.txt";
  1172. }
  1173. return parentFile;
  1174. }
  1175. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  1176. // include and library directories.
  1177. void cmMakefile::ExpandVariables()
  1178. {
  1179. // Now expand variables in the include and link strings
  1180. for(std::vector<std::string>::iterator d = m_IncludeDirectories.begin();
  1181. d != m_IncludeDirectories.end(); ++d)
  1182. {
  1183. this->ExpandVariablesInString(*d);
  1184. }
  1185. for(std::vector<std::string>::iterator d = m_LinkDirectories.begin();
  1186. d != m_LinkDirectories.end(); ++d)
  1187. {
  1188. this->ExpandVariablesInString(*d);
  1189. }
  1190. for(cmTarget::LinkLibraries::iterator l = m_LinkLibraries.begin();
  1191. l != m_LinkLibraries.end(); ++l)
  1192. {
  1193. this->ExpandVariablesInString(l->first);
  1194. }
  1195. }
  1196. void cmMakefile::ExpandVariablesInCustomCommands()
  1197. {
  1198. // do source files
  1199. for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
  1200. i != m_SourceFiles.end(); ++i)
  1201. {
  1202. cmCustomCommand *cc = (*i)->GetCustomCommand();
  1203. if (cc)
  1204. {
  1205. cc->ExpandVariables(*this);
  1206. }
  1207. }
  1208. // now do targets
  1209. std::vector<cmCustomCommand>::iterator ic;
  1210. for (cmTargets::iterator l = m_Targets.begin();
  1211. l != m_Targets.end(); l++)
  1212. {
  1213. for (ic = l->second.GetPreBuildCommands().begin();
  1214. ic != l->second.GetPreBuildCommands().end(); ++ic)
  1215. {
  1216. ic->ExpandVariables(*this);
  1217. }
  1218. for (ic = l->second.GetPreLinkCommands().begin();
  1219. ic != l->second.GetPreLinkCommands().end(); ++ic)
  1220. {
  1221. ic->ExpandVariables(*this);
  1222. }
  1223. for (ic = l->second.GetPostBuildCommands().begin();
  1224. ic != l->second.GetPostBuildCommands().end(); ++ic)
  1225. {
  1226. ic->ExpandVariables(*this);
  1227. }
  1228. }
  1229. }
  1230. bool cmMakefile::IsOn(const char* name) const
  1231. {
  1232. const char* value = this->GetDefinition(name);
  1233. return cmSystemTools::IsOn(value);
  1234. }
  1235. const char* cmMakefile::GetRequiredDefinition(const char* name) const
  1236. {
  1237. const char* ret = this->GetDefinition(name);
  1238. if(!ret)
  1239. {
  1240. cmSystemTools::Error("Error required internal CMake variable not set, cmake may be not be built correctly.\n",
  1241. "Missing variable is:\n",
  1242. name);
  1243. return "";
  1244. }
  1245. return ret;
  1246. }
  1247. const char* cmMakefile::GetDefinition(const char* name) const
  1248. {
  1249. const char* def = 0;
  1250. DefinitionMap::const_iterator pos = m_Definitions.find(name);
  1251. if(pos != m_Definitions.end())
  1252. {
  1253. def = (*pos).second.c_str();
  1254. }
  1255. else
  1256. {
  1257. def = this->GetCacheManager()->GetCacheValue(name);
  1258. }
  1259. #ifdef CMAKE_BUILD_WITH_CMAKE
  1260. cmVariableWatch* vv = this->GetVariableWatch();
  1261. if ( vv )
  1262. {
  1263. if ( def )
  1264. {
  1265. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS);
  1266. }
  1267. else
  1268. {
  1269. // are unknown access allowed
  1270. DefinitionMap::const_iterator pos2 =
  1271. m_Definitions.find("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  1272. if (pos2 != m_Definitions.end() &&
  1273. cmSystemTools::IsOn((*pos2).second.c_str()))
  1274. {
  1275. vv->VariableAccessed(name,
  1276. cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS);
  1277. }
  1278. else
  1279. {
  1280. vv->VariableAccessed(name, cmVariableWatch::
  1281. UNKNOWN_VARIABLE_READ_ACCESS);
  1282. }
  1283. }
  1284. }
  1285. #endif
  1286. return def;
  1287. }
  1288. const char* cmMakefile::GetSafeDefinition(const char* def) const
  1289. {
  1290. const char* ret = this->GetDefinition(def);
  1291. if(!ret)
  1292. {
  1293. return "";
  1294. }
  1295. return ret;
  1296. }
  1297. std::vector<std::string> cmMakefile::GetDefinitions(int cacheonly /* = 0 */) const
  1298. {
  1299. std::map<cmStdString, int> definitions;
  1300. if ( !cacheonly )
  1301. {
  1302. DefinitionMap::const_iterator it;
  1303. for ( it = m_Definitions.begin(); it != m_Definitions.end(); it ++ )
  1304. {
  1305. definitions[it->first] = 1;
  1306. }
  1307. }
  1308. cmCacheManager::CacheIterator cit = this->GetCacheManager()->GetCacheIterator();
  1309. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  1310. {
  1311. definitions[cit.GetName()] = 1;
  1312. }
  1313. std::vector<std::string> res;
  1314. std::map<cmStdString, int>::iterator fit;
  1315. for ( fit = definitions.begin(); fit != definitions.end(); fit ++ )
  1316. {
  1317. res.push_back(fit->first);
  1318. }
  1319. return res;
  1320. }
  1321. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  1322. {
  1323. return this->ExpandVariablesInString(source, false);
  1324. }
  1325. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  1326. bool escapeQuotes,
  1327. bool atOnly,
  1328. const char* filename,
  1329. long line) const
  1330. {
  1331. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  1332. // with GetDefinition(), if not found in the map, nothing is expanded.
  1333. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  1334. // the current environment variables.
  1335. // start by look for $ or @ in the string
  1336. std::string::size_type markerPos;
  1337. if(atOnly)
  1338. {
  1339. markerPos = source.find_first_of("@");
  1340. }
  1341. else
  1342. {
  1343. markerPos = source.find_first_of("$@");
  1344. }
  1345. // if not found, or found as the last character, then leave quickly as
  1346. // nothing needs to be expanded
  1347. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  1348. {
  1349. return source.c_str();
  1350. }
  1351. // current position
  1352. std::string::size_type currentPos =0; // start at 0
  1353. std::string result; // string with replacements
  1354. // go until the the end of the string
  1355. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  1356. {
  1357. // grab string from currentPos to the start of the variable
  1358. // and add it to the result
  1359. result += source.substr(currentPos, markerPos - currentPos);
  1360. char endVariableMarker; // what is the end of the variable @ or }
  1361. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  1362. if(!atOnly && source[markerPos] == '$')
  1363. {
  1364. // ${var} case
  1365. if(source[markerPos+1] == '{')
  1366. {
  1367. endVariableMarker = '}';
  1368. markerStartSize = 2;
  1369. }
  1370. // $ENV{var} case
  1371. else if(markerPos+4 < source.size() &&
  1372. source[markerPos+4] == '{' &&
  1373. !source.substr(markerPos+1, 3).compare("ENV"))
  1374. {
  1375. endVariableMarker = '}';
  1376. markerStartSize = 5;
  1377. }
  1378. else
  1379. {
  1380. // bogus $ with no { so add $ to result and move on
  1381. result += '$'; // add bogus $ back into string
  1382. currentPos = markerPos+1; // move on
  1383. endVariableMarker = ' '; // set end var to space so we can tell bogus
  1384. }
  1385. }
  1386. else
  1387. {
  1388. // @VAR case
  1389. endVariableMarker = '@';
  1390. }
  1391. // if it was a valid variable (started with @ or ${ or $ENV{ )
  1392. if(endVariableMarker != ' ')
  1393. {
  1394. markerPos += markerStartSize; // move past marker
  1395. // find the end variable marker starting at the markerPos
  1396. std::string::size_type endVariablePos =
  1397. source.find(endVariableMarker, markerPos);
  1398. if(endVariablePos == std::string::npos)
  1399. {
  1400. // no end marker found so add the bogus start
  1401. if(endVariableMarker == '@')
  1402. {
  1403. result += '@';
  1404. }
  1405. else
  1406. {
  1407. result += (markerStartSize == 5 ? "$ENV{" : "${");
  1408. }
  1409. currentPos = markerPos;
  1410. }
  1411. else
  1412. {
  1413. // good variable remove it
  1414. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  1415. bool found = false;
  1416. if (markerStartSize == 5) // $ENV{
  1417. {
  1418. char *ptr = getenv(var.c_str());
  1419. if (ptr)
  1420. {
  1421. if (escapeQuotes)
  1422. {
  1423. result += cmSystemTools::EscapeQuotes(ptr);
  1424. }
  1425. else
  1426. {
  1427. result += ptr;
  1428. }
  1429. found = true;
  1430. }
  1431. }
  1432. else
  1433. {
  1434. const char* lookup = this->GetDefinition(var.c_str());
  1435. if(lookup)
  1436. {
  1437. if (escapeQuotes)
  1438. {
  1439. result += cmSystemTools::EscapeQuotes(lookup);
  1440. }
  1441. else
  1442. {
  1443. result += lookup;
  1444. }
  1445. found = true;
  1446. }
  1447. else if(filename && (var == "CMAKE_CURRENT_LIST_FILE"))
  1448. {
  1449. result += filename;
  1450. found = true;
  1451. }
  1452. else if(line >= 0 && (var == "CMAKE_CURRENT_LIST_LINE"))
  1453. {
  1454. cmOStringStream ostr;
  1455. ostr << line;
  1456. result += ostr.str();
  1457. found = true;
  1458. }
  1459. }
  1460. // if found add to result, if not, then it gets blanked
  1461. if (!found)
  1462. {
  1463. // if no definition is found then add the var back
  1464. if(endVariableMarker == '@')
  1465. {
  1466. result += "@";
  1467. result += var;
  1468. result += "@";
  1469. }
  1470. }
  1471. // lookup var, and replace it
  1472. currentPos = endVariablePos+1;
  1473. }
  1474. }
  1475. if(atOnly)
  1476. {
  1477. markerPos = source.find_first_of("@", currentPos);
  1478. }
  1479. else
  1480. {
  1481. markerPos = source.find_first_of("$@", currentPos);
  1482. }
  1483. }
  1484. result += source.substr(currentPos); // pick up the rest of the string
  1485. source = result;
  1486. return source.c_str();
  1487. }
  1488. void cmMakefile::RemoveVariablesInString(std::string& source,
  1489. bool atOnly) const
  1490. {
  1491. if(!atOnly)
  1492. {
  1493. cmsys::RegularExpression var("(\\${[A-Za-z_0-9]*})");
  1494. while (var.find(source))
  1495. {
  1496. source.erase(var.start(),var.end() - var.start());
  1497. }
  1498. }
  1499. if(!atOnly)
  1500. {
  1501. cmsys::RegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  1502. while (varb.find(source))
  1503. {
  1504. source.erase(varb.start(),varb.end() - varb.start());
  1505. }
  1506. }
  1507. cmsys::RegularExpression var2("(@[A-Za-z_0-9]*@)");
  1508. while (var2.find(source))
  1509. {
  1510. source.erase(var2.start(),var2.end() - var2.start());
  1511. }
  1512. }
  1513. /**
  1514. * Add the default definitions to the makefile. These values must not
  1515. * be dependent on anything that isn't known when this cmMakefile instance
  1516. * is constructed.
  1517. */
  1518. void cmMakefile::AddDefaultDefinitions()
  1519. {
  1520. #if defined(_WIN32) || defined(__CYGWIN__)
  1521. this->AddDefinition("WIN32", "1");
  1522. #else
  1523. this->AddDefinition("UNIX", "1");
  1524. #endif
  1525. // Cygwin is more like unix so enable the unix commands
  1526. #if defined(__CYGWIN__)
  1527. this->AddDefinition("UNIX", "1");
  1528. this->AddDefinition("CYGWIN", "1");
  1529. #endif
  1530. #if defined(__APPLE__)
  1531. this->AddDefinition("APPLE", "1");
  1532. #endif
  1533. char temp[1024];
  1534. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1535. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1536. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1537. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1538. }
  1539. /**
  1540. * Find a source group whose regular expression matches the filename
  1541. * part of the given source name. Search backward through the list of
  1542. * source groups, and take the first matching group found. This way
  1543. * non-inherited SOURCE_GROUP commands will have precedence over
  1544. * inherited ones.
  1545. */
  1546. cmSourceGroup&
  1547. cmMakefile::FindSourceGroup(const char* source,
  1548. std::vector<cmSourceGroup> &groups)
  1549. {
  1550. // First search for a group that lists the file explicitly.
  1551. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1552. sg != groups.rend(); ++sg)
  1553. {
  1554. if(sg->MatchesFiles(source))
  1555. {
  1556. return *sg;
  1557. }
  1558. }
  1559. // Now search for a group whose regex matches the file.
  1560. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1561. sg != groups.rend(); ++sg)
  1562. {
  1563. if(sg->MatchesRegex(source))
  1564. {
  1565. return *sg;
  1566. }
  1567. }
  1568. // Shouldn't get here, but just in case, return the default group.
  1569. return groups.front();
  1570. }
  1571. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff)
  1572. {
  1573. // if there are no blockers get out of here
  1574. if (m_FunctionBlockers.begin() == m_FunctionBlockers.end())
  1575. {
  1576. return false;
  1577. }
  1578. // loop over all function blockers to see if any block this command
  1579. // evaluate in reverse, this is critical for balanced IF statements etc
  1580. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1581. for (pos = m_FunctionBlockers.rbegin();
  1582. pos != m_FunctionBlockers.rend(); ++pos)
  1583. {
  1584. if((*pos)->IsFunctionBlocked(lff, *this))
  1585. {
  1586. return true;
  1587. }
  1588. }
  1589. return false;
  1590. }
  1591. void cmMakefile::ExpandArguments(
  1592. std::vector<cmListFileArgument> const& inArgs,
  1593. std::vector<std::string>& outArgs)
  1594. {
  1595. std::vector<cmListFileArgument>::const_iterator i;
  1596. std::string value;
  1597. outArgs.reserve(inArgs.size());
  1598. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  1599. {
  1600. // Expand the variables in the argument.
  1601. value = i->Value;
  1602. this->ExpandVariablesInString(value, false, false, i->FilePath, i->Line);
  1603. // If the argument is quoted, it should be one argument.
  1604. // Otherwise, it may be a list of arguments.
  1605. if(i->Quoted)
  1606. {
  1607. outArgs.push_back(value);
  1608. }
  1609. else
  1610. {
  1611. cmSystemTools::ExpandListArgument(value, outArgs);
  1612. }
  1613. }
  1614. }
  1615. void cmMakefile::RemoveFunctionBlocker(const cmListFileFunction& lff)
  1616. {
  1617. // loop over all function blockers to see if any block this command
  1618. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1619. for (pos = m_FunctionBlockers.rbegin();
  1620. pos != m_FunctionBlockers.rend(); ++pos)
  1621. {
  1622. if ((*pos)->ShouldRemove(lff, *this))
  1623. {
  1624. cmFunctionBlocker* b = *pos;
  1625. m_FunctionBlockers.remove(b);
  1626. delete b;
  1627. break;
  1628. }
  1629. }
  1630. return;
  1631. }
  1632. void cmMakefile::SetHomeDirectory(const char* dir)
  1633. {
  1634. m_cmHomeDirectory = dir;
  1635. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1636. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1637. if ( !this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR") )
  1638. {
  1639. this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", this->GetHomeDirectory());
  1640. }
  1641. }
  1642. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1643. {
  1644. m_HomeOutputDirectory = lib;
  1645. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1646. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1647. if ( !this->GetDefinition("CMAKE_CURRENT_BINARY_DIR") )
  1648. {
  1649. this->AddDefinition("CMAKE_CURRENT_BINARY_DIR", this->GetHomeOutputDirectory());
  1650. }
  1651. }
  1652. /**
  1653. * Register the given cmData instance with its own name.
  1654. */
  1655. void cmMakefile::RegisterData(cmData* data)
  1656. {
  1657. std::string name = data->GetName();
  1658. DataMap::const_iterator d = m_DataMap.find(name);
  1659. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1660. {
  1661. delete d->second;
  1662. }
  1663. m_DataMap[name] = data;
  1664. }
  1665. /**
  1666. * Register the given cmData instance with the given name. This can be used
  1667. * to register a NULL pointer.
  1668. */
  1669. void cmMakefile::RegisterData(const char* name, cmData* data)
  1670. {
  1671. DataMap::const_iterator d = m_DataMap.find(name);
  1672. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1673. {
  1674. delete d->second;
  1675. }
  1676. m_DataMap[name] = data;
  1677. }
  1678. /**
  1679. * Lookup a cmData instance previously registered with the given name. If
  1680. * the instance cannot be found, return NULL.
  1681. */
  1682. cmData* cmMakefile::LookupData(const char* name) const
  1683. {
  1684. DataMap::const_iterator d = m_DataMap.find(name);
  1685. if(d != m_DataMap.end())
  1686. {
  1687. return d->second;
  1688. }
  1689. else
  1690. {
  1691. return 0;
  1692. }
  1693. }
  1694. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  1695. {
  1696. // if the source is provided with a full path use it, otherwise
  1697. // by default it is in the current source dir
  1698. std::string path = cmSystemTools::GetFilenamePath(sourceName);
  1699. if (path.empty())
  1700. {
  1701. path = this->GetCurrentDirectory();
  1702. }
  1703. std::string sname =
  1704. cmSystemTools::GetFilenameWithoutLastExtension(sourceName);
  1705. // compute the extension
  1706. std::string ext
  1707. = cmSystemTools::GetFilenameLastExtension(sourceName);
  1708. if ( ext.length() && ext[0] == '.' )
  1709. {
  1710. ext = ext.substr(1);
  1711. }
  1712. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1713. i != m_SourceFiles.end(); ++i)
  1714. {
  1715. if ((*i)->GetSourceName() == sname &&
  1716. cmSystemTools::GetFilenamePath((*i)->GetFullPath()) == path &&
  1717. (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1718. {
  1719. return *i;
  1720. }
  1721. }
  1722. // geeze, if it wasn't found maybe it is listed under the output dir
  1723. if (!cmSystemTools::GetFilenamePath(sourceName).empty())
  1724. {
  1725. return 0;
  1726. }
  1727. path = this->GetCurrentOutputDirectory();
  1728. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1729. i != m_SourceFiles.end(); ++i)
  1730. {
  1731. if ((*i)->GetSourceName() == sname &&
  1732. cmSystemTools::GetFilenamePath((*i)->GetFullPath()) == path &&
  1733. (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1734. {
  1735. return *i;
  1736. }
  1737. }
  1738. return 0;
  1739. }
  1740. cmSourceFile* cmMakefile::GetOrCreateSource(const char* sourceName,
  1741. bool generated)
  1742. {
  1743. // make it a full path first
  1744. std::string src = sourceName;
  1745. bool relative = !cmSystemTools::FileIsFullPath(sourceName);
  1746. std::string srcTreeFile = this->GetCurrentDirectory();
  1747. srcTreeFile += "/";
  1748. srcTreeFile += sourceName;
  1749. if(relative)
  1750. {
  1751. src = srcTreeFile;
  1752. }
  1753. // check to see if it exists
  1754. cmSourceFile* ret = this->GetSource(src.c_str());
  1755. if (ret)
  1756. {
  1757. return ret;
  1758. }
  1759. // OK a source file object doesn't exist for the source
  1760. // maybe we made a bad call on assuming it was in the src tree
  1761. std::string buildTreeFile = this->GetCurrentOutputDirectory();
  1762. buildTreeFile += "/";
  1763. buildTreeFile += sourceName;
  1764. if (relative)
  1765. {
  1766. src = buildTreeFile;
  1767. ret = this->GetSource(src.c_str());
  1768. if (ret)
  1769. {
  1770. return ret;
  1771. }
  1772. // if it has not been marked generated check to see if it exists in the
  1773. // src tree
  1774. if(!generated)
  1775. {
  1776. // see if the file is in the source tree, otherwise assume it
  1777. // is in the binary tree
  1778. if (cmSystemTools::FileExists(srcTreeFile.c_str()) &&
  1779. !cmSystemTools::FileIsDirectory(srcTreeFile.c_str()))
  1780. {
  1781. src = srcTreeFile;
  1782. }
  1783. else
  1784. {
  1785. if ( cmSystemTools::GetFilenameLastExtension(srcTreeFile.c_str()).size() == 0)
  1786. {
  1787. if (cmSystemTools::DoesFileExistWithExtensions(
  1788. srcTreeFile.c_str(), this->GetSourceExtensions()))
  1789. {
  1790. src = srcTreeFile;
  1791. }
  1792. else if (cmSystemTools::DoesFileExistWithExtensions(
  1793. srcTreeFile.c_str(), this->GetHeaderExtensions()))
  1794. {
  1795. src = srcTreeFile;
  1796. }
  1797. }
  1798. }
  1799. }
  1800. }
  1801. // a cmSourceFile instance does not exist yet so we must create one
  1802. // go back to looking in the source directory for it
  1803. // we must create one
  1804. cmSourceFile file;
  1805. std::string path = cmSystemTools::GetFilenamePath(src);
  1806. if(generated)
  1807. {
  1808. std::string ext = cmSystemTools::GetFilenameLastExtension(src);
  1809. std::string name_no_ext = cmSystemTools::GetFilenameName(src.c_str());
  1810. name_no_ext = name_no_ext.substr(0, name_no_ext.length()-ext.length());
  1811. if ( ext.length() && ext[0] == '.' )
  1812. {
  1813. ext = ext.substr(1);
  1814. }
  1815. bool headerFile = !(std::find( m_HeaderFileExtensions.begin(), m_HeaderFileExtensions.end(), ext ) ==
  1816. m_HeaderFileExtensions.end());
  1817. file.SetName(name_no_ext.c_str(), path.c_str(), ext.c_str(), headerFile);
  1818. }
  1819. else
  1820. {
  1821. file.SetName(cmSystemTools::GetFilenameName(src.c_str()).c_str(),
  1822. path.c_str(),
  1823. this->GetSourceExtensions(),
  1824. this->GetHeaderExtensions());
  1825. }
  1826. // add the source file to the makefile
  1827. this->AddSource(file);
  1828. src = file.GetFullPath();
  1829. ret = this->GetSource(src.c_str());
  1830. if (!ret)
  1831. {
  1832. cmSystemTools::Error(
  1833. "CMake failed to properly look up cmSourceFile: ", sourceName);
  1834. }
  1835. return ret;
  1836. }
  1837. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  1838. {
  1839. // check to see if it exists
  1840. cmSourceFile* ret = this->GetSource(sf.GetFullPath().c_str());
  1841. if(ret)
  1842. {
  1843. return ret;
  1844. }
  1845. ret = new cmSourceFile(sf);
  1846. m_SourceFiles.push_back(ret);
  1847. return ret;
  1848. }
  1849. void cmMakefile::EnableLanguage(std::vector<std::string> const & lang)
  1850. {
  1851. m_LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this);
  1852. }
  1853. void cmMakefile::ExpandSourceListArguments(
  1854. std::vector<std::string> const& arguments,
  1855. std::vector<std::string>& newargs, unsigned int start)
  1856. {
  1857. // first figure out if we need to handle version 1.2 style source lists
  1858. int oldVersion = 1;
  1859. const char* versionValue
  1860. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  1861. if (versionValue && atof(versionValue) > 1.2)
  1862. {
  1863. oldVersion = 0;
  1864. }
  1865. // now expand the args
  1866. unsigned int i;
  1867. for(i = 0; i < arguments.size(); ++i)
  1868. {
  1869. // is the arg defined ?, if so use the def
  1870. const char *def = this->GetDefinition(arguments[i].c_str());
  1871. if (def && oldVersion && i >= start)
  1872. {
  1873. // Definition lookup could result in a list that needs to be
  1874. // expanded.
  1875. cmSystemTools::ExpandListArgument(def, newargs);
  1876. }
  1877. else
  1878. {
  1879. // List expansion will have been done already.
  1880. newargs.push_back(arguments[i]);
  1881. }
  1882. }
  1883. }
  1884. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  1885. const char *projectName, const char *targetName,
  1886. const std::vector<std::string> *cmakeArgs,
  1887. std::string *output)
  1888. {
  1889. // does the binary directory exist ? If not create it...
  1890. if (!cmSystemTools::FileIsDirectory(bindir))
  1891. {
  1892. cmSystemTools::MakeDirectory(bindir);
  1893. }
  1894. // change to the tests directory and run cmake
  1895. // use the cmake object instead of calling cmake
  1896. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1897. cmSystemTools::ChangeDirectory(bindir);
  1898. // make sure the same generator is used
  1899. // use this program as the cmake to be run, it should not
  1900. // be run that way but the cmake object requires a vailid path
  1901. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  1902. cmake cm;
  1903. cm.SetIsInTryCompile(true);
  1904. cmGlobalGenerator *gg =
  1905. cm.CreateGlobalGenerator(m_LocalGenerator->GetGlobalGenerator()->GetName());
  1906. if (!gg)
  1907. {
  1908. cmSystemTools::Error(
  1909. "Internal CMake error, TryCompile bad GlobalGenerator");
  1910. // return to the original directory
  1911. cmSystemTools::ChangeDirectory(cwd.c_str());
  1912. return 1;
  1913. }
  1914. cm.SetGlobalGenerator(gg);
  1915. // do a configure
  1916. cm.SetHomeDirectory(srcdir);
  1917. cm.SetHomeOutputDirectory(bindir);
  1918. cm.SetStartDirectory(srcdir);
  1919. cm.SetStartOutputDirectory(bindir);
  1920. cm.SetCMakeCommand(cmakeCommand.c_str());
  1921. cm.LoadCache();
  1922. // if cmake args were provided then pass them in
  1923. if (cmakeArgs)
  1924. {
  1925. cm.SetCacheArgs(*cmakeArgs);
  1926. }
  1927. // to save time we pass the EnableLanguage info directly
  1928. gg->EnableLanguagesFromGenerator(m_LocalGenerator->GetGlobalGenerator());
  1929. if (cm.Configure() != 0)
  1930. {
  1931. cmSystemTools::Error(
  1932. "Internal CMake error, TryCompile configure of cmake failed");
  1933. // return to the original directory
  1934. cmSystemTools::ChangeDirectory(cwd.c_str());
  1935. return 1;
  1936. }
  1937. if (cm.Generate() != 0)
  1938. {
  1939. cmSystemTools::Error(
  1940. "Internal CMake error, TryCompile generation of cmake failed");
  1941. // return to the original directory
  1942. cmSystemTools::ChangeDirectory(cwd.c_str());
  1943. return 1;
  1944. }
  1945. // finally call the generator to actually build the resulting project
  1946. int ret =
  1947. m_LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  1948. projectName,
  1949. targetName,
  1950. output,
  1951. this);
  1952. cmSystemTools::ChangeDirectory(cwd.c_str());
  1953. return ret;
  1954. }
  1955. cmake *cmMakefile::GetCMakeInstance() const
  1956. {
  1957. if ( m_LocalGenerator && m_LocalGenerator->GetGlobalGenerator() )
  1958. {
  1959. return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  1960. }
  1961. return 0;
  1962. }
  1963. #ifdef CMAKE_BUILD_WITH_CMAKE
  1964. cmVariableWatch *cmMakefile::GetVariableWatch() const
  1965. {
  1966. if ( this->GetCMakeInstance() &&
  1967. this->GetCMakeInstance()->GetVariableWatch() )
  1968. {
  1969. return this->GetCMakeInstance()->GetVariableWatch();
  1970. }
  1971. return 0;
  1972. }
  1973. #endif
  1974. void cmMakefile::AddMacro(const char* name, const char* signature)
  1975. {
  1976. if ( !name || !signature )
  1977. {
  1978. return;
  1979. }
  1980. m_MacrosMap[name] = signature;
  1981. }
  1982. void cmMakefile::GetListOfMacros(std::string& macros)
  1983. {
  1984. StringStringMap::iterator it;
  1985. macros = "";
  1986. int cc = 0;
  1987. for ( it = m_MacrosMap.begin(); it != m_MacrosMap.end(); ++it )
  1988. {
  1989. if ( cc > 0 )
  1990. {
  1991. macros += ";";
  1992. }
  1993. macros += it->first;
  1994. cc ++;
  1995. }
  1996. }
  1997. cmCacheManager *cmMakefile::GetCacheManager() const
  1998. {
  1999. return this->GetCMakeInstance()->GetCacheManager();
  2000. }
  2001. bool cmMakefile::GetLocal() const
  2002. {
  2003. return this->GetCMakeInstance()->GetLocal();
  2004. }
  2005. void cmMakefile::DisplayStatus(const char* message, float s)
  2006. {
  2007. this->GetLocalGenerator()->GetGlobalGenerator()
  2008. ->GetCMakeInstance()->UpdateProgress(message, s);
  2009. }
  2010. /**
  2011. * Find the library with the given name. Searches the given path and then
  2012. * the system search path. Returns the full path to the library if it is
  2013. * found. Otherwise, the empty string is returned.
  2014. */
  2015. std::string cmMakefile::FindLibrary(const char* name,
  2016. const std::vector<std::string>& userPaths)
  2017. {
  2018. // See if the executable exists as written.
  2019. if(cmSystemTools::FileExists(name))
  2020. {
  2021. return cmSystemTools::CollapseFullPath(name);
  2022. }
  2023. // Add the system search path to our path.
  2024. std::vector<std::string> path;
  2025. cmSystemTools::GetPath(path, "CMAKE_LIBRARY_PATH");
  2026. cmSystemTools::GetPath(path, "LIB");
  2027. cmSystemTools::GetPath(path);
  2028. // now add the path
  2029. path.insert(path.end(), userPaths.begin(), userPaths.end());
  2030. // Add some lib directories specific to compilers, depending on the
  2031. // current generator, so that library that might have been stored here
  2032. // can be found too.
  2033. // i.e. Microsoft Visual Studio or .Net: path to compiler/../Lib
  2034. // Borland: path to compiler/../Lib
  2035. const char* genName = this->GetDefinition("CMAKE_GENERATOR");
  2036. if (genName)
  2037. {
  2038. if (!strcmp(genName, "NMake Makefiles") ||
  2039. !strcmp(genName, "Visual Studio 6"))
  2040. {
  2041. const char* compiler = this->GetDefinition("CMAKE_CXX_COMPILER");
  2042. if (compiler)
  2043. {
  2044. std::string compiler_path = cmSystemTools::FindProgram(compiler);
  2045. if (compiler_path.size())
  2046. {
  2047. std::string lib_path =
  2048. cmSystemTools::GetFilenamePath(
  2049. cmSystemTools::GetFilenamePath(compiler_path)) + "/Lib";
  2050. path.push_back(lib_path);
  2051. }
  2052. }
  2053. }
  2054. else if (!strcmp(genName, "Visual Studio 7"))
  2055. {
  2056. // It is likely that the compiler won't be in the path for .Net, but
  2057. // we know where devenv is.
  2058. const char* devenv = this->GetDefinition("MICROSOFT_DEVENV");
  2059. if (devenv)
  2060. {
  2061. std::string devenv_path = cmSystemTools::FindProgram(devenv);
  2062. if (devenv_path.size())
  2063. {
  2064. std::string vc7_path =
  2065. cmSystemTools::GetFilenamePath(
  2066. cmSystemTools::GetFilenamePath(
  2067. cmSystemTools::GetFilenamePath(devenv_path))) + "/Vc7";
  2068. path.push_back(vc7_path + "/lib");
  2069. path.push_back(vc7_path + "/PlatformSDK/lib");
  2070. }
  2071. }
  2072. }
  2073. else if (!strcmp(genName, "Borland Makefiles"))
  2074. {
  2075. const char* bcb_bin_path = this->GetDefinition("BCB_BIN_PATH");
  2076. if (bcb_bin_path)
  2077. {
  2078. std::string lib_path =
  2079. cmSystemTools::GetFilenamePath(bcb_bin_path) + "/Lib";
  2080. path.push_back(lib_path);
  2081. }
  2082. }
  2083. }
  2084. return cmSystemTools::FindLibrary(name, path);
  2085. }
  2086. std::string cmMakefile::GetModulesFile(const char* filename)
  2087. {
  2088. std::vector<std::string> modulePath;
  2089. const char* def = this->GetDefinition("CMAKE_MODULE_PATH");
  2090. if(def)
  2091. {
  2092. cmSystemTools::ExpandListArgument(def, modulePath);
  2093. }
  2094. // Also search in the standard modules location.
  2095. def = this->GetDefinition("CMAKE_ROOT");
  2096. if(def)
  2097. {
  2098. std::string rootModules = def;
  2099. rootModules += "/Modules";
  2100. modulePath.push_back(rootModules);
  2101. }
  2102. //std::string Look through the possible module directories.
  2103. for(std::vector<std::string>::iterator i = modulePath.begin();
  2104. i != modulePath.end(); ++i)
  2105. {
  2106. std::string itempl = *i;
  2107. cmSystemTools::ConvertToUnixSlashes(itempl);
  2108. itempl += "/";
  2109. itempl += filename;
  2110. if(cmSystemTools::FileExists(itempl.c_str()))
  2111. {
  2112. return itempl;
  2113. }
  2114. }
  2115. return "";
  2116. }
  2117. void cmMakefile::ConfigureString(const std::string& input,
  2118. std::string& output, bool atOnly,
  2119. bool escapeQuotes)
  2120. {
  2121. // Split input to handle one line at a time.
  2122. std::string::const_iterator lineStart = input.begin();
  2123. while(lineStart != input.end())
  2124. {
  2125. // Find the end of this line.
  2126. std::string::const_iterator lineEnd = lineStart;
  2127. while(lineEnd != input.end() && *lineEnd != '\n')
  2128. {
  2129. ++lineEnd;
  2130. }
  2131. // Copy the line.
  2132. std::string line(lineStart, lineEnd);
  2133. // Skip the newline character.
  2134. bool haveNewline = (lineEnd != input.end());
  2135. if(haveNewline)
  2136. {
  2137. ++lineEnd;
  2138. }
  2139. // Replace #cmakedefine instances.
  2140. if(m_cmDefineRegex.find(line))
  2141. {
  2142. const char* def = this->GetDefinition(m_cmDefineRegex.match(1).c_str());
  2143. if(!cmSystemTools::IsOff(def))
  2144. {
  2145. cmSystemTools::ReplaceString(line, "#cmakedefine", "#define");
  2146. output += line;
  2147. }
  2148. else
  2149. {
  2150. cmSystemTools::ReplaceString(line, "#cmakedefine", "#undef");
  2151. output += "/* ";
  2152. output += line;
  2153. output += " */";
  2154. }
  2155. }
  2156. else
  2157. {
  2158. output += line;
  2159. }
  2160. if(haveNewline)
  2161. {
  2162. output += "\n";
  2163. }
  2164. // Move to the next line.
  2165. lineStart = lineEnd;
  2166. }
  2167. // Perform variable replacements.
  2168. this->ExpandVariablesInString(output, escapeQuotes, atOnly);
  2169. this->RemoveVariablesInString(output, atOnly);
  2170. }
  2171. int cmMakefile::ConfigureFile(const char* infile, const char* outfile,
  2172. bool copyonly, bool atOnly, bool escapeQuotes)
  2173. {
  2174. int res = 1;
  2175. if ( !cmSystemTools::FileExists(infile) )
  2176. {
  2177. cmSystemTools::Error("File ", infile, " does not exist.");
  2178. return 0;
  2179. }
  2180. std::string soutfile = outfile;
  2181. std::string sinfile = infile;
  2182. this->AddCMakeDependFile(infile);
  2183. cmSystemTools::ConvertToUnixSlashes(soutfile);
  2184. mode_t perm = 0;
  2185. cmSystemTools::GetPermissions(sinfile.c_str(), perm);
  2186. std::string::size_type pos = soutfile.rfind('/');
  2187. if(pos != std::string::npos)
  2188. {
  2189. std::string path = soutfile.substr(0, pos);
  2190. cmSystemTools::MakeDirectory(path.c_str());
  2191. }
  2192. if(copyonly)
  2193. {
  2194. if ( !cmSystemTools::CopyFileIfDifferent(sinfile.c_str(),
  2195. soutfile.c_str()))
  2196. {
  2197. return 0;
  2198. }
  2199. }
  2200. else
  2201. {
  2202. std::string tempOutputFile = soutfile;
  2203. tempOutputFile += ".tmp";
  2204. std::ofstream fout(tempOutputFile.c_str());
  2205. if(!fout)
  2206. {
  2207. cmSystemTools::Error(
  2208. "Could not open file for write in copy operation ",
  2209. tempOutputFile.c_str());
  2210. cmSystemTools::ReportLastSystemError("");
  2211. return 0;
  2212. }
  2213. std::ifstream fin(sinfile.c_str());
  2214. if(!fin)
  2215. {
  2216. cmSystemTools::Error("Could not open file for read in copy operation ",
  2217. sinfile.c_str());
  2218. return 0;
  2219. }
  2220. // now copy input to output and expand variables in the
  2221. // input file at the same time
  2222. std::string inLine;
  2223. std::string outLine;
  2224. while( cmSystemTools::GetLineFromStream(fin, inLine) )
  2225. {
  2226. outLine = "";
  2227. this->ConfigureString(inLine, outLine, atOnly, escapeQuotes);
  2228. fout << outLine.c_str() << "\n";
  2229. }
  2230. // close the files before attempting to copy
  2231. fin.close();
  2232. fout.close();
  2233. if ( !cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  2234. soutfile.c_str()) )
  2235. {
  2236. res = 0;
  2237. }
  2238. else
  2239. {
  2240. cmSystemTools::SetPermissions(soutfile.c_str(), perm);
  2241. }
  2242. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  2243. }
  2244. return res;
  2245. }
  2246. void cmMakefile::AddWrittenFile(const char* file)
  2247. { this->GetCMakeInstance()->AddWrittenFile(file); }
  2248. bool cmMakefile::HasWrittenFile(const char* file)
  2249. { return this->GetCMakeInstance()->HasWrittenFile(file); }
  2250. bool cmMakefile::CheckInfiniteLoops()
  2251. {
  2252. std::vector<std::string>::iterator it;
  2253. for ( it = m_ListFiles.begin();
  2254. it != m_ListFiles.end();
  2255. ++ it )
  2256. {
  2257. if ( this->HasWrittenFile(it->c_str()) )
  2258. {
  2259. cmOStringStream str;
  2260. 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.";
  2261. cmSystemTools::Error(str.str().c_str());
  2262. return false;
  2263. }
  2264. }
  2265. return true;
  2266. }
  2267. void cmMakefile::SetProperty(const char* prop, const char* value)
  2268. {
  2269. if (!prop)
  2270. {
  2271. return;
  2272. }
  2273. if (!value)
  2274. {
  2275. value = "NOTFOUND";
  2276. }
  2277. m_Properties[prop] = value;
  2278. }
  2279. const char *cmMakefile::GetProperty(const char* prop) const
  2280. {
  2281. std::map<cmStdString,cmStdString>::const_iterator i =
  2282. m_Properties.find(prop);
  2283. if (i != m_Properties.end())
  2284. {
  2285. return i->second.c_str();
  2286. }
  2287. return 0;
  2288. }
  2289. bool cmMakefile::GetPropertyAsBool(const char* prop) const
  2290. {
  2291. std::map<cmStdString,cmStdString>::const_iterator i =
  2292. m_Properties.find(prop);
  2293. if (i != m_Properties.end())
  2294. {
  2295. return cmSystemTools::IsOn(i->second.c_str());
  2296. }
  2297. return false;
  2298. }