cmMakefile.cxx 72 KB

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