cmMakefile.cxx 74 KB

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