cmMakefile.cxx 68 KB

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