cmMakefile.cxx 60 KB

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