cmMakefile.cxx 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmMakefile.h"
  14. #include "cmCommand.h"
  15. #include "cmStandardIncludes.h"
  16. #include "cmSourceFile.h"
  17. #include "cmDirectory.h"
  18. #include "cmSystemTools.h"
  19. #include "cmGlobalGenerator.h"
  20. #include "cmLocalGenerator.h"
  21. #include "cmCommands.h"
  22. #include "cmCacheManager.h"
  23. #include "cmFunctionBlocker.h"
  24. #include "cmListFileCache.h"
  25. #include "cmake.h"
  26. #include <stdio.h> // required for sprintf
  27. // default is not to be building executables
  28. cmMakefile::cmMakefile()
  29. {
  30. // Setup the default include file regular expression (match everything).
  31. m_IncludeFileRegularExpression = "^.*$";
  32. // Setup the default include complaint regular expression (match nothing).
  33. m_ComplainFileRegularExpression = "^$";
  34. // Source and header file extensions that we can handle
  35. m_SourceFileExtensions.push_back( "cxx" );
  36. m_SourceFileExtensions.push_back( "cpp" );
  37. m_SourceFileExtensions.push_back( "c" );
  38. m_SourceFileExtensions.push_back( "M" );
  39. m_SourceFileExtensions.push_back( "m" );
  40. m_SourceFileExtensions.push_back( "mm" );
  41. m_HeaderFileExtensions.push_back( "h" );
  42. m_HeaderFileExtensions.push_back( "txx" );
  43. m_HeaderFileExtensions.push_back( "in" );
  44. m_DefineFlags = " ";
  45. m_LocalGenerator = 0;
  46. this->AddSourceGroup("", "^.*$");
  47. this->AddSourceGroup("Source Files", "\\.(cpp|C|c|cxx|rc|def|r|odl|idl|hpj|bat)$");
  48. this->AddSourceGroup("Header Files", "\\.(h|hh|hpp|hxx|hm|inl)$");
  49. this->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 = m_FunctionBlockers.begin())
  89. {
  90. cmFunctionBlocker* b = *pos;
  91. m_FunctionBlockers.remove(*pos);
  92. delete b;
  93. }
  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::SetProjectName(const char* p)
  525. {
  526. m_ProjectName = p;
  527. }
  528. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  529. {
  530. // for these targets do not add anything
  531. switch(target.GetType())
  532. {
  533. case cmTarget::UTILITY: return;
  534. case cmTarget::INSTALL_FILES: return;
  535. case cmTarget::INSTALL_PROGRAMS: return;
  536. default:;
  537. }
  538. std::vector<std::string>::iterator j;
  539. for(j = m_LinkDirectories.begin();
  540. j != m_LinkDirectories.end(); ++j)
  541. {
  542. target.AddLinkDirectory(j->c_str());
  543. }
  544. target.MergeLinkLibraries( *this, name, m_LinkLibraries );
  545. }
  546. void cmMakefile::AddLibrary(const char* lname, int shared,
  547. const std::vector<std::string> &srcs)
  548. {
  549. cmTarget target;
  550. switch (shared)
  551. {
  552. case 0:
  553. target.SetType(cmTarget::STATIC_LIBRARY);
  554. break;
  555. case 1:
  556. target.SetType(cmTarget::SHARED_LIBRARY);
  557. break;
  558. case 2:
  559. target.SetType(cmTarget::MODULE_LIBRARY);
  560. break;
  561. default:
  562. target.SetType(cmTarget::STATIC_LIBRARY);
  563. }
  564. // Clear its dependencies. Otherwise, dependencies might persist
  565. // over changes in CMakeLists.txt, making the information stale and
  566. // hence useless.
  567. std::string depname = lname;
  568. depname += "_LIB_DEPENDS";
  569. this->GetCacheManager()->
  570. AddCacheEntry(depname.c_str(), "",
  571. "Dependencies for target", cmCacheManager::STATIC);
  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. void cmMakefile::AddSourceGroup(const char* name, const char* regex)
  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. // We only want to set the regular expression. If there are already
  678. // source files in the group, we don't want to remove them.
  679. sg->SetGroupRegex(regex);
  680. return;
  681. }
  682. }
  683. // The group doesn't exist. Add it.
  684. m_SourceGroups.push_back(cmSourceGroup(name, regex));
  685. }
  686. void cmMakefile::AddExtraDirectory(const char* dir)
  687. {
  688. m_AuxSourceDirectories.push_back(dir);
  689. }
  690. // return the file name for the parent CMakeLists file to the
  691. // one passed in. Zero is returned if the CMakeLists file is the
  692. // one in the home directory or if for some reason a parent cmake lists
  693. // file cannot be found.
  694. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  695. {
  696. // extract the directory name
  697. std::string parentFile;
  698. std::string listsDir = currentFileName;
  699. std::string::size_type pos = listsDir.rfind('/');
  700. // if we could not find the directory return 0
  701. if(pos == std::string::npos)
  702. {
  703. return parentFile;
  704. }
  705. listsDir = listsDir.substr(0, pos);
  706. // if we are in the home directory then stop, return 0
  707. if(m_cmHomeDirectory == listsDir)
  708. {
  709. return parentFile;
  710. }
  711. // is there a parent directory we can check
  712. pos = listsDir.rfind('/');
  713. // if we could not find the directory return 0
  714. if(pos == std::string::npos)
  715. {
  716. return parentFile;
  717. }
  718. listsDir = listsDir.substr(0, pos);
  719. // is there a CMakeLists.txt file in the parent directory ?
  720. parentFile = listsDir;
  721. parentFile += "/CMakeLists.txt";
  722. while(!cmSystemTools::FileExists(parentFile.c_str()))
  723. {
  724. // There is no CMakeLists.txt file in the parent directory. This
  725. // can occur when coming out of a subdirectory resulting from a
  726. // SUBDIRS(Foo/Bar) command (coming out of Bar into Foo). Try
  727. // walking up until a CMakeLists.txt is found or the home
  728. // directory is hit.
  729. // if we are in the home directory then stop, return 0
  730. if(m_cmHomeDirectory == listsDir) { return ""; }
  731. // is there a parent directory we can check
  732. pos = listsDir.rfind('/');
  733. // if we could not find the directory return 0
  734. if(pos == std::string::npos) { return ""; }
  735. listsDir = listsDir.substr(0, pos);
  736. parentFile = listsDir;
  737. parentFile += "/CMakeLists.txt";
  738. }
  739. return parentFile;
  740. }
  741. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  742. // include and library directories.
  743. void cmMakefile::ExpandVariables()
  744. {
  745. // Now expand varibles in the include and link strings
  746. for(std::vector<std::string>::iterator d = m_IncludeDirectories.begin();
  747. d != m_IncludeDirectories.end(); ++d)
  748. {
  749. this->ExpandVariablesInString(*d);
  750. }
  751. for(std::vector<std::string>::iterator d = m_LinkDirectories.begin();
  752. d != m_LinkDirectories.end(); ++d)
  753. {
  754. this->ExpandVariablesInString(*d);
  755. }
  756. for(cmTarget::LinkLibraries::iterator l = m_LinkLibraries.begin();
  757. l != m_LinkLibraries.end(); ++l)
  758. {
  759. this->ExpandVariablesInString(l->first);
  760. }
  761. }
  762. bool cmMakefile::IsOn(const char* name) const
  763. {
  764. const char* value = this->GetDefinition(name);
  765. return cmSystemTools::IsOn(value);
  766. }
  767. const char* cmMakefile::GetDefinition(const char* name) const
  768. {
  769. DefinitionMap::const_iterator pos = m_Definitions.find(name);
  770. if(pos != m_Definitions.end())
  771. {
  772. return (*pos).second.c_str();
  773. }
  774. return this->GetCacheManager()->GetCacheValue(name);
  775. }
  776. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  777. {
  778. return this->ExpandVariablesInString(source, false);
  779. }
  780. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  781. bool escapeQuotes,
  782. bool atOnly) const
  783. {
  784. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  785. // with GetDefinition(), if not found in the map, nothing is expanded.
  786. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  787. // the current environment variables.
  788. // start by look for $ or @ in the string
  789. std::string::size_type markerPos;
  790. if(atOnly)
  791. {
  792. markerPos = source.find_first_of("@");
  793. }
  794. else
  795. {
  796. markerPos = source.find_first_of("$@");
  797. }
  798. // if not found, or found as the last character, then leave quickly as
  799. // nothing needs to be expanded
  800. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  801. {
  802. return source.c_str();
  803. }
  804. // current position
  805. std::string::size_type currentPos =0; // start at 0
  806. std::string result; // string with replacements
  807. // go until the the end of the string
  808. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  809. {
  810. // grab string from currentPos to the start of the variable
  811. // and add it to the result
  812. result += source.substr(currentPos, markerPos - currentPos);
  813. char endVariableMarker; // what is the end of the variable @ or }
  814. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  815. if(!atOnly && source[markerPos] == '$')
  816. {
  817. // ${var} case
  818. if(source[markerPos+1] == '{')
  819. {
  820. endVariableMarker = '}';
  821. markerStartSize = 2;
  822. }
  823. // $ENV{var} case
  824. else if(markerPos+4 < source.size() &&
  825. source[markerPos+4] == '{' &&
  826. !source.substr(markerPos+1, 3).compare("ENV"))
  827. {
  828. endVariableMarker = '}';
  829. markerStartSize = 5;
  830. }
  831. else
  832. {
  833. // bogus $ with no { so add $ to result and move on
  834. result += '$'; // add bogus $ back into string
  835. currentPos = markerPos+1; // move on
  836. endVariableMarker = ' '; // set end var to space so we can tell bogus
  837. }
  838. }
  839. else
  840. {
  841. // @VAR case
  842. endVariableMarker = '@';
  843. }
  844. // if it was a valid variable (started with @ or ${ or $ENV{ )
  845. if(endVariableMarker != ' ')
  846. {
  847. markerPos += markerStartSize; // move past marker
  848. // find the end variable marker starting at the markerPos
  849. std::string::size_type endVariablePos =
  850. source.find(endVariableMarker, markerPos);
  851. if(endVariablePos == std::string::npos)
  852. {
  853. // no end marker found so add the bogus start
  854. if(endVariableMarker == '@')
  855. {
  856. result += '@';
  857. }
  858. else
  859. {
  860. result += (markerStartSize == 5 ? "$ENV{" : "${");
  861. }
  862. currentPos = markerPos;
  863. }
  864. else
  865. {
  866. // good variable remove it
  867. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  868. bool found = false;
  869. if (markerStartSize == 5) // $ENV{
  870. {
  871. char *ptr = getenv(var.c_str());
  872. if (ptr)
  873. {
  874. if (escapeQuotes)
  875. {
  876. result += cmSystemTools::EscapeQuotes(ptr);
  877. }
  878. else
  879. {
  880. result += ptr;
  881. }
  882. found = true;
  883. }
  884. }
  885. else
  886. {
  887. const char* lookup = this->GetDefinition(var.c_str());
  888. if(lookup)
  889. {
  890. if (escapeQuotes)
  891. {
  892. result += cmSystemTools::EscapeQuotes(lookup);
  893. }
  894. else
  895. {
  896. result += lookup;
  897. }
  898. found = true;
  899. }
  900. }
  901. // if found add to result, if not, then it gets blanked
  902. if (!found)
  903. {
  904. // if no definition is found then add the var back
  905. if(endVariableMarker == '@')
  906. {
  907. result += "@";
  908. result += var;
  909. result += "@";
  910. }
  911. // do nothing, we remove the variable
  912. /* else
  913. {
  914. result += (markerStartSize == 5 ? "$ENV{" : "${");
  915. result += var;
  916. result += "}";
  917. }
  918. */
  919. }
  920. // lookup var, and replace it
  921. currentPos = endVariablePos+1;
  922. }
  923. }
  924. if(atOnly)
  925. {
  926. markerPos = source.find_first_of("@", currentPos);
  927. }
  928. else
  929. {
  930. markerPos = source.find_first_of("$@", currentPos);
  931. }
  932. }
  933. result += source.substr(currentPos); // pick up the rest of the string
  934. source = result;
  935. return source.c_str();
  936. }
  937. void cmMakefile::RemoveVariablesInString(std::string& source,
  938. bool atOnly) const
  939. {
  940. if(!atOnly)
  941. {
  942. cmRegularExpression var("(\\${[A-Za-z_0-9]*})");
  943. while (var.find(source))
  944. {
  945. source.erase(var.start(),var.end() - var.start());
  946. }
  947. }
  948. if(!atOnly)
  949. {
  950. cmRegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  951. while (varb.find(source))
  952. {
  953. source.erase(varb.start(),varb.end() - varb.start());
  954. }
  955. }
  956. cmRegularExpression var2("(@[A-Za-z_0-9]*@)");
  957. while (var2.find(source))
  958. {
  959. source.erase(var2.start(),var2.end() - var2.start());
  960. }
  961. }
  962. /**
  963. * Add the default definitions to the makefile. These values must not
  964. * be dependent on anything that isn't known when this cmMakefile instance
  965. * is constructed.
  966. */
  967. void cmMakefile::AddDefaultDefinitions()
  968. {
  969. #if defined(_WIN32) || defined(__CYGWIN__)
  970. this->AddDefinition("WIN32", "1");
  971. #else
  972. this->AddDefinition("UNIX", "1");
  973. #endif
  974. // Cygwin is more like unix so enable the unix commands
  975. #if defined(__CYGWIN__)
  976. this->AddDefinition("UNIX", "1");
  977. this->AddDefinition("CYGWIN", "1");
  978. #endif
  979. #if defined(__APPLE__)
  980. this->AddDefinition("APPLE", "1");
  981. #endif
  982. #if defined(_WIN32) && !defined(__CYGWIN__)
  983. this->AddDefinition("CMAKE_CFG_INTDIR","$(IntDir)");
  984. #else
  985. this->AddDefinition("CMAKE_CFG_INTDIR",".");
  986. #endif
  987. char temp[1024];
  988. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  989. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  990. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  991. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  992. }
  993. /**
  994. * Find a source group whose regular expression matches the filename
  995. * part of the given source name. Search backward through the list of
  996. * source groups, and take the first matching group found. This way
  997. * non-inherited SOURCE_GROUP commands will have precedence over
  998. * inherited ones.
  999. */
  1000. cmSourceGroup&
  1001. cmMakefile::FindSourceGroup(const char* source,
  1002. std::vector<cmSourceGroup> &groups)
  1003. {
  1004. std::string file = source;
  1005. std::string::size_type pos = file.rfind('/');
  1006. if(pos != std::string::npos)
  1007. {
  1008. file = file.substr(pos, file.length()-pos);
  1009. }
  1010. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1011. sg != groups.rend(); ++sg)
  1012. {
  1013. if(sg->Matches(file.c_str()))
  1014. {
  1015. return *sg;
  1016. }
  1017. }
  1018. // Shouldn't get here, but just in case, return the default group.
  1019. return groups.front();
  1020. }
  1021. bool cmMakefile::IsFunctionBlocked(const char *name,
  1022. std::vector<std::string> const&args)
  1023. {
  1024. // if there are no blockers get out of here
  1025. if (m_FunctionBlockers.begin() == m_FunctionBlockers.end())
  1026. {
  1027. return false;
  1028. }
  1029. // loop over all function blockers to see if any block this command
  1030. std::vector<std::string> expandedArguments;
  1031. for(std::vector<std::string>::const_iterator i = args.begin();
  1032. i != args.end(); ++i)
  1033. {
  1034. std::string tmps = *i;
  1035. this->ExpandVariablesInString(tmps);
  1036. if (tmps.find_first_not_of(" ") != std::string::npos)
  1037. {
  1038. // we found something in the args
  1039. expandedArguments.push_back(tmps);
  1040. }
  1041. }
  1042. // evaluate in reverse, this is critical for balanced IF statements etc
  1043. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1044. for (pos = m_FunctionBlockers.rbegin();
  1045. pos != m_FunctionBlockers.rend(); ++pos)
  1046. {
  1047. if ((*pos)->NeedExpandedVariables())
  1048. {
  1049. if ((*pos)->IsFunctionBlocked(name, expandedArguments, *this))
  1050. {
  1051. return true;
  1052. }
  1053. }
  1054. else
  1055. {
  1056. if ((*pos)->IsFunctionBlocked(name, args, *this))
  1057. {
  1058. return true;
  1059. }
  1060. }
  1061. }
  1062. return false;
  1063. }
  1064. void cmMakefile::RemoveFunctionBlocker(const char *name,
  1065. const std::vector<std::string> &args)
  1066. {
  1067. // loop over all function blockers to see if any block this command
  1068. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1069. for (pos = m_FunctionBlockers.rbegin();
  1070. pos != m_FunctionBlockers.rend(); ++pos)
  1071. {
  1072. if ((*pos)->ShouldRemove(name, args, *this))
  1073. {
  1074. cmFunctionBlocker* b = *pos;
  1075. m_FunctionBlockers.remove(*pos);
  1076. delete b;
  1077. return;
  1078. }
  1079. }
  1080. return;
  1081. }
  1082. void cmMakefile::SetHomeDirectory(const char* dir)
  1083. {
  1084. m_cmHomeDirectory = dir;
  1085. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1086. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1087. }
  1088. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1089. {
  1090. m_HomeOutputDirectory = lib;
  1091. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1092. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1093. }
  1094. /**
  1095. * Register the given cmData instance with its own name.
  1096. */
  1097. void cmMakefile::RegisterData(cmData* data)
  1098. {
  1099. std::string name = data->GetName();
  1100. DataMap::const_iterator d = m_DataMap.find(name);
  1101. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1102. {
  1103. delete d->second;
  1104. }
  1105. m_DataMap[name] = data;
  1106. }
  1107. /**
  1108. * Register the given cmData instance with the given name. This can be used
  1109. * to register a NULL pointer.
  1110. */
  1111. void cmMakefile::RegisterData(const char* name, cmData* data)
  1112. {
  1113. DataMap::const_iterator d = m_DataMap.find(name);
  1114. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1115. {
  1116. delete d->second;
  1117. }
  1118. m_DataMap[name] = data;
  1119. }
  1120. /**
  1121. * Lookup a cmData instance previously registered with the given name. If
  1122. * the instance cannot be found, return NULL.
  1123. */
  1124. cmData* cmMakefile::LookupData(const char* name) const
  1125. {
  1126. DataMap::const_iterator d = m_DataMap.find(name);
  1127. if(d != m_DataMap.end())
  1128. {
  1129. return d->second;
  1130. }
  1131. else
  1132. {
  1133. return 0;
  1134. }
  1135. }
  1136. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  1137. {
  1138. std::string s = cmSystemTools::GetFilenameName(sourceName);
  1139. std::string ext;
  1140. std::string::size_type pos = s.rfind('.');
  1141. if(pos != std::string::npos)
  1142. {
  1143. ext = s.substr(pos+1, s.size() - pos-1);
  1144. s = s.substr(0, pos);
  1145. }
  1146. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1147. i != m_SourceFiles.end(); ++i)
  1148. {
  1149. if ((*i)->GetSourceName() == s)
  1150. {
  1151. if ((ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1152. {
  1153. return *i;
  1154. }
  1155. }
  1156. }
  1157. return 0;
  1158. }
  1159. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  1160. {
  1161. // check to see if it exists
  1162. cmSourceFile* ret = this->GetSource(sf.GetSourceName().c_str());
  1163. if(ret && ret->GetSourceExtension() == sf.GetSourceExtension())
  1164. {
  1165. return ret;
  1166. }
  1167. ret = new cmSourceFile(sf);
  1168. m_SourceFiles.push_back(ret);
  1169. return ret;
  1170. }
  1171. void cmMakefile::EnableLanguage(const char* lang)
  1172. {
  1173. m_LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this);
  1174. }
  1175. void cmMakefile::ExpandSourceListArguments(
  1176. std::vector<std::string> const& arguments,
  1177. std::vector<std::string>& newargs, unsigned int start)
  1178. {
  1179. // first figure out if we need to handle version 1.2 style source lists
  1180. int oldVersion = 1;
  1181. const char* versionValue
  1182. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  1183. if (versionValue && atof(versionValue) > 1.2)
  1184. {
  1185. oldVersion = 0;
  1186. }
  1187. // now expand the args
  1188. std::vector<std::string> tmpArgs;
  1189. unsigned int i;
  1190. for(i = 0; i < arguments.size(); ++i)
  1191. {
  1192. // is the arg defined ?, if so use the def
  1193. const char *def = this->GetDefinition(arguments[i].c_str());
  1194. if (def && oldVersion && i >= start)
  1195. {
  1196. tmpArgs.push_back(def);
  1197. }
  1198. else
  1199. {
  1200. tmpArgs.push_back(arguments[i]);
  1201. }
  1202. }
  1203. cmSystemTools::ExpandListArguments(tmpArgs, newargs);
  1204. }
  1205. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  1206. const char *projectName, const char *targetName)
  1207. {
  1208. // does the binary directory exist ? If not create it...
  1209. if (!cmSystemTools::FileIsDirectory(bindir))
  1210. {
  1211. cmSystemTools::MakeDirectory(bindir);
  1212. }
  1213. // change to the tests directory and run cmake
  1214. // use the cmake object instead of calling cmake
  1215. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1216. cmSystemTools::ChangeDirectory(bindir);
  1217. // make sure the same generator is used
  1218. // use this program as the cmake to be run, it should not
  1219. // be run that way but the cmake object requires a vailid path
  1220. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  1221. cmake cm;
  1222. cm.SetIsInTryCompile(true);
  1223. cmGlobalGenerator *gg =
  1224. cm.CreateGlobalGenerator(m_LocalGenerator->GetGlobalGenerator()->GetName());
  1225. if (!gg)
  1226. {
  1227. cmSystemTools::Error(
  1228. "Internal CMake error, TryCompile bad GlobalGenerator");
  1229. // return to the original directory
  1230. cmSystemTools::ChangeDirectory(cwd.c_str());
  1231. return 1;
  1232. }
  1233. cm.SetGlobalGenerator(gg);
  1234. // do a configure
  1235. cm.SetHomeDirectory(srcdir);
  1236. cm.SetHomeOutputDirectory(bindir);
  1237. cm.SetStartDirectory(srcdir);
  1238. cm.SetStartOutputDirectory(bindir);
  1239. cm.SetCMakeCommand(cmakeCommand.c_str());
  1240. cm.LoadCache();
  1241. // to save time we pass the EnableLanguage info directly
  1242. gg->EnableLanguagesFromGenerator(m_LocalGenerator->GetGlobalGenerator(),
  1243. this);
  1244. if (cm.Configure() != 0)
  1245. {
  1246. cmSystemTools::Error(
  1247. "Internal CMake error, TryCompile configure of cmake failed");
  1248. // return to the original directory
  1249. cmSystemTools::ChangeDirectory(cwd.c_str());
  1250. return 1;
  1251. }
  1252. if (cm.Generate() != 0)
  1253. {
  1254. cmSystemTools::Error(
  1255. "Internal CMake error, TryCompile generation of cmake failed");
  1256. // return to the original directory
  1257. cmSystemTools::ChangeDirectory(cwd.c_str());
  1258. return 1;
  1259. }
  1260. // finally call the generator to actually build the resulting project
  1261. int ret =
  1262. m_LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  1263. projectName,
  1264. targetName);
  1265. cmSystemTools::ChangeDirectory(cwd.c_str());
  1266. return ret;
  1267. }
  1268. cmCacheManager *cmMakefile::GetCacheManager() const
  1269. {
  1270. return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance()->GetCacheManager();
  1271. }