cmLocalVisualStudio6Generator.cxx 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLocalVisualStudio6Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmCacheManager.h"
  19. #include "cmake.h"
  20. #include <cmsys/RegularExpression.hxx>
  21. cmLocalVisualStudio6Generator::cmLocalVisualStudio6Generator()
  22. {
  23. }
  24. cmLocalVisualStudio6Generator::~cmLocalVisualStudio6Generator()
  25. {
  26. }
  27. void cmLocalVisualStudio6Generator::Generate()
  28. {
  29. std::set<cmStdString> lang;
  30. lang.insert("C");
  31. lang.insert("CXX");
  32. this->CreateCustomTargetsAndCommands(lang);
  33. this->OutputDSPFile();
  34. }
  35. void cmLocalVisualStudio6Generator::OutputDSPFile()
  36. {
  37. // If not an in source build, then create the output directory
  38. if(strcmp(this->Makefile->GetStartOutputDirectory(),
  39. this->Makefile->GetHomeDirectory()) != 0)
  40. {
  41. if(!cmSystemTools::MakeDirectory
  42. (this->Makefile->GetStartOutputDirectory()))
  43. {
  44. cmSystemTools::Error("Error creating directory ",
  45. this->Makefile->GetStartOutputDirectory());
  46. }
  47. }
  48. // Setup /I and /LIBPATH options for the resulting DSP file. VS 6
  49. // truncates long include paths so make it as short as possible if
  50. // the length threatents this problem.
  51. unsigned int maxIncludeLength = 3000;
  52. bool useShortPath = false;
  53. for(int j=0; j < 2; ++j)
  54. {
  55. std::vector<std::string> includes;
  56. this->GetIncludeDirectories(includes);
  57. std::vector<std::string>::iterator i;
  58. for(i = includes.begin(); i != includes.end(); ++i)
  59. {
  60. std::string tmp =
  61. this->ConvertToOptionallyRelativeOutputPath(i->c_str());
  62. if(useShortPath)
  63. {
  64. cmSystemTools::GetShortPath(tmp.c_str(), tmp);
  65. }
  66. this->IncludeOptions += " /I ";
  67. // quote if not already quoted
  68. if (tmp[0] != '"')
  69. {
  70. this->IncludeOptions += "\"";
  71. this->IncludeOptions += tmp;
  72. this->IncludeOptions += "\"";
  73. }
  74. else
  75. {
  76. this->IncludeOptions += tmp;
  77. }
  78. }
  79. if(j == 0 && this->IncludeOptions.size() > maxIncludeLength)
  80. {
  81. this->IncludeOptions = "";
  82. useShortPath = true;
  83. }
  84. else
  85. {
  86. break;
  87. }
  88. }
  89. // Create the DSP or set of DSP's for libraries and executables
  90. // clear project names
  91. this->CreatedProjectNames.clear();
  92. // Call TraceVSDependencies on all targets
  93. cmTargets &tgts = this->Makefile->GetTargets();
  94. for(cmTargets::iterator l = tgts.begin();
  95. l != tgts.end(); l++)
  96. {
  97. // Add a rule to regenerate the build system when the target
  98. // specification source changes.
  99. const char* suppRegenRule =
  100. this->Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  101. if (!cmSystemTools::IsOn(suppRegenRule))
  102. {
  103. this->AddDSPBuildRule(l->second);
  104. }
  105. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  106. // so don't build a projectfile for it
  107. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  108. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  109. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  110. {
  111. cmTarget& target = l->second;
  112. target.TraceVSDependencies(target.GetName(), this->Makefile);
  113. }
  114. }
  115. // build any targets
  116. for(cmTargets::iterator l = tgts.begin();
  117. l != tgts.end(); l++)
  118. {
  119. switch(l->second.GetType())
  120. {
  121. case cmTarget::STATIC_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. case cmTarget::INSTALL_FILES:
  136. break;
  137. case cmTarget::INSTALL_PROGRAMS:
  138. break;
  139. default:
  140. cmSystemTools::Error("Bad target type", l->first.c_str());
  141. break;
  142. }
  143. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  144. // so don't build a projectfile for it
  145. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  146. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  147. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  148. {
  149. // check to see if the dsp is going into a sub-directory
  150. std::string::size_type pos = l->first.rfind('/');
  151. if(pos != std::string::npos)
  152. {
  153. std::string dir = this->Makefile->GetStartOutputDirectory();
  154. dir += "/";
  155. dir += l->first.substr(0, pos);
  156. if(!cmSystemTools::MakeDirectory(dir.c_str()))
  157. {
  158. cmSystemTools::Error("Error creating directory ", dir.c_str());
  159. }
  160. }
  161. this->CreateSingleDSP(l->first.c_str(),l->second);
  162. }
  163. }
  164. }
  165. void cmLocalVisualStudio6Generator::CreateSingleDSP(const char *lname,
  166. cmTarget &target)
  167. {
  168. // add to the list of projects
  169. std::string pname = lname;
  170. this->CreatedProjectNames.push_back(pname);
  171. // create the dsp.cmake file
  172. std::string fname;
  173. fname = this->Makefile->GetStartOutputDirectory();
  174. fname += "/";
  175. fname += lname;
  176. fname += ".dsp";
  177. // save the name of the real dsp file
  178. std::string realDSP = fname;
  179. fname += ".cmake";
  180. std::ofstream fout(fname.c_str());
  181. if(!fout)
  182. {
  183. cmSystemTools::Error("Error Writing ", fname.c_str());
  184. cmSystemTools::ReportLastSystemError("");
  185. }
  186. this->WriteDSPFile(fout,lname,target);
  187. fout.close();
  188. // if the dsp file has changed, then write it.
  189. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  190. }
  191. void cmLocalVisualStudio6Generator::AddDSPBuildRule(cmTarget& tgt)
  192. {
  193. std::string dspname = tgt.GetName();
  194. dspname += ".dsp.cmake";
  195. const char* dsprule =
  196. this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  197. cmCustomCommandLine commandLine;
  198. commandLine.push_back(dsprule);
  199. std::string makefileIn = this->Makefile->GetStartDirectory();
  200. makefileIn += "/";
  201. makefileIn += "CMakeLists.txt";
  202. std::string comment = "Building Custom Rule ";
  203. comment += makefileIn;
  204. std::string args;
  205. args = "-H";
  206. args += this->Convert(this->Makefile->GetHomeDirectory(),
  207. START_OUTPUT, UNCHANGED, true);
  208. commandLine.push_back(args);
  209. args = "-B";
  210. args +=
  211. this->Convert(this->Makefile->GetHomeOutputDirectory(),
  212. START_OUTPUT, UNCHANGED, true);
  213. commandLine.push_back(args);
  214. std::string configFile =
  215. this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  216. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  217. std::vector<std::string> listFiles = this->Makefile->GetListFiles();
  218. bool found = false;
  219. for(std::vector<std::string>::iterator i = listFiles.begin();
  220. i != listFiles.end(); ++i)
  221. {
  222. if(*i == configFile)
  223. {
  224. found = true;
  225. }
  226. }
  227. if(!found)
  228. {
  229. listFiles.push_back(configFile);
  230. }
  231. cmCustomCommandLines commandLines;
  232. commandLines.push_back(commandLine);
  233. const char* no_working_directory = 0;
  234. this->Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles,
  235. makefileIn.c_str(), commandLines,
  236. comment.c_str(),
  237. no_working_directory, true);
  238. if(cmSourceFile* file = this->Makefile->GetSource(makefileIn.c_str()))
  239. {
  240. tgt.GetSourceFiles().push_back(file);
  241. }
  242. else
  243. {
  244. cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
  245. }
  246. }
  247. void cmLocalVisualStudio6Generator::WriteDSPFile(std::ostream& fout,
  248. const char *libName,
  249. cmTarget &target)
  250. {
  251. // For utility targets need custom command since pre- and post-
  252. // build does not do anything in Visual Studio 6. In order for the
  253. // rules to run in the correct order as custom commands, we need
  254. // special care for dependencies. The first rule must depend on all
  255. // the dependencies of all the rules. The later rules must each
  256. // depend only on the previous rule.
  257. if ((target.GetType() == cmTarget::UTILITY ||
  258. target.GetType() == cmTarget::GLOBAL_TARGET) &&
  259. (!target.GetPreBuildCommands().empty() ||
  260. !target.GetPostBuildCommands().empty()))
  261. {
  262. // Accumulate the dependencies of all the commands.
  263. std::vector<std::string> depends;
  264. for (std::vector<cmCustomCommand>::const_iterator cr =
  265. target.GetPreBuildCommands().begin();
  266. cr != target.GetPreBuildCommands().end(); ++cr)
  267. {
  268. depends.insert(depends.end(),
  269. cr->GetDepends().begin(), cr->GetDepends().end());
  270. }
  271. for (std::vector<cmCustomCommand>::const_iterator cr =
  272. target.GetPostBuildCommands().begin();
  273. cr != target.GetPostBuildCommands().end(); ++cr)
  274. {
  275. depends.insert(depends.end(),
  276. cr->GetDepends().begin(), cr->GetDepends().end());
  277. }
  278. // Add the pre- and post-build commands in order.
  279. int count = 1;
  280. for (std::vector<cmCustomCommand>::const_iterator cr =
  281. target.GetPreBuildCommands().begin();
  282. cr != target.GetPreBuildCommands().end(); ++cr)
  283. {
  284. this->AddUtilityCommandHack(target, count++, depends, *cr);
  285. }
  286. for (std::vector<cmCustomCommand>::const_iterator cr =
  287. target.GetPostBuildCommands().begin();
  288. cr != target.GetPostBuildCommands().end(); ++cr)
  289. {
  290. this->AddUtilityCommandHack(target, count++, depends, *cr);
  291. }
  292. }
  293. // trace the visual studio dependencies
  294. std::string name = libName;
  295. name += ".dsp.cmake";
  296. // We may be modifying the source groups temporarily, so make a copy.
  297. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  298. // get the classes from the source lists then add them to the groups
  299. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  300. // now all of the source files have been properly assigned to the target
  301. // now stick them into source groups using the reg expressions
  302. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  303. i != classes.end(); i++)
  304. {
  305. // Add the file to the list of sources.
  306. std::string source = (*i)->GetFullPath();
  307. cmSourceGroup& sourceGroup =
  308. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  309. sourceGroup.AssignSource(*i);
  310. // while we are at it, if it is a .rule file then for visual studio 6 we
  311. // must generate it
  312. if ((*i)->GetSourceExtension() == "rule")
  313. {
  314. if(!cmSystemTools::FileExists(source.c_str()))
  315. {
  316. cmSystemTools::ReplaceString(source, "$(IntDir)/", "");
  317. #if defined(_WIN32) || defined(__CYGWIN__)
  318. std::ofstream fout(source.c_str(),
  319. std::ios::binary | std::ios::out
  320. | std::ios::trunc);
  321. #else
  322. std::ofstream fout(source.c_str(),
  323. std::ios::out | std::ios::trunc);
  324. #endif
  325. if(fout)
  326. {
  327. fout.write("# generated from CMake",22);
  328. fout.flush();
  329. fout.close();
  330. }
  331. }
  332. }
  333. }
  334. // Compute which sources need unique object computation.
  335. this->ComputeObjectNameRequirements(sourceGroups);
  336. // Write the DSP file's header.
  337. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  338. // Loop through every source group.
  339. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  340. sg != sourceGroups.end(); ++sg)
  341. {
  342. this->WriteGroup(&(*sg), target, fout, libName);
  343. }
  344. // Write the DSP file's footer.
  345. this->WriteDSPFooter(fout);
  346. }
  347. void cmLocalVisualStudio6Generator
  348. ::WriteGroup(const cmSourceGroup *sg, cmTarget target,
  349. std::ostream &fout, const char *libName)
  350. {
  351. const std::vector<const cmSourceFile *> &sourceFiles =
  352. sg->GetSourceFiles();
  353. // If the group is empty, don't write it at all.
  354. if(sourceFiles.empty())
  355. {
  356. return;
  357. }
  358. // If the group has a name, write the header.
  359. std::string name = sg->GetName();
  360. if(name != "")
  361. {
  362. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  363. }
  364. // Loop through each source in the source group.
  365. for(std::vector<const cmSourceFile *>::const_iterator sf =
  366. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  367. {
  368. std::string source = (*sf)->GetFullPath();
  369. const cmCustomCommand *command =
  370. (*sf)->GetCustomCommand();
  371. std::string compileFlags;
  372. std::vector<std::string> depends;
  373. std::string objectNameDir;
  374. if(this->NeedObjectName.find(*sf) != this->NeedObjectName.end())
  375. {
  376. objectNameDir =
  377. cmSystemTools::GetFilenamePath(
  378. this->GetObjectFileNameWithoutTarget(*(*sf)));
  379. }
  380. // Add per-source file flags.
  381. if(const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS"))
  382. {
  383. compileFlags += cflags;
  384. }
  385. const char* lang = this->GlobalGenerator->
  386. GetLanguageFromExtension((*sf)->GetSourceExtension().c_str());
  387. if(lang && strcmp(lang, "CXX") == 0)
  388. {
  389. // force a C++ file type
  390. compileFlags += " /TP ";
  391. }
  392. // Check for extra object-file dependencies.
  393. const char* dependsValue = (*sf)->GetProperty("OBJECT_DEPENDS");
  394. if(dependsValue)
  395. {
  396. cmSystemTools::ExpandListArgument(dependsValue, depends);
  397. }
  398. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  399. target.GetType() == cmTarget::GLOBAL_TARGET)
  400. {
  401. fout << "# Begin Source File\n\n";
  402. // Tell MS-Dev what the source is. If the compiler knows how to
  403. // build it, then it will.
  404. fout << "SOURCE=" <<
  405. this->ConvertToOptionallyRelativeOutputPath(source.c_str()) << "\n\n";
  406. if(!depends.empty())
  407. {
  408. // Write out the dependencies for the rule.
  409. fout << "USERDEP__HACK=";
  410. for(std::vector<std::string>::const_iterator d = depends.begin();
  411. d != depends.end(); ++d)
  412. {
  413. fout << "\\\n\t" <<
  414. this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  415. }
  416. fout << "\n";
  417. }
  418. if (command)
  419. {
  420. std::string script =
  421. this->ConstructScript(command->GetCommandLines(),
  422. command->GetWorkingDirectory(),
  423. command->GetEscapeOldStyle(),
  424. command->GetEscapeAllowMakeVars(),
  425. "\\\n\t");
  426. std::string comment =
  427. this->ConstructComment(*command,
  428. "Building Custom Rule $(InputPath)");
  429. if(comment == "<hack>")
  430. {
  431. comment = "";
  432. }
  433. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  434. this->WriteCustomRule(fout, source.c_str(), script.c_str(),
  435. comment.c_str(), command->GetDepends(),
  436. command->GetOutputs(), flags);
  437. }
  438. else if(!compileFlags.empty() || !objectNameDir.empty())
  439. {
  440. for(std::vector<std::string>::iterator i
  441. = this->Configurations.begin();
  442. i != this->Configurations.end(); ++i)
  443. {
  444. if (i == this->Configurations.begin())
  445. {
  446. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  447. }
  448. else
  449. {
  450. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  451. }
  452. if(!compileFlags.empty())
  453. {
  454. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  455. }
  456. if(!objectNameDir.empty())
  457. {
  458. // Strip the subdirectory name out of the configuration name.
  459. std::string config = *i;
  460. std::string::size_type pos = config.find_last_of(" ");
  461. config = config.substr(pos+1, std::string::npos);
  462. config = config.substr(0, config.size()-1);
  463. // Setup an alternate object file directory.
  464. fout << "\n# PROP Intermediate_Dir \""
  465. << config << "/" << objectNameDir << "\"\n\n";
  466. }
  467. }
  468. fout << "!ENDIF\n\n";
  469. }
  470. fout << "# End Source File\n";
  471. }
  472. }
  473. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  474. for(unsigned int i=0;i<children.size();++i)
  475. {
  476. this->WriteGroup(&children[i], target, fout, libName);
  477. }
  478. // If the group has a name, write the footer.
  479. if(name != "")
  480. {
  481. this->WriteDSPEndGroup(fout);
  482. }
  483. }
  484. void
  485. cmLocalVisualStudio6Generator
  486. ::AddUtilityCommandHack(cmTarget& target, int count,
  487. std::vector<std::string>& depends,
  488. const cmCustomCommand& origCommand)
  489. {
  490. // Create a fake output that forces the rule to run.
  491. char* output = new char[(strlen(this->Makefile->GetStartOutputDirectory()) +
  492. strlen(target.GetName()) + 30)];
  493. sprintf(output,"%s/%s_force_%i", this->Makefile->GetStartOutputDirectory(),
  494. target.GetName(), count);
  495. std::string comment = this->ConstructComment(origCommand, "<hack>");
  496. // Add the rule with the given dependencies and commands.
  497. const char* no_main_dependency = 0;
  498. this->Makefile->AddCustomCommandToOutput(output,
  499. depends,
  500. no_main_dependency,
  501. origCommand.GetCommandLines(),
  502. comment.c_str(),
  503. origCommand.GetWorkingDirectory());
  504. // Replace the dependencies with the output of this rule so that the
  505. // next rule added will run after this one.
  506. depends.clear();
  507. depends.push_back(output);
  508. // Add a source file representing this output to the project.
  509. cmSourceFile* outsf = this->Makefile->GetSourceFileWithOutput(output);
  510. target.GetSourceFiles().push_back(outsf);
  511. // Free the fake output name.
  512. delete [] output;
  513. }
  514. void
  515. cmLocalVisualStudio6Generator
  516. ::WriteCustomRule(std::ostream& fout,
  517. const char* source,
  518. const char* command,
  519. const char* comment,
  520. const std::vector<std::string>& depends,
  521. const std::vector<std::string>& outputs,
  522. const char* flags)
  523. {
  524. // Write the rule for each configuration.
  525. std::vector<std::string>::iterator i;
  526. for(i = this->Configurations.begin(); i != this->Configurations.end(); ++i)
  527. {
  528. if (i == this->Configurations.begin())
  529. {
  530. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  531. }
  532. else
  533. {
  534. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  535. }
  536. if(flags)
  537. {
  538. fout << "\n# ADD CPP " << flags << "\n\n";
  539. }
  540. // Write out the dependencies for the rule.
  541. fout << "USERDEP__HACK=";
  542. for(std::vector<std::string>::const_iterator d = depends.begin();
  543. d != depends.end(); ++d)
  544. {
  545. // Lookup the real name of the dependency in case it is a CMake target.
  546. std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
  547. fout << "\\\n\t" <<
  548. this->ConvertToOptionallyRelativeOutputPath(dep.c_str());
  549. }
  550. fout << "\n";
  551. fout << "# PROP Ignore_Default_Tool 1\n";
  552. fout << "# Begin Custom Build -";
  553. if(comment && *comment)
  554. {
  555. fout << " " << comment;
  556. }
  557. fout << "\n\n";
  558. if(outputs.empty())
  559. {
  560. fout << source
  561. << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  562. fout << command << "\n\n";
  563. }
  564. else
  565. {
  566. for(std::vector<std::string>::const_iterator o = outputs.begin();
  567. o != outputs.end(); ++o)
  568. {
  569. // Write a rule for every output generated by this command.
  570. fout << this->ConvertToOptionallyRelativeOutputPath(o->c_str())
  571. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  572. fout << command << "\n\n";
  573. }
  574. }
  575. fout << "# End Custom Build\n\n";
  576. }
  577. fout << "!ENDIF\n\n";
  578. }
  579. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  580. const char* group,
  581. const char* filter)
  582. {
  583. fout << "# Begin Group \"" << group << "\"\n"
  584. "# PROP Default_Filter \"" << filter << "\"\n";
  585. }
  586. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  587. {
  588. fout << "# End Group\n";
  589. }
  590. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b,
  591. const char* libName,
  592. cmTarget& target)
  593. {
  594. std::string root= this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  595. const char *def=
  596. this->Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  597. if( def)
  598. {
  599. root = def;
  600. }
  601. else
  602. {
  603. root += "/Templates";
  604. }
  605. switch(b)
  606. {
  607. case STATIC_LIBRARY:
  608. this->DSPHeaderTemplate = root;
  609. this->DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  610. this->DSPFooterTemplate = root;
  611. this->DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  612. break;
  613. case DLL:
  614. this->DSPHeaderTemplate = root;
  615. this->DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  616. this->DSPFooterTemplate = root;
  617. this->DSPFooterTemplate += "/DLLFooter.dsptemplate";
  618. break;
  619. case EXECUTABLE:
  620. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  621. {
  622. this->DSPHeaderTemplate = root;
  623. this->DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  624. this->DSPFooterTemplate = root;
  625. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  626. }
  627. else
  628. {
  629. this->DSPHeaderTemplate = root;
  630. this->DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  631. this->DSPFooterTemplate = root;
  632. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  633. }
  634. break;
  635. case UTILITY:
  636. this->DSPHeaderTemplate = root;
  637. this->DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  638. this->DSPFooterTemplate = root;
  639. this->DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  640. break;
  641. }
  642. // once the build type is set, determine what configurations are
  643. // possible
  644. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  645. cmsys::RegularExpression reg("# Name ");
  646. if(!fin)
  647. {
  648. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  649. }
  650. // reset this->Configurations
  651. this->Configurations.erase(this->Configurations.begin(),
  652. this->Configurations.end());
  653. // now add all the configurations possible
  654. std::string line;
  655. while(cmSystemTools::GetLineFromStream(fin, line))
  656. {
  657. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  658. if (reg.find(line))
  659. {
  660. this->Configurations.push_back(line.substr(reg.end()));
  661. }
  662. }
  663. }
  664. // look for custom rules on a target and collect them together
  665. std::string
  666. cmLocalVisualStudio6Generator::CreateTargetRules(cmTarget &target,
  667. const char * /* libName */)
  668. {
  669. std::string customRuleCode = "";
  670. if (target.GetType() >= cmTarget::UTILITY )
  671. {
  672. return customRuleCode;
  673. }
  674. // are there any rules?
  675. if (target.GetPreBuildCommands().size() +
  676. target.GetPreLinkCommands().size() +
  677. target.GetPostBuildCommands().size() == 0)
  678. {
  679. return customRuleCode;
  680. }
  681. customRuleCode = "# Begin Special Build Tool\n";
  682. // Write the pre-build and pre-link together (VS6 does not support
  683. // both). Make sure no continuation character is put on the last
  684. // line.
  685. int prelink_total = (static_cast<int>(target.GetPreBuildCommands().size())+
  686. static_cast<int>(target.GetPreLinkCommands().size()));
  687. int prelink_count = 0;
  688. if(prelink_total > 0)
  689. {
  690. // header stuff
  691. customRuleCode += "PreLink_Cmds=";
  692. }
  693. const char* prelink_newline = "\\\n\t";
  694. for (std::vector<cmCustomCommand>::const_iterator cr =
  695. target.GetPreBuildCommands().begin();
  696. cr != target.GetPreBuildCommands().end(); ++cr)
  697. {
  698. if(++prelink_count == prelink_total)
  699. {
  700. prelink_newline = "";
  701. }
  702. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  703. cr->GetWorkingDirectory(),
  704. cr->GetEscapeOldStyle(),
  705. cr->GetEscapeAllowMakeVars(),
  706. prelink_newline);
  707. }
  708. for (std::vector<cmCustomCommand>::const_iterator cr =
  709. target.GetPreLinkCommands().begin();
  710. cr != target.GetPreLinkCommands().end(); ++cr)
  711. {
  712. if(++prelink_count == prelink_total)
  713. {
  714. prelink_newline = "";
  715. }
  716. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  717. cr->GetWorkingDirectory(),
  718. cr->GetEscapeOldStyle(),
  719. cr->GetEscapeAllowMakeVars(),
  720. prelink_newline);
  721. }
  722. if(prelink_total > 0)
  723. {
  724. customRuleCode += "\n";
  725. }
  726. // Write the post-build rules. Make sure no continuation character
  727. // is put on the last line.
  728. int postbuild_total =
  729. static_cast<int>(target.GetPostBuildCommands().size());
  730. int postbuild_count = 0;
  731. const char* postbuild_newline = "\\\n\t";
  732. if(postbuild_total > 0)
  733. {
  734. customRuleCode += "PostBuild_Cmds=";
  735. }
  736. for (std::vector<cmCustomCommand>::const_iterator cr =
  737. target.GetPostBuildCommands().begin();
  738. cr != target.GetPostBuildCommands().end(); ++cr)
  739. {
  740. if(++postbuild_count == postbuild_total)
  741. {
  742. postbuild_newline = "";
  743. }
  744. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  745. cr->GetWorkingDirectory(),
  746. cr->GetEscapeOldStyle(),
  747. cr->GetEscapeAllowMakeVars(),
  748. postbuild_newline);
  749. }
  750. if(postbuild_total > 0)
  751. {
  752. customRuleCode += "\n";
  753. }
  754. customRuleCode += "# End Special Build Tool\n";
  755. return customRuleCode;
  756. }
  757. inline std::string removeQuotes(const std::string& s)
  758. {
  759. if(s[0] == '\"' && s[s.size()-1] == '\"')
  760. {
  761. return s.substr(1, s.size()-2);
  762. }
  763. return s;
  764. }
  765. // Code in blocks surrounded by a test for this definition is needed
  766. // only for compatibility with user project's replacement DSP
  767. // templates. The CMake templates no longer use them.
  768. #define CM_USE_OLD_VS6
  769. void cmLocalVisualStudio6Generator
  770. ::WriteDSPHeader(std::ostream& fout,
  771. const char *libName, cmTarget &target,
  772. std::vector<cmSourceGroup> &)
  773. {
  774. // Lookup the library and executable output directories.
  775. std::string libPath;
  776. if(this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  777. {
  778. libPath = this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  779. }
  780. std::string exePath;
  781. if(this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  782. {
  783. exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  784. }
  785. // Make sure there are trailing slashes.
  786. if(!libPath.empty())
  787. {
  788. if(libPath[libPath.size()-1] != '/')
  789. {
  790. libPath += "/";
  791. }
  792. }
  793. if(!exePath.empty())
  794. {
  795. if(exePath[exePath.size()-1] != '/')
  796. {
  797. exePath += "/";
  798. }
  799. }
  800. #ifdef CM_USE_OLD_VS6
  801. std::set<std::string> pathEmitted;
  802. // determine the link directories
  803. std::string libOptions;
  804. std::string libDebugOptions;
  805. std::string libOptimizedOptions;
  806. std::string libMultiLineOptions;
  807. std::string libMultiLineOptionsForDebug;
  808. std::string libMultiLineDebugOptions;
  809. std::string libMultiLineOptimizedOptions;
  810. if(libPath.size())
  811. {
  812. std::string lpath =
  813. this->ConvertToOptionallyRelativeOutputPath(libPath.c_str());
  814. if(lpath.size() == 0)
  815. {
  816. lpath = ".";
  817. }
  818. std::string lpathIntDir = libPath + "$(INTDIR)";
  819. lpathIntDir =
  820. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  821. if(pathEmitted.insert(lpath).second)
  822. {
  823. libOptions += " /LIBPATH:";
  824. libOptions += lpathIntDir;
  825. libOptions += " ";
  826. libOptions += " /LIBPATH:";
  827. libOptions += lpath;
  828. libOptions += " ";
  829. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  830. libMultiLineOptions += lpathIntDir;
  831. libMultiLineOptions += " ";
  832. libMultiLineOptions += " /LIBPATH:";
  833. libMultiLineOptions += lpath;
  834. libMultiLineOptions += " \n";
  835. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  836. libMultiLineOptionsForDebug += lpathIntDir;
  837. libMultiLineOptionsForDebug += " ";
  838. libMultiLineOptionsForDebug += " /LIBPATH:";
  839. libMultiLineOptionsForDebug += lpath;
  840. libMultiLineOptionsForDebug += " \n";
  841. }
  842. }
  843. if(exePath.size())
  844. {
  845. std::string lpath =
  846. this->ConvertToOptionallyRelativeOutputPath(exePath.c_str());
  847. if(lpath.size() == 0)
  848. {
  849. lpath = ".";
  850. }
  851. std::string lpathIntDir = exePath + "$(INTDIR)";
  852. lpathIntDir =
  853. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  854. if(pathEmitted.insert(lpath).second)
  855. {
  856. libOptions += " /LIBPATH:";
  857. libOptions += lpathIntDir;
  858. libOptions += " ";
  859. libOptions += " /LIBPATH:";
  860. libOptions += lpath;
  861. libOptions += " ";
  862. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  863. libMultiLineOptions += lpathIntDir;
  864. libMultiLineOptions += " ";
  865. libMultiLineOptions += " /LIBPATH:";
  866. libMultiLineOptions += lpath;
  867. libMultiLineOptions += " \n";
  868. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  869. libMultiLineOptionsForDebug += lpathIntDir;
  870. libMultiLineOptionsForDebug += " ";
  871. libMultiLineOptionsForDebug += " /LIBPATH:";
  872. libMultiLineOptionsForDebug += lpath;
  873. libMultiLineOptionsForDebug += " \n";
  874. }
  875. }
  876. std::vector<std::string>::const_iterator i;
  877. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  878. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  879. {
  880. std::string path = *i;
  881. if(path[path.size()-1] != '/')
  882. {
  883. path += "/";
  884. }
  885. std::string lpath =
  886. this->ConvertToOptionallyRelativeOutputPath(path.c_str());
  887. if(lpath.size() == 0)
  888. {
  889. lpath = ".";
  890. }
  891. std::string lpathIntDir = path + "$(INTDIR)";
  892. lpathIntDir =
  893. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  894. if(pathEmitted.insert(lpath).second)
  895. {
  896. libOptions += " /LIBPATH:";
  897. libOptions += lpathIntDir;
  898. libOptions += " ";
  899. libOptions += " /LIBPATH:";
  900. libOptions += lpath;
  901. libOptions += " ";
  902. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  903. libMultiLineOptions += lpathIntDir;
  904. libMultiLineOptions += " ";
  905. libMultiLineOptions += " /LIBPATH:";
  906. libMultiLineOptions += lpath;
  907. libMultiLineOptions += " \n";
  908. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  909. libMultiLineOptionsForDebug += lpathIntDir;
  910. libMultiLineOptionsForDebug += " ";
  911. libMultiLineOptionsForDebug += " /LIBPATH:";
  912. libMultiLineOptionsForDebug += lpath;
  913. libMultiLineOptionsForDebug += " \n";
  914. }
  915. }
  916. // find link libraries
  917. const cmTarget::LinkLibraryVectorType& libs = target.GetLinkLibraries();
  918. cmTarget::LinkLibraryVectorType::const_iterator j;
  919. for(j = libs.begin(); j != libs.end(); ++j)
  920. {
  921. // add libraries to executables and dlls (but never include
  922. // a library in a library, bad recursion)
  923. // NEVER LINK STATIC LIBRARIES TO OTHER STATIC LIBRARIES
  924. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  925. && target.GetType() != cmTarget::STATIC_LIBRARY
  926. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  927. (target.GetType()==cmTarget::SHARED_LIBRARY && libName != j->first) ||
  928. (target.GetType()==cmTarget::MODULE_LIBRARY && libName != j->first))
  929. {
  930. // Compute the proper name to use to link this library.
  931. std::string lib;
  932. std::string libDebug;
  933. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, j->first.c_str());
  934. if(tgt)
  935. {
  936. lib = cmSystemTools::GetFilenameWithoutExtension
  937. (tgt->GetFullName().c_str());
  938. libDebug = cmSystemTools::GetFilenameWithoutExtension
  939. (tgt->GetFullName("Debug").c_str());
  940. lib += ".lib";
  941. libDebug += ".lib";
  942. }
  943. else
  944. {
  945. lib = j->first.c_str();
  946. libDebug = j->first.c_str();
  947. if(j->first.find(".lib") == std::string::npos)
  948. {
  949. lib += ".lib";
  950. libDebug += ".lib";
  951. }
  952. }
  953. lib = this->ConvertToOptionallyRelativeOutputPath(lib.c_str());
  954. libDebug =
  955. this->ConvertToOptionallyRelativeOutputPath(libDebug.c_str());
  956. if (j->second == cmTarget::GENERAL)
  957. {
  958. libOptions += " ";
  959. libOptions += lib;
  960. libMultiLineOptions += "# ADD LINK32 ";
  961. libMultiLineOptions += lib;
  962. libMultiLineOptions += "\n";
  963. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  964. libMultiLineOptionsForDebug += libDebug;
  965. libMultiLineOptionsForDebug += "\n";
  966. }
  967. if (j->second == cmTarget::DEBUG)
  968. {
  969. libDebugOptions += " ";
  970. libDebugOptions += lib;
  971. libMultiLineDebugOptions += "# ADD LINK32 ";
  972. libMultiLineDebugOptions += libDebug;
  973. libMultiLineDebugOptions += "\n";
  974. }
  975. if (j->second == cmTarget::OPTIMIZED)
  976. {
  977. libOptimizedOptions += " ";
  978. libOptimizedOptions += lib;
  979. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  980. libMultiLineOptimizedOptions += lib;
  981. libMultiLineOptimizedOptions += "\n";
  982. }
  983. }
  984. }
  985. #endif
  986. // Get extra linker options for this target type.
  987. std::string extraLinkOptions;
  988. if(target.GetType() == cmTarget::EXECUTABLE)
  989. {
  990. extraLinkOptions =
  991. this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  992. }
  993. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  994. {
  995. extraLinkOptions =
  996. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  997. }
  998. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  999. {
  1000. extraLinkOptions =
  1001. this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1002. }
  1003. // Get extra linker options for this target.
  1004. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  1005. {
  1006. extraLinkOptions += " ";
  1007. extraLinkOptions += targetLinkFlags;
  1008. }
  1009. // Get standard libraries for this language.
  1010. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1011. target.GetType() <= cmTarget::MODULE_LIBRARY)
  1012. {
  1013. // Get the language to use for linking.
  1014. const char* linkLanguage =
  1015. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1016. if(!linkLanguage)
  1017. {
  1018. cmSystemTools::Error
  1019. ("CMake can not determine linker language for target:",
  1020. target.GetName());
  1021. return;
  1022. }
  1023. // Compute the variable name to lookup standard libraries for this
  1024. // language.
  1025. std::string standardLibsVar = "CMAKE_";
  1026. standardLibsVar += linkLanguage;
  1027. standardLibsVar += "_STANDARD_LIBRARIES";
  1028. // Add standard libraries.
  1029. if(const char* stdLibs =
  1030. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1031. {
  1032. extraLinkOptions += " ";
  1033. extraLinkOptions += stdLibs;
  1034. }
  1035. }
  1036. // Compute the real name of the target.
  1037. std::string outputName =
  1038. "(OUTPUT_NAME is for libraries and executables only)";
  1039. std::string outputNameDebug = outputName;
  1040. std::string outputNameRelease = outputName;
  1041. std::string outputNameMinSizeRel = outputName;
  1042. std::string outputNameRelWithDebInfo = outputName;
  1043. if(target.GetType() == cmTarget::EXECUTABLE ||
  1044. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1045. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1046. target.GetType() == cmTarget::MODULE_LIBRARY)
  1047. {
  1048. outputName = target.GetFullName();
  1049. outputNameDebug = target.GetFullName("Debug");
  1050. outputNameRelease = target.GetFullName("Release");
  1051. outputNameMinSizeRel = target.GetFullName("MinSizeRel");
  1052. outputNameRelWithDebInfo = target.GetFullName("RelWithDebInfo");
  1053. }
  1054. // Compute the proper link information for the target.
  1055. std::string optionsDebug;
  1056. std::string optionsRelease;
  1057. std::string optionsMinSizeRel;
  1058. std::string optionsRelWithDebInfo;
  1059. if(target.GetType() == cmTarget::EXECUTABLE ||
  1060. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1061. target.GetType() == cmTarget::MODULE_LIBRARY)
  1062. {
  1063. this->ComputeLinkOptions(target, "Debug", extraLinkOptions,
  1064. optionsDebug);
  1065. this->ComputeLinkOptions(target, "Release", extraLinkOptions,
  1066. optionsRelease);
  1067. this->ComputeLinkOptions(target, "MinSizeRel", extraLinkOptions,
  1068. optionsMinSizeRel);
  1069. this->ComputeLinkOptions(target, "RelWithDebInfo", extraLinkOptions,
  1070. optionsRelWithDebInfo);
  1071. }
  1072. #ifdef CM_USE_OLD_VS6
  1073. // Compute link information for the target.
  1074. if(extraLinkOptions.size())
  1075. {
  1076. libOptions += " ";
  1077. libOptions += extraLinkOptions;
  1078. libOptions += " ";
  1079. libMultiLineOptions += "# ADD LINK32 ";
  1080. libMultiLineOptions += extraLinkOptions;
  1081. libMultiLineOptions += " \n";
  1082. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1083. libMultiLineOptionsForDebug += extraLinkOptions;
  1084. libMultiLineOptionsForDebug += " \n";
  1085. }
  1086. #endif
  1087. // are there any custom rules on the target itself
  1088. // only if the target is a lib or exe
  1089. std::string customRuleCode = this->CreateTargetRules(target, libName);
  1090. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  1091. if(!fin)
  1092. {
  1093. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  1094. }
  1095. std::string staticLibOptions;
  1096. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1097. {
  1098. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  1099. {
  1100. staticLibOptions = libflags;
  1101. }
  1102. }
  1103. std::string exportSymbol;
  1104. if (const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  1105. {
  1106. exportSymbol = custom_export_name;
  1107. }
  1108. else
  1109. {
  1110. std::string in = libName;
  1111. in += "_EXPORTS";
  1112. exportSymbol = cmSystemTools::MakeCindentifier(in.c_str());
  1113. }
  1114. std::string line;
  1115. while(cmSystemTools::GetLineFromStream(fin, line))
  1116. {
  1117. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  1118. if(!mfcFlag)
  1119. {
  1120. mfcFlag = "0";
  1121. }
  1122. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  1123. exportSymbol.c_str());
  1124. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE",
  1125. customRuleCode.c_str());
  1126. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  1127. mfcFlag);
  1128. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1129. {
  1130. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS",
  1131. staticLibOptions.c_str());
  1132. }
  1133. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  1134. {
  1135. cmSystemTools::ReplaceString(line, "/nologo", "");
  1136. }
  1137. #ifdef CM_USE_OLD_VS6
  1138. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  1139. libOptions.c_str());
  1140. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  1141. libDebugOptions.c_str());
  1142. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  1143. libOptimizedOptions.c_str());
  1144. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES_FOR_DEBUG",
  1145. libMultiLineOptionsForDebug.c_str());
  1146. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  1147. libMultiLineOptions.c_str());
  1148. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  1149. libMultiLineDebugOptions.c_str());
  1150. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  1151. libMultiLineOptimizedOptions.c_str());
  1152. #endif
  1153. // Substitute the real output name into the template.
  1154. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_DEBUG",
  1155. outputNameDebug.c_str());
  1156. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELEASE",
  1157. outputNameRelease.c_str());
  1158. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_MINSIZEREL",
  1159. outputNameMinSizeRel.c_str());
  1160. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELWITHDEBINFO",
  1161. outputNameRelWithDebInfo.c_str());
  1162. cmSystemTools::ReplaceString(line, "OUTPUT_NAME", outputName.c_str());
  1163. // Substitute the proper link information into the template.
  1164. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_DEBUG",
  1165. optionsDebug.c_str());
  1166. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELEASE",
  1167. optionsRelease.c_str());
  1168. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_MINSIZEREL",
  1169. optionsMinSizeRel.c_str());
  1170. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELWITHDEBINFO",
  1171. optionsRelWithDebInfo.c_str());
  1172. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  1173. this->IncludeOptions.c_str());
  1174. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  1175. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  1176. // are already quoted in the template file,
  1177. // we need to remove the quotes here, we still need
  1178. // to convert to output path for unix to win32 conversion
  1179. cmSystemTools::ReplaceString
  1180. (line, "LIBRARY_OUTPUT_PATH",
  1181. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1182. (libPath.c_str())).c_str());
  1183. cmSystemTools::ReplaceString
  1184. (line, "EXECUTABLE_OUTPUT_PATH",
  1185. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1186. (exePath.c_str())).c_str());
  1187. cmSystemTools::ReplaceString(line,
  1188. "EXTRA_DEFINES",
  1189. this->Makefile->GetDefineFlags());
  1190. const char* debugPostfix
  1191. = this->Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  1192. cmSystemTools::ReplaceString(line, "DEBUG_POSTFIX",
  1193. debugPostfix?debugPostfix:"");
  1194. // store flags for each configuration
  1195. std::string flags = " ";
  1196. std::string flagsRelease = " ";
  1197. std::string flagsMinSize = " ";
  1198. std::string flagsDebug = " ";
  1199. std::string flagsDebugRel = " ";
  1200. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1201. target.GetType() <= cmTarget::MODULE_LIBRARY)
  1202. {
  1203. const char* linkLanguage =
  1204. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1205. if(!linkLanguage)
  1206. {
  1207. cmSystemTools::Error
  1208. ("CMake can not determine linker language for target:",
  1209. target.GetName());
  1210. return;
  1211. }
  1212. // if CXX is on and the target contains cxx code then add the cxx flags
  1213. std::string baseFlagVar = "CMAKE_";
  1214. baseFlagVar += linkLanguage;
  1215. baseFlagVar += "_FLAGS";
  1216. flags = this->Makefile->GetSafeDefinition(baseFlagVar.c_str());
  1217. std::string flagVar = baseFlagVar + "_RELEASE";
  1218. flagsRelease = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1219. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  1220. if(const char* targetLinkFlags =
  1221. target.GetProperty("LINK_FLAGS_RELEASE"))
  1222. {
  1223. flagsRelease += targetLinkFlags;
  1224. flagsRelease += " ";
  1225. }
  1226. flagVar = baseFlagVar + "_MINSIZEREL";
  1227. flagsMinSize = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1228. flagsMinSize += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  1229. if(const char* targetLinkFlags =
  1230. target.GetProperty("LINK_FLAGS_MINSIZEREL"))
  1231. {
  1232. flagsMinSize += targetLinkFlags;
  1233. flagsMinSize += " ";
  1234. }
  1235. flagVar = baseFlagVar + "_DEBUG";
  1236. flagsDebug = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1237. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  1238. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_DEBUG"))
  1239. {
  1240. flagsDebug += targetLinkFlags;
  1241. flagsDebug += " ";
  1242. }
  1243. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  1244. flagsDebugRel = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1245. flagsDebugRel += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  1246. if(const char* targetLinkFlags =
  1247. target.GetProperty("LINK_FLAGS_RELWITHDEBINFO"))
  1248. {
  1249. flagsDebugRel += targetLinkFlags;
  1250. flagsDebugRel += " ";
  1251. }
  1252. }
  1253. // if unicode is not found, then add -D_MBCS
  1254. std::string defs = this->Makefile->GetDefineFlags();
  1255. if(flags.find("D_UNICODE") == flags.npos &&
  1256. defs.find("D_UNICODE") == flags.npos)
  1257. {
  1258. flags += " /D \"_MBCS\"";
  1259. }
  1260. // Add per-target flags.
  1261. if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
  1262. {
  1263. flags += " ";
  1264. flags += targetFlags;
  1265. }
  1266. // The template files have CXX FLAGS in them, that need to be replaced.
  1267. // There are not separate CXX and C template files, so we use the same
  1268. // variable names. The previous code sets up flags* variables to contain
  1269. // the correct C or CXX flags
  1270. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL",
  1271. flagsMinSize.c_str());
  1272. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG",
  1273. flagsDebug.c_str());
  1274. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELWITHDEBINFO",
  1275. flagsDebugRel.c_str());
  1276. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE",
  1277. flagsRelease.c_str());
  1278. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  1279. fout << line.c_str() << std::endl;
  1280. }
  1281. }
  1282. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  1283. {
  1284. std::ifstream fin(this->DSPFooterTemplate.c_str());
  1285. if(!fin)
  1286. {
  1287. cmSystemTools::Error("Error Reading ",
  1288. this->DSPFooterTemplate.c_str());
  1289. }
  1290. std::string line;
  1291. while(cmSystemTools::GetLineFromStream(fin, line))
  1292. {
  1293. fout << line << std::endl;
  1294. }
  1295. }
  1296. //----------------------------------------------------------------------------
  1297. void cmLocalVisualStudio6Generator
  1298. ::ComputeLinkOptions(cmTarget& target,
  1299. const char* configName,
  1300. const std::string extraOptions,
  1301. std::string& options)
  1302. {
  1303. // Compute the link information for this configuration.
  1304. std::vector<cmStdString> linkLibs;
  1305. std::vector<cmStdString> linkDirs;
  1306. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  1307. // Build the link options code.
  1308. for(std::vector<cmStdString>::const_iterator d = linkDirs.begin();
  1309. d != linkDirs.end(); ++d)
  1310. {
  1311. std::string dir = *d;
  1312. if(!dir.empty())
  1313. {
  1314. if(dir[dir.size()-1] != '/')
  1315. {
  1316. dir += "/";
  1317. }
  1318. dir += "$(IntDir)";
  1319. options += "# ADD LINK32 /LIBPATH:";
  1320. options += this->ConvertToOptionallyRelativeOutputPath(dir.c_str());
  1321. options += " /LIBPATH:";
  1322. options += this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  1323. options += "\n";
  1324. }
  1325. }
  1326. for(std::vector<cmStdString>::const_iterator l = linkLibs.begin();
  1327. l != linkLibs.end(); ++l)
  1328. {
  1329. options += "# ADD LINK32 ";
  1330. options += this->ConvertToOptionallyRelativeOutputPath(l->c_str());
  1331. options += "\n";
  1332. }
  1333. // Add extra options if any.
  1334. if(!extraOptions.empty())
  1335. {
  1336. options += "# ADD LINK32 ";
  1337. options += extraOptions;
  1338. options += "\n";
  1339. }
  1340. }