cmMakefile.cxx 77 KB

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