cmMakefile.cxx 71 KB

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