cmLocalGenerator.cxx 107 KB

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