cmMakefile.cxx 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839
  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 "cmSourceFile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmGlobalGenerator.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmCommands.h"
  20. #include "cmCacheManager.h"
  21. #include "cmFunctionBlocker.h"
  22. #include "cmListFileCache.h"
  23. #include "cmCommandArgumentParserHelper.h"
  24. #include "cmTest.h"
  25. #ifdef CMAKE_BUILD_WITH_CMAKE
  26. # include "cmVariableWatch.h"
  27. #endif
  28. #include "cmInstallGenerator.h"
  29. #include "cmake.h"
  30. #include <stdlib.h> // required for atoi
  31. #include <cmsys/RegularExpression.hxx>
  32. #include <ctype.h> // for isspace
  33. // default is not to be building executables
  34. cmMakefile::cmMakefile()
  35. {
  36. // Setup the default include file regular expression (match everything).
  37. this->IncludeFileRegularExpression = "^.*$";
  38. // Setup the default include complaint regular expression (match nothing).
  39. this->ComplainFileRegularExpression = "^$";
  40. // Source and header file extensions that we can handle
  41. // Set up a list of source and header extensions
  42. // these are used to find files when the extension
  43. // is not given
  44. // The "c" extension MUST precede the "C" extension.
  45. this->SourceFileExtensions.push_back( "c" );
  46. this->SourceFileExtensions.push_back( "C" );
  47. this->SourceFileExtensions.push_back( "c++" );
  48. this->SourceFileExtensions.push_back( "cc" );
  49. this->SourceFileExtensions.push_back( "cpp" );
  50. this->SourceFileExtensions.push_back( "cxx" );
  51. this->SourceFileExtensions.push_back( "m" );
  52. this->SourceFileExtensions.push_back( "M" );
  53. this->SourceFileExtensions.push_back( "mm" );
  54. this->HeaderFileExtensions.push_back( "h" );
  55. this->HeaderFileExtensions.push_back( "hh" );
  56. this->HeaderFileExtensions.push_back( "h++" );
  57. this->HeaderFileExtensions.push_back( "hm" );
  58. this->HeaderFileExtensions.push_back( "hpp" );
  59. this->HeaderFileExtensions.push_back( "hxx" );
  60. this->HeaderFileExtensions.push_back( "in" );
  61. this->HeaderFileExtensions.push_back( "txx" );
  62. this->DefineFlags = " ";
  63. this->LocalGenerator = 0;
  64. #if defined(CMAKE_BUILD_WITH_CMAKE)
  65. this->AddSourceGroup("", "^.*$");
  66. this->AddSourceGroup
  67. ("Source Files",
  68. "\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|rc|def|r|odl|idl|hpj|bat)$");
  69. this->AddSourceGroup("Header Files",
  70. "\\.(h|hh|h\\+\\+|hm|hpp|hxx|in|txx|inl)$");
  71. this->AddSourceGroup("CMake Rules", "\\.rule$");
  72. this->AddSourceGroup("Resources", "\\.plist$");
  73. #endif
  74. this->AddDefaultDefinitions();
  75. this->cmDefineRegex.compile("#cmakedefine[ \t]+([A-Za-z_0-9]*)");
  76. this->cmDefine01Regex.compile("#cmakedefine01[ \t]+([A-Za-z_0-9]*)");
  77. this->PreOrder = false;
  78. }
  79. cmMakefile::cmMakefile(const cmMakefile& mf)
  80. {
  81. this->Prefix = mf.Prefix;
  82. this->AuxSourceDirectories = mf.AuxSourceDirectories;
  83. this->cmStartDirectory = mf.cmStartDirectory;
  84. this->StartOutputDirectory = mf.StartOutputDirectory;
  85. this->cmHomeDirectory = mf.cmHomeDirectory;
  86. this->HomeOutputDirectory = mf.HomeOutputDirectory;
  87. this->cmCurrentListFile = mf.cmCurrentListFile;
  88. this->ProjectName = mf.ProjectName;
  89. this->Targets = mf.Targets;
  90. this->SourceFiles = mf.SourceFiles;
  91. this->Tests = mf.Tests;
  92. this->IncludeDirectories = mf.IncludeDirectories;
  93. this->LinkDirectories = mf.LinkDirectories;
  94. this->ListFiles = mf.ListFiles;
  95. this->OutputFiles = mf.OutputFiles;
  96. this->LinkLibraries = mf.LinkLibraries;
  97. this->InstallGenerators = mf.InstallGenerators;
  98. this->IncludeFileRegularExpression = mf.IncludeFileRegularExpression;
  99. this->ComplainFileRegularExpression = mf.ComplainFileRegularExpression;
  100. this->SourceFileExtensions = mf.SourceFileExtensions;
  101. this->HeaderFileExtensions = mf.HeaderFileExtensions;
  102. this->DefineFlags = mf.DefineFlags;
  103. #if defined(CMAKE_BUILD_WITH_CMAKE)
  104. this->SourceGroups = mf.SourceGroups;
  105. #endif
  106. this->Definitions = mf.Definitions;
  107. this->LocalGenerator = mf.LocalGenerator;
  108. this->FunctionBlockers = mf.FunctionBlockers;
  109. this->DataMap = mf.DataMap;
  110. this->MacrosMap = mf.MacrosMap;
  111. this->SubDirectoryOrder = mf.SubDirectoryOrder;
  112. this->TemporaryDefinitionKey = mf.TemporaryDefinitionKey;
  113. this->Properties = mf.Properties;
  114. this->PreOrder = mf.PreOrder;
  115. this->ListFileStack = mf.ListFileStack;
  116. }
  117. const char* cmMakefile::GetReleaseVersion()
  118. {
  119. #if CMake_VERSION_MINOR & 1
  120. return "development";
  121. #else
  122. # if CMake_VERSION_PATCH == 0
  123. return "beta";
  124. # else
  125. return "patch " CMAKE_TO_STRING(CMake_VERSION_PATCH);
  126. # endif
  127. #endif
  128. }
  129. unsigned int cmMakefile::GetCacheMajorVersion()
  130. {
  131. if(const char* vstr =
  132. this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
  133. {
  134. unsigned int v=0;
  135. if(sscanf(vstr, "%u", &v) == 1)
  136. {
  137. return v;
  138. }
  139. }
  140. return 0;
  141. }
  142. unsigned int cmMakefile::GetCacheMinorVersion()
  143. {
  144. if(const char* vstr =
  145. this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"))
  146. {
  147. unsigned int v=0;
  148. if(sscanf(vstr, "%u", &v) == 1)
  149. {
  150. return v;
  151. }
  152. }
  153. return 0;
  154. }
  155. cmMakefile::~cmMakefile()
  156. {
  157. for(std::vector<cmInstallGenerator*>::iterator
  158. i = this->InstallGenerators.begin();
  159. i != this->InstallGenerators.end(); ++i)
  160. {
  161. delete *i;
  162. }
  163. for(std::vector<cmSourceFile*>::iterator i = this->SourceFiles.begin();
  164. i != this->SourceFiles.end(); ++i)
  165. {
  166. delete *i;
  167. }
  168. for(std::vector<cmTest*>::iterator i = this->Tests.begin();
  169. i != this->Tests.end(); ++i)
  170. {
  171. delete *i;
  172. }
  173. for(unsigned int i=0; i < this->UsedCommands.size(); i++)
  174. {
  175. delete this->UsedCommands[i];
  176. }
  177. for(DataMapType::const_iterator d = this->DataMap.begin();
  178. d != this->DataMap.end(); ++d)
  179. {
  180. if(d->second)
  181. {
  182. delete d->second;
  183. }
  184. }
  185. std::list<cmFunctionBlocker *>::iterator pos;
  186. for (pos = this->FunctionBlockers.begin();
  187. pos != this->FunctionBlockers.end(); ++pos)
  188. {
  189. cmFunctionBlocker* b = *pos;
  190. delete b;
  191. }
  192. this->FunctionBlockers.clear();
  193. }
  194. void cmMakefile::PrintStringVector(const char* s,
  195. const std::vector<std::string>& v) const
  196. {
  197. std::cout << s << ": ( \n";
  198. for(std::vector<std::string>::const_iterator i = v.begin();
  199. i != v.end(); ++i)
  200. {
  201. std::cout << (*i).c_str() << " ";
  202. }
  203. std::cout << " )\n";
  204. }
  205. void cmMakefile
  206. ::PrintStringVector(const char* s,
  207. const std::vector<std::pair<cmStdString, bool> >& v) const
  208. {
  209. std::cout << s << ": ( \n";
  210. for(std::vector<std::pair<cmStdString, bool> >::const_iterator i
  211. = v.begin(); i != v.end(); ++i)
  212. {
  213. std::cout << i->first.c_str() << " " << i->second;
  214. }
  215. std::cout << " )\n";
  216. }
  217. // call print on all the classes in the makefile
  218. void cmMakefile::Print()
  219. {
  220. // print the class lists
  221. std::cout << "classes:\n";
  222. std::cout << " this->Targets: ";
  223. for (cmTargets::iterator l = this->Targets.begin();
  224. l != this->Targets.end(); l++)
  225. {
  226. std::cout << l->first << std::endl;
  227. }
  228. std::cout << " this->StartOutputDirectory; " <<
  229. this->StartOutputDirectory.c_str() << std::endl;
  230. std::cout << " this->HomeOutputDirectory; " <<
  231. this->HomeOutputDirectory.c_str() << std::endl;
  232. std::cout << " this->cmStartDirectory; " <<
  233. this->cmStartDirectory.c_str() << std::endl;
  234. std::cout << " this->cmHomeDirectory; " <<
  235. this->cmHomeDirectory.c_str() << std::endl;
  236. std::cout << " this->ProjectName; "
  237. << this->ProjectName.c_str() << std::endl;
  238. this->PrintStringVector("this->IncludeDirectories;",
  239. this->IncludeDirectories);
  240. this->PrintStringVector("this->LinkDirectories", this->LinkDirectories);
  241. #if defined(CMAKE_BUILD_WITH_CMAKE)
  242. for( std::vector<cmSourceGroup>::const_iterator i =
  243. this->SourceGroups.begin(); i != this->SourceGroups.end(); ++i)
  244. {
  245. std::cout << "Source Group: " << i->GetName() << std::endl;
  246. }
  247. #endif
  248. }
  249. bool cmMakefile::CommandExists(const char* name) const
  250. {
  251. return this->GetCMakeInstance()->CommandExists(name);
  252. }
  253. bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff)
  254. {
  255. bool result = true;
  256. // quick return if blocked
  257. if(this->IsFunctionBlocked(lff))
  258. {
  259. // No error.
  260. return result;
  261. }
  262. std::string name = lff.Name;
  263. // execute the command
  264. cmCommand *rm =
  265. this->GetCMakeInstance()->GetCommand(name.c_str());
  266. if(rm)
  267. {
  268. const char* versionValue
  269. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  270. int major = 0;
  271. int minor = 0;
  272. if ( versionValue )
  273. {
  274. sscanf(versionValue, "%d.%d", &major, &minor);
  275. }
  276. if ( rm->IsDeprecated(major, minor) )
  277. {
  278. cmOStringStream error;
  279. error << "Error in cmake code at\n"
  280. << lff.FilePath << ":" << lff.Line << ":\n"
  281. << rm->GetError() << std::endl
  282. << "Current CMake stack: " << this->GetListFileStack().c_str();
  283. cmSystemTools::Error(error.str().c_str());
  284. return false;
  285. }
  286. cmCommand* usedCommand = rm->Clone();
  287. usedCommand->SetMakefile(this);
  288. bool keepCommand = false;
  289. if(usedCommand->GetEnabled() && !cmSystemTools::GetFatalErrorOccured() &&
  290. (!this->GetCMakeInstance()->GetScriptMode() ||
  291. usedCommand->IsScriptable()))
  292. {
  293. if(!usedCommand->InvokeInitialPass(lff.Arguments))
  294. {
  295. cmOStringStream error;
  296. error << "Error in cmake code at\n"
  297. << lff.FilePath << ":" << lff.Line << ":\n"
  298. << usedCommand->GetError() << std::endl
  299. << "Current CMake stack: " << this->GetListFileStack().c_str();
  300. cmSystemTools::Error(error.str().c_str());
  301. result = false;
  302. if ( this->GetCMakeInstance()->GetScriptMode() )
  303. {
  304. cmSystemTools::SetFatalErrorOccured();
  305. }
  306. }
  307. else
  308. {
  309. // use the command
  310. keepCommand = true;
  311. this->UsedCommands.push_back(usedCommand);
  312. }
  313. }
  314. else if ( this->GetCMakeInstance()->GetScriptMode()
  315. && !usedCommand->IsScriptable() )
  316. {
  317. cmOStringStream error;
  318. error << "Error in cmake code at\n"
  319. << lff.FilePath << ":" << lff.Line << ":\n"
  320. << "Command " << usedCommand->GetName()
  321. << " not scriptable" << std::endl;
  322. cmSystemTools::Error(error.str().c_str());
  323. result = false;
  324. cmSystemTools::SetFatalErrorOccured();
  325. }
  326. // if the Cloned command was not used
  327. // then delete it
  328. if(!keepCommand)
  329. {
  330. delete usedCommand;
  331. }
  332. }
  333. else
  334. {
  335. if(!cmSystemTools::GetFatalErrorOccured())
  336. {
  337. cmOStringStream error;
  338. error << "Error in cmake code at\n"
  339. << lff.FilePath << ":" << lff.Line << ":\n"
  340. << "Unknown CMake command \"" << lff.Name.c_str() << "\".";
  341. cmSystemTools::Error(error.str().c_str());
  342. result = false;
  343. cmSystemTools::SetFatalErrorOccured();
  344. }
  345. }
  346. return result;
  347. }
  348. // Parse the given CMakeLists.txt file executing all commands
  349. //
  350. bool cmMakefile::ReadListFile(const char* filename_in,
  351. const char *external_in)
  352. {
  353. std::string currentFile = this->GetSafeDefinition("CMAKE_PARENT_LIST_FILE");
  354. this->AddDefinition("CMAKE_PARENT_LIST_FILE", filename_in);
  355. // used to watch for blockers going out of scope
  356. // e.g. mismatched IF statement
  357. std::set<cmFunctionBlocker *> originalBlockers;
  358. const char* external = 0;
  359. std::string external_abs;
  360. const char* filename = filename_in;
  361. std::string filename_abs;
  362. if (external_in)
  363. {
  364. external_abs =
  365. cmSystemTools::CollapseFullPath(external_in,
  366. this->cmStartDirectory.c_str());
  367. external = external_abs.c_str();
  368. if (filename_in)
  369. {
  370. filename_abs =
  371. cmSystemTools::CollapseFullPath(filename_in,
  372. this->cmStartDirectory.c_str());
  373. filename = filename_abs.c_str();
  374. }
  375. }
  376. // keep track of the current file being read
  377. if (filename)
  378. {
  379. if(this->cmCurrentListFile != filename)
  380. {
  381. this->cmCurrentListFile = filename;
  382. }
  383. // loop over current function blockers and record them
  384. std::list<cmFunctionBlocker *>::iterator pos;
  385. for (pos = this->FunctionBlockers.begin();
  386. pos != this->FunctionBlockers.end(); ++pos)
  387. {
  388. originalBlockers.insert(*pos);
  389. }
  390. }
  391. // Now read the input file
  392. const char *filenametoread= filename;
  393. if( external)
  394. {
  395. filenametoread= external;
  396. }
  397. // try to see if the list file is the top most
  398. // list file for a project, and if it is, then it
  399. // must have a project command. If there is not
  400. // one, then cmake will provide one via the
  401. // cmListFileCache class.
  402. bool requireProjectCommand = false;
  403. if(!external && this->cmStartDirectory == this->cmHomeDirectory)
  404. {
  405. if(cmSystemTools::LowerCase(
  406. cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
  407. {
  408. requireProjectCommand = true;
  409. }
  410. }
  411. // push the listfile onto the stack
  412. this->ListFileStack.push_back(filenametoread);
  413. cmListFile cacheFile;
  414. if( !cacheFile.ParseFile(filenametoread, requireProjectCommand) )
  415. {
  416. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentFile.c_str());
  417. return false;
  418. }
  419. // add this list file to the list of dependencies
  420. this->ListFiles.push_back( filenametoread);
  421. const size_t numberFunctions = cacheFile.Functions.size();
  422. for(size_t i =0; i < numberFunctions; ++i)
  423. {
  424. this->ExecuteCommand(cacheFile.Functions[i]);
  425. if ( cmSystemTools::GetFatalErrorOccured() )
  426. {
  427. // pop the listfile off the stack
  428. this->ListFileStack.pop_back();
  429. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentFile.c_str());
  430. return true;
  431. }
  432. }
  433. // send scope ended to and function blockers
  434. if (filename)
  435. {
  436. // loop over all function blockers to see if any block this command
  437. std::list<cmFunctionBlocker *>::iterator pos;
  438. for (pos = this->FunctionBlockers.begin();
  439. pos != this->FunctionBlockers.end(); ++pos)
  440. {
  441. // if this blocker was not in the original then send a
  442. // scope ended message
  443. if (originalBlockers.find(*pos) == originalBlockers.end())
  444. {
  445. (*pos)->ScopeEnded(*this);
  446. }
  447. }
  448. }
  449. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentFile.c_str());
  450. // pop the listfile off the stack
  451. this->ListFileStack.pop_back();
  452. return true;
  453. }
  454. void cmMakefile::AddCommand(cmCommand* wg)
  455. {
  456. this->GetCMakeInstance()->AddCommand(wg);
  457. }
  458. // Set the make file
  459. void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
  460. {
  461. this->LocalGenerator = lg;
  462. }
  463. void cmMakefile::FinalPass()
  464. {
  465. // do all the variable expansions here
  466. this->ExpandVariables();
  467. // give all the commands a chance to do something
  468. // after the file has been parsed before generation
  469. for(std::vector<cmCommand*>::iterator i = this->UsedCommands.begin();
  470. i != this->UsedCommands.end(); ++i)
  471. {
  472. (*i)->FinalPass();
  473. }
  474. }
  475. // Generate the output file
  476. void cmMakefile::ConfigureFinalPass()
  477. {
  478. this->FinalPass();
  479. const char* oldValue
  480. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  481. if (oldValue && atof(oldValue) <= 1.2)
  482. {
  483. cmSystemTools::Error("You have requested backwards compatibility "
  484. "with CMake version 1.2 or earlier. This version "
  485. "of CMake only supports backwards compatibility "
  486. "with CMake 1.4 or later. For compatibility with "
  487. "1.2 or earlier please use CMake 2.0");
  488. }
  489. for (cmTargets::iterator l = this->Targets.begin();
  490. l != this->Targets.end(); l++)
  491. {
  492. l->second.GenerateSourceFilesFromSourceLists(*this);
  493. l->second.AnalyzeLibDependencies(*this);
  494. }
  495. }
  496. //----------------------------------------------------------------------------
  497. void
  498. cmMakefile::AddCustomCommandToTarget(const char* target,
  499. const std::vector<std::string>& depends,
  500. const cmCustomCommandLines& commandLines,
  501. cmTarget::CustomCommandType type,
  502. const char* comment,
  503. const char* workingDir,
  504. bool escapeOldStyle)
  505. {
  506. // Find the target to which to add the custom command.
  507. cmTargets::iterator ti = this->Targets.find(target);
  508. if(ti != this->Targets.end())
  509. {
  510. // Add the command to the appropriate build step for the target.
  511. std::vector<std::string> no_output;
  512. cmCustomCommand cc(no_output, depends, commandLines, comment, workingDir);
  513. cc.SetEscapeOldStyle(escapeOldStyle);
  514. switch(type)
  515. {
  516. case cmTarget::PRE_BUILD:
  517. ti->second.GetPreBuildCommands().push_back(cc);
  518. break;
  519. case cmTarget::PRE_LINK:
  520. ti->second.GetPreLinkCommands().push_back(cc);
  521. break;
  522. case cmTarget::POST_BUILD:
  523. ti->second.GetPostBuildCommands().push_back(cc);
  524. break;
  525. }
  526. // Add dependencies on commands CMake knows how to build.
  527. for(cmCustomCommandLines::const_iterator cli = commandLines.begin();
  528. cli != commandLines.end(); ++cli)
  529. {
  530. std::string cacheCommand = *cli->begin();
  531. if(const char* knownTarget =
  532. this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()))
  533. {
  534. ti->second.AddUtility(knownTarget);
  535. }
  536. }
  537. }
  538. }
  539. //----------------------------------------------------------------------------
  540. void
  541. cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
  542. const std::vector<std::string>& depends,
  543. const char* main_dependency,
  544. const cmCustomCommandLines& commandLines,
  545. const char* comment,
  546. const char* workingDir,
  547. bool replace,
  548. bool escapeOldStyle)
  549. {
  550. // Make sure there is at least one output.
  551. if(outputs.empty())
  552. {
  553. cmSystemTools::Error("Attempt to add a custom rule with no output!");
  554. return;
  555. }
  556. // Choose a source file on which to store the custom command.
  557. cmSourceFile* file = 0;
  558. if(main_dependency && main_dependency[0])
  559. {
  560. // The main dependency was specified. Use it unless a different
  561. // custom command already used it.
  562. file = this->GetSource(main_dependency);
  563. if(file && file->GetCustomCommand() && !replace)
  564. {
  565. // The main dependency already has a custom command.
  566. if(commandLines == file->GetCustomCommand()->GetCommandLines())
  567. {
  568. // The existing custom command is identical. Silently ignore
  569. // the duplicate.
  570. return;
  571. }
  572. else
  573. {
  574. // The existing custom command is different. We need to
  575. // generate a rule file for this new command.
  576. file = 0;
  577. }
  578. }
  579. else
  580. {
  581. // The main dependency does not have a custom command or we are
  582. // allowed to replace it. Use it to store the command.
  583. file = this->GetOrCreateSource(main_dependency);
  584. }
  585. }
  586. // Generate a rule file if the main dependency is not available.
  587. if(!file)
  588. {
  589. // Construct a rule file associated with the first output produced.
  590. std::string outName = outputs[0];
  591. outName += ".rule";
  592. // Check if the rule file already exists.
  593. file = this->GetSource(outName.c_str());
  594. if(file && file->GetCustomCommand() && !replace)
  595. {
  596. // The rule file already exists.
  597. if(commandLines != file->GetCustomCommand()->GetCommandLines())
  598. {
  599. cmSystemTools::Error("Attempt to add a custom rule to output \"",
  600. outName.c_str(),
  601. "\" which already has a custom rule.");
  602. }
  603. return;
  604. }
  605. // Create a cmSourceFile for the rule file.
  606. file = this->GetOrCreateSource(outName.c_str(), true);
  607. }
  608. // Always create the output sources and mark them generated.
  609. for(std::vector<std::string>::const_iterator o = outputs.begin();
  610. o != outputs.end(); ++o)
  611. {
  612. if(cmSourceFile* out = this->GetOrCreateSource(o->c_str(), true))
  613. {
  614. out->SetProperty("GENERATED", "1");
  615. }
  616. }
  617. // Construct a complete list of dependencies.
  618. std::vector<std::string> depends2(depends);
  619. if(main_dependency && main_dependency[0])
  620. {
  621. depends2.push_back(main_dependency);
  622. }
  623. // Attach the custom command to the file.
  624. if(file)
  625. {
  626. cmCustomCommand* cc =
  627. new cmCustomCommand(outputs, depends2, commandLines,
  628. comment, workingDir);
  629. cc->SetEscapeOldStyle(escapeOldStyle);
  630. file->SetCustomCommand(cc);
  631. }
  632. }
  633. //----------------------------------------------------------------------------
  634. void
  635. cmMakefile::AddCustomCommandToOutput(const char* output,
  636. const std::vector<std::string>& depends,
  637. const char* main_dependency,
  638. const cmCustomCommandLines& commandLines,
  639. const char* comment,
  640. const char* workingDir,
  641. bool replace,
  642. bool escapeOldStyle)
  643. {
  644. std::vector<std::string> outputs;
  645. outputs.push_back(output);
  646. this->AddCustomCommandToOutput(outputs, depends, main_dependency,
  647. commandLines, comment, workingDir,
  648. replace, escapeOldStyle);
  649. }
  650. //----------------------------------------------------------------------------
  651. void
  652. cmMakefile::AddCustomCommandOldStyle(const char* target,
  653. const std::vector<std::string>& outputs,
  654. const std::vector<std::string>& depends,
  655. const char* source,
  656. const cmCustomCommandLines& commandLines,
  657. const char* comment)
  658. {
  659. // Translate the old-style signature to one of the new-style
  660. // signatures.
  661. if(strcmp(source, target) == 0)
  662. {
  663. // In the old-style signature if the source and target were the
  664. // same then it added a post-build rule to the target. Preserve
  665. // this behavior.
  666. this->AddCustomCommandToTarget(target, depends, commandLines,
  667. cmTarget::POST_BUILD, comment, 0);
  668. return;
  669. }
  670. // Each output must get its own copy of this rule.
  671. cmsys::RegularExpression sourceFiles("\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|"
  672. "rc|def|r|odl|idl|hpj|bat|h|h\\+\\+|"
  673. "hm|hpp|hxx|in|txx|inl)$");
  674. for(std::vector<std::string>::const_iterator oi = outputs.begin();
  675. oi != outputs.end(); ++oi)
  676. {
  677. // Get the name of this output.
  678. const char* output = oi->c_str();
  679. // Choose whether to use a main dependency.
  680. if(sourceFiles.find(source))
  681. {
  682. // The source looks like a real file. Use it as the main dependency.
  683. this->AddCustomCommandToOutput(output, depends, source,
  684. commandLines, comment, 0);
  685. }
  686. else
  687. {
  688. // The source may not be a real file. Do not use a main dependency.
  689. const char* no_main_dependency = 0;
  690. std::vector<std::string> depends2 = depends;
  691. depends2.push_back(source);
  692. this->AddCustomCommandToOutput(output, depends2, no_main_dependency,
  693. commandLines, comment, 0);
  694. }
  695. // If the rule was added to the source (and not a .rule file),
  696. // then add the source to the target to make sure the rule is
  697. // included.
  698. std::string sname = output;
  699. sname += ".rule";
  700. if(!this->GetSource(sname.c_str()))
  701. {
  702. if (this->Targets.find(target) != this->Targets.end())
  703. {
  704. this->Targets[target].GetSourceLists().push_back(source);
  705. }
  706. else
  707. {
  708. cmSystemTools::Error("Attempt to add a custom rule to a target "
  709. "that does not exist yet for target ", target);
  710. return;
  711. }
  712. }
  713. }
  714. }
  715. //----------------------------------------------------------------------------
  716. void cmMakefile::AddUtilityCommand(const char* utilityName, bool all,
  717. const std::vector<std::string>& depends,
  718. const char* workingDirectory,
  719. const char* command,
  720. const char* arg1,
  721. const char* arg2,
  722. const char* arg3,
  723. const char* arg4)
  724. {
  725. // Construct the command line for the custom command.
  726. cmCustomCommandLine commandLine;
  727. commandLine.push_back(command);
  728. if(arg1)
  729. {
  730. commandLine.push_back(arg1);
  731. }
  732. if(arg2)
  733. {
  734. commandLine.push_back(arg2);
  735. }
  736. if(arg3)
  737. {
  738. commandLine.push_back(arg3);
  739. }
  740. if(arg4)
  741. {
  742. commandLine.push_back(arg4);
  743. }
  744. cmCustomCommandLines commandLines;
  745. commandLines.push_back(commandLine);
  746. // Call the real signature of this method.
  747. this->AddUtilityCommand(utilityName, all, workingDirectory,
  748. depends, commandLines);
  749. }
  750. //----------------------------------------------------------------------------
  751. void cmMakefile::AddUtilityCommand(const char* utilityName, bool all,
  752. const char* workingDirectory,
  753. const std::vector<std::string>& depends,
  754. const cmCustomCommandLines& commandLines,
  755. bool escapeOldStyle)
  756. {
  757. // Create a target instance for this utility.
  758. cmTarget target;
  759. target.SetType(cmTarget::UTILITY, utilityName);
  760. target.SetInAll(all);
  761. target.SetMakefile(this);
  762. // Store the custom command in the target.
  763. std::string force = this->GetStartOutputDirectory();
  764. force += cmake::GetCMakeFilesDirectory();
  765. force += "/";
  766. force += utilityName;
  767. const char* no_main_dependency = 0;
  768. const char* empty_comment = "";
  769. bool no_replace = false;
  770. this->AddCustomCommandToOutput(force.c_str(), depends,
  771. no_main_dependency,
  772. commandLines, empty_comment,
  773. workingDirectory, no_replace,
  774. escapeOldStyle);
  775. target.GetSourceLists().push_back(force);
  776. // The output is not actually created so mark it symbolic.
  777. if(cmSourceFile* sf = this->GetSource(force.c_str()))
  778. {
  779. sf->SetProperty("SYMBOLIC", "1");
  780. }
  781. else
  782. {
  783. cmSystemTools::Error("Could not get source file entry for ",
  784. force.c_str());
  785. }
  786. // Add the target to the set of targets.
  787. cmTargets::iterator it =
  788. this->Targets.insert(cmTargets::value_type(utilityName,target)).first;
  789. this->LocalGenerator->GetGlobalGenerator()->AddTarget(*it);
  790. }
  791. void cmMakefile::AddDefineFlag(const char* flag)
  792. {
  793. this->DefineFlags += " ";
  794. this->DefineFlags += flag;
  795. }
  796. void cmMakefile::RemoveDefineFlag(const char* flag)
  797. {
  798. // Check the length of the flag to remove.
  799. std::string::size_type len = strlen(flag);
  800. if(len < 1)
  801. {
  802. return;
  803. }
  804. // Remove all instances of the flag that are surrounded by
  805. // whitespace or the beginning/end of the string.
  806. for(std::string::size_type lpos = this->DefineFlags.find(flag, 0);
  807. lpos != std::string::npos; lpos = this->DefineFlags.find(flag, lpos))
  808. {
  809. std::string::size_type rpos = lpos + len;
  810. if((lpos <= 0 || isspace(this->DefineFlags[lpos-1])) &&
  811. (rpos >= this->DefineFlags.size() || isspace(this->DefineFlags[rpos])))
  812. {
  813. this->DefineFlags.erase(lpos, len);
  814. }
  815. else
  816. {
  817. ++lpos;
  818. }
  819. }
  820. }
  821. void cmMakefile::AddLinkLibrary(const char* lib,
  822. cmTarget::LinkLibraryType llt)
  823. {
  824. this->LinkLibraries.push_back(
  825. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  826. }
  827. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  828. const char* lib,
  829. cmTarget::LinkLibraryType llt)
  830. {
  831. cmTargets::iterator i = this->Targets.find(target);
  832. if ( i != this->Targets.end())
  833. {
  834. cmTarget* tgt =
  835. this->GetCMakeInstance()->GetGlobalGenerator()->FindTarget(0, lib);
  836. if(tgt)
  837. {
  838. bool allowModules = true;
  839. const char* versionValue
  840. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  841. if (versionValue && (atof(versionValue) >= 2.4) )
  842. {
  843. allowModules = false;
  844. }
  845. // if it is not a static or shared library then you can not link to it
  846. if(!((tgt->GetType() == cmTarget::STATIC_LIBRARY) ||
  847. (tgt->GetType() == cmTarget::SHARED_LIBRARY)))
  848. {
  849. cmOStringStream e;
  850. e << "Attempt to add link target " << lib << " of type: "
  851. << cmTarget::TargetTypeNames[static_cast<int>(tgt->GetType())]
  852. << "\nto target " << target
  853. << ". You can only link to STATIC or SHARED libraries.";
  854. // in older versions of cmake linking to modules was allowed
  855. if( tgt->GetType() == cmTarget::MODULE_LIBRARY )
  856. {
  857. e <<
  858. "\nTo allow linking of modules set "
  859. "CMAKE_BACKWARDS_COMPATIBILITY to 2.2 or lower\n";
  860. }
  861. // if no modules are allowed then this is always an error
  862. if(!allowModules ||
  863. // if we allow modules but the type is not a module then it is
  864. // still an error
  865. (allowModules && tgt->GetType() != cmTarget::MODULE_LIBRARY))
  866. {
  867. cmSystemTools::Error(e.str().c_str());
  868. }
  869. }
  870. }
  871. i->second.AddLinkLibrary( *this, target, lib, llt );
  872. }
  873. else
  874. {
  875. cmOStringStream e;
  876. e << "Attempt to add link library \""
  877. << lib << "\" to target \""
  878. << target << "\" which is not built by this project.";
  879. cmSystemTools::Error(e.str().c_str());
  880. }
  881. }
  882. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  883. const char* d)
  884. {
  885. cmTargets::iterator i = this->Targets.find(target);
  886. if ( i != this->Targets.end())
  887. {
  888. i->second.AddLinkDirectory( d );
  889. }
  890. else
  891. {
  892. cmSystemTools::Error
  893. ("Attempt to add link directories to non-existant target: ",
  894. target, " for directory ", d);
  895. }
  896. }
  897. void cmMakefile::AddLinkLibrary(const char* lib)
  898. {
  899. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  900. }
  901. void cmMakefile::AddLinkDirectory(const char* dir)
  902. {
  903. // Don't add a link directory that is already present. Yes, this
  904. // linear search results in n^2 behavior, but n won't be getting
  905. // much bigger than 20. We cannot use a set because of order
  906. // dependency of the link search path.
  907. // remove trailing slashes
  908. if(dir && dir[strlen(dir)-1] == '/')
  909. {
  910. std::string newdir = dir;
  911. newdir = newdir.substr(0, newdir.size()-1);
  912. if(std::find(this->LinkDirectories.begin(),
  913. this->LinkDirectories.end(),
  914. newdir.c_str()) == this->LinkDirectories.end())
  915. {
  916. this->LinkDirectories.push_back(newdir);
  917. }
  918. }
  919. else
  920. {
  921. if(std::find(this->LinkDirectories.begin(),
  922. this->LinkDirectories.end(), dir)
  923. == this->LinkDirectories.end())
  924. {
  925. this->LinkDirectories.push_back(dir);
  926. }
  927. }
  928. }
  929. void cmMakefile::InitializeFromParent()
  930. {
  931. cmMakefile *parent = this->LocalGenerator->GetParent()->GetMakefile();
  932. // copy the definitions
  933. this->Definitions = parent->Definitions;
  934. // copy include paths
  935. this->IncludeDirectories = parent->IncludeDirectories;
  936. // define flags
  937. this->DefineFlags = parent->DefineFlags;
  938. // link libraries
  939. this->LinkLibraries = parent->LinkLibraries;
  940. // link directories
  941. this->LinkDirectories = parent->LinkDirectories;
  942. // the initial project name
  943. this->ProjectName = parent->ProjectName;
  944. // Copy include regular expressions.
  945. this->IncludeFileRegularExpression = parent->IncludeFileRegularExpression;
  946. this->ComplainFileRegularExpression = parent->ComplainFileRegularExpression;
  947. }
  948. void cmMakefile::ConfigureSubDirectory(cmLocalGenerator *lg2)
  949. {
  950. // copy our variables from the child makefile
  951. lg2->GetMakefile()->InitializeFromParent();
  952. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  953. // finally configure the subdir
  954. lg2->Configure();
  955. }
  956. void cmMakefile::AddSubDirectory(const char* sub,
  957. bool topLevel, bool preorder)
  958. {
  959. // the source path must be made full if it isn't already
  960. std::string srcPath = sub;
  961. if (!cmSystemTools::FileIsFullPath(srcPath.c_str()))
  962. {
  963. srcPath = this->GetCurrentDirectory();
  964. srcPath += "/";
  965. srcPath += sub;
  966. }
  967. // binary path must be made full if it isn't already
  968. std::string binPath = sub;
  969. if (!cmSystemTools::FileIsFullPath(binPath.c_str()))
  970. {
  971. binPath = this->GetCurrentOutputDirectory();
  972. binPath += "/";
  973. binPath += sub;
  974. }
  975. this->AddSubDirectory(srcPath.c_str(), binPath.c_str(),
  976. topLevel, preorder, false);
  977. }
  978. void cmMakefile::AddSubDirectory(const char* srcPath, const char *binPath,
  979. bool topLevel, bool preorder,
  980. bool immediate)
  981. {
  982. std::vector<cmLocalGenerator *>& children =
  983. this->LocalGenerator->GetChildren();
  984. // has this directory already been added? If so error
  985. unsigned int i;
  986. for (i = 0; i < children.size(); ++i)
  987. {
  988. if (srcPath == children[i]->GetMakefile()->GetStartDirectory())
  989. {
  990. cmSystemTools::Error
  991. ("Attempt to add subdirectory multiple times for directory.\n",
  992. srcPath);
  993. return;
  994. }
  995. }
  996. // create a new local generator and set its parent
  997. cmLocalGenerator *lg2 =
  998. this->LocalGenerator->GetGlobalGenerator()->CreateLocalGenerator();
  999. lg2->SetParent(this->LocalGenerator);
  1000. this->LocalGenerator->GetGlobalGenerator()->AddLocalGenerator(lg2);
  1001. // set the subdirs start dirs
  1002. lg2->GetMakefile()->SetStartDirectory(srcPath);
  1003. lg2->GetMakefile()->SetStartOutputDirectory(binPath);
  1004. lg2->SetExcludeAll(!topLevel);
  1005. lg2->GetMakefile()->SetPreOrder(preorder);
  1006. if (immediate)
  1007. {
  1008. this->ConfigureSubDirectory(lg2);
  1009. }
  1010. }
  1011. void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
  1012. {
  1013. // Don't add an include directory that is already present. Yes,
  1014. // this linear search results in n^2 behavior, but n won't be
  1015. // getting much bigger than 20. We cannot use a set because of
  1016. // order dependency of the include path.
  1017. std::vector<std::string>::iterator i =
  1018. std::find(this->IncludeDirectories.begin(),
  1019. this->IncludeDirectories.end(), inc);
  1020. if(i == this->IncludeDirectories.end())
  1021. {
  1022. if (before)
  1023. {
  1024. // WARNING: this *is* expensive (linear time) since it's a vector
  1025. this->IncludeDirectories.insert(this->IncludeDirectories.begin(), inc);
  1026. }
  1027. else
  1028. {
  1029. this->IncludeDirectories.push_back(inc);
  1030. }
  1031. }
  1032. else
  1033. {
  1034. if(before)
  1035. {
  1036. // if this before and already in the path then remove it
  1037. this->IncludeDirectories.erase(i);
  1038. // WARNING: this *is* expensive (linear time) since it's a vector
  1039. this->IncludeDirectories.insert(this->IncludeDirectories.begin(), inc);
  1040. }
  1041. }
  1042. }
  1043. void cmMakefile::AddDefinition(const char* name, const char* value)
  1044. {
  1045. if (!value )
  1046. {
  1047. return;
  1048. }
  1049. this->TemporaryDefinitionKey = name;
  1050. this->Definitions[this->TemporaryDefinitionKey] = value;
  1051. #ifdef CMAKE_BUILD_WITH_CMAKE
  1052. cmVariableWatch* vv = this->GetVariableWatch();
  1053. if ( vv )
  1054. {
  1055. vv->VariableAccessed(this->TemporaryDefinitionKey,
  1056. cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  1057. }
  1058. #endif
  1059. }
  1060. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  1061. const char* doc,
  1062. cmCacheManager::CacheEntryType type)
  1063. {
  1064. const char* val = value;
  1065. cmCacheManager::CacheIterator it =
  1066. this->GetCacheManager()->GetCacheIterator(name);
  1067. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  1068. it.Initialized())
  1069. {
  1070. val = it.GetValue();
  1071. if ( type == cmCacheManager::PATH || type == cmCacheManager::FILEPATH )
  1072. {
  1073. std::vector<std::string>::size_type cc;
  1074. std::vector<std::string> files;
  1075. std::string nvalue = "";
  1076. cmSystemTools::ExpandListArgument(val, files);
  1077. for ( cc = 0; cc < files.size(); cc ++ )
  1078. {
  1079. files[cc] = cmSystemTools::CollapseFullPath(files[cc].c_str());
  1080. if ( cc > 0 )
  1081. {
  1082. nvalue += ";";
  1083. }
  1084. nvalue += files[cc];
  1085. }
  1086. this->GetCacheManager()->AddCacheEntry(name, nvalue.c_str(), doc, type);
  1087. val = it.GetValue();
  1088. }
  1089. }
  1090. this->GetCacheManager()->AddCacheEntry(name, val, doc, type);
  1091. this->AddDefinition(name, val);
  1092. }
  1093. void cmMakefile::AddDefinition(const char* name, bool value)
  1094. {
  1095. if(value)
  1096. {
  1097. this->Definitions.erase( DefinitionMap::key_type(name));
  1098. this->Definitions.insert(DefinitionMap::value_type(name, "ON"));
  1099. }
  1100. else
  1101. {
  1102. this->Definitions.erase( DefinitionMap::key_type(name));
  1103. this->Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  1104. }
  1105. #ifdef CMAKE_BUILD_WITH_CMAKE
  1106. cmVariableWatch* vv = this->GetVariableWatch();
  1107. if ( vv )
  1108. {
  1109. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  1110. }
  1111. #endif
  1112. }
  1113. void cmMakefile::AddCacheDefinition(const char* name,
  1114. bool value,
  1115. const char* doc)
  1116. {
  1117. bool val = value;
  1118. cmCacheManager::CacheIterator it =
  1119. this->GetCacheManager()->GetCacheIterator(name);
  1120. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  1121. it.Initialized())
  1122. {
  1123. val = it.GetValueAsBool();
  1124. }
  1125. this->GetCacheManager()->AddCacheEntry(name, val, doc);
  1126. this->AddDefinition(name, val);
  1127. }
  1128. void cmMakefile::RemoveDefinition(const char* name)
  1129. {
  1130. this->Definitions.erase(DefinitionMap::key_type(name));
  1131. #ifdef CMAKE_BUILD_WITH_CMAKE
  1132. cmVariableWatch* vv = this->GetVariableWatch();
  1133. if ( vv )
  1134. {
  1135. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS);
  1136. }
  1137. #endif
  1138. }
  1139. void cmMakefile::SetProjectName(const char* p)
  1140. {
  1141. this->ProjectName = p;
  1142. }
  1143. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  1144. {
  1145. // for these targets do not add anything
  1146. switch(target.GetType())
  1147. {
  1148. case cmTarget::UTILITY:
  1149. case cmTarget::GLOBAL_TARGET:
  1150. case cmTarget::INSTALL_FILES:
  1151. case cmTarget::INSTALL_PROGRAMS:
  1152. return;
  1153. default:;
  1154. }
  1155. std::vector<std::string>::iterator j;
  1156. for(j = this->LinkDirectories.begin();
  1157. j != this->LinkDirectories.end(); ++j)
  1158. {
  1159. target.AddLinkDirectory(j->c_str());
  1160. }
  1161. target.MergeLinkLibraries( *this, name, this->LinkLibraries );
  1162. }
  1163. void cmMakefile::AddLibrary(const char* lname, int shared,
  1164. const std::vector<std::string> &srcs)
  1165. {
  1166. cmTarget target;
  1167. switch (shared)
  1168. {
  1169. case 0:
  1170. target.SetType(cmTarget::STATIC_LIBRARY, lname);
  1171. break;
  1172. case 1:
  1173. target.SetType(cmTarget::SHARED_LIBRARY, lname);
  1174. break;
  1175. case 2:
  1176. target.SetType(cmTarget::MODULE_LIBRARY, lname);
  1177. break;
  1178. default:
  1179. target.SetType(cmTarget::STATIC_LIBRARY, lname);
  1180. }
  1181. // Clear its dependencies. Otherwise, dependencies might persist
  1182. // over changes in CMakeLists.txt, making the information stale and
  1183. // hence useless.
  1184. target.ClearDependencyInformation( *this, lname );
  1185. target.SetInAll(true);
  1186. target.GetSourceLists() = srcs;
  1187. target.SetMakefile(this);
  1188. this->AddGlobalLinkInformation(lname, target);
  1189. cmTargets::iterator it =
  1190. this->Targets.insert(cmTargets::value_type(lname,target)).first;
  1191. this->LocalGenerator->GetGlobalGenerator()->AddTarget(*it);
  1192. }
  1193. cmTarget* cmMakefile::AddExecutable(const char *exeName,
  1194. const std::vector<std::string> &srcs)
  1195. {
  1196. cmTarget target;
  1197. target.SetType(cmTarget::EXECUTABLE, exeName);
  1198. target.SetInAll(true);
  1199. target.GetSourceLists() = srcs;
  1200. target.SetMakefile(this);
  1201. this->AddGlobalLinkInformation(exeName, target);
  1202. cmTargets::iterator it =
  1203. this->Targets.insert(cmTargets::value_type(exeName,target)).first;
  1204. this->LocalGenerator->GetGlobalGenerator()->AddTarget(*it);
  1205. return &it->second;
  1206. }
  1207. cmSourceFile *cmMakefile::GetSourceFileWithOutput(const char *cname)
  1208. {
  1209. std::string name = cname;
  1210. std::string out;
  1211. // look through all the source files that have custom commands
  1212. // and see if the custom command has the passed source file as an output
  1213. // keep in mind the possible .rule extension that may be tacked on
  1214. for(std::vector<cmSourceFile*>::const_iterator i =
  1215. this->SourceFiles.begin(); i != this->SourceFiles.end(); ++i)
  1216. {
  1217. // does this source file have a custom command?
  1218. if ((*i)->GetCustomCommand())
  1219. {
  1220. // is the output of the custom command match the source files name
  1221. const std::vector<std::string>& outputs =
  1222. (*i)->GetCustomCommand()->GetOutputs();
  1223. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1224. o != outputs.end(); ++o)
  1225. {
  1226. out = *o;
  1227. std::string::size_type pos = out.rfind(name);
  1228. // If the output matches exactly
  1229. if (pos != out.npos &&
  1230. pos == out.size() - name.size() &&
  1231. (pos ==0 || out[pos-1] == '/'))
  1232. {
  1233. return *i;
  1234. }
  1235. }
  1236. }
  1237. }
  1238. // otherwise return NULL
  1239. return 0;
  1240. }
  1241. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1242. cmSourceGroup* cmMakefile::GetSourceGroup(const char* name)
  1243. {
  1244. // First see if the group exists. If so, replace its regular expression.
  1245. for(std::vector<cmSourceGroup>::iterator sg = this->SourceGroups.begin();
  1246. sg != this->SourceGroups.end(); ++sg)
  1247. {
  1248. std::string sgName = sg->GetName();
  1249. if(sgName == name)
  1250. {
  1251. return &(*sg);
  1252. }
  1253. else
  1254. {
  1255. cmSourceGroup *target = sg->lookupChild(name);
  1256. if(target)
  1257. {
  1258. return target;
  1259. }
  1260. }
  1261. }
  1262. return 0;
  1263. }
  1264. void cmMakefile::AddSourceGroup(const char* name,
  1265. const char* regex,
  1266. const char *parent)
  1267. {
  1268. // First see if the group exists. If so, replace its regular expression.
  1269. for(unsigned int i=0;i<this->SourceGroups.size();++i)
  1270. {
  1271. cmSourceGroup *sg = &this->SourceGroups[i];
  1272. std::string sgName = sg->GetName();
  1273. if(!parent)
  1274. {
  1275. if(sgName == name)
  1276. {
  1277. if ( regex )
  1278. {
  1279. // We only want to set the regular expression. If there are already
  1280. // source files in the group, we don't want to remove them.
  1281. sg->SetGroupRegex(regex);
  1282. }
  1283. return;
  1284. }
  1285. }
  1286. else
  1287. {
  1288. if(sgName == parent)
  1289. {
  1290. cmSourceGroup *localtarget = sg->lookupChild(name);
  1291. if(localtarget)
  1292. {
  1293. if ( regex )
  1294. {
  1295. // We only want to set the regular expression. If there are
  1296. // already source files in the group, we don't want to remove
  1297. // them.
  1298. localtarget->SetGroupRegex(regex);
  1299. }
  1300. }
  1301. else
  1302. {
  1303. sg->AddChild(cmSourceGroup(name, regex));
  1304. }
  1305. return;
  1306. }
  1307. else
  1308. {
  1309. cmSourceGroup *localtarget = sg->lookupChild(parent);
  1310. if(localtarget)
  1311. {
  1312. cmSourceGroup *addtarget = localtarget->lookupChild(name);
  1313. if(addtarget)
  1314. {
  1315. if ( regex )
  1316. {
  1317. // We only want to set the regular expression. If there are
  1318. // already source files in the group, we don't want to
  1319. // remove them.
  1320. addtarget->SetGroupRegex(regex);
  1321. }
  1322. }
  1323. else
  1324. {
  1325. localtarget->AddChild(cmSourceGroup(name, regex));
  1326. }
  1327. return;
  1328. }
  1329. }
  1330. }
  1331. }
  1332. // The group doesn't exist. Add it.
  1333. this->SourceGroups.push_back(cmSourceGroup(name, regex));
  1334. }
  1335. #endif
  1336. void cmMakefile::AddExtraDirectory(const char* dir)
  1337. {
  1338. this->AuxSourceDirectories.push_back(dir);
  1339. }
  1340. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  1341. // include and library directories.
  1342. void cmMakefile::ExpandVariables()
  1343. {
  1344. // Now expand variables in the include and link strings
  1345. for(std::vector<std::string>::iterator d = this->IncludeDirectories.begin();
  1346. d != this->IncludeDirectories.end(); ++d)
  1347. {
  1348. this->ExpandVariablesInString(*d, true, true);
  1349. }
  1350. for(std::vector<std::string>::iterator d = this->LinkDirectories.begin();
  1351. d != this->LinkDirectories.end(); ++d)
  1352. {
  1353. this->ExpandVariablesInString(*d, true, true);
  1354. }
  1355. for(cmTarget::LinkLibraryVectorType::iterator l =
  1356. this->LinkLibraries.begin();
  1357. l != this->LinkLibraries.end(); ++l)
  1358. {
  1359. this->ExpandVariablesInString(l->first, true, true);
  1360. }
  1361. }
  1362. bool cmMakefile::IsOn(const char* name) const
  1363. {
  1364. const char* value = this->GetDefinition(name);
  1365. return cmSystemTools::IsOn(value);
  1366. }
  1367. bool cmMakefile::IsSet(const char* name) const
  1368. {
  1369. const char* value = this->GetDefinition(name);
  1370. if ( !value )
  1371. {
  1372. return false;
  1373. }
  1374. if ( ! *value )
  1375. {
  1376. return false;
  1377. }
  1378. if ( cmSystemTools::IsNOTFOUND(value) )
  1379. {
  1380. return false;
  1381. }
  1382. return true;
  1383. }
  1384. bool cmMakefile::CanIWriteThisFile(const char* fileName)
  1385. {
  1386. if ( !this->IsOn("CMAKE_DISABLE_SOURCE_CHANGES") )
  1387. {
  1388. return true;
  1389. }
  1390. // If we are doing an in-source build, than the test will always fail
  1391. if ( cmSystemTools::SameFile(this->GetHomeDirectory(),
  1392. this->GetHomeOutputDirectory()) )
  1393. {
  1394. if ( this->IsOn("CMAKE_DISABLE_IN_SOURCE_BUILD") )
  1395. {
  1396. return false;
  1397. }
  1398. return true;
  1399. }
  1400. // Check if this is subdirectory of the source tree but not a
  1401. // subdirectory of a build tree
  1402. if ( cmSystemTools::IsSubDirectory(fileName,
  1403. this->GetHomeDirectory()) &&
  1404. !cmSystemTools::IsSubDirectory(fileName,
  1405. this->GetHomeOutputDirectory()) )
  1406. {
  1407. return false;
  1408. }
  1409. return true;
  1410. }
  1411. const char* cmMakefile::GetRequiredDefinition(const char* name) const
  1412. {
  1413. const char* ret = this->GetDefinition(name);
  1414. if(!ret)
  1415. {
  1416. cmSystemTools::Error("Error required internal CMake variable not "
  1417. "set, cmake may be not be built correctly.\n",
  1418. "Missing variable is:\n",
  1419. name);
  1420. return "";
  1421. }
  1422. return ret;
  1423. }
  1424. const char* cmMakefile::GetDefinition(const char* name) const
  1425. {
  1426. const char* def = 0;
  1427. DefinitionMap::const_iterator pos = this->Definitions.find(name);
  1428. if(pos != this->Definitions.end())
  1429. {
  1430. def = (*pos).second.c_str();
  1431. }
  1432. else
  1433. {
  1434. def = this->GetCacheManager()->GetCacheValue(name);
  1435. }
  1436. #ifdef CMAKE_BUILD_WITH_CMAKE
  1437. cmVariableWatch* vv = this->GetVariableWatch();
  1438. if ( vv )
  1439. {
  1440. if ( def )
  1441. {
  1442. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS);
  1443. }
  1444. else
  1445. {
  1446. // are unknown access allowed
  1447. DefinitionMap::const_iterator pos2 =
  1448. this->Definitions.find("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  1449. if (pos2 != this->Definitions.end() &&
  1450. cmSystemTools::IsOn((*pos2).second.c_str()))
  1451. {
  1452. vv->VariableAccessed
  1453. (name, cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS);
  1454. }
  1455. else
  1456. {
  1457. vv->VariableAccessed(name, cmVariableWatch::
  1458. UNKNOWN_VARIABLE_READ_ACCESS);
  1459. }
  1460. }
  1461. }
  1462. #endif
  1463. return def;
  1464. }
  1465. const char* cmMakefile::GetSafeDefinition(const char* def) const
  1466. {
  1467. const char* ret = this->GetDefinition(def);
  1468. if(!ret)
  1469. {
  1470. return "";
  1471. }
  1472. return ret;
  1473. }
  1474. std::vector<std::string> cmMakefile
  1475. ::GetDefinitions(int cacheonly /* = 0 */) const
  1476. {
  1477. std::map<cmStdString, int> definitions;
  1478. if ( !cacheonly )
  1479. {
  1480. DefinitionMap::const_iterator it;
  1481. for ( it = this->Definitions.begin();
  1482. it != this->Definitions.end(); it ++ )
  1483. {
  1484. definitions[it->first] = 1;
  1485. }
  1486. }
  1487. cmCacheManager::CacheIterator cit =
  1488. this->GetCacheManager()->GetCacheIterator();
  1489. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  1490. {
  1491. definitions[cit.GetName()] = 1;
  1492. }
  1493. std::vector<std::string> res;
  1494. std::map<cmStdString, int>::iterator fit;
  1495. for ( fit = definitions.begin(); fit != definitions.end(); fit ++ )
  1496. {
  1497. res.push_back(fit->first);
  1498. }
  1499. return res;
  1500. }
  1501. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  1502. {
  1503. return this->ExpandVariablesInString(source, false, false);
  1504. }
  1505. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  1506. bool escapeQuotes,
  1507. bool noEscapes,
  1508. bool atOnly,
  1509. const char* filename,
  1510. long line,
  1511. bool removeEmpty) const
  1512. {
  1513. if ( source.empty() || source.find_first_of("$@\\") == source.npos)
  1514. {
  1515. return source.c_str();
  1516. }
  1517. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  1518. // with GetDefinition(), if not found in the map, nothing is expanded.
  1519. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  1520. // the current environment variables.
  1521. bool notParsed = true;
  1522. if ( !atOnly )
  1523. {
  1524. cmCommandArgumentParserHelper parser;
  1525. parser.SetMakefile(this);
  1526. parser.SetLineFile(line, filename);
  1527. parser.SetEscapeQuotes(escapeQuotes);
  1528. parser.SetNoEscapeMode(noEscapes);
  1529. int res = parser.ParseString(source.c_str(), 0);
  1530. if ( res )
  1531. {
  1532. source = parser.GetResult();
  1533. notParsed = false;
  1534. }
  1535. else
  1536. {
  1537. cmOStringStream error;
  1538. error << "Syntax error in cmake code at\n"
  1539. << (filename?filename:"(no filename given)")
  1540. << ":" << line << ":\n"
  1541. << parser.GetError() << ", when parsing string \""
  1542. << source.c_str() << "\"";
  1543. const char* versionValue
  1544. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  1545. int major = 0;
  1546. int minor = 0;
  1547. if ( versionValue )
  1548. {
  1549. sscanf(versionValue, "%d.%d", &major, &minor);
  1550. }
  1551. if ( major < 2 || major == 2 && minor < 1 )
  1552. {
  1553. cmSystemTools::Error(error.str().c_str());
  1554. cmSystemTools::SetFatalErrorOccured();
  1555. return source.c_str();
  1556. }
  1557. else
  1558. {
  1559. cmSystemTools::Message(error.str().c_str());
  1560. }
  1561. }
  1562. }
  1563. if ( notParsed )
  1564. {
  1565. // start by look for $ or @ in the string
  1566. std::string::size_type markerPos;
  1567. if(atOnly)
  1568. {
  1569. markerPos = source.find_first_of("@");
  1570. }
  1571. else
  1572. {
  1573. markerPos = source.find_first_of("$@");
  1574. }
  1575. // if not found, or found as the last character, then leave quickly as
  1576. // nothing needs to be expanded
  1577. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  1578. {
  1579. return source.c_str();
  1580. }
  1581. // current position
  1582. std::string::size_type currentPos =0; // start at 0
  1583. std::string result; // string with replacements
  1584. // go until the the end of the string
  1585. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  1586. {
  1587. // grab string from currentPos to the start of the variable
  1588. // and add it to the result
  1589. result += source.substr(currentPos, markerPos - currentPos);
  1590. char endVariableMarker; // what is the end of the variable @ or }
  1591. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  1592. if(!atOnly && source[markerPos] == '$')
  1593. {
  1594. // ${var} case
  1595. if(source[markerPos+1] == '{')
  1596. {
  1597. endVariableMarker = '}';
  1598. markerStartSize = 2;
  1599. }
  1600. // $ENV{var} case
  1601. else if(markerPos+4 < source.size() &&
  1602. source[markerPos+4] == '{' &&
  1603. !source.substr(markerPos+1, 3).compare("ENV"))
  1604. {
  1605. endVariableMarker = '}';
  1606. markerStartSize = 5;
  1607. }
  1608. else
  1609. {
  1610. // bogus $ with no { so add $ to result and move on
  1611. result += '$'; // add bogus $ back into string
  1612. currentPos = markerPos+1; // move on
  1613. // set end var to space so we can tell bogus
  1614. endVariableMarker = ' ';
  1615. }
  1616. }
  1617. else
  1618. {
  1619. // @VAR case
  1620. endVariableMarker = '@';
  1621. }
  1622. // if it was a valid variable (started with @ or ${ or $ENV{ )
  1623. if(endVariableMarker != ' ')
  1624. {
  1625. markerPos += markerStartSize; // move past marker
  1626. // find the end variable marker starting at the markerPos
  1627. // make sure it is a valid variable between
  1628. std::string::size_type endVariablePos =
  1629. source.find_first_not_of(
  1630. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
  1631. markerPos);
  1632. if(endVariablePos != std::string::npos &&
  1633. source[endVariablePos] != endVariableMarker)
  1634. {
  1635. endVariablePos = std::string::npos;
  1636. }
  1637. if(endVariablePos == std::string::npos)
  1638. {
  1639. // no end marker found so add the bogus start
  1640. if(endVariableMarker == '@')
  1641. {
  1642. result += '@';
  1643. }
  1644. else
  1645. {
  1646. result += (markerStartSize == 5 ? "$ENV{" : "${");
  1647. }
  1648. currentPos = markerPos;
  1649. }
  1650. else
  1651. {
  1652. // good variable remove it
  1653. std::string var =
  1654. source.substr(markerPos, endVariablePos - markerPos);
  1655. bool found = false;
  1656. if (markerStartSize == 5) // $ENV{
  1657. {
  1658. char *ptr = getenv(var.c_str());
  1659. if (ptr)
  1660. {
  1661. if (escapeQuotes)
  1662. {
  1663. result += cmSystemTools::EscapeQuotes(ptr);
  1664. }
  1665. else
  1666. {
  1667. result += ptr;
  1668. }
  1669. found = true;
  1670. }
  1671. }
  1672. else
  1673. {
  1674. const char* lookup = this->GetDefinition(var.c_str());
  1675. if(lookup)
  1676. {
  1677. if (escapeQuotes)
  1678. {
  1679. result += cmSystemTools::EscapeQuotes(lookup);
  1680. }
  1681. else
  1682. {
  1683. result += lookup;
  1684. }
  1685. found = true;
  1686. }
  1687. else if(filename && (var == "CMAKE_CURRENT_LIST_FILE"))
  1688. {
  1689. result += filename;
  1690. found = true;
  1691. }
  1692. else if(line >= 0 && (var == "CMAKE_CURRENT_LIST_LINE"))
  1693. {
  1694. cmOStringStream ostr;
  1695. ostr << line;
  1696. result += ostr.str();
  1697. found = true;
  1698. }
  1699. }
  1700. // if found add to result, if not, then it gets blanked
  1701. if (!found)
  1702. {
  1703. // if no definition is found then add the var back
  1704. if(!removeEmpty && endVariableMarker == '@')
  1705. {
  1706. result += "@";
  1707. result += var;
  1708. result += "@";
  1709. }
  1710. }
  1711. // lookup var, and replace it
  1712. currentPos = endVariablePos+1;
  1713. }
  1714. }
  1715. if(atOnly)
  1716. {
  1717. markerPos = source.find_first_of("@", currentPos);
  1718. }
  1719. else
  1720. {
  1721. markerPos = source.find_first_of("$@", currentPos);
  1722. }
  1723. }
  1724. result += source.substr(currentPos); // pick up the rest of the string
  1725. source = result;
  1726. }
  1727. return source.c_str();
  1728. }
  1729. void cmMakefile::RemoveVariablesInString(std::string& source,
  1730. bool atOnly) const
  1731. {
  1732. if(!atOnly)
  1733. {
  1734. cmsys::RegularExpression var("(\\${[A-Za-z_0-9]*})");
  1735. while (var.find(source))
  1736. {
  1737. source.erase(var.start(),var.end() - var.start());
  1738. }
  1739. }
  1740. if(!atOnly)
  1741. {
  1742. cmsys::RegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  1743. while (varb.find(source))
  1744. {
  1745. source.erase(varb.start(),varb.end() - varb.start());
  1746. }
  1747. }
  1748. cmsys::RegularExpression var2("(@[A-Za-z_0-9]*@)");
  1749. while (var2.find(source))
  1750. {
  1751. source.erase(var2.start(),var2.end() - var2.start());
  1752. }
  1753. }
  1754. /**
  1755. * Add the default definitions to the makefile. These values must not
  1756. * be dependent on anything that isn't known when this cmMakefile instance
  1757. * is constructed.
  1758. */
  1759. void cmMakefile::AddDefaultDefinitions()
  1760. {
  1761. #if defined(_WIN32) || defined(__CYGWIN__)
  1762. this->AddDefinition("WIN32", "1");
  1763. #else
  1764. this->AddDefinition("UNIX", "1");
  1765. #endif
  1766. // Cygwin is more like unix so enable the unix commands
  1767. #if defined(__CYGWIN__)
  1768. this->AddDefinition("UNIX", "1");
  1769. this->AddDefinition("CYGWIN", "1");
  1770. #endif
  1771. #if defined(__APPLE__)
  1772. this->AddDefinition("APPLE", "1");
  1773. #endif
  1774. #if defined(__QNXNTO__)
  1775. this->AddDefinition("QNXNTO", "1");
  1776. #endif
  1777. char temp[1024];
  1778. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1779. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1780. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1781. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1782. sprintf(temp, "%d", cmMakefile::GetPatchVersion());
  1783. this->AddDefinition("CMAKE_PATCH_VERSION", temp);
  1784. this->AddDefinition("CMAKE_FILES_DIRECTORY",
  1785. cmake::GetCMakeFilesDirectory());
  1786. }
  1787. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1788. /**
  1789. * Find a source group whose regular expression matches the filename
  1790. * part of the given source name. Search backward through the list of
  1791. * source groups, and take the first matching group found. This way
  1792. * non-inherited SOURCE_GROUP commands will have precedence over
  1793. * inherited ones.
  1794. */
  1795. cmSourceGroup&
  1796. cmMakefile::FindSourceGroup(const char* source,
  1797. std::vector<cmSourceGroup> &groups)
  1798. {
  1799. // First search for a group that lists the file explicitly.
  1800. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1801. sg != groups.rend(); ++sg)
  1802. {
  1803. cmSourceGroup *result = sg->MatchChildrenFiles(source);
  1804. if(result)
  1805. {
  1806. return *result;
  1807. }
  1808. }
  1809. // Now search for a group whose regex matches the file.
  1810. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1811. sg != groups.rend(); ++sg)
  1812. {
  1813. cmSourceGroup *result = sg->MatchChildrenRegex(source);
  1814. if(result)
  1815. {
  1816. return *result;
  1817. }
  1818. }
  1819. // Shouldn't get here, but just in case, return the default group.
  1820. return groups.front();
  1821. }
  1822. #endif
  1823. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff)
  1824. {
  1825. // if there are no blockers get out of here
  1826. if (this->FunctionBlockers.begin() == this->FunctionBlockers.end())
  1827. {
  1828. return false;
  1829. }
  1830. // loop over all function blockers to see if any block this command
  1831. // evaluate in reverse, this is critical for balanced IF statements etc
  1832. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1833. for (pos = this->FunctionBlockers.rbegin();
  1834. pos != this->FunctionBlockers.rend(); ++pos)
  1835. {
  1836. if((*pos)->IsFunctionBlocked(lff, *this))
  1837. {
  1838. return true;
  1839. }
  1840. }
  1841. return false;
  1842. }
  1843. void cmMakefile::ExpandArguments(
  1844. std::vector<cmListFileArgument> const& inArgs,
  1845. std::vector<std::string>& outArgs)
  1846. {
  1847. std::vector<cmListFileArgument>::const_iterator i;
  1848. std::string value;
  1849. outArgs.reserve(inArgs.size());
  1850. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  1851. {
  1852. // Expand the variables in the argument.
  1853. value = i->Value;
  1854. this->ExpandVariablesInString(value, false, false, false,
  1855. i->FilePath, i->Line);
  1856. // If the argument is quoted, it should be one argument.
  1857. // Otherwise, it may be a list of arguments.
  1858. if(i->Quoted)
  1859. {
  1860. outArgs.push_back(value);
  1861. }
  1862. else
  1863. {
  1864. cmSystemTools::ExpandListArgument(value, outArgs);
  1865. }
  1866. }
  1867. }
  1868. void cmMakefile::RemoveFunctionBlocker(const cmListFileFunction& lff)
  1869. {
  1870. // loop over all function blockers to see if any block this command
  1871. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1872. for (pos = this->FunctionBlockers.rbegin();
  1873. pos != this->FunctionBlockers.rend(); ++pos)
  1874. {
  1875. if ((*pos)->ShouldRemove(lff, *this))
  1876. {
  1877. cmFunctionBlocker* b = *pos;
  1878. this->FunctionBlockers.remove(b);
  1879. delete b;
  1880. break;
  1881. }
  1882. }
  1883. return;
  1884. }
  1885. void cmMakefile::SetHomeDirectory(const char* dir)
  1886. {
  1887. this->cmHomeDirectory = dir;
  1888. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1889. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1890. if ( !this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR") )
  1891. {
  1892. this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", this->GetHomeDirectory());
  1893. }
  1894. }
  1895. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1896. {
  1897. this->HomeOutputDirectory = lib;
  1898. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1899. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1900. if ( !this->GetDefinition("CMAKE_CURRENT_BINARY_DIR") )
  1901. {
  1902. this->AddDefinition("CMAKE_CURRENT_BINARY_DIR",
  1903. this->GetHomeOutputDirectory());
  1904. }
  1905. }
  1906. /**
  1907. * Register the given cmData instance with its own name.
  1908. */
  1909. void cmMakefile::RegisterData(cmData* data)
  1910. {
  1911. std::string name = data->GetName();
  1912. DataMapType::const_iterator d = this->DataMap.find(name);
  1913. if((d != this->DataMap.end()) && (d->second != 0) && (d->second != data))
  1914. {
  1915. delete d->second;
  1916. }
  1917. this->DataMap[name] = data;
  1918. }
  1919. /**
  1920. * Register the given cmData instance with the given name. This can be used
  1921. * to register a NULL pointer.
  1922. */
  1923. void cmMakefile::RegisterData(const char* name, cmData* data)
  1924. {
  1925. DataMapType::const_iterator d = this->DataMap.find(name);
  1926. if((d != this->DataMap.end()) && (d->second != 0) && (d->second != data))
  1927. {
  1928. delete d->second;
  1929. }
  1930. this->DataMap[name] = data;
  1931. }
  1932. /**
  1933. * Lookup a cmData instance previously registered with the given name. If
  1934. * the instance cannot be found, return NULL.
  1935. */
  1936. cmData* cmMakefile::LookupData(const char* name) const
  1937. {
  1938. DataMapType::const_iterator d = this->DataMap.find(name);
  1939. if(d != this->DataMap.end())
  1940. {
  1941. return d->second;
  1942. }
  1943. else
  1944. {
  1945. return 0;
  1946. }
  1947. }
  1948. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  1949. {
  1950. // if the source is provided with a full path use it, otherwise
  1951. // by default it is in the current source dir
  1952. std::string path;
  1953. if (cmSystemTools::FileIsFullPath(sourceName))
  1954. {
  1955. path = cmSystemTools::GetFilenamePath(sourceName);
  1956. }
  1957. else
  1958. {
  1959. path = this->GetCurrentDirectory();
  1960. // even though it is not a full path, it may still be relative
  1961. std::string subpath = cmSystemTools::GetFilenamePath(sourceName);
  1962. if (!subpath.empty())
  1963. {
  1964. path += "/";
  1965. path += cmSystemTools::GetFilenamePath(sourceName);
  1966. }
  1967. }
  1968. std::string sname =
  1969. cmSystemTools::GetFilenameWithoutLastExtension(sourceName);
  1970. // compute the extension
  1971. std::string ext
  1972. = cmSystemTools::GetFilenameLastExtension(sourceName);
  1973. if ( ext.length() && ext[0] == '.' )
  1974. {
  1975. ext = ext.substr(1);
  1976. }
  1977. for(std::vector<cmSourceFile*>::const_iterator i =
  1978. this->SourceFiles.begin();
  1979. i != this->SourceFiles.end(); ++i)
  1980. {
  1981. if ((*i)->GetSourceNameWithoutLastExtension() == sname &&
  1982. cmSystemTools::GetFilenamePath((*i)->GetFullPath()) == path &&
  1983. (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1984. {
  1985. return *i;
  1986. }
  1987. }
  1988. // geeze, if it wasn't found maybe it is listed under the output dir
  1989. if (!cmSystemTools::GetFilenamePath(sourceName).empty())
  1990. {
  1991. return 0;
  1992. }
  1993. path = this->GetCurrentOutputDirectory();
  1994. for(std::vector<cmSourceFile*>::const_iterator i =
  1995. this->SourceFiles.begin();
  1996. i != this->SourceFiles.end(); ++i)
  1997. {
  1998. if ((*i)->GetSourceName() == sname &&
  1999. cmSystemTools::GetFilenamePath((*i)->GetFullPath()) == path &&
  2000. (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  2001. {
  2002. return *i;
  2003. }
  2004. }
  2005. return 0;
  2006. }
  2007. cmSourceFile* cmMakefile::GetOrCreateSource(const char* sourceName,
  2008. bool generated)
  2009. {
  2010. // make it a full path first
  2011. std::string src = sourceName;
  2012. bool relative = !cmSystemTools::FileIsFullPath(sourceName);
  2013. std::string srcTreeFile = this->GetCurrentDirectory();
  2014. srcTreeFile += "/";
  2015. srcTreeFile += sourceName;
  2016. if(relative)
  2017. {
  2018. src = srcTreeFile;
  2019. }
  2020. // check to see if it exists
  2021. cmSourceFile* ret = this->GetSource(src.c_str());
  2022. if (ret)
  2023. {
  2024. return ret;
  2025. }
  2026. // OK a source file object doesn't exist for the source
  2027. // maybe we made a bad call on assuming it was in the src tree
  2028. std::string buildTreeFile = this->GetCurrentOutputDirectory();
  2029. buildTreeFile += "/";
  2030. buildTreeFile += sourceName;
  2031. if (relative)
  2032. {
  2033. src = buildTreeFile;
  2034. ret = this->GetSource(src.c_str());
  2035. if (ret)
  2036. {
  2037. return ret;
  2038. }
  2039. // if it has not been marked generated check to see if it exists in the
  2040. // src tree
  2041. if(!generated)
  2042. {
  2043. // see if the file is in the source tree, otherwise assume it
  2044. // is in the binary tree
  2045. if (cmSystemTools::FileExists(srcTreeFile.c_str()) &&
  2046. !cmSystemTools::FileIsDirectory(srcTreeFile.c_str()))
  2047. {
  2048. src = srcTreeFile;
  2049. }
  2050. else
  2051. {
  2052. if ( cmSystemTools::GetFilenameLastExtension
  2053. (srcTreeFile.c_str()).size() == 0)
  2054. {
  2055. if (cmSystemTools::DoesFileExistWithExtensions(
  2056. srcTreeFile.c_str(), this->GetSourceExtensions()))
  2057. {
  2058. src = srcTreeFile;
  2059. }
  2060. else if (cmSystemTools::DoesFileExistWithExtensions(
  2061. srcTreeFile.c_str(), this->GetHeaderExtensions()))
  2062. {
  2063. src = srcTreeFile;
  2064. }
  2065. }
  2066. }
  2067. }
  2068. }
  2069. // a cmSourceFile instance does not exist yet so we must create one
  2070. // go back to looking in the source directory for it
  2071. // we must create one
  2072. cmSourceFile file;
  2073. std::string path = cmSystemTools::GetFilenamePath(src);
  2074. if(generated)
  2075. {
  2076. std::string ext = cmSystemTools::GetFilenameLastExtension(src);
  2077. std::string name_no_ext = cmSystemTools::GetFilenameName(src.c_str());
  2078. name_no_ext = name_no_ext.substr(0, name_no_ext.length()-ext.length());
  2079. if ( ext.length() && ext[0] == '.' )
  2080. {
  2081. ext = ext.substr(1);
  2082. }
  2083. bool headerFile =
  2084. !(std::find( this->HeaderFileExtensions.begin(),
  2085. this->HeaderFileExtensions.end(), ext ) ==
  2086. this->HeaderFileExtensions.end());
  2087. file.SetName(name_no_ext.c_str(), path.c_str(), ext.c_str(), headerFile);
  2088. }
  2089. else
  2090. {
  2091. std::string relPath = cmSystemTools::GetFilenamePath(sourceName);
  2092. if (relative && relPath.size())
  2093. {
  2094. // we need to keep the relative part of the filename
  2095. std::string fullPathLessRel = path;
  2096. std::string::size_type pos = fullPathLessRel.rfind(relPath);
  2097. if (pos == std::string::npos)
  2098. {
  2099. cmSystemTools::Error(
  2100. "CMake failed to properly look up relative cmSourceFile: ",
  2101. sourceName);
  2102. }
  2103. fullPathLessRel.erase(pos-1);
  2104. file.SetName(sourceName, fullPathLessRel.c_str(),
  2105. this->GetSourceExtensions(),
  2106. this->GetHeaderExtensions());
  2107. }
  2108. else
  2109. {
  2110. file.SetName(cmSystemTools::GetFilenameName(src.c_str()).c_str(),
  2111. path.c_str(),
  2112. this->GetSourceExtensions(),
  2113. this->GetHeaderExtensions());
  2114. }
  2115. }
  2116. // add the source file to the makefile
  2117. this->AddSource(file);
  2118. src = file.GetFullPath();
  2119. ret = this->GetSource(src.c_str());
  2120. if (!ret)
  2121. {
  2122. cmSystemTools::Error(
  2123. "CMake failed to properly look up cmSourceFile: ", sourceName);
  2124. }
  2125. return ret;
  2126. }
  2127. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  2128. {
  2129. // check to see if it exists
  2130. cmSourceFile* ret = this->GetSource(sf.GetFullPath().c_str());
  2131. if(ret)
  2132. {
  2133. return ret;
  2134. }
  2135. ret = new cmSourceFile(sf);
  2136. this->SourceFiles.push_back(ret);
  2137. return ret;
  2138. }
  2139. void cmMakefile::EnableLanguage(std::vector<std::string> const & lang)
  2140. {
  2141. this->AddDefinition("CMAKE_CFG_INTDIR",
  2142. this->LocalGenerator->GetGlobalGenerator()->GetCMakeCFGInitDirectory());
  2143. this->LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this);
  2144. }
  2145. void cmMakefile::ExpandSourceListArguments(
  2146. std::vector<std::string> const& arguments,
  2147. std::vector<std::string>& newargs, unsigned int /* start */)
  2148. {
  2149. // now expand the args
  2150. unsigned int i;
  2151. for(i = 0; i < arguments.size(); ++i)
  2152. {
  2153. // List expansion will have been done already.
  2154. newargs.push_back(arguments[i]);
  2155. }
  2156. }
  2157. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  2158. const char *projectName, const char *targetName,
  2159. const std::vector<std::string> *cmakeArgs,
  2160. std::string *output)
  2161. {
  2162. // does the binary directory exist ? If not create it...
  2163. if (!cmSystemTools::FileIsDirectory(bindir))
  2164. {
  2165. cmSystemTools::MakeDirectory(bindir);
  2166. }
  2167. // change to the tests directory and run cmake
  2168. // use the cmake object instead of calling cmake
  2169. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2170. cmSystemTools::ChangeDirectory(bindir);
  2171. // make sure the same generator is used
  2172. // use this program as the cmake to be run, it should not
  2173. // be run that way but the cmake object requires a vailid path
  2174. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  2175. cmake cm;
  2176. cm.SetIsInTryCompile(true);
  2177. cmGlobalGenerator *gg = cm.CreateGlobalGenerator
  2178. (this->LocalGenerator->GetGlobalGenerator()->GetName());
  2179. if (!gg)
  2180. {
  2181. cmSystemTools::Error(
  2182. "Internal CMake error, TryCompile bad GlobalGenerator");
  2183. // return to the original directory
  2184. cmSystemTools::ChangeDirectory(cwd.c_str());
  2185. return 1;
  2186. }
  2187. cm.SetGlobalGenerator(gg);
  2188. // do a configure
  2189. cm.SetHomeDirectory(srcdir);
  2190. cm.SetHomeOutputDirectory(bindir);
  2191. cm.SetStartDirectory(srcdir);
  2192. cm.SetStartOutputDirectory(bindir);
  2193. cm.SetCMakeCommand(cmakeCommand.c_str());
  2194. cm.LoadCache();
  2195. // if cmake args were provided then pass them in
  2196. if (cmakeArgs)
  2197. {
  2198. cm.SetCacheArgs(*cmakeArgs);
  2199. }
  2200. // to save time we pass the EnableLanguage info directly
  2201. gg->EnableLanguagesFromGenerator
  2202. (this->LocalGenerator->GetGlobalGenerator());
  2203. if (cm.Configure() != 0)
  2204. {
  2205. cmSystemTools::Error(
  2206. "Internal CMake error, TryCompile configure of cmake failed");
  2207. // return to the original directory
  2208. cmSystemTools::ChangeDirectory(cwd.c_str());
  2209. return 1;
  2210. }
  2211. if (cm.Generate() != 0)
  2212. {
  2213. cmSystemTools::Error(
  2214. "Internal CMake error, TryCompile generation of cmake failed");
  2215. // return to the original directory
  2216. cmSystemTools::ChangeDirectory(cwd.c_str());
  2217. return 1;
  2218. }
  2219. // finally call the generator to actually build the resulting project
  2220. int ret =
  2221. this->LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  2222. projectName,
  2223. targetName,
  2224. output,
  2225. this);
  2226. cmSystemTools::ChangeDirectory(cwd.c_str());
  2227. return ret;
  2228. }
  2229. cmake *cmMakefile::GetCMakeInstance() const
  2230. {
  2231. if ( this->LocalGenerator && this->LocalGenerator->GetGlobalGenerator() )
  2232. {
  2233. return this->LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  2234. }
  2235. return 0;
  2236. }
  2237. #ifdef CMAKE_BUILD_WITH_CMAKE
  2238. cmVariableWatch *cmMakefile::GetVariableWatch() const
  2239. {
  2240. if ( this->GetCMakeInstance() &&
  2241. this->GetCMakeInstance()->GetVariableWatch() )
  2242. {
  2243. return this->GetCMakeInstance()->GetVariableWatch();
  2244. }
  2245. return 0;
  2246. }
  2247. #endif
  2248. void cmMakefile::AddMacro(const char* name, const char* signature)
  2249. {
  2250. if ( !name || !signature )
  2251. {
  2252. return;
  2253. }
  2254. this->MacrosMap[name] = signature;
  2255. }
  2256. void cmMakefile::GetListOfMacros(std::string& macros)
  2257. {
  2258. StringStringMap::iterator it;
  2259. macros = "";
  2260. int cc = 0;
  2261. for ( it = this->MacrosMap.begin(); it != this->MacrosMap.end(); ++it )
  2262. {
  2263. if ( cc > 0 )
  2264. {
  2265. macros += ";";
  2266. }
  2267. macros += it->first;
  2268. cc ++;
  2269. }
  2270. }
  2271. cmCacheManager *cmMakefile::GetCacheManager() const
  2272. {
  2273. return this->GetCMakeInstance()->GetCacheManager();
  2274. }
  2275. void cmMakefile::DisplayStatus(const char* message, float s)
  2276. {
  2277. this->GetLocalGenerator()->GetGlobalGenerator()
  2278. ->GetCMakeInstance()->UpdateProgress(message, s);
  2279. }
  2280. std::string cmMakefile::GetModulesFile(const char* filename)
  2281. {
  2282. std::vector<std::string> modulePath;
  2283. const char* def = this->GetDefinition("CMAKE_MODULE_PATH");
  2284. if(def)
  2285. {
  2286. cmSystemTools::ExpandListArgument(def, modulePath);
  2287. }
  2288. // Also search in the standard modules location.
  2289. def = this->GetDefinition("CMAKE_ROOT");
  2290. if(def)
  2291. {
  2292. std::string rootModules = def;
  2293. rootModules += "/Modules";
  2294. modulePath.push_back(rootModules);
  2295. }
  2296. //std::string Look through the possible module directories.
  2297. for(std::vector<std::string>::iterator i = modulePath.begin();
  2298. i != modulePath.end(); ++i)
  2299. {
  2300. std::string itempl = *i;
  2301. cmSystemTools::ConvertToUnixSlashes(itempl);
  2302. itempl += "/";
  2303. itempl += filename;
  2304. if(cmSystemTools::FileExists(itempl.c_str()))
  2305. {
  2306. return itempl;
  2307. }
  2308. }
  2309. return "";
  2310. }
  2311. void cmMakefile::ConfigureString(const std::string& input,
  2312. std::string& output, bool atOnly,
  2313. bool escapeQuotes)
  2314. {
  2315. // Split input to handle one line at a time.
  2316. std::string::const_iterator lineStart = input.begin();
  2317. while(lineStart != input.end())
  2318. {
  2319. // Find the end of this line.
  2320. std::string::const_iterator lineEnd = lineStart;
  2321. while(lineEnd != input.end() && *lineEnd != '\n')
  2322. {
  2323. ++lineEnd;
  2324. }
  2325. // Copy the line.
  2326. std::string line(lineStart, lineEnd);
  2327. // Skip the newline character.
  2328. bool haveNewline = (lineEnd != input.end());
  2329. if(haveNewline)
  2330. {
  2331. ++lineEnd;
  2332. }
  2333. // Replace #cmakedefine instances.
  2334. if(this->cmDefineRegex.find(line))
  2335. {
  2336. const char* def =
  2337. this->GetDefinition(this->cmDefineRegex.match(1).c_str());
  2338. if(!cmSystemTools::IsOff(def))
  2339. {
  2340. cmSystemTools::ReplaceString(line, "#cmakedefine", "#define");
  2341. output += line;
  2342. }
  2343. else
  2344. {
  2345. cmSystemTools::ReplaceString(line, "#cmakedefine", "#undef");
  2346. output += "/* ";
  2347. output += line;
  2348. output += " */";
  2349. }
  2350. }
  2351. else if(this->cmDefine01Regex.find(line))
  2352. {
  2353. const char* def =
  2354. this->GetDefinition(this->cmDefine01Regex.match(1).c_str());
  2355. cmSystemTools::ReplaceString(line, "#cmakedefine01", "#define");
  2356. output += line;
  2357. if(!cmSystemTools::IsOff(def))
  2358. {
  2359. output += " 1";
  2360. }
  2361. else
  2362. {
  2363. output += " 0";
  2364. }
  2365. }
  2366. else
  2367. {
  2368. output += line;
  2369. }
  2370. if(haveNewline)
  2371. {
  2372. output += "\n";
  2373. }
  2374. // Move to the next line.
  2375. lineStart = lineEnd;
  2376. }
  2377. // Perform variable replacements.
  2378. this->ExpandVariablesInString(output, escapeQuotes, true,
  2379. atOnly, 0, -1, true);
  2380. }
  2381. int cmMakefile::ConfigureFile(const char* infile, const char* outfile,
  2382. bool copyonly, bool atOnly, bool escapeQuotes)
  2383. {
  2384. int res = 1;
  2385. if ( !this->CanIWriteThisFile(outfile) )
  2386. {
  2387. cmSystemTools::Error("Attempt to write file: ",
  2388. outfile, " into a source directory.");
  2389. return 0;
  2390. }
  2391. if ( !cmSystemTools::FileExists(infile) )
  2392. {
  2393. cmSystemTools::Error("File ", infile, " does not exist.");
  2394. return 0;
  2395. }
  2396. std::string soutfile = outfile;
  2397. std::string sinfile = infile;
  2398. this->AddCMakeDependFile(infile);
  2399. cmSystemTools::ConvertToUnixSlashes(soutfile);
  2400. mode_t perm = 0;
  2401. cmSystemTools::GetPermissions(sinfile.c_str(), perm);
  2402. std::string::size_type pos = soutfile.rfind('/');
  2403. if(pos != std::string::npos)
  2404. {
  2405. std::string path = soutfile.substr(0, pos);
  2406. cmSystemTools::MakeDirectory(path.c_str());
  2407. }
  2408. if(copyonly)
  2409. {
  2410. if ( !cmSystemTools::CopyFileIfDifferent(sinfile.c_str(),
  2411. soutfile.c_str()))
  2412. {
  2413. return 0;
  2414. }
  2415. }
  2416. else
  2417. {
  2418. std::string tempOutputFile = soutfile;
  2419. tempOutputFile += ".tmp";
  2420. std::ofstream fout(tempOutputFile.c_str());
  2421. if(!fout)
  2422. {
  2423. cmSystemTools::Error(
  2424. "Could not open file for write in copy operation ",
  2425. tempOutputFile.c_str());
  2426. cmSystemTools::ReportLastSystemError("");
  2427. return 0;
  2428. }
  2429. std::ifstream fin(sinfile.c_str());
  2430. if(!fin)
  2431. {
  2432. cmSystemTools::Error("Could not open file for read in copy operation ",
  2433. sinfile.c_str());
  2434. return 0;
  2435. }
  2436. // now copy input to output and expand variables in the
  2437. // input file at the same time
  2438. std::string inLine;
  2439. std::string outLine;
  2440. while( cmSystemTools::GetLineFromStream(fin, inLine) )
  2441. {
  2442. outLine = "";
  2443. this->ConfigureString(inLine, outLine, atOnly, escapeQuotes);
  2444. fout << outLine.c_str() << "\n";
  2445. }
  2446. // close the files before attempting to copy
  2447. fin.close();
  2448. fout.close();
  2449. if ( !cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  2450. soutfile.c_str()) )
  2451. {
  2452. res = 0;
  2453. }
  2454. else
  2455. {
  2456. cmSystemTools::SetPermissions(soutfile.c_str(), perm);
  2457. }
  2458. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  2459. }
  2460. return res;
  2461. }
  2462. void cmMakefile::AddWrittenFile(const char* file)
  2463. { this->GetCMakeInstance()->AddWrittenFile(file); }
  2464. bool cmMakefile::HasWrittenFile(const char* file)
  2465. { return this->GetCMakeInstance()->HasWrittenFile(file); }
  2466. bool cmMakefile::CheckInfiniteLoops()
  2467. {
  2468. std::vector<std::string>::iterator it;
  2469. for ( it = this->ListFiles.begin();
  2470. it != this->ListFiles.end();
  2471. ++ it )
  2472. {
  2473. if ( this->HasWrittenFile(it->c_str()) )
  2474. {
  2475. cmOStringStream str;
  2476. str << "File " << it->c_str() <<
  2477. " is written by WRITE_FILE (or FILE WRITE) command and should "
  2478. "not be used as input to CMake. Please use CONFIGURE_FILE to "
  2479. "be safe. Refer to the note next to FILE WRITE command.";
  2480. cmSystemTools::Error(str.str().c_str());
  2481. return false;
  2482. }
  2483. }
  2484. return true;
  2485. }
  2486. void cmMakefile::SetProperty(const char* prop, const char* value)
  2487. {
  2488. if (!prop)
  2489. {
  2490. return;
  2491. }
  2492. if (!value)
  2493. {
  2494. value = "NOTFOUND";
  2495. }
  2496. this->Properties[prop] = value;
  2497. }
  2498. const char *cmMakefile::GetProperty(const char* prop)
  2499. {
  2500. // watch for specific properties
  2501. if (!strcmp("PARENT_DIRECTORY",prop))
  2502. {
  2503. return this->LocalGenerator->GetParent()
  2504. ->GetMakefile()->GetStartDirectory();
  2505. }
  2506. // watch for specific properties
  2507. if (!strcmp("LISTFILE_STACK",prop))
  2508. {
  2509. std::string tmp;
  2510. for (std::deque<cmStdString>::iterator i = this->ListFileStack.begin();
  2511. i != this->ListFileStack.end(); ++i)
  2512. {
  2513. if (i != this->ListFileStack.begin())
  2514. {
  2515. tmp += ";";
  2516. }
  2517. tmp += *i;
  2518. }
  2519. this->SetProperty("LISTFILE_STACK",tmp.c_str());
  2520. }
  2521. std::map<cmStdString,cmStdString>::const_iterator i =
  2522. this->Properties.find(prop);
  2523. if (i != this->Properties.end())
  2524. {
  2525. return i->second.c_str();
  2526. }
  2527. return 0;
  2528. }
  2529. bool cmMakefile::GetPropertyAsBool(const char* prop) const
  2530. {
  2531. std::map<cmStdString,cmStdString>::const_iterator i =
  2532. this->Properties.find(prop);
  2533. if (i != this->Properties.end())
  2534. {
  2535. return cmSystemTools::IsOn(i->second.c_str());
  2536. }
  2537. return false;
  2538. }
  2539. cmTarget* cmMakefile::FindTarget(const char* name)
  2540. {
  2541. cmTargets& tgts = this->GetTargets();
  2542. cmTargets::iterator i = tgts.find(name);
  2543. if (i == tgts.end())
  2544. {
  2545. return 0;
  2546. }
  2547. return &i->second;
  2548. }
  2549. cmTest* cmMakefile::CreateTest(const char* testName)
  2550. {
  2551. if ( !testName )
  2552. {
  2553. return 0;
  2554. }
  2555. cmTest* test = this->GetTest(testName);
  2556. if ( test )
  2557. {
  2558. return test;
  2559. }
  2560. test = new cmTest;
  2561. test->SetName(testName);
  2562. this->Tests.push_back(test);
  2563. return test;
  2564. }
  2565. cmTest* cmMakefile::GetTest(const char* testName) const
  2566. {
  2567. if ( !testName )
  2568. {
  2569. return 0;
  2570. }
  2571. std::vector<cmTest*>::const_iterator it;
  2572. for ( it = this->Tests.begin(); it != this->Tests.end(); ++ it )
  2573. {
  2574. if ( strcmp((*it)->GetName(), testName) == 0 )
  2575. {
  2576. return *it;
  2577. }
  2578. }
  2579. return 0;
  2580. }
  2581. const std::vector<cmTest*> *cmMakefile::GetTests() const
  2582. {
  2583. return &this->Tests;
  2584. }
  2585. std::vector<cmTest*> *cmMakefile::GetTests()
  2586. {
  2587. return &this->Tests;
  2588. }
  2589. std::string cmMakefile::GetListFileStack()
  2590. {
  2591. std::string tmp;
  2592. for (std::deque<cmStdString>::iterator i = this->ListFileStack.begin();
  2593. i != this->ListFileStack.end(); ++i)
  2594. {
  2595. if (i != this->ListFileStack.begin())
  2596. {
  2597. tmp += ";";
  2598. }
  2599. tmp += *i;
  2600. }
  2601. return tmp;
  2602. }