cmMakefile.cxx 64 KB

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