cmMakefile.cxx 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  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 "cmake.h"
  26. #include <stdio.h> // required for sprintf
  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. m_SourceFileExtensions.push_back( "cxx" );
  36. m_SourceFileExtensions.push_back( "cpp" );
  37. m_SourceFileExtensions.push_back( "c" );
  38. m_SourceFileExtensions.push_back( "M" );
  39. m_SourceFileExtensions.push_back( "m" );
  40. m_SourceFileExtensions.push_back( "mm" );
  41. m_HeaderFileExtensions.push_back( "h" );
  42. m_HeaderFileExtensions.push_back( "txx" );
  43. m_HeaderFileExtensions.push_back( "in" );
  44. m_DefineFlags = " ";
  45. m_LocalGenerator = 0;
  46. this->AddSourceGroup("", "^.*$");
  47. this->AddSourceGroup("Source Files", "\\.(cpp|C|c|cxx|rc|def|r|odl|idl|hpj|bat)$");
  48. this->AddSourceGroup("Header Files", "\\.(h|hh|hpp|hxx|hm|inl)$");
  49. this->AddDefaultDefinitions();
  50. }
  51. unsigned int cmMakefile::GetCacheMajorVersion()
  52. {
  53. if(!this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
  54. {
  55. return 0;
  56. }
  57. return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"));
  58. }
  59. unsigned int cmMakefile::GetCacheMinorVersion()
  60. {
  61. if(!this->GetCacheManager()->GetCacheValue("Cmake_Cache_MINOR_VERSION"))
  62. {
  63. return 0;
  64. }
  65. return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"));
  66. }
  67. cmMakefile::~cmMakefile()
  68. {
  69. for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
  70. i != m_SourceFiles.end(); ++i)
  71. {
  72. delete *i;
  73. }
  74. for(unsigned int i=0; i < m_UsedCommands.size(); i++)
  75. {
  76. delete m_UsedCommands[i];
  77. }
  78. for(DataMap::const_iterator d = m_DataMap.begin();
  79. d != m_DataMap.end(); ++d)
  80. {
  81. if(d->second)
  82. {
  83. delete d->second;
  84. }
  85. }
  86. std::list<cmFunctionBlocker *>::iterator pos;
  87. for (pos = m_FunctionBlockers.begin();
  88. pos != m_FunctionBlockers.end(); ++pos)
  89. {
  90. cmFunctionBlocker* b = *pos;
  91. delete b;
  92. }
  93. m_FunctionBlockers.clear();
  94. }
  95. void cmMakefile::PrintStringVector(const char* s, const std::vector<std::string>& v) const
  96. {
  97. std::cout << s << ": ( \n";
  98. for(std::vector<std::string>::const_iterator i = v.begin();
  99. i != v.end(); ++i)
  100. {
  101. std::cout << (*i).c_str() << " ";
  102. }
  103. std::cout << " )\n";
  104. }
  105. // call print on all the classes in the makefile
  106. void cmMakefile::Print() const
  107. {
  108. // print the class lists
  109. std::cout << "classes:\n";
  110. std::cout << " m_Targets: ";
  111. for (cmTargets::const_iterator l = m_Targets.begin();
  112. l != m_Targets.end(); l++)
  113. {
  114. std::cout << l->first << std::endl;
  115. }
  116. std::cout << " m_CurrentOutputDirectory; " <<
  117. m_CurrentOutputDirectory.c_str() << std::endl;
  118. std::cout << " m_StartOutputDirectory; " <<
  119. m_StartOutputDirectory.c_str() << std::endl;
  120. std::cout << " m_HomeOutputDirectory; " <<
  121. m_HomeOutputDirectory.c_str() << std::endl;
  122. std::cout << " m_cmCurrentDirectory; " <<
  123. m_cmCurrentDirectory.c_str() << std::endl;
  124. std::cout << " m_cmStartDirectory; " <<
  125. m_cmStartDirectory.c_str() << std::endl;
  126. std::cout << " m_cmHomeDirectory; " <<
  127. m_cmHomeDirectory.c_str() << std::endl;
  128. std::cout << " m_ProjectName; " << m_ProjectName.c_str() << std::endl;
  129. this->PrintStringVector("m_SubDirectories ", m_SubDirectories);
  130. this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
  131. this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
  132. for( std::vector<cmSourceGroup>::const_iterator i = m_SourceGroups.begin();
  133. i != m_SourceGroups.end(); ++i)
  134. {
  135. i->Print();
  136. }
  137. }
  138. bool cmMakefile::CommandExists(const char* name) const
  139. {
  140. return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance()->CommandExists(name);
  141. }
  142. void cmMakefile::ExecuteCommand(std::string const &name,
  143. std::vector<std::string> const& arguments)
  144. {
  145. // quick return if blocked
  146. if(this->IsFunctionBlocked(name.c_str(), arguments))
  147. {
  148. return;
  149. }
  150. // execute the command
  151. cmCommand *rm =
  152. m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance()->GetCommand(name.c_str());
  153. if(rm)
  154. {
  155. cmCommand* usedCommand = rm->Clone();
  156. usedCommand->SetMakefile(this);
  157. bool keepCommand = false;
  158. if(usedCommand->GetEnabled())
  159. {
  160. // if not running in inherit mode or
  161. // if the command is inherited then InitialPass it.
  162. if(!m_Inheriting || usedCommand->IsInherited())
  163. {
  164. std::vector<std::string> expandedArguments;
  165. for(std::vector<std::string>::const_iterator i = arguments.begin();
  166. i != arguments.end(); ++i)
  167. {
  168. std::string tmps = *i;
  169. this->ExpandVariablesInString(tmps);
  170. if (tmps.find_first_not_of(" ") != std::string::npos)
  171. {
  172. // we found something in the args
  173. expandedArguments.push_back(tmps);
  174. }
  175. }
  176. if(!usedCommand->InitialPass(expandedArguments))
  177. {
  178. std::string error;
  179. error = usedCommand->GetName();
  180. error += ": Error : \n";
  181. error += usedCommand->GetError();
  182. error += " from CMakeLists.txt file in directory: ";
  183. error += m_cmCurrentDirectory;
  184. cmSystemTools::Error(error.c_str());
  185. }
  186. else
  187. {
  188. // use the command
  189. keepCommand = true;
  190. m_UsedCommands.push_back(usedCommand);
  191. }
  192. }
  193. }
  194. // if the Cloned command was not used
  195. // then delete it
  196. if(!keepCommand)
  197. {
  198. delete usedCommand;
  199. }
  200. }
  201. else if((name == "CABLE_WRAP_TCL") || (name == "CABLE_CLASS_SET") ||
  202. (name == "CONFIGURE_GCCXML"))
  203. {
  204. cmSystemTools::Error("The command ", name.c_str(),
  205. " is not implemented in this version of CMake.\n"
  206. "Contact [email protected] for more information.");
  207. }
  208. else
  209. {
  210. cmSystemTools::Error("unknown CMake command:", name.c_str(),
  211. "\nReading cmake file in directory:" ,
  212. m_cmCurrentDirectory.c_str());
  213. }
  214. }
  215. // Parse the given CMakeLists.txt file into a list of classes.
  216. // Reads in current CMakeLists file and all parent CMakeLists files
  217. // executing all inherited commands in the parents
  218. //
  219. // if external is non-zero, this means that we have branched to grab some
  220. // commands from a remote list-file (that is, the equivalent of a
  221. // #include has been called). We DO NOT look at the parents of this
  222. // list-file, and for all other purposes, the name of this list-file
  223. // is "filename" and not "external".
  224. bool cmMakefile::ReadListFile(const char* filename, const char* external)
  225. {
  226. // used to watch for blockers going out of scope
  227. // e.g. mismatched IF statement
  228. std::set<cmFunctionBlocker *> originalBlockers;
  229. // keep track of the current file being read
  230. if (filename)
  231. {
  232. if(m_cmCurrentListFile != filename)
  233. {
  234. m_cmCurrentListFile = filename;
  235. }
  236. // loop over current function blockers and record them
  237. std::list<cmFunctionBlocker *>::iterator pos;
  238. for (pos = m_FunctionBlockers.begin();
  239. pos != m_FunctionBlockers.end(); ++pos)
  240. {
  241. originalBlockers.insert(*pos);
  242. }
  243. }
  244. // if this is not a remote makefile
  245. // (if it were, this would be called from the "filename" call,
  246. // rather than the "external" call)
  247. if (!external)
  248. {
  249. // is there a parent CMakeLists file that does not go beyond the
  250. // Home directory? if so recurse and read in that List file
  251. std::string parentList = this->GetParentListFileName(filename);
  252. if (parentList != "")
  253. {
  254. std::string srcdir = m_cmCurrentDirectory;
  255. std::string bindir = m_CurrentOutputDirectory;
  256. std::string::size_type pos = parentList.rfind('/');
  257. m_cmCurrentDirectory = parentList.substr(0, pos);
  258. m_CurrentOutputDirectory = m_HomeOutputDirectory + parentList.substr(m_cmHomeDirectory.size(), pos - m_cmHomeDirectory.size());
  259. // if not found, oops
  260. if(pos == std::string::npos)
  261. {
  262. cmSystemTools::Error("Trailing slash not found");
  263. }
  264. this->ReadListFile(parentList.c_str());
  265. // restore the current directory
  266. m_cmCurrentDirectory = srcdir;
  267. m_CurrentOutputDirectory = bindir;
  268. }
  269. }
  270. // are we at the start CMakeLists file or are we processing a parent
  271. // lists file
  272. //
  273. // this might, or might not be true, irrespective if we are
  274. // off looking at an external makefile.
  275. m_Inheriting = (m_cmCurrentDirectory != m_cmStartDirectory);
  276. // Now read the input file
  277. const char *filenametoread= filename;
  278. if( external)
  279. {
  280. filenametoread= external;
  281. }
  282. // try to see if the list file is the top most
  283. // list file for a project, and if it is, then it
  284. // must have a project command. If there is not
  285. // one, then cmake will provide one via the
  286. // cmListFileCache class.
  287. bool requireProjectCommand = false;
  288. if(!external && m_cmCurrentDirectory == m_cmHomeDirectory)
  289. {
  290. if(cmSystemTools::LowerCase(
  291. cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
  292. {
  293. requireProjectCommand = true;
  294. }
  295. }
  296. cmListFile* lf =
  297. cmListFileCache::GetInstance()->GetFileCache(filenametoread,
  298. requireProjectCommand);
  299. if(!lf)
  300. {
  301. return false;
  302. }
  303. // add this list file to the list of dependencies
  304. m_ListFiles.push_back( filenametoread);
  305. const size_t numberFunctions = lf->m_Functions.size();
  306. for(size_t i =0; i < numberFunctions; ++i)
  307. {
  308. cmListFileFunction& curFunction = lf->m_Functions[i];
  309. this->ExecuteCommand(curFunction.m_Name,
  310. curFunction.m_Arguments);
  311. }
  312. // send scope ended to and funciton blockers
  313. if (filename)
  314. {
  315. // loop over all function blockers to see if any block this command
  316. std::list<cmFunctionBlocker *>::iterator pos;
  317. for (pos = m_FunctionBlockers.begin();
  318. pos != m_FunctionBlockers.end(); ++pos)
  319. {
  320. // if this blocker was not in the original then send a
  321. // scope ended message
  322. if (originalBlockers.find(*pos) == originalBlockers.end())
  323. {
  324. (*pos)->ScopeEnded(*this);
  325. }
  326. }
  327. }
  328. return true;
  329. }
  330. void cmMakefile::AddCommand(cmCommand* wg)
  331. {
  332. m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance()->AddCommand(wg);
  333. }
  334. // Set the make file
  335. void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
  336. {
  337. m_LocalGenerator = lg;
  338. }
  339. void cmMakefile::FinalPass()
  340. {
  341. // do all the variable expansions here
  342. this->ExpandVariables();
  343. // give all the commands a chance to do something
  344. // after the file has been parsed before generation
  345. for(std::vector<cmCommand*>::iterator i = m_UsedCommands.begin();
  346. i != m_UsedCommands.end(); ++i)
  347. {
  348. (*i)->FinalPass();
  349. }
  350. }
  351. // Generate the output file
  352. void cmMakefile::ConfigureFinalPass()
  353. {
  354. this->FinalPass();
  355. const char* versionValue
  356. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  357. bool oldVersion = (!versionValue || atof(versionValue) < 1.4);
  358. // merge libraries
  359. for (cmTargets::iterator l = m_Targets.begin();
  360. l != m_Targets.end(); l++)
  361. {
  362. l->second.GenerateSourceFilesFromSourceLists(*this);
  363. // pick up any LINK_LIBRARIES that were added after the target
  364. if(oldVersion)
  365. {
  366. this->AddGlobalLinkInformation(l->first.c_str(), l->second);
  367. }
  368. l->second.AnalyzeLibDependencies(*this);
  369. }
  370. }
  371. void cmMakefile::AddCustomCommand(const char* source,
  372. const char* command,
  373. const std::vector<std::string>& commandArgs,
  374. const std::vector<std::string>& depends,
  375. const std::vector<std::string>& outputs,
  376. const char *target,
  377. const char *comment)
  378. {
  379. // find the target,
  380. if (m_Targets.find(target) != m_Targets.end())
  381. {
  382. std::string expandC = command;
  383. this->ExpandVariablesInString(expandC);
  384. std::string c = cmSystemTools::EscapeSpaces(expandC.c_str());
  385. std::string combinedArgs;
  386. unsigned int i;
  387. for (i = 0; i < commandArgs.size(); ++i)
  388. {
  389. combinedArgs += cmSystemTools::EscapeSpaces(commandArgs[i].c_str());
  390. combinedArgs += " ";
  391. }
  392. cmCustomCommand cc(source,c.c_str(),combinedArgs.c_str(),depends,outputs);
  393. if ( comment && comment[0] )
  394. {
  395. cc.SetComment(comment);
  396. }
  397. m_Targets[target].GetCustomCommands().push_back(cc);
  398. std::string cacheCommand = command;
  399. this->ExpandVariablesInString(cacheCommand);
  400. if(this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()))
  401. {
  402. m_Targets[target].AddUtility(
  403. this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()));
  404. }
  405. }
  406. }
  407. void cmMakefile::AddCustomCommand(const char* source,
  408. const char* command,
  409. const std::vector<std::string>& commandArgs,
  410. const std::vector<std::string>& depends,
  411. const char* output,
  412. const char *target)
  413. {
  414. std::vector<std::string> outputs;
  415. outputs.push_back(output);
  416. this->AddCustomCommand(source, command, commandArgs, depends, outputs, target);
  417. }
  418. void cmMakefile::AddDefineFlag(const char* flag)
  419. {
  420. m_DefineFlags += " ";
  421. m_DefineFlags += flag;
  422. }
  423. void cmMakefile::AddLinkLibrary(const char* lib, cmTarget::LinkLibraryType llt)
  424. {
  425. m_LinkLibraries.push_back(
  426. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  427. }
  428. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  429. const char* lib,
  430. cmTarget::LinkLibraryType llt)
  431. {
  432. cmTargets::iterator i = m_Targets.find(target);
  433. if ( i != m_Targets.end())
  434. {
  435. i->second.AddLinkLibrary( *this, target, lib, llt );
  436. }
  437. else
  438. {
  439. cmSystemTools::Error("Attempt to add link libraries to non-existant target: ", target, " for lib ", lib);
  440. }
  441. }
  442. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  443. const char* d)
  444. {
  445. cmTargets::iterator i = m_Targets.find(target);
  446. if ( i != m_Targets.end())
  447. {
  448. i->second.AddLinkDirectory( d );
  449. }
  450. else
  451. {
  452. cmSystemTools::Error("Attempt to add link directories to non-existant target: ",
  453. target, " for directory ", d);
  454. }
  455. }
  456. void cmMakefile::AddLinkLibrary(const char* lib)
  457. {
  458. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  459. }
  460. void cmMakefile::AddLinkDirectory(const char* dir)
  461. {
  462. // Don't add a link directory that is already present. Yes, this
  463. // linear search results in n^2 behavior, but n won't be getting
  464. // much bigger than 20. We cannot use a set because of order
  465. // dependency of the link search path.
  466. // remove trailing slashes
  467. if(dir && dir[strlen(dir)-1] == '/')
  468. {
  469. std::string newdir = dir;
  470. newdir = newdir.substr(0, newdir.size()-1);
  471. if(std::find(m_LinkDirectories.begin(),
  472. m_LinkDirectories.end(), newdir.c_str()) == m_LinkDirectories.end())
  473. {
  474. m_LinkDirectories.push_back(newdir);
  475. }
  476. }
  477. else
  478. {
  479. if(std::find(m_LinkDirectories.begin(),
  480. m_LinkDirectories.end(), dir) == m_LinkDirectories.end())
  481. {
  482. m_LinkDirectories.push_back(dir);
  483. }
  484. }
  485. }
  486. void cmMakefile::AddSubDirectory(const char* sub)
  487. {
  488. m_SubDirectories.push_back(sub);
  489. }
  490. void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
  491. {
  492. // Don't add an include directory that is already present. Yes,
  493. // this linear search results in n^2 behavior, but n won't be
  494. // getting much bigger than 20. We cannot use a set because of
  495. // order dependency of the include path.
  496. if(std::find(m_IncludeDirectories.begin(),
  497. m_IncludeDirectories.end(), inc) == m_IncludeDirectories.end())
  498. {
  499. if (before)
  500. {
  501. // WARNING: this *is* expensive (linear time) since it's a vector
  502. m_IncludeDirectories.insert(m_IncludeDirectories.begin(), inc);
  503. }
  504. else
  505. {
  506. m_IncludeDirectories.push_back(inc);
  507. }
  508. }
  509. }
  510. void cmMakefile::AddDefinition(const char* name, const char* value)
  511. {
  512. if (!value )
  513. {
  514. return;
  515. }
  516. m_Definitions.erase( DefinitionMap::key_type(name));
  517. m_Definitions.insert(DefinitionMap::value_type(name, value));
  518. }
  519. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  520. const char* doc,
  521. cmCacheManager::CacheEntryType type)
  522. {
  523. this->GetCacheManager()->AddCacheEntry(name, value, doc, type);
  524. this->AddDefinition(name, value);
  525. }
  526. void cmMakefile::AddDefinition(const char* name, bool value)
  527. {
  528. if(value)
  529. {
  530. m_Definitions.erase( DefinitionMap::key_type(name));
  531. m_Definitions.insert(DefinitionMap::value_type(name, "ON"));
  532. }
  533. else
  534. {
  535. m_Definitions.erase( DefinitionMap::key_type(name));
  536. m_Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  537. }
  538. }
  539. void cmMakefile::AddCacheDefinition(const char* name, bool value, const char* doc)
  540. {
  541. this->GetCacheManager()->AddCacheEntry(name, value, doc);
  542. this->AddDefinition(name, value);
  543. }
  544. void cmMakefile::RemoveDefinition(const char* name)
  545. {
  546. m_Definitions.erase(DefinitionMap::key_type(name));
  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 varibles 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. DefinitionMap::const_iterator pos = m_Definitions.find(name);
  808. if(pos != m_Definitions.end())
  809. {
  810. return (*pos).second.c_str();
  811. }
  812. return this->GetCacheManager()->GetCacheValue(name);
  813. }
  814. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  815. {
  816. return this->ExpandVariablesInString(source, false);
  817. }
  818. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  819. bool escapeQuotes,
  820. bool atOnly) const
  821. {
  822. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  823. // with GetDefinition(), if not found in the map, nothing is expanded.
  824. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  825. // the current environment variables.
  826. // start by look for $ or @ in the string
  827. std::string::size_type markerPos;
  828. if(atOnly)
  829. {
  830. markerPos = source.find_first_of("@");
  831. }
  832. else
  833. {
  834. markerPos = source.find_first_of("$@");
  835. }
  836. // if not found, or found as the last character, then leave quickly as
  837. // nothing needs to be expanded
  838. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  839. {
  840. return source.c_str();
  841. }
  842. // current position
  843. std::string::size_type currentPos =0; // start at 0
  844. std::string result; // string with replacements
  845. // go until the the end of the string
  846. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  847. {
  848. // grab string from currentPos to the start of the variable
  849. // and add it to the result
  850. result += source.substr(currentPos, markerPos - currentPos);
  851. char endVariableMarker; // what is the end of the variable @ or }
  852. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  853. if(!atOnly && source[markerPos] == '$')
  854. {
  855. // ${var} case
  856. if(source[markerPos+1] == '{')
  857. {
  858. endVariableMarker = '}';
  859. markerStartSize = 2;
  860. }
  861. // $ENV{var} case
  862. else if(markerPos+4 < source.size() &&
  863. source[markerPos+4] == '{' &&
  864. !source.substr(markerPos+1, 3).compare("ENV"))
  865. {
  866. endVariableMarker = '}';
  867. markerStartSize = 5;
  868. }
  869. else
  870. {
  871. // bogus $ with no { so add $ to result and move on
  872. result += '$'; // add bogus $ back into string
  873. currentPos = markerPos+1; // move on
  874. endVariableMarker = ' '; // set end var to space so we can tell bogus
  875. }
  876. }
  877. else
  878. {
  879. // @VAR case
  880. endVariableMarker = '@';
  881. }
  882. // if it was a valid variable (started with @ or ${ or $ENV{ )
  883. if(endVariableMarker != ' ')
  884. {
  885. markerPos += markerStartSize; // move past marker
  886. // find the end variable marker starting at the markerPos
  887. std::string::size_type endVariablePos =
  888. source.find(endVariableMarker, markerPos);
  889. if(endVariablePos == std::string::npos)
  890. {
  891. // no end marker found so add the bogus start
  892. if(endVariableMarker == '@')
  893. {
  894. result += '@';
  895. }
  896. else
  897. {
  898. result += (markerStartSize == 5 ? "$ENV{" : "${");
  899. }
  900. currentPos = markerPos;
  901. }
  902. else
  903. {
  904. // good variable remove it
  905. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  906. bool found = false;
  907. if (markerStartSize == 5) // $ENV{
  908. {
  909. char *ptr = getenv(var.c_str());
  910. if (ptr)
  911. {
  912. if (escapeQuotes)
  913. {
  914. result += cmSystemTools::EscapeQuotes(ptr);
  915. }
  916. else
  917. {
  918. result += ptr;
  919. }
  920. found = true;
  921. }
  922. }
  923. else
  924. {
  925. const char* lookup = this->GetDefinition(var.c_str());
  926. if(lookup)
  927. {
  928. if (escapeQuotes)
  929. {
  930. result += cmSystemTools::EscapeQuotes(lookup);
  931. }
  932. else
  933. {
  934. result += lookup;
  935. }
  936. found = true;
  937. }
  938. }
  939. // if found add to result, if not, then it gets blanked
  940. if (!found)
  941. {
  942. // if no definition is found then add the var back
  943. if(endVariableMarker == '@')
  944. {
  945. result += "@";
  946. result += var;
  947. result += "@";
  948. }
  949. // do nothing, we remove the variable
  950. /* else
  951. {
  952. result += (markerStartSize == 5 ? "$ENV{" : "${");
  953. result += var;
  954. result += "}";
  955. }
  956. */
  957. }
  958. // lookup var, and replace it
  959. currentPos = endVariablePos+1;
  960. }
  961. }
  962. if(atOnly)
  963. {
  964. markerPos = source.find_first_of("@", currentPos);
  965. }
  966. else
  967. {
  968. markerPos = source.find_first_of("$@", currentPos);
  969. }
  970. }
  971. result += source.substr(currentPos); // pick up the rest of the string
  972. source = result;
  973. return source.c_str();
  974. }
  975. void cmMakefile::RemoveVariablesInString(std::string& source,
  976. bool atOnly) const
  977. {
  978. if(!atOnly)
  979. {
  980. cmRegularExpression var("(\\${[A-Za-z_0-9]*})");
  981. while (var.find(source))
  982. {
  983. source.erase(var.start(),var.end() - var.start());
  984. }
  985. }
  986. if(!atOnly)
  987. {
  988. cmRegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  989. while (varb.find(source))
  990. {
  991. source.erase(varb.start(),varb.end() - varb.start());
  992. }
  993. }
  994. cmRegularExpression var2("(@[A-Za-z_0-9]*@)");
  995. while (var2.find(source))
  996. {
  997. source.erase(var2.start(),var2.end() - var2.start());
  998. }
  999. }
  1000. /**
  1001. * Add the default definitions to the makefile. These values must not
  1002. * be dependent on anything that isn't known when this cmMakefile instance
  1003. * is constructed.
  1004. */
  1005. void cmMakefile::AddDefaultDefinitions()
  1006. {
  1007. #if defined(_WIN32) || defined(__CYGWIN__)
  1008. this->AddDefinition("WIN32", "1");
  1009. #else
  1010. this->AddDefinition("UNIX", "1");
  1011. #endif
  1012. // Cygwin is more like unix so enable the unix commands
  1013. #if defined(__CYGWIN__)
  1014. this->AddDefinition("UNIX", "1");
  1015. this->AddDefinition("CYGWIN", "1");
  1016. #endif
  1017. #if defined(__APPLE__)
  1018. this->AddDefinition("APPLE", "1");
  1019. #endif
  1020. char temp[1024];
  1021. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1022. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1023. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1024. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1025. }
  1026. /**
  1027. * Find a source group whose regular expression matches the filename
  1028. * part of the given source name. Search backward through the list of
  1029. * source groups, and take the first matching group found. This way
  1030. * non-inherited SOURCE_GROUP commands will have precedence over
  1031. * inherited ones.
  1032. */
  1033. cmSourceGroup&
  1034. cmMakefile::FindSourceGroup(const char* source,
  1035. std::vector<cmSourceGroup> &groups)
  1036. {
  1037. std::string file = source;
  1038. std::string::size_type pos = file.rfind('/');
  1039. if(pos != std::string::npos)
  1040. {
  1041. file = file.substr(pos, file.length()-pos);
  1042. }
  1043. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1044. sg != groups.rend(); ++sg)
  1045. {
  1046. if(sg->Matches(file.c_str()))
  1047. {
  1048. return *sg;
  1049. }
  1050. }
  1051. // Shouldn't get here, but just in case, return the default group.
  1052. return groups.front();
  1053. }
  1054. bool cmMakefile::IsFunctionBlocked(const char *name,
  1055. std::vector<std::string> const&args)
  1056. {
  1057. // if there are no blockers get out of here
  1058. if (m_FunctionBlockers.begin() == m_FunctionBlockers.end())
  1059. {
  1060. return false;
  1061. }
  1062. // loop over all function blockers to see if any block this command
  1063. std::vector<std::string> expandedArguments;
  1064. for(std::vector<std::string>::const_iterator i = args.begin();
  1065. i != args.end(); ++i)
  1066. {
  1067. std::string tmps = *i;
  1068. this->ExpandVariablesInString(tmps);
  1069. if (tmps.find_first_not_of(" ") != std::string::npos)
  1070. {
  1071. // we found something in the args
  1072. expandedArguments.push_back(tmps);
  1073. }
  1074. }
  1075. // evaluate in reverse, this is critical for balanced IF statements etc
  1076. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1077. for (pos = m_FunctionBlockers.rbegin();
  1078. pos != m_FunctionBlockers.rend(); ++pos)
  1079. {
  1080. if ((*pos)->NeedExpandedVariables())
  1081. {
  1082. if ((*pos)->IsFunctionBlocked(name, expandedArguments, *this))
  1083. {
  1084. return true;
  1085. }
  1086. }
  1087. else
  1088. {
  1089. if ((*pos)->IsFunctionBlocked(name, args, *this))
  1090. {
  1091. return true;
  1092. }
  1093. }
  1094. }
  1095. return false;
  1096. }
  1097. void cmMakefile::RemoveFunctionBlocker(const char *name,
  1098. const std::vector<std::string> &args)
  1099. {
  1100. // loop over all function blockers to see if any block this command
  1101. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1102. for (pos = m_FunctionBlockers.rbegin();
  1103. pos != m_FunctionBlockers.rend(); ++pos)
  1104. {
  1105. if ((*pos)->ShouldRemove(name, args, *this))
  1106. {
  1107. cmFunctionBlocker* b = *pos;
  1108. m_FunctionBlockers.remove(b);
  1109. delete b;
  1110. break;
  1111. }
  1112. }
  1113. return;
  1114. }
  1115. void cmMakefile::SetHomeDirectory(const char* dir)
  1116. {
  1117. m_cmHomeDirectory = dir;
  1118. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1119. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1120. }
  1121. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1122. {
  1123. m_HomeOutputDirectory = lib;
  1124. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1125. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1126. }
  1127. /**
  1128. * Register the given cmData instance with its own name.
  1129. */
  1130. void cmMakefile::RegisterData(cmData* data)
  1131. {
  1132. std::string name = data->GetName();
  1133. DataMap::const_iterator d = m_DataMap.find(name);
  1134. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1135. {
  1136. delete d->second;
  1137. }
  1138. m_DataMap[name] = data;
  1139. }
  1140. /**
  1141. * Register the given cmData instance with the given name. This can be used
  1142. * to register a NULL pointer.
  1143. */
  1144. void cmMakefile::RegisterData(const char* name, cmData* data)
  1145. {
  1146. DataMap::const_iterator d = m_DataMap.find(name);
  1147. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1148. {
  1149. delete d->second;
  1150. }
  1151. m_DataMap[name] = data;
  1152. }
  1153. /**
  1154. * Lookup a cmData instance previously registered with the given name. If
  1155. * the instance cannot be found, return NULL.
  1156. */
  1157. cmData* cmMakefile::LookupData(const char* name) const
  1158. {
  1159. DataMap::const_iterator d = m_DataMap.find(name);
  1160. if(d != m_DataMap.end())
  1161. {
  1162. return d->second;
  1163. }
  1164. else
  1165. {
  1166. return 0;
  1167. }
  1168. }
  1169. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  1170. {
  1171. std::string s = cmSystemTools::GetFilenameName(sourceName);
  1172. std::string ext;
  1173. std::string::size_type pos = s.rfind('.');
  1174. if(pos != std::string::npos)
  1175. {
  1176. ext = s.substr(pos+1, s.size() - pos-1);
  1177. s = s.substr(0, pos);
  1178. }
  1179. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1180. i != m_SourceFiles.end(); ++i)
  1181. {
  1182. if ((*i)->GetSourceName() == s)
  1183. {
  1184. if ((ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1185. {
  1186. return *i;
  1187. }
  1188. }
  1189. }
  1190. return 0;
  1191. }
  1192. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  1193. {
  1194. // check to see if it exists
  1195. cmSourceFile* ret = this->GetSource(sf.GetSourceName().c_str());
  1196. if(ret && ret->GetSourceExtension() == sf.GetSourceExtension())
  1197. {
  1198. return ret;
  1199. }
  1200. ret = new cmSourceFile(sf);
  1201. m_SourceFiles.push_back(ret);
  1202. return ret;
  1203. }
  1204. void cmMakefile::EnableLanguage(const char* lang)
  1205. {
  1206. m_LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this);
  1207. }
  1208. void cmMakefile::ExpandSourceListArguments(
  1209. std::vector<std::string> const& arguments,
  1210. std::vector<std::string>& newargs, unsigned int start)
  1211. {
  1212. // first figure out if we need to handle version 1.2 style source lists
  1213. int oldVersion = 1;
  1214. const char* versionValue
  1215. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  1216. if (versionValue && atof(versionValue) > 1.2)
  1217. {
  1218. oldVersion = 0;
  1219. }
  1220. // now expand the args
  1221. std::vector<std::string> tmpArgs;
  1222. unsigned int i;
  1223. for(i = 0; i < arguments.size(); ++i)
  1224. {
  1225. // is the arg defined ?, if so use the def
  1226. const char *def = this->GetDefinition(arguments[i].c_str());
  1227. if (def && oldVersion && i >= start)
  1228. {
  1229. tmpArgs.push_back(def);
  1230. }
  1231. else
  1232. {
  1233. tmpArgs.push_back(arguments[i]);
  1234. }
  1235. }
  1236. cmSystemTools::ExpandListArguments(tmpArgs, newargs);
  1237. }
  1238. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  1239. const char *projectName, const char *targetName,
  1240. const std::vector<std::string> *cmakeArgs,
  1241. std::string *output)
  1242. {
  1243. // does the binary directory exist ? If not create it...
  1244. if (!cmSystemTools::FileIsDirectory(bindir))
  1245. {
  1246. cmSystemTools::MakeDirectory(bindir);
  1247. }
  1248. // change to the tests directory and run cmake
  1249. // use the cmake object instead of calling cmake
  1250. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1251. cmSystemTools::ChangeDirectory(bindir);
  1252. // make sure the same generator is used
  1253. // use this program as the cmake to be run, it should not
  1254. // be run that way but the cmake object requires a vailid path
  1255. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  1256. cmake cm;
  1257. cm.SetIsInTryCompile(true);
  1258. cmGlobalGenerator *gg =
  1259. cm.CreateGlobalGenerator(m_LocalGenerator->GetGlobalGenerator()->GetName());
  1260. if (!gg)
  1261. {
  1262. cmSystemTools::Error(
  1263. "Internal CMake error, TryCompile bad GlobalGenerator");
  1264. // return to the original directory
  1265. cmSystemTools::ChangeDirectory(cwd.c_str());
  1266. return 1;
  1267. }
  1268. cm.SetGlobalGenerator(gg);
  1269. // do a configure
  1270. cm.SetHomeDirectory(srcdir);
  1271. cm.SetHomeOutputDirectory(bindir);
  1272. cm.SetStartDirectory(srcdir);
  1273. cm.SetStartOutputDirectory(bindir);
  1274. cm.SetCMakeCommand(cmakeCommand.c_str());
  1275. cm.LoadCache();
  1276. // if cmake args were provided then pass them in
  1277. if (cmakeArgs)
  1278. {
  1279. cm.SetCacheArgs(*cmakeArgs);
  1280. }
  1281. // to save time we pass the EnableLanguage info directly
  1282. gg->EnableLanguagesFromGenerator(m_LocalGenerator->GetGlobalGenerator());
  1283. if (cm.Configure() != 0)
  1284. {
  1285. cmSystemTools::Error(
  1286. "Internal CMake error, TryCompile configure of cmake failed");
  1287. // return to the original directory
  1288. cmSystemTools::ChangeDirectory(cwd.c_str());
  1289. return 1;
  1290. }
  1291. if (cm.Generate() != 0)
  1292. {
  1293. cmSystemTools::Error(
  1294. "Internal CMake error, TryCompile generation of cmake failed");
  1295. // return to the original directory
  1296. cmSystemTools::ChangeDirectory(cwd.c_str());
  1297. return 1;
  1298. }
  1299. // finally call the generator to actually build the resulting project
  1300. int ret =
  1301. m_LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  1302. projectName,
  1303. targetName,
  1304. output);
  1305. cmSystemTools::ChangeDirectory(cwd.c_str());
  1306. return ret;
  1307. }
  1308. cmCacheManager *cmMakefile::GetCacheManager() const
  1309. {
  1310. return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance()->GetCacheManager();
  1311. }
  1312. bool cmMakefile::GetLocal() const
  1313. {
  1314. return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance()->GetLocal();
  1315. }
  1316. void cmMakefile::DisplayStatus(const char* message, float s)
  1317. {
  1318. this->GetLocalGenerator()->GetGlobalGenerator()
  1319. ->GetCMakeInstance()->UpdateProgress(message, s);
  1320. }
  1321. /**
  1322. * Find the library with the given name. Searches the given path and then
  1323. * the system search path. Returns the full path to the library if it is
  1324. * found. Otherwise, the empty string is returned.
  1325. */
  1326. std::string cmMakefile::FindLibrary(const char* name,
  1327. const std::vector<std::string>& userPaths)
  1328. {
  1329. // See if the executable exists as written.
  1330. if(cmSystemTools::FileExists(name))
  1331. {
  1332. return cmSystemTools::CollapseFullPath(name);
  1333. }
  1334. // Add the system search path to our path.
  1335. std::vector<std::string> path = userPaths;
  1336. cmSystemTools::GetPath(path);
  1337. // Add some lib directories specific to compilers, depending on the
  1338. // current generator, so that library that might have been stored here
  1339. // can be found too.
  1340. // i.e. Microsoft Visual Studio or .Net: path to compiler/../Lib
  1341. // Borland: path to compiler/../Lib
  1342. const char* genName = this->GetDefinition("CMAKE_GENERATOR");
  1343. if (genName)
  1344. {
  1345. if (!strcmp(genName, "NMake Makefiles") ||
  1346. !strcmp(genName, "Visual Studio 6"))
  1347. {
  1348. const char* compiler = this->GetDefinition("CMAKE_CXX_COMPILER");
  1349. if (compiler)
  1350. {
  1351. std::string compiler_path = cmSystemTools::FindProgram(compiler);
  1352. if (compiler_path.size())
  1353. {
  1354. std::string lib_path =
  1355. cmSystemTools::GetFilenamePath(
  1356. cmSystemTools::GetFilenamePath(compiler_path)) + "/Lib";
  1357. path.push_back(lib_path);
  1358. }
  1359. }
  1360. }
  1361. else if (!strcmp(genName, "Visual Studio 7"))
  1362. {
  1363. // It is likely that the compiler won't be in the path for .Net, but
  1364. // we know where devenv is.
  1365. const char* devenv = this->GetDefinition("MICROSOFT_DEVENV");
  1366. if (devenv)
  1367. {
  1368. std::string devenv_path = cmSystemTools::FindProgram(devenv);
  1369. if (devenv_path.size())
  1370. {
  1371. std::string vc7_path =
  1372. cmSystemTools::GetFilenamePath(
  1373. cmSystemTools::GetFilenamePath(
  1374. cmSystemTools::GetFilenamePath(devenv_path))) + "/Vc7";
  1375. path.push_back(vc7_path + "/lib");
  1376. path.push_back(vc7_path + "/PlatformSDK/lib");
  1377. }
  1378. }
  1379. }
  1380. else if (!strcmp(genName, "Borland Makefiles"))
  1381. {
  1382. const char* bcb_bin_path = this->GetDefinition("BCB_BIN_PATH");
  1383. if (bcb_bin_path)
  1384. {
  1385. std::string lib_path =
  1386. cmSystemTools::GetFilenamePath(bcb_bin_path) + "/Lib";
  1387. path.push_back(lib_path);
  1388. }
  1389. }
  1390. }
  1391. return cmSystemTools::FindLibrary(name, path);
  1392. }