1
0

cmMakefile.cxx 62 KB

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