cmMakefile.cxx 77 KB

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