cmMakefile.cxx 63 KB

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