cmLocalGenerator.cxx 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820
  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 "cmLocalGenerator.h"
  14. #include "cmComputeLinkInformation.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGlobalGenerator.h"
  17. #include "cmInstallGenerator.h"
  18. #include "cmInstallFilesGenerator.h"
  19. #include "cmInstallScriptGenerator.h"
  20. #include "cmInstallTargetGenerator.h"
  21. #include "cmMakefile.h"
  22. #include "cmSourceFile.h"
  23. #include "cmTest.h"
  24. #include "cmake.h"
  25. #if defined(CMAKE_BUILD_WITH_CMAKE)
  26. # define CM_LG_ENCODE_OBJECT_NAMES
  27. # include <cmsys/MD5.h>
  28. #endif
  29. #include <cmsys/System.h>
  30. #include <ctype.h> // for isalpha
  31. #include <assert.h>
  32. cmLocalGenerator::cmLocalGenerator()
  33. {
  34. this->Makefile = new cmMakefile;
  35. this->Makefile->SetLocalGenerator(this);
  36. this->Parent = 0;
  37. this->WindowsShell = false;
  38. this->WindowsVSIDE = false;
  39. this->WatcomWMake = false;
  40. this->MinGWMake = false;
  41. this->NMake = false;
  42. this->MSYSShell = false;
  43. this->IgnoreLibPrefix = false;
  44. this->UseRelativePaths = false;
  45. this->Configured = false;
  46. this->EmitUniversalBinaryFlags = true;
  47. this->IsMakefileGenerator = false;
  48. this->RelativePathsConfigured = false;
  49. this->PathConversionsSetup = false;
  50. this->BackwardsCompatibility = 0;
  51. this->BackwardsCompatibilityFinal = false;
  52. }
  53. cmLocalGenerator::~cmLocalGenerator()
  54. {
  55. delete this->Makefile;
  56. }
  57. void cmLocalGenerator::Configure()
  58. {
  59. cmLocalGenerator* previousLg =
  60. this->GetGlobalGenerator()->GetCurrentLocalGenerator();
  61. this->GetGlobalGenerator()->SetCurrentLocalGenerator(this);
  62. // make sure the CMakeFiles dir is there
  63. std::string filesDir = this->Makefile->GetStartOutputDirectory();
  64. filesDir += cmake::GetCMakeFilesDirectory();
  65. cmSystemTools::MakeDirectory(filesDir.c_str());
  66. // find & read the list file
  67. std::string currentStart = this->Makefile->GetStartDirectory();
  68. currentStart += "/CMakeLists.txt";
  69. this->Makefile->ReadListFile(currentStart.c_str());
  70. // at the end of the ReadListFile handle any old style subdirs
  71. // first get all the subdirectories
  72. std::vector<cmLocalGenerator *> subdirs = this->GetChildren();
  73. // for each subdir recurse
  74. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  75. for (; sdi != subdirs.end(); ++sdi)
  76. {
  77. if (!(*sdi)->Configured)
  78. {
  79. this->Makefile->ConfigureSubDirectory(*sdi);
  80. }
  81. }
  82. // Check whether relative paths should be used for optionally
  83. // relative paths.
  84. this->UseRelativePaths = this->Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS");
  85. this->Configured = true;
  86. this->GetGlobalGenerator()->SetCurrentLocalGenerator(previousLg);
  87. }
  88. void cmLocalGenerator::SetupPathConversions()
  89. {
  90. // Setup the current output directory components for use by
  91. // Convert
  92. std::string outdir;
  93. outdir =
  94. cmSystemTools::CollapseFullPath(this->Makefile->GetHomeDirectory());
  95. cmSystemTools::SplitPath(outdir.c_str(), this->HomeDirectoryComponents);
  96. outdir =
  97. cmSystemTools::CollapseFullPath(this->Makefile->GetStartDirectory());
  98. cmSystemTools::SplitPath(outdir.c_str(), this->StartDirectoryComponents);
  99. outdir = cmSystemTools::CollapseFullPath
  100. (this->Makefile->GetHomeOutputDirectory());
  101. cmSystemTools::SplitPath(outdir.c_str(),
  102. this->HomeOutputDirectoryComponents);
  103. outdir = cmSystemTools::CollapseFullPath
  104. (this->Makefile->GetStartOutputDirectory());
  105. cmSystemTools::SplitPath(outdir.c_str(),
  106. this->StartOutputDirectoryComponents);
  107. }
  108. void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator *gg)
  109. {
  110. this->GlobalGenerator = gg;
  111. // setup the home directories
  112. this->Makefile->GetProperties().SetCMakeInstance(gg->GetCMakeInstance());
  113. this->Makefile->SetHomeDirectory(
  114. gg->GetCMakeInstance()->GetHomeDirectory());
  115. this->Makefile->SetHomeOutputDirectory(
  116. gg->GetCMakeInstance()->GetHomeOutputDirectory());
  117. }
  118. void cmLocalGenerator::ConfigureFinalPass()
  119. {
  120. this->Makefile->ConfigureFinalPass();
  121. }
  122. void cmLocalGenerator::TraceDependencies()
  123. {
  124. // Generate the rule files for each target.
  125. cmTargets& targets = this->Makefile->GetTargets();
  126. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  127. {
  128. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  129. // so don't build a projectfile for it
  130. if (strncmp(t->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0)
  131. {
  132. const char* projectFilename = 0;
  133. if (this->IsMakefileGenerator == false) // only use of this variable
  134. {
  135. projectFilename = t->second.GetName();
  136. }
  137. t->second.TraceDependencies(projectFilename);
  138. }
  139. }
  140. }
  141. void cmLocalGenerator::GenerateTestFiles()
  142. {
  143. if ( !this->Makefile->IsOn("CMAKE_TESTING_ENABLED") )
  144. {
  145. return;
  146. }
  147. std::string file = this->Makefile->GetStartOutputDirectory();
  148. file += "/";
  149. file += "CTestTestfile.cmake";
  150. cmGeneratedFileStream fout(file.c_str());
  151. fout.SetCopyIfDifferent(true);
  152. fout << "# CMake generated Testfile for " << std::endl
  153. << "# Source directory: "
  154. << this->Makefile->GetStartDirectory() << std::endl
  155. << "# Build directory: "
  156. << this->Makefile->GetStartOutputDirectory() << std::endl
  157. << "# " << std::endl
  158. << "# This file replicates the SUBDIRS() and ADD_TEST() commands "
  159. << "from the source" << std::endl
  160. << "# tree CMakeLists.txt file, skipping any SUBDIRS() or "
  161. << "ADD_TEST() commands" << std::endl
  162. << "# that are excluded by CMake control structures, i.e. IF() "
  163. << "commands." << std::endl;
  164. const char* testIncludeFile =
  165. this->Makefile->GetProperty("TEST_INCLUDE_FILE");
  166. if ( testIncludeFile )
  167. {
  168. fout << "INCLUDE(\"" << testIncludeFile << "\")" << std::endl;
  169. }
  170. const std::vector<cmTest*> *tests = this->Makefile->GetTests();
  171. std::vector<cmTest*>::const_iterator it;
  172. for ( it = tests->begin(); it != tests->end(); ++ it )
  173. {
  174. cmTest* test = *it;
  175. fout << "ADD_TEST(";
  176. fout << test->GetName() << " \"" << test->GetCommand() << "\"";
  177. std::vector<cmStdString>::const_iterator argit;
  178. for (argit = test->GetArguments().begin();
  179. argit != test->GetArguments().end(); ++argit)
  180. {
  181. // Just double-quote all arguments so they are re-parsed
  182. // correctly by the test system.
  183. fout << " \"";
  184. for(std::string::const_iterator c = argit->begin();
  185. c != argit->end(); ++c)
  186. {
  187. // Escape quotes within arguments. We should escape
  188. // backslashes too but we cannot because it makes the result
  189. // inconsistent with previous behavior of this command.
  190. if((*c == '"'))
  191. {
  192. fout << '\\';
  193. }
  194. fout << *c;
  195. }
  196. fout << "\"";
  197. }
  198. fout << ")" << std::endl;
  199. cmPropertyMap::const_iterator pit;
  200. cmPropertyMap* mpit = &test->GetProperties();
  201. if ( mpit->size() )
  202. {
  203. fout << "SET_TESTS_PROPERTIES(" << test->GetName() << " PROPERTIES ";
  204. for ( pit = mpit->begin(); pit != mpit->end(); ++ pit )
  205. {
  206. fout << " " << pit->first.c_str() << " \"";
  207. const char* value = pit->second.GetValue();
  208. for ( ; *value; ++ value )
  209. {
  210. switch ( *value )
  211. {
  212. case '\\':
  213. case '"':
  214. case ' ':
  215. case '#':
  216. case '(':
  217. case ')':
  218. case '$':
  219. case '^':
  220. fout << "\\" << *value;
  221. break;
  222. case '\t':
  223. fout << "\\t";
  224. break;
  225. case '\n':
  226. fout << "\\n";
  227. break;
  228. case '\r':
  229. fout << "\\r";
  230. break;
  231. default:
  232. fout << *value;
  233. }
  234. }
  235. fout << "\"";
  236. }
  237. fout << ")" << std::endl;
  238. }
  239. }
  240. if ( this->Children.size())
  241. {
  242. size_t i;
  243. for(i = 0; i < this->Children.size(); ++i)
  244. {
  245. fout << "SUBDIRS(";
  246. std::string outP =
  247. this->Children[i]->GetMakefile()->GetStartOutputDirectory();
  248. fout << this->Convert(outP.c_str(),START_OUTPUT);
  249. fout << ")" << std::endl;
  250. }
  251. }
  252. }
  253. //----------------------------------------------------------------------------
  254. void cmLocalGenerator::GenerateInstallRules()
  255. {
  256. // Compute the install prefix.
  257. const char* prefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  258. #if defined(_WIN32) && !defined(__CYGWIN__)
  259. std::string prefix_win32;
  260. if(!prefix)
  261. {
  262. if(!cmSystemTools::GetEnv("SystemDrive", prefix_win32))
  263. {
  264. prefix_win32 = "C:";
  265. }
  266. const char* project_name = this->Makefile->GetDefinition("PROJECT_NAME");
  267. if(project_name && project_name[0])
  268. {
  269. prefix_win32 += "/Program Files/";
  270. prefix_win32 += project_name;
  271. }
  272. else
  273. {
  274. prefix_win32 += "/InstalledCMakeProject";
  275. }
  276. prefix = prefix_win32.c_str();
  277. }
  278. #else
  279. if (!prefix)
  280. {
  281. prefix = "/usr/local";
  282. }
  283. #endif
  284. // Compute the set of configurations.
  285. std::vector<std::string> configurationTypes;
  286. if(const char* types =
  287. this->Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  288. {
  289. cmSystemTools::ExpandListArgument(types, configurationTypes);
  290. }
  291. const char* config = 0;
  292. if(configurationTypes.empty())
  293. {
  294. config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  295. }
  296. // Choose a default install configuration.
  297. const char* default_config = config;
  298. const char* default_order[] = {"RELEASE", "MINSIZEREL",
  299. "RELWITHDEBINFO", "DEBUG", 0};
  300. for(const char** c = default_order; *c && !default_config; ++c)
  301. {
  302. for(std::vector<std::string>::iterator i = configurationTypes.begin();
  303. i != configurationTypes.end(); ++i)
  304. {
  305. if(cmSystemTools::UpperCase(*i) == *c)
  306. {
  307. default_config = i->c_str();
  308. }
  309. }
  310. }
  311. if(!default_config && !configurationTypes.empty())
  312. {
  313. default_config = configurationTypes[0].c_str();
  314. }
  315. if(!default_config)
  316. {
  317. default_config = "Release";
  318. }
  319. // Create the install script file.
  320. std::string file = this->Makefile->GetStartOutputDirectory();
  321. std::string homedir = this->Makefile->GetHomeOutputDirectory();
  322. std::string currdir = this->Makefile->GetCurrentOutputDirectory();
  323. cmSystemTools::ConvertToUnixSlashes(file);
  324. cmSystemTools::ConvertToUnixSlashes(homedir);
  325. cmSystemTools::ConvertToUnixSlashes(currdir);
  326. int toplevel_install = 0;
  327. if ( currdir == homedir )
  328. {
  329. toplevel_install = 1;
  330. }
  331. file += "/cmake_install.cmake";
  332. cmGeneratedFileStream fout(file.c_str());
  333. fout.SetCopyIfDifferent(true);
  334. // Write the header.
  335. fout << "# Install script for directory: "
  336. << this->Makefile->GetCurrentDirectory() << std::endl << std::endl;
  337. fout << "# Set the install prefix" << std::endl
  338. << "IF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
  339. << " SET(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl
  340. << "ENDIF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
  341. << "STRING(REGEX REPLACE \"/$\" \"\" CMAKE_INSTALL_PREFIX "
  342. << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
  343. << std::endl;
  344. // Write support code for generating per-configuration install rules.
  345. fout <<
  346. "# Set the install configuration name.\n"
  347. "IF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
  348. " IF(BUILD_TYPE)\n"
  349. " STRING(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
  350. " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
  351. " ELSE(BUILD_TYPE)\n"
  352. " SET(CMAKE_INSTALL_CONFIG_NAME \"" << default_config << "\")\n"
  353. " ENDIF(BUILD_TYPE)\n"
  354. " MESSAGE(STATUS \"Install configuration: "
  355. "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
  356. "ENDIF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
  357. "\n";
  358. // Write support code for dealing with component-specific installs.
  359. fout <<
  360. "# Set the component getting installed.\n"
  361. "IF(NOT CMAKE_INSTALL_COMPONENT)\n"
  362. " IF(COMPONENT)\n"
  363. " MESSAGE(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
  364. " SET(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
  365. " ELSE(COMPONENT)\n"
  366. " SET(CMAKE_INSTALL_COMPONENT)\n"
  367. " ENDIF(COMPONENT)\n"
  368. "ENDIF(NOT CMAKE_INSTALL_COMPONENT)\n"
  369. "\n";
  370. // Copy user-specified install options to the install code.
  371. if(const char* so_no_exe =
  372. this->Makefile->GetDefinition("CMAKE_INSTALL_SO_NO_EXE"))
  373. {
  374. fout <<
  375. "# Install shared libraries without execute permission?\n"
  376. "IF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
  377. " SET(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe << "\")\n"
  378. "ENDIF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
  379. "\n";
  380. }
  381. // Ask each install generator to write its code.
  382. std::vector<cmInstallGenerator*> const& installers =
  383. this->Makefile->GetInstallGenerators();
  384. for(std::vector<cmInstallGenerator*>::const_iterator
  385. gi = installers.begin();
  386. gi != installers.end(); ++gi)
  387. {
  388. (*gi)->Generate(fout, config, configurationTypes);
  389. }
  390. // Write rules from old-style specification stored in targets.
  391. this->GenerateTargetInstallRules(fout, config, configurationTypes);
  392. // Include install scripts from subdirectories.
  393. if(!this->Children.empty())
  394. {
  395. fout << "IF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
  396. fout << " # Include the install script for each subdirectory.\n";
  397. for(std::vector<cmLocalGenerator*>::const_iterator
  398. ci = this->Children.begin(); ci != this->Children.end(); ++ci)
  399. {
  400. if(!(*ci)->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  401. {
  402. std::string odir = (*ci)->GetMakefile()->GetStartOutputDirectory();
  403. cmSystemTools::ConvertToUnixSlashes(odir);
  404. fout << " INCLUDE(\"" << odir.c_str()
  405. << "/cmake_install.cmake\")" << std::endl;
  406. }
  407. }
  408. fout << "\n";
  409. fout << "ENDIF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n\n";
  410. }
  411. // Record the install manifest.
  412. if ( toplevel_install )
  413. {
  414. fout <<
  415. "IF(CMAKE_INSTALL_COMPONENT)\n"
  416. " SET(CMAKE_INSTALL_MANIFEST \"install_manifest_"
  417. "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
  418. "ELSE(CMAKE_INSTALL_COMPONENT)\n"
  419. " SET(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
  420. "ENDIF(CMAKE_INSTALL_COMPONENT)\n\n";
  421. fout
  422. << "FILE(WRITE \""
  423. << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
  424. << "\"\")" << std::endl;
  425. fout
  426. << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
  427. << " FILE(APPEND \""
  428. << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
  429. << "\"${file}\\n\")" << std::endl
  430. << "ENDFOREACH(file)" << std::endl;
  431. }
  432. }
  433. //----------------------------------------------------------------------------
  434. void cmLocalGenerator::GenerateTargetManifest()
  435. {
  436. // Collect the set of configuration types.
  437. std::vector<std::string> configNames;
  438. if(const char* configurationTypes =
  439. this->Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  440. {
  441. cmSystemTools::ExpandListArgument(configurationTypes, configNames);
  442. }
  443. else if(const char* buildType =
  444. this->Makefile->GetDefinition("CMAKE_BUILD_TYPE"))
  445. {
  446. if(*buildType)
  447. {
  448. configNames.push_back(buildType);
  449. }
  450. }
  451. // Add our targets to the manifest for each configuration.
  452. cmTargets& targets = this->Makefile->GetTargets();
  453. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  454. {
  455. cmTarget& target = t->second;
  456. if(configNames.empty())
  457. {
  458. target.GenerateTargetManifest(0);
  459. }
  460. else
  461. {
  462. for(std::vector<std::string>::iterator ci = configNames.begin();
  463. ci != configNames.end(); ++ci)
  464. {
  465. const char* config = ci->c_str();
  466. target.GenerateTargetManifest(config);
  467. }
  468. }
  469. }
  470. }
  471. void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname,
  472. const char* lang,
  473. cmSourceFile& source,
  474. cmTarget& )
  475. {
  476. std::string objectDir = cmSystemTools::GetFilenamePath(std::string(ofname));
  477. objectDir = this->Convert(objectDir.c_str(),START_OUTPUT,SHELL);
  478. std::string objectFile = this->Convert(ofname,START_OUTPUT,SHELL);
  479. std::string sourceFile =
  480. this->Convert(source.GetFullPath().c_str(),START_OUTPUT,SHELL,true);
  481. std::string varString = "CMAKE_";
  482. varString += lang;
  483. varString += "_COMPILE_OBJECT";
  484. std::vector<std::string> rules;
  485. rules.push_back(this->Makefile->GetRequiredDefinition(varString.c_str()));
  486. varString = "CMAKE_";
  487. varString += lang;
  488. varString += "_FLAGS";
  489. std::string flags;
  490. flags += this->Makefile->GetSafeDefinition(varString.c_str());
  491. flags += " ";
  492. flags += this->GetIncludeFlags(lang);
  493. // Construct the command lines.
  494. cmCustomCommandLines commandLines;
  495. std::vector<std::string> commands;
  496. cmSystemTools::ExpandList(rules, commands);
  497. cmLocalGenerator::RuleVariables vars;
  498. vars.Language = lang;
  499. vars.Source = sourceFile.c_str();
  500. vars.Object = objectFile.c_str();
  501. vars.ObjectDir = objectDir.c_str();
  502. vars.Flags = flags.c_str();
  503. for(std::vector<std::string>::iterator i = commands.begin();
  504. i != commands.end(); ++i)
  505. {
  506. // Expand the full command line string.
  507. this->ExpandRuleVariables(*i, vars);
  508. // Parse the string to get the custom command line.
  509. cmCustomCommandLine commandLine;
  510. std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
  511. for(std::vector<cmStdString>::iterator a = cmd.begin();
  512. a != cmd.end(); ++a)
  513. {
  514. commandLine.push_back(*a);
  515. }
  516. // Store this command line.
  517. commandLines.push_back(commandLine);
  518. }
  519. // Check for extra object-file dependencies.
  520. std::vector<std::string> depends;
  521. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  522. if(additionalDeps)
  523. {
  524. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  525. }
  526. // Generate a meaningful comment for the command.
  527. std::string comment = "Building ";
  528. comment += lang;
  529. comment += " object ";
  530. comment += this->Convert(ofname, START_OUTPUT);
  531. // Add the custom command to build the object file.
  532. this->Makefile->AddCustomCommandToOutput(
  533. ofname,
  534. depends,
  535. source.GetFullPath().c_str(),
  536. commandLines,
  537. comment.c_str(),
  538. this->Makefile->GetStartOutputDirectory()
  539. );
  540. }
  541. void cmLocalGenerator::AddBuildTargetRule(const char* llang, cmTarget& target)
  542. {
  543. cmStdString objs;
  544. std::vector<std::string> objVector;
  545. // Add all the sources outputs to the depends of the target
  546. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  547. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  548. i != classes.end(); ++i)
  549. {
  550. cmSourceFile* sf = *i;
  551. if(!sf->GetCustomCommand() &&
  552. !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  553. !sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
  554. {
  555. std::string::size_type dir_len = 0;
  556. dir_len += strlen(this->Makefile->GetCurrentOutputDirectory());
  557. dir_len += 1;
  558. std::string obj = this->GetObjectFileNameWithoutTarget(*sf, dir_len);
  559. if(!obj.empty())
  560. {
  561. std::string ofname = this->Makefile->GetCurrentOutputDirectory();
  562. ofname += "/";
  563. ofname += obj;
  564. objVector.push_back(ofname);
  565. this->AddCustomCommandToCreateObject(ofname.c_str(),
  566. llang, *(*i), target);
  567. objs += this->Convert(ofname.c_str(),START_OUTPUT,MAKEFILE);
  568. objs += " ";
  569. }
  570. }
  571. }
  572. std::string createRule = "CMAKE_";
  573. createRule += llang;
  574. createRule += target.GetCreateRuleVariable();
  575. std::string targetName = target.GetFullName();
  576. // Executable :
  577. // Shared Library:
  578. // Static Library:
  579. // Shared Module:
  580. std::string linkLibs; // should be set
  581. std::string flags; // should be set
  582. std::string linkFlags; // should be set
  583. this->GetTargetFlags(linkLibs, flags, linkFlags, target);
  584. cmLocalGenerator::RuleVariables vars;
  585. vars.Language = llang;
  586. vars.Objects = objs.c_str();
  587. vars.ObjectDir = ".";
  588. vars.Target = targetName.c_str();
  589. vars.LinkLibraries = linkLibs.c_str();
  590. vars.Flags = flags.c_str();
  591. vars.LinkFlags = linkFlags.c_str();
  592. std::string langFlags;
  593. this->AddLanguageFlags(langFlags, llang, 0);
  594. vars.LanguageCompileFlags = langFlags.c_str();
  595. cmCustomCommandLines commandLines;
  596. std::vector<std::string> rules;
  597. rules.push_back(this->Makefile->GetRequiredDefinition(createRule.c_str()));
  598. std::vector<std::string> commands;
  599. cmSystemTools::ExpandList(rules, commands);
  600. for(std::vector<std::string>::iterator i = commands.begin();
  601. i != commands.end(); ++i)
  602. {
  603. // Expand the full command line string.
  604. this->ExpandRuleVariables(*i, vars);
  605. // Parse the string to get the custom command line.
  606. cmCustomCommandLine commandLine;
  607. std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
  608. for(std::vector<cmStdString>::iterator a = cmd.begin();
  609. a != cmd.end(); ++a)
  610. {
  611. commandLine.push_back(*a);
  612. }
  613. // Store this command line.
  614. commandLines.push_back(commandLine);
  615. }
  616. std::string targetFullPath = target.GetFullPath();
  617. // Generate a meaningful comment for the command.
  618. std::string comment = "Linking ";
  619. comment += llang;
  620. comment += " target ";
  621. comment += this->Convert(targetFullPath.c_str(), START_OUTPUT);
  622. this->Makefile->AddCustomCommandToOutput(
  623. targetFullPath.c_str(),
  624. objVector,
  625. 0,
  626. commandLines,
  627. comment.c_str(),
  628. this->Makefile->GetStartOutputDirectory()
  629. );
  630. target.AddSourceFile
  631. (this->Makefile->GetSource(targetFullPath.c_str()));
  632. }
  633. void cmLocalGenerator
  634. ::CreateCustomTargetsAndCommands(std::set<cmStdString> const& lang)
  635. {
  636. cmTargets &tgts = this->Makefile->GetTargets();
  637. for(cmTargets::iterator l = tgts.begin();
  638. l != tgts.end(); l++)
  639. {
  640. cmTarget& target = l->second;
  641. switch(target.GetType())
  642. {
  643. case cmTarget::STATIC_LIBRARY:
  644. case cmTarget::SHARED_LIBRARY:
  645. case cmTarget::MODULE_LIBRARY:
  646. case cmTarget::EXECUTABLE:
  647. {
  648. const char* llang =
  649. target.GetLinkerLanguage(this->GetGlobalGenerator());
  650. if(!llang)
  651. {
  652. cmSystemTools::Error
  653. ("CMake can not determine linker language for target:",
  654. target.GetName());
  655. return;
  656. }
  657. // if the language is not in the set lang then create custom
  658. // commands to build the target
  659. if(lang.count(llang) == 0)
  660. {
  661. this->AddBuildTargetRule(llang, target);
  662. }
  663. }
  664. break;
  665. case cmTarget::UTILITY:
  666. case cmTarget::GLOBAL_TARGET:
  667. case cmTarget::INSTALL_FILES:
  668. case cmTarget::INSTALL_PROGRAMS:
  669. case cmTarget::INSTALL_DIRECTORY:
  670. break;
  671. }
  672. }
  673. }
  674. // List of variables that are replaced when
  675. // rules are expanced. These variables are
  676. // replaced in the form <var> with GetSafeDefinition(var).
  677. // ${LANG} is replaced in the variable first with all enabled
  678. // languages.
  679. static const char* ruleReplaceVars[] =
  680. {
  681. "CMAKE_${LANG}_COMPILER",
  682. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  683. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  684. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  685. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  686. "CMAKE_${LANG}_LINK_FLAGS",
  687. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  688. "CMAKE_${LANG}_ARCHIVE",
  689. "CMAKE_AR",
  690. "CMAKE_CURRENT_SOURCE_DIR",
  691. "CMAKE_CURRENT_BINARY_DIR",
  692. "CMAKE_RANLIB",
  693. "CMAKE_LINKER",
  694. 0
  695. };
  696. std::string
  697. cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
  698. const RuleVariables& replaceValues)
  699. {
  700. if(replaceValues.LinkFlags)
  701. {
  702. if(variable == "LINK_FLAGS")
  703. {
  704. return replaceValues.LinkFlags;
  705. }
  706. }
  707. if(replaceValues.Flags)
  708. {
  709. if(variable == "FLAGS")
  710. {
  711. return replaceValues.Flags;
  712. }
  713. }
  714. if(replaceValues.Source)
  715. {
  716. if(variable == "SOURCE")
  717. {
  718. return replaceValues.Source;
  719. }
  720. }
  721. if(replaceValues.PreprocessedSource)
  722. {
  723. if(variable == "PREPROCESSED_SOURCE")
  724. {
  725. return replaceValues.PreprocessedSource;
  726. }
  727. }
  728. if(replaceValues.AssemblySource)
  729. {
  730. if(variable == "ASSEMBLY_SOURCE")
  731. {
  732. return replaceValues.AssemblySource;
  733. }
  734. }
  735. if(replaceValues.Object)
  736. {
  737. if(variable == "OBJECT")
  738. {
  739. return replaceValues.Object;
  740. }
  741. }
  742. if(replaceValues.ObjectDir)
  743. {
  744. if(variable == "OBJECT_DIR")
  745. {
  746. return replaceValues.ObjectDir;
  747. }
  748. }
  749. if(replaceValues.Objects)
  750. {
  751. if(variable == "OBJECTS")
  752. {
  753. return replaceValues.Objects;
  754. }
  755. }
  756. if(replaceValues.ObjectsQuoted)
  757. {
  758. if(variable == "OBJECTS_QUOTED")
  759. {
  760. return replaceValues.ObjectsQuoted;
  761. }
  762. }
  763. if(replaceValues.Defines && variable == "DEFINES")
  764. {
  765. return replaceValues.Defines;
  766. }
  767. if(replaceValues.TargetPDB )
  768. {
  769. if(variable == "TARGET_PDB")
  770. {
  771. return replaceValues.TargetPDB;
  772. }
  773. }
  774. if(replaceValues.Target)
  775. {
  776. if(variable == "TARGET_QUOTED")
  777. {
  778. std::string targetQuoted = replaceValues.Target;
  779. if(targetQuoted.size() && targetQuoted[0] != '\"')
  780. {
  781. targetQuoted = '\"';
  782. targetQuoted += replaceValues.Target;
  783. targetQuoted += '\"';
  784. }
  785. return targetQuoted;
  786. }
  787. if(replaceValues.LanguageCompileFlags)
  788. {
  789. if(variable == "LANGUAGE_COMPILE_FLAGS")
  790. {
  791. return replaceValues.LanguageCompileFlags;
  792. }
  793. }
  794. if(replaceValues.Target)
  795. {
  796. if(variable == "TARGET")
  797. {
  798. return replaceValues.Target;
  799. }
  800. }
  801. if(variable == "TARGET_IMPLIB")
  802. {
  803. return this->TargetImplib;
  804. }
  805. if(variable == "TARGET_VERSION_MAJOR")
  806. {
  807. if(replaceValues.TargetVersionMajor)
  808. {
  809. return replaceValues.TargetVersionMajor;
  810. }
  811. else
  812. {
  813. return "0";
  814. }
  815. }
  816. if(variable == "TARGET_VERSION_MINOR")
  817. {
  818. if(replaceValues.TargetVersionMinor)
  819. {
  820. return replaceValues.TargetVersionMinor;
  821. }
  822. else
  823. {
  824. return "0";
  825. }
  826. }
  827. if(replaceValues.Target)
  828. {
  829. if(variable == "TARGET_BASE")
  830. {
  831. // Strip the last extension off the target name.
  832. std::string targetBase = replaceValues.Target;
  833. std::string::size_type pos = targetBase.rfind(".");
  834. if(pos != targetBase.npos)
  835. {
  836. return targetBase.substr(0, pos);
  837. }
  838. else
  839. {
  840. return targetBase;
  841. }
  842. }
  843. }
  844. }
  845. if(replaceValues.TargetSOName)
  846. {
  847. if(variable == "TARGET_SONAME")
  848. {
  849. if(replaceValues.Language)
  850. {
  851. std::string name = "CMAKE_SHARED_LIBRARY_SONAME_";
  852. name += replaceValues.Language;
  853. name += "_FLAG";
  854. if(this->Makefile->GetDefinition(name.c_str()))
  855. {
  856. return replaceValues.TargetSOName;
  857. }
  858. }
  859. return "";
  860. }
  861. }
  862. if(replaceValues.TargetInstallNameDir)
  863. {
  864. if(variable == "TARGET_INSTALLNAME_DIR")
  865. {
  866. return replaceValues.TargetInstallNameDir;
  867. }
  868. }
  869. if(replaceValues.LinkLibraries)
  870. {
  871. if(variable == "LINK_LIBRARIES")
  872. {
  873. return replaceValues.LinkLibraries;
  874. }
  875. }
  876. std::vector<std::string> enabledLanguages;
  877. this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  878. // loop over language specific replace variables
  879. int pos = 0;
  880. while(ruleReplaceVars[pos])
  881. {
  882. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  883. i != enabledLanguages.end(); ++i)
  884. {
  885. const char* lang = i->c_str();
  886. std::string actualReplace = ruleReplaceVars[pos];
  887. // If this is the compiler then look for the extra variable
  888. // _COMPILER_ARG1 which must be the first argument to the compiler
  889. const char* compilerArg1 = 0;
  890. if(actualReplace == "CMAKE_${LANG}_COMPILER")
  891. {
  892. std::string arg1 = actualReplace + "_ARG1";
  893. cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
  894. compilerArg1 = this->Makefile->GetDefinition(arg1.c_str());
  895. }
  896. if(actualReplace.find("${LANG}") != actualReplace.npos)
  897. {
  898. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  899. }
  900. if(actualReplace == variable)
  901. {
  902. std::string replace =
  903. this->Makefile->GetSafeDefinition(variable.c_str());
  904. // if the variable is not a FLAG then treat it like a path
  905. if(variable.find("_FLAG") == variable.npos)
  906. {
  907. std::string ret = this->ConvertToOutputForExisting(replace.c_str());
  908. // if there is a required first argument to the compiler add it
  909. // to the compiler string
  910. if(compilerArg1)
  911. {
  912. ret += " ";
  913. ret += compilerArg1;
  914. }
  915. return ret;
  916. }
  917. return replace;
  918. }
  919. }
  920. pos++;
  921. }
  922. return variable;
  923. }
  924. void
  925. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  926. const RuleVariables& replaceValues)
  927. {
  928. std::vector<std::string> enabledLanguages;
  929. this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  930. std::string::size_type start = s.find('<');
  931. // no variables to expand
  932. if(start == s.npos)
  933. {
  934. return;
  935. }
  936. std::string::size_type pos = 0;
  937. std::string expandedInput;
  938. while(start != s.npos && start < s.size()-2)
  939. {
  940. std::string::size_type end = s.find('>', start);
  941. // if we find a < with no > we are done
  942. if(end == s.npos)
  943. {
  944. return;
  945. }
  946. char c = s[start+1];
  947. // if the next char after the < is not A-Za-z then
  948. // skip it and try to find the next < in the string
  949. if(!isalpha(c))
  950. {
  951. start = s.find('<', start+1);
  952. }
  953. else
  954. {
  955. // extract the var
  956. std::string var = s.substr(start+1, end - start-1);
  957. std::string replace = this->ExpandRuleVariable(var,
  958. replaceValues);
  959. expandedInput += s.substr(pos, start-pos);
  960. expandedInput += replace;
  961. // move to next one
  962. start = s.find('<', start+var.size()+2);
  963. pos = end+1;
  964. }
  965. }
  966. // add the rest of the input
  967. expandedInput += s.substr(pos, s.size()-pos);
  968. s = expandedInput;
  969. }
  970. std::string
  971. cmLocalGenerator::ConvertToOutputForExisting(const char* p)
  972. {
  973. std::string ret = p;
  974. if(this->WindowsShell && ret.find(' ') != ret.npos
  975. && cmSystemTools::FileExists(p))
  976. {
  977. if(cmSystemTools::GetShortPath(p, ret))
  978. {
  979. return this->Convert(ret.c_str(), NONE, SHELL, true);
  980. }
  981. }
  982. return this->Convert(p, START_OUTPUT, SHELL, true);
  983. }
  984. const char* cmLocalGenerator::GetIncludeFlags(const char* lang)
  985. {
  986. if(!lang)
  987. {
  988. return "";
  989. }
  990. if(this->LanguageToIncludeFlags.count(lang))
  991. {
  992. return this->LanguageToIncludeFlags[lang].c_str();
  993. }
  994. cmOStringStream includeFlags;
  995. std::vector<std::string> includes;
  996. this->GetIncludeDirectories(includes);
  997. std::vector<std::string>::iterator i;
  998. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  999. flagVar += lang;
  1000. const char* includeFlag =
  1001. this->Makefile->GetSafeDefinition(flagVar.c_str());
  1002. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  1003. flagVar += lang;
  1004. const char* sep = this->Makefile->GetDefinition(flagVar.c_str());
  1005. bool quotePaths = false;
  1006. if(this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
  1007. {
  1008. quotePaths = true;
  1009. }
  1010. bool repeatFlag = true;
  1011. // should the include flag be repeated like ie. -IA -IB
  1012. if(!sep)
  1013. {
  1014. sep = " ";
  1015. }
  1016. else
  1017. {
  1018. // if there is a separator then the flag is not repeated but is only
  1019. // given once i.e. -classpath a:b:c
  1020. repeatFlag = false;
  1021. }
  1022. // Support special system include flag if it is available and the
  1023. // normal flag is repeated for each directory.
  1024. std::string sysFlagVar = "CMAKE_INCLUDE_SYSTEM_FLAG_";
  1025. sysFlagVar += lang;
  1026. const char* sysIncludeFlag = 0;
  1027. if(repeatFlag)
  1028. {
  1029. sysIncludeFlag = this->Makefile->GetDefinition(sysFlagVar.c_str());
  1030. }
  1031. bool flagUsed = false;
  1032. std::set<cmStdString> emitted;
  1033. #ifdef __APPLE__
  1034. emitted.insert("/System/Library/Frameworks");
  1035. #endif
  1036. for(i = includes.begin(); i != includes.end(); ++i)
  1037. {
  1038. #ifdef __APPLE__
  1039. if(cmSystemTools::IsPathToFramework(i->c_str()))
  1040. {
  1041. std::string frameworkDir = *i;
  1042. frameworkDir += "/../";
  1043. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1044. if(emitted.insert(frameworkDir).second)
  1045. {
  1046. includeFlags
  1047. << "-F"
  1048. << this->ConvertToOutputForExisting(frameworkDir.c_str()) << " ";
  1049. }
  1050. continue;
  1051. }
  1052. #endif
  1053. std::string include = *i;
  1054. if(!flagUsed || repeatFlag)
  1055. {
  1056. if(sysIncludeFlag &&
  1057. this->Makefile->IsSystemIncludeDirectory(i->c_str()))
  1058. {
  1059. includeFlags << sysIncludeFlag;
  1060. }
  1061. else
  1062. {
  1063. includeFlags << includeFlag;
  1064. }
  1065. flagUsed = true;
  1066. }
  1067. std::string includePath = this->ConvertToOutputForExisting(i->c_str());
  1068. if(quotePaths && includePath.size() && includePath[0] != '\"')
  1069. {
  1070. includeFlags << "\"";
  1071. }
  1072. includeFlags << includePath;
  1073. if(quotePaths && includePath.size() && includePath[0] != '\"')
  1074. {
  1075. includeFlags << "\"";
  1076. }
  1077. includeFlags << sep;
  1078. }
  1079. std::string flags = includeFlags.str();
  1080. // remove trailing separators
  1081. if((sep[0] != ' ') && flags[flags.size()-1] == sep[0])
  1082. {
  1083. flags[flags.size()-1] = ' ';
  1084. }
  1085. std::string defineFlags = this->Makefile->GetDefineFlags();
  1086. flags += defineFlags;
  1087. this->LanguageToIncludeFlags[lang] = flags;
  1088. // Use this temorary variable for the return value to work-around a
  1089. // bogus GCC 2.95 warning.
  1090. const char* ret = this->LanguageToIncludeFlags[lang].c_str();
  1091. return ret;
  1092. }
  1093. //----------------------------------------------------------------------------
  1094. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
  1095. bool filter_system_dirs)
  1096. {
  1097. // Need to decide whether to automatically include the source and
  1098. // binary directories at the beginning of the include path.
  1099. bool includeSourceDir = false;
  1100. bool includeBinaryDir = false;
  1101. // When automatic include directories are requested for a build then
  1102. // include the source and binary directories at the beginning of the
  1103. // include path to approximate include file behavior for an
  1104. // in-source build. This does not account for the case of a source
  1105. // file in a subdirectory of the current source directory but we
  1106. // cannot fix this because not all native build tools support
  1107. // per-source-file include paths.
  1108. if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
  1109. {
  1110. includeSourceDir = true;
  1111. includeBinaryDir = true;
  1112. }
  1113. // CMake versions below 2.0 would add the source tree to the -I path
  1114. // automatically. Preserve compatibility.
  1115. const char* versionValue =
  1116. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  1117. int major = 0;
  1118. int minor = 0;
  1119. if(versionValue && sscanf(versionValue, "%d.%d", &major, &minor) != 2)
  1120. {
  1121. versionValue = 0;
  1122. }
  1123. if(versionValue && major < 2)
  1124. {
  1125. includeSourceDir = true;
  1126. }
  1127. // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
  1128. // not set the backwards compatibility level automatically.
  1129. const char* vtkSourceDir =
  1130. this->Makefile->GetDefinition("VTK_SOURCE_DIR");
  1131. if(vtkSourceDir)
  1132. {
  1133. const char* vtk_major =
  1134. this->Makefile->GetDefinition("VTK_MAJOR_VERSION");
  1135. const char* vtk_minor =
  1136. this->Makefile->GetDefinition("VTK_MINOR_VERSION");
  1137. vtk_major = vtk_major? vtk_major : "4";
  1138. vtk_minor = vtk_minor? vtk_minor : "4";
  1139. int vmajor = 0;
  1140. int vminor = 0;
  1141. if(sscanf(vtk_major, "%d", &vmajor) &&
  1142. sscanf(vtk_minor, "%d", &vminor) && vmajor == 4 && vminor <= 4)
  1143. {
  1144. includeSourceDir = true;
  1145. }
  1146. }
  1147. // Do not repeat an include path.
  1148. std::set<cmStdString> emitted;
  1149. // Store the automatic include paths.
  1150. if(includeBinaryDir)
  1151. {
  1152. dirs.push_back(this->Makefile->GetStartOutputDirectory());
  1153. emitted.insert(this->Makefile->GetStartOutputDirectory());
  1154. }
  1155. if(includeSourceDir)
  1156. {
  1157. if(emitted.find(this->Makefile->GetStartDirectory()) == emitted.end())
  1158. {
  1159. dirs.push_back(this->Makefile->GetStartDirectory());
  1160. emitted.insert(this->Makefile->GetStartDirectory());
  1161. }
  1162. }
  1163. if(filter_system_dirs)
  1164. {
  1165. // Do not explicitly add the standard include path "/usr/include".
  1166. // This can cause problems with certain standard library
  1167. // implementations because the wrong headers may be found first.
  1168. emitted.insert("/usr/include");
  1169. if(const char* implicitIncludes = this->Makefile->GetDefinition
  1170. ("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
  1171. {
  1172. std::vector<std::string> implicitIncludeVec;
  1173. cmSystemTools::ExpandListArgument(implicitIncludes, implicitIncludeVec);
  1174. for(unsigned int k = 0; k < implicitIncludeVec.size(); ++k)
  1175. {
  1176. emitted.insert(implicitIncludeVec[k]);
  1177. }
  1178. }
  1179. }
  1180. // Get the project-specified include directories.
  1181. std::vector<std::string>& includes =
  1182. this->Makefile->GetIncludeDirectories();
  1183. // Support putting all the in-project include directories first if
  1184. // it is requested by the project.
  1185. if(this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
  1186. {
  1187. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1188. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1189. for(std::vector<std::string>::iterator i = includes.begin();
  1190. i != includes.end(); ++i)
  1191. {
  1192. // Emit this directory only if it is a subdirectory of the
  1193. // top-level source or binary tree.
  1194. if(cmSystemTools::ComparePath(i->c_str(), topSourceDir) ||
  1195. cmSystemTools::ComparePath(i->c_str(), topBinaryDir) ||
  1196. cmSystemTools::IsSubDirectory(i->c_str(), topSourceDir) ||
  1197. cmSystemTools::IsSubDirectory(i->c_str(), topBinaryDir))
  1198. {
  1199. if(emitted.insert(*i).second)
  1200. {
  1201. dirs.push_back(*i);
  1202. }
  1203. }
  1204. }
  1205. }
  1206. // Construct the final ordered include directory list.
  1207. for(std::vector<std::string>::iterator i = includes.begin();
  1208. i != includes.end(); ++i)
  1209. {
  1210. if(emitted.insert(*i).second)
  1211. {
  1212. dirs.push_back(*i);
  1213. }
  1214. }
  1215. }
  1216. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  1217. std::string& flags,
  1218. std::string& linkFlags,
  1219. cmTarget& target)
  1220. {
  1221. std::string buildType =
  1222. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1223. buildType = cmSystemTools::UpperCase(buildType);
  1224. const char* libraryLinkVariable =
  1225. "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1226. switch(target.GetType())
  1227. {
  1228. case cmTarget::STATIC_LIBRARY:
  1229. {
  1230. const char* targetLinkFlags =
  1231. target.GetProperty("STATIC_LIBRARY_FLAGS");
  1232. if(targetLinkFlags)
  1233. {
  1234. linkFlags += targetLinkFlags;
  1235. linkFlags += " ";
  1236. }
  1237. }
  1238. break;
  1239. case cmTarget::MODULE_LIBRARY:
  1240. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1241. case cmTarget::SHARED_LIBRARY:
  1242. {
  1243. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  1244. linkFlags += " ";
  1245. if(buildType.size())
  1246. {
  1247. std::string build = libraryLinkVariable;
  1248. build += "_";
  1249. build += buildType;
  1250. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1251. linkFlags += " ";
  1252. }
  1253. if(this->Makefile->IsOn("WIN32") &&
  1254. !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
  1255. {
  1256. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1257. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1258. i != sources.end(); ++i)
  1259. {
  1260. cmSourceFile* sf = *i;
  1261. if(sf->GetExtension() == "def")
  1262. {
  1263. linkFlags +=
  1264. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1265. linkFlags += this->Convert(sf->GetFullPath().c_str(),
  1266. START_OUTPUT, SHELL);
  1267. linkFlags += " ";
  1268. }
  1269. }
  1270. }
  1271. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1272. if(targetLinkFlags)
  1273. {
  1274. linkFlags += targetLinkFlags;
  1275. linkFlags += " ";
  1276. std::string configLinkFlags = targetLinkFlags;
  1277. configLinkFlags += buildType;
  1278. targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
  1279. if(targetLinkFlags)
  1280. {
  1281. linkFlags += targetLinkFlags;
  1282. linkFlags += " ";
  1283. }
  1284. }
  1285. cmOStringStream linklibsStr;
  1286. this->OutputLinkLibraries(linklibsStr, target, false);
  1287. linkLibs = linklibsStr.str();
  1288. }
  1289. break;
  1290. case cmTarget::EXECUTABLE:
  1291. {
  1292. linkFlags +=
  1293. this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1294. linkFlags += " ";
  1295. if(buildType.size())
  1296. {
  1297. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1298. build += buildType;
  1299. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1300. linkFlags += " ";
  1301. }
  1302. const char* linkLanguage =
  1303. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1304. if(!linkLanguage)
  1305. {
  1306. cmSystemTools::Error
  1307. ("CMake can not determine linker language for target:",
  1308. target.GetName());
  1309. return;
  1310. }
  1311. std::string langVar = "CMAKE_";
  1312. langVar += linkLanguage;
  1313. std::string flagsVar = langVar + "_FLAGS";
  1314. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  1315. sharedFlagsVar += linkLanguage;
  1316. sharedFlagsVar += "_FLAGS";
  1317. flags += this->Makefile->GetSafeDefinition(flagsVar.c_str());
  1318. flags += " ";
  1319. flags += this->Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  1320. flags += " ";
  1321. cmOStringStream linklibs;
  1322. this->OutputLinkLibraries(linklibs, target, false);
  1323. linkLibs = linklibs.str();
  1324. if(cmSystemTools::IsOn
  1325. (this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1326. {
  1327. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_")
  1328. + linkLanguage + std::string("_FLAGS");
  1329. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar.c_str());
  1330. linkFlags += " ";
  1331. }
  1332. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  1333. {
  1334. linkFlags +=
  1335. this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1336. linkFlags += " ";
  1337. }
  1338. else
  1339. {
  1340. linkFlags +=
  1341. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1342. linkFlags += " ";
  1343. }
  1344. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1345. if(targetLinkFlags)
  1346. {
  1347. linkFlags += targetLinkFlags;
  1348. linkFlags += " ";
  1349. std::string configLinkFlags = targetLinkFlags;
  1350. configLinkFlags += buildType;
  1351. targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
  1352. if(targetLinkFlags)
  1353. {
  1354. linkFlags += targetLinkFlags;
  1355. linkFlags += " ";
  1356. }
  1357. }
  1358. }
  1359. break;
  1360. case cmTarget::UTILITY:
  1361. case cmTarget::GLOBAL_TARGET:
  1362. case cmTarget::INSTALL_FILES:
  1363. case cmTarget::INSTALL_PROGRAMS:
  1364. case cmTarget::INSTALL_DIRECTORY:
  1365. break;
  1366. }
  1367. }
  1368. bool cmLocalGenerator::GetLinkerArgs(std::string& rpath,
  1369. std::string& linkLibs,
  1370. cmTarget& tgt,
  1371. bool relink,
  1372. unsigned int minRpathSize)
  1373. {
  1374. rpath = "";
  1375. // collect all the flags needed for linking libraries
  1376. linkLibs = "";
  1377. const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1378. cmComputeLinkInformation cli(&tgt, config);
  1379. if(!cli.Compute())
  1380. {
  1381. return false;
  1382. }
  1383. const char* linkLanguage = cli.GetLinkLanguage();
  1384. // Embed runtime search paths if possible and if required.
  1385. bool outputRuntime = !this->Makefile->IsOn("CMAKE_SKIP_RPATH");
  1386. // Lookup rpath specification flags.
  1387. std::string runtimeFlag;
  1388. std::string runtimeSep;
  1389. if(tgt.GetType() != cmTarget::STATIC_LIBRARY)
  1390. {
  1391. std::string runTimeFlagVar = "CMAKE_";
  1392. if(tgt.GetType() == cmTarget::EXECUTABLE)
  1393. {
  1394. runTimeFlagVar += "EXECUTABLE";
  1395. }
  1396. else
  1397. {
  1398. runTimeFlagVar += "SHARED_LIBRARY";
  1399. }
  1400. runTimeFlagVar += "_RUNTIME_";
  1401. runTimeFlagVar += linkLanguage;
  1402. runTimeFlagVar += "_FLAG";
  1403. std::string runTimeFlagSepVar = runTimeFlagVar + "_SEP";
  1404. runtimeFlag = this->Makefile->GetSafeDefinition(runTimeFlagVar.c_str());
  1405. runtimeSep = this->Makefile->GetSafeDefinition(runTimeFlagSepVar.c_str());
  1406. }
  1407. // concatenate all paths or no?
  1408. bool runtimeConcatenate = !runtimeSep.empty();
  1409. const char* runtimeAlways =
  1410. this->Makefile->GetDefinition("CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH");
  1411. // Turn off rpath support if no flag is available to specify it.
  1412. if(runtimeFlag.empty())
  1413. {
  1414. outputRuntime = false;
  1415. runtimeAlways = 0;
  1416. }
  1417. std::string libPathFlag =
  1418. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1419. std::string libPathTerminator =
  1420. this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
  1421. std::string libLinkFlag =
  1422. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1423. // Flags to link an executable to shared libraries.
  1424. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1425. linkFlagsVar += linkLanguage;
  1426. linkFlagsVar += "_FLAGS";
  1427. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1428. {
  1429. linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1430. linkLibs += " ";
  1431. }
  1432. // Append the framework search path flags.
  1433. std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
  1434. for(std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  1435. fdi != fwDirs.end(); ++fdi)
  1436. {
  1437. linkLibs += "-F";
  1438. linkLibs += this->Convert(fdi->c_str(), NONE, SHELL, false);
  1439. linkLibs += " ";
  1440. }
  1441. // Append the library search path flags.
  1442. std::vector<std::string> const& libDirs = cli.GetDirectories();
  1443. for(std::vector<std::string>::const_iterator libDir = libDirs.begin();
  1444. libDir != libDirs.end(); ++libDir)
  1445. {
  1446. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1447. linkLibs += libPathFlag;
  1448. linkLibs += libpath;
  1449. linkLibs += libPathTerminator;
  1450. linkLibs += " ";
  1451. }
  1452. // Append the link items.
  1453. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1454. ItemVector const& items = cli.GetItems();
  1455. for(ItemVector::const_iterator li = items.begin(); li != items.end(); ++li)
  1456. {
  1457. if(li->IsPath)
  1458. {
  1459. linkLibs += this->Convert(li->Value.c_str(), START_OUTPUT, SHELL);
  1460. }
  1461. else
  1462. {
  1463. linkLibs += li->Value;
  1464. }
  1465. linkLibs += " ";
  1466. }
  1467. // Select whether to generate an rpath for the install tree or the
  1468. // build tree.
  1469. bool linking_for_install =
  1470. relink || tgt.GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
  1471. bool use_install_rpath =
  1472. outputRuntime && tgt.HaveInstallTreeRPATH() && linking_for_install;
  1473. bool use_build_rpath =
  1474. outputRuntime && tgt.HaveBuildTreeRPATH() && !linking_for_install;
  1475. bool use_link_rpath =
  1476. outputRuntime && linking_for_install &&
  1477. tgt.GetPropertyAsBool("INSTALL_RPATH_USE_LINK_PATH");
  1478. // Construct the RPATH.
  1479. std::vector<std::string> runtimeDirs;
  1480. if(use_install_rpath)
  1481. {
  1482. const char* install_rpath = tgt.GetProperty("INSTALL_RPATH");
  1483. cmSystemTools::ExpandListArgument(install_rpath, runtimeDirs);
  1484. }
  1485. if(use_build_rpath || use_link_rpath)
  1486. {
  1487. std::vector<std::string> const& rdirs = cli.GetRuntimeSearchPath();
  1488. for(std::vector<std::string>::const_iterator ri = rdirs.begin();
  1489. ri != rdirs.end(); ++ri)
  1490. {
  1491. // Put this directory in the rpath if using build-tree rpath
  1492. // support or if using the link path as an rpath.
  1493. if(use_build_rpath)
  1494. {
  1495. runtimeDirs.push_back(*ri);
  1496. }
  1497. else if(use_link_rpath)
  1498. {
  1499. // Do not add any path inside the source or build tree.
  1500. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1501. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1502. if(!cmSystemTools::ComparePath(ri->c_str(), topSourceDir) &&
  1503. !cmSystemTools::ComparePath(ri->c_str(), topBinaryDir) &&
  1504. !cmSystemTools::IsSubDirectory(ri->c_str(), topSourceDir) &&
  1505. !cmSystemTools::IsSubDirectory(ri->c_str(), topBinaryDir))
  1506. {
  1507. runtimeDirs.push_back(*ri);
  1508. }
  1509. }
  1510. }
  1511. }
  1512. if(runtimeAlways)
  1513. {
  1514. // Add runtime paths required by the platform to always be
  1515. // present. This is done even when skipping rpath support.
  1516. cmSystemTools::ExpandListArgument(runtimeAlways, runtimeDirs);
  1517. }
  1518. // Convert the runtime directory names for use in the build file.
  1519. for(std::vector<std::string>::iterator ri = runtimeDirs.begin();
  1520. ri != runtimeDirs.end(); ++ri)
  1521. {
  1522. *ri = this->Convert(ri->c_str(), FULL, SHELL, false);
  1523. }
  1524. if(!runtimeDirs.empty())
  1525. {
  1526. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  1527. // a "-R a -R b -R c" type link line
  1528. rpath += runtimeFlag;
  1529. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  1530. rpath += *itr;
  1531. ++itr;
  1532. for( ; itr != runtimeDirs.end(); ++itr )
  1533. {
  1534. if(runtimeConcatenate)
  1535. {
  1536. rpath += runtimeSep;
  1537. rpath += *itr;
  1538. }
  1539. else
  1540. {
  1541. rpath += " ";
  1542. rpath += runtimeFlag;
  1543. rpath += *itr;
  1544. }
  1545. }
  1546. }
  1547. while (rpath.size() < minRpathSize)
  1548. {
  1549. if (rpath.size()==0)
  1550. {
  1551. rpath += runtimeFlag;
  1552. }
  1553. rpath += runtimeSep;
  1554. }
  1555. return true;
  1556. }
  1557. /**
  1558. * Output the linking rules on a command line. For executables,
  1559. * targetLibrary should be a NULL pointer. For libraries, it should point
  1560. * to the name of the library. This will not link a library against itself.
  1561. */
  1562. void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
  1563. cmTarget& tgt,
  1564. bool relink)
  1565. {
  1566. std::string rpath;
  1567. std::string linkLibs;
  1568. unsigned int minBuildRpathSize = 0;
  1569. if ((relink==false)
  1570. && this->Makefile->IsOn("CMAKE_USE_CHRPATH")
  1571. && (tgt.IsChrpathAvailable()))
  1572. {
  1573. std::string installRpath;
  1574. std::string dummy;
  1575. this->GetLinkerArgs(installRpath, dummy, tgt, true, 0);
  1576. minBuildRpathSize = static_cast<unsigned int>(installRpath.size());
  1577. }
  1578. if (!this->GetLinkerArgs(rpath, linkLibs, tgt, relink, minBuildRpathSize))
  1579. {
  1580. return;
  1581. }
  1582. const char* linkLanguage =
  1583. tgt.GetLinkerLanguage(this->GetGlobalGenerator());
  1584. if(!linkLanguage)
  1585. {
  1586. cmSystemTools::
  1587. Error("CMake can not determine linker language for target:",
  1588. tgt.GetName());
  1589. return;
  1590. }
  1591. fout << linkLibs;
  1592. fout << rpath << " ";
  1593. // Add standard libraries for this language.
  1594. std::string standardLibsVar = "CMAKE_";
  1595. standardLibsVar += linkLanguage;
  1596. standardLibsVar += "_STANDARD_LIBRARIES";
  1597. if(const char* stdLibs =
  1598. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1599. {
  1600. fout << stdLibs << " ";
  1601. }
  1602. }
  1603. //----------------------------------------------------------------------------
  1604. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1605. const char* lang,
  1606. const char* config)
  1607. {
  1608. // Add language-specific flags.
  1609. std::string flagsVar = "CMAKE_";
  1610. flagsVar += lang;
  1611. flagsVar += "_FLAGS";
  1612. if(this->EmitUniversalBinaryFlags)
  1613. {
  1614. const char* osxArch =
  1615. this->Makefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  1616. const char* sysroot =
  1617. this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1618. if(osxArch && sysroot && lang && lang[0] =='C')
  1619. {
  1620. std::vector<std::string> archs;
  1621. cmSystemTools::ExpandListArgument(std::string(osxArch),
  1622. archs);
  1623. bool addArchFlag = true;
  1624. if(archs.size() == 1)
  1625. {
  1626. const char* archOrig =
  1627. this->Makefile->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES_DEFAULT");
  1628. if(archs[0] == archOrig)
  1629. {
  1630. addArchFlag = false;
  1631. }
  1632. }
  1633. // if there is more than one arch add the -arch and
  1634. // -isysroot flags, or if there is one arch flag, but
  1635. // it is not the default -arch flag for the system, then
  1636. // add it. Otherwize do not add -arch and -isysroot
  1637. if(addArchFlag)
  1638. {
  1639. for( std::vector<std::string>::iterator i = archs.begin();
  1640. i != archs.end(); ++i)
  1641. {
  1642. flags += " -arch ";
  1643. flags += *i;
  1644. }
  1645. flags += " -isysroot ";
  1646. flags += sysroot;
  1647. }
  1648. }
  1649. }
  1650. this->AddConfigVariableFlags(flags, flagsVar.c_str(), config);
  1651. }
  1652. //----------------------------------------------------------------------------
  1653. std::string cmLocalGenerator::GetRealDependency(const char* inName,
  1654. const char* config)
  1655. {
  1656. // Older CMake code may specify the dependency using the target
  1657. // output file rather than the target name. Such code would have
  1658. // been written before there was support for target properties that
  1659. // modify the name so stripping down to just the file name should
  1660. // produce the target name in this case.
  1661. std::string name = cmSystemTools::GetFilenameName(inName);
  1662. if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
  1663. {
  1664. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1665. }
  1666. // Look for a CMake target with the given name.
  1667. if(cmTarget* target = this->GlobalGenerator->FindTarget(0,name.c_str(),true))
  1668. {
  1669. // make sure it is not just a coincidence that the target name
  1670. // found is part of the inName
  1671. if(cmSystemTools::FileIsFullPath(inName))
  1672. {
  1673. std::string tLocation = target->GetLocation(config);
  1674. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1675. std::string depLocation = cmSystemTools::GetFilenamePath(
  1676. std::string(inName));
  1677. depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
  1678. tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
  1679. if(depLocation != tLocation)
  1680. {
  1681. // it is a full path to a depend that has the same name
  1682. // as a target but is in a different location so do not use
  1683. // the target as the depend
  1684. return inName;
  1685. }
  1686. }
  1687. switch (target->GetType())
  1688. {
  1689. case cmTarget::EXECUTABLE:
  1690. case cmTarget::STATIC_LIBRARY:
  1691. case cmTarget::SHARED_LIBRARY:
  1692. case cmTarget::MODULE_LIBRARY:
  1693. {
  1694. // Get the location of the target's output file and depend on it.
  1695. if(const char* location = target->GetLocation(config))
  1696. {
  1697. return location;
  1698. }
  1699. }
  1700. break;
  1701. case cmTarget::UTILITY:
  1702. case cmTarget::GLOBAL_TARGET:
  1703. // Depending on a utility target may not work but just trust
  1704. // the user to have given a valid name.
  1705. return inName;
  1706. case cmTarget::INSTALL_FILES:
  1707. case cmTarget::INSTALL_PROGRAMS:
  1708. case cmTarget::INSTALL_DIRECTORY:
  1709. break;
  1710. }
  1711. }
  1712. // The name was not that of a CMake target. It must name a file.
  1713. if(cmSystemTools::FileIsFullPath(inName))
  1714. {
  1715. // This is a full path. Return it as given.
  1716. return inName;
  1717. }
  1718. // Treat the name as relative to the source directory in which it
  1719. // was given.
  1720. name = this->Makefile->GetCurrentDirectory();
  1721. name += "/";
  1722. name += inName;
  1723. return name;
  1724. }
  1725. //----------------------------------------------------------------------------
  1726. std::string cmLocalGenerator::GetRealLocation(const char* inName,
  1727. const char* config)
  1728. {
  1729. std::string outName=inName;
  1730. // Look for a CMake target with the given name, which is an executable
  1731. // and which can be run
  1732. cmTarget* target = this->GlobalGenerator->FindTarget(0, inName, true);
  1733. if ((target != 0)
  1734. && (target->GetType() == cmTarget::EXECUTABLE)
  1735. && ((this->Makefile->IsOn("CMAKE_CROSSCOMPILING") == false)
  1736. || (target->IsImported() == true)))
  1737. {
  1738. outName = target->GetLocation( config );
  1739. }
  1740. return outName;
  1741. }
  1742. //----------------------------------------------------------------------------
  1743. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1744. const char* lang,
  1745. bool shared)
  1746. {
  1747. std::string flagsVar;
  1748. // Add flags for dealing with shared libraries for this language.
  1749. if(shared)
  1750. {
  1751. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1752. flagsVar += lang;
  1753. flagsVar += "_FLAGS";
  1754. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1755. }
  1756. // Add flags specific to shared builds.
  1757. if(cmSystemTools::IsOn(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1758. {
  1759. flagsVar = "CMAKE_SHARED_BUILD_";
  1760. flagsVar += lang;
  1761. flagsVar += "_FLAGS";
  1762. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1763. }
  1764. }
  1765. //----------------------------------------------------------------------------
  1766. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1767. const char* var,
  1768. const char* config)
  1769. {
  1770. // Add the flags from the variable itself.
  1771. std::string flagsVar = var;
  1772. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1773. // Add the flags from the build-type specific variable.
  1774. if(config && *config)
  1775. {
  1776. flagsVar += "_";
  1777. flagsVar += cmSystemTools::UpperCase(config);
  1778. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1779. }
  1780. }
  1781. //----------------------------------------------------------------------------
  1782. void cmLocalGenerator::AppendFlags(std::string& flags,
  1783. const char* newFlags)
  1784. {
  1785. if(newFlags && *newFlags)
  1786. {
  1787. std::string newf = newFlags;
  1788. if(flags.size())
  1789. {
  1790. flags += " ";
  1791. }
  1792. flags += newFlags;
  1793. }
  1794. }
  1795. //----------------------------------------------------------------------------
  1796. void cmLocalGenerator::AppendDefines(std::string& defines,
  1797. const char* defines_list,
  1798. const char* lang)
  1799. {
  1800. // Short-circuit if there are no definitions.
  1801. if(!defines_list)
  1802. {
  1803. return;
  1804. }
  1805. // Expand the list of definitions.
  1806. std::vector<std::string> defines_vec;
  1807. cmSystemTools::ExpandListArgument(defines_list, defines_vec);
  1808. // Short-circuit if there are no definitions.
  1809. if(defines_vec.empty())
  1810. {
  1811. return;
  1812. }
  1813. // Lookup the define flag for the current language.
  1814. std::string dflag = "-D";
  1815. if(lang)
  1816. {
  1817. std::string defineFlagVar = "CMAKE_";
  1818. defineFlagVar += lang;
  1819. defineFlagVar += "_DEFINE_FLAG";
  1820. const char* df = this->Makefile->GetDefinition(defineFlagVar.c_str());
  1821. if(df && *df)
  1822. {
  1823. dflag = df;
  1824. }
  1825. }
  1826. // Add each definition to the command line with appropriate escapes.
  1827. const char* dsep = defines.empty()? "" : " ";
  1828. for(std::vector<std::string>::const_iterator di = defines_vec.begin();
  1829. di != defines_vec.end(); ++di)
  1830. {
  1831. // Skip unsupported definitions.
  1832. if(!this->CheckDefinition(*di))
  1833. {
  1834. continue;
  1835. }
  1836. // Separate from previous definitions.
  1837. defines += dsep;
  1838. dsep = " ";
  1839. // Append the definition with proper escaping.
  1840. defines += dflag;
  1841. if(this->WatcomWMake)
  1842. {
  1843. // The Watcom compiler does its own command line parsing instead
  1844. // of using the windows shell rules. Definitions are one of
  1845. // -DNAME
  1846. // -DNAME=<cpp-token>
  1847. // -DNAME="c-string with spaces and other characters(?@#$)"
  1848. //
  1849. // Watcom will properly parse each of these cases from the
  1850. // command line without any escapes. However we still have to
  1851. // get the '$' and '#' characters through WMake as '$$' and
  1852. // '$#'.
  1853. for(const char* c = di->c_str(); *c; ++c)
  1854. {
  1855. if(*c == '$' || *c == '#')
  1856. {
  1857. defines += '$';
  1858. }
  1859. defines += *c;
  1860. }
  1861. }
  1862. else
  1863. {
  1864. // Make the definition appear properly on the command line.
  1865. defines += this->EscapeForShell(di->c_str(), true);
  1866. }
  1867. }
  1868. }
  1869. //----------------------------------------------------------------------------
  1870. std::string
  1871. cmLocalGenerator::ConstructComment(const cmCustomCommand& cc,
  1872. const char* default_comment)
  1873. {
  1874. // Check for a comment provided with the command.
  1875. if(cc.GetComment())
  1876. {
  1877. return cc.GetComment();
  1878. }
  1879. // Construct a reasonable default comment if possible.
  1880. if(!cc.GetOutputs().empty())
  1881. {
  1882. std::string comment;
  1883. comment = "Generating ";
  1884. const char* sep = "";
  1885. for(std::vector<std::string>::const_iterator o = cc.GetOutputs().begin();
  1886. o != cc.GetOutputs().end(); ++o)
  1887. {
  1888. comment += sep;
  1889. comment += this->Convert(o->c_str(), cmLocalGenerator::START_OUTPUT);
  1890. sep = ", ";
  1891. }
  1892. return comment;
  1893. }
  1894. // Otherwise use the provided default.
  1895. return default_comment;
  1896. }
  1897. //----------------------------------------------------------------------------
  1898. std::string
  1899. cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
  1900. {
  1901. return this->Convert(remote, START_OUTPUT, SHELL, true);
  1902. }
  1903. //----------------------------------------------------------------------------
  1904. std::string cmLocalGenerator::Convert(const char* source,
  1905. RelativeRoot relative,
  1906. OutputFormat output,
  1907. bool optional)
  1908. {
  1909. // Make sure the relative path conversion components are set.
  1910. if(!this->PathConversionsSetup)
  1911. {
  1912. this->SetupPathConversions();
  1913. this->PathConversionsSetup = true;
  1914. }
  1915. // Convert the path to a relative path.
  1916. std::string result = source;
  1917. if (!optional || this->UseRelativePaths)
  1918. {
  1919. switch (relative)
  1920. {
  1921. case HOME:
  1922. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1923. result = this->ConvertToRelativePath(this->HomeDirectoryComponents,
  1924. result.c_str());
  1925. break;
  1926. case START:
  1927. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1928. result = this->ConvertToRelativePath(this->StartDirectoryComponents,
  1929. result.c_str());
  1930. break;
  1931. case HOME_OUTPUT:
  1932. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1933. result =
  1934. this->ConvertToRelativePath(this->HomeOutputDirectoryComponents,
  1935. result.c_str());
  1936. break;
  1937. case START_OUTPUT:
  1938. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1939. result =
  1940. this->ConvertToRelativePath(this->StartOutputDirectoryComponents,
  1941. result.c_str());
  1942. break;
  1943. case FULL:
  1944. result = cmSystemTools::CollapseFullPath(result.c_str());
  1945. break;
  1946. case NONE:
  1947. break;
  1948. }
  1949. }
  1950. // Now convert it to an output path.
  1951. if (output == MAKEFILE)
  1952. {
  1953. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  1954. }
  1955. else if( output == SHELL)
  1956. {
  1957. // For the MSYS shell convert drive letters to posix paths, so
  1958. // that c:/some/path becomes /c/some/path. This is needed to
  1959. // avoid problems with the shell path translation.
  1960. if(this->MSYSShell)
  1961. {
  1962. if(result.size() > 2 && result[1] == ':')
  1963. {
  1964. result[1] = result[0];
  1965. result[0] = '/';
  1966. }
  1967. }
  1968. if(this->WindowsShell)
  1969. {
  1970. std::string::size_type pos = 0;
  1971. while((pos = result.find('/', pos)) != std::string::npos)
  1972. {
  1973. result[pos] = '\\';
  1974. pos++;
  1975. }
  1976. }
  1977. result = this->EscapeForShell(result.c_str(), true, false);
  1978. }
  1979. return result;
  1980. }
  1981. //----------------------------------------------------------------------------
  1982. std::string cmLocalGenerator::FindRelativePathTopSource()
  1983. {
  1984. // Relative path conversion within a single tree managed by CMake is
  1985. // safe. We can use our parent relative path top if and only if
  1986. // this is a subdirectory of that top.
  1987. if(cmLocalGenerator* parent = this->GetParent())
  1988. {
  1989. std::string parentTop = parent->FindRelativePathTopSource();
  1990. if(cmSystemTools::IsSubDirectory(
  1991. this->Makefile->GetStartDirectory(), parentTop.c_str()))
  1992. {
  1993. return parentTop;
  1994. }
  1995. }
  1996. // Otherwise this directory itself is the new top.
  1997. return this->Makefile->GetStartDirectory();
  1998. }
  1999. //----------------------------------------------------------------------------
  2000. std::string cmLocalGenerator::FindRelativePathTopBinary()
  2001. {
  2002. // Relative path conversion within a single tree managed by CMake is
  2003. // safe. We can use our parent relative path top if and only if
  2004. // this is a subdirectory of that top.
  2005. if(cmLocalGenerator* parent = this->GetParent())
  2006. {
  2007. std::string parentTop = parent->FindRelativePathTopBinary();
  2008. if(cmSystemTools::IsSubDirectory(
  2009. this->Makefile->GetStartOutputDirectory(), parentTop.c_str()))
  2010. {
  2011. return parentTop;
  2012. }
  2013. }
  2014. // Otherwise this directory itself is the new top.
  2015. return this->Makefile->GetStartOutputDirectory();
  2016. }
  2017. //----------------------------------------------------------------------------
  2018. void cmLocalGenerator::ConfigureRelativePaths()
  2019. {
  2020. // Relative path conversion inside the source tree is not used to
  2021. // construct relative paths passed to build tools so it is safe to
  2022. // even when the source is a network path.
  2023. std::string source = this->FindRelativePathTopSource();
  2024. this->RelativePathTopSource = source;
  2025. // The current working directory on Windows cannot be a network
  2026. // path. Therefore relative paths cannot work when the binary tree
  2027. // is a network path.
  2028. std::string binary = this->FindRelativePathTopBinary();
  2029. if(binary.size() < 2 || binary.substr(0, 2) != "//")
  2030. {
  2031. this->RelativePathTopBinary = binary;
  2032. }
  2033. else
  2034. {
  2035. this->RelativePathTopBinary = "";
  2036. }
  2037. }
  2038. //----------------------------------------------------------------------------
  2039. static bool cmLocalGeneratorNotAbove(const char* a, const char* b)
  2040. {
  2041. return (cmSystemTools::ComparePath(a, b) ||
  2042. cmSystemTools::IsSubDirectory(a, b));
  2043. }
  2044. //----------------------------------------------------------------------------
  2045. std::string
  2046. cmLocalGenerator::ConvertToRelativePath(const std::vector<std::string>& local,
  2047. const char* in_remote)
  2048. {
  2049. // The path should never be quoted.
  2050. assert(in_remote[0] != '\"');
  2051. // The local path should never have a trailing slash.
  2052. assert(local.size() > 0 && !(local[local.size()-1] == ""));
  2053. // If the path is already relative then just return the path.
  2054. if(!cmSystemTools::FileIsFullPath(in_remote))
  2055. {
  2056. return in_remote;
  2057. }
  2058. // Make sure relative path conversion is configured.
  2059. if(!this->RelativePathsConfigured)
  2060. {
  2061. this->ConfigureRelativePaths();
  2062. this->RelativePathsConfigured = true;
  2063. }
  2064. // Skip conversion if the path and local are not both in the source
  2065. // or both in the binary tree.
  2066. std::string local_path = cmSystemTools::JoinPath(local);
  2067. if(!((cmLocalGeneratorNotAbove(local_path.c_str(),
  2068. this->RelativePathTopBinary.c_str()) &&
  2069. cmLocalGeneratorNotAbove(in_remote,
  2070. this->RelativePathTopBinary.c_str())) ||
  2071. (cmLocalGeneratorNotAbove(local_path.c_str(),
  2072. this->RelativePathTopSource.c_str()) &&
  2073. cmLocalGeneratorNotAbove(in_remote,
  2074. this->RelativePathTopSource.c_str()))))
  2075. {
  2076. return in_remote;
  2077. }
  2078. // Identify the longest shared path component between the remote
  2079. // path and the local path.
  2080. std::vector<std::string> remote;
  2081. cmSystemTools::SplitPath(in_remote, remote);
  2082. unsigned int common=0;
  2083. while(common < remote.size() &&
  2084. common < local.size() &&
  2085. cmSystemTools::ComparePath(remote[common].c_str(),
  2086. local[common].c_str()))
  2087. {
  2088. ++common;
  2089. }
  2090. // If no part of the path is in common then return the full path.
  2091. if(common == 0)
  2092. {
  2093. return in_remote;
  2094. }
  2095. // If the entire path is in common then just return a ".".
  2096. if(common == remote.size() &&
  2097. common == local.size())
  2098. {
  2099. return ".";
  2100. }
  2101. // If the entire path is in common except for a trailing slash then
  2102. // just return a "./".
  2103. if(common+1 == remote.size() &&
  2104. remote[common].size() == 0 &&
  2105. common == local.size())
  2106. {
  2107. return "./";
  2108. }
  2109. // Construct the relative path.
  2110. std::string relative;
  2111. // First add enough ../ to get up to the level of the shared portion
  2112. // of the path. Leave off the trailing slash. Note that the last
  2113. // component of local will never be empty because local should never
  2114. // have a trailing slash.
  2115. for(unsigned int i=common; i < local.size(); ++i)
  2116. {
  2117. relative += "..";
  2118. if(i < local.size()-1)
  2119. {
  2120. relative += "/";
  2121. }
  2122. }
  2123. // Now add the portion of the destination path that is not included
  2124. // in the shared portion of the path. Add a slash the first time
  2125. // only if there was already something in the path. If there was a
  2126. // trailing slash in the input then the last iteration of the loop
  2127. // will add a slash followed by an empty string which will preserve
  2128. // the trailing slash in the output.
  2129. for(unsigned int i=common; i < remote.size(); ++i)
  2130. {
  2131. if(relative.size() > 0)
  2132. {
  2133. relative += "/";
  2134. }
  2135. relative += remote[i];
  2136. }
  2137. // Finally return the path.
  2138. return relative;
  2139. }
  2140. //----------------------------------------------------------------------------
  2141. void
  2142. cmLocalGenerator
  2143. ::GenerateTargetInstallRules(
  2144. std::ostream& os, const char* config,
  2145. std::vector<std::string> const& configurationTypes)
  2146. {
  2147. // Convert the old-style install specification from each target to
  2148. // an install generator and run it.
  2149. cmTargets& tgts = this->Makefile->GetTargets();
  2150. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  2151. {
  2152. // Include the user-specified pre-install script for this target.
  2153. if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
  2154. {
  2155. cmInstallScriptGenerator g(preinstall, false, 0);
  2156. g.Generate(os, config, configurationTypes);
  2157. }
  2158. // Install this target if a destination is given.
  2159. if(l->second.GetInstallPath() != "")
  2160. {
  2161. // Compute the full install destination. Note that converting
  2162. // to unix slashes also removes any trailing slash.
  2163. std::string destination = "${CMAKE_INSTALL_PREFIX}";
  2164. destination += l->second.GetInstallPath();
  2165. cmSystemTools::ConvertToUnixSlashes(destination);
  2166. // Generate the proper install generator for this target type.
  2167. switch(l->second.GetType())
  2168. {
  2169. case cmTarget::EXECUTABLE:
  2170. case cmTarget::STATIC_LIBRARY:
  2171. case cmTarget::MODULE_LIBRARY:
  2172. {
  2173. // Use a target install generator.
  2174. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  2175. g.Generate(os, config, configurationTypes);
  2176. }
  2177. break;
  2178. case cmTarget::SHARED_LIBRARY:
  2179. {
  2180. #if defined(_WIN32) || defined(__CYGWIN__)
  2181. // Special code to handle DLL. Install the import library
  2182. // to the normal destination and the DLL to the runtime
  2183. // destination.
  2184. cmInstallTargetGenerator g1(l->second, destination.c_str(), true);
  2185. g1.Generate(os, config, configurationTypes);
  2186. destination = "${CMAKE_INSTALL_PREFIX}";
  2187. destination += l->second.GetRuntimeInstallPath();
  2188. cmSystemTools::ConvertToUnixSlashes(destination);
  2189. cmInstallTargetGenerator g2(l->second, destination.c_str(), false);
  2190. g2.Generate(os, config, configurationTypes);
  2191. #else
  2192. // Use a target install generator.
  2193. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  2194. g.Generate(os, config, configurationTypes);
  2195. #endif
  2196. }
  2197. break;
  2198. default:
  2199. break;
  2200. }
  2201. }
  2202. // Include the user-specified post-install script for this target.
  2203. if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
  2204. {
  2205. cmInstallScriptGenerator g(postinstall, false, 0);
  2206. g.Generate(os, config, configurationTypes);
  2207. }
  2208. }
  2209. }
  2210. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2211. static std::string cmLocalGeneratorMD5(const char* input)
  2212. {
  2213. char md5out[32];
  2214. cmsysMD5* md5 = cmsysMD5_New();
  2215. cmsysMD5_Initialize(md5);
  2216. cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
  2217. cmsysMD5_FinalizeHex(md5, md5out);
  2218. cmsysMD5_Delete(md5);
  2219. return std::string(md5out, 32);
  2220. }
  2221. static bool
  2222. cmLocalGeneratorShortenObjectName(std::string& objName,
  2223. std::string::size_type max_len)
  2224. {
  2225. // Replace the beginning of the path portion of the object name with
  2226. // its own md5 sum.
  2227. std::string::size_type pos = objName.find('/', objName.size()-max_len+32);
  2228. if(pos != objName.npos)
  2229. {
  2230. std::string md5name = cmLocalGeneratorMD5(objName.substr(0, pos).c_str());
  2231. md5name += objName.substr(pos);
  2232. objName = md5name;
  2233. // The object name is now short enough.
  2234. return true;
  2235. }
  2236. else
  2237. {
  2238. // The object name could not be shortened enough.
  2239. return false;
  2240. }
  2241. }
  2242. static bool cmLocalGeneratorCheckObjectName(std::string& objName,
  2243. std::string::size_type dir_len)
  2244. {
  2245. // Choose a maximum file name length.
  2246. #if defined(_WIN32) || defined(__CYGWIN__)
  2247. std::string::size_type const max_total_len = 250;
  2248. #else
  2249. std::string::size_type const max_total_len = 1000;
  2250. #endif
  2251. // Enforce the maximum file name length if possible.
  2252. std::string::size_type max_obj_len = max_total_len;
  2253. if(dir_len < max_total_len)
  2254. {
  2255. max_obj_len = max_total_len - dir_len;
  2256. if(objName.size() > max_obj_len)
  2257. {
  2258. // The current object file name is too long. Try to shorten it.
  2259. return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
  2260. }
  2261. else
  2262. {
  2263. // The object file name is short enough.
  2264. return true;
  2265. }
  2266. }
  2267. else
  2268. {
  2269. // The build directory in which the object will be stored is
  2270. // already too deep.
  2271. return false;
  2272. }
  2273. }
  2274. #endif
  2275. //----------------------------------------------------------------------------
  2276. std::string&
  2277. cmLocalGenerator
  2278. ::CreateSafeUniqueObjectFileName(const char* sin,
  2279. std::string::size_type dir_len)
  2280. {
  2281. // Look for an existing mapped name for this object file.
  2282. std::map<cmStdString,cmStdString>::iterator it =
  2283. this->UniqueObjectNamesMap.find(sin);
  2284. // If no entry exists create one.
  2285. if(it == this->UniqueObjectNamesMap.end())
  2286. {
  2287. // Start with the original name.
  2288. std::string ssin = sin;
  2289. // Avoid full paths by removing leading slashes.
  2290. std::string::size_type pos = 0;
  2291. for(;pos < ssin.size() && ssin[pos] == '/'; ++pos);
  2292. ssin = ssin.substr(pos);
  2293. // Avoid full paths by removing colons.
  2294. cmSystemTools::ReplaceString(ssin, ":", "_");
  2295. // Avoid relative paths that go up the tree.
  2296. cmSystemTools::ReplaceString(ssin, "../", "__/");
  2297. // Avoid spaces.
  2298. cmSystemTools::ReplaceString(ssin, " ", "_");
  2299. // Mangle the name if necessary.
  2300. if(this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
  2301. {
  2302. bool done;
  2303. int cc = 0;
  2304. char rpstr[100];
  2305. sprintf(rpstr, "_p_");
  2306. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2307. std::string sssin = sin;
  2308. do
  2309. {
  2310. done = true;
  2311. for ( it = this->UniqueObjectNamesMap.begin();
  2312. it != this->UniqueObjectNamesMap.end();
  2313. ++ it )
  2314. {
  2315. if ( it->second == ssin )
  2316. {
  2317. done = false;
  2318. }
  2319. }
  2320. if ( done )
  2321. {
  2322. break;
  2323. }
  2324. sssin = ssin;
  2325. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2326. sprintf(rpstr, "_p%d_", cc++);
  2327. }
  2328. while ( !done );
  2329. }
  2330. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2331. cmLocalGeneratorCheckObjectName(ssin, dir_len);
  2332. #else
  2333. (void)dir_len;
  2334. #endif
  2335. // Insert the newly mapped object file name.
  2336. std::map<cmStdString, cmStdString>::value_type e(sin, ssin);
  2337. it = this->UniqueObjectNamesMap.insert(e).first;
  2338. }
  2339. // Return the map entry.
  2340. return it->second;
  2341. }
  2342. //----------------------------------------------------------------------------
  2343. std::string
  2344. cmLocalGenerator
  2345. ::GetObjectFileNameWithoutTarget(const cmSourceFile& source,
  2346. std::string::size_type dir_len,
  2347. bool* hasSourceExtension)
  2348. {
  2349. // Construct the object file name using the full path to the source
  2350. // file which is its only unique identification.
  2351. const char* fullPath = source.GetFullPath().c_str();
  2352. // Try referencing the source relative to the source tree.
  2353. std::string relFromSource = this->Convert(fullPath, START);
  2354. assert(!relFromSource.empty());
  2355. bool relSource = !cmSystemTools::FileIsFullPath(relFromSource.c_str());
  2356. bool subSource = relSource && relFromSource[0] != '.';
  2357. // Try referencing the source relative to the binary tree.
  2358. std::string relFromBinary = this->Convert(fullPath, START_OUTPUT);
  2359. assert(!relFromBinary.empty());
  2360. bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary.c_str());
  2361. bool subBinary = relBinary && relFromBinary[0] != '.';
  2362. // Select a nice-looking reference to the source file to construct
  2363. // the object file name.
  2364. std::string objectName;
  2365. if((relSource && !relBinary) || (subSource && !subBinary))
  2366. {
  2367. objectName = relFromSource;
  2368. }
  2369. else if((relBinary && !relSource) || (subBinary && !subSource))
  2370. {
  2371. objectName = relFromBinary;
  2372. }
  2373. else if(relFromBinary.length() < relFromSource.length())
  2374. {
  2375. objectName = relFromBinary;
  2376. }
  2377. else
  2378. {
  2379. objectName = relFromSource;
  2380. }
  2381. // if it is still a full path check for the try compile case
  2382. // try compile never have in source sources, and should not
  2383. // have conflicting source file names in the same target
  2384. if(cmSystemTools::FileIsFullPath(objectName.c_str()))
  2385. {
  2386. if(this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile())
  2387. {
  2388. objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
  2389. }
  2390. }
  2391. // Replace the original source file extension with the object file
  2392. // extension.
  2393. bool keptSourceExtension = true;
  2394. if(!source.GetPropertyAsBool("KEEP_EXTENSION"))
  2395. {
  2396. // Decide whether this language wants to replace the source
  2397. // extension with the object extension. For CMake 2.4
  2398. // compatibility do this by default.
  2399. bool replaceExt = this->NeedBackwardsCompatibility(2, 4);
  2400. if(!replaceExt)
  2401. {
  2402. std::string repVar = "CMAKE_";
  2403. repVar += source.GetLanguage();
  2404. repVar += "_OUTPUT_EXTENSION_REPLACE";
  2405. replaceExt = this->Makefile->IsOn(repVar.c_str());
  2406. }
  2407. // Remove the source extension if it is to be replaced.
  2408. if(replaceExt)
  2409. {
  2410. keptSourceExtension = false;
  2411. std::string::size_type dot_pos = objectName.rfind(".");
  2412. if(dot_pos != std::string::npos)
  2413. {
  2414. objectName = objectName.substr(0, dot_pos);
  2415. }
  2416. }
  2417. // Store the new extension.
  2418. objectName +=
  2419. this->GlobalGenerator->GetLanguageOutputExtension(source);
  2420. }
  2421. if(hasSourceExtension)
  2422. {
  2423. *hasSourceExtension = keptSourceExtension;
  2424. }
  2425. // Convert to a safe name.
  2426. return this->CreateSafeUniqueObjectFileName(objectName.c_str(), dir_len);
  2427. }
  2428. //----------------------------------------------------------------------------
  2429. const char*
  2430. cmLocalGenerator
  2431. ::GetSourceFileLanguage(const cmSourceFile& source)
  2432. {
  2433. return source.GetLanguage();
  2434. }
  2435. //----------------------------------------------------------------------------
  2436. std::string cmLocalGenerator::EscapeForShellOldStyle(const char* str)
  2437. {
  2438. std::string result;
  2439. bool forceOn = cmSystemTools::GetForceUnixPaths();
  2440. if(forceOn && this->WindowsShell)
  2441. {
  2442. cmSystemTools::SetForceUnixPaths(false);
  2443. }
  2444. result = cmSystemTools::EscapeSpaces(str);
  2445. if(forceOn && this->WindowsShell)
  2446. {
  2447. cmSystemTools::SetForceUnixPaths(true);
  2448. }
  2449. return result;
  2450. }
  2451. //----------------------------------------------------------------------------
  2452. std::string cmLocalGenerator::EscapeForShell(const char* str, bool makeVars,
  2453. bool forEcho)
  2454. {
  2455. // Compute the flags for the target shell environment.
  2456. int flags = 0;
  2457. if(this->WindowsVSIDE)
  2458. {
  2459. flags |= cmsysSystem_Shell_Flag_VSIDE;
  2460. }
  2461. else
  2462. {
  2463. flags |= cmsysSystem_Shell_Flag_Make;
  2464. }
  2465. if(makeVars)
  2466. {
  2467. flags |= cmsysSystem_Shell_Flag_AllowMakeVariables;
  2468. }
  2469. if(forEcho)
  2470. {
  2471. flags |= cmsysSystem_Shell_Flag_EchoWindows;
  2472. }
  2473. if(this->WatcomWMake)
  2474. {
  2475. flags |= cmsysSystem_Shell_Flag_WatcomWMake;
  2476. }
  2477. if(this->MinGWMake)
  2478. {
  2479. flags |= cmsysSystem_Shell_Flag_MinGWMake;
  2480. }
  2481. if(this->NMake)
  2482. {
  2483. flags |= cmsysSystem_Shell_Flag_NMake;
  2484. }
  2485. // Compute the buffer size needed.
  2486. int size = (this->WindowsShell ?
  2487. cmsysSystem_Shell_GetArgumentSizeForWindows(str, flags) :
  2488. cmsysSystem_Shell_GetArgumentSizeForUnix(str, flags));
  2489. // Compute the shell argument itself.
  2490. std::vector<char> arg(size);
  2491. if(this->WindowsShell)
  2492. {
  2493. cmsysSystem_Shell_GetArgumentForWindows(str, &arg[0], flags);
  2494. }
  2495. else
  2496. {
  2497. cmsysSystem_Shell_GetArgumentForUnix(str, &arg[0], flags);
  2498. }
  2499. return std::string(&arg[0]);
  2500. }
  2501. //----------------------------------------------------------------------------
  2502. std::string cmLocalGenerator::EscapeForCMake(const char* str)
  2503. {
  2504. // Always double-quote the argument to take care of most escapes.
  2505. std::string result = "\"";
  2506. for(const char* c = str; *c; ++c)
  2507. {
  2508. if(*c == '"')
  2509. {
  2510. // Escape the double quote to avoid ending the argument.
  2511. result += "\\\"";
  2512. }
  2513. else if(*c == '$')
  2514. {
  2515. // Escape the dollar to avoid expanding variables.
  2516. result += "\\$";
  2517. }
  2518. else if(*c == '\\')
  2519. {
  2520. // Escape the backslash to avoid other escapes.
  2521. result += "\\\\";
  2522. }
  2523. else
  2524. {
  2525. // Other characters will be parsed correctly.
  2526. result += *c;
  2527. }
  2528. }
  2529. result += "\"";
  2530. return result;
  2531. }
  2532. //----------------------------------------------------------------------------
  2533. std::string
  2534. cmLocalGenerator::GetTargetDirectory(cmTarget const&) const
  2535. {
  2536. cmSystemTools::Error("GetTargetDirectory"
  2537. " called on cmLocalGenerator");
  2538. return "";
  2539. }
  2540. //----------------------------------------------------------------------------
  2541. void
  2542. cmLocalGenerator::GetTargetObjectFileDirectories(cmTarget* ,
  2543. std::vector<std::string>&
  2544. )
  2545. {
  2546. cmSystemTools::Error("GetTargetObjectFileDirectories"
  2547. " called on cmLocalGenerator");
  2548. }
  2549. //----------------------------------------------------------------------------
  2550. unsigned int cmLocalGenerator::GetBackwardsCompatibility()
  2551. {
  2552. // The computed version may change until the project is fully
  2553. // configured.
  2554. if(!this->BackwardsCompatibilityFinal)
  2555. {
  2556. unsigned int major = 0;
  2557. unsigned int minor = 0;
  2558. unsigned int patch = 0;
  2559. if(const char* value
  2560. = this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY"))
  2561. {
  2562. switch(sscanf(value, "%u.%u.%u", &major, &minor, &patch))
  2563. {
  2564. case 2: patch = 0; break;
  2565. case 1: minor = 0; patch = 0; break;
  2566. default: break;
  2567. }
  2568. }
  2569. this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
  2570. this->BackwardsCompatibilityFinal = this->Configured;
  2571. }
  2572. return this->BackwardsCompatibility;
  2573. }
  2574. //----------------------------------------------------------------------------
  2575. bool cmLocalGenerator::NeedBackwardsCompatibility(unsigned int major,
  2576. unsigned int minor,
  2577. unsigned int patch)
  2578. {
  2579. // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
  2580. // equal to or lower than the given version.
  2581. unsigned int actual_compat = this->GetBackwardsCompatibility();
  2582. return (actual_compat &&
  2583. actual_compat <= CMake_VERSION_ENCODE(major, minor, patch));
  2584. }
  2585. //----------------------------------------------------------------------------
  2586. bool cmLocalGenerator::CheckDefinition(std::string const& define) const
  2587. {
  2588. // Many compilers do not support -DNAME(arg)=sdf so we disable it.
  2589. bool function_style = false;
  2590. for(const char* c = define.c_str(); *c && *c != '='; ++c)
  2591. {
  2592. if(*c == '(')
  2593. {
  2594. function_style = true;
  2595. break;
  2596. }
  2597. }
  2598. if(function_style)
  2599. {
  2600. cmOStringStream e;
  2601. e << "WARNING: Function-style preprocessor definitions may not be "
  2602. << "passed on the compiler command line because many compilers "
  2603. << "do not support it.\n"
  2604. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2605. << "Consider defining the macro in a (configured) header file.\n";
  2606. cmSystemTools::Message(e.str().c_str());
  2607. return false;
  2608. }
  2609. // Many compilers do not support # in the value so we disable it.
  2610. if(define.find_first_of("#") != define.npos)
  2611. {
  2612. cmOStringStream e;
  2613. e << "WARNING: Peprocessor definitions containing '#' may not be "
  2614. << "passed on the compiler command line because many compilers "
  2615. << "do not support it.\n"
  2616. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2617. << "Consider defining the macro in a (configured) header file.\n";
  2618. cmSystemTools::Message(e.str().c_str());
  2619. return false;
  2620. }
  2621. // Assume it is supported.
  2622. return true;
  2623. }