cmMakefile.cxx 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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 "cmMakefileGenerator.h"
  20. #include "cmCommands.h"
  21. #include "cmCacheManager.h"
  22. #include "cmFunctionBlocker.h"
  23. #include "cmListFileCache.h"
  24. #include <stdio.h> // required for sprintf
  25. // default is not to be building executables
  26. cmMakefile::cmMakefile()
  27. {
  28. // Setup the default include file regular expression (match everything).
  29. m_IncludeFileRegularExpression = "^.*$";
  30. // Setup the default include complaint regular expression (match nothing).
  31. m_ComplainFileRegularExpression = "^$";
  32. // Source and header file extensions that we can handle
  33. m_SourceFileExtensions.push_back( "cxx" );
  34. m_SourceFileExtensions.push_back( "cpp" );
  35. m_SourceFileExtensions.push_back( "c" );
  36. m_SourceFileExtensions.push_back( "M" );
  37. m_SourceFileExtensions.push_back( "m" );
  38. m_SourceFileExtensions.push_back( "mm" );
  39. m_HeaderFileExtensions.push_back( "h" );
  40. m_HeaderFileExtensions.push_back( "txx" );
  41. m_DefineFlags = " ";
  42. m_MakefileGenerator = 0;
  43. this->AddSourceGroup("", "^.*$");
  44. this->AddSourceGroup("Source Files", "\\.(cpp|C|c|cxx|rc|def|r|odl|idl|hpj|bat)$");
  45. this->AddSourceGroup("Header Files", "\\.(h|hh|hpp|hxx|hm|inl)$");
  46. this->AddDefaultCommands();
  47. this->AddDefaultDefinitions();
  48. }
  49. unsigned int cmMakefile::GetCacheMajorVersion()
  50. {
  51. if(!cmCacheManager::GetInstance()->
  52. GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
  53. {
  54. return 0;
  55. }
  56. return atoi(cmCacheManager::GetInstance()->
  57. GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"));
  58. }
  59. unsigned int cmMakefile::GetCacheMinorVersion()
  60. {
  61. if(!cmCacheManager::GetInstance()->
  62. GetCacheValue("Cmake_Cache_MINOR_VERSION"))
  63. {
  64. return 0;
  65. }
  66. return atoi(cmCacheManager::GetInstance()->
  67. GetCacheValue("CMAKE_CACHE_MINOR_VERSION"));
  68. }
  69. void cmMakefile::AddDefaultCommands()
  70. {
  71. std::list<cmCommand*> commands;
  72. GetPredefinedCommands(commands);
  73. for(std::list<cmCommand*>::iterator i = commands.begin();
  74. i != commands.end(); ++i)
  75. {
  76. this->AddCommand(*i);
  77. }
  78. #if defined(_WIN32) || defined(__CYGWIN__)
  79. this->AddDefinition("WIN32", "1");
  80. #else
  81. this->AddDefinition("UNIX", "1");
  82. #endif
  83. // Cygwin is more like unix so enable the unix commands
  84. #if defined(__CYGWIN__)
  85. this->AddDefinition("UNIX", "1");
  86. this->AddDefinition("CYGWIN", "1");
  87. #endif
  88. #if defined(__APPLE__)
  89. this->AddDefinition("APPLE", "1");
  90. #endif
  91. }
  92. cmMakefile::~cmMakefile()
  93. {
  94. for(unsigned int i=0; i < m_UsedCommands.size(); i++)
  95. {
  96. delete m_UsedCommands[i];
  97. }
  98. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  99. j != m_Commands.end(); ++j)
  100. {
  101. delete (*j).second;
  102. }
  103. for(DataMap::const_iterator d = m_DataMap.begin();
  104. d != m_DataMap.end(); ++d)
  105. {
  106. if(d->second)
  107. {
  108. delete d->second;
  109. }
  110. }
  111. std::set<cmFunctionBlocker *>::const_iterator pos;
  112. for (pos = m_FunctionBlockers.begin();
  113. pos != m_FunctionBlockers.end(); pos = m_FunctionBlockers.begin())
  114. {
  115. cmFunctionBlocker* b = *pos;
  116. m_FunctionBlockers.erase(*pos);
  117. delete b;
  118. }
  119. delete m_MakefileGenerator;
  120. }
  121. void cmMakefile::PrintStringVector(const char* s, const std::vector<std::string>& v) const
  122. {
  123. std::cout << s << ": ( \n";
  124. for(std::vector<std::string>::const_iterator i = v.begin();
  125. i != v.end(); ++i)
  126. {
  127. std::cout << (*i).c_str() << " ";
  128. }
  129. std::cout << " )\n";
  130. }
  131. // call print on all the classes in the makefile
  132. void cmMakefile::Print() const
  133. {
  134. // print the class lists
  135. std::cout << "classes:\n";
  136. for(SourceMap::const_iterator l = m_Sources.begin();
  137. l != m_Sources.end(); l++)
  138. {
  139. std::cout << " Class list named: " << l->first << std::endl;
  140. for(std::vector<cmSourceFile>::const_iterator i = l->second.begin();
  141. i != l->second.end(); i++)
  142. {
  143. i->Print();
  144. }
  145. }
  146. std::cout << " m_Targets: ";
  147. for (cmTargets::const_iterator l = m_Targets.begin();
  148. l != m_Targets.end(); l++)
  149. {
  150. std::cout << l->first << std::endl;
  151. }
  152. std::cout << " m_CurrentOutputDirectory; " <<
  153. m_CurrentOutputDirectory.c_str() << std::endl;
  154. std::cout << " m_StartOutputDirectory; " <<
  155. m_StartOutputDirectory.c_str() << std::endl;
  156. std::cout << " m_HomeOutputDirectory; " <<
  157. m_HomeOutputDirectory.c_str() << std::endl;
  158. std::cout << " m_cmCurrentDirectory; " <<
  159. m_cmCurrentDirectory.c_str() << std::endl;
  160. std::cout << " m_cmStartDirectory; " <<
  161. m_cmStartDirectory.c_str() << std::endl;
  162. std::cout << " m_cmHomeDirectory; " <<
  163. m_cmHomeDirectory.c_str() << std::endl;
  164. std::cout << " m_ProjectName; " << m_ProjectName.c_str() << std::endl;
  165. this->PrintStringVector("m_SubDirectories ", m_SubDirectories);
  166. this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
  167. this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
  168. for( std::vector<cmSourceGroup>::const_iterator i = m_SourceGroups.begin();
  169. i != m_SourceGroups.end(); ++i)
  170. {
  171. i->Print();
  172. }
  173. }
  174. bool cmMakefile::CommandExists(const char* name) const
  175. {
  176. return (m_Commands.find(name) != m_Commands.end());
  177. }
  178. void cmMakefile::ExecuteCommand(std::string &name,
  179. std::vector<std::string> const& arguments)
  180. {
  181. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  182. if(pos != m_Commands.end())
  183. {
  184. cmCommand* rm = (*pos).second;
  185. cmCommand* usedCommand = rm->Clone();
  186. usedCommand->SetMakefile(this);
  187. bool keepCommand = false;
  188. if(usedCommand->GetEnabled())
  189. {
  190. // if not running in inherit mode or
  191. // if the command is inherited then InitialPass it.
  192. if(!m_Inheriting || usedCommand->IsInherited())
  193. {
  194. std::vector<std::string> expandedArguments = arguments;
  195. for(std::vector<std::string>::iterator i = expandedArguments.begin();
  196. i != expandedArguments.end(); ++i)
  197. {
  198. this->ExpandVariablesInString(*i);
  199. }
  200. if(!usedCommand->InitialPass(expandedArguments))
  201. {
  202. cmSystemTools::Error(usedCommand->GetName(),
  203. ": Error : \n",
  204. usedCommand->GetError(),
  205. m_cmCurrentDirectory.c_str());
  206. }
  207. else
  208. {
  209. // use the command
  210. keepCommand = true;
  211. m_UsedCommands.push_back(usedCommand);
  212. }
  213. }
  214. }
  215. // if the Cloned command was not used
  216. // then delete it
  217. if(!keepCommand)
  218. {
  219. delete usedCommand;
  220. }
  221. }
  222. else
  223. {
  224. cmSystemTools::Error("unknown CMake command:", name.c_str(),
  225. "\nReading cmake file in directory:" ,
  226. m_cmCurrentDirectory.c_str());
  227. }
  228. }
  229. // Parse the given CMakeLists.txt file into a list of classes.
  230. // Reads in current CMakeLists file and all parent CMakeLists files
  231. // executing all inherited commands in the parents
  232. //
  233. // if external is non-zero, this means that we have branched to grab some
  234. // commands from a remote list-file (that is, the equivalent of a
  235. // #include has been called). We DO NOT look at the parents of this
  236. // list-file, and for all other purposes, the name of this list-file
  237. // is "filename" and not "external".
  238. bool cmMakefile::ReadListFile(const char* filename, const char* external)
  239. {
  240. // keep track of the current file being read
  241. if (filename)
  242. {
  243. if(m_cmCurrentListFile != filename)
  244. {
  245. m_cmCurrentListFile = filename;
  246. }
  247. }
  248. // if this is not a remote makefile
  249. // (if it were, this would be called from the "filename" call,
  250. // rather than the "external" call)
  251. if (!external)
  252. {
  253. // is there a parent CMakeLists file that does not go beyond the
  254. // Home directory? if so recurse and read in that List file
  255. std::string parentList = this->GetParentListFileName(filename);
  256. if (parentList != "")
  257. {
  258. std::string srcdir = m_cmCurrentDirectory;
  259. std::string bindir = m_CurrentOutputDirectory;
  260. std::string::size_type pos = parentList.rfind('/');
  261. m_cmCurrentDirectory = parentList.substr(0, pos);
  262. m_CurrentOutputDirectory = m_HomeOutputDirectory + parentList.substr(m_cmHomeDirectory.size(), pos - m_cmHomeDirectory.size());
  263. // if not found, oops
  264. if(pos == std::string::npos)
  265. {
  266. cmSystemTools::Error("Trailing slash not found");
  267. }
  268. this->ReadListFile(parentList.c_str());
  269. // restore the current directory
  270. m_cmCurrentDirectory = srcdir;
  271. m_CurrentOutputDirectory = bindir;
  272. }
  273. }
  274. // are we at the start CMakeLists file or are we processing a parent
  275. // lists file
  276. //
  277. // this might, or might not be true, irrespective if we are
  278. // off looking at an external makefile.
  279. m_Inheriting = (m_cmCurrentDirectory != m_cmStartDirectory);
  280. // Now read the input file
  281. const char *filenametoread= filename;
  282. if( external)
  283. {
  284. filenametoread= external;
  285. }
  286. cmListFile* lf =
  287. cmListFileCache::GetInstance()->GetFileCache(filenametoread);
  288. if(!lf)
  289. {
  290. cmSystemTools::Error("error can not open file ", filenametoread);
  291. return false;
  292. }
  293. // add this list file to the list of dependencies
  294. m_ListFiles.push_back( filenametoread);
  295. const size_t numberFunctions = lf->m_Functions.size();
  296. for(size_t i =0; i < numberFunctions; ++i)
  297. {
  298. cmListFileFunction& curFunction = lf->m_Functions[i];
  299. if(!this->IsFunctionBlocked(curFunction.m_Name.c_str(),
  300. curFunction.m_Arguments))
  301. {
  302. this->ExecuteCommand(curFunction.m_Name,
  303. curFunction.m_Arguments);
  304. }
  305. }
  306. // send scope ended to and funciton blockers
  307. if (filename)
  308. {
  309. // loop over all function blockers to see if any block this command
  310. std::set<cmFunctionBlocker *>::const_iterator pos;
  311. for (pos = m_FunctionBlockers.begin();
  312. pos != m_FunctionBlockers.end(); ++pos)
  313. {
  314. (*pos)->ScopeEnded(*this);
  315. }
  316. }
  317. return true;
  318. }
  319. cmSourceFile *cmMakefile::GetSource(const char *srclist, const char *cname)
  320. {
  321. SourceMap::iterator sl = m_Sources.find(srclist);
  322. // find the src list
  323. if (sl == m_Sources.end())
  324. {
  325. return 0;
  326. }
  327. // find the class
  328. for (std::vector<cmSourceFile>::iterator i = sl->second.begin();
  329. i != sl->second.end(); ++i)
  330. {
  331. if (i->GetSourceName() == cname)
  332. {
  333. return &(*i);
  334. }
  335. }
  336. return 0;
  337. }
  338. void cmMakefile::AddCommand(cmCommand* wg)
  339. {
  340. std::string name = wg->GetName();
  341. m_Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  342. }
  343. // Set the make file
  344. void cmMakefile::SetMakefileGenerator(cmMakefileGenerator* mf)
  345. {
  346. if(mf == m_MakefileGenerator)
  347. {
  348. return;
  349. }
  350. delete m_MakefileGenerator;
  351. m_MakefileGenerator = mf;
  352. mf->SetMakefile(this);
  353. }
  354. void cmMakefile::FinalPass()
  355. {
  356. // do all the variable expansions here
  357. this->ExpandVariables();
  358. // give all the commands a chance to do something
  359. // after the file has been parsed before generation
  360. for(std::vector<cmCommand*>::iterator i = m_UsedCommands.begin();
  361. i != m_UsedCommands.end(); ++i)
  362. {
  363. (*i)->FinalPass();
  364. }
  365. }
  366. // Generate the output file
  367. void cmMakefile::GenerateMakefile()
  368. {
  369. this->FinalPass();
  370. // merge libraries
  371. for (cmTargets::iterator l = m_Targets.begin();
  372. l != m_Targets.end(); l++)
  373. {
  374. l->second.GenerateSourceFilesFromSourceLists(*this);
  375. l->second.MergeLibraries(m_LinkLibraries);
  376. }
  377. // now do the generation
  378. m_MakefileGenerator->GenerateMakefile();
  379. }
  380. void cmMakefile::AddSource(cmSourceFile& cmfile, const char *srclist)
  381. {
  382. m_Sources[srclist].push_back(cmfile);
  383. }
  384. struct FindSrcByName : std::binary_function<cmSourceFile, cmSourceFile, bool>
  385. {
  386. public:
  387. bool operator () (const cmSourceFile &f, const cmSourceFile &test) const
  388. {
  389. return !strcmp(f.GetSourceName().c_str(),test.GetSourceName().c_str());
  390. }
  391. };
  392. void cmMakefile::RemoveSource(cmSourceFile& cmfile,const char *srclist)
  393. {
  394. std::vector<cmSourceFile> &maplist = m_Sources[srclist];
  395. std::vector<cmSourceFile>::iterator f =
  396. std::find_if(maplist.begin(), maplist.end(), std::bind2nd(FindSrcByName(),cmfile));
  397. // std::vector<cmSourceFile>::iterator f = find_if(maplist.begin(), maplist.end(), matches(srclist);
  398. if (f!=maplist.end())
  399. {
  400. maplist.erase(f);
  401. }
  402. }
  403. void cmMakefile::AddCustomCommand(const char* source,
  404. const char* command,
  405. const std::vector<std::string>& commandArgs,
  406. const std::vector<std::string>& depends,
  407. const std::vector<std::string>& outputs,
  408. const char *target)
  409. {
  410. // find the target,
  411. if (m_Targets.find(target) != m_Targets.end())
  412. {
  413. std::string expandC = command;
  414. this->ExpandVariablesInString(expandC);
  415. std::string c = cmSystemTools::EscapeSpaces(expandC.c_str());
  416. std::string combinedArgs;
  417. unsigned int i;
  418. for (i = 0; i < commandArgs.size(); ++i)
  419. {
  420. combinedArgs += cmSystemTools::EscapeSpaces(commandArgs[i].c_str());
  421. combinedArgs += " ";
  422. }
  423. cmCustomCommand cc(source,c.c_str(),combinedArgs.c_str(),depends,outputs);
  424. m_Targets[target].GetCustomCommands().push_back(cc);
  425. std::string cacheCommand = command;
  426. this->ExpandVariablesInString(cacheCommand);
  427. if(cmCacheManager::GetInstance()->GetCacheValue(cacheCommand.c_str()))
  428. {
  429. m_Targets[target].AddUtility(
  430. cmCacheManager::GetInstance()->GetCacheValue(cacheCommand.c_str()));
  431. }
  432. }
  433. }
  434. void cmMakefile::AddCustomCommand(const char* source,
  435. const char* command,
  436. const std::vector<std::string>& commandArgs,
  437. const std::vector<std::string>& depends,
  438. const char* output,
  439. const char *target)
  440. {
  441. std::vector<std::string> outputs;
  442. outputs.push_back(output);
  443. this->AddCustomCommand(source, command, commandArgs, depends, outputs, target);
  444. }
  445. void cmMakefile::AddDefineFlag(const char* flag)
  446. {
  447. m_DefineFlags += " ";
  448. m_DefineFlags += flag;
  449. }
  450. void cmMakefile::AddLinkLibrary(const char* lib, cmTarget::LinkLibraryType llt)
  451. {
  452. m_LinkLibraries.push_back(
  453. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  454. }
  455. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  456. const char* lib,
  457. cmTarget::LinkLibraryType llt)
  458. {
  459. if (m_Targets.find(target) != m_Targets.end())
  460. {
  461. m_Targets[target].GetLinkLibraries().
  462. push_back(
  463. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  464. }
  465. else
  466. {
  467. cmSystemTools::Error("Attempt to add link libraries to non-existant target: ", target, " for lib ", lib);
  468. }
  469. }
  470. void cmMakefile::AddLinkLibrary(const char* lib)
  471. {
  472. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  473. }
  474. void cmMakefile::AddLinkDirectory(const char* dir)
  475. {
  476. // Don't add a link directory that is already present. Yes, this
  477. // linear search results in n^2 behavior, but n won't be getting
  478. // much bigger than 20. We cannot use a set because of order
  479. // dependency of the link search path.
  480. if(std::find(m_LinkDirectories.begin(),
  481. m_LinkDirectories.end(), dir) == m_LinkDirectories.end())
  482. {
  483. m_LinkDirectories.push_back(dir);
  484. }
  485. }
  486. void cmMakefile::AddSubDirectory(const char* sub)
  487. {
  488. m_SubDirectories.push_back(sub);
  489. }
  490. void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
  491. {
  492. // Don't add an include directory that is already present. Yes,
  493. // this linear search results in n^2 behavior, but n won't be
  494. // getting much bigger than 20. We cannot use a set because of
  495. // order dependency of the include path.
  496. if(std::find(m_IncludeDirectories.begin(),
  497. m_IncludeDirectories.end(), inc) == m_IncludeDirectories.end())
  498. {
  499. if (before)
  500. {
  501. // WARNING: this *is* expensive (linear time) since it's a vector
  502. m_IncludeDirectories.insert(m_IncludeDirectories.begin(), inc);
  503. }
  504. else
  505. {
  506. m_IncludeDirectories.push_back(inc);
  507. }
  508. }
  509. }
  510. void cmMakefile::AddDefinition(const char* name, const char* value)
  511. {
  512. m_Definitions.erase( DefinitionMap::key_type(name));
  513. m_Definitions.insert(DefinitionMap::value_type(name, value));
  514. }
  515. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  516. const char* doc,
  517. cmCacheManager::CacheEntryType type)
  518. {
  519. cmCacheManager::GetInstance()->AddCacheEntry(name, value, doc, type);
  520. this->AddDefinition(name, value);
  521. }
  522. void cmMakefile::AddDefinition(const char* name, bool value)
  523. {
  524. if(value)
  525. {
  526. m_Definitions.erase( DefinitionMap::key_type(name));
  527. m_Definitions.insert(DefinitionMap::value_type(name, "ON"));
  528. }
  529. else
  530. {
  531. m_Definitions.erase( DefinitionMap::key_type(name));
  532. m_Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  533. }
  534. }
  535. void cmMakefile::AddCacheDefinition(const char* name, bool value, const char* doc)
  536. {
  537. cmCacheManager::GetInstance()->AddCacheEntry(name, value, doc);
  538. this->AddDefinition(name, value);
  539. }
  540. void cmMakefile::SetProjectName(const char* p)
  541. {
  542. m_ProjectName = p;
  543. }
  544. void cmMakefile::AddLibrary(const char* lname, int shared,
  545. const std::vector<std::string> &srcs)
  546. {
  547. cmTarget target;
  548. switch (shared)
  549. {
  550. case 0:
  551. target.SetType(cmTarget::STATIC_LIBRARY);
  552. break;
  553. case 1:
  554. target.SetType(cmTarget::SHARED_LIBRARY);
  555. break;
  556. case 2:
  557. target.SetType(cmTarget::MODULE_LIBRARY);
  558. break;
  559. default:
  560. target.SetType(cmTarget::STATIC_LIBRARY);
  561. }
  562. target.SetInAll(true);
  563. target.GetSourceLists() = srcs;
  564. m_Targets.insert(cmTargets::value_type(lname,target));
  565. // Add an entry into the cache
  566. cmCacheManager::GetInstance()->
  567. AddCacheEntry(lname,
  568. this->GetCurrentOutputDirectory(),
  569. "Path to a library", cmCacheManager::INTERNAL);
  570. // Add an entry into the cache
  571. std::string ltname = lname;
  572. ltname += "_LIBRARY_TYPE";
  573. switch (shared)
  574. {
  575. case 0:
  576. cmCacheManager::GetInstance()->
  577. AddCacheEntry(ltname.c_str(),
  578. "STATIC",
  579. "Whether a library is static, shared or module.",
  580. cmCacheManager::INTERNAL);
  581. break;
  582. case 1:
  583. cmCacheManager::GetInstance()->
  584. AddCacheEntry(ltname.c_str(),
  585. "SHARED",
  586. "Whether a library is static, shared or module.",
  587. cmCacheManager::INTERNAL);
  588. break;
  589. case 2:
  590. cmCacheManager::GetInstance()->
  591. AddCacheEntry(ltname.c_str(),
  592. "MODULE",
  593. "Whether a library is static, shared or module.",
  594. cmCacheManager::INTERNAL);
  595. break;
  596. default:
  597. cmCacheManager::GetInstance()->
  598. AddCacheEntry(ltname.c_str(),
  599. "STATIC",
  600. "Whether a library is static, shared or module.",
  601. cmCacheManager::INTERNAL);
  602. }
  603. }
  604. void cmMakefile::AddExecutable(const char *exeName,
  605. const std::vector<std::string> &srcs)
  606. {
  607. this->AddExecutable(exeName,srcs,false);
  608. }
  609. void cmMakefile::AddExecutable(const char *exeName,
  610. const std::vector<std::string> &srcs,
  611. bool win32)
  612. {
  613. cmTarget target;
  614. if (win32)
  615. {
  616. target.SetType(cmTarget::WIN32_EXECUTABLE);
  617. }
  618. else
  619. {
  620. target.SetType(cmTarget::EXECUTABLE);
  621. }
  622. target.SetInAll(true);
  623. target.GetSourceLists() = srcs;
  624. m_Targets.insert(cmTargets::value_type(exeName,target));
  625. // Add an entry into the cache
  626. cmCacheManager::GetInstance()->
  627. AddCacheEntry(exeName,
  628. this->GetCurrentOutputDirectory(),
  629. "Path to an executable", cmCacheManager::INTERNAL);
  630. }
  631. void cmMakefile::AddUtilityCommand(const char* utilityName,
  632. const char* command,
  633. const char* arguments,
  634. bool all)
  635. {
  636. std::vector<std::string> empty;
  637. this->AddUtilityCommand(utilityName,command,arguments,all,
  638. empty,empty);
  639. }
  640. void cmMakefile::AddUtilityCommand(const char* utilityName,
  641. const char* command,
  642. const char* arguments,
  643. bool all,
  644. const std::vector<std::string> &dep,
  645. const std::vector<std::string> &out)
  646. {
  647. cmTarget target;
  648. target.SetType(cmTarget::UTILITY);
  649. target.SetInAll(all);
  650. cmCustomCommand cc(utilityName, command, arguments, dep, out);
  651. target.GetCustomCommands().push_back(cc);
  652. m_Targets.insert(cmTargets::value_type(utilityName,target));
  653. }
  654. void cmMakefile::AddSourceGroup(const char* name, const char* regex)
  655. {
  656. // First see if the group exists. If so, replace its regular expression.
  657. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  658. sg != m_SourceGroups.end(); ++sg)
  659. {
  660. std::string sgName = sg->GetName();
  661. if(sgName == name)
  662. {
  663. // We only want to set the regular expression. If there are already
  664. // source files in the group, we don't want to remove them.
  665. sg->SetGroupRegex(regex);
  666. return;
  667. }
  668. }
  669. // The group doesn't exist. Add it.
  670. m_SourceGroups.push_back(cmSourceGroup(name, regex));
  671. }
  672. void cmMakefile::AddExtraDirectory(const char* dir)
  673. {
  674. m_AuxSourceDirectories.push_back(dir);
  675. }
  676. // return the file name for the parent CMakeLists file to the
  677. // one passed in. Zero is returned if the CMakeLists file is the
  678. // one in the home directory or if for some reason a parent cmake lists
  679. // file cannot be found.
  680. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  681. {
  682. // extract the directory name
  683. std::string parentFile;
  684. std::string listsDir = currentFileName;
  685. std::string::size_type pos = listsDir.rfind('/');
  686. // if we could not find the directory return 0
  687. if(pos == std::string::npos)
  688. {
  689. return parentFile;
  690. }
  691. listsDir = listsDir.substr(0, pos);
  692. // if we are in the home directory then stop, return 0
  693. if(m_cmHomeDirectory == listsDir)
  694. {
  695. return parentFile;
  696. }
  697. // is there a parent directory we can check
  698. pos = listsDir.rfind('/');
  699. // if we could not find the directory return 0
  700. if(pos == std::string::npos)
  701. {
  702. return parentFile;
  703. }
  704. listsDir = listsDir.substr(0, pos);
  705. // is there a CMakeLists.txt file in the parent directory ?
  706. parentFile = listsDir;
  707. parentFile += "/CMakeLists.txt";
  708. while(!cmSystemTools::FileExists(parentFile.c_str()))
  709. {
  710. // There is no CMakeLists.txt file in the parent directory. This
  711. // can occur when coming out of a subdirectory resulting from a
  712. // SUBDIRS(Foo/Bar) command (coming out of Bar into Foo). Try
  713. // walking up until a CMakeLists.txt is found or the home
  714. // directory is hit.
  715. // if we are in the home directory then stop, return 0
  716. if(m_cmHomeDirectory == listsDir) { return ""; }
  717. // is there a parent directory we can check
  718. pos = listsDir.rfind('/');
  719. // if we could not find the directory return 0
  720. if(pos == std::string::npos) { return ""; }
  721. listsDir = listsDir.substr(0, pos);
  722. parentFile = listsDir;
  723. parentFile += "/CMakeLists.txt";
  724. }
  725. return parentFile;
  726. }
  727. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  728. // include and library directories.
  729. void cmMakefile::ExpandVariables()
  730. {
  731. // Now expand varibles in the include and link strings
  732. for(std::vector<std::string>::iterator d = m_IncludeDirectories.begin();
  733. d != m_IncludeDirectories.end(); ++d)
  734. {
  735. this->ExpandVariablesInString(*d);
  736. }
  737. for(std::vector<std::string>::iterator d = m_LinkDirectories.begin();
  738. d != m_LinkDirectories.end(); ++d)
  739. {
  740. this->ExpandVariablesInString(*d);
  741. }
  742. for(cmTarget::LinkLibraries::iterator l = m_LinkLibraries.begin();
  743. l != m_LinkLibraries.end(); ++l)
  744. {
  745. this->ExpandVariablesInString(l->first);
  746. }
  747. }
  748. bool cmMakefile::IsOn(const char* name)
  749. {
  750. const char* value = this->GetDefinition(name);
  751. return cmSystemTools::IsOn(value);
  752. }
  753. const char* cmMakefile::GetDefinition(const char* name) const
  754. {
  755. DefinitionMap::const_iterator pos = m_Definitions.find(name);
  756. if(pos != m_Definitions.end())
  757. {
  758. return (*pos).second.c_str();
  759. }
  760. return cmCacheManager::GetInstance()->GetCacheValue(name);
  761. }
  762. int cmMakefile::DumpDocumentationToFile(std::ostream& f)
  763. {
  764. // Open the supplied filename
  765. // Loop over all registered commands and print out documentation
  766. const char *name;
  767. const char *terse;
  768. const char *full;
  769. char tmp[1024];
  770. sprintf(tmp,"Version %d.%d", cmMakefile::GetMajorVersion(),
  771. cmMakefile::GetMinorVersion());
  772. f << "<html>\n";
  773. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  774. f << "<ul>\n";
  775. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  776. j != m_Commands.end(); ++j)
  777. {
  778. name = (*j).second->GetName();
  779. terse = (*j).second->GetTerseDocumentation();
  780. full = (*j).second->GetFullDocumentation();
  781. f << "<li><b>" << name << "</b> - " << terse << std::endl
  782. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  783. }
  784. f << "</ul></html>\n";
  785. return 1;
  786. }
  787. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  788. {
  789. return this->ExpandVariablesInString(source, false);
  790. }
  791. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  792. bool escapeQuotes,
  793. bool atOnly) const
  794. {
  795. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  796. // with GetDefinition(), if not found in the map, nothing is expanded.
  797. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  798. // the current environment variables.
  799. // start by look for $ or @ in the string
  800. std::string::size_type markerPos;
  801. if(atOnly)
  802. {
  803. markerPos = source.find_first_of("@");
  804. }
  805. else
  806. {
  807. markerPos = source.find_first_of("$@");
  808. }
  809. // if not found, or found as the last character, then leave quickly as
  810. // nothing needs to be expanded
  811. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  812. {
  813. return source.c_str();
  814. }
  815. // current position
  816. std::string::size_type currentPos =0; // start at 0
  817. std::string result; // string with replacements
  818. // go until the the end of the string
  819. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  820. {
  821. // grab string from currentPos to the start of the variable
  822. // and add it to the result
  823. result += source.substr(currentPos, markerPos - currentPos);
  824. char endVariableMarker; // what is the end of the variable @ or }
  825. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  826. if(!atOnly && source[markerPos] == '$')
  827. {
  828. // ${var} case
  829. if(source[markerPos+1] == '{')
  830. {
  831. endVariableMarker = '}';
  832. markerStartSize = 2;
  833. }
  834. // $ENV{var} case
  835. else if(markerPos+4 < source.size() &&
  836. source[markerPos+4] == '{' &&
  837. !source.substr(markerPos+1, 3).compare("ENV"))
  838. {
  839. endVariableMarker = '}';
  840. markerStartSize = 5;
  841. }
  842. else
  843. {
  844. // bogus $ with no { so add $ to result and move on
  845. result += '$'; // add bogus $ back into string
  846. currentPos = markerPos+1; // move on
  847. endVariableMarker = ' '; // set end var to space so we can tell bogus
  848. }
  849. }
  850. else
  851. {
  852. // @VAR case
  853. endVariableMarker = '@';
  854. }
  855. // if it was a valid variable (started with @ or ${ or $ENV{ )
  856. if(endVariableMarker != ' ')
  857. {
  858. markerPos += markerStartSize; // move past marker
  859. // find the end variable marker starting at the markerPos
  860. std::string::size_type endVariablePos =
  861. source.find(endVariableMarker, markerPos);
  862. if(endVariablePos == std::string::npos)
  863. {
  864. // no end marker found so add the bogus start
  865. if(endVariableMarker == '@')
  866. {
  867. result += '@';
  868. }
  869. else
  870. {
  871. result += (markerStartSize == 5 ? "$ENV{" : "${");
  872. }
  873. currentPos = markerPos;
  874. }
  875. else
  876. {
  877. // good variable remove it
  878. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  879. bool found = false;
  880. if (markerStartSize == 5) // $ENV{
  881. {
  882. char *ptr = getenv(var.c_str());
  883. if (ptr)
  884. {
  885. if (escapeQuotes)
  886. {
  887. result += cmSystemTools::EscapeQuotes(ptr);
  888. }
  889. else
  890. {
  891. result += ptr;
  892. }
  893. found = true;
  894. }
  895. }
  896. else
  897. {
  898. const char* lookup = this->GetDefinition(var.c_str());
  899. if(lookup)
  900. {
  901. if (escapeQuotes)
  902. {
  903. result += cmSystemTools::EscapeQuotes(lookup);
  904. }
  905. else
  906. {
  907. result += lookup;
  908. }
  909. found = true;
  910. }
  911. }
  912. // if found add to result, if not, then it gets blanked
  913. if (!found)
  914. {
  915. // if no definition is found then add the var back
  916. if(endVariableMarker == '@')
  917. {
  918. result += "@";
  919. result += var;
  920. result += "@";
  921. }
  922. else
  923. {
  924. result += (markerStartSize == 5 ? "$ENV{" : "${");
  925. result += var;
  926. result += "}";
  927. }
  928. }
  929. // lookup var, and replace it
  930. currentPos = endVariablePos+1;
  931. }
  932. }
  933. if(atOnly)
  934. {
  935. markerPos = source.find_first_of("@", currentPos);
  936. }
  937. else
  938. {
  939. markerPos = source.find_first_of("$@", currentPos);
  940. }
  941. }
  942. result += source.substr(currentPos); // pick up the rest of the string
  943. source = result;
  944. return source.c_str();
  945. }
  946. void cmMakefile::RemoveVariablesInString(std::string& source,
  947. bool atOnly) const
  948. {
  949. if(!atOnly)
  950. {
  951. cmRegularExpression var("(\\${[A-Za-z_0-9]*})");
  952. while (var.find(source))
  953. {
  954. source.erase(var.start(),var.end() - var.start());
  955. }
  956. }
  957. if(!atOnly)
  958. {
  959. cmRegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  960. while (varb.find(source))
  961. {
  962. source.erase(varb.start(),varb.end() - varb.start());
  963. }
  964. }
  965. cmRegularExpression var2("(@[A-Za-z_0-9]*@)");
  966. while (var2.find(source))
  967. {
  968. source.erase(var2.start(),var2.end() - var2.start());
  969. }
  970. }
  971. // recursive function to create a vector of cmMakefile objects
  972. // This is done by reading the sub directory CMakeLists.txt files,
  973. // then calling this function with the new cmMakefile object
  974. void
  975. cmMakefile::FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>&
  976. makefiles)
  977. {
  978. // loop over all the sub directories of this makefile
  979. const std::vector<std::string>& subdirs = this->GetSubDirectories();
  980. for(std::vector<std::string>::const_iterator i = subdirs.begin();
  981. i != subdirs.end(); ++i)
  982. {
  983. std::string subdir = *i;
  984. // Create a path to the list file in the sub directory
  985. std::string listFile = this->GetCurrentDirectory();
  986. listFile += "/";
  987. listFile += subdir;
  988. listFile += "/CMakeLists.txt";
  989. // if there is a CMakeLists.txt file read it
  990. if(!cmSystemTools::FileExists(listFile.c_str()))
  991. {
  992. cmSystemTools::Error("CMakeLists.txt file missing from sub directory:",
  993. listFile.c_str());
  994. }
  995. else
  996. {
  997. cmMakefile* mf = new cmMakefile;
  998. makefiles.push_back(mf);
  999. // initialize new makefile
  1000. mf->SetHomeOutputDirectory(this->GetHomeOutputDirectory());
  1001. mf->SetHomeDirectory(this->GetHomeDirectory());
  1002. // add the subdir to the start output directory
  1003. std::string outdir = this->GetStartOutputDirectory();
  1004. outdir += "/";
  1005. outdir += subdir;
  1006. mf->SetStartOutputDirectory(outdir.c_str());
  1007. // add the subdir to the start source directory
  1008. std::string currentDir = this->GetStartDirectory();
  1009. currentDir += "/";
  1010. currentDir += subdir;
  1011. mf->SetStartDirectory(currentDir.c_str());
  1012. // Parse the CMakeLists.txt file
  1013. currentDir += "/CMakeLists.txt";
  1014. mf->MakeStartDirectoriesCurrent();
  1015. mf->ReadListFile(currentDir.c_str());
  1016. // recurse into nextDir
  1017. mf->FindSubDirectoryCMakeListsFiles(makefiles);
  1018. }
  1019. }
  1020. }
  1021. /**
  1022. * Add the default definitions to the makefile. These values must not
  1023. * be dependent on anything that isn't known when this cmMakefile instance
  1024. * is constructed.
  1025. */
  1026. void cmMakefile::AddDefaultDefinitions()
  1027. {
  1028. #if defined(_WIN32) && !defined(__CYGWIN__)
  1029. this->AddDefinition("CMAKE_CFG_INTDIR","$(IntDir)");
  1030. #else
  1031. this->AddDefinition("CMAKE_CFG_INTDIR",".");
  1032. #endif
  1033. char temp[1024];
  1034. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1035. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1036. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1037. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1038. }
  1039. /**
  1040. * Find a source group whose regular expression matches the filename
  1041. * part of the given source name. Search backward through the list of
  1042. * source groups, and take the first matching group found. This way
  1043. * non-inherited SOURCE_GROUP commands will have precedence over
  1044. * inherited ones.
  1045. */
  1046. cmSourceGroup&
  1047. cmMakefile::FindSourceGroup(const char* source,
  1048. std::vector<cmSourceGroup> &groups)
  1049. {
  1050. std::string file = source;
  1051. std::string::size_type pos = file.rfind('/');
  1052. if(pos != std::string::npos)
  1053. {
  1054. file = file.substr(pos, file.length()-pos);
  1055. }
  1056. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1057. sg != groups.rend(); ++sg)
  1058. {
  1059. if(sg->Matches(file.c_str()))
  1060. {
  1061. return *sg;
  1062. }
  1063. }
  1064. // Shouldn't get here, but just in case, return the default group.
  1065. return groups.front();
  1066. }
  1067. bool cmMakefile::IsFunctionBlocked(const char *name,
  1068. std::vector<std::string> const&args)
  1069. {
  1070. // loop over all function blockers to see if any block this command
  1071. std::set<cmFunctionBlocker *>::const_iterator pos;
  1072. std::vector<std::string> expandedArguments = args;
  1073. for(std::vector<std::string>::iterator i = expandedArguments.begin();
  1074. i != expandedArguments.end(); ++i)
  1075. {
  1076. this->ExpandVariablesInString(*i);
  1077. }
  1078. for (pos = m_FunctionBlockers.begin();
  1079. pos != m_FunctionBlockers.end(); ++pos)
  1080. {
  1081. if ((*pos)->IsFunctionBlocked(name, expandedArguments, *this))
  1082. {
  1083. return true;
  1084. }
  1085. }
  1086. return false;
  1087. }
  1088. void cmMakefile::RemoveFunctionBlocker(const char *name,
  1089. const std::vector<std::string> &args)
  1090. {
  1091. // loop over all function blockers to see if any block this command
  1092. std::set<cmFunctionBlocker *>::const_iterator pos;
  1093. for (pos = m_FunctionBlockers.begin();
  1094. pos != m_FunctionBlockers.end(); ++pos)
  1095. {
  1096. if ((*pos)->ShouldRemove(name, args, *this))
  1097. {
  1098. cmFunctionBlocker* b = *pos;
  1099. m_FunctionBlockers.erase(*pos);
  1100. delete b;
  1101. return;
  1102. }
  1103. }
  1104. return;
  1105. }
  1106. void cmMakefile::SetHomeDirectory(const char* dir)
  1107. {
  1108. m_cmHomeDirectory = dir;
  1109. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1110. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1111. }
  1112. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1113. {
  1114. m_HomeOutputDirectory = lib;
  1115. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1116. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1117. }
  1118. /**
  1119. * Register the given cmData instance with its own name.
  1120. */
  1121. void cmMakefile::RegisterData(cmData* data)
  1122. {
  1123. std::string name = data->GetName();
  1124. DataMap::const_iterator d = m_DataMap.find(name);
  1125. if((d != m_DataMap.end()) && (d->second != NULL) && (d->second != data))
  1126. {
  1127. delete d->second;
  1128. }
  1129. m_DataMap[name] = data;
  1130. }
  1131. /**
  1132. * Register the given cmData instance with the given name. This can be used
  1133. * to register a NULL pointer.
  1134. */
  1135. void cmMakefile::RegisterData(const char* name, cmData* data)
  1136. {
  1137. DataMap::const_iterator d = m_DataMap.find(name);
  1138. if((d != m_DataMap.end()) && (d->second != NULL) && (d->second != data))
  1139. {
  1140. delete d->second;
  1141. }
  1142. m_DataMap[name] = data;
  1143. }
  1144. /**
  1145. * Lookup a cmData instance previously registered with the given name. If
  1146. * the instance cannot be found, return NULL.
  1147. */
  1148. cmData* cmMakefile::LookupData(const char* name) const
  1149. {
  1150. DataMap::const_iterator d = m_DataMap.find(name);
  1151. if(d != m_DataMap.end())
  1152. {
  1153. return d->second;
  1154. }
  1155. else
  1156. {
  1157. return NULL;
  1158. }
  1159. }