cmMakefile.cxx 37 KB

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