cmMakefile.cxx 69 KB

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