cmLocalVisualStudio6Generator.cxx 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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->GetSourceFileLanguage(*(*sf));
  386. if(lang && strcmp(lang, "CXX") == 0)
  387. {
  388. // force a C++ file type
  389. compileFlags += " /TP ";
  390. }
  391. else if(strcmp(lang, "C") == 0)
  392. {
  393. // force to c file type
  394. compileFlags += " /TC ";
  395. }
  396. // Check for extra object-file dependencies.
  397. const char* dependsValue = (*sf)->GetProperty("OBJECT_DEPENDS");
  398. if(dependsValue)
  399. {
  400. cmSystemTools::ExpandListArgument(dependsValue, depends);
  401. }
  402. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  403. target.GetType() == cmTarget::GLOBAL_TARGET)
  404. {
  405. fout << "# Begin Source File\n\n";
  406. // Tell MS-Dev what the source is. If the compiler knows how to
  407. // build it, then it will.
  408. fout << "SOURCE=" <<
  409. this->ConvertToOptionallyRelativeOutputPath(source.c_str()) << "\n\n";
  410. if(!depends.empty())
  411. {
  412. // Write out the dependencies for the rule.
  413. fout << "USERDEP__HACK=";
  414. for(std::vector<std::string>::const_iterator d = depends.begin();
  415. d != depends.end(); ++d)
  416. {
  417. fout << "\\\n\t" <<
  418. this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  419. }
  420. fout << "\n";
  421. }
  422. if (command)
  423. {
  424. std::string script =
  425. this->ConstructScript(command->GetCommandLines(),
  426. command->GetWorkingDirectory(),
  427. command->GetEscapeOldStyle(),
  428. command->GetEscapeAllowMakeVars(),
  429. "\\\n\t");
  430. std::string comment =
  431. this->ConstructComment(*command,
  432. "Building Custom Rule $(InputPath)");
  433. if(comment == "<hack>")
  434. {
  435. comment = "";
  436. }
  437. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  438. this->WriteCustomRule(fout, source.c_str(), script.c_str(),
  439. comment.c_str(), command->GetDepends(),
  440. command->GetOutputs(), flags);
  441. }
  442. else if(!compileFlags.empty() || !objectNameDir.empty())
  443. {
  444. for(std::vector<std::string>::iterator i
  445. = this->Configurations.begin();
  446. i != this->Configurations.end(); ++i)
  447. {
  448. if (i == this->Configurations.begin())
  449. {
  450. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  451. }
  452. else
  453. {
  454. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  455. }
  456. if(!compileFlags.empty())
  457. {
  458. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  459. }
  460. if(!objectNameDir.empty())
  461. {
  462. // Strip the subdirectory name out of the configuration name.
  463. std::string config = *i;
  464. std::string::size_type pos = config.find_last_of(" ");
  465. config = config.substr(pos+1, std::string::npos);
  466. config = config.substr(0, config.size()-1);
  467. // Setup an alternate object file directory.
  468. fout << "\n# PROP Intermediate_Dir \""
  469. << config << "/" << objectNameDir << "\"\n\n";
  470. }
  471. }
  472. fout << "!ENDIF\n\n";
  473. }
  474. fout << "# End Source File\n";
  475. }
  476. }
  477. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  478. for(unsigned int i=0;i<children.size();++i)
  479. {
  480. this->WriteGroup(&children[i], target, fout, libName);
  481. }
  482. // If the group has a name, write the footer.
  483. if(name != "")
  484. {
  485. this->WriteDSPEndGroup(fout);
  486. }
  487. }
  488. void
  489. cmLocalVisualStudio6Generator
  490. ::AddUtilityCommandHack(cmTarget& target, int count,
  491. std::vector<std::string>& depends,
  492. const cmCustomCommand& origCommand)
  493. {
  494. // Create a fake output that forces the rule to run.
  495. char* output = new char[(strlen(this->Makefile->GetStartOutputDirectory()) +
  496. strlen(target.GetName()) + 30)];
  497. sprintf(output,"%s/%s_force_%i", this->Makefile->GetStartOutputDirectory(),
  498. target.GetName(), count);
  499. std::string comment = this->ConstructComment(origCommand, "<hack>");
  500. // Add the rule with the given dependencies and commands.
  501. const char* no_main_dependency = 0;
  502. this->Makefile->AddCustomCommandToOutput(output,
  503. depends,
  504. no_main_dependency,
  505. origCommand.GetCommandLines(),
  506. comment.c_str(),
  507. origCommand.GetWorkingDirectory());
  508. // Replace the dependencies with the output of this rule so that the
  509. // next rule added will run after this one.
  510. depends.clear();
  511. depends.push_back(output);
  512. // Add a source file representing this output to the project.
  513. cmSourceFile* outsf = this->Makefile->GetSourceFileWithOutput(output);
  514. target.GetSourceFiles().push_back(outsf);
  515. // Free the fake output name.
  516. delete [] output;
  517. }
  518. void
  519. cmLocalVisualStudio6Generator
  520. ::WriteCustomRule(std::ostream& fout,
  521. const char* source,
  522. const char* command,
  523. const char* comment,
  524. const std::vector<std::string>& depends,
  525. const std::vector<std::string>& outputs,
  526. const char* flags)
  527. {
  528. // Write the rule for each configuration.
  529. std::vector<std::string>::iterator i;
  530. for(i = this->Configurations.begin(); i != this->Configurations.end(); ++i)
  531. {
  532. if (i == this->Configurations.begin())
  533. {
  534. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  535. }
  536. else
  537. {
  538. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  539. }
  540. if(flags)
  541. {
  542. fout << "\n# ADD CPP " << flags << "\n\n";
  543. }
  544. // Write out the dependencies for the rule.
  545. fout << "USERDEP__HACK=";
  546. for(std::vector<std::string>::const_iterator d = depends.begin();
  547. d != depends.end(); ++d)
  548. {
  549. // Lookup the real name of the dependency in case it is a CMake target.
  550. std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
  551. fout << "\\\n\t" <<
  552. this->ConvertToOptionallyRelativeOutputPath(dep.c_str());
  553. }
  554. fout << "\n";
  555. fout << "# PROP Ignore_Default_Tool 1\n";
  556. fout << "# Begin Custom Build -";
  557. if(comment && *comment)
  558. {
  559. fout << " " << comment;
  560. }
  561. fout << "\n\n";
  562. if(outputs.empty())
  563. {
  564. fout << source
  565. << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  566. fout << command << "\n\n";
  567. }
  568. else
  569. {
  570. for(std::vector<std::string>::const_iterator o = outputs.begin();
  571. o != outputs.end(); ++o)
  572. {
  573. // Write a rule for every output generated by this command.
  574. fout << this->ConvertToOptionallyRelativeOutputPath(o->c_str())
  575. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  576. fout << command << "\n\n";
  577. }
  578. }
  579. fout << "# End Custom Build\n\n";
  580. }
  581. fout << "!ENDIF\n\n";
  582. }
  583. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  584. const char* group,
  585. const char* filter)
  586. {
  587. fout << "# Begin Group \"" << group << "\"\n"
  588. "# PROP Default_Filter \"" << filter << "\"\n";
  589. }
  590. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  591. {
  592. fout << "# End Group\n";
  593. }
  594. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b,
  595. const char* libName,
  596. cmTarget& target)
  597. {
  598. std::string root= this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  599. const char *def=
  600. this->Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  601. if( def)
  602. {
  603. root = def;
  604. }
  605. else
  606. {
  607. root += "/Templates";
  608. }
  609. switch(b)
  610. {
  611. case STATIC_LIBRARY:
  612. this->DSPHeaderTemplate = root;
  613. this->DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  614. this->DSPFooterTemplate = root;
  615. this->DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  616. break;
  617. case DLL:
  618. this->DSPHeaderTemplate = root;
  619. this->DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  620. this->DSPFooterTemplate = root;
  621. this->DSPFooterTemplate += "/DLLFooter.dsptemplate";
  622. break;
  623. case EXECUTABLE:
  624. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  625. {
  626. this->DSPHeaderTemplate = root;
  627. this->DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  628. this->DSPFooterTemplate = root;
  629. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  630. }
  631. else
  632. {
  633. this->DSPHeaderTemplate = root;
  634. this->DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  635. this->DSPFooterTemplate = root;
  636. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  637. }
  638. break;
  639. case UTILITY:
  640. this->DSPHeaderTemplate = root;
  641. this->DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  642. this->DSPFooterTemplate = root;
  643. this->DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  644. break;
  645. }
  646. // once the build type is set, determine what configurations are
  647. // possible
  648. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  649. cmsys::RegularExpression reg("# Name ");
  650. if(!fin)
  651. {
  652. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  653. }
  654. // reset this->Configurations
  655. this->Configurations.erase(this->Configurations.begin(),
  656. this->Configurations.end());
  657. // now add all the configurations possible
  658. std::string line;
  659. while(cmSystemTools::GetLineFromStream(fin, line))
  660. {
  661. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  662. if (reg.find(line))
  663. {
  664. this->Configurations.push_back(line.substr(reg.end()));
  665. }
  666. }
  667. }
  668. // look for custom rules on a target and collect them together
  669. std::string
  670. cmLocalVisualStudio6Generator::CreateTargetRules(cmTarget &target,
  671. const char * /* libName */)
  672. {
  673. std::string customRuleCode = "";
  674. if (target.GetType() >= cmTarget::UTILITY )
  675. {
  676. return customRuleCode;
  677. }
  678. // are there any rules?
  679. if (target.GetPreBuildCommands().size() +
  680. target.GetPreLinkCommands().size() +
  681. target.GetPostBuildCommands().size() == 0)
  682. {
  683. return customRuleCode;
  684. }
  685. customRuleCode = "# Begin Special Build Tool\n";
  686. // Write the pre-build and pre-link together (VS6 does not support
  687. // both). Make sure no continuation character is put on the last
  688. // line.
  689. int prelink_total = (static_cast<int>(target.GetPreBuildCommands().size())+
  690. static_cast<int>(target.GetPreLinkCommands().size()));
  691. int prelink_count = 0;
  692. if(prelink_total > 0)
  693. {
  694. // header stuff
  695. customRuleCode += "PreLink_Cmds=";
  696. }
  697. for (std::vector<cmCustomCommand>::const_iterator cr =
  698. target.GetPreBuildCommands().begin();
  699. cr != target.GetPreBuildCommands().end(); ++cr)
  700. {
  701. if(prelink_count++ > 0)
  702. {
  703. customRuleCode += "\\\n\t";
  704. }
  705. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  706. cr->GetWorkingDirectory(),
  707. cr->GetEscapeOldStyle(),
  708. cr->GetEscapeAllowMakeVars(),
  709. "\\\n\t");
  710. }
  711. for (std::vector<cmCustomCommand>::const_iterator cr =
  712. target.GetPreLinkCommands().begin();
  713. cr != target.GetPreLinkCommands().end(); ++cr)
  714. {
  715. if(prelink_count++ > 0)
  716. {
  717. customRuleCode += "\\\n\t";
  718. }
  719. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  720. cr->GetWorkingDirectory(),
  721. cr->GetEscapeOldStyle(),
  722. cr->GetEscapeAllowMakeVars(),
  723. "\\\n\t");
  724. }
  725. if(prelink_total > 0)
  726. {
  727. customRuleCode += "\n";
  728. }
  729. // Write the post-build rules. Make sure no continuation character
  730. // is put on the last line.
  731. int postbuild_total =
  732. static_cast<int>(target.GetPostBuildCommands().size());
  733. int postbuild_count = 0;
  734. if(postbuild_total > 0)
  735. {
  736. customRuleCode += "PostBuild_Cmds=";
  737. }
  738. for (std::vector<cmCustomCommand>::const_iterator cr =
  739. target.GetPostBuildCommands().begin();
  740. cr != target.GetPostBuildCommands().end(); ++cr)
  741. {
  742. if(postbuild_count++ > 0)
  743. {
  744. customRuleCode += "\\\n\t";
  745. }
  746. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  747. cr->GetWorkingDirectory(),
  748. cr->GetEscapeOldStyle(),
  749. cr->GetEscapeAllowMakeVars(),
  750. "\\\n\t");
  751. }
  752. if(postbuild_total > 0)
  753. {
  754. customRuleCode += "\n";
  755. }
  756. customRuleCode += "# End Special Build Tool\n";
  757. return customRuleCode;
  758. }
  759. inline std::string removeQuotes(const std::string& s)
  760. {
  761. if(s[0] == '\"' && s[s.size()-1] == '\"')
  762. {
  763. return s.substr(1, s.size()-2);
  764. }
  765. return s;
  766. }
  767. // Code in blocks surrounded by a test for this definition is needed
  768. // only for compatibility with user project's replacement DSP
  769. // templates. The CMake templates no longer use them.
  770. #define CM_USE_OLD_VS6
  771. void cmLocalVisualStudio6Generator
  772. ::WriteDSPHeader(std::ostream& fout,
  773. const char *libName, cmTarget &target,
  774. std::vector<cmSourceGroup> &)
  775. {
  776. // Lookup the library and executable output directories.
  777. std::string libPath;
  778. if(this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  779. {
  780. libPath = this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  781. }
  782. std::string exePath;
  783. if(this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  784. {
  785. exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  786. }
  787. // Make sure there are trailing slashes.
  788. if(!libPath.empty())
  789. {
  790. if(libPath[libPath.size()-1] != '/')
  791. {
  792. libPath += "/";
  793. }
  794. }
  795. if(!exePath.empty())
  796. {
  797. if(exePath[exePath.size()-1] != '/')
  798. {
  799. exePath += "/";
  800. }
  801. }
  802. #ifdef CM_USE_OLD_VS6
  803. std::set<std::string> pathEmitted;
  804. // determine the link directories
  805. std::string libOptions;
  806. std::string libDebugOptions;
  807. std::string libOptimizedOptions;
  808. std::string libMultiLineOptions;
  809. std::string libMultiLineOptionsForDebug;
  810. std::string libMultiLineDebugOptions;
  811. std::string libMultiLineOptimizedOptions;
  812. if(libPath.size())
  813. {
  814. std::string lpath =
  815. this->ConvertToOptionallyRelativeOutputPath(libPath.c_str());
  816. if(lpath.size() == 0)
  817. {
  818. lpath = ".";
  819. }
  820. std::string lpathIntDir = libPath + "$(INTDIR)";
  821. lpathIntDir =
  822. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  823. if(pathEmitted.insert(lpath).second)
  824. {
  825. libOptions += " /LIBPATH:";
  826. libOptions += lpathIntDir;
  827. libOptions += " ";
  828. libOptions += " /LIBPATH:";
  829. libOptions += lpath;
  830. libOptions += " ";
  831. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  832. libMultiLineOptions += lpathIntDir;
  833. libMultiLineOptions += " ";
  834. libMultiLineOptions += " /LIBPATH:";
  835. libMultiLineOptions += lpath;
  836. libMultiLineOptions += " \n";
  837. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  838. libMultiLineOptionsForDebug += lpathIntDir;
  839. libMultiLineOptionsForDebug += " ";
  840. libMultiLineOptionsForDebug += " /LIBPATH:";
  841. libMultiLineOptionsForDebug += lpath;
  842. libMultiLineOptionsForDebug += " \n";
  843. }
  844. }
  845. if(exePath.size())
  846. {
  847. std::string lpath =
  848. this->ConvertToOptionallyRelativeOutputPath(exePath.c_str());
  849. if(lpath.size() == 0)
  850. {
  851. lpath = ".";
  852. }
  853. std::string lpathIntDir = exePath + "$(INTDIR)";
  854. lpathIntDir =
  855. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  856. if(pathEmitted.insert(lpath).second)
  857. {
  858. libOptions += " /LIBPATH:";
  859. libOptions += lpathIntDir;
  860. libOptions += " ";
  861. libOptions += " /LIBPATH:";
  862. libOptions += lpath;
  863. libOptions += " ";
  864. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  865. libMultiLineOptions += lpathIntDir;
  866. libMultiLineOptions += " ";
  867. libMultiLineOptions += " /LIBPATH:";
  868. libMultiLineOptions += lpath;
  869. libMultiLineOptions += " \n";
  870. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  871. libMultiLineOptionsForDebug += lpathIntDir;
  872. libMultiLineOptionsForDebug += " ";
  873. libMultiLineOptionsForDebug += " /LIBPATH:";
  874. libMultiLineOptionsForDebug += lpath;
  875. libMultiLineOptionsForDebug += " \n";
  876. }
  877. }
  878. std::vector<std::string>::const_iterator i;
  879. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  880. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  881. {
  882. std::string path = *i;
  883. if(path[path.size()-1] != '/')
  884. {
  885. path += "/";
  886. }
  887. std::string lpath =
  888. this->ConvertToOptionallyRelativeOutputPath(path.c_str());
  889. if(lpath.size() == 0)
  890. {
  891. lpath = ".";
  892. }
  893. std::string lpathIntDir = path + "$(INTDIR)";
  894. lpathIntDir =
  895. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  896. if(pathEmitted.insert(lpath).second)
  897. {
  898. libOptions += " /LIBPATH:";
  899. libOptions += lpathIntDir;
  900. libOptions += " ";
  901. libOptions += " /LIBPATH:";
  902. libOptions += lpath;
  903. libOptions += " ";
  904. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  905. libMultiLineOptions += lpathIntDir;
  906. libMultiLineOptions += " ";
  907. libMultiLineOptions += " /LIBPATH:";
  908. libMultiLineOptions += lpath;
  909. libMultiLineOptions += " \n";
  910. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  911. libMultiLineOptionsForDebug += lpathIntDir;
  912. libMultiLineOptionsForDebug += " ";
  913. libMultiLineOptionsForDebug += " /LIBPATH:";
  914. libMultiLineOptionsForDebug += lpath;
  915. libMultiLineOptionsForDebug += " \n";
  916. }
  917. }
  918. // find link libraries
  919. const cmTarget::LinkLibraryVectorType& libs = target.GetLinkLibraries();
  920. cmTarget::LinkLibraryVectorType::const_iterator j;
  921. for(j = libs.begin(); j != libs.end(); ++j)
  922. {
  923. // add libraries to executables and dlls (but never include
  924. // a library in a library, bad recursion)
  925. // NEVER LINK STATIC LIBRARIES TO OTHER STATIC LIBRARIES
  926. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  927. && target.GetType() != cmTarget::STATIC_LIBRARY
  928. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  929. (target.GetType()==cmTarget::SHARED_LIBRARY && libName != j->first) ||
  930. (target.GetType()==cmTarget::MODULE_LIBRARY && libName != j->first))
  931. {
  932. // Compute the proper name to use to link this library.
  933. std::string lib;
  934. std::string libDebug;
  935. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, j->first.c_str());
  936. if(tgt)
  937. {
  938. lib = cmSystemTools::GetFilenameWithoutExtension
  939. (tgt->GetFullName().c_str());
  940. libDebug = cmSystemTools::GetFilenameWithoutExtension
  941. (tgt->GetFullName("Debug").c_str());
  942. lib += ".lib";
  943. libDebug += ".lib";
  944. }
  945. else
  946. {
  947. lib = j->first.c_str();
  948. libDebug = j->first.c_str();
  949. if(j->first.find(".lib") == std::string::npos)
  950. {
  951. lib += ".lib";
  952. libDebug += ".lib";
  953. }
  954. }
  955. lib = this->ConvertToOptionallyRelativeOutputPath(lib.c_str());
  956. libDebug =
  957. this->ConvertToOptionallyRelativeOutputPath(libDebug.c_str());
  958. if (j->second == cmTarget::GENERAL)
  959. {
  960. libOptions += " ";
  961. libOptions += lib;
  962. libMultiLineOptions += "# ADD LINK32 ";
  963. libMultiLineOptions += lib;
  964. libMultiLineOptions += "\n";
  965. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  966. libMultiLineOptionsForDebug += libDebug;
  967. libMultiLineOptionsForDebug += "\n";
  968. }
  969. if (j->second == cmTarget::DEBUG)
  970. {
  971. libDebugOptions += " ";
  972. libDebugOptions += lib;
  973. libMultiLineDebugOptions += "# ADD LINK32 ";
  974. libMultiLineDebugOptions += libDebug;
  975. libMultiLineDebugOptions += "\n";
  976. }
  977. if (j->second == cmTarget::OPTIMIZED)
  978. {
  979. libOptimizedOptions += " ";
  980. libOptimizedOptions += lib;
  981. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  982. libMultiLineOptimizedOptions += lib;
  983. libMultiLineOptimizedOptions += "\n";
  984. }
  985. }
  986. }
  987. #endif
  988. // Get extra linker options for this target type.
  989. std::string extraLinkOptions;
  990. if(target.GetType() == cmTarget::EXECUTABLE)
  991. {
  992. extraLinkOptions =
  993. this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  994. }
  995. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  996. {
  997. extraLinkOptions =
  998. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  999. }
  1000. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1001. {
  1002. extraLinkOptions =
  1003. this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1004. }
  1005. // Get extra linker options for this target.
  1006. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  1007. {
  1008. extraLinkOptions += " ";
  1009. extraLinkOptions += targetLinkFlags;
  1010. }
  1011. // Get standard libraries for this language.
  1012. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1013. target.GetType() <= cmTarget::MODULE_LIBRARY)
  1014. {
  1015. // Get the language to use for linking.
  1016. const char* linkLanguage =
  1017. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1018. if(!linkLanguage)
  1019. {
  1020. cmSystemTools::Error
  1021. ("CMake can not determine linker language for target:",
  1022. target.GetName());
  1023. return;
  1024. }
  1025. // Compute the variable name to lookup standard libraries for this
  1026. // language.
  1027. std::string standardLibsVar = "CMAKE_";
  1028. standardLibsVar += linkLanguage;
  1029. standardLibsVar += "_STANDARD_LIBRARIES";
  1030. // Add standard libraries.
  1031. if(const char* stdLibs =
  1032. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1033. {
  1034. extraLinkOptions += " ";
  1035. extraLinkOptions += stdLibs;
  1036. }
  1037. }
  1038. // Compute version number information.
  1039. std::string targetVersionFlag;
  1040. if(target.GetType() == cmTarget::EXECUTABLE ||
  1041. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1042. target.GetType() == cmTarget::MODULE_LIBRARY)
  1043. {
  1044. int major;
  1045. int minor;
  1046. target.GetTargetVersion(major, minor);
  1047. cmOStringStream targetVersionStream;
  1048. targetVersionStream << "/version:" << major << "." << minor;
  1049. targetVersionFlag = targetVersionStream.str();
  1050. }
  1051. // Compute the real name of the target.
  1052. std::string outputName =
  1053. "(OUTPUT_NAME is for libraries and executables only)";
  1054. std::string outputNameDebug = outputName;
  1055. std::string outputNameRelease = outputName;
  1056. std::string outputNameMinSizeRel = outputName;
  1057. std::string outputNameRelWithDebInfo = outputName;
  1058. if(target.GetType() == cmTarget::EXECUTABLE ||
  1059. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1060. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1061. target.GetType() == cmTarget::MODULE_LIBRARY)
  1062. {
  1063. outputName = target.GetFullName();
  1064. outputNameDebug = target.GetFullName("Debug");
  1065. outputNameRelease = target.GetFullName("Release");
  1066. outputNameMinSizeRel = target.GetFullName("MinSizeRel");
  1067. outputNameRelWithDebInfo = target.GetFullName("RelWithDebInfo");
  1068. }
  1069. // Compute the proper link information for the target.
  1070. std::string optionsDebug;
  1071. std::string optionsRelease;
  1072. std::string optionsMinSizeRel;
  1073. std::string optionsRelWithDebInfo;
  1074. if(target.GetType() == cmTarget::EXECUTABLE ||
  1075. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1076. target.GetType() == cmTarget::MODULE_LIBRARY)
  1077. {
  1078. this->ComputeLinkOptions(target, "Debug", extraLinkOptions,
  1079. optionsDebug);
  1080. this->ComputeLinkOptions(target, "Release", extraLinkOptions,
  1081. optionsRelease);
  1082. this->ComputeLinkOptions(target, "MinSizeRel", extraLinkOptions,
  1083. optionsMinSizeRel);
  1084. this->ComputeLinkOptions(target, "RelWithDebInfo", extraLinkOptions,
  1085. optionsRelWithDebInfo);
  1086. }
  1087. #ifdef CM_USE_OLD_VS6
  1088. // Compute link information for the target.
  1089. if(extraLinkOptions.size())
  1090. {
  1091. libOptions += " ";
  1092. libOptions += extraLinkOptions;
  1093. libOptions += " ";
  1094. libMultiLineOptions += "# ADD LINK32 ";
  1095. libMultiLineOptions += extraLinkOptions;
  1096. libMultiLineOptions += " \n";
  1097. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1098. libMultiLineOptionsForDebug += extraLinkOptions;
  1099. libMultiLineOptionsForDebug += " \n";
  1100. }
  1101. #endif
  1102. // are there any custom rules on the target itself
  1103. // only if the target is a lib or exe
  1104. std::string customRuleCode = this->CreateTargetRules(target, libName);
  1105. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  1106. if(!fin)
  1107. {
  1108. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  1109. }
  1110. std::string staticLibOptions;
  1111. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1112. {
  1113. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  1114. {
  1115. staticLibOptions = libflags;
  1116. }
  1117. }
  1118. std::string exportSymbol;
  1119. if (const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  1120. {
  1121. exportSymbol = custom_export_name;
  1122. }
  1123. else
  1124. {
  1125. std::string in = libName;
  1126. in += "_EXPORTS";
  1127. exportSymbol = cmSystemTools::MakeCindentifier(in.c_str());
  1128. }
  1129. std::string line;
  1130. while(cmSystemTools::GetLineFromStream(fin, line))
  1131. {
  1132. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  1133. if(!mfcFlag)
  1134. {
  1135. mfcFlag = "0";
  1136. }
  1137. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  1138. exportSymbol.c_str());
  1139. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE",
  1140. customRuleCode.c_str());
  1141. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  1142. mfcFlag);
  1143. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1144. {
  1145. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS",
  1146. staticLibOptions.c_str());
  1147. }
  1148. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  1149. {
  1150. cmSystemTools::ReplaceString(line, "/nologo", "");
  1151. }
  1152. #ifdef CM_USE_OLD_VS6
  1153. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  1154. libOptions.c_str());
  1155. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  1156. libDebugOptions.c_str());
  1157. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  1158. libOptimizedOptions.c_str());
  1159. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES_FOR_DEBUG",
  1160. libMultiLineOptionsForDebug.c_str());
  1161. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  1162. libMultiLineOptions.c_str());
  1163. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  1164. libMultiLineDebugOptions.c_str());
  1165. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  1166. libMultiLineOptimizedOptions.c_str());
  1167. #endif
  1168. // Substitute the real output name into the template.
  1169. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_DEBUG",
  1170. outputNameDebug.c_str());
  1171. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELEASE",
  1172. outputNameRelease.c_str());
  1173. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_MINSIZEREL",
  1174. outputNameMinSizeRel.c_str());
  1175. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELWITHDEBINFO",
  1176. outputNameRelWithDebInfo.c_str());
  1177. cmSystemTools::ReplaceString(line, "OUTPUT_NAME", outputName.c_str());
  1178. // Substitute the proper link information into the template.
  1179. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_DEBUG",
  1180. optionsDebug.c_str());
  1181. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELEASE",
  1182. optionsRelease.c_str());
  1183. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_MINSIZEREL",
  1184. optionsMinSizeRel.c_str());
  1185. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELWITHDEBINFO",
  1186. optionsRelWithDebInfo.c_str());
  1187. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  1188. this->IncludeOptions.c_str());
  1189. cmSystemTools::ReplaceString(line, "TARGET_VERSION_FLAG",
  1190. targetVersionFlag.c_str());
  1191. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  1192. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  1193. // are already quoted in the template file,
  1194. // we need to remove the quotes here, we still need
  1195. // to convert to output path for unix to win32 conversion
  1196. cmSystemTools::ReplaceString
  1197. (line, "LIBRARY_OUTPUT_PATH",
  1198. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1199. (libPath.c_str())).c_str());
  1200. cmSystemTools::ReplaceString
  1201. (line, "EXECUTABLE_OUTPUT_PATH",
  1202. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1203. (exePath.c_str())).c_str());
  1204. cmSystemTools::ReplaceString(line,
  1205. "EXTRA_DEFINES",
  1206. this->Makefile->GetDefineFlags());
  1207. const char* debugPostfix
  1208. = this->Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  1209. cmSystemTools::ReplaceString(line, "DEBUG_POSTFIX",
  1210. debugPostfix?debugPostfix:"");
  1211. // store flags for each configuration
  1212. std::string flags = " ";
  1213. std::string flagsRelease = " ";
  1214. std::string flagsMinSize = " ";
  1215. std::string flagsDebug = " ";
  1216. std::string flagsDebugRel = " ";
  1217. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1218. target.GetType() <= cmTarget::MODULE_LIBRARY)
  1219. {
  1220. const char* linkLanguage =
  1221. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1222. if(!linkLanguage)
  1223. {
  1224. cmSystemTools::Error
  1225. ("CMake can not determine linker language for target:",
  1226. target.GetName());
  1227. return;
  1228. }
  1229. // if CXX is on and the target contains cxx code then add the cxx flags
  1230. std::string baseFlagVar = "CMAKE_";
  1231. baseFlagVar += linkLanguage;
  1232. baseFlagVar += "_FLAGS";
  1233. flags = this->Makefile->GetSafeDefinition(baseFlagVar.c_str());
  1234. std::string flagVar = baseFlagVar + "_RELEASE";
  1235. flagsRelease = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1236. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  1237. if(const char* targetLinkFlags =
  1238. target.GetProperty("LINK_FLAGS_RELEASE"))
  1239. {
  1240. flagsRelease += targetLinkFlags;
  1241. flagsRelease += " ";
  1242. }
  1243. flagVar = baseFlagVar + "_MINSIZEREL";
  1244. flagsMinSize = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1245. flagsMinSize += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  1246. if(const char* targetLinkFlags =
  1247. target.GetProperty("LINK_FLAGS_MINSIZEREL"))
  1248. {
  1249. flagsMinSize += targetLinkFlags;
  1250. flagsMinSize += " ";
  1251. }
  1252. flagVar = baseFlagVar + "_DEBUG";
  1253. flagsDebug = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1254. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  1255. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_DEBUG"))
  1256. {
  1257. flagsDebug += targetLinkFlags;
  1258. flagsDebug += " ";
  1259. }
  1260. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  1261. flagsDebugRel = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1262. flagsDebugRel += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  1263. if(const char* targetLinkFlags =
  1264. target.GetProperty("LINK_FLAGS_RELWITHDEBINFO"))
  1265. {
  1266. flagsDebugRel += targetLinkFlags;
  1267. flagsDebugRel += " ";
  1268. }
  1269. }
  1270. // if unicode is not found, then add -D_MBCS
  1271. std::string defs = this->Makefile->GetDefineFlags();
  1272. if(flags.find("D_UNICODE") == flags.npos &&
  1273. defs.find("D_UNICODE") == flags.npos)
  1274. {
  1275. flags += " /D \"_MBCS\"";
  1276. }
  1277. // Add per-target flags.
  1278. if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
  1279. {
  1280. flags += " ";
  1281. flags += targetFlags;
  1282. }
  1283. // The template files have CXX FLAGS in them, that need to be replaced.
  1284. // There are not separate CXX and C template files, so we use the same
  1285. // variable names. The previous code sets up flags* variables to contain
  1286. // the correct C or CXX flags
  1287. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL",
  1288. flagsMinSize.c_str());
  1289. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG",
  1290. flagsDebug.c_str());
  1291. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELWITHDEBINFO",
  1292. flagsDebugRel.c_str());
  1293. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE",
  1294. flagsRelease.c_str());
  1295. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  1296. fout << line.c_str() << std::endl;
  1297. }
  1298. }
  1299. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  1300. {
  1301. std::ifstream fin(this->DSPFooterTemplate.c_str());
  1302. if(!fin)
  1303. {
  1304. cmSystemTools::Error("Error Reading ",
  1305. this->DSPFooterTemplate.c_str());
  1306. }
  1307. std::string line;
  1308. while(cmSystemTools::GetLineFromStream(fin, line))
  1309. {
  1310. fout << line << std::endl;
  1311. }
  1312. }
  1313. //----------------------------------------------------------------------------
  1314. void cmLocalVisualStudio6Generator
  1315. ::ComputeLinkOptions(cmTarget& target,
  1316. const char* configName,
  1317. const std::string extraOptions,
  1318. std::string& options)
  1319. {
  1320. // Compute the link information for this configuration.
  1321. std::vector<cmStdString> linkLibs;
  1322. std::vector<cmStdString> linkDirs;
  1323. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  1324. // Build the link options code.
  1325. for(std::vector<cmStdString>::const_iterator d = linkDirs.begin();
  1326. d != linkDirs.end(); ++d)
  1327. {
  1328. std::string dir = *d;
  1329. if(!dir.empty())
  1330. {
  1331. if(dir[dir.size()-1] != '/')
  1332. {
  1333. dir += "/";
  1334. }
  1335. dir += "$(IntDir)";
  1336. options += "# ADD LINK32 /LIBPATH:";
  1337. options += this->ConvertToOptionallyRelativeOutputPath(dir.c_str());
  1338. options += " /LIBPATH:";
  1339. options += this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  1340. options += "\n";
  1341. }
  1342. }
  1343. for(std::vector<cmStdString>::const_iterator l = linkLibs.begin();
  1344. l != linkLibs.end(); ++l)
  1345. {
  1346. options += "# ADD LINK32 ";
  1347. options += this->ConvertToOptionallyRelativeOutputPath(l->c_str());
  1348. options += "\n";
  1349. }
  1350. // Add extra options if any.
  1351. if(!extraOptions.empty())
  1352. {
  1353. options += "# ADD LINK32 ";
  1354. options += extraOptions;
  1355. options += "\n";
  1356. }
  1357. }
  1358. void cmLocalVisualStudio6Generator
  1359. ::GetTargetObjectFileDirectories(cmTarget* ,
  1360. std::vector<std::string>&
  1361. dirs)
  1362. {
  1363. std::string dir = this->Makefile->GetCurrentOutputDirectory();
  1364. dir += "/";
  1365. dir += this->GetGlobalGenerator()->GetCMakeCFGInitDirectory();
  1366. dirs.push_back(dir);
  1367. }