cmMakefile.cxx 62 KB

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