cmMakefile.cxx 70 KB

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