cmLocalGenerator.cxx 88 KB

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