cmMakefile.cxx 70 KB

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