cmLocalGenerator.cxx 112 KB

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