cmMakefile.cxx 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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 "cmStandardIncludes.h"
  16. #include "cmSourceFile.h"
  17. #include "cmDirectory.h"
  18. #include "cmSystemTools.h"
  19. #include "cmGlobalGenerator.h"
  20. #include "cmLocalGenerator.h"
  21. #include "cmCommands.h"
  22. #include "cmCacheManager.h"
  23. #include "cmFunctionBlocker.h"
  24. #include "cmListFileCache.h"
  25. #include "cmVariableWatch.h"
  26. #include "cmake.h"
  27. #include <stdio.h> // required for sprintf
  28. // default is not to be building executables
  29. cmMakefile::cmMakefile()
  30. {
  31. // Setup the default include file regular expression (match everything).
  32. m_IncludeFileRegularExpression = "^.*$";
  33. // Setup the default include complaint regular expression (match nothing).
  34. m_ComplainFileRegularExpression = "^$";
  35. // Source and header file extensions that we can handle
  36. m_SourceFileExtensions.push_back( "cxx" );
  37. m_SourceFileExtensions.push_back( "cpp" );
  38. m_SourceFileExtensions.push_back( "c" );
  39. m_SourceFileExtensions.push_back( "M" );
  40. m_SourceFileExtensions.push_back( "m" );
  41. m_SourceFileExtensions.push_back( "mm" );
  42. m_HeaderFileExtensions.push_back( "h" );
  43. m_HeaderFileExtensions.push_back( "txx" );
  44. m_HeaderFileExtensions.push_back( "in" );
  45. m_DefineFlags = " ";
  46. m_LocalGenerator = 0;
  47. this->AddSourceGroup("", "^.*$");
  48. this->AddSourceGroup("Source Files", "\\.(cpp|C|c|cxx|rc|def|r|odl|idl|hpj|bat)$");
  49. this->AddSourceGroup("Header Files", "\\.(h|hh|hpp|hxx|hm|inl)$");
  50. this->AddDefaultDefinitions();
  51. }
  52. unsigned int cmMakefile::GetCacheMajorVersion()
  53. {
  54. if(!this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
  55. {
  56. return 0;
  57. }
  58. return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"));
  59. }
  60. unsigned int cmMakefile::GetCacheMinorVersion()
  61. {
  62. if(!this->GetCacheManager()->GetCacheValue("Cmake_Cache_MINOR_VERSION"))
  63. {
  64. return 0;
  65. }
  66. return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"));
  67. }
  68. cmMakefile::~cmMakefile()
  69. {
  70. for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
  71. i != m_SourceFiles.end(); ++i)
  72. {
  73. delete *i;
  74. }
  75. for(unsigned int i=0; i < m_UsedCommands.size(); i++)
  76. {
  77. delete m_UsedCommands[i];
  78. }
  79. for(DataMap::const_iterator d = m_DataMap.begin();
  80. d != m_DataMap.end(); ++d)
  81. {
  82. if(d->second)
  83. {
  84. delete d->second;
  85. }
  86. }
  87. std::list<cmFunctionBlocker *>::iterator pos;
  88. for (pos = m_FunctionBlockers.begin();
  89. pos != m_FunctionBlockers.end(); ++pos)
  90. {
  91. cmFunctionBlocker* b = *pos;
  92. delete b;
  93. }
  94. m_FunctionBlockers.clear();
  95. }
  96. void cmMakefile::PrintStringVector(const char* s, const std::vector<std::string>& v) const
  97. {
  98. std::cout << s << ": ( \n";
  99. for(std::vector<std::string>::const_iterator i = v.begin();
  100. i != v.end(); ++i)
  101. {
  102. std::cout << (*i).c_str() << " ";
  103. }
  104. std::cout << " )\n";
  105. }
  106. // call print on all the classes in the makefile
  107. void cmMakefile::Print() const
  108. {
  109. // print the class lists
  110. std::cout << "classes:\n";
  111. std::cout << " m_Targets: ";
  112. for (cmTargets::const_iterator l = m_Targets.begin();
  113. l != m_Targets.end(); l++)
  114. {
  115. std::cout << l->first << std::endl;
  116. }
  117. std::cout << " m_CurrentOutputDirectory; " <<
  118. m_CurrentOutputDirectory.c_str() << std::endl;
  119. std::cout << " m_StartOutputDirectory; " <<
  120. m_StartOutputDirectory.c_str() << std::endl;
  121. std::cout << " m_HomeOutputDirectory; " <<
  122. m_HomeOutputDirectory.c_str() << std::endl;
  123. std::cout << " m_cmCurrentDirectory; " <<
  124. m_cmCurrentDirectory.c_str() << std::endl;
  125. std::cout << " m_cmStartDirectory; " <<
  126. m_cmStartDirectory.c_str() << std::endl;
  127. std::cout << " m_cmHomeDirectory; " <<
  128. m_cmHomeDirectory.c_str() << std::endl;
  129. std::cout << " m_ProjectName; " << m_ProjectName.c_str() << std::endl;
  130. this->PrintStringVector("m_SubDirectories ", m_SubDirectories);
  131. this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
  132. this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
  133. for( std::vector<cmSourceGroup>::const_iterator i = m_SourceGroups.begin();
  134. i != m_SourceGroups.end(); ++i)
  135. {
  136. i->Print();
  137. }
  138. }
  139. bool cmMakefile::CommandExists(const char* name) const
  140. {
  141. return this->GetCMakeInstance()->CommandExists(name);
  142. }
  143. bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff)
  144. {
  145. bool result = true;
  146. // quick return if blocked
  147. if(this->IsFunctionBlocked(lff))
  148. {
  149. // No error.
  150. return result;
  151. }
  152. std::string name = lff.m_Name;
  153. // execute the command
  154. cmCommand *rm =
  155. this->GetCMakeInstance()->GetCommand(name.c_str());
  156. if(rm)
  157. {
  158. cmCommand* usedCommand = rm->Clone();
  159. usedCommand->SetMakefile(this);
  160. bool keepCommand = false;
  161. if(usedCommand->GetEnabled())
  162. {
  163. // if not running in inherit mode or
  164. // if the command is inherited then InitialPass it.
  165. if(!m_Inheriting || usedCommand->IsInherited())
  166. {
  167. if(!usedCommand->InvokeInitialPass(lff.m_Arguments))
  168. {
  169. cmOStringStream error;
  170. error << "Error in cmake code at\n"
  171. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  172. << usedCommand->GetError();
  173. cmSystemTools::Error(error.str().c_str());
  174. result = false;
  175. }
  176. else
  177. {
  178. // use the command
  179. keepCommand = true;
  180. m_UsedCommands.push_back(usedCommand);
  181. }
  182. }
  183. }
  184. // if the Cloned command was not used
  185. // then delete it
  186. if(!keepCommand)
  187. {
  188. delete usedCommand;
  189. }
  190. }
  191. else
  192. {
  193. cmOStringStream error;
  194. error << "Error in cmake code at\n"
  195. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  196. << "Unknown CMake command \"" << lff.m_Name.c_str() << "\".";
  197. cmSystemTools::Error(error.str().c_str());
  198. result = false;
  199. }
  200. return result;
  201. }
  202. // Parse the given CMakeLists.txt file into a list of classes.
  203. // Reads in current CMakeLists file and all parent CMakeLists files
  204. // executing all inherited commands in the parents
  205. //
  206. // if external is non-zero, this means that we have branched to grab some
  207. // commands from a remote list-file (that is, the equivalent of a
  208. // #include has been called). We DO NOT look at the parents of this
  209. // list-file, and for all other purposes, the name of this list-file
  210. // is "filename" and not "external".
  211. bool cmMakefile::ReadListFile(const char* filename, const char* external)
  212. {
  213. // used to watch for blockers going out of scope
  214. // e.g. mismatched IF statement
  215. std::set<cmFunctionBlocker *> originalBlockers;
  216. // keep track of the current file being read
  217. if (filename)
  218. {
  219. if(m_cmCurrentListFile != filename)
  220. {
  221. m_cmCurrentListFile = filename;
  222. }
  223. // loop over current function blockers and record them
  224. std::list<cmFunctionBlocker *>::iterator pos;
  225. for (pos = m_FunctionBlockers.begin();
  226. pos != m_FunctionBlockers.end(); ++pos)
  227. {
  228. originalBlockers.insert(*pos);
  229. }
  230. }
  231. // if this is not a remote makefile
  232. // (if it were, this would be called from the "filename" call,
  233. // rather than the "external" call)
  234. if (!external)
  235. {
  236. // is there a parent CMakeLists file that does not go beyond the
  237. // Home directory? if so recurse and read in that List file
  238. std::string parentList = this->GetParentListFileName(filename);
  239. if (parentList != "")
  240. {
  241. std::string srcdir = m_cmCurrentDirectory;
  242. std::string bindir = m_CurrentOutputDirectory;
  243. std::string::size_type pos = parentList.rfind('/');
  244. m_cmCurrentDirectory = parentList.substr(0, pos);
  245. m_CurrentOutputDirectory = m_HomeOutputDirectory + parentList.substr(m_cmHomeDirectory.size(), pos - m_cmHomeDirectory.size());
  246. // if not found, oops
  247. if(pos == std::string::npos)
  248. {
  249. cmSystemTools::Error("Trailing slash not found");
  250. }
  251. this->ReadListFile(parentList.c_str());
  252. // restore the current directory
  253. m_cmCurrentDirectory = srcdir;
  254. m_CurrentOutputDirectory = bindir;
  255. }
  256. }
  257. // are we at the start CMakeLists file or are we processing a parent
  258. // lists file
  259. //
  260. // this might, or might not be true, irrespective if we are
  261. // off looking at an external makefile.
  262. m_Inheriting = (m_cmCurrentDirectory != m_cmStartDirectory);
  263. // Now read the input file
  264. const char *filenametoread= filename;
  265. if( external)
  266. {
  267. filenametoread= external;
  268. }
  269. // try to see if the list file is the top most
  270. // list file for a project, and if it is, then it
  271. // must have a project command. If there is not
  272. // one, then cmake will provide one via the
  273. // cmListFileCache class.
  274. bool requireProjectCommand = false;
  275. if(!external && m_cmCurrentDirectory == m_cmHomeDirectory)
  276. {
  277. if(cmSystemTools::LowerCase(
  278. cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
  279. {
  280. requireProjectCommand = true;
  281. }
  282. }
  283. cmListFile* lf =
  284. cmListFileCache::GetInstance()->GetFileCache(filenametoread,
  285. requireProjectCommand);
  286. if(!lf)
  287. {
  288. return false;
  289. }
  290. // add this list file to the list of dependencies
  291. m_ListFiles.push_back( filenametoread);
  292. const size_t numberFunctions = lf->m_Functions.size();
  293. for(size_t i =0; i < numberFunctions; ++i)
  294. {
  295. this->ExecuteCommand(lf->m_Functions[i]);
  296. }
  297. // send scope ended to and funciton blockers
  298. if (filename)
  299. {
  300. // loop over all function blockers to see if any block this command
  301. std::list<cmFunctionBlocker *>::iterator pos;
  302. for (pos = m_FunctionBlockers.begin();
  303. pos != m_FunctionBlockers.end(); ++pos)
  304. {
  305. // if this blocker was not in the original then send a
  306. // scope ended message
  307. if (originalBlockers.find(*pos) == originalBlockers.end())
  308. {
  309. (*pos)->ScopeEnded(*this);
  310. }
  311. }
  312. }
  313. return true;
  314. }
  315. void cmMakefile::AddCommand(cmCommand* wg)
  316. {
  317. this->GetCMakeInstance()->AddCommand(wg);
  318. }
  319. // Set the make file
  320. void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
  321. {
  322. m_LocalGenerator = lg;
  323. }
  324. void cmMakefile::FinalPass()
  325. {
  326. // do all the variable expansions here
  327. this->ExpandVariables();
  328. // give all the commands a chance to do something
  329. // after the file has been parsed before generation
  330. for(std::vector<cmCommand*>::iterator i = m_UsedCommands.begin();
  331. i != m_UsedCommands.end(); ++i)
  332. {
  333. (*i)->FinalPass();
  334. }
  335. }
  336. // Generate the output file
  337. void cmMakefile::ConfigureFinalPass()
  338. {
  339. this->FinalPass();
  340. const char* versionValue
  341. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  342. bool oldVersion = (!versionValue || atof(versionValue) < 1.4);
  343. // merge libraries
  344. for (cmTargets::iterator l = m_Targets.begin();
  345. l != m_Targets.end(); l++)
  346. {
  347. l->second.GenerateSourceFilesFromSourceLists(*this);
  348. // pick up any LINK_LIBRARIES that were added after the target
  349. if(oldVersion)
  350. {
  351. this->AddGlobalLinkInformation(l->first.c_str(), l->second);
  352. }
  353. l->second.AnalyzeLibDependencies(*this);
  354. }
  355. }
  356. void cmMakefile::AddCustomCommand(const char* source,
  357. const char* command,
  358. const std::vector<std::string>& commandArgs,
  359. const std::vector<std::string>& depends,
  360. const std::vector<std::string>& outputs,
  361. const char *target,
  362. const char *comment)
  363. {
  364. // find the target,
  365. if (m_Targets.find(target) != m_Targets.end())
  366. {
  367. std::string expandC = command;
  368. this->ExpandVariablesInString(expandC);
  369. std::string c = cmSystemTools::EscapeSpaces(expandC.c_str());
  370. std::string combinedArgs;
  371. unsigned int i;
  372. for (i = 0; i < commandArgs.size(); ++i)
  373. {
  374. combinedArgs += cmSystemTools::EscapeSpaces(commandArgs[i].c_str());
  375. combinedArgs += " ";
  376. }
  377. cmCustomCommand cc(source,c.c_str(),combinedArgs.c_str(),depends,outputs);
  378. if ( comment && comment[0] )
  379. {
  380. cc.SetComment(comment);
  381. }
  382. m_Targets[target].GetCustomCommands().push_back(cc);
  383. std::string cacheCommand = command;
  384. this->ExpandVariablesInString(cacheCommand);
  385. if(this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()))
  386. {
  387. m_Targets[target].AddUtility(
  388. this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()));
  389. }
  390. }
  391. }
  392. void cmMakefile::AddCustomCommand(const char* source,
  393. const char* command,
  394. const std::vector<std::string>& commandArgs,
  395. const std::vector<std::string>& depends,
  396. const char* output,
  397. const char *target)
  398. {
  399. std::vector<std::string> outputs;
  400. outputs.push_back(output);
  401. this->AddCustomCommand(source, command, commandArgs, depends, outputs, target);
  402. }
  403. void cmMakefile::AddDefineFlag(const char* flag)
  404. {
  405. m_DefineFlags += " ";
  406. m_DefineFlags += flag;
  407. }
  408. void cmMakefile::AddLinkLibrary(const char* lib, cmTarget::LinkLibraryType llt)
  409. {
  410. m_LinkLibraries.push_back(
  411. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  412. }
  413. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  414. const char* lib,
  415. cmTarget::LinkLibraryType llt)
  416. {
  417. cmTargets::iterator i = m_Targets.find(target);
  418. if ( i != m_Targets.end())
  419. {
  420. i->second.AddLinkLibrary( *this, target, lib, llt );
  421. }
  422. else
  423. {
  424. cmSystemTools::Error("Attempt to add link libraries to non-existant target: ", target, " for lib ", lib);
  425. }
  426. }
  427. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  428. const char* d)
  429. {
  430. cmTargets::iterator i = m_Targets.find(target);
  431. if ( i != m_Targets.end())
  432. {
  433. i->second.AddLinkDirectory( d );
  434. }
  435. else
  436. {
  437. cmSystemTools::Error("Attempt to add link directories to non-existant target: ",
  438. target, " for directory ", d);
  439. }
  440. }
  441. void cmMakefile::AddLinkLibrary(const char* lib)
  442. {
  443. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  444. }
  445. void cmMakefile::AddLinkDirectory(const char* dir)
  446. {
  447. // Don't add a link directory that is already present. Yes, this
  448. // linear search results in n^2 behavior, but n won't be getting
  449. // much bigger than 20. We cannot use a set because of order
  450. // dependency of the link search path.
  451. // remove trailing slashes
  452. if(dir && dir[strlen(dir)-1] == '/')
  453. {
  454. std::string newdir = dir;
  455. newdir = newdir.substr(0, newdir.size()-1);
  456. if(std::find(m_LinkDirectories.begin(),
  457. m_LinkDirectories.end(), newdir.c_str()) == m_LinkDirectories.end())
  458. {
  459. m_LinkDirectories.push_back(newdir);
  460. }
  461. }
  462. else
  463. {
  464. if(std::find(m_LinkDirectories.begin(),
  465. m_LinkDirectories.end(), dir) == m_LinkDirectories.end())
  466. {
  467. m_LinkDirectories.push_back(dir);
  468. }
  469. }
  470. }
  471. void cmMakefile::AddSubDirectory(const char* sub)
  472. {
  473. m_SubDirectories.push_back(sub);
  474. }
  475. void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
  476. {
  477. // Don't add an include directory that is already present. Yes,
  478. // this linear search results in n^2 behavior, but n won't be
  479. // getting much bigger than 20. We cannot use a set because of
  480. // order dependency of the include path.
  481. if(std::find(m_IncludeDirectories.begin(),
  482. m_IncludeDirectories.end(), inc) == m_IncludeDirectories.end())
  483. {
  484. if (before)
  485. {
  486. // WARNING: this *is* expensive (linear time) since it's a vector
  487. m_IncludeDirectories.insert(m_IncludeDirectories.begin(), inc);
  488. }
  489. else
  490. {
  491. m_IncludeDirectories.push_back(inc);
  492. }
  493. }
  494. }
  495. void cmMakefile::AddDefinition(const char* name, const char* value)
  496. {
  497. if (!value )
  498. {
  499. return;
  500. }
  501. m_Definitions.erase( DefinitionMap::key_type(name));
  502. m_Definitions.insert(DefinitionMap::value_type(name, value));
  503. cmVariableWatch* vv = this->GetVariableWatch();
  504. if ( vv )
  505. {
  506. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  507. }
  508. }
  509. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  510. const char* doc,
  511. cmCacheManager::CacheEntryType type)
  512. {
  513. this->GetCacheManager()->AddCacheEntry(name, value, doc, type);
  514. this->AddDefinition(name, value);
  515. }
  516. void cmMakefile::AddDefinition(const char* name, bool value)
  517. {
  518. if(value)
  519. {
  520. m_Definitions.erase( DefinitionMap::key_type(name));
  521. m_Definitions.insert(DefinitionMap::value_type(name, "ON"));
  522. }
  523. else
  524. {
  525. m_Definitions.erase( DefinitionMap::key_type(name));
  526. m_Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  527. }
  528. cmVariableWatch* vv = this->GetVariableWatch();
  529. if ( vv )
  530. {
  531. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  532. }
  533. }
  534. void cmMakefile::AddCacheDefinition(const char* name, bool value, const char* doc)
  535. {
  536. this->GetCacheManager()->AddCacheEntry(name, value, doc);
  537. this->AddDefinition(name, value);
  538. }
  539. void cmMakefile::RemoveDefinition(const char* name)
  540. {
  541. m_Definitions.erase(DefinitionMap::key_type(name));
  542. cmVariableWatch* vv = this->GetVariableWatch();
  543. if ( vv )
  544. {
  545. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS);
  546. }
  547. }
  548. void cmMakefile::SetProjectName(const char* p)
  549. {
  550. m_ProjectName = p;
  551. }
  552. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  553. {
  554. // for these targets do not add anything
  555. switch(target.GetType())
  556. {
  557. case cmTarget::UTILITY:
  558. case cmTarget::INSTALL_FILES:
  559. case cmTarget::INSTALL_PROGRAMS:
  560. return;
  561. default:;
  562. }
  563. std::vector<std::string>::iterator j;
  564. for(j = m_LinkDirectories.begin();
  565. j != m_LinkDirectories.end(); ++j)
  566. {
  567. target.AddLinkDirectory(j->c_str());
  568. }
  569. target.MergeLinkLibraries( *this, name, m_LinkLibraries );
  570. }
  571. void cmMakefile::AddLibrary(const char* lname, int shared,
  572. const std::vector<std::string> &srcs)
  573. {
  574. cmTarget target;
  575. switch (shared)
  576. {
  577. case 0:
  578. target.SetType(cmTarget::STATIC_LIBRARY);
  579. break;
  580. case 1:
  581. target.SetType(cmTarget::SHARED_LIBRARY);
  582. break;
  583. case 2:
  584. target.SetType(cmTarget::MODULE_LIBRARY);
  585. break;
  586. default:
  587. target.SetType(cmTarget::STATIC_LIBRARY);
  588. }
  589. // Clear its dependencies. Otherwise, dependencies might persist
  590. // over changes in CMakeLists.txt, making the information stale and
  591. // hence useless.
  592. target.ClearDependencyInformation( *this, lname );
  593. target.SetInAll(true);
  594. target.GetSourceLists() = srcs;
  595. this->AddGlobalLinkInformation(lname, target);
  596. m_Targets.insert(cmTargets::value_type(lname,target));
  597. // Add an entry into the cache
  598. std::string libPath = lname;
  599. libPath += "_CMAKE_PATH";
  600. this->GetCacheManager()->
  601. AddCacheEntry(libPath.c_str(),
  602. this->GetCurrentOutputDirectory(),
  603. "Path to a library", cmCacheManager::INTERNAL);
  604. // Add an entry into the cache
  605. std::string ltname = lname;
  606. ltname += "_LIBRARY_TYPE";
  607. switch (shared)
  608. {
  609. case 0:
  610. this->GetCacheManager()->AddCacheEntry(ltname.c_str(),"STATIC",
  611. "Whether a library is static, shared or module.",
  612. cmCacheManager::INTERNAL);
  613. break;
  614. case 1:
  615. this->GetCacheManager()->
  616. AddCacheEntry(ltname.c_str(),
  617. "SHARED",
  618. "Whether a library is static, shared or module.",
  619. cmCacheManager::INTERNAL);
  620. break;
  621. case 2:
  622. this->GetCacheManager()->
  623. AddCacheEntry(ltname.c_str(),
  624. "MODULE",
  625. "Whether a library is static, shared or module.",
  626. cmCacheManager::INTERNAL);
  627. break;
  628. default:
  629. this->GetCacheManager()->
  630. AddCacheEntry(ltname.c_str(),
  631. "STATIC",
  632. "Whether a library is static, shared or module.",
  633. cmCacheManager::INTERNAL);
  634. }
  635. }
  636. void cmMakefile::AddExecutable(const char *exeName,
  637. const std::vector<std::string> &srcs)
  638. {
  639. this->AddExecutable(exeName,srcs,false);
  640. }
  641. void cmMakefile::AddExecutable(const char *exeName,
  642. const std::vector<std::string> &srcs,
  643. bool win32)
  644. {
  645. cmTarget target;
  646. if (win32)
  647. {
  648. target.SetType(cmTarget::WIN32_EXECUTABLE);
  649. }
  650. else
  651. {
  652. target.SetType(cmTarget::EXECUTABLE);
  653. }
  654. target.SetInAll(true);
  655. target.GetSourceLists() = srcs;
  656. this->AddGlobalLinkInformation(exeName, target);
  657. m_Targets.insert(cmTargets::value_type(exeName,target));
  658. // Add an entry into the cache
  659. std::string exePath = exeName;
  660. exePath += "_CMAKE_PATH";
  661. this->GetCacheManager()->
  662. AddCacheEntry(exePath.c_str(),
  663. this->GetCurrentOutputDirectory(),
  664. "Path to an executable", cmCacheManager::INTERNAL);
  665. }
  666. void cmMakefile::AddUtilityCommand(const char* utilityName,
  667. const char* command,
  668. const char* arguments,
  669. bool all)
  670. {
  671. std::vector<std::string> empty;
  672. this->AddUtilityCommand(utilityName,command,arguments,all,
  673. empty,empty);
  674. }
  675. void cmMakefile::AddUtilityCommand(const char* utilityName,
  676. const char* command,
  677. const char* arguments,
  678. bool all,
  679. const std::vector<std::string> &dep,
  680. const std::vector<std::string> &out)
  681. {
  682. cmTarget target;
  683. target.SetType(cmTarget::UTILITY);
  684. target.SetInAll(all);
  685. cmCustomCommand cc(utilityName, command, arguments, dep, out);
  686. target.GetCustomCommands().push_back(cc);
  687. m_Targets.insert(cmTargets::value_type(utilityName,target));
  688. }
  689. cmSourceGroup* cmMakefile::GetSourceGroup(const char* name)
  690. {
  691. // First see if the group exists. If so, replace its regular expression.
  692. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  693. sg != m_SourceGroups.end(); ++sg)
  694. {
  695. std::string sgName = sg->GetName();
  696. if(sgName == name)
  697. {
  698. return &(*sg);
  699. }
  700. }
  701. return 0;
  702. }
  703. void cmMakefile::AddSourceGroup(const char* name, const char* regex)
  704. {
  705. // First see if the group exists. If so, replace its regular expression.
  706. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  707. sg != m_SourceGroups.end(); ++sg)
  708. {
  709. std::string sgName = sg->GetName();
  710. if(sgName == name)
  711. {
  712. if ( regex )
  713. {
  714. // We only want to set the regular expression. If there are already
  715. // source files in the group, we don't want to remove them.
  716. sg->SetGroupRegex(regex);
  717. }
  718. return;
  719. }
  720. }
  721. // The group doesn't exist. Add it.
  722. m_SourceGroups.push_back(cmSourceGroup(name, regex));
  723. }
  724. void cmMakefile::AddExtraDirectory(const char* dir)
  725. {
  726. m_AuxSourceDirectories.push_back(dir);
  727. }
  728. // return the file name for the parent CMakeLists file to the
  729. // one passed in. Zero is returned if the CMakeLists file is the
  730. // one in the home directory or if for some reason a parent cmake lists
  731. // file cannot be found.
  732. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  733. {
  734. // extract the directory name
  735. std::string parentFile;
  736. std::string listsDir = currentFileName;
  737. std::string::size_type pos = listsDir.rfind('/');
  738. // if we could not find the directory return 0
  739. if(pos == std::string::npos)
  740. {
  741. return parentFile;
  742. }
  743. listsDir = listsDir.substr(0, pos);
  744. // if we are in the home directory then stop, return 0
  745. if(m_cmHomeDirectory == listsDir)
  746. {
  747. return parentFile;
  748. }
  749. // is there a parent directory we can check
  750. pos = listsDir.rfind('/');
  751. // if we could not find the directory return 0
  752. if(pos == std::string::npos)
  753. {
  754. return parentFile;
  755. }
  756. listsDir = listsDir.substr(0, pos);
  757. // is there a CMakeLists.txt file in the parent directory ?
  758. parentFile = listsDir;
  759. parentFile += "/CMakeLists.txt";
  760. while(!cmSystemTools::FileExists(parentFile.c_str()))
  761. {
  762. // There is no CMakeLists.txt file in the parent directory. This
  763. // can occur when coming out of a subdirectory resulting from a
  764. // SUBDIRS(Foo/Bar) command (coming out of Bar into Foo). Try
  765. // walking up until a CMakeLists.txt is found or the home
  766. // directory is hit.
  767. // if we are in the home directory then stop, return 0
  768. if(m_cmHomeDirectory == listsDir) { return ""; }
  769. // is there a parent directory we can check
  770. pos = listsDir.rfind('/');
  771. // if we could not find the directory return 0
  772. if(pos == std::string::npos) { return ""; }
  773. listsDir = listsDir.substr(0, pos);
  774. parentFile = listsDir;
  775. parentFile += "/CMakeLists.txt";
  776. }
  777. return parentFile;
  778. }
  779. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  780. // include and library directories.
  781. void cmMakefile::ExpandVariables()
  782. {
  783. // Now expand variables in the include and link strings
  784. for(std::vector<std::string>::iterator d = m_IncludeDirectories.begin();
  785. d != m_IncludeDirectories.end(); ++d)
  786. {
  787. this->ExpandVariablesInString(*d);
  788. }
  789. for(std::vector<std::string>::iterator d = m_LinkDirectories.begin();
  790. d != m_LinkDirectories.end(); ++d)
  791. {
  792. this->ExpandVariablesInString(*d);
  793. }
  794. for(cmTarget::LinkLibraries::iterator l = m_LinkLibraries.begin();
  795. l != m_LinkLibraries.end(); ++l)
  796. {
  797. this->ExpandVariablesInString(l->first);
  798. }
  799. }
  800. bool cmMakefile::IsOn(const char* name) const
  801. {
  802. const char* value = this->GetDefinition(name);
  803. return cmSystemTools::IsOn(value);
  804. }
  805. const char* cmMakefile::GetDefinition(const char* name) const
  806. {
  807. const char* def = 0;
  808. DefinitionMap::const_iterator pos = m_Definitions.find(name);
  809. if(pos != m_Definitions.end())
  810. {
  811. def = (*pos).second.c_str();
  812. }
  813. else
  814. {
  815. def = this->GetCacheManager()->GetCacheValue(name);
  816. }
  817. cmVariableWatch* vv = this->GetVariableWatch();
  818. if ( vv )
  819. {
  820. if ( def )
  821. {
  822. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS);
  823. }
  824. else
  825. {
  826. // are unknown access allowed
  827. DefinitionMap::const_iterator pos2 =
  828. m_Definitions.find("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  829. if (pos2 != m_Definitions.end() &&
  830. cmSystemTools::IsOn((*pos2).second.c_str()))
  831. {
  832. vv->VariableAccessed(name,
  833. cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS);
  834. }
  835. else
  836. {
  837. vv->VariableAccessed(name, cmVariableWatch::
  838. UNKNOWN_VARIABLE_READ_ACCESS);
  839. }
  840. }
  841. }
  842. return def;
  843. }
  844. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  845. {
  846. return this->ExpandVariablesInString(source, false);
  847. }
  848. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  849. bool escapeQuotes,
  850. bool atOnly) const
  851. {
  852. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  853. // with GetDefinition(), if not found in the map, nothing is expanded.
  854. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  855. // the current environment variables.
  856. // start by look for $ or @ in the string
  857. std::string::size_type markerPos;
  858. if(atOnly)
  859. {
  860. markerPos = source.find_first_of("@");
  861. }
  862. else
  863. {
  864. markerPos = source.find_first_of("$@");
  865. }
  866. // if not found, or found as the last character, then leave quickly as
  867. // nothing needs to be expanded
  868. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  869. {
  870. return source.c_str();
  871. }
  872. // current position
  873. std::string::size_type currentPos =0; // start at 0
  874. std::string result; // string with replacements
  875. // go until the the end of the string
  876. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  877. {
  878. // grab string from currentPos to the start of the variable
  879. // and add it to the result
  880. result += source.substr(currentPos, markerPos - currentPos);
  881. char endVariableMarker; // what is the end of the variable @ or }
  882. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  883. if(!atOnly && source[markerPos] == '$')
  884. {
  885. // ${var} case
  886. if(source[markerPos+1] == '{')
  887. {
  888. endVariableMarker = '}';
  889. markerStartSize = 2;
  890. }
  891. // $ENV{var} case
  892. else if(markerPos+4 < source.size() &&
  893. source[markerPos+4] == '{' &&
  894. !source.substr(markerPos+1, 3).compare("ENV"))
  895. {
  896. endVariableMarker = '}';
  897. markerStartSize = 5;
  898. }
  899. else
  900. {
  901. // bogus $ with no { so add $ to result and move on
  902. result += '$'; // add bogus $ back into string
  903. currentPos = markerPos+1; // move on
  904. endVariableMarker = ' '; // set end var to space so we can tell bogus
  905. }
  906. }
  907. else
  908. {
  909. // @VAR case
  910. endVariableMarker = '@';
  911. }
  912. // if it was a valid variable (started with @ or ${ or $ENV{ )
  913. if(endVariableMarker != ' ')
  914. {
  915. markerPos += markerStartSize; // move past marker
  916. // find the end variable marker starting at the markerPos
  917. std::string::size_type endVariablePos =
  918. source.find(endVariableMarker, markerPos);
  919. if(endVariablePos == std::string::npos)
  920. {
  921. // no end marker found so add the bogus start
  922. if(endVariableMarker == '@')
  923. {
  924. result += '@';
  925. }
  926. else
  927. {
  928. result += (markerStartSize == 5 ? "$ENV{" : "${");
  929. }
  930. currentPos = markerPos;
  931. }
  932. else
  933. {
  934. // good variable remove it
  935. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  936. bool found = false;
  937. if (markerStartSize == 5) // $ENV{
  938. {
  939. char *ptr = getenv(var.c_str());
  940. if (ptr)
  941. {
  942. if (escapeQuotes)
  943. {
  944. result += cmSystemTools::EscapeQuotes(ptr);
  945. }
  946. else
  947. {
  948. result += ptr;
  949. }
  950. found = true;
  951. }
  952. }
  953. else
  954. {
  955. const char* lookup = this->GetDefinition(var.c_str());
  956. if(lookup)
  957. {
  958. if (escapeQuotes)
  959. {
  960. result += cmSystemTools::EscapeQuotes(lookup);
  961. }
  962. else
  963. {
  964. result += lookup;
  965. }
  966. found = true;
  967. }
  968. }
  969. // if found add to result, if not, then it gets blanked
  970. if (!found)
  971. {
  972. // if no definition is found then add the var back
  973. if(endVariableMarker == '@')
  974. {
  975. result += "@";
  976. result += var;
  977. result += "@";
  978. }
  979. // do nothing, we remove the variable
  980. /* else
  981. {
  982. result += (markerStartSize == 5 ? "$ENV{" : "${");
  983. result += var;
  984. result += "}";
  985. }
  986. */
  987. }
  988. // lookup var, and replace it
  989. currentPos = endVariablePos+1;
  990. }
  991. }
  992. if(atOnly)
  993. {
  994. markerPos = source.find_first_of("@", currentPos);
  995. }
  996. else
  997. {
  998. markerPos = source.find_first_of("$@", currentPos);
  999. }
  1000. }
  1001. result += source.substr(currentPos); // pick up the rest of the string
  1002. source = result;
  1003. return source.c_str();
  1004. }
  1005. void cmMakefile::RemoveVariablesInString(std::string& source,
  1006. bool atOnly) const
  1007. {
  1008. if(!atOnly)
  1009. {
  1010. cmRegularExpression var("(\\${[A-Za-z_0-9]*})");
  1011. while (var.find(source))
  1012. {
  1013. source.erase(var.start(),var.end() - var.start());
  1014. }
  1015. }
  1016. if(!atOnly)
  1017. {
  1018. cmRegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  1019. while (varb.find(source))
  1020. {
  1021. source.erase(varb.start(),varb.end() - varb.start());
  1022. }
  1023. }
  1024. cmRegularExpression var2("(@[A-Za-z_0-9]*@)");
  1025. while (var2.find(source))
  1026. {
  1027. source.erase(var2.start(),var2.end() - var2.start());
  1028. }
  1029. }
  1030. /**
  1031. * Add the default definitions to the makefile. These values must not
  1032. * be dependent on anything that isn't known when this cmMakefile instance
  1033. * is constructed.
  1034. */
  1035. void cmMakefile::AddDefaultDefinitions()
  1036. {
  1037. #if defined(_WIN32) || defined(__CYGWIN__)
  1038. this->AddDefinition("WIN32", "1");
  1039. #else
  1040. this->AddDefinition("UNIX", "1");
  1041. #endif
  1042. // Cygwin is more like unix so enable the unix commands
  1043. #if defined(__CYGWIN__)
  1044. this->AddDefinition("UNIX", "1");
  1045. this->AddDefinition("CYGWIN", "1");
  1046. #endif
  1047. #if defined(__APPLE__)
  1048. this->AddDefinition("APPLE", "1");
  1049. #endif
  1050. char temp[1024];
  1051. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1052. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1053. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1054. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1055. }
  1056. /**
  1057. * Find a source group whose regular expression matches the filename
  1058. * part of the given source name. Search backward through the list of
  1059. * source groups, and take the first matching group found. This way
  1060. * non-inherited SOURCE_GROUP commands will have precedence over
  1061. * inherited ones.
  1062. */
  1063. cmSourceGroup&
  1064. cmMakefile::FindSourceGroup(const char* source,
  1065. std::vector<cmSourceGroup> &groups)
  1066. {
  1067. std::string file = source;
  1068. std::string::size_type pos = file.rfind('/');
  1069. if(pos != std::string::npos)
  1070. {
  1071. file = file.substr(pos, file.length()-pos);
  1072. }
  1073. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1074. sg != groups.rend(); ++sg)
  1075. {
  1076. if(sg->Matches(file.c_str()))
  1077. {
  1078. return *sg;
  1079. }
  1080. }
  1081. // Shouldn't get here, but just in case, return the default group.
  1082. return groups.front();
  1083. }
  1084. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff)
  1085. {
  1086. // if there are no blockers get out of here
  1087. if (m_FunctionBlockers.begin() == m_FunctionBlockers.end())
  1088. {
  1089. return false;
  1090. }
  1091. // loop over all function blockers to see if any block this command
  1092. // evaluate in reverse, this is critical for balanced IF statements etc
  1093. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1094. for (pos = m_FunctionBlockers.rbegin();
  1095. pos != m_FunctionBlockers.rend(); ++pos)
  1096. {
  1097. if((*pos)->IsFunctionBlocked(lff, *this))
  1098. {
  1099. return true;
  1100. }
  1101. }
  1102. return false;
  1103. }
  1104. void cmMakefile::ExpandArguments(
  1105. std::vector<cmListFileArgument> const& inArgs,
  1106. std::vector<std::string>& outArgs)
  1107. {
  1108. std::vector<cmListFileArgument>::const_iterator i;
  1109. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  1110. {
  1111. // Expand the variables in the argument.
  1112. std::string value = i->Value;
  1113. this->ExpandVariablesInString(value);
  1114. // If the argument is quoted, it should be one argument.
  1115. // Otherwise, it may be a list of arguments.
  1116. if(i->Quoted)
  1117. {
  1118. outArgs.push_back(value);
  1119. }
  1120. else
  1121. {
  1122. cmSystemTools::ExpandListArgument(value, outArgs);
  1123. }
  1124. }
  1125. }
  1126. void cmMakefile::RemoveFunctionBlocker(const cmListFileFunction& lff)
  1127. {
  1128. // loop over all function blockers to see if any block this command
  1129. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1130. for (pos = m_FunctionBlockers.rbegin();
  1131. pos != m_FunctionBlockers.rend(); ++pos)
  1132. {
  1133. if ((*pos)->ShouldRemove(lff, *this))
  1134. {
  1135. cmFunctionBlocker* b = *pos;
  1136. m_FunctionBlockers.remove(b);
  1137. delete b;
  1138. break;
  1139. }
  1140. }
  1141. return;
  1142. }
  1143. void cmMakefile::SetHomeDirectory(const char* dir)
  1144. {
  1145. m_cmHomeDirectory = dir;
  1146. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1147. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1148. }
  1149. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1150. {
  1151. m_HomeOutputDirectory = lib;
  1152. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1153. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1154. }
  1155. /**
  1156. * Register the given cmData instance with its own name.
  1157. */
  1158. void cmMakefile::RegisterData(cmData* data)
  1159. {
  1160. std::string name = data->GetName();
  1161. DataMap::const_iterator d = m_DataMap.find(name);
  1162. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1163. {
  1164. delete d->second;
  1165. }
  1166. m_DataMap[name] = data;
  1167. }
  1168. /**
  1169. * Register the given cmData instance with the given name. This can be used
  1170. * to register a NULL pointer.
  1171. */
  1172. void cmMakefile::RegisterData(const char* name, cmData* data)
  1173. {
  1174. DataMap::const_iterator d = m_DataMap.find(name);
  1175. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1176. {
  1177. delete d->second;
  1178. }
  1179. m_DataMap[name] = data;
  1180. }
  1181. /**
  1182. * Lookup a cmData instance previously registered with the given name. If
  1183. * the instance cannot be found, return NULL.
  1184. */
  1185. cmData* cmMakefile::LookupData(const char* name) const
  1186. {
  1187. DataMap::const_iterator d = m_DataMap.find(name);
  1188. if(d != m_DataMap.end())
  1189. {
  1190. return d->second;
  1191. }
  1192. else
  1193. {
  1194. return 0;
  1195. }
  1196. }
  1197. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  1198. {
  1199. std::string s = cmSystemTools::GetFilenameName(sourceName);
  1200. std::string ext;
  1201. std::string::size_type pos = s.rfind('.');
  1202. if(pos != std::string::npos)
  1203. {
  1204. ext = s.substr(pos+1, s.size() - pos-1);
  1205. s = s.substr(0, pos);
  1206. }
  1207. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1208. i != m_SourceFiles.end(); ++i)
  1209. {
  1210. if ((*i)->GetSourceName() == s)
  1211. {
  1212. if ((ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1213. {
  1214. return *i;
  1215. }
  1216. }
  1217. }
  1218. return 0;
  1219. }
  1220. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  1221. {
  1222. // check to see if it exists
  1223. cmSourceFile* ret = this->GetSource(sf.GetSourceName().c_str());
  1224. if(ret && ret->GetSourceExtension() == sf.GetSourceExtension())
  1225. {
  1226. return ret;
  1227. }
  1228. ret = new cmSourceFile(sf);
  1229. m_SourceFiles.push_back(ret);
  1230. return ret;
  1231. }
  1232. void cmMakefile::EnableLanguage(const char* lang)
  1233. {
  1234. m_LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this);
  1235. }
  1236. void cmMakefile::ExpandSourceListArguments(
  1237. std::vector<std::string> const& arguments,
  1238. std::vector<std::string>& newargs, unsigned int start)
  1239. {
  1240. // first figure out if we need to handle version 1.2 style source lists
  1241. int oldVersion = 1;
  1242. const char* versionValue
  1243. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  1244. if (versionValue && atof(versionValue) > 1.2)
  1245. {
  1246. oldVersion = 0;
  1247. }
  1248. // now expand the args
  1249. unsigned int i;
  1250. for(i = 0; i < arguments.size(); ++i)
  1251. {
  1252. // is the arg defined ?, if so use the def
  1253. const char *def = this->GetDefinition(arguments[i].c_str());
  1254. if (def && oldVersion && i >= start)
  1255. {
  1256. // Definition lookup could result in a list that needs to be
  1257. // expanded.
  1258. cmSystemTools::ExpandListArgument(def, newargs);
  1259. }
  1260. else
  1261. {
  1262. // List expansion will have been done already.
  1263. newargs.push_back(arguments[i]);
  1264. }
  1265. }
  1266. }
  1267. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  1268. const char *projectName, const char *targetName,
  1269. const std::vector<std::string> *cmakeArgs,
  1270. std::string *output)
  1271. {
  1272. // does the binary directory exist ? If not create it...
  1273. if (!cmSystemTools::FileIsDirectory(bindir))
  1274. {
  1275. cmSystemTools::MakeDirectory(bindir);
  1276. }
  1277. // change to the tests directory and run cmake
  1278. // use the cmake object instead of calling cmake
  1279. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1280. cmSystemTools::ChangeDirectory(bindir);
  1281. // make sure the same generator is used
  1282. // use this program as the cmake to be run, it should not
  1283. // be run that way but the cmake object requires a vailid path
  1284. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  1285. cmake cm;
  1286. cm.SetIsInTryCompile(true);
  1287. cmGlobalGenerator *gg =
  1288. cm.CreateGlobalGenerator(m_LocalGenerator->GetGlobalGenerator()->GetName());
  1289. if (!gg)
  1290. {
  1291. cmSystemTools::Error(
  1292. "Internal CMake error, TryCompile bad GlobalGenerator");
  1293. // return to the original directory
  1294. cmSystemTools::ChangeDirectory(cwd.c_str());
  1295. return 1;
  1296. }
  1297. cm.SetGlobalGenerator(gg);
  1298. // do a configure
  1299. cm.SetHomeDirectory(srcdir);
  1300. cm.SetHomeOutputDirectory(bindir);
  1301. cm.SetStartDirectory(srcdir);
  1302. cm.SetStartOutputDirectory(bindir);
  1303. cm.SetCMakeCommand(cmakeCommand.c_str());
  1304. cm.LoadCache();
  1305. // if cmake args were provided then pass them in
  1306. if (cmakeArgs)
  1307. {
  1308. cm.SetCacheArgs(*cmakeArgs);
  1309. }
  1310. // to save time we pass the EnableLanguage info directly
  1311. gg->EnableLanguagesFromGenerator(m_LocalGenerator->GetGlobalGenerator());
  1312. if (cm.Configure() != 0)
  1313. {
  1314. cmSystemTools::Error(
  1315. "Internal CMake error, TryCompile configure of cmake failed");
  1316. // return to the original directory
  1317. cmSystemTools::ChangeDirectory(cwd.c_str());
  1318. return 1;
  1319. }
  1320. if (cm.Generate() != 0)
  1321. {
  1322. cmSystemTools::Error(
  1323. "Internal CMake error, TryCompile generation of cmake failed");
  1324. // return to the original directory
  1325. cmSystemTools::ChangeDirectory(cwd.c_str());
  1326. return 1;
  1327. }
  1328. // finally call the generator to actually build the resulting project
  1329. int ret =
  1330. m_LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  1331. projectName,
  1332. targetName,
  1333. output);
  1334. cmSystemTools::ChangeDirectory(cwd.c_str());
  1335. return ret;
  1336. }
  1337. cmake *cmMakefile::GetCMakeInstance() const
  1338. {
  1339. if ( m_LocalGenerator && m_LocalGenerator->GetGlobalGenerator() )
  1340. {
  1341. return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  1342. }
  1343. return 0;
  1344. }
  1345. cmVariableWatch *cmMakefile::GetVariableWatch() const
  1346. {
  1347. if ( this->GetCMakeInstance() &&
  1348. this->GetCMakeInstance()->GetVariableWatch() )
  1349. {
  1350. return this->GetCMakeInstance()->GetVariableWatch();
  1351. }
  1352. return 0;
  1353. }
  1354. cmCacheManager *cmMakefile::GetCacheManager() const
  1355. {
  1356. return this->GetCMakeInstance()->GetCacheManager();
  1357. }
  1358. bool cmMakefile::GetLocal() const
  1359. {
  1360. return this->GetCMakeInstance()->GetLocal();
  1361. }
  1362. void cmMakefile::DisplayStatus(const char* message, float s)
  1363. {
  1364. this->GetLocalGenerator()->GetGlobalGenerator()
  1365. ->GetCMakeInstance()->UpdateProgress(message, s);
  1366. }
  1367. /**
  1368. * Find the library with the given name. Searches the given path and then
  1369. * the system search path. Returns the full path to the library if it is
  1370. * found. Otherwise, the empty string is returned.
  1371. */
  1372. std::string cmMakefile::FindLibrary(const char* name,
  1373. const std::vector<std::string>& userPaths)
  1374. {
  1375. // See if the executable exists as written.
  1376. if(cmSystemTools::FileExists(name))
  1377. {
  1378. return cmSystemTools::CollapseFullPath(name);
  1379. }
  1380. // Add the system search path to our path.
  1381. std::vector<std::string> path = userPaths;
  1382. cmSystemTools::GetPath(path);
  1383. // Add some lib directories specific to compilers, depending on the
  1384. // current generator, so that library that might have been stored here
  1385. // can be found too.
  1386. // i.e. Microsoft Visual Studio or .Net: path to compiler/../Lib
  1387. // Borland: path to compiler/../Lib
  1388. const char* genName = this->GetDefinition("CMAKE_GENERATOR");
  1389. if (genName)
  1390. {
  1391. if (!strcmp(genName, "NMake Makefiles") ||
  1392. !strcmp(genName, "Visual Studio 6"))
  1393. {
  1394. const char* compiler = this->GetDefinition("CMAKE_CXX_COMPILER");
  1395. if (compiler)
  1396. {
  1397. std::string compiler_path = cmSystemTools::FindProgram(compiler);
  1398. if (compiler_path.size())
  1399. {
  1400. std::string lib_path =
  1401. cmSystemTools::GetFilenamePath(
  1402. cmSystemTools::GetFilenamePath(compiler_path)) + "/Lib";
  1403. path.push_back(lib_path);
  1404. }
  1405. }
  1406. }
  1407. else if (!strcmp(genName, "Visual Studio 7"))
  1408. {
  1409. // It is likely that the compiler won't be in the path for .Net, but
  1410. // we know where devenv is.
  1411. const char* devenv = this->GetDefinition("MICROSOFT_DEVENV");
  1412. if (devenv)
  1413. {
  1414. std::string devenv_path = cmSystemTools::FindProgram(devenv);
  1415. if (devenv_path.size())
  1416. {
  1417. std::string vc7_path =
  1418. cmSystemTools::GetFilenamePath(
  1419. cmSystemTools::GetFilenamePath(
  1420. cmSystemTools::GetFilenamePath(devenv_path))) + "/Vc7";
  1421. path.push_back(vc7_path + "/lib");
  1422. path.push_back(vc7_path + "/PlatformSDK/lib");
  1423. }
  1424. }
  1425. }
  1426. else if (!strcmp(genName, "Borland Makefiles"))
  1427. {
  1428. const char* bcb_bin_path = this->GetDefinition("BCB_BIN_PATH");
  1429. if (bcb_bin_path)
  1430. {
  1431. std::string lib_path =
  1432. cmSystemTools::GetFilenamePath(bcb_bin_path) + "/Lib";
  1433. path.push_back(lib_path);
  1434. }
  1435. }
  1436. }
  1437. return cmSystemTools::FindLibrary(name, path);
  1438. }