cmLocalGenerator.cxx 87 KB

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