cmMakefile.cxx 41 KB

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