cmMakefile.cxx 61 KB

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