cmMakefile.cxx 44 KB

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