cmMakefile.cxx 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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. target.ClearDependencyInformation( *this, lname );
  573. target.SetInAll(true);
  574. target.GetSourceLists() = srcs;
  575. this->AddGlobalLinkInformation(lname, target);
  576. m_Targets.insert(cmTargets::value_type(lname,target));
  577. // Add an entry into the cache
  578. std::string libPath = lname;
  579. libPath += "_CMAKE_PATH";
  580. this->GetCacheManager()->
  581. AddCacheEntry(libPath.c_str(),
  582. this->GetCurrentOutputDirectory(),
  583. "Path to a library", cmCacheManager::INTERNAL);
  584. // Add an entry into the cache
  585. std::string ltname = lname;
  586. ltname += "_LIBRARY_TYPE";
  587. switch (shared)
  588. {
  589. case 0:
  590. this->GetCacheManager()->AddCacheEntry(ltname.c_str(),"STATIC",
  591. "Whether a library is static, shared or module.",
  592. cmCacheManager::INTERNAL);
  593. break;
  594. case 1:
  595. this->GetCacheManager()->
  596. AddCacheEntry(ltname.c_str(),
  597. "SHARED",
  598. "Whether a library is static, shared or module.",
  599. cmCacheManager::INTERNAL);
  600. break;
  601. case 2:
  602. this->GetCacheManager()->
  603. AddCacheEntry(ltname.c_str(),
  604. "MODULE",
  605. "Whether a library is static, shared or module.",
  606. cmCacheManager::INTERNAL);
  607. break;
  608. default:
  609. this->GetCacheManager()->
  610. AddCacheEntry(ltname.c_str(),
  611. "STATIC",
  612. "Whether a library is static, shared or module.",
  613. cmCacheManager::INTERNAL);
  614. }
  615. }
  616. void cmMakefile::AddExecutable(const char *exeName,
  617. const std::vector<std::string> &srcs)
  618. {
  619. this->AddExecutable(exeName,srcs,false);
  620. }
  621. void cmMakefile::AddExecutable(const char *exeName,
  622. const std::vector<std::string> &srcs,
  623. bool win32)
  624. {
  625. cmTarget target;
  626. if (win32)
  627. {
  628. target.SetType(cmTarget::WIN32_EXECUTABLE);
  629. }
  630. else
  631. {
  632. target.SetType(cmTarget::EXECUTABLE);
  633. }
  634. target.SetInAll(true);
  635. target.GetSourceLists() = srcs;
  636. this->AddGlobalLinkInformation(exeName, target);
  637. m_Targets.insert(cmTargets::value_type(exeName,target));
  638. // Add an entry into the cache
  639. std::string exePath = exeName;
  640. exePath += "_CMAKE_PATH";
  641. this->GetCacheManager()->
  642. AddCacheEntry(exePath.c_str(),
  643. this->GetCurrentOutputDirectory(),
  644. "Path to an executable", cmCacheManager::INTERNAL);
  645. }
  646. void cmMakefile::AddUtilityCommand(const char* utilityName,
  647. const char* command,
  648. const char* arguments,
  649. bool all)
  650. {
  651. std::vector<std::string> empty;
  652. this->AddUtilityCommand(utilityName,command,arguments,all,
  653. empty,empty);
  654. }
  655. void cmMakefile::AddUtilityCommand(const char* utilityName,
  656. const char* command,
  657. const char* arguments,
  658. bool all,
  659. const std::vector<std::string> &dep,
  660. const std::vector<std::string> &out)
  661. {
  662. cmTarget target;
  663. target.SetType(cmTarget::UTILITY);
  664. target.SetInAll(all);
  665. cmCustomCommand cc(utilityName, command, arguments, dep, out);
  666. target.GetCustomCommands().push_back(cc);
  667. m_Targets.insert(cmTargets::value_type(utilityName,target));
  668. }
  669. cmSourceGroup* cmMakefile::GetSourceGroup(const char* name)
  670. {
  671. // First see if the group exists. If so, replace its regular expression.
  672. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  673. sg != m_SourceGroups.end(); ++sg)
  674. {
  675. std::string sgName = sg->GetName();
  676. if(sgName == name)
  677. {
  678. return &(*sg);
  679. }
  680. }
  681. return 0;
  682. }
  683. void cmMakefile::AddSourceGroup(const char* name, const char* regex)
  684. {
  685. // First see if the group exists. If so, replace its regular expression.
  686. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  687. sg != m_SourceGroups.end(); ++sg)
  688. {
  689. std::string sgName = sg->GetName();
  690. if(sgName == name)
  691. {
  692. if ( regex )
  693. {
  694. // We only want to set the regular expression. If there are already
  695. // source files in the group, we don't want to remove them.
  696. sg->SetGroupRegex(regex);
  697. }
  698. return;
  699. }
  700. }
  701. // The group doesn't exist. Add it.
  702. m_SourceGroups.push_back(cmSourceGroup(name, regex));
  703. }
  704. void cmMakefile::AddExtraDirectory(const char* dir)
  705. {
  706. m_AuxSourceDirectories.push_back(dir);
  707. }
  708. // return the file name for the parent CMakeLists file to the
  709. // one passed in. Zero is returned if the CMakeLists file is the
  710. // one in the home directory or if for some reason a parent cmake lists
  711. // file cannot be found.
  712. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  713. {
  714. // extract the directory name
  715. std::string parentFile;
  716. std::string listsDir = currentFileName;
  717. std::string::size_type pos = listsDir.rfind('/');
  718. // if we could not find the directory return 0
  719. if(pos == std::string::npos)
  720. {
  721. return parentFile;
  722. }
  723. listsDir = listsDir.substr(0, pos);
  724. // if we are in the home directory then stop, return 0
  725. if(m_cmHomeDirectory == listsDir)
  726. {
  727. return parentFile;
  728. }
  729. // is there a parent directory we can check
  730. pos = listsDir.rfind('/');
  731. // if we could not find the directory return 0
  732. if(pos == std::string::npos)
  733. {
  734. return parentFile;
  735. }
  736. listsDir = listsDir.substr(0, pos);
  737. // is there a CMakeLists.txt file in the parent directory ?
  738. parentFile = listsDir;
  739. parentFile += "/CMakeLists.txt";
  740. while(!cmSystemTools::FileExists(parentFile.c_str()))
  741. {
  742. // There is no CMakeLists.txt file in the parent directory. This
  743. // can occur when coming out of a subdirectory resulting from a
  744. // SUBDIRS(Foo/Bar) command (coming out of Bar into Foo). Try
  745. // walking up until a CMakeLists.txt is found or the home
  746. // directory is hit.
  747. // if we are in the home directory then stop, return 0
  748. if(m_cmHomeDirectory == listsDir) { return ""; }
  749. // is there a parent directory we can check
  750. pos = listsDir.rfind('/');
  751. // if we could not find the directory return 0
  752. if(pos == std::string::npos) { return ""; }
  753. listsDir = listsDir.substr(0, pos);
  754. parentFile = listsDir;
  755. parentFile += "/CMakeLists.txt";
  756. }
  757. return parentFile;
  758. }
  759. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  760. // include and library directories.
  761. void cmMakefile::ExpandVariables()
  762. {
  763. // Now expand varibles in the include and link strings
  764. for(std::vector<std::string>::iterator d = m_IncludeDirectories.begin();
  765. d != m_IncludeDirectories.end(); ++d)
  766. {
  767. this->ExpandVariablesInString(*d);
  768. }
  769. for(std::vector<std::string>::iterator d = m_LinkDirectories.begin();
  770. d != m_LinkDirectories.end(); ++d)
  771. {
  772. this->ExpandVariablesInString(*d);
  773. }
  774. for(cmTarget::LinkLibraries::iterator l = m_LinkLibraries.begin();
  775. l != m_LinkLibraries.end(); ++l)
  776. {
  777. this->ExpandVariablesInString(l->first);
  778. }
  779. }
  780. bool cmMakefile::IsOn(const char* name) const
  781. {
  782. const char* value = this->GetDefinition(name);
  783. return cmSystemTools::IsOn(value);
  784. }
  785. const char* cmMakefile::GetDefinition(const char* name) const
  786. {
  787. DefinitionMap::const_iterator pos = m_Definitions.find(name);
  788. if(pos != m_Definitions.end())
  789. {
  790. return (*pos).second.c_str();
  791. }
  792. return this->GetCacheManager()->GetCacheValue(name);
  793. }
  794. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  795. {
  796. return this->ExpandVariablesInString(source, false);
  797. }
  798. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  799. bool escapeQuotes,
  800. bool atOnly) const
  801. {
  802. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  803. // with GetDefinition(), if not found in the map, nothing is expanded.
  804. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  805. // the current environment variables.
  806. // start by look for $ or @ in the string
  807. std::string::size_type markerPos;
  808. if(atOnly)
  809. {
  810. markerPos = source.find_first_of("@");
  811. }
  812. else
  813. {
  814. markerPos = source.find_first_of("$@");
  815. }
  816. // if not found, or found as the last character, then leave quickly as
  817. // nothing needs to be expanded
  818. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  819. {
  820. return source.c_str();
  821. }
  822. // current position
  823. std::string::size_type currentPos =0; // start at 0
  824. std::string result; // string with replacements
  825. // go until the the end of the string
  826. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  827. {
  828. // grab string from currentPos to the start of the variable
  829. // and add it to the result
  830. result += source.substr(currentPos, markerPos - currentPos);
  831. char endVariableMarker; // what is the end of the variable @ or }
  832. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  833. if(!atOnly && source[markerPos] == '$')
  834. {
  835. // ${var} case
  836. if(source[markerPos+1] == '{')
  837. {
  838. endVariableMarker = '}';
  839. markerStartSize = 2;
  840. }
  841. // $ENV{var} case
  842. else if(markerPos+4 < source.size() &&
  843. source[markerPos+4] == '{' &&
  844. !source.substr(markerPos+1, 3).compare("ENV"))
  845. {
  846. endVariableMarker = '}';
  847. markerStartSize = 5;
  848. }
  849. else
  850. {
  851. // bogus $ with no { so add $ to result and move on
  852. result += '$'; // add bogus $ back into string
  853. currentPos = markerPos+1; // move on
  854. endVariableMarker = ' '; // set end var to space so we can tell bogus
  855. }
  856. }
  857. else
  858. {
  859. // @VAR case
  860. endVariableMarker = '@';
  861. }
  862. // if it was a valid variable (started with @ or ${ or $ENV{ )
  863. if(endVariableMarker != ' ')
  864. {
  865. markerPos += markerStartSize; // move past marker
  866. // find the end variable marker starting at the markerPos
  867. std::string::size_type endVariablePos =
  868. source.find(endVariableMarker, markerPos);
  869. if(endVariablePos == std::string::npos)
  870. {
  871. // no end marker found so add the bogus start
  872. if(endVariableMarker == '@')
  873. {
  874. result += '@';
  875. }
  876. else
  877. {
  878. result += (markerStartSize == 5 ? "$ENV{" : "${");
  879. }
  880. currentPos = markerPos;
  881. }
  882. else
  883. {
  884. // good variable remove it
  885. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  886. bool found = false;
  887. if (markerStartSize == 5) // $ENV{
  888. {
  889. char *ptr = getenv(var.c_str());
  890. if (ptr)
  891. {
  892. if (escapeQuotes)
  893. {
  894. result += cmSystemTools::EscapeQuotes(ptr);
  895. }
  896. else
  897. {
  898. result += ptr;
  899. }
  900. found = true;
  901. }
  902. }
  903. else
  904. {
  905. const char* lookup = this->GetDefinition(var.c_str());
  906. if(lookup)
  907. {
  908. if (escapeQuotes)
  909. {
  910. result += cmSystemTools::EscapeQuotes(lookup);
  911. }
  912. else
  913. {
  914. result += lookup;
  915. }
  916. found = true;
  917. }
  918. }
  919. // if found add to result, if not, then it gets blanked
  920. if (!found)
  921. {
  922. // if no definition is found then add the var back
  923. if(endVariableMarker == '@')
  924. {
  925. result += "@";
  926. result += var;
  927. result += "@";
  928. }
  929. // do nothing, we remove the variable
  930. /* else
  931. {
  932. result += (markerStartSize == 5 ? "$ENV{" : "${");
  933. result += var;
  934. result += "}";
  935. }
  936. */
  937. }
  938. // lookup var, and replace it
  939. currentPos = endVariablePos+1;
  940. }
  941. }
  942. if(atOnly)
  943. {
  944. markerPos = source.find_first_of("@", currentPos);
  945. }
  946. else
  947. {
  948. markerPos = source.find_first_of("$@", currentPos);
  949. }
  950. }
  951. result += source.substr(currentPos); // pick up the rest of the string
  952. source = result;
  953. return source.c_str();
  954. }
  955. void cmMakefile::RemoveVariablesInString(std::string& source,
  956. bool atOnly) const
  957. {
  958. if(!atOnly)
  959. {
  960. cmRegularExpression var("(\\${[A-Za-z_0-9]*})");
  961. while (var.find(source))
  962. {
  963. source.erase(var.start(),var.end() - var.start());
  964. }
  965. }
  966. if(!atOnly)
  967. {
  968. cmRegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  969. while (varb.find(source))
  970. {
  971. source.erase(varb.start(),varb.end() - varb.start());
  972. }
  973. }
  974. cmRegularExpression var2("(@[A-Za-z_0-9]*@)");
  975. while (var2.find(source))
  976. {
  977. source.erase(var2.start(),var2.end() - var2.start());
  978. }
  979. }
  980. /**
  981. * Add the default definitions to the makefile. These values must not
  982. * be dependent on anything that isn't known when this cmMakefile instance
  983. * is constructed.
  984. */
  985. void cmMakefile::AddDefaultDefinitions()
  986. {
  987. #if defined(_WIN32) || defined(__CYGWIN__)
  988. this->AddDefinition("WIN32", "1");
  989. #else
  990. this->AddDefinition("UNIX", "1");
  991. #endif
  992. // Cygwin is more like unix so enable the unix commands
  993. #if defined(__CYGWIN__)
  994. this->AddDefinition("UNIX", "1");
  995. this->AddDefinition("CYGWIN", "1");
  996. #endif
  997. #if defined(__APPLE__)
  998. this->AddDefinition("APPLE", "1");
  999. #endif
  1000. #if defined(_WIN32) && !defined(__CYGWIN__)
  1001. this->AddDefinition("CMAKE_CFG_INTDIR","$(IntDir)");
  1002. #else
  1003. this->AddDefinition("CMAKE_CFG_INTDIR",".");
  1004. #endif
  1005. char temp[1024];
  1006. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1007. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1008. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1009. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1010. }
  1011. /**
  1012. * Find a source group whose regular expression matches the filename
  1013. * part of the given source name. Search backward through the list of
  1014. * source groups, and take the first matching group found. This way
  1015. * non-inherited SOURCE_GROUP commands will have precedence over
  1016. * inherited ones.
  1017. */
  1018. cmSourceGroup&
  1019. cmMakefile::FindSourceGroup(const char* source,
  1020. std::vector<cmSourceGroup> &groups)
  1021. {
  1022. std::string file = source;
  1023. std::string::size_type pos = file.rfind('/');
  1024. if(pos != std::string::npos)
  1025. {
  1026. file = file.substr(pos, file.length()-pos);
  1027. }
  1028. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1029. sg != groups.rend(); ++sg)
  1030. {
  1031. if(sg->Matches(file.c_str()))
  1032. {
  1033. return *sg;
  1034. }
  1035. }
  1036. // Shouldn't get here, but just in case, return the default group.
  1037. return groups.front();
  1038. }
  1039. bool cmMakefile::IsFunctionBlocked(const char *name,
  1040. std::vector<std::string> const&args)
  1041. {
  1042. // if there are no blockers get out of here
  1043. if (m_FunctionBlockers.begin() == m_FunctionBlockers.end())
  1044. {
  1045. return false;
  1046. }
  1047. // loop over all function blockers to see if any block this command
  1048. std::vector<std::string> expandedArguments;
  1049. for(std::vector<std::string>::const_iterator i = args.begin();
  1050. i != args.end(); ++i)
  1051. {
  1052. std::string tmps = *i;
  1053. this->ExpandVariablesInString(tmps);
  1054. if (tmps.find_first_not_of(" ") != std::string::npos)
  1055. {
  1056. // we found something in the args
  1057. expandedArguments.push_back(tmps);
  1058. }
  1059. }
  1060. // evaluate in reverse, this is critical for balanced IF statements etc
  1061. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1062. for (pos = m_FunctionBlockers.rbegin();
  1063. pos != m_FunctionBlockers.rend(); ++pos)
  1064. {
  1065. if ((*pos)->NeedExpandedVariables())
  1066. {
  1067. if ((*pos)->IsFunctionBlocked(name, expandedArguments, *this))
  1068. {
  1069. return true;
  1070. }
  1071. }
  1072. else
  1073. {
  1074. if ((*pos)->IsFunctionBlocked(name, args, *this))
  1075. {
  1076. return true;
  1077. }
  1078. }
  1079. }
  1080. return false;
  1081. }
  1082. void cmMakefile::RemoveFunctionBlocker(const char *name,
  1083. const std::vector<std::string> &args)
  1084. {
  1085. // loop over all function blockers to see if any block this command
  1086. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1087. for (pos = m_FunctionBlockers.rbegin();
  1088. pos != m_FunctionBlockers.rend(); ++pos)
  1089. {
  1090. if ((*pos)->ShouldRemove(name, args, *this))
  1091. {
  1092. cmFunctionBlocker* b = *pos;
  1093. m_FunctionBlockers.remove(*pos);
  1094. delete b;
  1095. break;
  1096. }
  1097. }
  1098. return;
  1099. }
  1100. void cmMakefile::SetHomeDirectory(const char* dir)
  1101. {
  1102. m_cmHomeDirectory = dir;
  1103. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1104. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1105. }
  1106. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1107. {
  1108. m_HomeOutputDirectory = lib;
  1109. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1110. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1111. }
  1112. /**
  1113. * Register the given cmData instance with its own name.
  1114. */
  1115. void cmMakefile::RegisterData(cmData* data)
  1116. {
  1117. std::string name = data->GetName();
  1118. DataMap::const_iterator d = m_DataMap.find(name);
  1119. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1120. {
  1121. delete d->second;
  1122. }
  1123. m_DataMap[name] = data;
  1124. }
  1125. /**
  1126. * Register the given cmData instance with the given name. This can be used
  1127. * to register a NULL pointer.
  1128. */
  1129. void cmMakefile::RegisterData(const char* name, cmData* data)
  1130. {
  1131. DataMap::const_iterator d = m_DataMap.find(name);
  1132. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1133. {
  1134. delete d->second;
  1135. }
  1136. m_DataMap[name] = data;
  1137. }
  1138. /**
  1139. * Lookup a cmData instance previously registered with the given name. If
  1140. * the instance cannot be found, return NULL.
  1141. */
  1142. cmData* cmMakefile::LookupData(const char* name) const
  1143. {
  1144. DataMap::const_iterator d = m_DataMap.find(name);
  1145. if(d != m_DataMap.end())
  1146. {
  1147. return d->second;
  1148. }
  1149. else
  1150. {
  1151. return 0;
  1152. }
  1153. }
  1154. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  1155. {
  1156. std::string s = cmSystemTools::GetFilenameName(sourceName);
  1157. std::string ext;
  1158. std::string::size_type pos = s.rfind('.');
  1159. if(pos != std::string::npos)
  1160. {
  1161. ext = s.substr(pos+1, s.size() - pos-1);
  1162. s = s.substr(0, pos);
  1163. }
  1164. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1165. i != m_SourceFiles.end(); ++i)
  1166. {
  1167. if ((*i)->GetSourceName() == s)
  1168. {
  1169. if ((ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1170. {
  1171. return *i;
  1172. }
  1173. }
  1174. }
  1175. return 0;
  1176. }
  1177. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  1178. {
  1179. // check to see if it exists
  1180. cmSourceFile* ret = this->GetSource(sf.GetSourceName().c_str());
  1181. if(ret && ret->GetSourceExtension() == sf.GetSourceExtension())
  1182. {
  1183. return ret;
  1184. }
  1185. ret = new cmSourceFile(sf);
  1186. m_SourceFiles.push_back(ret);
  1187. return ret;
  1188. }
  1189. void cmMakefile::EnableLanguage(const char* lang)
  1190. {
  1191. m_LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this);
  1192. }
  1193. void cmMakefile::ExpandSourceListArguments(
  1194. std::vector<std::string> const& arguments,
  1195. std::vector<std::string>& newargs, unsigned int start)
  1196. {
  1197. // first figure out if we need to handle version 1.2 style source lists
  1198. int oldVersion = 1;
  1199. const char* versionValue
  1200. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  1201. if (versionValue && atof(versionValue) > 1.2)
  1202. {
  1203. oldVersion = 0;
  1204. }
  1205. // now expand the args
  1206. std::vector<std::string> tmpArgs;
  1207. unsigned int i;
  1208. for(i = 0; i < arguments.size(); ++i)
  1209. {
  1210. // is the arg defined ?, if so use the def
  1211. const char *def = this->GetDefinition(arguments[i].c_str());
  1212. if (def && oldVersion && i >= start)
  1213. {
  1214. tmpArgs.push_back(def);
  1215. }
  1216. else
  1217. {
  1218. tmpArgs.push_back(arguments[i]);
  1219. }
  1220. }
  1221. cmSystemTools::ExpandListArguments(tmpArgs, newargs);
  1222. }
  1223. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  1224. const char *projectName, const char *targetName,
  1225. const std::vector<std::string> *cmakeArgs,
  1226. std::string *output)
  1227. {
  1228. // does the binary directory exist ? If not create it...
  1229. if (!cmSystemTools::FileIsDirectory(bindir))
  1230. {
  1231. cmSystemTools::MakeDirectory(bindir);
  1232. }
  1233. // change to the tests directory and run cmake
  1234. // use the cmake object instead of calling cmake
  1235. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1236. cmSystemTools::ChangeDirectory(bindir);
  1237. // make sure the same generator is used
  1238. // use this program as the cmake to be run, it should not
  1239. // be run that way but the cmake object requires a vailid path
  1240. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  1241. cmake cm;
  1242. cm.SetIsInTryCompile(true);
  1243. cmGlobalGenerator *gg =
  1244. cm.CreateGlobalGenerator(m_LocalGenerator->GetGlobalGenerator()->GetName());
  1245. if (!gg)
  1246. {
  1247. cmSystemTools::Error(
  1248. "Internal CMake error, TryCompile bad GlobalGenerator");
  1249. // return to the original directory
  1250. cmSystemTools::ChangeDirectory(cwd.c_str());
  1251. return 1;
  1252. }
  1253. cm.SetGlobalGenerator(gg);
  1254. // do a configure
  1255. cm.SetHomeDirectory(srcdir);
  1256. cm.SetHomeOutputDirectory(bindir);
  1257. cm.SetStartDirectory(srcdir);
  1258. cm.SetStartOutputDirectory(bindir);
  1259. cm.SetCMakeCommand(cmakeCommand.c_str());
  1260. cm.LoadCache();
  1261. // if cmake args were provided then pass them in
  1262. if (cmakeArgs)
  1263. {
  1264. cm.SetCacheArgs(*cmakeArgs);
  1265. }
  1266. // to save time we pass the EnableLanguage info directly
  1267. gg->EnableLanguagesFromGenerator(m_LocalGenerator->GetGlobalGenerator());
  1268. if (cm.Configure() != 0)
  1269. {
  1270. cmSystemTools::Error(
  1271. "Internal CMake error, TryCompile configure of cmake failed");
  1272. // return to the original directory
  1273. cmSystemTools::ChangeDirectory(cwd.c_str());
  1274. return 1;
  1275. }
  1276. if (cm.Generate() != 0)
  1277. {
  1278. cmSystemTools::Error(
  1279. "Internal CMake error, TryCompile generation of cmake failed");
  1280. // return to the original directory
  1281. cmSystemTools::ChangeDirectory(cwd.c_str());
  1282. return 1;
  1283. }
  1284. // finally call the generator to actually build the resulting project
  1285. int ret =
  1286. m_LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  1287. projectName,
  1288. targetName,
  1289. output);
  1290. cmSystemTools::ChangeDirectory(cwd.c_str());
  1291. return ret;
  1292. }
  1293. cmCacheManager *cmMakefile::GetCacheManager() const
  1294. {
  1295. return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance()->GetCacheManager();
  1296. }
  1297. bool cmMakefile::GetLocal() const
  1298. {
  1299. return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance()->GetLocal();
  1300. }
  1301. void cmMakefile::DisplayStatus(const char* message, float s)
  1302. {
  1303. this->GetLocalGenerator()->GetGlobalGenerator()
  1304. ->GetCMakeInstance()->UpdateProgress(message, s);
  1305. }