cmLocalGenerator.cxx 107 KB

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