cmMakefile.cxx 58 KB

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