cmMakefile.cxx 63 KB

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