cmMakefile.cxx 78 KB

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