cmLocalVisualStudio6Generator.cxx 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  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 "cmGlobalGenerator.h"
  11. #include "cmLocalVisualStudio6Generator.h"
  12. #include "cmMakefile.h"
  13. #include "cmSystemTools.h"
  14. #include "cmSourceFile.h"
  15. #include "cmGeneratorTarget.h"
  16. #include "cmCustomCommandGenerator.h"
  17. #include "cmake.h"
  18. #include "cmComputeLinkInformation.h"
  19. #include <cmsys/RegularExpression.hxx>
  20. #include <cmsys/FStream.hxx>
  21. cmLocalVisualStudio6Generator
  22. ::cmLocalVisualStudio6Generator(cmGlobalGenerator* gg,
  23. cmLocalGenerator* parent):
  24. cmLocalVisualStudioGenerator(VS6, gg, parent)
  25. {
  26. }
  27. cmLocalVisualStudio6Generator::~cmLocalVisualStudio6Generator()
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. // Helper class to write build events.
  32. class cmLocalVisualStudio6Generator::EventWriter
  33. {
  34. public:
  35. EventWriter(cmLocalVisualStudio6Generator* lg,
  36. const std::string& config, std::string& code):
  37. LG(lg), Config(config), Code(code), First(true) {}
  38. void Start(const char* event)
  39. {
  40. this->First = true;
  41. this->Event = event;
  42. }
  43. void Finish()
  44. {
  45. this->Code += (this->First? "" : "\n");
  46. }
  47. void Write(std::vector<cmCustomCommand> const& ccs)
  48. {
  49. for(std::vector<cmCustomCommand>::const_iterator ci = ccs.begin();
  50. ci != ccs.end(); ++ci)
  51. {
  52. this->Write(*ci);
  53. }
  54. }
  55. void Write(cmCustomCommand const& cc)
  56. {
  57. cmCustomCommandGenerator ccg(cc, this->Config, this->LG->GetMakefile());
  58. if(this->First)
  59. {
  60. this->Code += this->Event + "_Cmds=";
  61. this->First = false;
  62. }
  63. else
  64. {
  65. this->Code += "\\\n\t";
  66. }
  67. this->Code += this->LG->ConstructScript(ccg, "\\\n\t");
  68. }
  69. private:
  70. cmLocalVisualStudio6Generator* LG;
  71. std::string Config;
  72. std::string& Code;
  73. bool First;
  74. std::string Event;
  75. };
  76. void cmLocalVisualStudio6Generator::AddHelperCommands()
  77. {
  78. std::set<std::string> lang;
  79. lang.insert("C");
  80. lang.insert("CXX");
  81. this->CreateCustomTargetsAndCommands(lang);
  82. }
  83. void cmLocalVisualStudio6Generator::AddCMakeListsRules()
  84. {
  85. cmTargets &tgts = this->Makefile->GetTargets();
  86. for(cmTargets::iterator l = tgts.begin();
  87. l != tgts.end(); l++)
  88. {
  89. if (l->second.GetType() == cmTarget::INTERFACE_LIBRARY)
  90. {
  91. continue;
  92. }
  93. // Add a rule to regenerate the build system when the target
  94. // specification source changes.
  95. const char* suppRegenRule =
  96. this->Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  97. if (!cmSystemTools::IsOn(suppRegenRule))
  98. {
  99. this->AddDSPBuildRule(l->second);
  100. }
  101. }
  102. }
  103. void cmLocalVisualStudio6Generator::Generate()
  104. {
  105. this->OutputDSPFile();
  106. }
  107. void cmLocalVisualStudio6Generator::OutputDSPFile()
  108. {
  109. // If not an in source build, then create the output directory
  110. if(strcmp(this->Makefile->GetCurrentBinaryDirectory(),
  111. this->Makefile->GetHomeDirectory()) != 0)
  112. {
  113. if(!cmSystemTools::MakeDirectory
  114. (this->Makefile->GetCurrentBinaryDirectory()))
  115. {
  116. cmSystemTools::Error("Error creating directory ",
  117. this->Makefile->GetCurrentBinaryDirectory());
  118. }
  119. }
  120. // Create the DSP or set of DSP's for libraries and executables
  121. cmTargets &tgts = this->Makefile->GetTargets();
  122. // build any targets
  123. for(cmTargets::iterator l = tgts.begin();
  124. l != tgts.end(); l++)
  125. {
  126. switch(l->second.GetType())
  127. {
  128. case cmTarget::STATIC_LIBRARY:
  129. case cmTarget::OBJECT_LIBRARY:
  130. this->SetBuildType(STATIC_LIBRARY, l->first.c_str(), l->second);
  131. break;
  132. case cmTarget::SHARED_LIBRARY:
  133. case cmTarget::MODULE_LIBRARY:
  134. this->SetBuildType(DLL, l->first.c_str(), l->second);
  135. break;
  136. case cmTarget::EXECUTABLE:
  137. this->SetBuildType(EXECUTABLE,l->first.c_str(), l->second);
  138. break;
  139. case cmTarget::UTILITY:
  140. case cmTarget::GLOBAL_TARGET:
  141. this->SetBuildType(UTILITY, l->first.c_str(), l->second);
  142. break;
  143. case cmTarget::INTERFACE_LIBRARY:
  144. continue;
  145. default:
  146. cmSystemTools::Error("Bad target type: ", l->first.c_str());
  147. break;
  148. }
  149. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  150. // so don't build a projectfile for it
  151. const char* path =
  152. l->second.GetProperty("EXTERNAL_MSPROJECT");
  153. if(!path)
  154. {
  155. // check to see if the dsp is going into a sub-directory
  156. std::string::size_type pos = l->first.rfind('/');
  157. if(pos != std::string::npos)
  158. {
  159. std::string dir = this->Makefile->GetCurrentBinaryDirectory();
  160. dir += "/";
  161. dir += l->first.substr(0, pos);
  162. if(!cmSystemTools::MakeDirectory(dir.c_str()))
  163. {
  164. cmSystemTools::Error("Error creating directory: ", dir.c_str());
  165. }
  166. }
  167. this->CreateSingleDSP(l->first.c_str(),l->second);
  168. }
  169. }
  170. }
  171. // Utility function to make a valid VS6 *.dsp filename out
  172. // of a CMake target name:
  173. //
  174. extern std::string GetVS6TargetName(const std::string& targetName);
  175. void cmLocalVisualStudio6Generator::CreateSingleDSP(const std::string& lname,
  176. cmTarget &target)
  177. {
  178. // add to the list of projects
  179. std::string pname = GetVS6TargetName(lname);
  180. // create the dsp.cmake file
  181. std::string fname;
  182. fname = this->Makefile->GetCurrentBinaryDirectory();
  183. fname += "/";
  184. fname += pname;
  185. fname += ".dsp";
  186. // save the name of the real dsp file
  187. std::string realDSP = fname;
  188. fname += ".cmake";
  189. cmsys::ofstream fout(fname.c_str());
  190. if(!fout)
  191. {
  192. cmSystemTools::Error("Error Writing ", fname.c_str());
  193. cmSystemTools::ReportLastSystemError("");
  194. }
  195. this->WriteDSPFile(fout,pname.c_str(),target);
  196. fout.close();
  197. // if the dsp file has changed, then write it.
  198. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  199. }
  200. void cmLocalVisualStudio6Generator::AddDSPBuildRule(cmTarget& tgt)
  201. {
  202. std::string dspname = GetVS6TargetName(tgt.GetName());
  203. dspname += ".dsp.cmake";
  204. const char* dsprule =
  205. this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  206. cmCustomCommandLine commandLine;
  207. commandLine.push_back(dsprule);
  208. std::string makefileIn = this->Makefile->GetCurrentSourceDirectory();
  209. makefileIn += "/";
  210. makefileIn += "CMakeLists.txt";
  211. if(!cmSystemTools::FileExists(makefileIn.c_str()))
  212. {
  213. return;
  214. }
  215. std::string comment = "Building Custom Rule ";
  216. comment += makefileIn;
  217. std::string args;
  218. args = "-H";
  219. args += this->Convert(this->Makefile->GetHomeDirectory(),
  220. START_OUTPUT, UNCHANGED, true);
  221. commandLine.push_back(args);
  222. args = "-B";
  223. args +=
  224. this->Convert(this->Makefile->GetHomeOutputDirectory(),
  225. START_OUTPUT, UNCHANGED, true);
  226. commandLine.push_back(args);
  227. std::vector<std::string> const& listFiles = this->Makefile->GetListFiles();
  228. cmCustomCommandLines commandLines;
  229. commandLines.push_back(commandLine);
  230. const char* no_working_directory = 0;
  231. this->Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles,
  232. makefileIn.c_str(), commandLines,
  233. comment.c_str(),
  234. no_working_directory, true);
  235. if(this->Makefile->GetSource(makefileIn.c_str()))
  236. {
  237. tgt.AddSource(makefileIn);
  238. }
  239. else
  240. {
  241. cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
  242. }
  243. }
  244. void cmLocalVisualStudio6Generator::WriteDSPFile(std::ostream& fout,
  245. const std::string& libName,
  246. cmTarget &target)
  247. {
  248. // For utility targets need custom command since pre- and post-
  249. // build does not do anything in Visual Studio 6. In order for the
  250. // rules to run in the correct order as custom commands, we need
  251. // special care for dependencies. The first rule must depend on all
  252. // the dependencies of all the rules. The later rules must each
  253. // depend only on the previous rule.
  254. if ((target.GetType() == cmTarget::UTILITY ||
  255. target.GetType() == cmTarget::GLOBAL_TARGET) &&
  256. (!target.GetPreBuildCommands().empty() ||
  257. !target.GetPostBuildCommands().empty()))
  258. {
  259. // Accumulate the dependencies of all the commands.
  260. std::vector<std::string> depends;
  261. for (std::vector<cmCustomCommand>::const_iterator cr =
  262. target.GetPreBuildCommands().begin();
  263. cr != target.GetPreBuildCommands().end(); ++cr)
  264. {
  265. depends.insert(depends.end(),
  266. cr->GetDepends().begin(), cr->GetDepends().end());
  267. }
  268. for (std::vector<cmCustomCommand>::const_iterator cr =
  269. target.GetPostBuildCommands().begin();
  270. cr != target.GetPostBuildCommands().end(); ++cr)
  271. {
  272. depends.insert(depends.end(),
  273. cr->GetDepends().begin(), cr->GetDepends().end());
  274. }
  275. // Add the pre- and post-build commands in order.
  276. int count = 1;
  277. for (std::vector<cmCustomCommand>::const_iterator cr =
  278. target.GetPreBuildCommands().begin();
  279. cr != target.GetPreBuildCommands().end(); ++cr)
  280. {
  281. this->AddUtilityCommandHack(target, count++, depends, *cr);
  282. }
  283. for (std::vector<cmCustomCommand>::const_iterator cr =
  284. target.GetPostBuildCommands().begin();
  285. cr != target.GetPostBuildCommands().end(); ++cr)
  286. {
  287. this->AddUtilityCommandHack(target, count++, depends, *cr);
  288. }
  289. }
  290. // We may be modifying the source groups temporarily, so make a copy.
  291. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  292. // get the classes from the source lists then add them to the groups
  293. std::vector<cmSourceFile*> classes;
  294. if (!target.GetConfigCommonSourceFiles(classes))
  295. {
  296. return;
  297. }
  298. // now all of the source files have been properly assigned to the target
  299. // now stick them into source groups using the reg expressions
  300. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  301. i != classes.end(); i++)
  302. {
  303. if (!(*i)->GetObjectLibrary().empty())
  304. {
  305. continue;
  306. }
  307. // Add the file to the list of sources.
  308. std::string source = (*i)->GetFullPath();
  309. cmSourceGroup* sourceGroup =
  310. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  311. sourceGroup->AssignSource(*i);
  312. // while we are at it, if it is a .rule file then for visual studio 6 we
  313. // must generate it
  314. if ((*i)->GetPropertyAsBool("__CMAKE_RULE"))
  315. {
  316. if(!cmSystemTools::FileExists(source.c_str()))
  317. {
  318. cmSystemTools::ReplaceString(source, "$(IntDir)/", "");
  319. // Make sure the path exists for the file
  320. std::string path = cmSystemTools::GetFilenamePath(source);
  321. cmSystemTools::MakeDirectory(path.c_str());
  322. #if defined(_WIN32) || defined(__CYGWIN__)
  323. cmsys::ofstream sourceFout(source.c_str(),
  324. std::ios::binary | std::ios::out
  325. | std::ios::trunc);
  326. #else
  327. cmsys::ofstream sourceFout(source.c_str(),
  328. std::ios::out | std::ios::trunc);
  329. #endif
  330. if(sourceFout)
  331. {
  332. sourceFout.write("# generated from CMake",22);
  333. sourceFout.flush();
  334. sourceFout.close();
  335. }
  336. }
  337. }
  338. }
  339. // Write the DSP file's header.
  340. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  341. // Loop through every source group.
  342. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  343. sg != sourceGroups.end(); ++sg)
  344. {
  345. this->WriteGroup(&(*sg), target, fout, libName);
  346. }
  347. // Write the DSP file's footer.
  348. this->WriteDSPFooter(fout);
  349. }
  350. void cmLocalVisualStudio6Generator
  351. ::WriteGroup(const cmSourceGroup *sg, cmTarget& target,
  352. std::ostream &fout, const std::string& libName)
  353. {
  354. cmGeneratorTarget* gt =
  355. this->GlobalGenerator->GetGeneratorTarget(&target);
  356. const std::vector<const cmSourceFile *> &sourceFiles =
  357. sg->GetSourceFiles();
  358. // If the group is empty, don't write it at all.
  359. if(sourceFiles.empty() && sg->GetGroupChildren().empty())
  360. {
  361. return;
  362. }
  363. // If the group has a name, write the header.
  364. std::string name = sg->GetName();
  365. if(name != "")
  366. {
  367. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  368. }
  369. // Loop through each source in the source group.
  370. for(std::vector<const cmSourceFile *>::const_iterator sf =
  371. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  372. {
  373. if (!(*sf)->GetObjectLibrary().empty())
  374. {
  375. continue;
  376. }
  377. std::string source = (*sf)->GetFullPath();
  378. const cmCustomCommand *command =
  379. (*sf)->GetCustomCommand();
  380. std::string compileFlags;
  381. std::vector<std::string> depends;
  382. std::string objectNameDir;
  383. if(gt->HasExplicitObjectName(*sf))
  384. {
  385. objectNameDir = cmSystemTools::GetFilenamePath(gt->GetObjectName(*sf));
  386. }
  387. // Add per-source file flags.
  388. if(const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS"))
  389. {
  390. compileFlags += cflags;
  391. }
  392. const std::string& lang = this->GetSourceFileLanguage(*(*sf));
  393. if(lang == "CXX")
  394. {
  395. // force a C++ file type
  396. compileFlags += " /TP ";
  397. }
  398. else if(lang == "C")
  399. {
  400. // force to c file type
  401. compileFlags += " /TC ";
  402. }
  403. // Add per-source and per-configuration preprocessor definitions.
  404. std::map<std::string, std::string> cdmap;
  405. {
  406. std::set<std::string> targetCompileDefinitions;
  407. this->AppendDefines(targetCompileDefinitions,
  408. (*sf)->GetProperty("COMPILE_DEFINITIONS"));
  409. this->JoinDefines(targetCompileDefinitions, compileFlags, lang);
  410. }
  411. if(const char* cdefs = (*sf)->GetProperty("COMPILE_DEFINITIONS_DEBUG"))
  412. {
  413. std::set<std::string> debugCompileDefinitions;
  414. this->AppendDefines(debugCompileDefinitions, cdefs);
  415. this->JoinDefines(debugCompileDefinitions, cdmap["DEBUG"], lang);
  416. }
  417. if(const char* cdefs = (*sf)->GetProperty("COMPILE_DEFINITIONS_RELEASE"))
  418. {
  419. std::set<std::string> releaseCompileDefinitions;
  420. this->AppendDefines(releaseCompileDefinitions, cdefs);
  421. this->JoinDefines(releaseCompileDefinitions, cdmap["RELEASE"], lang);
  422. }
  423. if(const char* cdefs =
  424. (*sf)->GetProperty("COMPILE_DEFINITIONS_MINSIZEREL"))
  425. {
  426. std::set<std::string> minsizerelCompileDefinitions;
  427. this->AppendDefines(minsizerelCompileDefinitions, cdefs);
  428. this->JoinDefines(minsizerelCompileDefinitions, cdmap["MINSIZEREL"],
  429. lang);
  430. }
  431. if(const char* cdefs =
  432. (*sf)->GetProperty("COMPILE_DEFINITIONS_RELWITHDEBINFO"))
  433. {
  434. std::set<std::string> relwithdebinfoCompileDefinitions;
  435. this->AppendDefines(relwithdebinfoCompileDefinitions, cdefs);
  436. this->JoinDefines(relwithdebinfoCompileDefinitions,
  437. cdmap["RELWITHDEBINFO"], lang);
  438. }
  439. bool excludedFromBuild =
  440. (!lang.empty() && (*sf)->GetPropertyAsBool("HEADER_FILE_ONLY"));
  441. // Check for extra object-file dependencies.
  442. const char* dependsValue = (*sf)->GetProperty("OBJECT_DEPENDS");
  443. if(dependsValue)
  444. {
  445. cmSystemTools::ExpandListArgument(dependsValue, depends);
  446. }
  447. if (GetVS6TargetName(source) != libName ||
  448. target.GetType() == cmTarget::UTILITY ||
  449. target.GetType() == cmTarget::GLOBAL_TARGET)
  450. {
  451. fout << "# Begin Source File\n\n";
  452. // Tell MS-Dev what the source is. If the compiler knows how to
  453. // build it, then it will.
  454. fout << "SOURCE=" <<
  455. this->ConvertToOptionallyRelativeOutputPath(source.c_str()) << "\n\n";
  456. if(!depends.empty())
  457. {
  458. // Write out the dependencies for the rule.
  459. fout << "USERDEP__HACK=";
  460. for(std::vector<std::string>::const_iterator d = depends.begin();
  461. d != depends.end(); ++d)
  462. {
  463. fout << "\\\n\t" <<
  464. this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  465. }
  466. fout << "\n";
  467. }
  468. if (command)
  469. {
  470. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  471. this->WriteCustomRule(fout, source.c_str(), *command, flags);
  472. }
  473. else if(!compileFlags.empty() || !objectNameDir.empty() ||
  474. excludedFromBuild || !cdmap.empty())
  475. {
  476. for(std::vector<std::string>::iterator i
  477. = this->Configurations.begin();
  478. i != this->Configurations.end(); ++i)
  479. {
  480. // Strip the subdirectory name out of the configuration name.
  481. std::string config = this->GetConfigName(*i);
  482. if (i == this->Configurations.begin())
  483. {
  484. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  485. }
  486. else
  487. {
  488. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  489. }
  490. if(excludedFromBuild)
  491. {
  492. fout << "# PROP Exclude_From_Build 1\n";
  493. }
  494. if(!compileFlags.empty())
  495. {
  496. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  497. }
  498. std::map<std::string, std::string>::iterator cdi =
  499. cdmap.find(cmSystemTools::UpperCase(config));
  500. if(cdi != cdmap.end() && !cdi->second.empty())
  501. {
  502. fout << "\n# ADD CPP " << cdi->second << "\n\n";
  503. }
  504. if(!objectNameDir.empty())
  505. {
  506. // Setup an alternate object file directory.
  507. fout << "\n# PROP Intermediate_Dir \""
  508. << config << "/" << objectNameDir << "\"\n\n";
  509. }
  510. }
  511. fout << "!ENDIF\n\n";
  512. }
  513. fout << "# End Source File\n";
  514. }
  515. }
  516. std::vector<cmSourceGroup> const& children = sg->GetGroupChildren();
  517. for(unsigned int i=0;i<children.size();++i)
  518. {
  519. this->WriteGroup(&children[i], target, fout, libName);
  520. }
  521. // If the group has a name, write the footer.
  522. if(name != "")
  523. {
  524. this->WriteDSPEndGroup(fout);
  525. }
  526. }
  527. void
  528. cmLocalVisualStudio6Generator
  529. ::AddUtilityCommandHack(cmTarget& target, int count,
  530. std::vector<std::string>& depends,
  531. const cmCustomCommand& origCommand)
  532. {
  533. // Create a fake output that forces the rule to run.
  534. char* output = new char[(strlen(this->Makefile->GetCurrentBinaryDirectory())
  535. + target.GetName().size() + 30)];
  536. sprintf(output,"%s/%s_force_%i", this->Makefile->GetCurrentBinaryDirectory(),
  537. target.GetName().c_str(), count);
  538. const char* comment = origCommand.GetComment();
  539. if(!comment && origCommand.GetOutputs().empty())
  540. {
  541. comment = "<hack>";
  542. }
  543. // Add the rule with the given dependencies and commands.
  544. std::string no_main_dependency = "";
  545. if(cmSourceFile* outsf =
  546. this->Makefile->AddCustomCommandToOutput(
  547. output, depends, no_main_dependency,
  548. origCommand.GetCommandLines(), comment,
  549. origCommand.GetWorkingDirectory().c_str()))
  550. {
  551. target.AddSource(outsf->GetFullPath());
  552. }
  553. // Replace the dependencies with the output of this rule so that the
  554. // next rule added will run after this one.
  555. depends.clear();
  556. depends.push_back(output);
  557. // Free the fake output name.
  558. delete [] output;
  559. }
  560. void
  561. cmLocalVisualStudio6Generator
  562. ::WriteCustomRule(std::ostream& fout,
  563. const char* source,
  564. const cmCustomCommand& command,
  565. const char* flags)
  566. {
  567. // Write the rule for each configuration.
  568. std::vector<std::string>::iterator i;
  569. for(i = this->Configurations.begin(); i != this->Configurations.end(); ++i)
  570. {
  571. std::string config = this->GetConfigName(*i);
  572. cmCustomCommandGenerator ccg(command, config, this->Makefile);
  573. std::string comment =
  574. this->ConstructComment(ccg, "Building Custom Rule $(InputPath)");
  575. if(comment == "<hack>")
  576. {
  577. comment = "";
  578. }
  579. std::string script =
  580. this->ConstructScript(ccg, "\\\n\t");
  581. if (i == this->Configurations.begin())
  582. {
  583. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  584. }
  585. else
  586. {
  587. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  588. }
  589. if(flags)
  590. {
  591. fout << "\n# ADD CPP " << flags << "\n\n";
  592. }
  593. // Write out the dependencies for the rule.
  594. fout << "USERDEP__HACK=";
  595. for(std::vector<std::string>::const_iterator d =
  596. ccg.GetDepends().begin();
  597. d != ccg.GetDepends().end();
  598. ++d)
  599. {
  600. // Lookup the real name of the dependency in case it is a CMake target.
  601. std::string dep;
  602. if(this->GetRealDependency(d->c_str(), config.c_str(), dep))
  603. {
  604. fout << "\\\n\t" <<
  605. this->ConvertToOptionallyRelativeOutputPath(dep.c_str());
  606. }
  607. }
  608. fout << "\n";
  609. fout << "# PROP Ignore_Default_Tool 1\n";
  610. fout << "# Begin Custom Build -";
  611. if(!comment.empty())
  612. {
  613. fout << " " << comment.c_str();
  614. }
  615. fout << "\n\n";
  616. if(ccg.GetOutputs().empty())
  617. {
  618. fout << source
  619. << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  620. fout << script.c_str() << "\n\n";
  621. }
  622. else
  623. {
  624. for(std::vector<std::string>::const_iterator o =
  625. ccg.GetOutputs().begin();
  626. o != ccg.GetOutputs().end();
  627. ++o)
  628. {
  629. // Write a rule for every output generated by this command.
  630. fout << this->ConvertToOptionallyRelativeOutputPath(o->c_str())
  631. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  632. fout << script.c_str() << "\n\n";
  633. }
  634. }
  635. fout << "# End Custom Build\n\n";
  636. }
  637. fout << "!ENDIF\n\n";
  638. }
  639. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  640. const char* group,
  641. const char* filter)
  642. {
  643. fout << "# Begin Group \"" << group << "\"\n"
  644. "# PROP Default_Filter \"" << filter << "\"\n";
  645. }
  646. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  647. {
  648. fout << "# End Group\n";
  649. }
  650. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b,
  651. const std::string& libName,
  652. cmTarget& target)
  653. {
  654. std::string root= this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  655. const char *def=
  656. this->Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  657. if( def)
  658. {
  659. root = def;
  660. }
  661. else
  662. {
  663. root += "/Templates";
  664. }
  665. switch(b)
  666. {
  667. case WIN32_EXECUTABLE:
  668. break;
  669. case STATIC_LIBRARY:
  670. this->DSPHeaderTemplate = root;
  671. this->DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  672. this->DSPFooterTemplate = root;
  673. this->DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  674. break;
  675. case DLL:
  676. this->DSPHeaderTemplate = root;
  677. this->DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  678. this->DSPFooterTemplate = root;
  679. this->DSPFooterTemplate += "/DLLFooter.dsptemplate";
  680. break;
  681. case EXECUTABLE:
  682. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  683. {
  684. this->DSPHeaderTemplate = root;
  685. this->DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  686. this->DSPFooterTemplate = root;
  687. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  688. }
  689. else
  690. {
  691. this->DSPHeaderTemplate = root;
  692. this->DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  693. this->DSPFooterTemplate = root;
  694. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  695. }
  696. break;
  697. case UTILITY:
  698. this->DSPHeaderTemplate = root;
  699. this->DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  700. this->DSPFooterTemplate = root;
  701. this->DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  702. break;
  703. }
  704. // once the build type is set, determine what configurations are
  705. // possible
  706. cmsys::ifstream fin(this->DSPHeaderTemplate.c_str());
  707. cmsys::RegularExpression reg("# Name ");
  708. if(!fin)
  709. {
  710. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  711. }
  712. // reset this->Configurations
  713. this->Configurations.erase(this->Configurations.begin(),
  714. this->Configurations.end());
  715. // now add all the configurations possible
  716. std::string vs6name = GetVS6TargetName(libName);
  717. std::string line;
  718. while(cmSystemTools::GetLineFromStream(fin, line))
  719. {
  720. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME", vs6name.c_str());
  721. if (reg.find(line))
  722. {
  723. this->Configurations.push_back(line.substr(reg.end()));
  724. }
  725. }
  726. }
  727. //----------------------------------------------------------------------------
  728. cmsys::auto_ptr<cmCustomCommand>
  729. cmLocalVisualStudio6Generator::MaybeCreateOutputDir(cmTarget& target,
  730. const std::string& config)
  731. {
  732. cmsys::auto_ptr<cmCustomCommand> pcc;
  733. // VS6 forgets to create the output directory for archives if it
  734. // differs from the intermediate directory.
  735. if(target.GetType() != cmTarget::STATIC_LIBRARY) { return pcc; }
  736. std::string outDir = target.GetDirectory(config, false);
  737. // Add a pre-link event to create the directory.
  738. cmCustomCommandLine command;
  739. command.push_back(this->Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  740. command.push_back("-E");
  741. command.push_back("make_directory");
  742. command.push_back(outDir);
  743. std::vector<std::string> no_output;
  744. std::vector<std::string> no_byproducts;
  745. std::vector<std::string> no_depends;
  746. cmCustomCommandLines commands;
  747. commands.push_back(command);
  748. pcc.reset(new cmCustomCommand(0, no_output, no_byproducts,
  749. no_depends, commands, 0, 0));
  750. pcc->SetEscapeOldStyle(false);
  751. pcc->SetEscapeAllowMakeVars(true);
  752. return pcc;
  753. }
  754. // look for custom rules on a target and collect them together
  755. std::string
  756. cmLocalVisualStudio6Generator::CreateTargetRules(cmTarget &target,
  757. const std::string& configName,
  758. const std::string& /* libName */)
  759. {
  760. if (target.GetType() >= cmTarget::UTILITY )
  761. {
  762. return "";
  763. }
  764. std::string customRuleCode = "# Begin Special Build Tool\n";
  765. EventWriter event(this, configName, customRuleCode);
  766. // Write the pre-build and pre-link together (VS6 does not support both).
  767. event.Start("PreLink");
  768. event.Write(target.GetPreBuildCommands());
  769. event.Write(target.GetPreLinkCommands());
  770. cmsys::auto_ptr<cmCustomCommand> pcc(
  771. this->MaybeCreateImplibDir(target, configName, false));
  772. if(pcc.get())
  773. {
  774. event.Write(*pcc);
  775. }
  776. pcc = this->MaybeCreateOutputDir(target, configName);
  777. if(pcc.get())
  778. {
  779. event.Write(*pcc);
  780. }
  781. event.Finish();
  782. // Write the post-build rules.
  783. event.Start("PostBuild");
  784. event.Write(target.GetPostBuildCommands());
  785. event.Finish();
  786. customRuleCode += "# End Special Build Tool\n";
  787. return customRuleCode;
  788. }
  789. inline std::string removeQuotes(const std::string& s)
  790. {
  791. if(s[0] == '\"' && s[s.size()-1] == '\"')
  792. {
  793. return s.substr(1, s.size()-2);
  794. }
  795. return s;
  796. }
  797. std::string
  798. cmLocalVisualStudio6Generator::GetTargetIncludeOptions(cmTarget &target,
  799. const std::string& config)
  800. {
  801. std::string includeOptions;
  802. // Setup /I and /LIBPATH options for the resulting DSP file. VS 6
  803. // truncates long include paths so make it as short as possible if
  804. // the length threatens this problem.
  805. unsigned int maxIncludeLength = 3000;
  806. bool useShortPath = false;
  807. cmGeneratorTarget* gt =
  808. this->GlobalGenerator->GetGeneratorTarget(&target);
  809. for(int j=0; j < 2; ++j)
  810. {
  811. std::vector<std::string> includes;
  812. this->GetIncludeDirectories(includes, gt, "C", config);
  813. std::vector<std::string>::iterator i;
  814. for(i = includes.begin(); i != includes.end(); ++i)
  815. {
  816. std::string tmp =
  817. this->ConvertToOptionallyRelativeOutputPath(i->c_str());
  818. if(useShortPath)
  819. {
  820. cmSystemTools::GetShortPath(tmp.c_str(), tmp);
  821. }
  822. includeOptions += " /I ";
  823. // quote if not already quoted
  824. if (tmp[0] != '"')
  825. {
  826. includeOptions += "\"";
  827. includeOptions += tmp;
  828. includeOptions += "\"";
  829. }
  830. else
  831. {
  832. includeOptions += tmp;
  833. }
  834. }
  835. if(j == 0 && includeOptions.size() > maxIncludeLength)
  836. {
  837. includeOptions = "";
  838. useShortPath = true;
  839. }
  840. else
  841. {
  842. break;
  843. }
  844. }
  845. return includeOptions;
  846. }
  847. // Code in blocks surrounded by a test for this definition is needed
  848. // only for compatibility with user project's replacement DSP
  849. // templates. The CMake templates no longer use them.
  850. #define CM_USE_OLD_VS6
  851. void cmLocalVisualStudio6Generator
  852. ::WriteDSPHeader(std::ostream& fout,
  853. const std::string& libName, cmTarget &target,
  854. std::vector<cmSourceGroup> &)
  855. {
  856. bool targetBuilds = (target.GetType() >= cmTarget::EXECUTABLE &&
  857. target.GetType() <= cmTarget::MODULE_LIBRARY);
  858. #ifdef CM_USE_OLD_VS6
  859. // Lookup the library and executable output directories.
  860. std::string libPath;
  861. if(this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  862. {
  863. libPath = this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  864. }
  865. std::string exePath;
  866. if(this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  867. {
  868. exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  869. }
  870. // Make sure there are trailing slashes.
  871. if(!libPath.empty())
  872. {
  873. if(libPath[libPath.size()-1] != '/')
  874. {
  875. libPath += "/";
  876. }
  877. }
  878. if(!exePath.empty())
  879. {
  880. if(exePath[exePath.size()-1] != '/')
  881. {
  882. exePath += "/";
  883. }
  884. }
  885. std::set<std::string> pathEmitted;
  886. // determine the link directories
  887. std::string libOptions;
  888. std::string libDebugOptions;
  889. std::string libOptimizedOptions;
  890. std::string libMultiLineOptions;
  891. std::string libMultiLineOptionsForDebug;
  892. std::string libMultiLineDebugOptions;
  893. std::string libMultiLineOptimizedOptions;
  894. if(libPath.size())
  895. {
  896. std::string lpath =
  897. this->ConvertToOptionallyRelativeOutputPath(libPath.c_str());
  898. if(lpath.size() == 0)
  899. {
  900. lpath = ".";
  901. }
  902. std::string lpathIntDir = libPath + "$(INTDIR)";
  903. lpathIntDir =
  904. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  905. if(pathEmitted.insert(lpath).second)
  906. {
  907. libOptions += " /LIBPATH:";
  908. libOptions += lpathIntDir;
  909. libOptions += " ";
  910. libOptions += " /LIBPATH:";
  911. libOptions += lpath;
  912. libOptions += " ";
  913. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  914. libMultiLineOptions += lpathIntDir;
  915. libMultiLineOptions += " ";
  916. libMultiLineOptions += " /LIBPATH:";
  917. libMultiLineOptions += lpath;
  918. libMultiLineOptions += " \n";
  919. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  920. libMultiLineOptionsForDebug += lpathIntDir;
  921. libMultiLineOptionsForDebug += " ";
  922. libMultiLineOptionsForDebug += " /LIBPATH:";
  923. libMultiLineOptionsForDebug += lpath;
  924. libMultiLineOptionsForDebug += " \n";
  925. }
  926. }
  927. if(exePath.size())
  928. {
  929. std::string lpath =
  930. this->ConvertToOptionallyRelativeOutputPath(exePath.c_str());
  931. if(lpath.size() == 0)
  932. {
  933. lpath = ".";
  934. }
  935. std::string lpathIntDir = exePath + "$(INTDIR)";
  936. lpathIntDir =
  937. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  938. if(pathEmitted.insert(lpath).second)
  939. {
  940. libOptions += " /LIBPATH:";
  941. libOptions += lpathIntDir;
  942. libOptions += " ";
  943. libOptions += " /LIBPATH:";
  944. libOptions += lpath;
  945. libOptions += " ";
  946. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  947. libMultiLineOptions += lpathIntDir;
  948. libMultiLineOptions += " ";
  949. libMultiLineOptions += " /LIBPATH:";
  950. libMultiLineOptions += lpath;
  951. libMultiLineOptions += " \n";
  952. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  953. libMultiLineOptionsForDebug += lpathIntDir;
  954. libMultiLineOptionsForDebug += " ";
  955. libMultiLineOptionsForDebug += " /LIBPATH:";
  956. libMultiLineOptionsForDebug += lpath;
  957. libMultiLineOptionsForDebug += " \n";
  958. }
  959. }
  960. std::vector<std::string>::const_iterator i;
  961. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  962. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  963. {
  964. std::string path = *i;
  965. if(path[path.size()-1] != '/')
  966. {
  967. path += "/";
  968. }
  969. std::string lpath =
  970. this->ConvertToOptionallyRelativeOutputPath(path.c_str());
  971. if(lpath.size() == 0)
  972. {
  973. lpath = ".";
  974. }
  975. std::string lpathIntDir = path + "$(INTDIR)";
  976. lpathIntDir =
  977. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  978. if(pathEmitted.insert(lpath).second)
  979. {
  980. libOptions += " /LIBPATH:";
  981. libOptions += lpathIntDir;
  982. libOptions += " ";
  983. libOptions += " /LIBPATH:";
  984. libOptions += lpath;
  985. libOptions += " ";
  986. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  987. libMultiLineOptions += lpathIntDir;
  988. libMultiLineOptions += " ";
  989. libMultiLineOptions += " /LIBPATH:";
  990. libMultiLineOptions += lpath;
  991. libMultiLineOptions += " \n";
  992. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  993. libMultiLineOptionsForDebug += lpathIntDir;
  994. libMultiLineOptionsForDebug += " ";
  995. libMultiLineOptionsForDebug += " /LIBPATH:";
  996. libMultiLineOptionsForDebug += lpath;
  997. libMultiLineOptionsForDebug += " \n";
  998. }
  999. }
  1000. // find link libraries
  1001. const cmTarget::LinkLibraryVectorType& libs =
  1002. target.GetLinkLibrariesForVS6();
  1003. cmTarget::LinkLibraryVectorType::const_iterator j;
  1004. for(j = libs.begin(); j != libs.end(); ++j)
  1005. {
  1006. // add libraries to executables and dlls (but never include
  1007. // a library in a library, bad recursion)
  1008. // NEVER LINK STATIC LIBRARIES TO OTHER STATIC LIBRARIES
  1009. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  1010. && target.GetType() != cmTarget::STATIC_LIBRARY
  1011. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  1012. (target.GetType()==cmTarget::SHARED_LIBRARY
  1013. && libName != GetVS6TargetName(j->first)) ||
  1014. (target.GetType()==cmTarget::MODULE_LIBRARY
  1015. && libName != GetVS6TargetName(j->first)))
  1016. {
  1017. // Compute the proper name to use to link this library.
  1018. std::string lib;
  1019. std::string libDebug;
  1020. cmTarget* tgt = this->GlobalGenerator->FindTarget(j->first.c_str());
  1021. if(tgt)
  1022. {
  1023. lib = cmSystemTools::GetFilenameWithoutExtension
  1024. (tgt->GetFullName().c_str());
  1025. libDebug = cmSystemTools::GetFilenameWithoutExtension
  1026. (tgt->GetFullName("Debug").c_str());
  1027. lib += ".lib";
  1028. libDebug += ".lib";
  1029. }
  1030. else
  1031. {
  1032. lib = j->first.c_str();
  1033. libDebug = j->first.c_str();
  1034. if(j->first.find(".lib") == std::string::npos)
  1035. {
  1036. lib += ".lib";
  1037. libDebug += ".lib";
  1038. }
  1039. }
  1040. lib = this->ConvertToOptionallyRelativeOutputPath(lib.c_str());
  1041. libDebug =
  1042. this->ConvertToOptionallyRelativeOutputPath(libDebug.c_str());
  1043. if (j->second == cmTarget::GENERAL)
  1044. {
  1045. libOptions += " ";
  1046. libOptions += lib;
  1047. libMultiLineOptions += "# ADD LINK32 ";
  1048. libMultiLineOptions += lib;
  1049. libMultiLineOptions += "\n";
  1050. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1051. libMultiLineOptionsForDebug += libDebug;
  1052. libMultiLineOptionsForDebug += "\n";
  1053. }
  1054. if (j->second == cmTarget::DEBUG)
  1055. {
  1056. libDebugOptions += " ";
  1057. libDebugOptions += lib;
  1058. libMultiLineDebugOptions += "# ADD LINK32 ";
  1059. libMultiLineDebugOptions += libDebug;
  1060. libMultiLineDebugOptions += "\n";
  1061. }
  1062. if (j->second == cmTarget::OPTIMIZED)
  1063. {
  1064. libOptimizedOptions += " ";
  1065. libOptimizedOptions += lib;
  1066. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  1067. libMultiLineOptimizedOptions += lib;
  1068. libMultiLineOptimizedOptions += "\n";
  1069. }
  1070. }
  1071. }
  1072. #endif
  1073. // Get include options for this target.
  1074. std::string includeOptionsDebug = this->GetTargetIncludeOptions(target,
  1075. "DEBUG");
  1076. std::string includeOptionsRelease = this->GetTargetIncludeOptions(target,
  1077. "RELEASE");
  1078. std::string includeOptionsRelWithDebInfo = this->GetTargetIncludeOptions(
  1079. target,
  1080. "RELWITHDEBINFO");
  1081. std::string includeOptionsMinSizeRel = this->GetTargetIncludeOptions(target,
  1082. "MINSIZEREL");
  1083. // Get extra linker options for this target type.
  1084. std::string extraLinkOptions;
  1085. std::string extraLinkOptionsDebug;
  1086. std::string extraLinkOptionsRelease;
  1087. std::string extraLinkOptionsMinSizeRel;
  1088. std::string extraLinkOptionsRelWithDebInfo;
  1089. if(target.GetType() == cmTarget::EXECUTABLE)
  1090. {
  1091. extraLinkOptions = this->Makefile->
  1092. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  1093. extraLinkOptionsDebug = this->Makefile->
  1094. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_DEBUG");
  1095. extraLinkOptionsRelease = this->Makefile->
  1096. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_RELEASE");
  1097. extraLinkOptionsMinSizeRel = this->Makefile->
  1098. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_MINSIZEREL");
  1099. extraLinkOptionsRelWithDebInfo = this->Makefile->
  1100. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO");
  1101. }
  1102. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1103. {
  1104. extraLinkOptions = this->Makefile->
  1105. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1106. extraLinkOptionsDebug = this->Makefile->
  1107. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_DEBUG");
  1108. extraLinkOptionsRelease = this->Makefile->
  1109. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_RELEASE");
  1110. extraLinkOptionsMinSizeRel = this->Makefile->
  1111. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL");
  1112. extraLinkOptionsRelWithDebInfo = this->Makefile->
  1113. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO");
  1114. }
  1115. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1116. {
  1117. extraLinkOptions = this->Makefile->
  1118. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1119. extraLinkOptionsDebug = this->Makefile->
  1120. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_DEBUG");
  1121. extraLinkOptionsRelease = this->Makefile->
  1122. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_RELEASE");
  1123. extraLinkOptionsMinSizeRel = this->Makefile->
  1124. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL");
  1125. extraLinkOptionsRelWithDebInfo = this->Makefile->
  1126. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO");
  1127. }
  1128. // Get extra linker options for this target.
  1129. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  1130. {
  1131. extraLinkOptions += " ";
  1132. extraLinkOptions += targetLinkFlags;
  1133. }
  1134. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_DEBUG"))
  1135. {
  1136. extraLinkOptionsDebug += " ";
  1137. extraLinkOptionsDebug += targetLinkFlags;
  1138. }
  1139. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_RELEASE"))
  1140. {
  1141. extraLinkOptionsRelease += " ";
  1142. extraLinkOptionsRelease += targetLinkFlags;
  1143. }
  1144. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_MINSIZEREL"))
  1145. {
  1146. extraLinkOptionsMinSizeRel += " ";
  1147. extraLinkOptionsMinSizeRel += targetLinkFlags;
  1148. }
  1149. if(const char* targetLinkFlags =
  1150. target.GetProperty("LINK_FLAGS_RELWITHDEBINFO"))
  1151. {
  1152. extraLinkOptionsRelWithDebInfo += " ";
  1153. extraLinkOptionsRelWithDebInfo += targetLinkFlags;
  1154. }
  1155. // Get standard libraries for this language.
  1156. if(targetBuilds)
  1157. {
  1158. // Get the language to use for linking.
  1159. std::vector<std::string> configs;
  1160. target.GetMakefile()->GetConfigurations(configs);
  1161. std::vector<std::string>::const_iterator it = configs.begin();
  1162. const std::string& linkLanguage = target.GetLinkerLanguage(*it);
  1163. for ( ; it != configs.end(); ++it)
  1164. {
  1165. const std::string& configLinkLanguage = target.GetLinkerLanguage(*it);
  1166. if (configLinkLanguage != linkLanguage)
  1167. {
  1168. cmSystemTools::Error
  1169. ("Linker language must not vary by configuration for target: ",
  1170. target.GetName().c_str());
  1171. }
  1172. }
  1173. if(linkLanguage.empty())
  1174. {
  1175. cmSystemTools::Error
  1176. ("CMake can not determine linker language for target: ",
  1177. target.GetName().c_str());
  1178. return;
  1179. }
  1180. // Compute the variable name to lookup standard libraries for this
  1181. // language.
  1182. std::string standardLibsVar = "CMAKE_";
  1183. standardLibsVar += linkLanguage;
  1184. standardLibsVar += "_STANDARD_LIBRARIES";
  1185. // Add standard libraries.
  1186. if(const char* stdLibs =
  1187. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1188. {
  1189. extraLinkOptions += " ";
  1190. extraLinkOptions += stdLibs;
  1191. }
  1192. }
  1193. // Compute version number information.
  1194. std::string targetVersionFlag;
  1195. if(target.GetType() == cmTarget::EXECUTABLE ||
  1196. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1197. target.GetType() == cmTarget::MODULE_LIBRARY)
  1198. {
  1199. int major;
  1200. int minor;
  1201. target.GetTargetVersion(major, minor);
  1202. std::ostringstream targetVersionStream;
  1203. targetVersionStream << "/version:" << major << "." << minor;
  1204. targetVersionFlag = targetVersionStream.str();
  1205. }
  1206. // Compute the real name of the target.
  1207. std::string outputName =
  1208. "(OUTPUT_NAME is for libraries and executables only)";
  1209. std::string outputNameDebug = outputName;
  1210. std::string outputNameRelease = outputName;
  1211. std::string outputNameMinSizeRel = outputName;
  1212. std::string outputNameRelWithDebInfo = outputName;
  1213. if(target.GetType() == cmTarget::EXECUTABLE ||
  1214. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1215. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1216. target.GetType() == cmTarget::MODULE_LIBRARY)
  1217. {
  1218. outputName = target.GetFullName();
  1219. outputNameDebug = target.GetFullName("Debug");
  1220. outputNameRelease = target.GetFullName("Release");
  1221. outputNameMinSizeRel = target.GetFullName("MinSizeRel");
  1222. outputNameRelWithDebInfo = target.GetFullName("RelWithDebInfo");
  1223. }
  1224. else if(target.GetType() == cmTarget::OBJECT_LIBRARY)
  1225. {
  1226. outputName = target.GetName();
  1227. outputName += ".lib";
  1228. outputNameDebug = outputName;
  1229. outputNameRelease = outputName;
  1230. outputNameMinSizeRel = outputName;
  1231. outputNameRelWithDebInfo = outputName;
  1232. }
  1233. // Compute the output directory for the target.
  1234. std::string outputDirOld;
  1235. std::string outputDirDebug;
  1236. std::string outputDirRelease;
  1237. std::string outputDirMinSizeRel;
  1238. std::string outputDirRelWithDebInfo;
  1239. if(target.GetType() == cmTarget::EXECUTABLE ||
  1240. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1241. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1242. target.GetType() == cmTarget::MODULE_LIBRARY)
  1243. {
  1244. #ifdef CM_USE_OLD_VS6
  1245. outputDirOld =
  1246. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1247. (target.GetDirectory().c_str()));
  1248. #endif
  1249. outputDirDebug =
  1250. removeQuotes(this->ConvertToOptionallyRelativeOutputPath(
  1251. target.GetDirectory("Debug").c_str()));
  1252. outputDirRelease =
  1253. removeQuotes(this->ConvertToOptionallyRelativeOutputPath(
  1254. target.GetDirectory("Release").c_str()));
  1255. outputDirMinSizeRel =
  1256. removeQuotes(this->ConvertToOptionallyRelativeOutputPath(
  1257. target.GetDirectory("MinSizeRel").c_str()));
  1258. outputDirRelWithDebInfo =
  1259. removeQuotes(this->ConvertToOptionallyRelativeOutputPath(
  1260. target.GetDirectory("RelWithDebInfo").c_str()));
  1261. }
  1262. else if(target.GetType() == cmTarget::OBJECT_LIBRARY)
  1263. {
  1264. std::string outputDir = cmake::GetCMakeFilesDirectoryPostSlash();
  1265. outputDirDebug = outputDir + "Debug";
  1266. outputDirRelease = outputDir + "Release";
  1267. outputDirMinSizeRel = outputDir + "MinSizeRel";
  1268. outputDirRelWithDebInfo = outputDir + "RelWithDebInfo";
  1269. }
  1270. // Compute the proper link information for the target.
  1271. std::string optionsDebug;
  1272. std::string optionsRelease;
  1273. std::string optionsMinSizeRel;
  1274. std::string optionsRelWithDebInfo;
  1275. if(target.GetType() == cmTarget::EXECUTABLE ||
  1276. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1277. target.GetType() == cmTarget::MODULE_LIBRARY)
  1278. {
  1279. extraLinkOptionsDebug =
  1280. extraLinkOptions + " " + extraLinkOptionsDebug;
  1281. extraLinkOptionsRelease =
  1282. extraLinkOptions + " " + extraLinkOptionsRelease;
  1283. extraLinkOptionsMinSizeRel =
  1284. extraLinkOptions + " " + extraLinkOptionsMinSizeRel;
  1285. extraLinkOptionsRelWithDebInfo =
  1286. extraLinkOptions + " " + extraLinkOptionsRelWithDebInfo;
  1287. this->ComputeLinkOptions(target, "Debug", extraLinkOptionsDebug,
  1288. optionsDebug);
  1289. this->ComputeLinkOptions(target, "Release", extraLinkOptionsRelease,
  1290. optionsRelease);
  1291. this->ComputeLinkOptions(target, "MinSizeRel", extraLinkOptionsMinSizeRel,
  1292. optionsMinSizeRel);
  1293. this->ComputeLinkOptions(target, "RelWithDebInfo",
  1294. extraLinkOptionsRelWithDebInfo,
  1295. optionsRelWithDebInfo);
  1296. }
  1297. // Compute the path of the import library.
  1298. std::string targetImplibFlagDebug;
  1299. std::string targetImplibFlagRelease;
  1300. std::string targetImplibFlagMinSizeRel;
  1301. std::string targetImplibFlagRelWithDebInfo;
  1302. if(target.GetType() == cmTarget::SHARED_LIBRARY ||
  1303. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1304. target.GetType() == cmTarget::EXECUTABLE)
  1305. {
  1306. std::string fullPathImpDebug = target.GetDirectory("Debug", true);
  1307. std::string fullPathImpRelease = target.GetDirectory("Release", true);
  1308. std::string fullPathImpMinSizeRel =
  1309. target.GetDirectory("MinSizeRel", true);
  1310. std::string fullPathImpRelWithDebInfo =
  1311. target.GetDirectory("RelWithDebInfo", true);
  1312. fullPathImpDebug += "/";
  1313. fullPathImpRelease += "/";
  1314. fullPathImpMinSizeRel += "/";
  1315. fullPathImpRelWithDebInfo += "/";
  1316. fullPathImpDebug += target.GetFullName("Debug", true);
  1317. fullPathImpRelease += target.GetFullName("Release", true);
  1318. fullPathImpMinSizeRel += target.GetFullName("MinSizeRel", true);
  1319. fullPathImpRelWithDebInfo += target.GetFullName("RelWithDebInfo", true);
  1320. targetImplibFlagDebug = "/implib:";
  1321. targetImplibFlagRelease = "/implib:";
  1322. targetImplibFlagMinSizeRel = "/implib:";
  1323. targetImplibFlagRelWithDebInfo = "/implib:";
  1324. targetImplibFlagDebug +=
  1325. this->ConvertToOptionallyRelativeOutputPath(fullPathImpDebug.c_str());
  1326. targetImplibFlagRelease +=
  1327. this->ConvertToOptionallyRelativeOutputPath(fullPathImpRelease.c_str());
  1328. targetImplibFlagMinSizeRel +=
  1329. this->ConvertToOptionallyRelativeOutputPath(
  1330. fullPathImpMinSizeRel.c_str());
  1331. targetImplibFlagRelWithDebInfo +=
  1332. this->ConvertToOptionallyRelativeOutputPath(
  1333. fullPathImpRelWithDebInfo.c_str());
  1334. }
  1335. #ifdef CM_USE_OLD_VS6
  1336. // Compute link information for the target.
  1337. if(extraLinkOptions.size())
  1338. {
  1339. libOptions += " ";
  1340. libOptions += extraLinkOptions;
  1341. libOptions += " ";
  1342. libMultiLineOptions += "# ADD LINK32 ";
  1343. libMultiLineOptions += extraLinkOptions;
  1344. libMultiLineOptions += " \n";
  1345. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1346. libMultiLineOptionsForDebug += extraLinkOptions;
  1347. libMultiLineOptionsForDebug += " \n";
  1348. }
  1349. #endif
  1350. // are there any custom rules on the target itself
  1351. // only if the target is a lib or exe
  1352. std::string customRuleCodeRelease
  1353. = this->CreateTargetRules(target, "RELEASE", libName);
  1354. std::string customRuleCodeDebug
  1355. = this->CreateTargetRules(target, "DEBUG", libName);
  1356. std::string customRuleCodeMinSizeRel
  1357. = this->CreateTargetRules(target, "MINSIZEREL", libName);
  1358. std::string customRuleCodeRelWithDebInfo
  1359. = this->CreateTargetRules(target, "RELWITHDEBINFO", libName);
  1360. cmsys::ifstream fin(this->DSPHeaderTemplate.c_str());
  1361. if(!fin)
  1362. {
  1363. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  1364. }
  1365. std::string staticLibOptions;
  1366. std::string staticLibOptionsDebug;
  1367. std::string staticLibOptionsRelease;
  1368. std::string staticLibOptionsMinSizeRel;
  1369. std::string staticLibOptionsRelWithDebInfo;
  1370. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1371. {
  1372. const char *libflagsGlobal =
  1373. this->Makefile->GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS");
  1374. this->AppendFlags(staticLibOptions, libflagsGlobal);
  1375. this->AppendFlags(staticLibOptionsDebug, libflagsGlobal);
  1376. this->AppendFlags(staticLibOptionsRelease, libflagsGlobal);
  1377. this->AppendFlags(staticLibOptionsMinSizeRel, libflagsGlobal);
  1378. this->AppendFlags(staticLibOptionsRelWithDebInfo, libflagsGlobal);
  1379. this->AppendFlags(staticLibOptionsDebug, this->Makefile->
  1380. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_DEBUG"));
  1381. this->AppendFlags(staticLibOptionsRelease, this->Makefile->
  1382. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_RELEASE"));
  1383. this->AppendFlags(staticLibOptionsMinSizeRel, this->Makefile->
  1384. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL"));
  1385. this->AppendFlags(staticLibOptionsRelWithDebInfo, this->Makefile->
  1386. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO"));
  1387. const char *libflags = target.GetProperty("STATIC_LIBRARY_FLAGS");
  1388. this->AppendFlags(staticLibOptions, libflags);
  1389. this->AppendFlags(staticLibOptionsDebug, libflags);
  1390. this->AppendFlags(staticLibOptionsRelease, libflags);
  1391. this->AppendFlags(staticLibOptionsMinSizeRel, libflags);
  1392. this->AppendFlags(staticLibOptionsRelWithDebInfo, libflags);
  1393. this->AppendFlags(staticLibOptionsDebug,
  1394. target.GetProperty("STATIC_LIBRARY_FLAGS_DEBUG"));
  1395. this->AppendFlags(staticLibOptionsRelease,
  1396. target.GetProperty("STATIC_LIBRARY_FLAGS_RELEASE"));
  1397. this->AppendFlags(staticLibOptionsMinSizeRel,
  1398. target.GetProperty("STATIC_LIBRARY_FLAGS_MINSIZEREL"));
  1399. this->AppendFlags(staticLibOptionsRelWithDebInfo,
  1400. target.GetProperty("STATIC_LIBRARY_FLAGS_RELWITHDEBINFO"));
  1401. std::string objects;
  1402. this->OutputObjects(target, "LIB", objects);
  1403. if(!objects.empty())
  1404. {
  1405. objects = "\n" + objects;
  1406. staticLibOptionsDebug += objects;
  1407. staticLibOptionsRelease += objects;
  1408. staticLibOptionsMinSizeRel += objects;
  1409. staticLibOptionsRelWithDebInfo += objects;
  1410. }
  1411. }
  1412. // Add the export symbol definition for shared library objects.
  1413. std::string exportSymbol;
  1414. if(const char* exportMacro = target.GetExportMacro())
  1415. {
  1416. exportSymbol = exportMacro;
  1417. }
  1418. std::string line;
  1419. std::string libnameExports;
  1420. if(exportSymbol.size())
  1421. {
  1422. libnameExports = "/D \"";
  1423. libnameExports += exportSymbol;
  1424. libnameExports += "\"";
  1425. }
  1426. while(cmSystemTools::GetLineFromStream(fin, line))
  1427. {
  1428. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  1429. if(!mfcFlag)
  1430. {
  1431. mfcFlag = "0";
  1432. }
  1433. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  1434. libnameExports.c_str());
  1435. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  1436. mfcFlag);
  1437. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  1438. target.GetType() == cmTarget::OBJECT_LIBRARY)
  1439. {
  1440. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_DEBUG",
  1441. staticLibOptionsDebug.c_str());
  1442. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_RELEASE",
  1443. staticLibOptionsRelease.c_str());
  1444. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_MINSIZEREL",
  1445. staticLibOptionsMinSizeRel.c_str());
  1446. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_RELWITHDEBINFO",
  1447. staticLibOptionsRelWithDebInfo.c_str());
  1448. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS",
  1449. staticLibOptions.c_str());
  1450. }
  1451. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  1452. {
  1453. cmSystemTools::ReplaceString(line, "/nologo", "");
  1454. }
  1455. #ifdef CM_USE_OLD_VS6
  1456. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  1457. libOptions.c_str());
  1458. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  1459. libDebugOptions.c_str());
  1460. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  1461. libOptimizedOptions.c_str());
  1462. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES_FOR_DEBUG",
  1463. libMultiLineOptionsForDebug.c_str());
  1464. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  1465. libMultiLineOptions.c_str());
  1466. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  1467. libMultiLineDebugOptions.c_str());
  1468. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  1469. libMultiLineOptimizedOptions.c_str());
  1470. #endif
  1471. // Substitute the rules for custom command. When specifying just the
  1472. // target name for the command the command can be different for
  1473. // different configs
  1474. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_RELEASE",
  1475. customRuleCodeRelease.c_str());
  1476. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_DEBUG",
  1477. customRuleCodeDebug.c_str());
  1478. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_MINSIZEREL",
  1479. customRuleCodeMinSizeRel.c_str());
  1480. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_RELWITHDEBINFO",
  1481. customRuleCodeRelWithDebInfo.c_str());
  1482. // Substitute the real output name into the template.
  1483. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_DEBUG",
  1484. outputNameDebug.c_str());
  1485. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELEASE",
  1486. outputNameRelease.c_str());
  1487. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_MINSIZEREL",
  1488. outputNameMinSizeRel.c_str());
  1489. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELWITHDEBINFO",
  1490. outputNameRelWithDebInfo.c_str());
  1491. cmSystemTools::ReplaceString(line, "OUTPUT_NAME", outputName.c_str());
  1492. // Substitute the proper link information into the template.
  1493. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_DEBUG",
  1494. optionsDebug.c_str());
  1495. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELEASE",
  1496. optionsRelease.c_str());
  1497. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_MINSIZEREL",
  1498. optionsMinSizeRel.c_str());
  1499. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELWITHDEBINFO",
  1500. optionsRelWithDebInfo.c_str());
  1501. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_DEBUG",
  1502. includeOptionsDebug.c_str());
  1503. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_RELEASE",
  1504. includeOptionsRelease.c_str());
  1505. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_MINSIZEREL",
  1506. includeOptionsMinSizeRel.c_str());
  1507. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_RELWITHDEBINFO",
  1508. includeOptionsRelWithDebInfo.c_str());
  1509. cmSystemTools::ReplaceString(line, "TARGET_VERSION_FLAG",
  1510. targetVersionFlag.c_str());
  1511. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_DEBUG",
  1512. targetImplibFlagDebug.c_str());
  1513. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELEASE",
  1514. targetImplibFlagRelease.c_str());
  1515. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_MINSIZEREL",
  1516. targetImplibFlagMinSizeRel.c_str());
  1517. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELWITHDEBINFO",
  1518. targetImplibFlagRelWithDebInfo.c_str());
  1519. std::string vs6name = GetVS6TargetName(libName);
  1520. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME", vs6name.c_str());
  1521. #ifdef CM_USE_OLD_VS6
  1522. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  1523. // are already quoted in the template file,
  1524. // we need to remove the quotes here, we still need
  1525. // to convert to output path for unix to win32 conversion
  1526. cmSystemTools::ReplaceString
  1527. (line, "LIBRARY_OUTPUT_PATH",
  1528. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1529. (libPath.c_str())).c_str());
  1530. cmSystemTools::ReplaceString
  1531. (line, "EXECUTABLE_OUTPUT_PATH",
  1532. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1533. (exePath.c_str())).c_str());
  1534. #endif
  1535. if(targetBuilds || target.GetType() == cmTarget::OBJECT_LIBRARY)
  1536. {
  1537. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_DEBUG",
  1538. outputDirDebug.c_str());
  1539. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_RELEASE",
  1540. outputDirRelease.c_str());
  1541. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_MINSIZEREL",
  1542. outputDirMinSizeRel.c_str());
  1543. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_RELWITHDEBINFO",
  1544. outputDirRelWithDebInfo.c_str());
  1545. if(!outputDirOld.empty())
  1546. {
  1547. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY",
  1548. outputDirOld.c_str());
  1549. }
  1550. }
  1551. cmSystemTools::ReplaceString(line,
  1552. "EXTRA_DEFINES",
  1553. this->Makefile->GetDefineFlags());
  1554. const char* debugPostfix
  1555. = this->Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  1556. cmSystemTools::ReplaceString(line, "DEBUG_POSTFIX",
  1557. debugPostfix?debugPostfix:"");
  1558. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1559. target.GetType() <= cmTarget::OBJECT_LIBRARY)
  1560. {
  1561. // store flags for each configuration
  1562. std::string flags = " ";
  1563. std::string flagsRelease = " ";
  1564. std::string flagsMinSizeRel = " ";
  1565. std::string flagsDebug = " ";
  1566. std::string flagsRelWithDebInfo = " ";
  1567. std::vector<std::string> configs;
  1568. target.GetMakefile()->GetConfigurations(configs);
  1569. std::vector<std::string>::const_iterator it = configs.begin();
  1570. const std::string& linkLanguage = target.GetLinkerLanguage(*it);
  1571. for ( ; it != configs.end(); ++it)
  1572. {
  1573. const std::string& configLinkLanguage = target.GetLinkerLanguage(*it);
  1574. if (configLinkLanguage != linkLanguage)
  1575. {
  1576. cmSystemTools::Error
  1577. ("Linker language must not vary by configuration for target: ",
  1578. target.GetName().c_str());
  1579. }
  1580. }
  1581. if(linkLanguage.empty())
  1582. {
  1583. cmSystemTools::Error
  1584. ("CMake can not determine linker language for target: ",
  1585. target.GetName().c_str());
  1586. return;
  1587. }
  1588. // if CXX is on and the target contains cxx code then add the cxx flags
  1589. std::string baseFlagVar = "CMAKE_";
  1590. baseFlagVar += linkLanguage;
  1591. baseFlagVar += "_FLAGS";
  1592. flags = this->Makefile->GetSafeDefinition(baseFlagVar.c_str());
  1593. std::string flagVar = baseFlagVar + "_RELEASE";
  1594. flagsRelease = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1595. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  1596. flagVar = baseFlagVar + "_MINSIZEREL";
  1597. flagsMinSizeRel = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1598. flagsMinSizeRel += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  1599. flagVar = baseFlagVar + "_DEBUG";
  1600. flagsDebug = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1601. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  1602. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  1603. flagsRelWithDebInfo = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1604. flagsRelWithDebInfo += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  1605. this->AddCompileOptions(flags, &target, linkLanguage, "");
  1606. this->AddCompileOptions(flagsDebug, &target, linkLanguage, "Debug");
  1607. this->AddCompileOptions(flagsRelease, &target, linkLanguage, "Release");
  1608. this->AddCompileOptions(flagsMinSizeRel, &target, linkLanguage,
  1609. "MinSizeRel");
  1610. this->AddCompileOptions(flagsRelWithDebInfo, &target, linkLanguage,
  1611. "RelWithDebInfo");
  1612. // if _UNICODE and _SBCS are not found, then add -D_MBCS
  1613. std::string defs = this->Makefile->GetDefineFlags();
  1614. if(flags.find("D_UNICODE") == flags.npos &&
  1615. defs.find("D_UNICODE") == flags.npos &&
  1616. flags.find("D_SBCS") == flags.npos &&
  1617. defs.find("D_SBCS") == flags.npos)
  1618. {
  1619. flags += " /D \"_MBCS\"";
  1620. }
  1621. // Add per-target and per-configuration preprocessor definitions.
  1622. std::set<std::string> definesSet;
  1623. std::set<std::string> debugDefinesSet;
  1624. std::set<std::string> releaseDefinesSet;
  1625. std::set<std::string> minsizeDefinesSet;
  1626. std::set<std::string> debugrelDefinesSet;
  1627. this->AddCompileDefinitions(definesSet, &target, "", linkLanguage);
  1628. this->AddCompileDefinitions(debugDefinesSet, &target,
  1629. "DEBUG", linkLanguage);
  1630. this->AddCompileDefinitions(releaseDefinesSet, &target,
  1631. "RELEASE", linkLanguage);
  1632. this->AddCompileDefinitions(minsizeDefinesSet, &target,
  1633. "MINSIZEREL", linkLanguage);
  1634. this->AddCompileDefinitions(debugrelDefinesSet, &target,
  1635. "RELWITHDEBINFO", linkLanguage);
  1636. std::string defines = " ";
  1637. std::string debugDefines = " ";
  1638. std::string releaseDefines = " ";
  1639. std::string minsizeDefines = " ";
  1640. std::string debugrelDefines = " ";
  1641. this->JoinDefines(definesSet, defines, "");
  1642. this->JoinDefines(debugDefinesSet, debugDefines, "");
  1643. this->JoinDefines(releaseDefinesSet, releaseDefines, "");
  1644. this->JoinDefines(minsizeDefinesSet, minsizeDefines, "");
  1645. this->JoinDefines(debugrelDefinesSet, debugrelDefines, "");
  1646. flags += defines;
  1647. flagsDebug += debugDefines;
  1648. flagsRelease += releaseDefines;
  1649. flagsMinSizeRel += minsizeDefines;
  1650. flagsRelWithDebInfo += debugrelDefines;
  1651. // The template files have CXX FLAGS in them, that need to be replaced.
  1652. // There are not separate CXX and C template files, so we use the same
  1653. // variable names. The previous code sets up flags* variables to
  1654. // contain the correct C or CXX flags
  1655. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL",
  1656. flagsMinSizeRel.c_str());
  1657. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG",
  1658. flagsDebug.c_str());
  1659. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELWITHDEBINFO",
  1660. flagsRelWithDebInfo.c_str());
  1661. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE",
  1662. flagsRelease.c_str());
  1663. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  1664. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_MINSIZEREL",
  1665. minsizeDefines.c_str());
  1666. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_DEBUG",
  1667. debugDefines.c_str());
  1668. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_RELWITHDEBINFO",
  1669. debugrelDefines.c_str());
  1670. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_RELEASE",
  1671. releaseDefines.c_str());
  1672. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS",
  1673. defines.c_str());
  1674. }
  1675. fout << line.c_str() << std::endl;
  1676. }
  1677. }
  1678. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  1679. {
  1680. cmsys::ifstream fin(this->DSPFooterTemplate.c_str());
  1681. if(!fin)
  1682. {
  1683. cmSystemTools::Error("Error Reading ",
  1684. this->DSPFooterTemplate.c_str());
  1685. }
  1686. std::string line;
  1687. while(cmSystemTools::GetLineFromStream(fin, line))
  1688. {
  1689. fout << line << std::endl;
  1690. }
  1691. }
  1692. //----------------------------------------------------------------------------
  1693. void cmLocalVisualStudio6Generator
  1694. ::ComputeLinkOptions(cmTarget& target,
  1695. const std::string& configName,
  1696. const std::string extraOptions,
  1697. std::string& options)
  1698. {
  1699. // Compute the link information for this configuration.
  1700. cmComputeLinkInformation* pcli = target.GetLinkInformation(configName);
  1701. if(!pcli)
  1702. {
  1703. return;
  1704. }
  1705. cmComputeLinkInformation& cli = *pcli;
  1706. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1707. ItemVector const& linkLibs = cli.GetItems();
  1708. std::vector<std::string> const& linkDirs = cli.GetDirectories();
  1709. this->OutputObjects(target, "LINK", options);
  1710. // Build the link options code.
  1711. for(std::vector<std::string>::const_iterator d = linkDirs.begin();
  1712. d != linkDirs.end(); ++d)
  1713. {
  1714. std::string dir = *d;
  1715. if(!dir.empty())
  1716. {
  1717. if(dir[dir.size()-1] != '/')
  1718. {
  1719. dir += "/";
  1720. }
  1721. dir += "$(IntDir)";
  1722. options += "# ADD LINK32 /LIBPATH:";
  1723. options += this->ConvertToOptionallyRelativeOutputPath(dir.c_str());
  1724. options += " /LIBPATH:";
  1725. options += this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  1726. options += "\n";
  1727. }
  1728. }
  1729. for(ItemVector::const_iterator l = linkLibs.begin();
  1730. l != linkLibs.end(); ++l)
  1731. {
  1732. options += "# ADD LINK32 ";
  1733. if(l->IsPath)
  1734. {
  1735. options +=
  1736. this->ConvertToOptionallyRelativeOutputPath(l->Value.c_str());
  1737. }
  1738. else if (!l->Target
  1739. || l->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  1740. {
  1741. options += l->Value;
  1742. }
  1743. options += "\n";
  1744. }
  1745. // Add extra options if any.
  1746. if(!extraOptions.empty())
  1747. {
  1748. options += "# ADD LINK32 ";
  1749. options += extraOptions;
  1750. options += "\n";
  1751. }
  1752. }
  1753. //----------------------------------------------------------------------------
  1754. void cmLocalVisualStudio6Generator
  1755. ::OutputObjects(cmTarget& target, const char* tool,
  1756. std::string& options)
  1757. {
  1758. // VS 6 does not support per-config source locations so we
  1759. // list object library content on the link line instead.
  1760. cmGeneratorTarget* gt =
  1761. this->GlobalGenerator->GetGeneratorTarget(&target);
  1762. std::vector<std::string> objs;
  1763. gt->UseObjectLibraries(objs, "");
  1764. for(std::vector<std::string>::const_iterator
  1765. oi = objs.begin(); oi != objs.end(); ++oi)
  1766. {
  1767. options += "# ADD ";
  1768. options += tool;
  1769. options += "32 ";
  1770. options += this->ConvertToOptionallyRelativeOutputPath(oi->c_str());
  1771. options += "\n";
  1772. }
  1773. }
  1774. std::string
  1775. cmLocalVisualStudio6Generator
  1776. ::GetTargetDirectory(cmTarget const&) const
  1777. {
  1778. // No per-target directory for this generator (yet).
  1779. return "";
  1780. }
  1781. //----------------------------------------------------------------------------
  1782. std::string
  1783. cmLocalVisualStudio6Generator
  1784. ::ComputeLongestObjectDirectory(cmTarget&) const
  1785. {
  1786. // Compute the maximum length configuration name.
  1787. std::string config_max;
  1788. for(std::vector<std::string>::const_iterator
  1789. i = this->Configurations.begin();
  1790. i != this->Configurations.end(); ++i)
  1791. {
  1792. // Strip the subdirectory name out of the configuration name.
  1793. std::string config = this->GetConfigName(*i);
  1794. if(config.size() > config_max.size())
  1795. {
  1796. config_max = config;
  1797. }
  1798. }
  1799. // Compute the maximum length full path to the intermediate
  1800. // files directory for any configuration. This is used to construct
  1801. // object file names that do not produce paths that are too long.
  1802. std::string dir_max;
  1803. dir_max += this->Makefile->GetCurrentBinaryDirectory();
  1804. dir_max += "/";
  1805. dir_max += config_max;
  1806. dir_max += "/";
  1807. return dir_max;
  1808. }
  1809. std::string
  1810. cmLocalVisualStudio6Generator
  1811. ::GetConfigName(std::string const& configuration) const
  1812. {
  1813. // Strip the subdirectory name out of the configuration name.
  1814. std::string config = configuration;
  1815. std::string::size_type pos = config.find_last_of(" ");
  1816. config = config.substr(pos+1, std::string::npos);
  1817. config = config.substr(0, config.size()-1);
  1818. return config;
  1819. }
  1820. //----------------------------------------------------------------------------
  1821. bool
  1822. cmLocalVisualStudio6Generator
  1823. ::CheckDefinition(std::string const& define) const
  1824. {
  1825. // Perform the standard check first.
  1826. if(!this->cmLocalGenerator::CheckDefinition(define))
  1827. {
  1828. return false;
  1829. }
  1830. // Now do the VS6-specific check.
  1831. if(define.find_first_of(" ") != define.npos &&
  1832. define.find_first_of("\"$;") != define.npos)
  1833. {
  1834. std::ostringstream e;
  1835. e << "WARNING: The VS6 IDE does not support preprocessor definition "
  1836. << "values with spaces and '\"', '$', or ';'.\n"
  1837. << "CMake is dropping a preprocessor definition: " << define << "\n"
  1838. << "Consider defining the macro in a (configured) header file.\n";
  1839. cmSystemTools::Message(e.str().c_str());
  1840. return false;
  1841. }
  1842. // Assume it is supported.
  1843. return true;
  1844. }