cmMakefile.cxx 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmMakefile.h"
  14. #include "cmCommand.h"
  15. #include "cmStandardIncludes.h"
  16. #include "cmSourceFile.h"
  17. #include "cmDirectory.h"
  18. #include "cmSystemTools.h"
  19. #include "cmGlobalGenerator.h"
  20. #include "cmLocalGenerator.h"
  21. #include "cmCommands.h"
  22. #include "cmCacheManager.h"
  23. #include "cmFunctionBlocker.h"
  24. #include "cmListFileCache.h"
  25. #include "cmVariableWatch.h"
  26. #include "cmake.h"
  27. #include <stdio.h> // required for sprintf
  28. #include <stdlib.h> // required for atoi
  29. // default is not to be building executables
  30. cmMakefile::cmMakefile()
  31. {
  32. // Setup the default include file regular expression (match everything).
  33. m_IncludeFileRegularExpression = "^.*$";
  34. // Setup the default include complaint regular expression (match nothing).
  35. m_ComplainFileRegularExpression = "^$";
  36. // Source and header file extensions that we can handle
  37. // The "c" extension MUST precede the "C" extension.
  38. m_SourceFileExtensions.push_back( "c" );
  39. m_SourceFileExtensions.push_back( "C" );
  40. m_SourceFileExtensions.push_back( "c++" );
  41. m_SourceFileExtensions.push_back( "cc" );
  42. m_SourceFileExtensions.push_back( "cpp" );
  43. m_SourceFileExtensions.push_back( "cxx" );
  44. m_SourceFileExtensions.push_back( "m" );
  45. m_SourceFileExtensions.push_back( "M" );
  46. m_SourceFileExtensions.push_back( "mm" );
  47. m_HeaderFileExtensions.push_back( "h" );
  48. m_HeaderFileExtensions.push_back( "h++" );
  49. m_HeaderFileExtensions.push_back( "hm" );
  50. m_HeaderFileExtensions.push_back( "hpp" );
  51. m_HeaderFileExtensions.push_back( "hxx" );
  52. m_HeaderFileExtensions.push_back( "in" );
  53. m_HeaderFileExtensions.push_back( "txx" );
  54. m_DefineFlags = " ";
  55. m_LocalGenerator = 0;
  56. this->AddSourceGroup("", "^.*$");
  57. this->AddSourceGroup("Source Files",
  58. "\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|rc|def|r|odl|idl|hpj|bat)$");
  59. this->AddSourceGroup("Header Files", "\\.(h|h\\+\\+|hm|hpp|hxx|in|txx|inl)$");
  60. this->AddSourceGroup("CMake Rules", "\\.rule$");
  61. this->AddDefaultDefinitions();
  62. }
  63. const char* cmMakefile::GetReleaseVersion()
  64. {
  65. #if CMake_VERSION_MINOR & 1
  66. return "development";
  67. #else
  68. return "patch " CMAKE_TO_STRING(CMake_VERSION_PATCH);
  69. #endif
  70. }
  71. unsigned int cmMakefile::GetCacheMajorVersion()
  72. {
  73. if(!this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
  74. {
  75. return 0;
  76. }
  77. return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"));
  78. }
  79. unsigned int cmMakefile::GetCacheMinorVersion()
  80. {
  81. if(!this->GetCacheManager()->GetCacheValue("Cmake_Cache_MINOR_VERSION"))
  82. {
  83. return 0;
  84. }
  85. return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"));
  86. }
  87. cmMakefile::~cmMakefile()
  88. {
  89. for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
  90. i != m_SourceFiles.end(); ++i)
  91. {
  92. delete *i;
  93. }
  94. for(unsigned int i=0; i < m_UsedCommands.size(); i++)
  95. {
  96. delete m_UsedCommands[i];
  97. }
  98. for(DataMap::const_iterator d = m_DataMap.begin();
  99. d != m_DataMap.end(); ++d)
  100. {
  101. if(d->second)
  102. {
  103. delete d->second;
  104. }
  105. }
  106. std::list<cmFunctionBlocker *>::iterator pos;
  107. for (pos = m_FunctionBlockers.begin();
  108. pos != m_FunctionBlockers.end(); ++pos)
  109. {
  110. cmFunctionBlocker* b = *pos;
  111. delete b;
  112. }
  113. m_FunctionBlockers.clear();
  114. }
  115. void cmMakefile::PrintStringVector(const char* s, const std::vector<std::string>& v) const
  116. {
  117. std::cout << s << ": ( \n";
  118. for(std::vector<std::string>::const_iterator i = v.begin();
  119. i != v.end(); ++i)
  120. {
  121. std::cout << (*i).c_str() << " ";
  122. }
  123. std::cout << " )\n";
  124. }
  125. // call print on all the classes in the makefile
  126. void cmMakefile::Print() const
  127. {
  128. // print the class lists
  129. std::cout << "classes:\n";
  130. std::cout << " m_Targets: ";
  131. for (cmTargets::const_iterator l = m_Targets.begin();
  132. l != m_Targets.end(); l++)
  133. {
  134. std::cout << l->first << std::endl;
  135. }
  136. std::cout << " m_CurrentOutputDirectory; " <<
  137. m_CurrentOutputDirectory.c_str() << std::endl;
  138. std::cout << " m_StartOutputDirectory; " <<
  139. m_StartOutputDirectory.c_str() << std::endl;
  140. std::cout << " m_HomeOutputDirectory; " <<
  141. m_HomeOutputDirectory.c_str() << std::endl;
  142. std::cout << " m_cmCurrentDirectory; " <<
  143. m_cmCurrentDirectory.c_str() << std::endl;
  144. std::cout << " m_cmStartDirectory; " <<
  145. m_cmStartDirectory.c_str() << std::endl;
  146. std::cout << " m_cmHomeDirectory; " <<
  147. m_cmHomeDirectory.c_str() << std::endl;
  148. std::cout << " m_ProjectName; " << m_ProjectName.c_str() << std::endl;
  149. this->PrintStringVector("m_SubDirectories ", m_SubDirectories);
  150. this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
  151. this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
  152. for( std::vector<cmSourceGroup>::const_iterator i = m_SourceGroups.begin();
  153. i != m_SourceGroups.end(); ++i)
  154. {
  155. std::cout << "Source Group: " << i->GetName() << std::endl;
  156. }
  157. }
  158. bool cmMakefile::CommandExists(const char* name) const
  159. {
  160. return this->GetCMakeInstance()->CommandExists(name);
  161. }
  162. bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff)
  163. {
  164. bool result = true;
  165. // quick return if blocked
  166. if(this->IsFunctionBlocked(lff))
  167. {
  168. // No error.
  169. return result;
  170. }
  171. std::string name = lff.m_Name;
  172. // execute the command
  173. cmCommand *rm =
  174. this->GetCMakeInstance()->GetCommand(name.c_str());
  175. if(rm)
  176. {
  177. cmCommand* usedCommand = rm->Clone();
  178. usedCommand->SetMakefile(this);
  179. bool keepCommand = false;
  180. if(usedCommand->GetEnabled() && !cmSystemTools::GetFatalErrorOccured())
  181. {
  182. // if not running in inherit mode or
  183. // if the command is inherited then InitialPass it.
  184. if(!m_Inheriting || usedCommand->IsInherited())
  185. {
  186. if(!usedCommand->InvokeInitialPass(lff.m_Arguments))
  187. {
  188. cmOStringStream error;
  189. error << "Error in cmake code at\n"
  190. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  191. << usedCommand->GetError();
  192. cmSystemTools::Error(error.str().c_str());
  193. result = false;
  194. }
  195. else
  196. {
  197. // use the command
  198. keepCommand = true;
  199. m_UsedCommands.push_back(usedCommand);
  200. }
  201. }
  202. }
  203. // if the Cloned command was not used
  204. // then delete it
  205. if(!keepCommand)
  206. {
  207. delete usedCommand;
  208. }
  209. }
  210. else
  211. {
  212. if(!cmSystemTools::GetFatalErrorOccured())
  213. {
  214. cmOStringStream error;
  215. error << "Error in cmake code at\n"
  216. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  217. << "Unknown CMake command \"" << lff.m_Name.c_str() << "\".";
  218. cmSystemTools::Error(error.str().c_str());
  219. result = false;
  220. }
  221. }
  222. return result;
  223. }
  224. // Parse the given CMakeLists.txt file into a list of classes.
  225. // Reads in current CMakeLists file and all parent CMakeLists files
  226. // executing all inherited commands in the parents
  227. //
  228. // if external is non-zero, this means that we have branched to grab some
  229. // commands from a remote list-file (that is, the equivalent of a
  230. // #include has been called). We DO NOT look at the parents of this
  231. // list-file, and for all other purposes, the name of this list-file
  232. // is "filename" and not "external".
  233. bool cmMakefile::ReadListFile(const char* filename_in, const char* external_in)
  234. {
  235. // used to watch for blockers going out of scope
  236. // e.g. mismatched IF statement
  237. std::set<cmFunctionBlocker *> originalBlockers;
  238. const char* external = 0;
  239. std::string external_abs;
  240. const char* filename = filename_in;
  241. std::string filename_abs;
  242. if (external_in)
  243. {
  244. external_abs =
  245. cmSystemTools::CollapseFullPath(external_in,
  246. m_cmCurrentDirectory.c_str());
  247. external = external_abs.c_str();
  248. if (filename_in)
  249. {
  250. filename_abs =
  251. cmSystemTools::CollapseFullPath(filename_in,
  252. m_cmCurrentDirectory.c_str());
  253. filename = filename_abs.c_str();
  254. }
  255. }
  256. // keep track of the current file being read
  257. if (filename)
  258. {
  259. if(m_cmCurrentListFile != filename)
  260. {
  261. m_cmCurrentListFile = filename;
  262. }
  263. // loop over current function blockers and record them
  264. std::list<cmFunctionBlocker *>::iterator pos;
  265. for (pos = m_FunctionBlockers.begin();
  266. pos != m_FunctionBlockers.end(); ++pos)
  267. {
  268. originalBlockers.insert(*pos);
  269. }
  270. }
  271. // if this is not a remote makefile
  272. // (if it were, this would be called from the "filename" call,
  273. // rather than the "external" call)
  274. if (!external)
  275. {
  276. // is there a parent CMakeLists file that does not go beyond the
  277. // Home directory? if so recurse and read in that List file
  278. std::string parentList = this->GetParentListFileName(filename);
  279. if (parentList != "")
  280. {
  281. std::string srcdir = m_cmCurrentDirectory;
  282. std::string bindir = m_CurrentOutputDirectory;
  283. std::string::size_type pos = parentList.rfind('/');
  284. m_cmCurrentDirectory = parentList.substr(0, pos);
  285. m_CurrentOutputDirectory = m_HomeOutputDirectory + parentList.substr(m_cmHomeDirectory.size(), pos - m_cmHomeDirectory.size());
  286. // if not found, oops
  287. if(pos == std::string::npos)
  288. {
  289. cmSystemTools::Error("Trailing slash not found");
  290. }
  291. this->ReadListFile(parentList.c_str());
  292. // restore the current directory
  293. m_cmCurrentDirectory = srcdir;
  294. m_CurrentOutputDirectory = bindir;
  295. }
  296. }
  297. // are we at the start CMakeLists file or are we processing a parent
  298. // lists file
  299. //
  300. // this might, or might not be true, irrespective if we are
  301. // off looking at an external makefile.
  302. m_Inheriting = (m_cmCurrentDirectory != m_cmStartDirectory);
  303. // Now read the input file
  304. const char *filenametoread= filename;
  305. if( external)
  306. {
  307. filenametoread= external;
  308. }
  309. // try to see if the list file is the top most
  310. // list file for a project, and if it is, then it
  311. // must have a project command. If there is not
  312. // one, then cmake will provide one via the
  313. // cmListFileCache class.
  314. bool requireProjectCommand = false;
  315. if(!external && m_cmCurrentDirectory == m_cmHomeDirectory)
  316. {
  317. if(cmSystemTools::LowerCase(
  318. cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
  319. {
  320. requireProjectCommand = true;
  321. }
  322. }
  323. cmListFile* lf =
  324. cmListFileCache::GetInstance()->GetFileCache(filenametoread,
  325. requireProjectCommand);
  326. if(!lf)
  327. {
  328. return false;
  329. }
  330. // add this list file to the list of dependencies
  331. m_ListFiles.push_back( filenametoread);
  332. const size_t numberFunctions = lf->m_Functions.size();
  333. for(size_t i =0; i < numberFunctions; ++i)
  334. {
  335. this->ExecuteCommand(lf->m_Functions[i]);
  336. }
  337. // send scope ended to and funciton blockers
  338. if (filename)
  339. {
  340. // loop over all function blockers to see if any block this command
  341. std::list<cmFunctionBlocker *>::iterator pos;
  342. for (pos = m_FunctionBlockers.begin();
  343. pos != m_FunctionBlockers.end(); ++pos)
  344. {
  345. // if this blocker was not in the original then send a
  346. // scope ended message
  347. if (originalBlockers.find(*pos) == originalBlockers.end())
  348. {
  349. (*pos)->ScopeEnded(*this);
  350. }
  351. }
  352. }
  353. return true;
  354. }
  355. void cmMakefile::AddCommand(cmCommand* wg)
  356. {
  357. this->GetCMakeInstance()->AddCommand(wg);
  358. }
  359. // Set the make file
  360. void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
  361. {
  362. m_LocalGenerator = lg;
  363. }
  364. void cmMakefile::FinalPass()
  365. {
  366. // do all the variable expansions here
  367. this->ExpandVariables();
  368. // give all the commands a chance to do something
  369. // after the file has been parsed before generation
  370. for(std::vector<cmCommand*>::iterator i = m_UsedCommands.begin();
  371. i != m_UsedCommands.end(); ++i)
  372. {
  373. (*i)->FinalPass();
  374. }
  375. }
  376. // Generate the output file
  377. void cmMakefile::ConfigureFinalPass()
  378. {
  379. this->FinalPass();
  380. const char* versionValue
  381. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  382. bool oldVersion = (!versionValue || atof(versionValue) < 1.4);
  383. // merge libraries
  384. for (cmTargets::iterator l = m_Targets.begin();
  385. l != m_Targets.end(); l++)
  386. {
  387. l->second.GenerateSourceFilesFromSourceLists(*this);
  388. // pick up any LINK_LIBRARIES that were added after the target
  389. if(oldVersion)
  390. {
  391. this->AddGlobalLinkInformation(l->first.c_str(), l->second);
  392. }
  393. l->second.AnalyzeLibDependencies(*this);
  394. }
  395. }
  396. // this is the old style signature, we convert to new style
  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. const char *comment)
  404. {
  405. if (strcmp(source,target))
  406. {
  407. // what a pain, for backwards compatibility we will try to
  408. // convert this to an output based rule... so for each output..
  409. for(std::vector<std::string>::const_iterator d = outputs.begin();
  410. d != outputs.end(); ++d)
  411. {
  412. this->AddCustomCommandToOutput(d->c_str(), command, commandArgs,
  413. source, depends, comment);
  414. // add the output to the target?
  415. std::string sname = *d;
  416. sname += ".rule";
  417. if (!this->GetSource(sname.c_str()))
  418. {
  419. m_Targets[target].GetSourceLists().push_back(source);
  420. }
  421. }
  422. }
  423. else
  424. {
  425. this->AddCustomCommandToTarget(target, command, commandArgs,
  426. cmTarget::POST_BUILD,
  427. comment);
  428. }
  429. }
  430. void cmMakefile::AddCustomCommand(const char* source,
  431. const char* command,
  432. const std::vector<std::string>& commandArgs,
  433. const std::vector<std::string>& depends,
  434. const char* output,
  435. const char *target)
  436. {
  437. std::vector<std::string> outputs;
  438. outputs.push_back(output);
  439. this->AddCustomCommand(source, command, commandArgs, depends,
  440. outputs, target);
  441. }
  442. void cmMakefile::
  443. AddCustomCommandToOutput(const char* output,
  444. const char* command,
  445. const std::vector<std::string>& commandArgs,
  446. const char *main_dependency,
  447. const std::vector<std::string>& depends,
  448. const char *comment,
  449. bool replace)
  450. {
  451. std::string expandC;
  452. std::string combinedArgs;
  453. unsigned int i;
  454. for (i = 0; i < commandArgs.size(); ++i)
  455. {
  456. expandC = commandArgs[i].c_str();
  457. this->ExpandVariablesInString(expandC);
  458. combinedArgs += cmSystemTools::EscapeSpaces(expandC.c_str());
  459. combinedArgs += " ";
  460. }
  461. cmSourceFile *file = 0;
  462. std::string outName = output;
  463. outName += ".rule";
  464. // OK this rule will be placed on a generated output file unless the main
  465. // depednency was specified.
  466. if (main_dependency && main_dependency[0] != '\0')
  467. {
  468. file = this->GetSource(main_dependency);
  469. if (file && file->GetCustomCommand() && !replace)
  470. {
  471. cmCustomCommand* cc = file->GetCustomCommand();
  472. // if the command and args are the same
  473. // as the command already there, then silently skip
  474. // this add command
  475. if(cc->IsEquivalent(command, combinedArgs.c_str()))
  476. {
  477. return;
  478. }
  479. // generate a source instead
  480. file = 0;
  481. }
  482. else
  483. {
  484. file = this->GetOrCreateSource(main_dependency);
  485. }
  486. }
  487. if (!file)
  488. {
  489. file = this->GetSource(outName.c_str());
  490. if (file && file->GetCustomCommand() && !replace)
  491. {
  492. cmCustomCommand* cc = file->GetCustomCommand();
  493. // if the command and args are the same
  494. // as the command already there, then silently skip
  495. // this add command
  496. if(cc->IsEquivalent(command, combinedArgs.c_str()))
  497. {
  498. return;
  499. }
  500. // produce error if two different commands are given to produce
  501. // the same output
  502. cmSystemTools::Error("Attempt to add a custom rule to an output that already"
  503. " has a custom rule. For output: ", output);
  504. return;
  505. }
  506. // create a cmSourceFile for the output
  507. file = this->GetOrCreateSource(outName.c_str(), true);
  508. // always mark as generated
  509. file->SetProperty("GENERATED","1");
  510. }
  511. // always create the output and mark it generated
  512. cmSourceFile *out = this->GetOrCreateSource(output, true);
  513. out->SetProperty("GENERATED","1");
  514. // process the command
  515. expandC = command;
  516. this->ExpandVariablesInString(expandC);
  517. std::string c = cmSystemTools::EscapeSpaces(expandC.c_str());
  518. std::vector<std::string> depends2(depends);
  519. if (main_dependency && main_dependency[0] != '\0')
  520. {
  521. depends2.push_back(main_dependency);
  522. }
  523. cmCustomCommand *cc =
  524. new cmCustomCommand(c.c_str(),combinedArgs.c_str(),depends2, output);
  525. if ( comment && comment[0] )
  526. {
  527. cc->SetComment(comment);
  528. }
  529. if (file->GetCustomCommand())
  530. {
  531. delete file->GetCustomCommand();
  532. }
  533. file->SetCustomCommand(cc);
  534. }
  535. void cmMakefile::
  536. AddCustomCommandToTarget(const char* target, const char* command,
  537. const std::vector<std::string>& commandArgs,
  538. cmTarget::CustomCommandType type,
  539. const char *comment)
  540. {
  541. // find the target,
  542. if (m_Targets.find(target) != m_Targets.end())
  543. {
  544. std::string expandC = command;
  545. this->ExpandVariablesInString(expandC);
  546. std::string c = cmSystemTools::EscapeSpaces(expandC.c_str());
  547. std::string combinedArgs;
  548. unsigned int i;
  549. for (i = 0; i < commandArgs.size(); ++i)
  550. {
  551. expandC = commandArgs[i].c_str();
  552. this->ExpandVariablesInString(expandC);
  553. combinedArgs += cmSystemTools::EscapeSpaces(expandC.c_str());
  554. combinedArgs += " ";
  555. }
  556. cmCustomCommand cc(c.c_str(),combinedArgs.c_str());
  557. if ( comment && comment[0] )
  558. {
  559. cc.SetComment(comment);
  560. }
  561. switch (type)
  562. {
  563. case cmTarget::PRE_BUILD:
  564. m_Targets[target].GetPreBuildCommands().push_back(cc);
  565. break;
  566. case cmTarget::PRE_LINK:
  567. m_Targets[target].GetPreLinkCommands().push_back(cc);
  568. break;
  569. case cmTarget::POST_BUILD:
  570. m_Targets[target].GetPostBuildCommands().push_back(cc);
  571. break;
  572. }
  573. std::string cacheCommand = command;
  574. this->ExpandVariablesInString(cacheCommand);
  575. if(this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()))
  576. {
  577. m_Targets[target].AddUtility(
  578. this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()));
  579. }
  580. }
  581. }
  582. void cmMakefile::AddDefineFlag(const char* flag)
  583. {
  584. m_DefineFlags += " ";
  585. m_DefineFlags += flag;
  586. }
  587. void cmMakefile::AddLinkLibrary(const char* lib, cmTarget::LinkLibraryType llt)
  588. {
  589. m_LinkLibraries.push_back(
  590. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  591. }
  592. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  593. const char* lib,
  594. cmTarget::LinkLibraryType llt)
  595. {
  596. cmTargets::iterator i = m_Targets.find(target);
  597. if ( i != m_Targets.end())
  598. {
  599. i->second.AddLinkLibrary( *this, target, lib, llt );
  600. }
  601. else
  602. {
  603. cmSystemTools::Error("Attempt to add link libraries to non-existant target: ", target, " for lib ", lib);
  604. }
  605. }
  606. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  607. const char* d)
  608. {
  609. cmTargets::iterator i = m_Targets.find(target);
  610. if ( i != m_Targets.end())
  611. {
  612. i->second.AddLinkDirectory( d );
  613. }
  614. else
  615. {
  616. cmSystemTools::Error("Attempt to add link directories to non-existant target: ",
  617. target, " for directory ", d);
  618. }
  619. }
  620. void cmMakefile::AddLinkLibrary(const char* lib)
  621. {
  622. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  623. }
  624. void cmMakefile::AddLinkDirectory(const char* dir)
  625. {
  626. // Don't add a link directory that is already present. Yes, this
  627. // linear search results in n^2 behavior, but n won't be getting
  628. // much bigger than 20. We cannot use a set because of order
  629. // dependency of the link search path.
  630. // remove trailing slashes
  631. if(dir && dir[strlen(dir)-1] == '/')
  632. {
  633. std::string newdir = dir;
  634. newdir = newdir.substr(0, newdir.size()-1);
  635. if(std::find(m_LinkDirectories.begin(),
  636. m_LinkDirectories.end(), newdir.c_str()) == m_LinkDirectories.end())
  637. {
  638. m_LinkDirectories.push_back(newdir);
  639. }
  640. }
  641. else
  642. {
  643. if(std::find(m_LinkDirectories.begin(),
  644. m_LinkDirectories.end(), dir) == m_LinkDirectories.end())
  645. {
  646. m_LinkDirectories.push_back(dir);
  647. }
  648. }
  649. }
  650. void cmMakefile::AddSubDirectory(const char* sub)
  651. {
  652. // make sure it isn't already there
  653. if (std::find(m_SubDirectories.begin(),
  654. m_SubDirectories.end(), sub) == m_SubDirectories.end())
  655. {
  656. m_SubDirectories.push_back(sub);
  657. }
  658. }
  659. void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
  660. {
  661. // Don't add an include directory that is already present. Yes,
  662. // this linear search results in n^2 behavior, but n won't be
  663. // getting much bigger than 20. We cannot use a set because of
  664. // order dependency of the include path.
  665. if(std::find(m_IncludeDirectories.begin(),
  666. m_IncludeDirectories.end(), inc) == m_IncludeDirectories.end())
  667. {
  668. if (before)
  669. {
  670. // WARNING: this *is* expensive (linear time) since it's a vector
  671. m_IncludeDirectories.insert(m_IncludeDirectories.begin(), inc);
  672. }
  673. else
  674. {
  675. m_IncludeDirectories.push_back(inc);
  676. }
  677. }
  678. }
  679. void cmMakefile::AddDefinition(const char* name, const char* value)
  680. {
  681. if (!value )
  682. {
  683. return;
  684. }
  685. m_Definitions.erase( DefinitionMap::key_type(name));
  686. m_Definitions.insert(DefinitionMap::value_type(name, value));
  687. cmVariableWatch* vv = this->GetVariableWatch();
  688. if ( vv )
  689. {
  690. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  691. }
  692. }
  693. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  694. const char* doc,
  695. cmCacheManager::CacheEntryType type)
  696. {
  697. this->GetCacheManager()->AddCacheEntry(name, value, doc, type);
  698. this->AddDefinition(name, value);
  699. }
  700. void cmMakefile::AddDefinition(const char* name, bool value)
  701. {
  702. if(value)
  703. {
  704. m_Definitions.erase( DefinitionMap::key_type(name));
  705. m_Definitions.insert(DefinitionMap::value_type(name, "ON"));
  706. }
  707. else
  708. {
  709. m_Definitions.erase( DefinitionMap::key_type(name));
  710. m_Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  711. }
  712. cmVariableWatch* vv = this->GetVariableWatch();
  713. if ( vv )
  714. {
  715. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  716. }
  717. }
  718. void cmMakefile::AddCacheDefinition(const char* name, bool value, const char* doc)
  719. {
  720. this->GetCacheManager()->AddCacheEntry(name, value, doc);
  721. this->AddDefinition(name, value);
  722. }
  723. void cmMakefile::RemoveDefinition(const char* name)
  724. {
  725. m_Definitions.erase(DefinitionMap::key_type(name));
  726. cmVariableWatch* vv = this->GetVariableWatch();
  727. if ( vv )
  728. {
  729. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS);
  730. }
  731. }
  732. void cmMakefile::SetProjectName(const char* p)
  733. {
  734. m_ProjectName = p;
  735. }
  736. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  737. {
  738. // for these targets do not add anything
  739. switch(target.GetType())
  740. {
  741. case cmTarget::UTILITY:
  742. case cmTarget::INSTALL_FILES:
  743. case cmTarget::INSTALL_PROGRAMS:
  744. return;
  745. default:;
  746. }
  747. std::vector<std::string>::iterator j;
  748. for(j = m_LinkDirectories.begin();
  749. j != m_LinkDirectories.end(); ++j)
  750. {
  751. target.AddLinkDirectory(j->c_str());
  752. }
  753. target.MergeLinkLibraries( *this, name, m_LinkLibraries );
  754. }
  755. void cmMakefile::AddLibrary(const char* lname, int shared,
  756. const std::vector<std::string> &srcs)
  757. {
  758. cmTarget target;
  759. switch (shared)
  760. {
  761. case 0:
  762. target.SetType(cmTarget::STATIC_LIBRARY);
  763. break;
  764. case 1:
  765. target.SetType(cmTarget::SHARED_LIBRARY);
  766. break;
  767. case 2:
  768. target.SetType(cmTarget::MODULE_LIBRARY);
  769. break;
  770. default:
  771. target.SetType(cmTarget::STATIC_LIBRARY);
  772. }
  773. // Clear its dependencies. Otherwise, dependencies might persist
  774. // over changes in CMakeLists.txt, making the information stale and
  775. // hence useless.
  776. target.ClearDependencyInformation( *this, lname );
  777. target.SetInAll(true);
  778. target.GetSourceLists() = srcs;
  779. this->AddGlobalLinkInformation(lname, target);
  780. m_Targets.insert(cmTargets::value_type(lname,target));
  781. // Add an entry into the cache
  782. std::string libPath = lname;
  783. libPath += "_CMAKE_PATH";
  784. this->GetCacheManager()->
  785. AddCacheEntry(libPath.c_str(),
  786. this->GetCurrentOutputDirectory(),
  787. "Path to a library", cmCacheManager::INTERNAL);
  788. // Add an entry into the cache
  789. std::string ltname = lname;
  790. ltname += "_LIBRARY_TYPE";
  791. switch (shared)
  792. {
  793. case 0:
  794. this->GetCacheManager()->AddCacheEntry(ltname.c_str(),"STATIC",
  795. "Whether a library is static, shared or module.",
  796. cmCacheManager::INTERNAL);
  797. break;
  798. case 1:
  799. this->GetCacheManager()->
  800. AddCacheEntry(ltname.c_str(),
  801. "SHARED",
  802. "Whether a library is static, shared or module.",
  803. cmCacheManager::INTERNAL);
  804. break;
  805. case 2:
  806. this->GetCacheManager()->
  807. AddCacheEntry(ltname.c_str(),
  808. "MODULE",
  809. "Whether a library is static, shared or module.",
  810. cmCacheManager::INTERNAL);
  811. break;
  812. default:
  813. this->GetCacheManager()->
  814. AddCacheEntry(ltname.c_str(),
  815. "STATIC",
  816. "Whether a library is static, shared or module.",
  817. cmCacheManager::INTERNAL);
  818. }
  819. }
  820. void cmMakefile::AddExecutable(const char *exeName,
  821. const std::vector<std::string> &srcs)
  822. {
  823. this->AddExecutable(exeName,srcs,false);
  824. }
  825. void cmMakefile::AddExecutable(const char *exeName,
  826. const std::vector<std::string> &srcs,
  827. bool win32)
  828. {
  829. cmTarget target;
  830. if (win32)
  831. {
  832. target.SetType(cmTarget::WIN32_EXECUTABLE);
  833. }
  834. else
  835. {
  836. target.SetType(cmTarget::EXECUTABLE);
  837. }
  838. target.SetInAll(true);
  839. target.GetSourceLists() = srcs;
  840. this->AddGlobalLinkInformation(exeName, target);
  841. m_Targets.insert(cmTargets::value_type(exeName,target));
  842. // Add an entry into the cache
  843. std::string exePath = exeName;
  844. exePath += "_CMAKE_PATH";
  845. this->GetCacheManager()->
  846. AddCacheEntry(exePath.c_str(),
  847. this->GetCurrentOutputDirectory(),
  848. "Path to an executable", cmCacheManager::INTERNAL);
  849. }
  850. void cmMakefile::AddUtilityCommand(const char* utilityName,
  851. const char* command,
  852. const char* arguments,
  853. bool all,
  854. const std::vector<std::string> &depends)
  855. {
  856. std::vector<std::string> empty;
  857. this->AddUtilityCommand(utilityName,command,arguments,all,
  858. depends, empty);
  859. }
  860. void cmMakefile::AddUtilityCommand(const char* utilityName,
  861. const char* command,
  862. const char* arguments,
  863. bool all,
  864. const std::vector<std::string> &dep,
  865. const std::vector<std::string> &out)
  866. {
  867. cmTarget target;
  868. target.SetType(cmTarget::UTILITY);
  869. target.SetInAll(all);
  870. if (out.size() > 1)
  871. {
  872. cmSystemTools::Error(
  873. "Utility targets can only have one output. For utilityNamed: ",
  874. utilityName);
  875. return;
  876. }
  877. if (out.size())
  878. {
  879. cmCustomCommand cc(command, arguments, dep, out[0].c_str());
  880. target.GetPostBuildCommands().push_back(cc);
  881. }
  882. else
  883. {
  884. cmCustomCommand cc(command, arguments, dep, (const char *)0);
  885. target.GetPostBuildCommands().push_back(cc);
  886. }
  887. m_Targets.insert(cmTargets::value_type(utilityName,target));
  888. }
  889. cmSourceFile *cmMakefile::GetSourceFileWithOutput(const char *cname)
  890. {
  891. std::string name = cname;
  892. // look through all the source files that have custom commands
  893. // and see if the custom command has the passed source file as an output
  894. // keep in mind the possible .rule extension that may be tacked on
  895. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  896. i != m_SourceFiles.end(); ++i)
  897. {
  898. // does this source file have a custom command?
  899. if ((*i)->GetCustomCommand())
  900. {
  901. // is the output of the custom command match the source files name
  902. std::string out = (*i)->GetCustomCommand()->GetOutput();
  903. if (out.rfind(name) != out.npos &&
  904. out.rfind(name) == out.size() - name.size())
  905. {
  906. return *i;
  907. }
  908. }
  909. }
  910. // otherwise return NULL
  911. return 0;
  912. }
  913. cmSourceGroup* cmMakefile::GetSourceGroup(const char* name)
  914. {
  915. // First see if the group exists. If so, replace its regular expression.
  916. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  917. sg != m_SourceGroups.end(); ++sg)
  918. {
  919. std::string sgName = sg->GetName();
  920. if(sgName == name)
  921. {
  922. return &(*sg);
  923. }
  924. }
  925. return 0;
  926. }
  927. void cmMakefile::AddSourceGroup(const char* name, const char* regex)
  928. {
  929. // First see if the group exists. If so, replace its regular expression.
  930. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  931. sg != m_SourceGroups.end(); ++sg)
  932. {
  933. std::string sgName = sg->GetName();
  934. if(sgName == name)
  935. {
  936. if ( regex )
  937. {
  938. // We only want to set the regular expression. If there are already
  939. // source files in the group, we don't want to remove them.
  940. sg->SetGroupRegex(regex);
  941. }
  942. return;
  943. }
  944. }
  945. // The group doesn't exist. Add it.
  946. m_SourceGroups.push_back(cmSourceGroup(name, regex));
  947. }
  948. void cmMakefile::AddExtraDirectory(const char* dir)
  949. {
  950. m_AuxSourceDirectories.push_back(dir);
  951. }
  952. // return the file name for the parent CMakeLists file to the
  953. // one passed in. Zero is returned if the CMakeLists file is the
  954. // one in the home directory or if for some reason a parent cmake lists
  955. // file cannot be found.
  956. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  957. {
  958. // extract the directory name
  959. std::string parentFile;
  960. std::string listsDir = currentFileName;
  961. std::string::size_type pos = listsDir.rfind('/');
  962. // if we could not find the directory return 0
  963. if(pos == std::string::npos)
  964. {
  965. return parentFile;
  966. }
  967. listsDir = listsDir.substr(0, pos);
  968. // if we are in the home directory then stop, return 0
  969. if(m_cmHomeDirectory == listsDir)
  970. {
  971. return parentFile;
  972. }
  973. // is there a parent directory we can check
  974. pos = listsDir.rfind('/');
  975. // if we could not find the directory return 0
  976. if(pos == std::string::npos)
  977. {
  978. return parentFile;
  979. }
  980. listsDir = listsDir.substr(0, pos);
  981. // is there a CMakeLists.txt file in the parent directory ?
  982. parentFile = listsDir;
  983. parentFile += "/CMakeLists.txt";
  984. while(!cmSystemTools::FileExists(parentFile.c_str()))
  985. {
  986. // There is no CMakeLists.txt file in the parent directory. This
  987. // can occur when coming out of a subdirectory resulting from a
  988. // SUBDIRS(Foo/Bar) command (coming out of Bar into Foo). Try
  989. // walking up until a CMakeLists.txt is found or the home
  990. // directory is hit.
  991. // if we are in the home directory then stop, return 0
  992. if(m_cmHomeDirectory == listsDir) { return ""; }
  993. // is there a parent directory we can check
  994. pos = listsDir.rfind('/');
  995. // if we could not find the directory return 0
  996. if(pos == std::string::npos) { return ""; }
  997. listsDir = listsDir.substr(0, pos);
  998. parentFile = listsDir;
  999. parentFile += "/CMakeLists.txt";
  1000. }
  1001. return parentFile;
  1002. }
  1003. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  1004. // include and library directories.
  1005. void cmMakefile::ExpandVariables()
  1006. {
  1007. // Now expand variables in the include and link strings
  1008. for(std::vector<std::string>::iterator d = m_IncludeDirectories.begin();
  1009. d != m_IncludeDirectories.end(); ++d)
  1010. {
  1011. this->ExpandVariablesInString(*d);
  1012. }
  1013. for(std::vector<std::string>::iterator d = m_LinkDirectories.begin();
  1014. d != m_LinkDirectories.end(); ++d)
  1015. {
  1016. this->ExpandVariablesInString(*d);
  1017. }
  1018. for(cmTarget::LinkLibraries::iterator l = m_LinkLibraries.begin();
  1019. l != m_LinkLibraries.end(); ++l)
  1020. {
  1021. this->ExpandVariablesInString(l->first);
  1022. }
  1023. }
  1024. void cmMakefile::ExpandVariablesInCustomCommands()
  1025. {
  1026. for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
  1027. i != m_SourceFiles.end(); ++i)
  1028. {
  1029. cmCustomCommand *cc = (*i)->GetCustomCommand();
  1030. if (cc)
  1031. {
  1032. cc->ExpandVariables(*this);
  1033. }
  1034. }
  1035. }
  1036. bool cmMakefile::IsOn(const char* name) const
  1037. {
  1038. const char* value = this->GetDefinition(name);
  1039. return cmSystemTools::IsOn(value);
  1040. }
  1041. const char* cmMakefile::GetDefinition(const char* name) const
  1042. {
  1043. const char* def = 0;
  1044. DefinitionMap::const_iterator pos = m_Definitions.find(name);
  1045. if(pos != m_Definitions.end())
  1046. {
  1047. def = (*pos).second.c_str();
  1048. }
  1049. else
  1050. {
  1051. def = this->GetCacheManager()->GetCacheValue(name);
  1052. }
  1053. cmVariableWatch* vv = this->GetVariableWatch();
  1054. if ( vv )
  1055. {
  1056. if ( def )
  1057. {
  1058. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS);
  1059. }
  1060. else
  1061. {
  1062. // are unknown access allowed
  1063. DefinitionMap::const_iterator pos2 =
  1064. m_Definitions.find("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  1065. if (pos2 != m_Definitions.end() &&
  1066. cmSystemTools::IsOn((*pos2).second.c_str()))
  1067. {
  1068. vv->VariableAccessed(name,
  1069. cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS);
  1070. }
  1071. else
  1072. {
  1073. vv->VariableAccessed(name, cmVariableWatch::
  1074. UNKNOWN_VARIABLE_READ_ACCESS);
  1075. }
  1076. }
  1077. }
  1078. return def;
  1079. }
  1080. std::vector<std::string> cmMakefile::GetDefinitions(int cacheonly /* = 0 */) const
  1081. {
  1082. std::map<std::string, int> definitions;
  1083. if ( !cacheonly )
  1084. {
  1085. DefinitionMap::const_iterator it;
  1086. for ( it = m_Definitions.begin(); it != m_Definitions.end(); it ++ )
  1087. {
  1088. definitions[it->first] = 1;
  1089. }
  1090. }
  1091. cmCacheManager::CacheIterator cit = this->GetCacheManager()->GetCacheIterator();
  1092. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  1093. {
  1094. definitions[cit.GetName()] = 1;
  1095. }
  1096. std::vector<std::string> res;
  1097. std::map<std::string, int>::iterator fit;
  1098. for ( fit = definitions.begin(); fit != definitions.end(); fit ++ )
  1099. {
  1100. res.push_back(fit->first);
  1101. }
  1102. return res;
  1103. }
  1104. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  1105. {
  1106. return this->ExpandVariablesInString(source, false);
  1107. }
  1108. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  1109. bool escapeQuotes,
  1110. bool atOnly) const
  1111. {
  1112. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  1113. // with GetDefinition(), if not found in the map, nothing is expanded.
  1114. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  1115. // the current environment variables.
  1116. // start by look for $ or @ in the string
  1117. std::string::size_type markerPos;
  1118. if(atOnly)
  1119. {
  1120. markerPos = source.find_first_of("@");
  1121. }
  1122. else
  1123. {
  1124. markerPos = source.find_first_of("$@");
  1125. }
  1126. // if not found, or found as the last character, then leave quickly as
  1127. // nothing needs to be expanded
  1128. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  1129. {
  1130. return source.c_str();
  1131. }
  1132. // current position
  1133. std::string::size_type currentPos =0; // start at 0
  1134. std::string result; // string with replacements
  1135. // go until the the end of the string
  1136. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  1137. {
  1138. // grab string from currentPos to the start of the variable
  1139. // and add it to the result
  1140. result += source.substr(currentPos, markerPos - currentPos);
  1141. char endVariableMarker; // what is the end of the variable @ or }
  1142. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  1143. if(!atOnly && source[markerPos] == '$')
  1144. {
  1145. // ${var} case
  1146. if(source[markerPos+1] == '{')
  1147. {
  1148. endVariableMarker = '}';
  1149. markerStartSize = 2;
  1150. }
  1151. // $ENV{var} case
  1152. else if(markerPos+4 < source.size() &&
  1153. source[markerPos+4] == '{' &&
  1154. !source.substr(markerPos+1, 3).compare("ENV"))
  1155. {
  1156. endVariableMarker = '}';
  1157. markerStartSize = 5;
  1158. }
  1159. else
  1160. {
  1161. // bogus $ with no { so add $ to result and move on
  1162. result += '$'; // add bogus $ back into string
  1163. currentPos = markerPos+1; // move on
  1164. endVariableMarker = ' '; // set end var to space so we can tell bogus
  1165. }
  1166. }
  1167. else
  1168. {
  1169. // @VAR case
  1170. endVariableMarker = '@';
  1171. }
  1172. // if it was a valid variable (started with @ or ${ or $ENV{ )
  1173. if(endVariableMarker != ' ')
  1174. {
  1175. markerPos += markerStartSize; // move past marker
  1176. // find the end variable marker starting at the markerPos
  1177. std::string::size_type endVariablePos =
  1178. source.find(endVariableMarker, markerPos);
  1179. if(endVariablePos == std::string::npos)
  1180. {
  1181. // no end marker found so add the bogus start
  1182. if(endVariableMarker == '@')
  1183. {
  1184. result += '@';
  1185. }
  1186. else
  1187. {
  1188. result += (markerStartSize == 5 ? "$ENV{" : "${");
  1189. }
  1190. currentPos = markerPos;
  1191. }
  1192. else
  1193. {
  1194. // good variable remove it
  1195. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  1196. bool found = false;
  1197. if (markerStartSize == 5) // $ENV{
  1198. {
  1199. char *ptr = getenv(var.c_str());
  1200. if (ptr)
  1201. {
  1202. if (escapeQuotes)
  1203. {
  1204. result += cmSystemTools::EscapeQuotes(ptr);
  1205. }
  1206. else
  1207. {
  1208. result += ptr;
  1209. }
  1210. found = true;
  1211. }
  1212. }
  1213. else
  1214. {
  1215. const char* lookup = this->GetDefinition(var.c_str());
  1216. if(lookup)
  1217. {
  1218. if (escapeQuotes)
  1219. {
  1220. result += cmSystemTools::EscapeQuotes(lookup);
  1221. }
  1222. else
  1223. {
  1224. result += lookup;
  1225. }
  1226. found = true;
  1227. }
  1228. }
  1229. // if found add to result, if not, then it gets blanked
  1230. if (!found)
  1231. {
  1232. // if no definition is found then add the var back
  1233. if(endVariableMarker == '@')
  1234. {
  1235. result += "@";
  1236. result += var;
  1237. result += "@";
  1238. }
  1239. // do nothing, we remove the variable
  1240. /* else
  1241. {
  1242. result += (markerStartSize == 5 ? "$ENV{" : "${");
  1243. result += var;
  1244. result += "}";
  1245. }
  1246. */
  1247. }
  1248. // lookup var, and replace it
  1249. currentPos = endVariablePos+1;
  1250. }
  1251. }
  1252. if(atOnly)
  1253. {
  1254. markerPos = source.find_first_of("@", currentPos);
  1255. }
  1256. else
  1257. {
  1258. markerPos = source.find_first_of("$@", currentPos);
  1259. }
  1260. }
  1261. result += source.substr(currentPos); // pick up the rest of the string
  1262. source = result;
  1263. return source.c_str();
  1264. }
  1265. void cmMakefile::RemoveVariablesInString(std::string& source,
  1266. bool atOnly) const
  1267. {
  1268. if(!atOnly)
  1269. {
  1270. cmRegularExpression var("(\\${[A-Za-z_0-9]*})");
  1271. while (var.find(source))
  1272. {
  1273. source.erase(var.start(),var.end() - var.start());
  1274. }
  1275. }
  1276. if(!atOnly)
  1277. {
  1278. cmRegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  1279. while (varb.find(source))
  1280. {
  1281. source.erase(varb.start(),varb.end() - varb.start());
  1282. }
  1283. }
  1284. cmRegularExpression var2("(@[A-Za-z_0-9]*@)");
  1285. while (var2.find(source))
  1286. {
  1287. source.erase(var2.start(),var2.end() - var2.start());
  1288. }
  1289. }
  1290. /**
  1291. * Add the default definitions to the makefile. These values must not
  1292. * be dependent on anything that isn't known when this cmMakefile instance
  1293. * is constructed.
  1294. */
  1295. void cmMakefile::AddDefaultDefinitions()
  1296. {
  1297. #if defined(_WIN32) || defined(__CYGWIN__)
  1298. this->AddDefinition("WIN32", "1");
  1299. #else
  1300. this->AddDefinition("UNIX", "1");
  1301. #endif
  1302. // Cygwin is more like unix so enable the unix commands
  1303. #if defined(__CYGWIN__)
  1304. this->AddDefinition("UNIX", "1");
  1305. this->AddDefinition("CYGWIN", "1");
  1306. #endif
  1307. #if defined(__APPLE__)
  1308. this->AddDefinition("APPLE", "1");
  1309. #endif
  1310. char temp[1024];
  1311. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1312. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1313. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1314. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1315. }
  1316. /**
  1317. * Find a source group whose regular expression matches the filename
  1318. * part of the given source name. Search backward through the list of
  1319. * source groups, and take the first matching group found. This way
  1320. * non-inherited SOURCE_GROUP commands will have precedence over
  1321. * inherited ones.
  1322. */
  1323. cmSourceGroup&
  1324. cmMakefile::FindSourceGroup(const char* source,
  1325. std::vector<cmSourceGroup> &groups)
  1326. {
  1327. std::string file = source;
  1328. std::string::size_type pos = file.rfind('/');
  1329. if(pos != std::string::npos)
  1330. {
  1331. file = file.substr(pos+1);
  1332. }
  1333. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1334. sg != groups.rend(); ++sg)
  1335. {
  1336. if(sg->Matches(file.c_str()))
  1337. {
  1338. return *sg;
  1339. }
  1340. }
  1341. // Shouldn't get here, but just in case, return the default group.
  1342. return groups.front();
  1343. }
  1344. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff)
  1345. {
  1346. // if there are no blockers get out of here
  1347. if (m_FunctionBlockers.begin() == m_FunctionBlockers.end())
  1348. {
  1349. return false;
  1350. }
  1351. // loop over all function blockers to see if any block this command
  1352. // evaluate in reverse, this is critical for balanced IF statements etc
  1353. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1354. for (pos = m_FunctionBlockers.rbegin();
  1355. pos != m_FunctionBlockers.rend(); ++pos)
  1356. {
  1357. if((*pos)->IsFunctionBlocked(lff, *this))
  1358. {
  1359. return true;
  1360. }
  1361. }
  1362. return false;
  1363. }
  1364. void cmMakefile::ExpandArguments(
  1365. std::vector<cmListFileArgument> const& inArgs,
  1366. std::vector<std::string>& outArgs)
  1367. {
  1368. std::vector<cmListFileArgument>::const_iterator i;
  1369. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  1370. {
  1371. // Expand the variables in the argument.
  1372. std::string value = i->Value;
  1373. this->ExpandVariablesInString(value);
  1374. // If the argument is quoted, it should be one argument.
  1375. // Otherwise, it may be a list of arguments.
  1376. if(i->Quoted)
  1377. {
  1378. outArgs.push_back(value);
  1379. }
  1380. else
  1381. {
  1382. cmSystemTools::ExpandListArgument(value, outArgs);
  1383. }
  1384. }
  1385. }
  1386. void cmMakefile::RemoveFunctionBlocker(const cmListFileFunction& lff)
  1387. {
  1388. // loop over all function blockers to see if any block this command
  1389. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1390. for (pos = m_FunctionBlockers.rbegin();
  1391. pos != m_FunctionBlockers.rend(); ++pos)
  1392. {
  1393. if ((*pos)->ShouldRemove(lff, *this))
  1394. {
  1395. cmFunctionBlocker* b = *pos;
  1396. m_FunctionBlockers.remove(b);
  1397. delete b;
  1398. break;
  1399. }
  1400. }
  1401. return;
  1402. }
  1403. void cmMakefile::SetHomeDirectory(const char* dir)
  1404. {
  1405. m_cmHomeDirectory = dir;
  1406. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1407. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1408. }
  1409. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1410. {
  1411. m_HomeOutputDirectory = lib;
  1412. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1413. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1414. }
  1415. /**
  1416. * Register the given cmData instance with its own name.
  1417. */
  1418. void cmMakefile::RegisterData(cmData* data)
  1419. {
  1420. std::string name = data->GetName();
  1421. DataMap::const_iterator d = m_DataMap.find(name);
  1422. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1423. {
  1424. delete d->second;
  1425. }
  1426. m_DataMap[name] = data;
  1427. }
  1428. /**
  1429. * Register the given cmData instance with the given name. This can be used
  1430. * to register a NULL pointer.
  1431. */
  1432. void cmMakefile::RegisterData(const char* name, cmData* data)
  1433. {
  1434. DataMap::const_iterator d = m_DataMap.find(name);
  1435. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1436. {
  1437. delete d->second;
  1438. }
  1439. m_DataMap[name] = data;
  1440. }
  1441. /**
  1442. * Lookup a cmData instance previously registered with the given name. If
  1443. * the instance cannot be found, return NULL.
  1444. */
  1445. cmData* cmMakefile::LookupData(const char* name) const
  1446. {
  1447. DataMap::const_iterator d = m_DataMap.find(name);
  1448. if(d != m_DataMap.end())
  1449. {
  1450. return d->second;
  1451. }
  1452. else
  1453. {
  1454. return 0;
  1455. }
  1456. }
  1457. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  1458. {
  1459. std::string s = cmSystemTools::GetFilenameName(sourceName);
  1460. std::string ext;
  1461. ext = cmSystemTools::GetFilenameLastExtension(s);
  1462. s = s.substr(0, s.length()-ext.length());
  1463. if ( ext.length() && ext[0] == '.' )
  1464. {
  1465. ext = ext.substr(1);
  1466. }
  1467. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1468. i != m_SourceFiles.end(); ++i)
  1469. {
  1470. if ((*i)->GetSourceName() == s)
  1471. {
  1472. if ((ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1473. {
  1474. return *i;
  1475. }
  1476. }
  1477. }
  1478. return 0;
  1479. }
  1480. cmSourceFile* cmMakefile::GetOrCreateSource(const char* sourceName,
  1481. bool generated)
  1482. {
  1483. // check to see if it exists
  1484. cmSourceFile* ret = this->GetSource(sourceName);
  1485. if (ret)
  1486. {
  1487. return ret;
  1488. }
  1489. // we must create one
  1490. std::string newfile = sourceName;
  1491. cmSourceFile file;
  1492. std::string path = cmSystemTools::GetFilenamePath(newfile);
  1493. if(generated)
  1494. {
  1495. std::string ext = cmSystemTools::GetFilenameLastExtension(newfile);
  1496. std::string name_no_ext = cmSystemTools::GetFilenameName(newfile.c_str());
  1497. name_no_ext = name_no_ext.substr(0, name_no_ext.length()-ext.length());
  1498. if ( ext.length() && ext[0] == '.' )
  1499. {
  1500. ext = ext.substr(1);
  1501. }
  1502. if((path.size() && path[0] == '/') ||
  1503. (path.size() > 1 && path[1] == ':'))
  1504. {
  1505. file.SetName(name_no_ext.c_str(), path.c_str(), ext.c_str(), false);
  1506. }
  1507. else
  1508. {
  1509. file.SetName(name_no_ext.c_str(), this->GetCurrentOutputDirectory(),
  1510. ext.c_str(), false);
  1511. }
  1512. }
  1513. else
  1514. {
  1515. // if this is a full path then
  1516. if((path.size() && path[0] == '/') ||
  1517. (path.size() > 1 && path[1] == ':'))
  1518. {
  1519. file.SetName(cmSystemTools::GetFilenameName(newfile.c_str()).c_str(),
  1520. path.c_str(),
  1521. this->GetSourceExtensions(),
  1522. this->GetHeaderExtensions());
  1523. }
  1524. else
  1525. {
  1526. file.SetName(newfile.c_str(), this->GetCurrentDirectory(),
  1527. this->GetSourceExtensions(),
  1528. this->GetHeaderExtensions());
  1529. }
  1530. }
  1531. // add the source file to the makefile
  1532. this->AddSource(file);
  1533. ret = this->GetSource(sourceName);
  1534. if (!ret)
  1535. {
  1536. cmSystemTools::Error(
  1537. "CMake failed to properly look up cmSourceFile: ", sourceName);
  1538. }
  1539. return ret;
  1540. }
  1541. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  1542. {
  1543. // check to see if it exists
  1544. cmSourceFile* ret = this->GetSource(sf.GetSourceName().c_str());
  1545. if(ret && ret->GetSourceExtension() == sf.GetSourceExtension())
  1546. {
  1547. return ret;
  1548. }
  1549. ret = new cmSourceFile(sf);
  1550. m_SourceFiles.push_back(ret);
  1551. return ret;
  1552. }
  1553. void cmMakefile::EnableLanguage(const char* lang)
  1554. {
  1555. m_LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this);
  1556. }
  1557. void cmMakefile::ExpandSourceListArguments(
  1558. std::vector<std::string> const& arguments,
  1559. std::vector<std::string>& newargs, unsigned int start)
  1560. {
  1561. // first figure out if we need to handle version 1.2 style source lists
  1562. int oldVersion = 1;
  1563. const char* versionValue
  1564. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  1565. if (versionValue && atof(versionValue) > 1.2)
  1566. {
  1567. oldVersion = 0;
  1568. }
  1569. // now expand the args
  1570. unsigned int i;
  1571. for(i = 0; i < arguments.size(); ++i)
  1572. {
  1573. // is the arg defined ?, if so use the def
  1574. const char *def = this->GetDefinition(arguments[i].c_str());
  1575. if (def && oldVersion && i >= start)
  1576. {
  1577. // Definition lookup could result in a list that needs to be
  1578. // expanded.
  1579. cmSystemTools::ExpandListArgument(def, newargs);
  1580. }
  1581. else
  1582. {
  1583. // List expansion will have been done already.
  1584. newargs.push_back(arguments[i]);
  1585. }
  1586. }
  1587. }
  1588. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  1589. const char *projectName, const char *targetName,
  1590. const std::vector<std::string> *cmakeArgs,
  1591. std::string *output)
  1592. {
  1593. // does the binary directory exist ? If not create it...
  1594. if (!cmSystemTools::FileIsDirectory(bindir))
  1595. {
  1596. cmSystemTools::MakeDirectory(bindir);
  1597. }
  1598. // change to the tests directory and run cmake
  1599. // use the cmake object instead of calling cmake
  1600. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1601. cmSystemTools::ChangeDirectory(bindir);
  1602. // make sure the same generator is used
  1603. // use this program as the cmake to be run, it should not
  1604. // be run that way but the cmake object requires a vailid path
  1605. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  1606. cmake cm;
  1607. cm.SetIsInTryCompile(true);
  1608. cmGlobalGenerator *gg =
  1609. cm.CreateGlobalGenerator(m_LocalGenerator->GetGlobalGenerator()->GetName());
  1610. if (!gg)
  1611. {
  1612. cmSystemTools::Error(
  1613. "Internal CMake error, TryCompile bad GlobalGenerator");
  1614. // return to the original directory
  1615. cmSystemTools::ChangeDirectory(cwd.c_str());
  1616. return 1;
  1617. }
  1618. cm.SetGlobalGenerator(gg);
  1619. // do a configure
  1620. cm.SetHomeDirectory(srcdir);
  1621. cm.SetHomeOutputDirectory(bindir);
  1622. cm.SetStartDirectory(srcdir);
  1623. cm.SetStartOutputDirectory(bindir);
  1624. cm.SetCMakeCommand(cmakeCommand.c_str());
  1625. cm.LoadCache();
  1626. // if cmake args were provided then pass them in
  1627. if (cmakeArgs)
  1628. {
  1629. cm.SetCacheArgs(*cmakeArgs);
  1630. }
  1631. // to save time we pass the EnableLanguage info directly
  1632. gg->EnableLanguagesFromGenerator(m_LocalGenerator->GetGlobalGenerator());
  1633. if (cm.Configure() != 0)
  1634. {
  1635. cmSystemTools::Error(
  1636. "Internal CMake error, TryCompile configure of cmake failed");
  1637. // return to the original directory
  1638. cmSystemTools::ChangeDirectory(cwd.c_str());
  1639. return 1;
  1640. }
  1641. if (cm.Generate() != 0)
  1642. {
  1643. cmSystemTools::Error(
  1644. "Internal CMake error, TryCompile generation of cmake failed");
  1645. // return to the original directory
  1646. cmSystemTools::ChangeDirectory(cwd.c_str());
  1647. return 1;
  1648. }
  1649. // finally call the generator to actually build the resulting project
  1650. int ret =
  1651. m_LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  1652. projectName,
  1653. targetName,
  1654. output);
  1655. cmSystemTools::ChangeDirectory(cwd.c_str());
  1656. return ret;
  1657. }
  1658. cmake *cmMakefile::GetCMakeInstance() const
  1659. {
  1660. if ( m_LocalGenerator && m_LocalGenerator->GetGlobalGenerator() )
  1661. {
  1662. return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  1663. }
  1664. return 0;
  1665. }
  1666. cmVariableWatch *cmMakefile::GetVariableWatch() const
  1667. {
  1668. if ( this->GetCMakeInstance() &&
  1669. this->GetCMakeInstance()->GetVariableWatch() )
  1670. {
  1671. return this->GetCMakeInstance()->GetVariableWatch();
  1672. }
  1673. return 0;
  1674. }
  1675. cmCacheManager *cmMakefile::GetCacheManager() const
  1676. {
  1677. return this->GetCMakeInstance()->GetCacheManager();
  1678. }
  1679. bool cmMakefile::GetLocal() const
  1680. {
  1681. return this->GetCMakeInstance()->GetLocal();
  1682. }
  1683. void cmMakefile::DisplayStatus(const char* message, float s)
  1684. {
  1685. this->GetLocalGenerator()->GetGlobalGenerator()
  1686. ->GetCMakeInstance()->UpdateProgress(message, s);
  1687. }
  1688. /**
  1689. * Find the library with the given name. Searches the given path and then
  1690. * the system search path. Returns the full path to the library if it is
  1691. * found. Otherwise, the empty string is returned.
  1692. */
  1693. std::string cmMakefile::FindLibrary(const char* name,
  1694. const std::vector<std::string>& userPaths)
  1695. {
  1696. // See if the executable exists as written.
  1697. if(cmSystemTools::FileExists(name))
  1698. {
  1699. return cmSystemTools::CollapseFullPath(name);
  1700. }
  1701. // Add the system search path to our path.
  1702. std::vector<std::string> path = userPaths;
  1703. cmSystemTools::GetPath(path);
  1704. // Add some lib directories specific to compilers, depending on the
  1705. // current generator, so that library that might have been stored here
  1706. // can be found too.
  1707. // i.e. Microsoft Visual Studio or .Net: path to compiler/../Lib
  1708. // Borland: path to compiler/../Lib
  1709. const char* genName = this->GetDefinition("CMAKE_GENERATOR");
  1710. if (genName)
  1711. {
  1712. if (!strcmp(genName, "NMake Makefiles") ||
  1713. !strcmp(genName, "Visual Studio 6"))
  1714. {
  1715. const char* compiler = this->GetDefinition("CMAKE_CXX_COMPILER");
  1716. if (compiler)
  1717. {
  1718. std::string compiler_path = cmSystemTools::FindProgram(compiler);
  1719. if (compiler_path.size())
  1720. {
  1721. std::string lib_path =
  1722. cmSystemTools::GetFilenamePath(
  1723. cmSystemTools::GetFilenamePath(compiler_path)) + "/Lib";
  1724. path.push_back(lib_path);
  1725. }
  1726. }
  1727. }
  1728. else if (!strcmp(genName, "Visual Studio 7"))
  1729. {
  1730. // It is likely that the compiler won't be in the path for .Net, but
  1731. // we know where devenv is.
  1732. const char* devenv = this->GetDefinition("MICROSOFT_DEVENV");
  1733. if (devenv)
  1734. {
  1735. std::string devenv_path = cmSystemTools::FindProgram(devenv);
  1736. if (devenv_path.size())
  1737. {
  1738. std::string vc7_path =
  1739. cmSystemTools::GetFilenamePath(
  1740. cmSystemTools::GetFilenamePath(
  1741. cmSystemTools::GetFilenamePath(devenv_path))) + "/Vc7";
  1742. path.push_back(vc7_path + "/lib");
  1743. path.push_back(vc7_path + "/PlatformSDK/lib");
  1744. }
  1745. }
  1746. }
  1747. else if (!strcmp(genName, "Borland Makefiles"))
  1748. {
  1749. const char* bcb_bin_path = this->GetDefinition("BCB_BIN_PATH");
  1750. if (bcb_bin_path)
  1751. {
  1752. std::string lib_path =
  1753. cmSystemTools::GetFilenamePath(bcb_bin_path) + "/Lib";
  1754. path.push_back(lib_path);
  1755. }
  1756. }
  1757. }
  1758. return cmSystemTools::FindLibrary(name, path);
  1759. }