cmMakefile.cxx 69 KB

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