cmMakefile.cxx 60 KB

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