cmMakefile.cxx 77 KB

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