cmMakefile.cxx 42 KB

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