cmLocalGenerator.cxx 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmLocalGenerator.h"
  11. #include "cmComputeLinkInformation.h"
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmInstallGenerator.h"
  15. #include "cmInstallFilesGenerator.h"
  16. #include "cmInstallScriptGenerator.h"
  17. #include "cmInstallTargetGenerator.h"
  18. #include "cmMakefile.h"
  19. #include "cmSourceFile.h"
  20. #include "cmTest.h"
  21. #include "cmTestGenerator.h"
  22. #include "cmVersion.h"
  23. #include "cmake.h"
  24. #if defined(CMAKE_BUILD_WITH_CMAKE)
  25. # define CM_LG_ENCODE_OBJECT_NAMES
  26. # include <cmsys/MD5.h>
  27. #endif
  28. #include <cmsys/System.h>
  29. #include <ctype.h> // for isalpha
  30. #include <assert.h>
  31. #if defined(__HAIKU__)
  32. #include <FindDirectory.h>
  33. #include <StorageDefs.h>
  34. #endif
  35. cmLocalGenerator::cmLocalGenerator()
  36. {
  37. this->Makefile = 0; // moved to after set on global
  38. this->Parent = 0;
  39. this->WindowsShell = false;
  40. this->WindowsVSIDE = false;
  41. this->WatcomWMake = false;
  42. this->MinGWMake = false;
  43. this->NMake = false;
  44. this->MSYSShell = false;
  45. this->LinkScriptShell = false;
  46. this->IgnoreLibPrefix = false;
  47. this->UseRelativePaths = false;
  48. this->Configured = false;
  49. this->EmitUniversalBinaryFlags = true;
  50. this->RelativePathsConfigured = false;
  51. this->PathConversionsSetup = false;
  52. this->BackwardsCompatibility = 0;
  53. this->BackwardsCompatibilityFinal = false;
  54. }
  55. cmLocalGenerator::~cmLocalGenerator()
  56. {
  57. delete this->Makefile;
  58. }
  59. //----------------------------------------------------------------------------
  60. class cmLocalGeneratorCurrent
  61. {
  62. cmGlobalGenerator* GG;
  63. cmLocalGenerator* LG;
  64. public:
  65. cmLocalGeneratorCurrent(cmLocalGenerator* lg)
  66. {
  67. this->GG = lg->GetGlobalGenerator();
  68. this->LG = this->GG->GetCurrentLocalGenerator();
  69. this->GG->SetCurrentLocalGenerator(lg);
  70. }
  71. ~cmLocalGeneratorCurrent()
  72. {
  73. this->GG->SetCurrentLocalGenerator(this->LG);
  74. }
  75. };
  76. //----------------------------------------------------------------------------
  77. void cmLocalGenerator::Configure()
  78. {
  79. // Manage the global generator's current local generator.
  80. cmLocalGeneratorCurrent clg(this);
  81. static_cast<void>(clg);
  82. // make sure the CMakeFiles dir is there
  83. std::string filesDir = this->Makefile->GetStartOutputDirectory();
  84. filesDir += cmake::GetCMakeFilesDirectory();
  85. cmSystemTools::MakeDirectory(filesDir.c_str());
  86. // find & read the list file
  87. this->ReadInputFile();
  88. // at the end of the ReadListFile handle any old style subdirs
  89. // first get all the subdirectories
  90. std::vector<cmLocalGenerator *> subdirs = this->GetChildren();
  91. // for each subdir recurse
  92. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  93. for (; sdi != subdirs.end(); ++sdi)
  94. {
  95. if (!(*sdi)->Configured)
  96. {
  97. this->Makefile->ConfigureSubDirectory(*sdi);
  98. }
  99. }
  100. // Check whether relative paths should be used for optionally
  101. // relative paths.
  102. this->UseRelativePaths = this->Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS");
  103. this->ComputeObjectMaxPath();
  104. this->Configured = true;
  105. }
  106. //----------------------------------------------------------------------------
  107. void cmLocalGenerator::ComputeObjectMaxPath()
  108. {
  109. // Choose a maximum object file name length.
  110. #if defined(_WIN32) || defined(__CYGWIN__)
  111. this->ObjectPathMax = 250;
  112. #else
  113. this->ObjectPathMax = 1000;
  114. #endif
  115. const char* plen = this->Makefile->GetDefinition("CMAKE_OBJECT_PATH_MAX");
  116. if(plen && *plen)
  117. {
  118. unsigned int pmax;
  119. if(sscanf(plen, "%u", &pmax) == 1)
  120. {
  121. if(pmax >= 128)
  122. {
  123. this->ObjectPathMax = pmax;
  124. }
  125. else
  126. {
  127. cmOStringStream w;
  128. w << "CMAKE_OBJECT_PATH_MAX is set to " << pmax
  129. << ", which is less than the minimum of 128. "
  130. << "The value will be ignored.";
  131. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  132. }
  133. }
  134. else
  135. {
  136. cmOStringStream w;
  137. w << "CMAKE_OBJECT_PATH_MAX is set to \"" << plen
  138. << "\", which fails to parse as a positive integer. "
  139. << "The value will be ignored.";
  140. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  141. }
  142. }
  143. this->ObjectMaxPathViolations.clear();
  144. }
  145. //----------------------------------------------------------------------------
  146. void cmLocalGenerator::ReadInputFile()
  147. {
  148. // Look for the CMakeLists.txt file.
  149. std::string currentStart = this->Makefile->GetStartDirectory();
  150. currentStart += "/CMakeLists.txt";
  151. if(cmSystemTools::FileExists(currentStart.c_str(), true))
  152. {
  153. this->Makefile->ReadListFile(currentStart.c_str());
  154. return;
  155. }
  156. if(!this->Parent)
  157. {
  158. return;
  159. }
  160. // The file is missing. Check policy CMP0014.
  161. cmMakefile* mf = this->Parent->GetMakefile();
  162. cmOStringStream e;
  163. e << "The source directory\n"
  164. << " " << this->Makefile->GetStartDirectory() << "\n"
  165. << "does not contain a CMakeLists.txt file.";
  166. switch (mf->GetPolicyStatus(cmPolicies::CMP0014))
  167. {
  168. case cmPolicies::WARN:
  169. // Print the warning.
  170. e << "\n"
  171. << "CMake does not support this case but it used "
  172. << "to work accidentally and is being allowed for "
  173. << "compatibility."
  174. << "\n"
  175. << mf->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0014);
  176. mf->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  177. case cmPolicies::OLD:
  178. // OLD behavior does not warn.
  179. return;
  180. case cmPolicies::REQUIRED_IF_USED:
  181. case cmPolicies::REQUIRED_ALWAYS:
  182. e << "\n"
  183. << mf->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0014);
  184. case cmPolicies::NEW:
  185. // NEW behavior prints the error.
  186. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  187. break;
  188. }
  189. }
  190. void cmLocalGenerator::SetupPathConversions()
  191. {
  192. // Setup the current output directory components for use by
  193. // Convert
  194. std::string outdir;
  195. outdir =
  196. cmSystemTools::CollapseFullPath(this->Makefile->GetHomeDirectory());
  197. cmSystemTools::SplitPath(outdir.c_str(), this->HomeDirectoryComponents);
  198. outdir =
  199. cmSystemTools::CollapseFullPath(this->Makefile->GetStartDirectory());
  200. cmSystemTools::SplitPath(outdir.c_str(), this->StartDirectoryComponents);
  201. outdir = cmSystemTools::CollapseFullPath
  202. (this->Makefile->GetHomeOutputDirectory());
  203. cmSystemTools::SplitPath(outdir.c_str(),
  204. this->HomeOutputDirectoryComponents);
  205. outdir = cmSystemTools::CollapseFullPath
  206. (this->Makefile->GetStartOutputDirectory());
  207. cmSystemTools::SplitPath(outdir.c_str(),
  208. this->StartOutputDirectoryComponents);
  209. }
  210. void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator *gg)
  211. {
  212. this->GlobalGenerator = gg;
  213. this->Makefile = new cmMakefile;
  214. this->Makefile->SetLocalGenerator(this);
  215. // setup the home directories
  216. this->Makefile->GetProperties().SetCMakeInstance(gg->GetCMakeInstance());
  217. this->Makefile->SetHomeDirectory(
  218. gg->GetCMakeInstance()->GetHomeDirectory());
  219. this->Makefile->SetHomeOutputDirectory(
  220. gg->GetCMakeInstance()->GetHomeOutputDirectory());
  221. }
  222. void cmLocalGenerator::ConfigureFinalPass()
  223. {
  224. this->Makefile->ConfigureFinalPass();
  225. }
  226. void cmLocalGenerator::TraceDependencies()
  227. {
  228. // Generate the rule files for each target.
  229. cmTargets& targets = this->Makefile->GetTargets();
  230. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  231. {
  232. t->second.TraceDependencies();
  233. }
  234. }
  235. void cmLocalGenerator::GenerateTestFiles()
  236. {
  237. if ( !this->Makefile->IsOn("CMAKE_TESTING_ENABLED") )
  238. {
  239. return;
  240. }
  241. // Compute the set of configurations.
  242. std::vector<std::string> configurationTypes;
  243. const char* config =
  244. this->Makefile->GetConfigurations(configurationTypes, false);
  245. std::string file = this->Makefile->GetStartOutputDirectory();
  246. file += "/";
  247. file += "CTestTestfile.cmake";
  248. cmGeneratedFileStream fout(file.c_str());
  249. fout.SetCopyIfDifferent(true);
  250. fout << "# CMake generated Testfile for " << std::endl
  251. << "# Source directory: "
  252. << this->Makefile->GetStartDirectory() << std::endl
  253. << "# Build directory: "
  254. << this->Makefile->GetStartOutputDirectory() << std::endl
  255. << "# " << std::endl
  256. << "# This file includes the relevant testing commands "
  257. << "required for " << std::endl
  258. << "# testing this directory and lists subdirectories to "
  259. << "be tested as well." << std::endl;
  260. const char* testIncludeFile =
  261. this->Makefile->GetProperty("TEST_INCLUDE_FILE");
  262. if ( testIncludeFile )
  263. {
  264. fout << "include(\"" << testIncludeFile << "\")" << std::endl;
  265. }
  266. // Ask each test generator to write its code.
  267. std::vector<cmTestGenerator*> const&
  268. testers = this->Makefile->GetTestGenerators();
  269. for(std::vector<cmTestGenerator*>::const_iterator gi = testers.begin();
  270. gi != testers.end(); ++gi)
  271. {
  272. (*gi)->Generate(fout, config, configurationTypes);
  273. }
  274. if ( this->Children.size())
  275. {
  276. size_t i;
  277. for(i = 0; i < this->Children.size(); ++i)
  278. {
  279. // TODO: Use add_subdirectory instead?
  280. fout << "subdirs(";
  281. std::string outP =
  282. this->Children[i]->GetMakefile()->GetStartOutputDirectory();
  283. fout << this->Convert(outP.c_str(),START_OUTPUT);
  284. fout << ")" << std::endl;
  285. }
  286. }
  287. }
  288. //----------------------------------------------------------------------------
  289. void cmLocalGenerator::GenerateInstallRules()
  290. {
  291. // Compute the install prefix.
  292. const char* prefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  293. #if defined(_WIN32) && !defined(__CYGWIN__)
  294. std::string prefix_win32;
  295. if(!prefix)
  296. {
  297. if(!cmSystemTools::GetEnv("SystemDrive", prefix_win32))
  298. {
  299. prefix_win32 = "C:";
  300. }
  301. const char* project_name = this->Makefile->GetDefinition("PROJECT_NAME");
  302. if(project_name && project_name[0])
  303. {
  304. prefix_win32 += "/Program Files/";
  305. prefix_win32 += project_name;
  306. }
  307. else
  308. {
  309. prefix_win32 += "/InstalledCMakeProject";
  310. }
  311. prefix = prefix_win32.c_str();
  312. }
  313. #elif defined(__HAIKU__)
  314. char dir[B_PATH_NAME_LENGTH];
  315. if (!prefix)
  316. {
  317. if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir))
  318. == B_OK)
  319. {
  320. prefix = dir;
  321. }
  322. else
  323. {
  324. prefix = "/boot/system";
  325. }
  326. }
  327. #else
  328. if (!prefix)
  329. {
  330. prefix = "/usr/local";
  331. }
  332. #endif
  333. // Compute the set of configurations.
  334. std::vector<std::string> configurationTypes;
  335. const char* config =
  336. this->Makefile->GetConfigurations(configurationTypes, false);
  337. // Choose a default install configuration.
  338. const char* default_config = config;
  339. const char* default_order[] = {"RELEASE", "MINSIZEREL",
  340. "RELWITHDEBINFO", "DEBUG", 0};
  341. for(const char** c = default_order; *c && !default_config; ++c)
  342. {
  343. for(std::vector<std::string>::iterator i = configurationTypes.begin();
  344. i != configurationTypes.end(); ++i)
  345. {
  346. if(cmSystemTools::UpperCase(*i) == *c)
  347. {
  348. default_config = i->c_str();
  349. }
  350. }
  351. }
  352. if(!default_config && !configurationTypes.empty())
  353. {
  354. default_config = configurationTypes[0].c_str();
  355. }
  356. if(!default_config)
  357. {
  358. default_config = "Release";
  359. }
  360. // Create the install script file.
  361. std::string file = this->Makefile->GetStartOutputDirectory();
  362. std::string homedir = this->Makefile->GetHomeOutputDirectory();
  363. std::string currdir = this->Makefile->GetCurrentOutputDirectory();
  364. cmSystemTools::ConvertToUnixSlashes(file);
  365. cmSystemTools::ConvertToUnixSlashes(homedir);
  366. cmSystemTools::ConvertToUnixSlashes(currdir);
  367. int toplevel_install = 0;
  368. if ( currdir == homedir )
  369. {
  370. toplevel_install = 1;
  371. }
  372. file += "/cmake_install.cmake";
  373. cmGeneratedFileStream fout(file.c_str());
  374. fout.SetCopyIfDifferent(true);
  375. // Write the header.
  376. fout << "# Install script for directory: "
  377. << this->Makefile->GetCurrentDirectory() << std::endl << std::endl;
  378. fout << "# Set the install prefix" << std::endl
  379. << "if(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
  380. << " set(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl
  381. << "endif()" << std::endl
  382. << "string(REGEX REPLACE \"/$\" \"\" CMAKE_INSTALL_PREFIX "
  383. << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
  384. << std::endl;
  385. // Write support code for generating per-configuration install rules.
  386. fout <<
  387. "# Set the install configuration name.\n"
  388. "if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
  389. " if(BUILD_TYPE)\n"
  390. " string(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
  391. " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
  392. " else()\n"
  393. " set(CMAKE_INSTALL_CONFIG_NAME \"" << default_config << "\")\n"
  394. " endif()\n"
  395. " message(STATUS \"Install configuration: "
  396. "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
  397. "endif()\n"
  398. "\n";
  399. // Write support code for dealing with component-specific installs.
  400. fout <<
  401. "# Set the component getting installed.\n"
  402. "if(NOT CMAKE_INSTALL_COMPONENT)\n"
  403. " if(COMPONENT)\n"
  404. " message(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
  405. " set(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
  406. " else()\n"
  407. " set(CMAKE_INSTALL_COMPONENT)\n"
  408. " endif()\n"
  409. "endif()\n"
  410. "\n";
  411. // Copy user-specified install options to the install code.
  412. if(const char* so_no_exe =
  413. this->Makefile->GetDefinition("CMAKE_INSTALL_SO_NO_EXE"))
  414. {
  415. fout <<
  416. "# Install shared libraries without execute permission?\n"
  417. "if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
  418. " set(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe << "\")\n"
  419. "endif()\n"
  420. "\n";
  421. }
  422. // Ask each install generator to write its code.
  423. std::vector<cmInstallGenerator*> const& installers =
  424. this->Makefile->GetInstallGenerators();
  425. for(std::vector<cmInstallGenerator*>::const_iterator
  426. gi = installers.begin();
  427. gi != installers.end(); ++gi)
  428. {
  429. (*gi)->Generate(fout, config, configurationTypes);
  430. }
  431. // Write rules from old-style specification stored in targets.
  432. this->GenerateTargetInstallRules(fout, config, configurationTypes);
  433. // Include install scripts from subdirectories.
  434. if(!this->Children.empty())
  435. {
  436. fout << "if(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
  437. fout << " # Include the install script for each subdirectory.\n";
  438. for(std::vector<cmLocalGenerator*>::const_iterator
  439. ci = this->Children.begin(); ci != this->Children.end(); ++ci)
  440. {
  441. if(!(*ci)->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  442. {
  443. std::string odir = (*ci)->GetMakefile()->GetStartOutputDirectory();
  444. cmSystemTools::ConvertToUnixSlashes(odir);
  445. fout << " include(\"" << odir.c_str()
  446. << "/cmake_install.cmake\")" << std::endl;
  447. }
  448. }
  449. fout << "\n";
  450. fout << "endif()\n\n";
  451. }
  452. // Record the install manifest.
  453. if ( toplevel_install )
  454. {
  455. fout <<
  456. "if(CMAKE_INSTALL_COMPONENT)\n"
  457. " set(CMAKE_INSTALL_MANIFEST \"install_manifest_"
  458. "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
  459. "else()\n"
  460. " set(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
  461. "endif()\n\n";
  462. fout
  463. << "file(WRITE \""
  464. << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
  465. << "\"\")" << std::endl;
  466. fout
  467. << "foreach(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
  468. << " file(APPEND \""
  469. << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
  470. << "\"${file}\\n\")" << std::endl
  471. << "endforeach()" << std::endl;
  472. }
  473. }
  474. //----------------------------------------------------------------------------
  475. void cmLocalGenerator::GenerateTargetManifest()
  476. {
  477. // Collect the set of configuration types.
  478. std::vector<std::string> configNames;
  479. this->Makefile->GetConfigurations(configNames);
  480. // Add our targets to the manifest for each configuration.
  481. cmTargets& targets = this->Makefile->GetTargets();
  482. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  483. {
  484. cmTarget& target = t->second;
  485. if(configNames.empty())
  486. {
  487. target.GenerateTargetManifest(0);
  488. }
  489. else
  490. {
  491. for(std::vector<std::string>::iterator ci = configNames.begin();
  492. ci != configNames.end(); ++ci)
  493. {
  494. const char* config = ci->c_str();
  495. target.GenerateTargetManifest(config);
  496. }
  497. }
  498. }
  499. }
  500. void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname,
  501. const char* lang,
  502. cmSourceFile& source,
  503. cmGeneratorTarget& target)
  504. {
  505. std::string objectDir = cmSystemTools::GetFilenamePath(std::string(ofname));
  506. objectDir = this->Convert(objectDir.c_str(),START_OUTPUT,SHELL);
  507. std::string objectFile = this->Convert(ofname,START_OUTPUT,SHELL);
  508. std::string sourceFile =
  509. this->Convert(source.GetFullPath().c_str(),START_OUTPUT,SHELL,true);
  510. std::string varString = "CMAKE_";
  511. varString += lang;
  512. varString += "_COMPILE_OBJECT";
  513. std::vector<std::string> rules;
  514. rules.push_back(this->Makefile->GetRequiredDefinition(varString.c_str()));
  515. varString = "CMAKE_";
  516. varString += lang;
  517. varString += "_FLAGS";
  518. std::string flags;
  519. flags += this->Makefile->GetSafeDefinition(varString.c_str());
  520. flags += " ";
  521. {
  522. std::vector<std::string> includes;
  523. this->GetIncludeDirectories(includes, &target, lang);
  524. flags += this->GetIncludeFlags(includes, &target, lang);
  525. }
  526. flags += this->Makefile->GetDefineFlags();
  527. // Construct the command lines.
  528. cmCustomCommandLines commandLines;
  529. std::vector<std::string> commands;
  530. cmSystemTools::ExpandList(rules, commands);
  531. cmLocalGenerator::RuleVariables vars;
  532. vars.Language = lang;
  533. vars.Source = sourceFile.c_str();
  534. vars.Object = objectFile.c_str();
  535. vars.ObjectDir = objectDir.c_str();
  536. vars.Flags = flags.c_str();
  537. for(std::vector<std::string>::iterator i = commands.begin();
  538. i != commands.end(); ++i)
  539. {
  540. // Expand the full command line string.
  541. this->ExpandRuleVariables(*i, vars);
  542. // Parse the string to get the custom command line.
  543. cmCustomCommandLine commandLine;
  544. std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
  545. for(std::vector<cmStdString>::iterator a = cmd.begin();
  546. a != cmd.end(); ++a)
  547. {
  548. commandLine.push_back(*a);
  549. }
  550. // Store this command line.
  551. commandLines.push_back(commandLine);
  552. }
  553. // Check for extra object-file dependencies.
  554. std::vector<std::string> depends;
  555. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  556. if(additionalDeps)
  557. {
  558. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  559. }
  560. // Generate a meaningful comment for the command.
  561. std::string comment = "Building ";
  562. comment += lang;
  563. comment += " object ";
  564. comment += this->Convert(ofname, START_OUTPUT);
  565. // Add the custom command to build the object file.
  566. this->Makefile->AddCustomCommandToOutput(
  567. ofname,
  568. depends,
  569. source.GetFullPath().c_str(),
  570. commandLines,
  571. comment.c_str(),
  572. this->Makefile->GetStartOutputDirectory()
  573. );
  574. }
  575. void cmLocalGenerator::AddBuildTargetRule(const char* llang,
  576. cmGeneratorTarget& target)
  577. {
  578. cmStdString objs;
  579. std::vector<std::string> objVector;
  580. // Add all the sources outputs to the depends of the target
  581. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  582. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  583. i != classes.end(); ++i)
  584. {
  585. cmSourceFile* sf = *i;
  586. if(!sf->GetCustomCommand() &&
  587. !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  588. !sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
  589. {
  590. std::string dir_max;
  591. dir_max += this->Makefile->GetCurrentOutputDirectory();
  592. dir_max += "/";
  593. std::string obj = this->GetObjectFileNameWithoutTarget(*sf, dir_max);
  594. if(!obj.empty())
  595. {
  596. std::string ofname = this->Makefile->GetCurrentOutputDirectory();
  597. ofname += "/";
  598. ofname += obj;
  599. objVector.push_back(ofname);
  600. this->AddCustomCommandToCreateObject(ofname.c_str(),
  601. llang, *(*i), target);
  602. objs += this->Convert(ofname.c_str(),START_OUTPUT,MAKEFILE);
  603. objs += " ";
  604. }
  605. }
  606. }
  607. std::string createRule = "CMAKE_";
  608. createRule += llang;
  609. createRule += target.GetCreateRuleVariable();
  610. std::string targetName = target.Target->GetFullName();
  611. // Executable :
  612. // Shared Library:
  613. // Static Library:
  614. // Shared Module:
  615. std::string linkLibs; // should be set
  616. std::string frameworkPath;
  617. std::string linkPath;
  618. std::string flags; // should be set
  619. std::string linkFlags; // should be set
  620. this->GetTargetFlags(linkLibs, frameworkPath, linkPath, flags, linkFlags,
  621. &target);
  622. linkLibs = frameworkPath + linkPath + linkLibs;
  623. cmLocalGenerator::RuleVariables vars;
  624. vars.Language = llang;
  625. vars.Objects = objs.c_str();
  626. vars.ObjectDir = ".";
  627. vars.Target = targetName.c_str();
  628. vars.LinkLibraries = linkLibs.c_str();
  629. vars.Flags = flags.c_str();
  630. vars.LinkFlags = linkFlags.c_str();
  631. std::string langFlags;
  632. this->AddLanguageFlags(langFlags, llang, 0);
  633. this->AddArchitectureFlags(langFlags, &target, llang, 0);
  634. vars.LanguageCompileFlags = langFlags.c_str();
  635. cmCustomCommandLines commandLines;
  636. std::vector<std::string> rules;
  637. rules.push_back(this->Makefile->GetRequiredDefinition(createRule.c_str()));
  638. std::vector<std::string> commands;
  639. cmSystemTools::ExpandList(rules, commands);
  640. for(std::vector<std::string>::iterator i = commands.begin();
  641. i != commands.end(); ++i)
  642. {
  643. // Expand the full command line string.
  644. this->ExpandRuleVariables(*i, vars);
  645. // Parse the string to get the custom command line.
  646. cmCustomCommandLine commandLine;
  647. std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
  648. for(std::vector<cmStdString>::iterator a = cmd.begin();
  649. a != cmd.end(); ++a)
  650. {
  651. commandLine.push_back(*a);
  652. }
  653. // Store this command line.
  654. commandLines.push_back(commandLine);
  655. }
  656. std::string targetFullPath = target.Target->GetFullPath();
  657. // Generate a meaningful comment for the command.
  658. std::string comment = "Linking ";
  659. comment += llang;
  660. comment += " target ";
  661. comment += this->Convert(targetFullPath.c_str(), START_OUTPUT);
  662. this->Makefile->AddCustomCommandToOutput(
  663. targetFullPath.c_str(),
  664. objVector,
  665. 0,
  666. commandLines,
  667. comment.c_str(),
  668. this->Makefile->GetStartOutputDirectory()
  669. );
  670. target.Target->AddSourceFile
  671. (this->Makefile->GetSource(targetFullPath.c_str()));
  672. }
  673. void cmLocalGenerator
  674. ::CreateCustomTargetsAndCommands(std::set<cmStdString> const& lang)
  675. {
  676. cmGeneratorTargetsType tgts = this->Makefile->GetGeneratorTargets();
  677. for(cmGeneratorTargetsType::iterator l = tgts.begin();
  678. l != tgts.end(); l++)
  679. {
  680. if (l->first->IsImported())
  681. {
  682. continue;
  683. }
  684. cmGeneratorTarget& target = *l->second;
  685. switch(target.GetType())
  686. {
  687. case cmTarget::STATIC_LIBRARY:
  688. case cmTarget::SHARED_LIBRARY:
  689. case cmTarget::MODULE_LIBRARY:
  690. case cmTarget::EXECUTABLE:
  691. {
  692. const char* llang = target.Target->GetLinkerLanguage();
  693. if(!llang)
  694. {
  695. cmSystemTools::Error
  696. ("CMake can not determine linker language for target: ",
  697. target.Target->GetName());
  698. return;
  699. }
  700. // if the language is not in the set lang then create custom
  701. // commands to build the target
  702. if(lang.count(llang) == 0)
  703. {
  704. this->AddBuildTargetRule(llang, target);
  705. }
  706. }
  707. break;
  708. default:
  709. break;
  710. }
  711. }
  712. }
  713. // List of variables that are replaced when
  714. // rules are expanced. These variables are
  715. // replaced in the form <var> with GetSafeDefinition(var).
  716. // ${LANG} is replaced in the variable first with all enabled
  717. // languages.
  718. static const char* ruleReplaceVars[] =
  719. {
  720. "CMAKE_${LANG}_COMPILER",
  721. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  722. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  723. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  724. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  725. "CMAKE_${LANG}_LINK_FLAGS",
  726. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  727. "CMAKE_${LANG}_ARCHIVE",
  728. "CMAKE_AR",
  729. "CMAKE_CURRENT_SOURCE_DIR",
  730. "CMAKE_CURRENT_BINARY_DIR",
  731. "CMAKE_RANLIB",
  732. "CMAKE_LINKER",
  733. 0
  734. };
  735. std::string
  736. cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
  737. const RuleVariables& replaceValues)
  738. {
  739. if(replaceValues.LinkFlags)
  740. {
  741. if(variable == "LINK_FLAGS")
  742. {
  743. return replaceValues.LinkFlags;
  744. }
  745. }
  746. if(replaceValues.Flags)
  747. {
  748. if(variable == "FLAGS")
  749. {
  750. return replaceValues.Flags;
  751. }
  752. }
  753. if(replaceValues.Source)
  754. {
  755. if(variable == "SOURCE")
  756. {
  757. return replaceValues.Source;
  758. }
  759. }
  760. if(replaceValues.PreprocessedSource)
  761. {
  762. if(variable == "PREPROCESSED_SOURCE")
  763. {
  764. return replaceValues.PreprocessedSource;
  765. }
  766. }
  767. if(replaceValues.AssemblySource)
  768. {
  769. if(variable == "ASSEMBLY_SOURCE")
  770. {
  771. return replaceValues.AssemblySource;
  772. }
  773. }
  774. if(replaceValues.Object)
  775. {
  776. if(variable == "OBJECT")
  777. {
  778. return replaceValues.Object;
  779. }
  780. }
  781. if(replaceValues.ObjectDir)
  782. {
  783. if(variable == "OBJECT_DIR")
  784. {
  785. return replaceValues.ObjectDir;
  786. }
  787. }
  788. if(replaceValues.Objects)
  789. {
  790. if(variable == "OBJECTS")
  791. {
  792. return replaceValues.Objects;
  793. }
  794. }
  795. if(replaceValues.ObjectsQuoted)
  796. {
  797. if(variable == "OBJECTS_QUOTED")
  798. {
  799. return replaceValues.ObjectsQuoted;
  800. }
  801. }
  802. if(replaceValues.Defines && variable == "DEFINES")
  803. {
  804. return replaceValues.Defines;
  805. }
  806. if(replaceValues.TargetPDB )
  807. {
  808. if(variable == "TARGET_PDB")
  809. {
  810. return replaceValues.TargetPDB;
  811. }
  812. }
  813. if(replaceValues.DependencyFile )
  814. {
  815. if(variable == "DEP_FILE")
  816. {
  817. return replaceValues.DependencyFile;
  818. }
  819. }
  820. if(replaceValues.Target)
  821. {
  822. if(variable == "TARGET_QUOTED")
  823. {
  824. std::string targetQuoted = replaceValues.Target;
  825. if(targetQuoted.size() && targetQuoted[0] != '\"')
  826. {
  827. targetQuoted = '\"';
  828. targetQuoted += replaceValues.Target;
  829. targetQuoted += '\"';
  830. }
  831. return targetQuoted;
  832. }
  833. if(variable == "TARGET_UNQUOTED")
  834. {
  835. std::string unquoted = replaceValues.Target;
  836. std::string::size_type sz = unquoted.size();
  837. if(sz > 2 && unquoted[0] == '\"' && unquoted[sz-1] == '\"')
  838. {
  839. unquoted = unquoted.substr(1, sz-2);
  840. }
  841. return unquoted;
  842. }
  843. if(replaceValues.LanguageCompileFlags)
  844. {
  845. if(variable == "LANGUAGE_COMPILE_FLAGS")
  846. {
  847. return replaceValues.LanguageCompileFlags;
  848. }
  849. }
  850. if(replaceValues.Target)
  851. {
  852. if(variable == "TARGET")
  853. {
  854. return replaceValues.Target;
  855. }
  856. }
  857. if(variable == "TARGET_IMPLIB")
  858. {
  859. return this->TargetImplib;
  860. }
  861. if(variable == "TARGET_VERSION_MAJOR")
  862. {
  863. if(replaceValues.TargetVersionMajor)
  864. {
  865. return replaceValues.TargetVersionMajor;
  866. }
  867. else
  868. {
  869. return "0";
  870. }
  871. }
  872. if(variable == "TARGET_VERSION_MINOR")
  873. {
  874. if(replaceValues.TargetVersionMinor)
  875. {
  876. return replaceValues.TargetVersionMinor;
  877. }
  878. else
  879. {
  880. return "0";
  881. }
  882. }
  883. if(replaceValues.Target)
  884. {
  885. if(variable == "TARGET_BASE")
  886. {
  887. // Strip the last extension off the target name.
  888. std::string targetBase = replaceValues.Target;
  889. std::string::size_type pos = targetBase.rfind(".");
  890. if(pos != targetBase.npos)
  891. {
  892. return targetBase.substr(0, pos);
  893. }
  894. else
  895. {
  896. return targetBase;
  897. }
  898. }
  899. }
  900. }
  901. if(variable == "TARGET_SONAME" || variable == "SONAME_FLAG" ||
  902. variable == "TARGET_INSTALLNAME_DIR")
  903. {
  904. // All these variables depend on TargetSOName
  905. if(replaceValues.TargetSOName)
  906. {
  907. if(variable == "TARGET_SONAME")
  908. {
  909. return replaceValues.TargetSOName;
  910. }
  911. if(variable == "SONAME_FLAG" && replaceValues.SONameFlag)
  912. {
  913. return replaceValues.SONameFlag;
  914. }
  915. if(replaceValues.TargetInstallNameDir &&
  916. variable == "TARGET_INSTALLNAME_DIR")
  917. {
  918. return replaceValues.TargetInstallNameDir;
  919. }
  920. }
  921. return "";
  922. }
  923. if(replaceValues.LinkLibraries)
  924. {
  925. if(variable == "LINK_LIBRARIES")
  926. {
  927. return replaceValues.LinkLibraries;
  928. }
  929. }
  930. if(replaceValues.Language)
  931. {
  932. if(variable == "LANGUAGE")
  933. {
  934. return replaceValues.Language;
  935. }
  936. }
  937. if(replaceValues.CMTarget)
  938. {
  939. if(variable == "TARGET_NAME")
  940. {
  941. return replaceValues.CMTarget->GetName();
  942. }
  943. if(variable == "TARGET_TYPE")
  944. {
  945. return cmTarget::GetTargetTypeName(replaceValues.CMTarget->GetType());
  946. }
  947. }
  948. if(replaceValues.Output)
  949. {
  950. if(variable == "OUTPUT")
  951. {
  952. return replaceValues.Output;
  953. }
  954. }
  955. if(variable == "CMAKE_COMMAND")
  956. {
  957. const char* cmcommand =
  958. this->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  959. return this->Convert(cmcommand, FULL, SHELL);
  960. }
  961. std::vector<std::string> enabledLanguages;
  962. this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  963. // loop over language specific replace variables
  964. int pos = 0;
  965. while(ruleReplaceVars[pos])
  966. {
  967. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  968. i != enabledLanguages.end(); ++i)
  969. {
  970. const char* lang = i->c_str();
  971. std::string actualReplace = ruleReplaceVars[pos];
  972. // If this is the compiler then look for the extra variable
  973. // _COMPILER_ARG1 which must be the first argument to the compiler
  974. const char* compilerArg1 = 0;
  975. if(actualReplace == "CMAKE_${LANG}_COMPILER")
  976. {
  977. std::string arg1 = actualReplace + "_ARG1";
  978. cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
  979. compilerArg1 = this->Makefile->GetDefinition(arg1.c_str());
  980. }
  981. if(actualReplace.find("${LANG}") != actualReplace.npos)
  982. {
  983. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  984. }
  985. if(actualReplace == variable)
  986. {
  987. std::string replace =
  988. this->Makefile->GetSafeDefinition(variable.c_str());
  989. // if the variable is not a FLAG then treat it like a path
  990. if(variable.find("_FLAG") == variable.npos)
  991. {
  992. std::string ret = this->ConvertToOutputForExisting(replace.c_str());
  993. // if there is a required first argument to the compiler add it
  994. // to the compiler string
  995. if(compilerArg1)
  996. {
  997. ret += " ";
  998. ret += compilerArg1;
  999. }
  1000. return ret;
  1001. }
  1002. return replace;
  1003. }
  1004. }
  1005. pos++;
  1006. }
  1007. return variable;
  1008. }
  1009. void
  1010. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  1011. const RuleVariables& replaceValues)
  1012. {
  1013. this->InsertRuleLauncher(s, replaceValues.CMTarget,
  1014. replaceValues.RuleLauncher);
  1015. std::string::size_type start = s.find('<');
  1016. // no variables to expand
  1017. if(start == s.npos)
  1018. {
  1019. return;
  1020. }
  1021. std::string::size_type pos = 0;
  1022. std::string expandedInput;
  1023. while(start != s.npos && start < s.size()-2)
  1024. {
  1025. std::string::size_type end = s.find('>', start);
  1026. // if we find a < with no > we are done
  1027. if(end == s.npos)
  1028. {
  1029. return;
  1030. }
  1031. char c = s[start+1];
  1032. // if the next char after the < is not A-Za-z then
  1033. // skip it and try to find the next < in the string
  1034. if(!isalpha(c))
  1035. {
  1036. start = s.find('<', start+1);
  1037. }
  1038. else
  1039. {
  1040. // extract the var
  1041. std::string var = s.substr(start+1, end - start-1);
  1042. std::string replace = this->ExpandRuleVariable(var,
  1043. replaceValues);
  1044. expandedInput += s.substr(pos, start-pos);
  1045. expandedInput += replace;
  1046. // move to next one
  1047. start = s.find('<', start+var.size()+2);
  1048. pos = end+1;
  1049. }
  1050. }
  1051. // add the rest of the input
  1052. expandedInput += s.substr(pos, s.size()-pos);
  1053. s = expandedInput;
  1054. }
  1055. //----------------------------------------------------------------------------
  1056. const char* cmLocalGenerator::GetRuleLauncher(cmTarget* target,
  1057. const char* prop)
  1058. {
  1059. if(target)
  1060. {
  1061. return target->GetProperty(prop);
  1062. }
  1063. else
  1064. {
  1065. return this->Makefile->GetProperty(prop);
  1066. }
  1067. }
  1068. //----------------------------------------------------------------------------
  1069. void cmLocalGenerator::InsertRuleLauncher(std::string& s, cmTarget* target,
  1070. const char* prop)
  1071. {
  1072. if(const char* val = this->GetRuleLauncher(target, prop))
  1073. {
  1074. cmOStringStream wrapped;
  1075. wrapped << val << " " << s;
  1076. s = wrapped.str();
  1077. }
  1078. }
  1079. //----------------------------------------------------------------------------
  1080. std::string
  1081. cmLocalGenerator::ConvertToOutputForExistingCommon(const char* remote,
  1082. std::string const& result)
  1083. {
  1084. // If this is a windows shell, the result has a space, and the path
  1085. // already exists, we can use a short-path to reference it without a
  1086. // space.
  1087. if(this->WindowsShell && result.find(' ') != result.npos &&
  1088. cmSystemTools::FileExists(remote))
  1089. {
  1090. std::string tmp;
  1091. if(cmSystemTools::GetShortPath(remote, tmp))
  1092. {
  1093. return this->Convert(tmp.c_str(), NONE, SHELL, true);
  1094. }
  1095. }
  1096. // Otherwise, leave it unchanged.
  1097. return result;
  1098. }
  1099. //----------------------------------------------------------------------------
  1100. std::string
  1101. cmLocalGenerator::ConvertToOutputForExisting(const char* remote,
  1102. RelativeRoot local)
  1103. {
  1104. // Perform standard conversion.
  1105. std::string result = this->Convert(remote, local, SHELL, true);
  1106. // Consider short-path.
  1107. return this->ConvertToOutputForExistingCommon(remote, result);
  1108. }
  1109. //----------------------------------------------------------------------------
  1110. std::string
  1111. cmLocalGenerator::ConvertToOutputForExisting(RelativeRoot remote,
  1112. const char* local)
  1113. {
  1114. // Perform standard conversion.
  1115. std::string result = this->Convert(remote, local, SHELL, true);
  1116. // Consider short-path.
  1117. const char* remotePath = this->GetRelativeRootPath(remote);
  1118. return this->ConvertToOutputForExistingCommon(remotePath, result);
  1119. }
  1120. //----------------------------------------------------------------------------
  1121. std::string
  1122. cmLocalGenerator::ConvertToIncludeReference(std::string const& path)
  1123. {
  1124. return this->ConvertToOutputForExisting(path.c_str());
  1125. }
  1126. //----------------------------------------------------------------------------
  1127. std::string cmLocalGenerator::GetIncludeFlags(
  1128. const std::vector<std::string> &includes,
  1129. cmGeneratorTarget* target,
  1130. const char* lang, bool forResponseFile,
  1131. const char *config)
  1132. {
  1133. if(!lang)
  1134. {
  1135. return "";
  1136. }
  1137. cmOStringStream includeFlags;
  1138. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  1139. flagVar += lang;
  1140. const char* includeFlag =
  1141. this->Makefile->GetSafeDefinition(flagVar.c_str());
  1142. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  1143. flagVar += lang;
  1144. const char* sep = this->Makefile->GetDefinition(flagVar.c_str());
  1145. bool quotePaths = false;
  1146. if(this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
  1147. {
  1148. quotePaths = true;
  1149. }
  1150. bool repeatFlag = true;
  1151. // should the include flag be repeated like ie. -IA -IB
  1152. if(!sep)
  1153. {
  1154. sep = " ";
  1155. }
  1156. else
  1157. {
  1158. // if there is a separator then the flag is not repeated but is only
  1159. // given once i.e. -classpath a:b:c
  1160. repeatFlag = false;
  1161. }
  1162. // Support special system include flag if it is available and the
  1163. // normal flag is repeated for each directory.
  1164. std::string sysFlagVar = "CMAKE_INCLUDE_SYSTEM_FLAG_";
  1165. sysFlagVar += lang;
  1166. const char* sysIncludeFlag = 0;
  1167. if(repeatFlag)
  1168. {
  1169. sysIncludeFlag = this->Makefile->GetDefinition(sysFlagVar.c_str());
  1170. }
  1171. bool flagUsed = false;
  1172. std::set<cmStdString> emitted;
  1173. #ifdef __APPLE__
  1174. emitted.insert("/System/Library/Frameworks");
  1175. #endif
  1176. std::vector<std::string>::const_iterator i;
  1177. for(i = includes.begin(); i != includes.end(); ++i)
  1178. {
  1179. if(this->Makefile->IsOn("APPLE")
  1180. && cmSystemTools::IsPathToFramework(i->c_str()))
  1181. {
  1182. std::string frameworkDir = *i;
  1183. frameworkDir += "/../";
  1184. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1185. if(emitted.insert(frameworkDir).second)
  1186. {
  1187. OutputFormat format = forResponseFile? RESPONSE : SHELL;
  1188. includeFlags
  1189. << "-F" << this->Convert(frameworkDir.c_str(),
  1190. START_OUTPUT, format, true)
  1191. << " ";
  1192. }
  1193. continue;
  1194. }
  1195. if(!flagUsed || repeatFlag)
  1196. {
  1197. if(sysIncludeFlag && target &&
  1198. target->IsSystemIncludeDirectory(i->c_str(), config))
  1199. {
  1200. includeFlags << sysIncludeFlag;
  1201. }
  1202. else
  1203. {
  1204. includeFlags << includeFlag;
  1205. }
  1206. flagUsed = true;
  1207. }
  1208. std::string includePath;
  1209. if(forResponseFile)
  1210. {
  1211. includePath = this->Convert(i->c_str(), START_OUTPUT,
  1212. RESPONSE, true);
  1213. }
  1214. else
  1215. {
  1216. includePath = this->ConvertToIncludeReference(*i);
  1217. }
  1218. if(quotePaths && includePath.size() && includePath[0] != '\"')
  1219. {
  1220. includeFlags << "\"";
  1221. }
  1222. includeFlags << includePath;
  1223. if(quotePaths && includePath.size() && includePath[0] != '\"')
  1224. {
  1225. includeFlags << "\"";
  1226. }
  1227. includeFlags << sep;
  1228. }
  1229. std::string flags = includeFlags.str();
  1230. // remove trailing separators
  1231. if((sep[0] != ' ') && flags.size()>0 && flags[flags.size()-1] == sep[0])
  1232. {
  1233. flags[flags.size()-1] = ' ';
  1234. }
  1235. return flags;
  1236. }
  1237. //----------------------------------------------------------------------------
  1238. void cmLocalGenerator::AddCompileDefinitions(std::set<std::string>& defines,
  1239. cmTarget* target,
  1240. const char* config)
  1241. {
  1242. std::vector<std::string> targetDefines;
  1243. target->GetCompileDefinitions(targetDefines,
  1244. config);
  1245. this->AppendDefines(defines, targetDefines);
  1246. }
  1247. //----------------------------------------------------------------------------
  1248. void cmLocalGenerator::AddCompileOptions(
  1249. std::string& flags, cmTarget* target,
  1250. const char* lang, const char* config
  1251. )
  1252. {
  1253. std::string langFlagRegexVar = std::string("CMAKE_")+lang+"_FLAG_REGEX";
  1254. if(const char* langFlagRegexStr =
  1255. this->Makefile->GetDefinition(langFlagRegexVar.c_str()))
  1256. {
  1257. // Filter flags acceptable to this language.
  1258. cmsys::RegularExpression r(langFlagRegexStr);
  1259. std::vector<std::string> opts;
  1260. if(const char* targetFlags = target->GetProperty("COMPILE_FLAGS"))
  1261. {
  1262. cmSystemTools::ParseWindowsCommandLine(targetFlags, opts);
  1263. }
  1264. target->GetCompileOptions(opts, config);
  1265. for(std::vector<std::string>::const_iterator i = opts.begin();
  1266. i != opts.end(); ++i)
  1267. {
  1268. if(r.find(i->c_str()))
  1269. {
  1270. // (Re-)Escape this flag. COMPILE_FLAGS were already parsed
  1271. // as a command line above, and COMPILE_OPTIONS are escaped.
  1272. this->AppendFlagEscape(flags, i->c_str());
  1273. }
  1274. }
  1275. }
  1276. else
  1277. {
  1278. // Use all flags.
  1279. if(const char* targetFlags = target->GetProperty("COMPILE_FLAGS"))
  1280. {
  1281. // COMPILE_FLAGS are not escaped for historical reasons.
  1282. this->AppendFlags(flags, targetFlags);
  1283. }
  1284. std::vector<std::string> opts;
  1285. target->GetCompileOptions(opts, config);
  1286. for(std::vector<std::string>::const_iterator i = opts.begin();
  1287. i != opts.end(); ++i)
  1288. {
  1289. // COMPILE_OPTIONS are escaped.
  1290. this->AppendFlagEscape(flags, i->c_str());
  1291. }
  1292. }
  1293. }
  1294. //----------------------------------------------------------------------------
  1295. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
  1296. cmGeneratorTarget* target,
  1297. const char* lang,
  1298. const char *config,
  1299. bool stripImplicitInclDirs
  1300. )
  1301. {
  1302. // Need to decide whether to automatically include the source and
  1303. // binary directories at the beginning of the include path.
  1304. bool includeSourceDir = false;
  1305. bool includeBinaryDir = false;
  1306. // When automatic include directories are requested for a build then
  1307. // include the source and binary directories at the beginning of the
  1308. // include path to approximate include file behavior for an
  1309. // in-source build. This does not account for the case of a source
  1310. // file in a subdirectory of the current source directory but we
  1311. // cannot fix this because not all native build tools support
  1312. // per-source-file include paths.
  1313. if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
  1314. {
  1315. includeSourceDir = true;
  1316. includeBinaryDir = true;
  1317. }
  1318. // CMake versions below 2.0 would add the source tree to the -I path
  1319. // automatically. Preserve compatibility.
  1320. if(this->NeedBackwardsCompatibility(1,9))
  1321. {
  1322. includeSourceDir = true;
  1323. }
  1324. // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
  1325. // not set the backwards compatibility level automatically.
  1326. const char* vtkSourceDir =
  1327. this->Makefile->GetDefinition("VTK_SOURCE_DIR");
  1328. if(vtkSourceDir)
  1329. {
  1330. const char* vtk_major =
  1331. this->Makefile->GetDefinition("VTK_MAJOR_VERSION");
  1332. const char* vtk_minor =
  1333. this->Makefile->GetDefinition("VTK_MINOR_VERSION");
  1334. vtk_major = vtk_major? vtk_major : "4";
  1335. vtk_minor = vtk_minor? vtk_minor : "4";
  1336. int vmajor = 0;
  1337. int vminor = 0;
  1338. if(sscanf(vtk_major, "%d", &vmajor) &&
  1339. sscanf(vtk_minor, "%d", &vminor) && vmajor == 4 && vminor <= 4)
  1340. {
  1341. includeSourceDir = true;
  1342. }
  1343. }
  1344. // Do not repeat an include path.
  1345. std::set<cmStdString> emitted;
  1346. // Store the automatic include paths.
  1347. if(includeBinaryDir)
  1348. {
  1349. if(emitted.find(
  1350. this->Makefile->GetStartOutputDirectory()) == emitted.end())
  1351. {
  1352. dirs.push_back(this->Makefile->GetStartOutputDirectory());
  1353. emitted.insert(this->Makefile->GetStartOutputDirectory());
  1354. }
  1355. }
  1356. if(includeSourceDir)
  1357. {
  1358. if(emitted.find(this->Makefile->GetStartDirectory()) == emitted.end())
  1359. {
  1360. dirs.push_back(this->Makefile->GetStartDirectory());
  1361. emitted.insert(this->Makefile->GetStartDirectory());
  1362. }
  1363. }
  1364. if(!target)
  1365. {
  1366. return;
  1367. }
  1368. std::vector<std::string> implicitDirs;
  1369. // Load implicit include directories for this language.
  1370. std::string impDirVar = "CMAKE_";
  1371. impDirVar += lang;
  1372. impDirVar += "_IMPLICIT_INCLUDE_DIRECTORIES";
  1373. if(const char* value = this->Makefile->GetDefinition(impDirVar.c_str()))
  1374. {
  1375. std::vector<std::string> impDirVec;
  1376. cmSystemTools::ExpandListArgument(value, impDirVec);
  1377. for(std::vector<std::string>::const_iterator i = impDirVec.begin();
  1378. i != impDirVec.end(); ++i)
  1379. {
  1380. emitted.insert(*i);
  1381. if (!stripImplicitInclDirs)
  1382. {
  1383. implicitDirs.push_back(*i);
  1384. }
  1385. }
  1386. }
  1387. // Get the target-specific include directories.
  1388. std::vector<std::string> includes;
  1389. includes = target->GetIncludeDirectories(config);
  1390. // Support putting all the in-project include directories first if
  1391. // it is requested by the project.
  1392. if(this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
  1393. {
  1394. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1395. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1396. for(std::vector<std::string>::const_iterator i = includes.begin();
  1397. i != includes.end(); ++i)
  1398. {
  1399. // Emit this directory only if it is a subdirectory of the
  1400. // top-level source or binary tree.
  1401. if(cmSystemTools::ComparePath(i->c_str(), topSourceDir) ||
  1402. cmSystemTools::ComparePath(i->c_str(), topBinaryDir) ||
  1403. cmSystemTools::IsSubDirectory(i->c_str(), topSourceDir) ||
  1404. cmSystemTools::IsSubDirectory(i->c_str(), topBinaryDir))
  1405. {
  1406. if(emitted.insert(*i).second)
  1407. {
  1408. dirs.push_back(*i);
  1409. }
  1410. }
  1411. }
  1412. }
  1413. // Construct the final ordered include directory list.
  1414. for(std::vector<std::string>::const_iterator i = includes.begin();
  1415. i != includes.end(); ++i)
  1416. {
  1417. if(emitted.insert(*i).second)
  1418. {
  1419. dirs.push_back(*i);
  1420. }
  1421. }
  1422. for(std::vector<std::string>::const_iterator i = implicitDirs.begin();
  1423. i != implicitDirs.end(); ++i)
  1424. {
  1425. if(std::find(includes.begin(), includes.end(), *i) != includes.end())
  1426. {
  1427. dirs.push_back(*i);
  1428. }
  1429. }
  1430. }
  1431. void cmLocalGenerator::GetStaticLibraryFlags(std::string& flags,
  1432. std::string const& config,
  1433. cmTarget* target)
  1434. {
  1435. this->AppendFlags(flags,
  1436. this->Makefile->GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS"));
  1437. if(!config.empty())
  1438. {
  1439. std::string name = "CMAKE_STATIC_LINKER_FLAGS_" + config;
  1440. this->AppendFlags(flags, this->Makefile->GetSafeDefinition(name.c_str()));
  1441. }
  1442. this->AppendFlags(flags, target->GetProperty("STATIC_LIBRARY_FLAGS"));
  1443. if(!config.empty())
  1444. {
  1445. std::string name = "STATIC_LIBRARY_FLAGS_" + config;
  1446. this->AppendFlags(flags, target->GetProperty(name.c_str()));
  1447. }
  1448. }
  1449. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  1450. std::string& flags,
  1451. std::string& linkFlags,
  1452. std::string& frameworkPath,
  1453. std::string& linkPath,
  1454. cmGeneratorTarget* target)
  1455. {
  1456. std::string buildType =
  1457. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1458. buildType = cmSystemTools::UpperCase(buildType);
  1459. const char* libraryLinkVariable =
  1460. "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1461. switch(target->GetType())
  1462. {
  1463. case cmTarget::STATIC_LIBRARY:
  1464. this->GetStaticLibraryFlags(linkFlags, buildType, target->Target);
  1465. break;
  1466. case cmTarget::MODULE_LIBRARY:
  1467. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1468. case cmTarget::SHARED_LIBRARY:
  1469. {
  1470. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  1471. linkFlags += " ";
  1472. if(!buildType.empty())
  1473. {
  1474. std::string build = libraryLinkVariable;
  1475. build += "_";
  1476. build += buildType;
  1477. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1478. linkFlags += " ";
  1479. }
  1480. if(this->Makefile->IsOn("WIN32") &&
  1481. !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
  1482. {
  1483. const std::vector<cmSourceFile*>& sources = target->GetSourceFiles();
  1484. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1485. i != sources.end(); ++i)
  1486. {
  1487. cmSourceFile* sf = *i;
  1488. if(sf->GetExtension() == "def")
  1489. {
  1490. linkFlags +=
  1491. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1492. linkFlags += this->Convert(sf->GetFullPath().c_str(),
  1493. FULL, SHELL);
  1494. linkFlags += " ";
  1495. }
  1496. }
  1497. }
  1498. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1499. if(targetLinkFlags)
  1500. {
  1501. linkFlags += targetLinkFlags;
  1502. linkFlags += " ";
  1503. }
  1504. if(!buildType.empty())
  1505. {
  1506. std::string configLinkFlags = "LINK_FLAGS_";
  1507. configLinkFlags += buildType;
  1508. targetLinkFlags = target->GetProperty(configLinkFlags.c_str());
  1509. if(targetLinkFlags)
  1510. {
  1511. linkFlags += targetLinkFlags;
  1512. linkFlags += " ";
  1513. }
  1514. }
  1515. this->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1516. *target, false);
  1517. }
  1518. break;
  1519. case cmTarget::EXECUTABLE:
  1520. {
  1521. linkFlags +=
  1522. this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1523. linkFlags += " ";
  1524. if(!buildType.empty())
  1525. {
  1526. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1527. build += buildType;
  1528. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1529. linkFlags += " ";
  1530. }
  1531. const char* linkLanguage = target->Target->GetLinkerLanguage();
  1532. if(!linkLanguage)
  1533. {
  1534. cmSystemTools::Error
  1535. ("CMake can not determine linker language for target: ",
  1536. target->Target->GetName());
  1537. return;
  1538. }
  1539. this->AddLanguageFlags(flags, linkLanguage, buildType.c_str());
  1540. this->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1541. *target, false);
  1542. if(cmSystemTools::IsOn
  1543. (this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1544. {
  1545. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_")
  1546. + linkLanguage + std::string("_FLAGS");
  1547. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar.c_str());
  1548. linkFlags += " ";
  1549. }
  1550. if ( target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  1551. {
  1552. linkFlags +=
  1553. this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1554. linkFlags += " ";
  1555. }
  1556. else
  1557. {
  1558. linkFlags +=
  1559. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1560. linkFlags += " ";
  1561. }
  1562. if (target->Target->IsExecutableWithExports())
  1563. {
  1564. std::string exportFlagVar = "CMAKE_EXE_EXPORTS_";
  1565. exportFlagVar += linkLanguage;
  1566. exportFlagVar += "_FLAG";
  1567. linkFlags +=
  1568. this->Makefile->GetSafeDefinition(exportFlagVar.c_str());
  1569. linkFlags += " ";
  1570. }
  1571. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1572. if(targetLinkFlags)
  1573. {
  1574. linkFlags += targetLinkFlags;
  1575. linkFlags += " ";
  1576. }
  1577. if(!buildType.empty())
  1578. {
  1579. std::string configLinkFlags = "LINK_FLAGS_";
  1580. configLinkFlags += buildType;
  1581. targetLinkFlags = target->GetProperty(configLinkFlags.c_str());
  1582. if(targetLinkFlags)
  1583. {
  1584. linkFlags += targetLinkFlags;
  1585. linkFlags += " ";
  1586. }
  1587. }
  1588. }
  1589. break;
  1590. default:
  1591. break;
  1592. }
  1593. }
  1594. std::string cmLocalGenerator::ConvertToLinkReference(std::string const& lib)
  1595. {
  1596. #if defined(_WIN32) && !defined(__CYGWIN__)
  1597. // Work-ardound command line parsing limitations in MSVC 6.0 and
  1598. // Watcom.
  1599. if(this->Makefile->IsOn("MSVC60") || this->Makefile->IsOn("WATCOM"))
  1600. {
  1601. // Search for the last space.
  1602. std::string::size_type pos = lib.rfind(' ');
  1603. if(pos != lib.npos)
  1604. {
  1605. // Find the slash after the last space, if any.
  1606. pos = lib.find('/', pos);
  1607. // Convert the portion of the path with a space to a short path.
  1608. std::string sp;
  1609. if(cmSystemTools::GetShortPath(lib.substr(0, pos).c_str(), sp))
  1610. {
  1611. // Append the rest of the path with no space.
  1612. sp += lib.substr(pos);
  1613. // Convert to an output path.
  1614. return this->Convert(sp.c_str(), NONE, SHELL);
  1615. }
  1616. }
  1617. }
  1618. #endif
  1619. // Normal behavior.
  1620. return this->Convert(lib.c_str(), START_OUTPUT, SHELL);
  1621. }
  1622. /**
  1623. * Output the linking rules on a command line. For executables,
  1624. * targetLibrary should be a NULL pointer. For libraries, it should point
  1625. * to the name of the library. This will not link a library against itself.
  1626. */
  1627. void cmLocalGenerator::OutputLinkLibraries(std::string& linkLibraries,
  1628. std::string& frameworkPath,
  1629. std::string& linkPath,
  1630. cmGeneratorTarget &tgt,
  1631. bool relink)
  1632. {
  1633. cmOStringStream fout;
  1634. const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1635. cmComputeLinkInformation* pcli = tgt.Target->GetLinkInformation(config);
  1636. if(!pcli)
  1637. {
  1638. return;
  1639. }
  1640. cmComputeLinkInformation& cli = *pcli;
  1641. // Collect library linking flags command line options.
  1642. std::string linkLibs;
  1643. const char* linkLanguage = cli.GetLinkLanguage();
  1644. std::string libPathFlag =
  1645. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1646. std::string libPathTerminator =
  1647. this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
  1648. // Flags to link an executable to shared libraries.
  1649. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1650. linkFlagsVar += linkLanguage;
  1651. linkFlagsVar += "_FLAGS";
  1652. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1653. {
  1654. linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1655. linkLibs += " ";
  1656. }
  1657. // Append the framework search path flags.
  1658. std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
  1659. for(std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  1660. fdi != fwDirs.end(); ++fdi)
  1661. {
  1662. frameworkPath += "-F";
  1663. frameworkPath += this->Convert(fdi->c_str(), NONE, SHELL, false);
  1664. frameworkPath += " ";
  1665. }
  1666. // Append the library search path flags.
  1667. std::vector<std::string> const& libDirs = cli.GetDirectories();
  1668. for(std::vector<std::string>::const_iterator libDir = libDirs.begin();
  1669. libDir != libDirs.end(); ++libDir)
  1670. {
  1671. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1672. linkPath += " " + libPathFlag;
  1673. linkPath += libpath;
  1674. linkPath += libPathTerminator;
  1675. linkPath += " ";
  1676. }
  1677. // Append the link items.
  1678. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1679. ItemVector const& items = cli.GetItems();
  1680. for(ItemVector::const_iterator li = items.begin(); li != items.end(); ++li)
  1681. {
  1682. if(li->Target && li->Target->GetType() == cmTarget::INTERFACE_LIBRARY)
  1683. {
  1684. continue;
  1685. }
  1686. if(li->IsPath)
  1687. {
  1688. linkLibs += this->ConvertToLinkReference(li->Value);
  1689. }
  1690. else
  1691. {
  1692. linkLibs += li->Value;
  1693. }
  1694. linkLibs += " ";
  1695. }
  1696. // Write the library flags to the build rule.
  1697. fout << linkLibs;
  1698. // Get the RPATH entries.
  1699. std::vector<std::string> runtimeDirs;
  1700. cli.GetRPath(runtimeDirs, relink);
  1701. // Check what kind of rpath flags to use.
  1702. if(cli.GetRuntimeSep().empty())
  1703. {
  1704. // Each rpath entry gets its own option ("-R a -R b -R c")
  1705. std::string rpath;
  1706. for(std::vector<std::string>::iterator ri = runtimeDirs.begin();
  1707. ri != runtimeDirs.end(); ++ri)
  1708. {
  1709. rpath += cli.GetRuntimeFlag();
  1710. rpath += this->Convert(ri->c_str(), NONE, SHELL, false);
  1711. rpath += " ";
  1712. }
  1713. fout << rpath;
  1714. }
  1715. else
  1716. {
  1717. // All rpath entries are combined ("-Wl,-rpath,a:b:c").
  1718. std::string rpath = cli.GetRPathString(relink);
  1719. // Store the rpath option in the stream.
  1720. if(!rpath.empty())
  1721. {
  1722. fout << cli.GetRuntimeFlag();
  1723. fout << this->EscapeForShell(rpath.c_str(), true);
  1724. fout << " ";
  1725. }
  1726. }
  1727. // Add the linker runtime search path if any.
  1728. std::string rpath_link = cli.GetRPathLinkString();
  1729. if(!cli.GetRPathLinkFlag().empty() && !rpath_link.empty())
  1730. {
  1731. fout << cli.GetRPathLinkFlag();
  1732. fout << this->EscapeForShell(rpath_link.c_str(), true);
  1733. fout << " ";
  1734. }
  1735. // Add standard libraries for this language.
  1736. std::string standardLibsVar = "CMAKE_";
  1737. standardLibsVar += cli.GetLinkLanguage();
  1738. standardLibsVar += "_STANDARD_LIBRARIES";
  1739. if(const char* stdLibs =
  1740. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1741. {
  1742. fout << stdLibs << " ";
  1743. }
  1744. linkLibraries = fout.str();
  1745. }
  1746. //----------------------------------------------------------------------------
  1747. void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
  1748. cmGeneratorTarget* target,
  1749. const char *lang,
  1750. const char* config)
  1751. {
  1752. // Only add Mac OS X specific flags on Darwin platforms (OSX and iphone):
  1753. if(!this->Makefile->IsOn("APPLE"))
  1754. {
  1755. return;
  1756. }
  1757. if(this->EmitUniversalBinaryFlags)
  1758. {
  1759. std::vector<std::string> archs;
  1760. target->GetAppleArchs(config, archs);
  1761. const char* sysroot = this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1762. if(sysroot && sysroot[0] == '/' && !sysroot[1])
  1763. { sysroot = 0; }
  1764. std::string sysrootFlagVar =
  1765. std::string("CMAKE_") + lang + "_SYSROOT_FLAG";
  1766. const char* sysrootFlag =
  1767. this->Makefile->GetDefinition(sysrootFlagVar.c_str());
  1768. const char* deploymentTarget =
  1769. this->Makefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  1770. std::string deploymentTargetFlagVar =
  1771. std::string("CMAKE_") + lang + "_OSX_DEPLOYMENT_TARGET_FLAG";
  1772. const char* deploymentTargetFlag =
  1773. this->Makefile->GetDefinition(deploymentTargetFlagVar.c_str());
  1774. if(!archs.empty() && lang && (lang[0] =='C' || lang[0] == 'F'))
  1775. {
  1776. for(std::vector<std::string>::iterator i = archs.begin();
  1777. i != archs.end(); ++i)
  1778. {
  1779. flags += " -arch ";
  1780. flags += *i;
  1781. }
  1782. }
  1783. if(sysrootFlag && *sysrootFlag && sysroot && *sysroot)
  1784. {
  1785. flags += " ";
  1786. flags += sysrootFlag;
  1787. flags += " ";
  1788. flags += sysroot;
  1789. }
  1790. if (deploymentTargetFlag && *deploymentTargetFlag &&
  1791. deploymentTarget && *deploymentTarget)
  1792. {
  1793. flags += " ";
  1794. flags += deploymentTargetFlag;
  1795. flags += deploymentTarget;
  1796. }
  1797. }
  1798. }
  1799. //----------------------------------------------------------------------------
  1800. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1801. const char* lang,
  1802. const char* config)
  1803. {
  1804. // Add language-specific flags.
  1805. std::string flagsVar = "CMAKE_";
  1806. flagsVar += lang;
  1807. flagsVar += "_FLAGS";
  1808. this->AddConfigVariableFlags(flags, flagsVar.c_str(), config);
  1809. }
  1810. //----------------------------------------------------------------------------
  1811. bool cmLocalGenerator::GetRealDependency(const char* inName,
  1812. const char* config,
  1813. std::string& dep)
  1814. {
  1815. // Older CMake code may specify the dependency using the target
  1816. // output file rather than the target name. Such code would have
  1817. // been written before there was support for target properties that
  1818. // modify the name so stripping down to just the file name should
  1819. // produce the target name in this case.
  1820. std::string name = cmSystemTools::GetFilenameName(inName);
  1821. // If the input name is the empty string, there is no real
  1822. // dependency. Short-circuit the other checks:
  1823. if(name == "")
  1824. {
  1825. return false;
  1826. }
  1827. if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
  1828. {
  1829. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1830. }
  1831. // Look for a CMake target with the given name.
  1832. if(cmTarget* target = this->Makefile->FindTargetToUse(name.c_str()))
  1833. {
  1834. // make sure it is not just a coincidence that the target name
  1835. // found is part of the inName
  1836. if(cmSystemTools::FileIsFullPath(inName))
  1837. {
  1838. std::string tLocation;
  1839. if(target->GetType() >= cmTarget::EXECUTABLE &&
  1840. target->GetType() <= cmTarget::MODULE_LIBRARY)
  1841. {
  1842. tLocation = target->GetLocation(config);
  1843. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1844. tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
  1845. }
  1846. std::string depLocation = cmSystemTools::GetFilenamePath(
  1847. std::string(inName));
  1848. depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
  1849. if(depLocation != tLocation)
  1850. {
  1851. // it is a full path to a depend that has the same name
  1852. // as a target but is in a different location so do not use
  1853. // the target as the depend
  1854. dep = inName;
  1855. return true;
  1856. }
  1857. }
  1858. switch (target->GetType())
  1859. {
  1860. case cmTarget::EXECUTABLE:
  1861. case cmTarget::STATIC_LIBRARY:
  1862. case cmTarget::SHARED_LIBRARY:
  1863. case cmTarget::MODULE_LIBRARY:
  1864. case cmTarget::UNKNOWN_LIBRARY:
  1865. dep = target->GetLocation(config);
  1866. return true;
  1867. case cmTarget::OBJECT_LIBRARY:
  1868. // An object library has no single file on which to depend.
  1869. // This was listed to get the target-level dependency.
  1870. return false;
  1871. case cmTarget::INTERFACE_LIBRARY:
  1872. // An interface library has no file on which to depend.
  1873. // This was listed to get the target-level dependency.
  1874. return false;
  1875. case cmTarget::UTILITY:
  1876. case cmTarget::GLOBAL_TARGET:
  1877. // A utility target has no file on which to depend. This was listed
  1878. // only to get the target-level dependency.
  1879. return false;
  1880. }
  1881. }
  1882. // The name was not that of a CMake target. It must name a file.
  1883. if(cmSystemTools::FileIsFullPath(inName))
  1884. {
  1885. // This is a full path. Return it as given.
  1886. dep = inName;
  1887. return true;
  1888. }
  1889. // Check for a source file in this directory that matches the
  1890. // dependency.
  1891. if(cmSourceFile* sf = this->Makefile->GetSource(inName))
  1892. {
  1893. dep = sf->GetFullPath();
  1894. return true;
  1895. }
  1896. // Treat the name as relative to the source directory in which it
  1897. // was given.
  1898. dep = this->Makefile->GetCurrentDirectory();
  1899. dep += "/";
  1900. dep += inName;
  1901. return true;
  1902. }
  1903. //----------------------------------------------------------------------------
  1904. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1905. const char* lang,
  1906. bool shared)
  1907. {
  1908. std::string flagsVar;
  1909. // Add flags for dealing with shared libraries for this language.
  1910. if(shared)
  1911. {
  1912. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1913. flagsVar += lang;
  1914. flagsVar += "_FLAGS";
  1915. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1916. }
  1917. }
  1918. static void AddVisibilityCompileOption(std::string &flags, cmTarget* target,
  1919. cmLocalGenerator *lg, const char *lang)
  1920. {
  1921. std::string l(lang);
  1922. std::string compileOption = "CMAKE_" + l + "_COMPILE_OPTIONS_VISIBILITY";
  1923. const char *opt = lg->GetMakefile()->GetDefinition(compileOption.c_str());
  1924. if (!opt)
  1925. {
  1926. return;
  1927. }
  1928. std::string flagDefine = l + "_VISIBILITY_PRESET";
  1929. const char *prop = target->GetProperty(flagDefine.c_str());
  1930. if (!prop)
  1931. {
  1932. return;
  1933. }
  1934. if (strcmp(prop, "hidden") != 0
  1935. && strcmp(prop, "default") != 0
  1936. && strcmp(prop, "protected") != 0
  1937. && strcmp(prop, "internal") != 0 )
  1938. {
  1939. cmOStringStream e;
  1940. e << "Target " << target->GetName() << " uses unsupported value \""
  1941. << prop << "\" for " << flagDefine << ".";
  1942. cmSystemTools::Error(e.str().c_str());
  1943. return;
  1944. }
  1945. std::string option = std::string(opt) + prop;
  1946. lg->AppendFlags(flags, option.c_str());
  1947. }
  1948. static void AddInlineVisibilityCompileOption(std::string &flags,
  1949. cmTarget* target,
  1950. cmLocalGenerator *lg)
  1951. {
  1952. std::string compileOption
  1953. = "CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN";
  1954. const char *opt = lg->GetMakefile()->GetDefinition(compileOption.c_str());
  1955. if (!opt)
  1956. {
  1957. return;
  1958. }
  1959. bool prop = target->GetPropertyAsBool("VISIBILITY_INLINES_HIDDEN");
  1960. if (!prop)
  1961. {
  1962. return;
  1963. }
  1964. lg->AppendFlags(flags, opt);
  1965. }
  1966. //----------------------------------------------------------------------------
  1967. void cmLocalGenerator
  1968. ::AddVisibilityPresetFlags(std::string &flags, cmTarget* target,
  1969. const char *lang)
  1970. {
  1971. int targetType = target->GetType();
  1972. bool suitableTarget = ((targetType == cmTarget::SHARED_LIBRARY)
  1973. || (targetType == cmTarget::MODULE_LIBRARY)
  1974. || (target->IsExecutableWithExports()));
  1975. if (!suitableTarget)
  1976. {
  1977. return;
  1978. }
  1979. if (!lang)
  1980. {
  1981. return;
  1982. }
  1983. AddVisibilityCompileOption(flags, target, this, lang);
  1984. AddInlineVisibilityCompileOption(flags, target, this);
  1985. }
  1986. //----------------------------------------------------------------------------
  1987. void cmLocalGenerator::AddCMP0018Flags(std::string &flags, cmTarget* target,
  1988. std::string const& lang,
  1989. const char *config)
  1990. {
  1991. int targetType = target->GetType();
  1992. bool shared = ((targetType == cmTarget::SHARED_LIBRARY) ||
  1993. (targetType == cmTarget::MODULE_LIBRARY));
  1994. if (this->GetShouldUseOldFlags(shared, lang))
  1995. {
  1996. this->AddSharedFlags(flags, lang.c_str(), shared);
  1997. }
  1998. else
  1999. {
  2000. if (target->GetType() == cmTarget::OBJECT_LIBRARY)
  2001. {
  2002. if (target->GetPropertyAsBool("POSITION_INDEPENDENT_CODE"))
  2003. {
  2004. this->AddPositionIndependentFlags(flags, lang, targetType);
  2005. }
  2006. return;
  2007. }
  2008. if (target->GetLinkInterfaceDependentBoolProperty(
  2009. "POSITION_INDEPENDENT_CODE",
  2010. config))
  2011. {
  2012. this->AddPositionIndependentFlags(flags, lang, targetType);
  2013. }
  2014. if (shared)
  2015. {
  2016. this->AppendFeatureOptions(flags, lang.c_str(), "DLL");
  2017. }
  2018. }
  2019. }
  2020. //----------------------------------------------------------------------------
  2021. bool cmLocalGenerator::GetShouldUseOldFlags(bool shared,
  2022. const std::string &lang) const
  2023. {
  2024. std::string originalFlags =
  2025. this->GlobalGenerator->GetSharedLibFlagsForLanguage(lang);
  2026. if (shared)
  2027. {
  2028. std::string flagsVar = "CMAKE_SHARED_LIBRARY_";
  2029. flagsVar += lang;
  2030. flagsVar += "_FLAGS";
  2031. const char* flags =
  2032. this->Makefile->GetSafeDefinition(flagsVar.c_str());
  2033. if (flags && flags != originalFlags)
  2034. {
  2035. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0018))
  2036. {
  2037. case cmPolicies::WARN:
  2038. {
  2039. cmOStringStream e;
  2040. e << "Variable " << flagsVar << " has been modified. CMake "
  2041. "will ignore the POSITION_INDEPENDENT_CODE target property for "
  2042. "shared libraries and will use the " << flagsVar << " variable "
  2043. "instead. This may cause errors if the original content of "
  2044. << flagsVar << " was removed.\n"
  2045. << this->Makefile->GetPolicies()->GetPolicyWarning(
  2046. cmPolicies::CMP0018);
  2047. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  2048. // fall through to OLD behaviour
  2049. }
  2050. case cmPolicies::OLD:
  2051. return true;
  2052. case cmPolicies::REQUIRED_IF_USED:
  2053. case cmPolicies::REQUIRED_ALWAYS:
  2054. case cmPolicies::NEW:
  2055. default:
  2056. return false;
  2057. }
  2058. }
  2059. }
  2060. return false;
  2061. }
  2062. //----------------------------------------------------------------------------
  2063. void cmLocalGenerator::AddPositionIndependentFlags(std::string& flags,
  2064. std::string const& lang,
  2065. int targetType)
  2066. {
  2067. const char* picFlags = 0;
  2068. if(targetType == cmTarget::EXECUTABLE)
  2069. {
  2070. std::string flagsVar = "CMAKE_";
  2071. flagsVar += lang;
  2072. flagsVar += "_COMPILE_OPTIONS_PIE";
  2073. picFlags = this->Makefile->GetSafeDefinition(flagsVar.c_str());
  2074. }
  2075. if (!picFlags)
  2076. {
  2077. std::string flagsVar = "CMAKE_";
  2078. flagsVar += lang;
  2079. flagsVar += "_COMPILE_OPTIONS_PIC";
  2080. picFlags = this->Makefile->GetSafeDefinition(flagsVar.c_str());
  2081. }
  2082. if (picFlags)
  2083. {
  2084. std::vector<std::string> options;
  2085. cmSystemTools::ExpandListArgument(picFlags, options);
  2086. for(std::vector<std::string>::const_iterator oi = options.begin();
  2087. oi != options.end(); ++oi)
  2088. {
  2089. this->AppendFlagEscape(flags, oi->c_str());
  2090. }
  2091. }
  2092. }
  2093. //----------------------------------------------------------------------------
  2094. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  2095. const char* var,
  2096. const char* config)
  2097. {
  2098. // Add the flags from the variable itself.
  2099. std::string flagsVar = var;
  2100. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  2101. // Add the flags from the build-type specific variable.
  2102. if(config && *config)
  2103. {
  2104. flagsVar += "_";
  2105. flagsVar += cmSystemTools::UpperCase(config);
  2106. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  2107. }
  2108. }
  2109. //----------------------------------------------------------------------------
  2110. void cmLocalGenerator::AppendFlags(std::string& flags,
  2111. const char* newFlags)
  2112. {
  2113. if(newFlags && *newFlags)
  2114. {
  2115. std::string newf = newFlags;
  2116. if(flags.size())
  2117. {
  2118. flags += " ";
  2119. }
  2120. flags += newFlags;
  2121. }
  2122. }
  2123. //----------------------------------------------------------------------------
  2124. void cmLocalGenerator::AppendFlagEscape(std::string& flags,
  2125. const char* rawFlag)
  2126. {
  2127. this->AppendFlags(flags, this->EscapeForShell(rawFlag).c_str());
  2128. }
  2129. //----------------------------------------------------------------------------
  2130. void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
  2131. const char* defines_list)
  2132. {
  2133. // Short-circuit if there are no definitions.
  2134. if(!defines_list)
  2135. {
  2136. return;
  2137. }
  2138. // Expand the list of definitions.
  2139. std::vector<std::string> defines_vec;
  2140. cmSystemTools::ExpandListArgument(defines_list, defines_vec);
  2141. this->AppendDefines(defines, defines_vec);
  2142. }
  2143. //----------------------------------------------------------------------------
  2144. void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
  2145. const std::vector<std::string> &defines_vec)
  2146. {
  2147. for(std::vector<std::string>::const_iterator di = defines_vec.begin();
  2148. di != defines_vec.end(); ++di)
  2149. {
  2150. // Skip unsupported definitions.
  2151. if(!this->CheckDefinition(*di))
  2152. {
  2153. continue;
  2154. }
  2155. defines.insert(*di);
  2156. }
  2157. }
  2158. //----------------------------------------------------------------------------
  2159. void cmLocalGenerator::JoinDefines(const std::set<std::string>& defines,
  2160. std::string &definesString,
  2161. const char* lang)
  2162. {
  2163. // Lookup the define flag for the current language.
  2164. std::string dflag = "-D";
  2165. if(lang)
  2166. {
  2167. std::string defineFlagVar = "CMAKE_";
  2168. defineFlagVar += lang;
  2169. defineFlagVar += "_DEFINE_FLAG";
  2170. const char* df = this->Makefile->GetDefinition(defineFlagVar.c_str());
  2171. if(df && *df)
  2172. {
  2173. dflag = df;
  2174. }
  2175. }
  2176. std::set<std::string>::const_iterator defineIt = defines.begin();
  2177. const std::set<std::string>::const_iterator defineEnd = defines.end();
  2178. const char* itemSeparator = definesString.empty() ? "" : " ";
  2179. for( ; defineIt != defineEnd; ++defineIt)
  2180. {
  2181. // Append the definition with proper escaping.
  2182. std::string def = dflag;
  2183. if(this->WatcomWMake)
  2184. {
  2185. // The Watcom compiler does its own command line parsing instead
  2186. // of using the windows shell rules. Definitions are one of
  2187. // -DNAME
  2188. // -DNAME=<cpp-token>
  2189. // -DNAME="c-string with spaces and other characters(?@#$)"
  2190. //
  2191. // Watcom will properly parse each of these cases from the
  2192. // command line without any escapes. However we still have to
  2193. // get the '$' and '#' characters through WMake as '$$' and
  2194. // '$#'.
  2195. for(const char* c = defineIt->c_str(); *c; ++c)
  2196. {
  2197. if(*c == '$' || *c == '#')
  2198. {
  2199. def += '$';
  2200. }
  2201. def += *c;
  2202. }
  2203. }
  2204. else
  2205. {
  2206. // Make the definition appear properly on the command line. Use
  2207. // -DNAME="value" instead of -D"NAME=value" to help VS6 parser.
  2208. std::string::size_type eq = defineIt->find("=");
  2209. def += defineIt->substr(0, eq);
  2210. if(eq != defineIt->npos)
  2211. {
  2212. def += "=";
  2213. def += this->EscapeForShell(defineIt->c_str() + eq + 1, true);
  2214. }
  2215. }
  2216. definesString += itemSeparator;
  2217. itemSeparator = " ";
  2218. definesString += def;
  2219. }
  2220. }
  2221. //----------------------------------------------------------------------------
  2222. void cmLocalGenerator::AppendFeatureOptions(
  2223. std::string& flags, const char* lang, const char* feature)
  2224. {
  2225. std::string optVar = "CMAKE_";
  2226. optVar += lang;
  2227. optVar += "_COMPILE_OPTIONS_";
  2228. optVar += feature;
  2229. if(const char* optionList = this->Makefile->GetDefinition(optVar.c_str()))
  2230. {
  2231. std::vector<std::string> options;
  2232. cmSystemTools::ExpandListArgument(optionList, options);
  2233. for(std::vector<std::string>::const_iterator oi = options.begin();
  2234. oi != options.end(); ++oi)
  2235. {
  2236. this->AppendFlagEscape(flags, oi->c_str());
  2237. }
  2238. }
  2239. }
  2240. //----------------------------------------------------------------------------
  2241. std::string
  2242. cmLocalGenerator::ConstructComment(const cmCustomCommand& cc,
  2243. const char* default_comment)
  2244. {
  2245. // Check for a comment provided with the command.
  2246. if(cc.GetComment())
  2247. {
  2248. return cc.GetComment();
  2249. }
  2250. // Construct a reasonable default comment if possible.
  2251. if(!cc.GetOutputs().empty())
  2252. {
  2253. std::string comment;
  2254. comment = "Generating ";
  2255. const char* sep = "";
  2256. for(std::vector<std::string>::const_iterator o = cc.GetOutputs().begin();
  2257. o != cc.GetOutputs().end(); ++o)
  2258. {
  2259. comment += sep;
  2260. comment += this->Convert(o->c_str(), cmLocalGenerator::START_OUTPUT);
  2261. sep = ", ";
  2262. }
  2263. return comment;
  2264. }
  2265. // Otherwise use the provided default.
  2266. return default_comment;
  2267. }
  2268. //----------------------------------------------------------------------------
  2269. std::string
  2270. cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
  2271. {
  2272. return this->Convert(remote, START_OUTPUT, SHELL, true);
  2273. }
  2274. //----------------------------------------------------------------------------
  2275. const char* cmLocalGenerator::GetRelativeRootPath(RelativeRoot relroot)
  2276. {
  2277. switch (relroot)
  2278. {
  2279. case HOME: return this->Makefile->GetHomeDirectory();
  2280. case START: return this->Makefile->GetStartDirectory();
  2281. case HOME_OUTPUT: return this->Makefile->GetHomeOutputDirectory();
  2282. case START_OUTPUT: return this->Makefile->GetStartOutputDirectory();
  2283. default: break;
  2284. }
  2285. return 0;
  2286. }
  2287. //----------------------------------------------------------------------------
  2288. std::string cmLocalGenerator::Convert(const char* source,
  2289. RelativeRoot relative,
  2290. OutputFormat output,
  2291. bool optional)
  2292. {
  2293. // Make sure the relative path conversion components are set.
  2294. if(!this->PathConversionsSetup)
  2295. {
  2296. this->SetupPathConversions();
  2297. this->PathConversionsSetup = true;
  2298. }
  2299. // Convert the path to a relative path.
  2300. std::string result = source;
  2301. if (!optional || this->UseRelativePaths)
  2302. {
  2303. switch (relative)
  2304. {
  2305. case HOME:
  2306. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2307. result = this->ConvertToRelativePath(this->HomeDirectoryComponents,
  2308. result.c_str());
  2309. break;
  2310. case START:
  2311. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2312. result = this->ConvertToRelativePath(this->StartDirectoryComponents,
  2313. result.c_str());
  2314. break;
  2315. case HOME_OUTPUT:
  2316. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2317. result =
  2318. this->ConvertToRelativePath(this->HomeOutputDirectoryComponents,
  2319. result.c_str());
  2320. break;
  2321. case START_OUTPUT:
  2322. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2323. result =
  2324. this->ConvertToRelativePath(this->StartOutputDirectoryComponents,
  2325. result.c_str());
  2326. break;
  2327. case FULL:
  2328. result = cmSystemTools::CollapseFullPath(result.c_str());
  2329. break;
  2330. case NONE:
  2331. break;
  2332. }
  2333. }
  2334. return this->ConvertToOutputFormat(result.c_str(), output);
  2335. }
  2336. //----------------------------------------------------------------------------
  2337. std::string cmLocalGenerator::ConvertToOutputFormat(const char* source,
  2338. OutputFormat output)
  2339. {
  2340. std::string result = source;
  2341. // Convert it to an output path.
  2342. if (output == MAKEFILE)
  2343. {
  2344. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  2345. }
  2346. else if( output == SHELL)
  2347. {
  2348. // For the MSYS shell convert drive letters to posix paths, so
  2349. // that c:/some/path becomes /c/some/path. This is needed to
  2350. // avoid problems with the shell path translation.
  2351. if(this->MSYSShell && !this->LinkScriptShell)
  2352. {
  2353. if(result.size() > 2 && result[1] == ':')
  2354. {
  2355. result[1] = result[0];
  2356. result[0] = '/';
  2357. }
  2358. }
  2359. if(this->WindowsShell)
  2360. {
  2361. std::string::size_type pos = 0;
  2362. while((pos = result.find('/', pos)) != std::string::npos)
  2363. {
  2364. result[pos] = '\\';
  2365. pos++;
  2366. }
  2367. }
  2368. result = this->EscapeForShell(result.c_str(), true, false);
  2369. }
  2370. else if(output == RESPONSE)
  2371. {
  2372. result = this->EscapeForShell(result.c_str(), false, false);
  2373. }
  2374. return result;
  2375. }
  2376. //----------------------------------------------------------------------------
  2377. std::string cmLocalGenerator::Convert(RelativeRoot remote,
  2378. const char* local,
  2379. OutputFormat output,
  2380. bool optional)
  2381. {
  2382. const char* remotePath = this->GetRelativeRootPath(remote);
  2383. // The relative root must have a path (i.e. not FULL or NONE)
  2384. assert(remotePath != 0);
  2385. if(local && (!optional || this->UseRelativePaths))
  2386. {
  2387. std::vector<std::string> components;
  2388. cmSystemTools::SplitPath(local, components);
  2389. std::string result = this->ConvertToRelativePath(components, remotePath);
  2390. return this->ConvertToOutputFormat(result.c_str(), output);
  2391. }
  2392. else
  2393. {
  2394. return this->ConvertToOutputFormat(remotePath, output);
  2395. }
  2396. }
  2397. //----------------------------------------------------------------------------
  2398. std::string cmLocalGenerator::FindRelativePathTopSource()
  2399. {
  2400. // Relative path conversion within a single tree managed by CMake is
  2401. // safe. We can use our parent relative path top if and only if
  2402. // this is a subdirectory of that top.
  2403. if(cmLocalGenerator* parent = this->GetParent())
  2404. {
  2405. std::string parentTop = parent->FindRelativePathTopSource();
  2406. if(cmSystemTools::IsSubDirectory(
  2407. this->Makefile->GetStartDirectory(), parentTop.c_str()))
  2408. {
  2409. return parentTop;
  2410. }
  2411. }
  2412. // Otherwise this directory itself is the new top.
  2413. return this->Makefile->GetStartDirectory();
  2414. }
  2415. //----------------------------------------------------------------------------
  2416. std::string cmLocalGenerator::FindRelativePathTopBinary()
  2417. {
  2418. // Relative path conversion within a single tree managed by CMake is
  2419. // safe. We can use our parent relative path top if and only if
  2420. // this is a subdirectory of that top.
  2421. if(cmLocalGenerator* parent = this->GetParent())
  2422. {
  2423. std::string parentTop = parent->FindRelativePathTopBinary();
  2424. if(cmSystemTools::IsSubDirectory(
  2425. this->Makefile->GetStartOutputDirectory(), parentTop.c_str()))
  2426. {
  2427. return parentTop;
  2428. }
  2429. }
  2430. // Otherwise this directory itself is the new top.
  2431. return this->Makefile->GetStartOutputDirectory();
  2432. }
  2433. //----------------------------------------------------------------------------
  2434. void cmLocalGenerator::ConfigureRelativePaths()
  2435. {
  2436. // Relative path conversion inside the source tree is not used to
  2437. // construct relative paths passed to build tools so it is safe to
  2438. // even when the source is a network path.
  2439. std::string source = this->FindRelativePathTopSource();
  2440. this->RelativePathTopSource = source;
  2441. // The current working directory on Windows cannot be a network
  2442. // path. Therefore relative paths cannot work when the binary tree
  2443. // is a network path.
  2444. std::string binary = this->FindRelativePathTopBinary();
  2445. if(binary.size() < 2 || binary.substr(0, 2) != "//")
  2446. {
  2447. this->RelativePathTopBinary = binary;
  2448. }
  2449. else
  2450. {
  2451. this->RelativePathTopBinary = "";
  2452. }
  2453. }
  2454. //----------------------------------------------------------------------------
  2455. static bool cmLocalGeneratorNotAbove(const char* a, const char* b)
  2456. {
  2457. return (cmSystemTools::ComparePath(a, b) ||
  2458. cmSystemTools::IsSubDirectory(a, b));
  2459. }
  2460. //----------------------------------------------------------------------------
  2461. std::string
  2462. cmLocalGenerator::ConvertToRelativePath(const std::vector<std::string>& local,
  2463. const char* in_remote, bool force)
  2464. {
  2465. // The path should never be quoted.
  2466. assert(in_remote[0] != '\"');
  2467. // The local path should never have a trailing slash.
  2468. assert(local.size() > 0 && !(local[local.size()-1] == ""));
  2469. // If the path is already relative then just return the path.
  2470. if(!cmSystemTools::FileIsFullPath(in_remote))
  2471. {
  2472. return in_remote;
  2473. }
  2474. // Make sure relative path conversion is configured.
  2475. if(!this->RelativePathsConfigured)
  2476. {
  2477. this->ConfigureRelativePaths();
  2478. this->RelativePathsConfigured = true;
  2479. }
  2480. if(!force)
  2481. {
  2482. // Skip conversion if the path and local are not both in the source
  2483. // or both in the binary tree.
  2484. std::string local_path = cmSystemTools::JoinPath(local);
  2485. if(!((cmLocalGeneratorNotAbove(local_path.c_str(),
  2486. this->RelativePathTopBinary.c_str()) &&
  2487. cmLocalGeneratorNotAbove(in_remote,
  2488. this->RelativePathTopBinary.c_str())) ||
  2489. (cmLocalGeneratorNotAbove(local_path.c_str(),
  2490. this->RelativePathTopSource.c_str()) &&
  2491. cmLocalGeneratorNotAbove(in_remote,
  2492. this->RelativePathTopSource.c_str()))))
  2493. {
  2494. return in_remote;
  2495. }
  2496. }
  2497. // Identify the longest shared path component between the remote
  2498. // path and the local path.
  2499. std::vector<std::string> remote;
  2500. cmSystemTools::SplitPath(in_remote, remote);
  2501. unsigned int common=0;
  2502. while(common < remote.size() &&
  2503. common < local.size() &&
  2504. cmSystemTools::ComparePath(remote[common].c_str(),
  2505. local[common].c_str()))
  2506. {
  2507. ++common;
  2508. }
  2509. // If no part of the path is in common then return the full path.
  2510. if(common == 0)
  2511. {
  2512. return in_remote;
  2513. }
  2514. // If the entire path is in common then just return a ".".
  2515. if(common == remote.size() &&
  2516. common == local.size())
  2517. {
  2518. return ".";
  2519. }
  2520. // If the entire path is in common except for a trailing slash then
  2521. // just return a "./".
  2522. if(common+1 == remote.size() &&
  2523. remote[common].size() == 0 &&
  2524. common == local.size())
  2525. {
  2526. return "./";
  2527. }
  2528. // Construct the relative path.
  2529. std::string relative;
  2530. // First add enough ../ to get up to the level of the shared portion
  2531. // of the path. Leave off the trailing slash. Note that the last
  2532. // component of local will never be empty because local should never
  2533. // have a trailing slash.
  2534. for(unsigned int i=common; i < local.size(); ++i)
  2535. {
  2536. relative += "..";
  2537. if(i < local.size()-1)
  2538. {
  2539. relative += "/";
  2540. }
  2541. }
  2542. // Now add the portion of the destination path that is not included
  2543. // in the shared portion of the path. Add a slash the first time
  2544. // only if there was already something in the path. If there was a
  2545. // trailing slash in the input then the last iteration of the loop
  2546. // will add a slash followed by an empty string which will preserve
  2547. // the trailing slash in the output.
  2548. for(unsigned int i=common; i < remote.size(); ++i)
  2549. {
  2550. if(relative.size() > 0)
  2551. {
  2552. relative += "/";
  2553. }
  2554. relative += remote[i];
  2555. }
  2556. // Finally return the path.
  2557. return relative;
  2558. }
  2559. //----------------------------------------------------------------------------
  2560. void
  2561. cmLocalGenerator
  2562. ::GenerateTargetInstallRules(
  2563. std::ostream& os, const char* config,
  2564. std::vector<std::string> const& configurationTypes)
  2565. {
  2566. // Convert the old-style install specification from each target to
  2567. // an install generator and run it.
  2568. cmTargets& tgts = this->Makefile->GetTargets();
  2569. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  2570. {
  2571. // Include the user-specified pre-install script for this target.
  2572. if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
  2573. {
  2574. cmInstallScriptGenerator g(preinstall, false, 0);
  2575. g.Generate(os, config, configurationTypes);
  2576. }
  2577. // Install this target if a destination is given.
  2578. if(l->second.GetInstallPath() != "")
  2579. {
  2580. // Compute the full install destination. Note that converting
  2581. // to unix slashes also removes any trailing slash.
  2582. // We also skip over the leading slash given by the user.
  2583. std::string destination = l->second.GetInstallPath().substr(1);
  2584. cmSystemTools::ConvertToUnixSlashes(destination);
  2585. if(destination.empty())
  2586. {
  2587. destination = ".";
  2588. }
  2589. // Generate the proper install generator for this target type.
  2590. switch(l->second.GetType())
  2591. {
  2592. case cmTarget::EXECUTABLE:
  2593. case cmTarget::STATIC_LIBRARY:
  2594. case cmTarget::MODULE_LIBRARY:
  2595. {
  2596. // Use a target install generator.
  2597. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  2598. g.Generate(os, config, configurationTypes);
  2599. }
  2600. break;
  2601. case cmTarget::SHARED_LIBRARY:
  2602. {
  2603. #if defined(_WIN32) || defined(__CYGWIN__)
  2604. // Special code to handle DLL. Install the import library
  2605. // to the normal destination and the DLL to the runtime
  2606. // destination.
  2607. cmInstallTargetGenerator g1(l->second, destination.c_str(), true);
  2608. g1.Generate(os, config, configurationTypes);
  2609. // We also skip over the leading slash given by the user.
  2610. destination = l->second.GetRuntimeInstallPath().substr(1);
  2611. cmSystemTools::ConvertToUnixSlashes(destination);
  2612. cmInstallTargetGenerator g2(l->second, destination.c_str(), false);
  2613. g2.Generate(os, config, configurationTypes);
  2614. #else
  2615. // Use a target install generator.
  2616. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  2617. g.Generate(os, config, configurationTypes);
  2618. #endif
  2619. }
  2620. break;
  2621. default:
  2622. break;
  2623. }
  2624. }
  2625. // Include the user-specified post-install script for this target.
  2626. if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
  2627. {
  2628. cmInstallScriptGenerator g(postinstall, false, 0);
  2629. g.Generate(os, config, configurationTypes);
  2630. }
  2631. }
  2632. }
  2633. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2634. static std::string cmLocalGeneratorMD5(const char* input)
  2635. {
  2636. char md5out[32];
  2637. cmsysMD5* md5 = cmsysMD5_New();
  2638. cmsysMD5_Initialize(md5);
  2639. cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
  2640. cmsysMD5_FinalizeHex(md5, md5out);
  2641. cmsysMD5_Delete(md5);
  2642. return std::string(md5out, 32);
  2643. }
  2644. static bool
  2645. cmLocalGeneratorShortenObjectName(std::string& objName,
  2646. std::string::size_type max_len)
  2647. {
  2648. // Replace the beginning of the path portion of the object name with
  2649. // its own md5 sum.
  2650. std::string::size_type pos = objName.find('/', objName.size()-max_len+32);
  2651. if(pos != objName.npos)
  2652. {
  2653. std::string md5name = cmLocalGeneratorMD5(objName.substr(0, pos).c_str());
  2654. md5name += objName.substr(pos);
  2655. objName = md5name;
  2656. // The object name is now short enough.
  2657. return true;
  2658. }
  2659. else
  2660. {
  2661. // The object name could not be shortened enough.
  2662. return false;
  2663. }
  2664. }
  2665. static
  2666. bool cmLocalGeneratorCheckObjectName(std::string& objName,
  2667. std::string::size_type dir_len,
  2668. std::string::size_type max_total_len)
  2669. {
  2670. // Enforce the maximum file name length if possible.
  2671. std::string::size_type max_obj_len = max_total_len;
  2672. if(dir_len < max_total_len)
  2673. {
  2674. max_obj_len = max_total_len - dir_len;
  2675. if(objName.size() > max_obj_len)
  2676. {
  2677. // The current object file name is too long. Try to shorten it.
  2678. return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
  2679. }
  2680. else
  2681. {
  2682. // The object file name is short enough.
  2683. return true;
  2684. }
  2685. }
  2686. else
  2687. {
  2688. // The build directory in which the object will be stored is
  2689. // already too deep.
  2690. return false;
  2691. }
  2692. }
  2693. #endif
  2694. //----------------------------------------------------------------------------
  2695. std::string&
  2696. cmLocalGenerator
  2697. ::CreateSafeUniqueObjectFileName(const char* sin,
  2698. std::string const& dir_max)
  2699. {
  2700. // Look for an existing mapped name for this object file.
  2701. std::map<cmStdString,cmStdString>::iterator it =
  2702. this->UniqueObjectNamesMap.find(sin);
  2703. // If no entry exists create one.
  2704. if(it == this->UniqueObjectNamesMap.end())
  2705. {
  2706. // Start with the original name.
  2707. std::string ssin = sin;
  2708. // Avoid full paths by removing leading slashes.
  2709. std::string::size_type pos = 0;
  2710. for(;pos < ssin.size() && ssin[pos] == '/'; ++pos)
  2711. {
  2712. }
  2713. ssin = ssin.substr(pos);
  2714. // Avoid full paths by removing colons.
  2715. cmSystemTools::ReplaceString(ssin, ":", "_");
  2716. // Avoid relative paths that go up the tree.
  2717. cmSystemTools::ReplaceString(ssin, "../", "__/");
  2718. // Avoid spaces.
  2719. cmSystemTools::ReplaceString(ssin, " ", "_");
  2720. // Mangle the name if necessary.
  2721. if(this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
  2722. {
  2723. bool done;
  2724. int cc = 0;
  2725. char rpstr[100];
  2726. sprintf(rpstr, "_p_");
  2727. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2728. std::string sssin = sin;
  2729. do
  2730. {
  2731. done = true;
  2732. for ( it = this->UniqueObjectNamesMap.begin();
  2733. it != this->UniqueObjectNamesMap.end();
  2734. ++ it )
  2735. {
  2736. if ( it->second == ssin )
  2737. {
  2738. done = false;
  2739. }
  2740. }
  2741. if ( done )
  2742. {
  2743. break;
  2744. }
  2745. sssin = ssin;
  2746. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2747. sprintf(rpstr, "_p%d_", cc++);
  2748. }
  2749. while ( !done );
  2750. }
  2751. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2752. if(!cmLocalGeneratorCheckObjectName(ssin, dir_max.size(),
  2753. this->ObjectPathMax))
  2754. {
  2755. // Warn if this is the first time the path has been seen.
  2756. if(this->ObjectMaxPathViolations.insert(dir_max).second)
  2757. {
  2758. cmOStringStream m;
  2759. m << "The object file directory\n"
  2760. << " " << dir_max << "\n"
  2761. << "has " << dir_max.size() << " characters. "
  2762. << "The maximum full path to an object file is "
  2763. << this->ObjectPathMax << " characters "
  2764. << "(see CMAKE_OBJECT_PATH_MAX). "
  2765. << "Object file\n"
  2766. << " " << ssin << "\n"
  2767. << "cannot be safely placed under this directory. "
  2768. << "The build may not work correctly.";
  2769. this->Makefile->IssueMessage(cmake::WARNING, m.str());
  2770. }
  2771. }
  2772. #else
  2773. (void)dir_max;
  2774. #endif
  2775. // Insert the newly mapped object file name.
  2776. std::map<cmStdString, cmStdString>::value_type e(sin, ssin);
  2777. it = this->UniqueObjectNamesMap.insert(e).first;
  2778. }
  2779. // Return the map entry.
  2780. return it->second;
  2781. }
  2782. //----------------------------------------------------------------------------
  2783. std::string
  2784. cmLocalGenerator
  2785. ::GetObjectFileNameWithoutTarget(const cmSourceFile& source,
  2786. std::string const& dir_max,
  2787. bool* hasSourceExtension)
  2788. {
  2789. // Construct the object file name using the full path to the source
  2790. // file which is its only unique identification.
  2791. const char* fullPath = source.GetFullPath().c_str();
  2792. // Try referencing the source relative to the source tree.
  2793. std::string relFromSource = this->Convert(fullPath, START);
  2794. assert(!relFromSource.empty());
  2795. bool relSource = !cmSystemTools::FileIsFullPath(relFromSource.c_str());
  2796. bool subSource = relSource && relFromSource[0] != '.';
  2797. // Try referencing the source relative to the binary tree.
  2798. std::string relFromBinary = this->Convert(fullPath, START_OUTPUT);
  2799. assert(!relFromBinary.empty());
  2800. bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary.c_str());
  2801. bool subBinary = relBinary && relFromBinary[0] != '.';
  2802. // Select a nice-looking reference to the source file to construct
  2803. // the object file name.
  2804. std::string objectName;
  2805. if((relSource && !relBinary) || (subSource && !subBinary))
  2806. {
  2807. objectName = relFromSource;
  2808. }
  2809. else if((relBinary && !relSource) || (subBinary && !subSource))
  2810. {
  2811. objectName = relFromBinary;
  2812. }
  2813. else if(relFromBinary.length() < relFromSource.length())
  2814. {
  2815. objectName = relFromBinary;
  2816. }
  2817. else
  2818. {
  2819. objectName = relFromSource;
  2820. }
  2821. // if it is still a full path check for the try compile case
  2822. // try compile never have in source sources, and should not
  2823. // have conflicting source file names in the same target
  2824. if(cmSystemTools::FileIsFullPath(objectName.c_str()))
  2825. {
  2826. if(this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile())
  2827. {
  2828. objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
  2829. }
  2830. }
  2831. // Replace the original source file extension with the object file
  2832. // extension.
  2833. bool keptSourceExtension = true;
  2834. if(!source.GetPropertyAsBool("KEEP_EXTENSION"))
  2835. {
  2836. // Decide whether this language wants to replace the source
  2837. // extension with the object extension. For CMake 2.4
  2838. // compatibility do this by default.
  2839. bool replaceExt = this->NeedBackwardsCompatibility(2, 4);
  2840. if(!replaceExt)
  2841. {
  2842. if(const char* lang = source.GetLanguage())
  2843. {
  2844. std::string repVar = "CMAKE_";
  2845. repVar += lang;
  2846. repVar += "_OUTPUT_EXTENSION_REPLACE";
  2847. replaceExt = this->Makefile->IsOn(repVar.c_str());
  2848. }
  2849. }
  2850. // Remove the source extension if it is to be replaced.
  2851. if(replaceExt)
  2852. {
  2853. keptSourceExtension = false;
  2854. std::string::size_type dot_pos = objectName.rfind(".");
  2855. if(dot_pos != std::string::npos)
  2856. {
  2857. objectName = objectName.substr(0, dot_pos);
  2858. }
  2859. }
  2860. // Store the new extension.
  2861. objectName +=
  2862. this->GlobalGenerator->GetLanguageOutputExtension(source);
  2863. }
  2864. if(hasSourceExtension)
  2865. {
  2866. *hasSourceExtension = keptSourceExtension;
  2867. }
  2868. // Convert to a safe name.
  2869. return this->CreateSafeUniqueObjectFileName(objectName.c_str(), dir_max);
  2870. }
  2871. //----------------------------------------------------------------------------
  2872. const char*
  2873. cmLocalGenerator
  2874. ::GetSourceFileLanguage(const cmSourceFile& source)
  2875. {
  2876. return source.GetLanguage();
  2877. }
  2878. //----------------------------------------------------------------------------
  2879. std::string cmLocalGenerator::EscapeForShellOldStyle(const char* str)
  2880. {
  2881. std::string result;
  2882. #if defined(_WIN32) && !defined(__CYGWIN__)
  2883. // if there are spaces
  2884. std::string temp = str;
  2885. if (temp.find(" ") != std::string::npos &&
  2886. temp.find("\"")==std::string::npos)
  2887. {
  2888. result = "\"";
  2889. result += str;
  2890. result += "\"";
  2891. return result;
  2892. }
  2893. return str;
  2894. #else
  2895. for(const char* ch = str; *ch != '\0'; ++ch)
  2896. {
  2897. if(*ch == ' ')
  2898. {
  2899. result += '\\';
  2900. }
  2901. result += *ch;
  2902. }
  2903. return result;
  2904. #endif
  2905. }
  2906. //----------------------------------------------------------------------------
  2907. static bool cmLocalGeneratorIsShellOperator(const char* str)
  2908. {
  2909. if(strcmp(str, "<") == 0 ||
  2910. strcmp(str, ">") == 0 ||
  2911. strcmp(str, "<<") == 0 ||
  2912. strcmp(str, ">>") == 0 ||
  2913. strcmp(str, "|") == 0 ||
  2914. strcmp(str, "||") == 0 ||
  2915. strcmp(str, "&&") == 0 ||
  2916. strcmp(str, "&>") == 0 ||
  2917. strcmp(str, "1>") == 0 ||
  2918. strcmp(str, "2>") == 0 ||
  2919. strcmp(str, "2>&1") == 0 ||
  2920. strcmp(str, "1>&2") == 0)
  2921. {
  2922. return true;
  2923. }
  2924. return false;
  2925. }
  2926. //----------------------------------------------------------------------------
  2927. std::string cmLocalGenerator::EscapeForShell(const char* str, bool makeVars,
  2928. bool forEcho)
  2929. {
  2930. // Do not escape shell operators.
  2931. if(cmLocalGeneratorIsShellOperator(str))
  2932. {
  2933. return str;
  2934. }
  2935. // Compute the flags for the target shell environment.
  2936. int flags = 0;
  2937. if(this->WindowsVSIDE)
  2938. {
  2939. flags |= cmsysSystem_Shell_Flag_VSIDE;
  2940. }
  2941. else if(!this->LinkScriptShell)
  2942. {
  2943. flags |= cmsysSystem_Shell_Flag_Make;
  2944. }
  2945. if(makeVars)
  2946. {
  2947. flags |= cmsysSystem_Shell_Flag_AllowMakeVariables;
  2948. }
  2949. if(forEcho)
  2950. {
  2951. flags |= cmsysSystem_Shell_Flag_EchoWindows;
  2952. }
  2953. if(this->WatcomWMake)
  2954. {
  2955. flags |= cmsysSystem_Shell_Flag_WatcomWMake;
  2956. }
  2957. if(this->MinGWMake)
  2958. {
  2959. flags |= cmsysSystem_Shell_Flag_MinGWMake;
  2960. }
  2961. if(this->NMake)
  2962. {
  2963. flags |= cmsysSystem_Shell_Flag_NMake;
  2964. }
  2965. // Compute the buffer size needed.
  2966. int size = (this->WindowsShell ?
  2967. cmsysSystem_Shell_GetArgumentSizeForWindows(str, flags) :
  2968. cmsysSystem_Shell_GetArgumentSizeForUnix(str, flags));
  2969. // Compute the shell argument itself.
  2970. std::vector<char> arg(size);
  2971. if(this->WindowsShell)
  2972. {
  2973. cmsysSystem_Shell_GetArgumentForWindows(str, &arg[0], flags);
  2974. }
  2975. else
  2976. {
  2977. cmsysSystem_Shell_GetArgumentForUnix(str, &arg[0], flags);
  2978. }
  2979. return std::string(&arg[0]);
  2980. }
  2981. //----------------------------------------------------------------------------
  2982. std::string cmLocalGenerator::EscapeForCMake(const char* str)
  2983. {
  2984. // Always double-quote the argument to take care of most escapes.
  2985. std::string result = "\"";
  2986. for(const char* c = str; *c; ++c)
  2987. {
  2988. if(*c == '"')
  2989. {
  2990. // Escape the double quote to avoid ending the argument.
  2991. result += "\\\"";
  2992. }
  2993. else if(*c == '$')
  2994. {
  2995. // Escape the dollar to avoid expanding variables.
  2996. result += "\\$";
  2997. }
  2998. else if(*c == '\\')
  2999. {
  3000. // Escape the backslash to avoid other escapes.
  3001. result += "\\\\";
  3002. }
  3003. else
  3004. {
  3005. // Other characters will be parsed correctly.
  3006. result += *c;
  3007. }
  3008. }
  3009. result += "\"";
  3010. return result;
  3011. }
  3012. //----------------------------------------------------------------------------
  3013. cmLocalGenerator::FortranFormat
  3014. cmLocalGenerator::GetFortranFormat(const char* value)
  3015. {
  3016. FortranFormat format = FortranFormatNone;
  3017. if(value && *value)
  3018. {
  3019. std::vector<std::string> fmt;
  3020. cmSystemTools::ExpandListArgument(value, fmt);
  3021. for(std::vector<std::string>::iterator fi = fmt.begin();
  3022. fi != fmt.end(); ++fi)
  3023. {
  3024. if(*fi == "FIXED")
  3025. {
  3026. format = FortranFormatFixed;
  3027. }
  3028. if(*fi == "FREE")
  3029. {
  3030. format = FortranFormatFree;
  3031. }
  3032. }
  3033. }
  3034. return format;
  3035. }
  3036. //----------------------------------------------------------------------------
  3037. std::string
  3038. cmLocalGenerator::GetTargetDirectory(cmTarget const&) const
  3039. {
  3040. cmSystemTools::Error("GetTargetDirectory"
  3041. " called on cmLocalGenerator");
  3042. return "";
  3043. }
  3044. //----------------------------------------------------------------------------
  3045. unsigned int cmLocalGenerator::GetBackwardsCompatibility()
  3046. {
  3047. // The computed version may change until the project is fully
  3048. // configured.
  3049. if(!this->BackwardsCompatibilityFinal)
  3050. {
  3051. unsigned int major = 0;
  3052. unsigned int minor = 0;
  3053. unsigned int patch = 0;
  3054. if(const char* value
  3055. = this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY"))
  3056. {
  3057. switch(sscanf(value, "%u.%u.%u", &major, &minor, &patch))
  3058. {
  3059. case 2: patch = 0; break;
  3060. case 1: minor = 0; patch = 0; break;
  3061. default: break;
  3062. }
  3063. }
  3064. this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
  3065. this->BackwardsCompatibilityFinal = this->Configured;
  3066. }
  3067. return this->BackwardsCompatibility;
  3068. }
  3069. //----------------------------------------------------------------------------
  3070. bool cmLocalGenerator::NeedBackwardsCompatibility(unsigned int major,
  3071. unsigned int minor,
  3072. unsigned int patch)
  3073. {
  3074. // Check the policy to decide whether to pay attention to this
  3075. // variable.
  3076. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0001))
  3077. {
  3078. case cmPolicies::WARN:
  3079. // WARN is just OLD without warning because user code does not
  3080. // always affect whether this check is done.
  3081. case cmPolicies::OLD:
  3082. // Old behavior is to check the variable.
  3083. break;
  3084. case cmPolicies::NEW:
  3085. // New behavior is to ignore the variable.
  3086. return false;
  3087. case cmPolicies::REQUIRED_IF_USED:
  3088. case cmPolicies::REQUIRED_ALWAYS:
  3089. // This will never be the case because the only way to require
  3090. // the setting is to require the user to specify version policy
  3091. // 2.6 or higher. Once we add that requirement then this whole
  3092. // method can be removed anyway.
  3093. return false;
  3094. }
  3095. // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
  3096. // equal to or lower than the given version.
  3097. unsigned int actual_compat = this->GetBackwardsCompatibility();
  3098. return (actual_compat &&
  3099. actual_compat <= CMake_VERSION_ENCODE(major, minor, patch));
  3100. }
  3101. //----------------------------------------------------------------------------
  3102. bool cmLocalGenerator::CheckDefinition(std::string const& define) const
  3103. {
  3104. // Many compilers do not support -DNAME(arg)=sdf so we disable it.
  3105. bool function_style = false;
  3106. for(const char* c = define.c_str(); *c && *c != '='; ++c)
  3107. {
  3108. if(*c == '(')
  3109. {
  3110. function_style = true;
  3111. break;
  3112. }
  3113. }
  3114. if(function_style)
  3115. {
  3116. cmOStringStream e;
  3117. e << "WARNING: Function-style preprocessor definitions may not be "
  3118. << "passed on the compiler command line because many compilers "
  3119. << "do not support it.\n"
  3120. << "CMake is dropping a preprocessor definition: " << define << "\n"
  3121. << "Consider defining the macro in a (configured) header file.\n";
  3122. cmSystemTools::Message(e.str().c_str());
  3123. return false;
  3124. }
  3125. // Many compilers do not support # in the value so we disable it.
  3126. if(define.find_first_of("#") != define.npos)
  3127. {
  3128. cmOStringStream e;
  3129. e << "WARNING: Preprocessor definitions containing '#' may not be "
  3130. << "passed on the compiler command line because many compilers "
  3131. << "do not support it.\n"
  3132. << "CMake is dropping a preprocessor definition: " << define << "\n"
  3133. << "Consider defining the macro in a (configured) header file.\n";
  3134. cmSystemTools::Message(e.str().c_str());
  3135. return false;
  3136. }
  3137. // Assume it is supported.
  3138. return true;
  3139. }
  3140. //----------------------------------------------------------------------------
  3141. static void cmLGInfoProp(cmMakefile* mf, cmTarget* target, const char* prop)
  3142. {
  3143. if(const char* val = target->GetProperty(prop))
  3144. {
  3145. mf->AddDefinition(prop, val);
  3146. }
  3147. }
  3148. //----------------------------------------------------------------------------
  3149. void cmLocalGenerator::GenerateAppleInfoPList(cmTarget* target,
  3150. const char* targetName,
  3151. const char* fname)
  3152. {
  3153. // Find the Info.plist template.
  3154. const char* in = target->GetProperty("MACOSX_BUNDLE_INFO_PLIST");
  3155. std::string inFile = (in && *in)? in : "MacOSXBundleInfo.plist.in";
  3156. if(!cmSystemTools::FileIsFullPath(inFile.c_str()))
  3157. {
  3158. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  3159. if(!inMod.empty())
  3160. {
  3161. inFile = inMod;
  3162. }
  3163. }
  3164. if(!cmSystemTools::FileExists(inFile.c_str(), true))
  3165. {
  3166. cmOStringStream e;
  3167. e << "Target " << target->GetName() << " Info.plist template \""
  3168. << inFile << "\" could not be found.";
  3169. cmSystemTools::Error(e.str().c_str());
  3170. return;
  3171. }
  3172. // Convert target properties to variables in an isolated makefile
  3173. // scope to configure the file. If properties are set they will
  3174. // override user make variables. If not the configuration will fall
  3175. // back to the directory-level values set by the user.
  3176. cmMakefile* mf = this->Makefile;
  3177. mf->PushScope();
  3178. mf->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName);
  3179. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_INFO_STRING");
  3180. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_ICON_FILE");
  3181. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_GUI_IDENTIFIER");
  3182. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_LONG_VERSION_STRING");
  3183. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_NAME");
  3184. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
  3185. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_VERSION");
  3186. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_COPYRIGHT");
  3187. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  3188. mf->PopScope();
  3189. }
  3190. //----------------------------------------------------------------------------
  3191. void cmLocalGenerator::GenerateFrameworkInfoPList(cmTarget* target,
  3192. const char* targetName,
  3193. const char* fname)
  3194. {
  3195. // Find the Info.plist template.
  3196. const char* in = target->GetProperty("MACOSX_FRAMEWORK_INFO_PLIST");
  3197. std::string inFile = (in && *in)? in : "MacOSXFrameworkInfo.plist.in";
  3198. if(!cmSystemTools::FileIsFullPath(inFile.c_str()))
  3199. {
  3200. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  3201. if(!inMod.empty())
  3202. {
  3203. inFile = inMod;
  3204. }
  3205. }
  3206. if(!cmSystemTools::FileExists(inFile.c_str(), true))
  3207. {
  3208. cmOStringStream e;
  3209. e << "Target " << target->GetName() << " Info.plist template \""
  3210. << inFile << "\" could not be found.";
  3211. cmSystemTools::Error(e.str().c_str());
  3212. return;
  3213. }
  3214. // Convert target properties to variables in an isolated makefile
  3215. // scope to configure the file. If properties are set they will
  3216. // override user make variables. If not the configuration will fall
  3217. // back to the directory-level values set by the user.
  3218. cmMakefile* mf = this->Makefile;
  3219. mf->PushScope();
  3220. mf->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName);
  3221. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_ICON_FILE");
  3222. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_IDENTIFIER");
  3223. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
  3224. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
  3225. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  3226. mf->PopScope();
  3227. }