cmLocalVisualStudio6Generator.cxx 66 KB

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