cmMakefile.cxx 72 KB

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