cmLocalVisualStudio7Generator.cxx 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmLocalVisualStudio7Generator.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cstring>
  7. #include <functional>
  8. #include <sstream>
  9. #include <utility>
  10. #include <cm/memory>
  11. #include <cm/optional>
  12. #include <cmext/algorithm>
  13. #include <windows.h>
  14. #include <cm3p/expat.h>
  15. #include "cmsys/FStream.hxx"
  16. #include "cmComputeLinkInformation.h"
  17. #include "cmCustomCommand.h"
  18. #include "cmCustomCommandGenerator.h"
  19. #include "cmCustomCommandLines.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include "cmGeneratorExpression.h"
  22. #include "cmGeneratorOptions.h"
  23. #include "cmGeneratorTarget.h"
  24. #include "cmGlobalGenerator.h"
  25. #include "cmGlobalVisualStudio7Generator.h"
  26. #include "cmGlobalVisualStudioGenerator.h"
  27. #include "cmList.h"
  28. #include "cmListFileCache.h"
  29. #include "cmMakefile.h"
  30. #include "cmMessageType.h"
  31. #include "cmOutputConverter.h"
  32. #include "cmPolicies.h"
  33. #include "cmSourceFile.h"
  34. #include "cmSourceGroup.h"
  35. #include "cmStateTypes.h"
  36. #include "cmStringAlgorithms.h"
  37. #include "cmSystemTools.h"
  38. #include "cmTarget.h"
  39. #include "cmTargetDepend.h"
  40. #include "cmValue.h"
  41. #include "cmVsProjectType.h"
  42. #include "cmXMLParser.h"
  43. #include "cmake.h"
  44. static bool cmLVS7G_IsFAT(const char* dir);
  45. class cmLocalVisualStudio7GeneratorInternals
  46. {
  47. public:
  48. cmLocalVisualStudio7GeneratorInternals(cmLocalVisualStudio7Generator* e)
  49. : LocalGenerator(e)
  50. {
  51. }
  52. using ItemVector = cmComputeLinkInformation::ItemVector;
  53. void OutputLibraries(std::ostream& fout, ItemVector const& libs);
  54. void OutputObjects(std::ostream& fout, cmGeneratorTarget* t,
  55. std::string const& config, const char* isep = nullptr);
  56. private:
  57. cmLocalVisualStudio7Generator* LocalGenerator;
  58. };
  59. class cmLocalVisualStudio7Generator::AllConfigSources
  60. {
  61. public:
  62. std::vector<cmGeneratorTarget::AllConfigSource> Sources;
  63. std::map<cmSourceFile const*, size_t> Index;
  64. };
  65. extern cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[];
  66. cmLocalVisualStudio7Generator::cmLocalVisualStudio7Generator(
  67. cmGlobalGenerator* gg, cmMakefile* mf)
  68. : cmLocalVisualStudioGenerator(gg, mf)
  69. , Internal(cm::make_unique<cmLocalVisualStudio7GeneratorInternals>(this))
  70. {
  71. }
  72. cmLocalVisualStudio7Generator::~cmLocalVisualStudio7Generator() = default;
  73. void cmLocalVisualStudio7Generator::AddHelperCommands()
  74. {
  75. // Now create GUIDs for targets
  76. const auto& tgts = this->GetGeneratorTargets();
  77. for (const auto& l : tgts) {
  78. if (!l->IsInBuildSystem()) {
  79. continue;
  80. }
  81. cmValue path = l->GetProperty("EXTERNAL_MSPROJECT");
  82. if (path) {
  83. this->ReadAndStoreExternalGUID(l->GetName(), path->c_str());
  84. }
  85. }
  86. this->FixGlobalTargets();
  87. }
  88. void cmLocalVisualStudio7Generator::Generate()
  89. {
  90. // Create the project file for each target.
  91. for (cmGeneratorTarget* gt :
  92. this->GlobalGenerator->GetLocalGeneratorTargetsInOrder(this)) {
  93. if (!gt->IsInBuildSystem() || gt->GetProperty("EXTERNAL_MSPROJECT")) {
  94. continue;
  95. }
  96. auto& gtVisited = this->GetSourcesVisited(gt);
  97. auto const& deps = this->GlobalGenerator->GetTargetDirectDepends(gt);
  98. for (auto const& d : deps) {
  99. // Take the union of visited source files of custom commands
  100. auto depVisited = this->GetSourcesVisited(d);
  101. gtVisited.insert(depVisited.begin(), depVisited.end());
  102. }
  103. this->GenerateTarget(gt);
  104. }
  105. this->WriteStampFiles();
  106. }
  107. void cmLocalVisualStudio7Generator::FixGlobalTargets()
  108. {
  109. // Visual Studio .NET 2003 Service Pack 1 will not run post-build
  110. // commands for targets in which no sources are built. Add dummy
  111. // rules to force these targets to build.
  112. const auto& tgts = this->GetGeneratorTargets();
  113. for (auto const& l : tgts) {
  114. if (l->GetType() == cmStateEnums::GLOBAL_TARGET) {
  115. cmCustomCommandLines force_commands =
  116. cmMakeSingleCommandLine({ "cd", "." });
  117. std::string force = cmStrCat(this->GetCurrentBinaryDirectory(),
  118. "/CMakeFiles/", l->GetName(), "_force");
  119. if (cmSourceFile* sf =
  120. this->Makefile->GetOrCreateGeneratedSource(force)) {
  121. sf->SetProperty("SYMBOLIC", "1");
  122. }
  123. auto cc = cm::make_unique<cmCustomCommand>();
  124. cc->SetOutputs(force);
  125. cc->SetCommandLines(force_commands);
  126. cc->SetComment(" ");
  127. if (cmSourceFile* file =
  128. this->AddCustomCommandToOutput(std::move(cc), true)) {
  129. l->AddSource(file->ResolveFullPath());
  130. }
  131. }
  132. }
  133. }
  134. void cmLocalVisualStudio7Generator::WriteStampFiles()
  135. {
  136. // Touch a timestamp file used to determine when the project file is
  137. // out of date.
  138. std::string stampName =
  139. cmStrCat(this->GetCurrentBinaryDirectory(), "/CMakeFiles");
  140. cmSystemTools::MakeDirectory(stampName);
  141. stampName += "/generate.stamp";
  142. cmsys::ofstream stamp(stampName.c_str());
  143. stamp << "# CMake generation timestamp file for this directory.\n";
  144. // Create a helper file so CMake can determine when it is run
  145. // through the rule created by CreateVCProjBuildRule whether it
  146. // really needs to regenerate the project. This file lists its own
  147. // dependencies. If any file listed in it is newer than itself then
  148. // CMake must rerun. Otherwise the project files are up to date and
  149. // the stamp file can just be touched.
  150. std::string depName = cmStrCat(stampName, ".depend");
  151. cmsys::ofstream depFile(depName.c_str());
  152. depFile << "# CMake generation dependency list for this directory.\n";
  153. std::vector<std::string> listFiles(this->Makefile->GetListFiles());
  154. cmake* cm = this->GlobalGenerator->GetCMakeInstance();
  155. if (cm->DoWriteGlobVerifyTarget()) {
  156. listFiles.push_back(cm->GetGlobVerifyStamp());
  157. }
  158. // Sort the list of input files and remove duplicates.
  159. std::sort(listFiles.begin(), listFiles.end(), std::less<std::string>());
  160. auto new_end = std::unique(listFiles.begin(), listFiles.end());
  161. listFiles.erase(new_end, listFiles.end());
  162. for (const std::string& lf : listFiles) {
  163. depFile << lf << '\n';
  164. }
  165. }
  166. void cmLocalVisualStudio7Generator::GenerateTarget(cmGeneratorTarget* target)
  167. {
  168. std::string const& lname = target->GetName();
  169. cmGlobalVisualStudioGenerator* gg =
  170. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  171. this->FortranProject = gg->TargetIsFortranOnly(target);
  172. this->WindowsCEProject = gg->TargetsWindowsCE();
  173. // add to the list of projects
  174. target->Target->SetProperty("GENERATOR_FILE_NAME", lname);
  175. // create the dsp.cmake file
  176. std::string fname;
  177. fname = cmStrCat(this->GetCurrentBinaryDirectory(), '/', lname);
  178. if (this->FortranProject) {
  179. fname += ".vfproj";
  180. } else {
  181. fname += ".vcproj";
  182. }
  183. // Generate the project file and replace it atomically with
  184. // copy-if-different. We use a separate timestamp so that the IDE
  185. // does not reload project files unnecessarily.
  186. cmGeneratedFileStream fout(fname);
  187. fout.SetCopyIfDifferent(true);
  188. this->WriteVCProjFile(fout, lname, target);
  189. if (fout.Close()) {
  190. this->GlobalGenerator->FileReplacedDuringGenerate(fname);
  191. }
  192. this->WindowsCEProject = false;
  193. this->FortranProject = false;
  194. }
  195. cmSourceFile* cmLocalVisualStudio7Generator::CreateVCProjBuildRule()
  196. {
  197. if (this->GlobalGenerator->GlobalSettingIsOn(
  198. "CMAKE_SUPPRESS_REGENERATION")) {
  199. return nullptr;
  200. }
  201. std::string makefileIn =
  202. cmStrCat(this->GetCurrentSourceDirectory(), "/CMakeLists.txt");
  203. if (cmSourceFile* file = this->Makefile->GetSource(makefileIn)) {
  204. if (file->GetCustomCommand()) {
  205. return file;
  206. }
  207. }
  208. if (!cmSystemTools::FileExists(makefileIn)) {
  209. return nullptr;
  210. }
  211. std::vector<std::string> listFiles = this->Makefile->GetListFiles();
  212. cmake* cm = this->GlobalGenerator->GetCMakeInstance();
  213. if (cm->DoWriteGlobVerifyTarget()) {
  214. listFiles.push_back(cm->GetGlobVerifyStamp());
  215. }
  216. // Sort the list of input files and remove duplicates.
  217. std::sort(listFiles.begin(), listFiles.end(), std::less<std::string>());
  218. auto new_end = std::unique(listFiles.begin(), listFiles.end());
  219. listFiles.erase(new_end, listFiles.end());
  220. std::string argS = cmStrCat("-S", this->GetSourceDirectory());
  221. std::string argB = cmStrCat("-B", this->GetBinaryDirectory());
  222. std::string stampName =
  223. cmStrCat(this->GetCurrentBinaryDirectory(), "/CMakeFiles/generate.stamp");
  224. cmCustomCommandLines commandLines =
  225. cmMakeSingleCommandLine({ cmSystemTools::GetCMakeCommand(), argS, argB,
  226. "--check-stamp-file", stampName });
  227. if (cm->GetIgnoreCompileWarningAsError()) {
  228. commandLines[0].emplace_back("--compile-no-warning-as-error");
  229. }
  230. if (cm->GetIgnoreLinkWarningAsError()) {
  231. commandLines[0].emplace_back("--link-no-warning-as-error");
  232. }
  233. std::string comment = cmStrCat("Building Custom Rule ", makefileIn);
  234. auto cc = cm::make_unique<cmCustomCommand>();
  235. cc->SetOutputs(stampName);
  236. cc->SetMainDependency(makefileIn);
  237. cc->SetDepends(listFiles);
  238. cc->SetCommandLines(commandLines);
  239. cc->SetComment(comment.c_str());
  240. cc->SetEscapeOldStyle(false);
  241. cc->SetStdPipesUTF8(true);
  242. cc->SetUsesTerminal(true);
  243. this->AddCustomCommandToOutput(std::move(cc), true);
  244. if (cmSourceFile* file = this->Makefile->GetSource(makefileIn)) {
  245. // Finalize the source file path now since we're adding this after
  246. // the generator validated all project-named sources.
  247. file->ResolveFullPath();
  248. return file;
  249. }
  250. cmSystemTools::Error(cmStrCat("Error adding rule for ", makefileIn));
  251. return nullptr;
  252. }
  253. void cmLocalVisualStudio7Generator::WriteConfigurations(
  254. std::ostream& fout, std::vector<std::string> const& configs,
  255. const std::string& libName, cmGeneratorTarget* target)
  256. {
  257. fout << "\t<Configurations>\n";
  258. for (std::string const& config : configs) {
  259. this->WriteConfiguration(fout, config, libName, target);
  260. }
  261. fout << "\t</Configurations>\n";
  262. }
  263. cmVS7FlagTable cmLocalVisualStudio7GeneratorFortranFlagTable[] = {
  264. { "Preprocess", "fpp", "Run Preprocessor on files", "preprocessYes", 0 },
  265. { "Preprocess", "nofpp", "Run Preprocessor on files", "preprocessNo", 0 },
  266. { "SuppressStartupBanner", "nologo", "SuppressStartupBanner", "true", 0 },
  267. { "SourceFileFormat", "fixed", "Use Fixed Format", "fileFormatFixed", 0 },
  268. { "SourceFileFormat", "free", "Use Free Format", "fileFormatFree", 0 },
  269. { "DebugInformationFormat", "debug:full", "full debug", "debugEnabled", 0 },
  270. { "DebugInformationFormat", "debug:minimal", "line numbers",
  271. "debugLineInfoOnly", 0 },
  272. { "Optimization", "Od", "disable optimization", "optimizeDisabled", 0 },
  273. { "Optimization", "O1", "min space", "optimizeMinSpace", 0 },
  274. { "Optimization", "O3", "full optimize", "optimizeFull", 0 },
  275. { "GlobalOptimizations", "Og", "global optimize", "true", 0 },
  276. { "InterproceduralOptimizations", "Qipo",
  277. "Interprocedural optimization across multiple files", "ipoMultiFile", 0 },
  278. { "InlineFunctionExpansion", "Ob0", "", "expandDisable", 0 },
  279. { "InlineFunctionExpansion", "Ob1", "", "expandOnlyInline", 0 },
  280. { "FavorSizeOrSpeed", "Os", "", "favorSize", 0 },
  281. { "OmitFramePointers", "Oy-", "", "false", 0 },
  282. { "OptimizeForProcessor", "GB", "", "procOptimizeBlended", 0 },
  283. { "OptimizeForProcessor", "G5", "", "procOptimizePentium", 0 },
  284. { "OptimizeForProcessor", "G6", "", "procOptimizePentiumProThruIII", 0 },
  285. { "UseProcessorExtensions", "QzxK", "", "codeForStreamingSIMD", 0 },
  286. { "OptimizeForProcessor", "QaxN", "", "codeForPentium4", 0 },
  287. { "OptimizeForProcessor", "QaxB", "", "codeForPentiumM", 0 },
  288. { "OptimizeForProcessor", "QaxP", "", "codeForCodeNamedPrescott", 0 },
  289. { "OptimizeForProcessor", "QaxT", "", "codeForCore2Duo", 0 },
  290. { "OptimizeForProcessor", "QxK", "", "codeExclusivelyStreamingSIMD", 0 },
  291. { "OptimizeForProcessor", "QxN", "", "codeExclusivelyPentium4", 0 },
  292. { "OptimizeForProcessor", "QxB", "", "codeExclusivelyPentiumM", 0 },
  293. { "OptimizeForProcessor", "QxP", "", "codeExclusivelyCodeNamedPrescott", 0 },
  294. { "OptimizeForProcessor", "QxT", "", "codeExclusivelyCore2Duo", 0 },
  295. { "OptimizeForProcessor", "QxO", "", "codeExclusivelyCore2StreamingSIMD",
  296. 0 },
  297. { "OptimizeForProcessor", "QxS", "", "codeExclusivelyCore2StreamingSIMD4",
  298. 0 },
  299. { "OpenMP", "Qopenmp", "", "OpenMPParallelCode", 0 },
  300. { "OpenMP", "Qopenmp-stubs", "", "OpenMPSequentialCode", 0 },
  301. { "Traceback", "traceback", "", "true", 0 },
  302. { "Traceback", "notraceback", "", "false", 0 },
  303. { "FloatingPointExceptionHandling", "fpe:0", "", "fpe0", 0 },
  304. { "FloatingPointExceptionHandling", "fpe:1", "", "fpe1", 0 },
  305. { "FloatingPointExceptionHandling", "fpe:3", "", "fpe3", 0 },
  306. { "MultiProcessorCompilation", "MP", "", "true",
  307. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue },
  308. { "ProcessorNumber", "MP", "Multi-processor Compilation", "",
  309. cmVS7FlagTable::UserValueRequired },
  310. { "ModulePath", "module:", "", "", cmVS7FlagTable::UserValueRequired },
  311. { "LoopUnrolling", "Qunroll:", "", "", cmVS7FlagTable::UserValueRequired },
  312. { "AutoParallelThreshold", "Qpar-threshold:", "", "",
  313. cmVS7FlagTable::UserValueRequired },
  314. { "HeapArrays", "heap-arrays:", "", "", cmVS7FlagTable::UserValueRequired },
  315. { "ObjectText", "bintext:", "", "", cmVS7FlagTable::UserValueRequired },
  316. { "Parallelization", "Qparallel", "", "true", 0 },
  317. { "PrefetchInsertion", "Qprefetch-", "", "false", 0 },
  318. { "BufferedIO", "assume:buffered_io", "", "true", 0 },
  319. { "CallingConvention", "iface:stdcall", "", "callConventionStdCall", 0 },
  320. { "CallingConvention", "iface:cref", "", "callConventionCRef", 0 },
  321. { "CallingConvention", "iface:stdref", "", "callConventionStdRef", 0 },
  322. { "CallingConvention", "iface:stdcall", "", "callConventionStdCall", 0 },
  323. { "CallingConvention", "iface:cvf", "", "callConventionCVF", 0 },
  324. { "EnableRecursion", "recursive", "", "true", 0 },
  325. { "ReentrantCode", "reentrancy", "", "true", 0 },
  326. // done up to Language
  327. { "", "", "", "", 0 }
  328. };
  329. // fill the table here currently the comment field is not used for
  330. // anything other than documentation NOTE: Make sure the longer
  331. // commandFlag comes FIRST!
  332. cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[] = {
  333. // option flags (some flags map to the same option)
  334. { "BasicRuntimeChecks", "GZ", "Stack frame checks", "1", 0 },
  335. { "BasicRuntimeChecks", "RTCsu", "Both stack and uninitialized checks", "3",
  336. 0 },
  337. { "BasicRuntimeChecks", "RTCs", "Stack frame checks", "1", 0 },
  338. { "BasicRuntimeChecks", "RTCu", "Uninitialized Variables ", "2", 0 },
  339. { "BasicRuntimeChecks", "RTC1", "Both stack and uninitialized checks", "3",
  340. 0 },
  341. { "DebugInformationFormat", "Z7", "debug format", "1", 0 },
  342. { "DebugInformationFormat", "Zd", "debug format", "2", 0 },
  343. { "DebugInformationFormat", "Zi", "debug format", "3", 0 },
  344. { "DebugInformationFormat", "ZI", "debug format", "4", 0 },
  345. { "EnableEnhancedInstructionSet", "arch:SSE2", "Use sse2 instructions", "2",
  346. 0 },
  347. { "EnableEnhancedInstructionSet", "arch:SSE", "Use sse instructions", "1",
  348. 0 },
  349. { "FloatingPointModel", "fp:precise", "Use precise floating point model",
  350. "0", 0 },
  351. { "FloatingPointModel", "fp:strict", "Use strict floating point model", "1",
  352. 0 },
  353. { "FloatingPointModel", "fp:fast", "Use fast floating point model", "2", 0 },
  354. { "FavorSizeOrSpeed", "Ot", "Favor fast code", "1", 0 },
  355. { "FavorSizeOrSpeed", "Os", "Favor small code", "2", 0 },
  356. { "CompileAs", "TC", "Compile as c code", "1", 0 },
  357. { "CompileAs", "TP", "Compile as c++ code", "2", 0 },
  358. { "Optimization", "Od", "Non Debug", "0", 0 },
  359. { "Optimization", "O1", "Min Size", "1", 0 },
  360. { "Optimization", "O2", "Max Speed", "2", 0 },
  361. { "Optimization", "Ox", "Max Optimization", "3", 0 },
  362. { "OptimizeForProcessor", "GB", "Blended processor mode", "0", 0 },
  363. { "OptimizeForProcessor", "G5", "Pentium", "1", 0 },
  364. { "OptimizeForProcessor", "G6", "PPro PII PIII", "2", 0 },
  365. { "OptimizeForProcessor", "G7", "Pentium 4 or Athlon", "3", 0 },
  366. { "InlineFunctionExpansion", "Ob0", "no inlines", "0", 0 },
  367. { "InlineFunctionExpansion", "Ob1", "when inline keyword", "1", 0 },
  368. { "InlineFunctionExpansion", "Ob2", "any time you can inline", "2", 0 },
  369. { "RuntimeLibrary", "MTd", "Multithreaded debug", "1", 0 },
  370. { "RuntimeLibrary", "MT", "Multithreaded", "0", 0 },
  371. { "RuntimeLibrary", "MDd", "Multithreaded dll debug", "3", 0 },
  372. { "RuntimeLibrary", "MD", "Multithreaded dll", "2", 0 },
  373. { "RuntimeLibrary", "MLd", "Single Thread debug", "5", 0 },
  374. { "RuntimeLibrary", "ML", "Single Thread", "4", 0 },
  375. { "StructMemberAlignment", "Zp16", "struct align 16 byte ", "5", 0 },
  376. { "StructMemberAlignment", "Zp1", "struct align 1 byte ", "1", 0 },
  377. { "StructMemberAlignment", "Zp2", "struct align 2 byte ", "2", 0 },
  378. { "StructMemberAlignment", "Zp4", "struct align 4 byte ", "3", 0 },
  379. { "StructMemberAlignment", "Zp8", "struct align 8 byte ", "4", 0 },
  380. { "WarningLevel", "W0", "Warning level", "0", 0 },
  381. { "WarningLevel", "W1", "Warning level", "1", 0 },
  382. { "WarningLevel", "W2", "Warning level", "2", 0 },
  383. { "WarningLevel", "W3", "Warning level", "3", 0 },
  384. { "WarningLevel", "W4", "Warning level", "4", 0 },
  385. { "DisableSpecificWarnings", "wd", "Disable specific warnings", "",
  386. cmVS7FlagTable::UserValue | cmVS7FlagTable::SemicolonAppendable },
  387. // Precompiled header and related options. Note that the
  388. // UsePrecompiledHeader entries are marked as "Continue" so that the
  389. // corresponding PrecompiledHeaderThrough entry can be found.
  390. { "UsePrecompiledHeader", "Yc", "Create Precompiled Header", "1",
  391. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue },
  392. { "PrecompiledHeaderThrough", "Yc", "Precompiled Header Name", "",
  393. cmVS7FlagTable::UserValueRequired },
  394. { "UsePrecompiledHeader", "Y-", "Don't use precompiled header", "0", 0 },
  395. { "PrecompiledHeaderFile", "Fp", "Generated Precompiled Header", "",
  396. cmVS7FlagTable::UserValue },
  397. // The YX and Yu options are in a per-global-generator table because
  398. // their values differ based on the VS IDE version.
  399. { "ForcedIncludeFiles", "FI", "Forced include files", "",
  400. cmVS7FlagTable::UserValueRequired | cmVS7FlagTable::SemicolonAppendable },
  401. { "AssemblerListingLocation", "Fa", "ASM List Location", "",
  402. cmVS7FlagTable::UserValue },
  403. { "ProgramDataBaseFileName", "Fd", "Program Database File Name", "",
  404. cmVS7FlagTable::UserValue },
  405. // boolean flags
  406. { "BufferSecurityCheck", "GS", "Buffer security check", "true", 0 },
  407. { "BufferSecurityCheck", "GS-", "Turn off Buffer security check", "false",
  408. 0 },
  409. { "Detect64BitPortabilityProblems", "Wp64",
  410. "Detect 64-bit Portability Problems", "true", 0 },
  411. { "EnableFiberSafeOptimizations", "GT", "Enable Fiber-safe Optimizations",
  412. "true", 0 },
  413. { "EnableFunctionLevelLinking", "Gy", "EnableFunctionLevelLinking", "true",
  414. 0 },
  415. { "EnableIntrinsicFunctions", "Oi", "EnableIntrinsicFunctions", "true", 0 },
  416. { "GlobalOptimizations", "Og", "Global Optimize", "true", 0 },
  417. { "ImproveFloatingPointConsistency", "Op", "ImproveFloatingPointConsistency",
  418. "true", 0 },
  419. { "MinimalRebuild", "Gm", "minimal rebuild", "true", 0 },
  420. { "OmitFramePointers", "Oy", "OmitFramePointers", "true", 0 },
  421. { "OptimizeForWindowsApplication", "GA", "Optimize for windows", "true", 0 },
  422. { "RuntimeTypeInfo", "GR", "Turn on Run time type information for c++",
  423. "true", 0 },
  424. { "RuntimeTypeInfo", "GR-", "Turn off Run time type information for c++",
  425. "false", 0 },
  426. { "SmallerTypeCheck", "RTCc", "smaller type check", "true", 0 },
  427. { "SuppressStartupBanner", "nologo", "SuppressStartupBanner", "true", 0 },
  428. { "WholeProgramOptimization", "GL", "Enables whole program optimization",
  429. "true", 0 },
  430. { "WholeProgramOptimization", "GL-", "Disables whole program optimization",
  431. "false", 0 },
  432. { "WarnAsError", "WX", "Treat warnings as errors", "true", 0 },
  433. { "BrowseInformation", "FR", "Generate browse information", "1", 0 },
  434. { "StringPooling", "GF", "Enable StringPooling", "true", 0 },
  435. { "", "", "", "", 0 }
  436. };
  437. cmVS7FlagTable cmLocalVisualStudio7GeneratorLinkFlagTable[] = {
  438. // option flags (some flags map to the same option)
  439. { "GenerateManifest", "MANIFEST:NO", "disable manifest generation", "false",
  440. 0 },
  441. { "GenerateManifest", "MANIFEST", "enable manifest generation", "true", 0 },
  442. { "LinkIncremental", "INCREMENTAL:NO", "link incremental", "1", 0 },
  443. { "LinkIncremental", "INCREMENTAL:YES", "link incremental", "2", 0 },
  444. { "CLRUnmanagedCodeCheck", "CLRUNMANAGEDCODECHECK:NO", "", "false", 0 },
  445. { "CLRUnmanagedCodeCheck", "CLRUNMANAGEDCODECHECK", "", "true", 0 },
  446. { "DataExecutionPrevention", "NXCOMPAT:NO",
  447. "Not known to work with Windows Data Execution Prevention", "1", 0 },
  448. { "DataExecutionPrevention", "NXCOMPAT",
  449. "Known to work with Windows Data Execution Prevention", "2", 0 },
  450. { "DelaySign", "DELAYSIGN:NO", "", "false", 0 },
  451. { "DelaySign", "DELAYSIGN", "", "true", 0 },
  452. { "EntryPointSymbol", "ENTRY:", "sets the starting address", "",
  453. cmVS7FlagTable::UserValue },
  454. { "IgnoreDefaultLibraryNames", "NODEFAULTLIB:", "default libs to ignore", "",
  455. cmVS7FlagTable::UserValue | cmVS7FlagTable::SemicolonAppendable },
  456. { "IgnoreAllDefaultLibraries", "NODEFAULTLIB", "ignore all default libs",
  457. "true", 0 },
  458. { "FixedBaseAddress", "FIXED:NO", "Generate a relocation section", "1", 0 },
  459. { "FixedBaseAddress", "FIXED", "Image must be loaded at a fixed address",
  460. "2", 0 },
  461. { "EnableCOMDATFolding", "OPT:NOICF", "Do not remove redundant COMDATs", "1",
  462. 0 },
  463. { "EnableCOMDATFolding", "OPT:ICF", "Remove redundant COMDATs", "2", 0 },
  464. { "ResourceOnlyDLL", "NOENTRY", "Create DLL with no entry point", "true",
  465. 0 },
  466. { "OptimizeReferences", "OPT:NOREF", "Keep unreferenced data", "1", 0 },
  467. { "OptimizeReferences", "OPT:REF", "Eliminate unreferenced data", "2", 0 },
  468. { "Profile", "PROFILE", "", "true", 0 },
  469. { "RandomizedBaseAddress", "DYNAMICBASE:NO",
  470. "Image may not be rebased at load-time", "1", 0 },
  471. { "RandomizedBaseAddress", "DYNAMICBASE",
  472. "Image may be rebased at load-time", "2", 0 },
  473. { "SetChecksum", "RELEASE", "Enable setting checksum in header", "true", 0 },
  474. { "SupportUnloadOfDelayLoadedDLL", "DELAY:UNLOAD", "", "true", 0 },
  475. { "TargetMachine", "MACHINE:I386", "Machine x86", "1", 0 },
  476. { "TargetMachine", "MACHINE:X86", "Machine x86", "1", 0 },
  477. { "TargetMachine", "MACHINE:AM33", "Machine AM33", "2", 0 },
  478. { "TargetMachine", "MACHINE:ARM", "Machine ARM", "3", 0 },
  479. { "TargetMachine", "MACHINE:EBC", "Machine EBC", "4", 0 },
  480. { "TargetMachine", "MACHINE:IA64", "Machine IA64", "5", 0 },
  481. { "TargetMachine", "MACHINE:M32R", "Machine M32R", "6", 0 },
  482. { "TargetMachine", "MACHINE:MIPS", "Machine MIPS", "7", 0 },
  483. { "TargetMachine", "MACHINE:MIPS16", "Machine MIPS16", "8", 0 },
  484. { "TargetMachine", "MACHINE:MIPSFPU)", "Machine MIPSFPU", "9", 0 },
  485. { "TargetMachine", "MACHINE:MIPSFPU16", "Machine MIPSFPU16", "10", 0 },
  486. { "TargetMachine", "MACHINE:MIPSR41XX", "Machine MIPSR41XX", "11", 0 },
  487. { "TargetMachine", "MACHINE:SH3", "Machine SH3", "12", 0 },
  488. { "TargetMachine", "MACHINE:SH3DSP", "Machine SH3DSP", "13", 0 },
  489. { "TargetMachine", "MACHINE:SH4", "Machine SH4", "14", 0 },
  490. { "TargetMachine", "MACHINE:SH5", "Machine SH5", "15", 0 },
  491. { "TargetMachine", "MACHINE:THUMB", "Machine THUMB", "16", 0 },
  492. { "TargetMachine", "MACHINE:X64", "Machine x64", "17", 0 },
  493. { "TargetMachine", "MACHINE:ARM64", "Machine ARM64", "18", 0 },
  494. { "TurnOffAssemblyGeneration", "NOASSEMBLY",
  495. "No assembly even if CLR information is present in objects.", "true", 0 },
  496. { "ModuleDefinitionFile", "DEF:", "add an export def file", "",
  497. cmVS7FlagTable::UserValue },
  498. { "GenerateMapFile", "MAP", "enable generation of map file", "true", 0 },
  499. { "", "", "", "", 0 }
  500. };
  501. cmVS7FlagTable cmLocalVisualStudio7GeneratorFortranLinkFlagTable[] = {
  502. { "LinkIncremental", "INCREMENTAL:NO", "link incremental",
  503. "linkIncrementalNo", 0 },
  504. { "LinkIncremental", "INCREMENTAL:YES", "link incremental",
  505. "linkIncrementalYes", 0 },
  506. { "EnableCOMDATFolding", "OPT:NOICF", "Do not remove redundant COMDATs",
  507. "optNoFolding", 0 },
  508. { "EnableCOMDATFolding", "OPT:ICF", "Remove redundant COMDATs", "optFolding",
  509. 0 },
  510. { "OptimizeReferences", "OPT:NOREF", "Keep unreferenced data",
  511. "optNoReferences", 0 },
  512. { "OptimizeReferences", "OPT:REF", "Eliminate unreferenced data",
  513. "optReferences", 0 },
  514. { "", "", "", "", 0 }
  515. };
  516. // Helper class to write build event <Tool .../> elements.
  517. class cmLocalVisualStudio7Generator::EventWriter
  518. {
  519. public:
  520. EventWriter(cmLocalVisualStudio7Generator* lg, std::string config,
  521. std::ostream& os)
  522. : LG(lg)
  523. , Config(std::move(config))
  524. , Stream(os)
  525. {
  526. }
  527. void Start(const char* tool)
  528. {
  529. this->First = true;
  530. this->Stream << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << '"';
  531. }
  532. void Finish()
  533. {
  534. // If any commands were generated, finish constructing them.
  535. if (!this->First) {
  536. std::string finishScript =
  537. this->LG->FinishConstructScript(VsProjectType::vcxproj);
  538. this->Stream << this->LG->EscapeForXML(finishScript) << '"';
  539. }
  540. this->Stream << "/>\n";
  541. }
  542. void Write(std::vector<cmCustomCommand> const& ccs)
  543. {
  544. for (cmCustomCommand const& command : ccs) {
  545. this->Write(command);
  546. }
  547. }
  548. void Write(cmCustomCommand const& cc)
  549. {
  550. cmCustomCommandGenerator ccg(cc, this->Config, this->LG);
  551. if (this->First) {
  552. if (cm::optional<std::string> comment = ccg.GetComment()) {
  553. this->Stream << "\nDescription=\"" << this->LG->EscapeForXML(*comment)
  554. << "\"";
  555. }
  556. this->Stream << "\nCommandLine=\"";
  557. this->First = false;
  558. } else {
  559. this->Stream << this->LG->EscapeForXML("\n");
  560. }
  561. std::string script = this->LG->ConstructScript(ccg);
  562. this->Stream << this->LG->EscapeForXML(script);
  563. }
  564. private:
  565. cmLocalVisualStudio7Generator* LG;
  566. std::string Config;
  567. std::ostream& Stream;
  568. bool First = true;
  569. };
  570. void cmLocalVisualStudio7Generator::WriteConfiguration(
  571. std::ostream& fout, const std::string& configName,
  572. const std::string& libName, cmGeneratorTarget* target)
  573. {
  574. std::string mfcFlag;
  575. if (cmValue p = this->Makefile->GetDefinition("CMAKE_MFC_FLAG")) {
  576. mfcFlag = cmGeneratorExpression::Evaluate(*p, this, configName);
  577. } else {
  578. mfcFlag = "0";
  579. }
  580. cmGlobalVisualStudio7Generator* gg =
  581. static_cast<cmGlobalVisualStudio7Generator*>(this->GlobalGenerator);
  582. fout << "\t\t<Configuration\n"
  583. "\t\t\tName=\""
  584. << configName << '|' << gg->GetPlatformName() << "\"\n";
  585. // This is an internal type to Visual Studio, it seems that:
  586. // 4 == static library
  587. // 2 == dll
  588. // 1 == executable
  589. // 10 == utility
  590. const char* configType = "10";
  591. const char* projectType = nullptr;
  592. bool targetBuilds = true;
  593. switch (target->GetType()) {
  594. case cmStateEnums::OBJECT_LIBRARY:
  595. targetBuilds = false; // no manifest tool for object library
  596. CM_FALLTHROUGH;
  597. case cmStateEnums::STATIC_LIBRARY:
  598. projectType = "typeStaticLibrary";
  599. configType = "4";
  600. break;
  601. case cmStateEnums::SHARED_LIBRARY:
  602. case cmStateEnums::MODULE_LIBRARY:
  603. projectType = "typeDynamicLibrary";
  604. configType = "2";
  605. break;
  606. case cmStateEnums::EXECUTABLE:
  607. configType = "1";
  608. break;
  609. case cmStateEnums::UTILITY:
  610. case cmStateEnums::GLOBAL_TARGET:
  611. case cmStateEnums::INTERFACE_LIBRARY:
  612. configType = "10";
  613. CM_FALLTHROUGH;
  614. case cmStateEnums::UNKNOWN_LIBRARY:
  615. targetBuilds = false;
  616. break;
  617. }
  618. if (this->FortranProject && projectType) {
  619. configType = projectType;
  620. }
  621. std::string flags;
  622. std::string langForClCompile;
  623. const std::string& linkLanguage =
  624. (this->FortranProject ? std::string("Fortran")
  625. : target->GetLinkerLanguage(configName));
  626. if (linkLanguage.empty()) {
  627. cmSystemTools::Error(
  628. cmStrCat("CMake can not determine linker language for target: ",
  629. target->GetName()));
  630. return;
  631. }
  632. if (target->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
  633. langForClCompile = linkLanguage;
  634. if (langForClCompile == "C" || langForClCompile == "CXX" ||
  635. langForClCompile == "Fortran") {
  636. this->AddLanguageFlags(flags, target, cmBuildStep::Compile,
  637. langForClCompile, configName);
  638. }
  639. // set the correct language
  640. if (linkLanguage == "C") {
  641. flags += " /TC ";
  642. }
  643. if (linkLanguage == "CXX") {
  644. flags += " /TP ";
  645. }
  646. // Add the target-specific flags.
  647. this->AddCompileOptions(flags, target, langForClCompile, configName);
  648. // Check IPO related warning/error.
  649. if (target->IsIPOEnabled(linkLanguage, configName)) {
  650. if (this->FortranProject) {
  651. this->AppendCompileOptions(flags,
  652. this->Makefile->GetSafeDefinition(
  653. "CMAKE_Fortran_COMPILE_OPTIONS_IPO"));
  654. }
  655. }
  656. }
  657. if (this->FortranProject) {
  658. switch (cmOutputConverter::GetFortranFormat(
  659. target->GetSafeProperty("Fortran_FORMAT"))) {
  660. case cmOutputConverter::FortranFormatFixed:
  661. flags += " -fixed";
  662. break;
  663. case cmOutputConverter::FortranFormatFree:
  664. flags += " -free";
  665. break;
  666. default:
  667. break;
  668. }
  669. switch (cmOutputConverter::GetFortranPreprocess(
  670. target->GetSafeProperty("Fortran_PREPROCESS"))) {
  671. case cmOutputConverter::FortranPreprocess::Needed:
  672. flags += " -fpp";
  673. break;
  674. case cmOutputConverter::FortranPreprocess::NotNeeded:
  675. flags += " -nofpp";
  676. break;
  677. default:
  678. break;
  679. }
  680. }
  681. // Get preprocessor definitions for this directory.
  682. std::string defineFlags = this->Makefile->GetDefineFlags();
  683. Options::Tool t = Options::Compiler;
  684. cmVS7FlagTable const* table = cmLocalVisualStudio7GeneratorFlagTable;
  685. if (this->FortranProject) {
  686. t = Options::FortranCompiler;
  687. table = cmLocalVisualStudio7GeneratorFortranFlagTable;
  688. }
  689. Options targetOptions(this, t, table, gg->ExtraFlagTable);
  690. targetOptions.FixExceptionHandlingDefault();
  691. targetOptions.AddFlag("AssemblerListingLocation", "$(IntDir)\\");
  692. targetOptions.Parse(flags);
  693. targetOptions.Parse(defineFlags);
  694. targetOptions.ParseFinish();
  695. if (!langForClCompile.empty()) {
  696. std::vector<std::string> targetDefines;
  697. target->GetCompileDefinitions(targetDefines, configName, langForClCompile);
  698. targetOptions.AddDefines(targetDefines);
  699. std::vector<std::string> targetIncludes;
  700. this->GetIncludeDirectories(targetIncludes, target, langForClCompile,
  701. configName);
  702. targetOptions.AddIncludes(targetIncludes);
  703. }
  704. targetOptions.SetVerboseMakefile(
  705. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  706. // Add a definition for the configuration name.
  707. std::string configDefine = cmStrCat("CMAKE_INTDIR=\"", configName, '"');
  708. targetOptions.AddDefine(configDefine);
  709. // Add the export symbol definition for shared library objects.
  710. if (const std::string* exportMacro = target->GetExportMacro()) {
  711. targetOptions.AddDefine(*exportMacro);
  712. }
  713. // The intermediate directory name consists of a directory for the
  714. // target and a subdirectory for the configuration name.
  715. std::string intermediateDir =
  716. cmStrCat(this->GetTargetDirectory(target), '/', configName);
  717. if (target->GetType() < cmStateEnums::UTILITY) {
  718. std::string const& outDir =
  719. target->GetType() == cmStateEnums::OBJECT_LIBRARY
  720. ? intermediateDir
  721. : target->GetDirectory(configName);
  722. fout << "\t\t\tOutputDirectory=\""
  723. << this->ConvertToXMLOutputPathSingle(outDir) << "\"\n";
  724. }
  725. fout << "\t\t\tIntermediateDirectory=\""
  726. << this->ConvertToXMLOutputPath(intermediateDir)
  727. << "\"\n"
  728. "\t\t\tConfigurationType=\""
  729. << configType
  730. << "\"\n"
  731. "\t\t\tUseOfMFC=\""
  732. << mfcFlag
  733. << "\"\n"
  734. "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"false\"\n";
  735. if (this->FortranProject) {
  736. // Intel Fortran >= 15.0 uses TargetName property.
  737. std::string const targetNameFull = target->GetFullName(configName);
  738. std::string const targetName =
  739. cmSystemTools::GetFilenameWithoutLastExtension(targetNameFull);
  740. std::string const targetExt =
  741. target->GetType() == cmStateEnums::OBJECT_LIBRARY
  742. ? ".lib"
  743. : cmSystemTools::GetFilenameLastExtension(targetNameFull);
  744. if (cm::optional<std::string> fortran = gg->GetPlatformToolsetFortran()) {
  745. fout << "\t\t\tUseCompiler=\"" << *fortran << "Compiler\"\n";
  746. }
  747. /* clang-format off */
  748. fout <<
  749. "\t\t\tTargetName=\"" << this->EscapeForXML(targetName) << "\"\n"
  750. "\t\t\tTargetExt=\"" << this->EscapeForXML(targetExt) << "\"\n"
  751. ;
  752. /* clang-format on */
  753. }
  754. // If unicode is enabled change the character set to unicode, if not
  755. // then default to MBCS.
  756. if (targetOptions.UsingUnicode()) {
  757. fout << "\t\t\tCharacterSet=\"1\">\n";
  758. } else if (targetOptions.UsingSBCS()) {
  759. fout << "\t\t\tCharacterSet=\"0\">\n";
  760. } else {
  761. fout << "\t\t\tCharacterSet=\"2\">\n";
  762. }
  763. const char* tool = "VCCLCompilerTool";
  764. if (this->FortranProject) {
  765. tool = "VFFortranCompilerTool";
  766. }
  767. fout << "\t\t\t<Tool\n"
  768. "\t\t\t\tName=\""
  769. << tool << "\"\n";
  770. if (this->FortranProject) {
  771. cmValue target_mod_dir = target->GetProperty("Fortran_MODULE_DIRECTORY");
  772. std::string modDir;
  773. if (target_mod_dir) {
  774. modDir = this->MaybeRelativeToCurBinDir(*target_mod_dir);
  775. } else {
  776. modDir = ".";
  777. }
  778. fout << "\t\t\t\tModulePath=\"" << this->ConvertToXMLOutputPath(modDir)
  779. << "\\$(ConfigurationName)\"\n";
  780. }
  781. targetOptions.OutputAdditionalIncludeDirectories(
  782. fout, 4, this->FortranProject ? "Fortran" : langForClCompile);
  783. targetOptions.OutputFlagMap(fout, 4);
  784. targetOptions.OutputPreprocessorDefinitions(fout, 4, langForClCompile);
  785. fout << "\t\t\t\tObjectFile=\"$(IntDir)\\\"\n";
  786. if (target->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
  787. // Specify the compiler program database file if configured.
  788. std::string pdb = target->GetCompilePDBPath(configName);
  789. if (!pdb.empty()) {
  790. fout << "\t\t\t\tProgramDataBaseFileName=\""
  791. << this->ConvertToXMLOutputPathSingle(pdb) << "\"\n";
  792. }
  793. }
  794. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  795. if (gg->IsMarmasmEnabled() && !this->FortranProject) {
  796. Options marmasmOptions(this, Options::MarmasmCompiler, nullptr, nullptr);
  797. fout << "\t\t\t<Tool\n"
  798. "\t\t\t\tName=\"MARMASM\"\n";
  799. targetOptions.OutputAdditionalIncludeDirectories(fout, 4, "ASM_MARMASM");
  800. // Use same preprocessor definitions as VCCLCompilerTool.
  801. targetOptions.OutputPreprocessorDefinitions(fout, 4, "ASM_MARMASM");
  802. marmasmOptions.OutputFlagMap(fout, 4);
  803. fout << "\t\t\t\tObjectFile=\"$(IntDir)\\\"\n"
  804. "\t\t\t/>\n";
  805. }
  806. if (gg->IsMasmEnabled() && !this->FortranProject) {
  807. Options masmOptions(this, Options::MasmCompiler, nullptr, nullptr);
  808. fout << "\t\t\t<Tool\n"
  809. "\t\t\t\tName=\"MASM\"\n";
  810. targetOptions.OutputAdditionalIncludeDirectories(fout, 4, "ASM_MASM");
  811. // Use same preprocessor definitions as VCCLCompilerTool.
  812. targetOptions.OutputPreprocessorDefinitions(fout, 4, "ASM_MASM");
  813. masmOptions.OutputFlagMap(fout, 4);
  814. fout << "\t\t\t\tObjectFile=\"$(IntDir)\\\"\n"
  815. "\t\t\t/>\n";
  816. }
  817. tool = "VCCustomBuildTool";
  818. if (this->FortranProject) {
  819. tool = "VFCustomBuildTool";
  820. }
  821. fout << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << "\"/>\n";
  822. tool = "VCResourceCompilerTool";
  823. if (this->FortranProject) {
  824. tool = "VFResourceCompilerTool";
  825. }
  826. fout << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << "\"\n";
  827. targetOptions.OutputAdditionalIncludeDirectories(fout, 4, "RC");
  828. // add the -D flags to the RC tool
  829. targetOptions.OutputPreprocessorDefinitions(fout, 4, "RC");
  830. fout << "\t\t\t/>\n";
  831. tool = "VCMIDLTool";
  832. if (this->FortranProject) {
  833. tool = "VFMIDLTool";
  834. }
  835. fout << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << "\"\n";
  836. targetOptions.OutputAdditionalIncludeDirectories(fout, 4, "MIDL");
  837. fout << "\t\t\t\tMkTypLibCompatible=\"false\"\n";
  838. if (gg->GetPlatformName() == "x64") {
  839. fout << "\t\t\t\tTargetEnvironment=\"3\"\n";
  840. } else if (gg->GetPlatformName() == "ia64") {
  841. fout << "\t\t\t\tTargetEnvironment=\"2\"\n";
  842. } else {
  843. fout << "\t\t\t\tTargetEnvironment=\"1\"\n";
  844. }
  845. fout << "\t\t\t\tGenerateStublessProxies=\"true\"\n"
  846. "\t\t\t\tTypeLibraryName=\"$(InputName).tlb\"\n"
  847. "\t\t\t\tOutputDirectory=\"$(IntDir)\"\n"
  848. "\t\t\t\tHeaderFileName=\"$(InputName).h\"\n"
  849. "\t\t\t\tDLLDataFileName=\"\"\n"
  850. "\t\t\t\tInterfaceIdentifierFileName=\"$(InputName)_i.c\"\n"
  851. "\t\t\t\tProxyFileName=\"$(InputName)_p.c\"/>\n";
  852. // end of <Tool Name=VCMIDLTool
  853. // Add manifest tool settings.
  854. if (targetBuilds) {
  855. const char* manifestTool = "VCManifestTool";
  856. if (this->FortranProject) {
  857. manifestTool = "VFManifestTool";
  858. }
  859. fout << "\t\t\t<Tool\n"
  860. "\t\t\t\tName=\""
  861. << manifestTool << '"';
  862. std::vector<cmSourceFile const*> manifest_srcs;
  863. target->GetManifests(manifest_srcs, configName);
  864. if (!manifest_srcs.empty()) {
  865. fout << "\n\t\t\t\tAdditionalManifestFiles=\"";
  866. for (cmSourceFile const* manifest : manifest_srcs) {
  867. std::string m = manifest->GetFullPath();
  868. fout << this->ConvertToXMLOutputPath(m) << ';';
  869. }
  870. fout << '"';
  871. }
  872. // Check if we need the FAT32 workaround.
  873. // Check the filesystem type where the target will be written.
  874. if (cmLVS7G_IsFAT(target->GetDirectory(configName).c_str())) {
  875. // Add a flag telling the manifest tool to use a workaround
  876. // for FAT32 file systems, which can cause an empty manifest
  877. // to be embedded into the resulting executable. See CMake
  878. // bug #2617.
  879. fout << "\n\t\t\t\tUseFAT32Workaround=\"true\"";
  880. }
  881. fout << "/>\n";
  882. }
  883. this->OutputTargetRules(fout, configName, target, libName);
  884. this->OutputBuildTool(fout, linkLanguage, configName, target, targetOptions);
  885. this->OutputDeploymentDebuggerTool(fout, configName, target);
  886. fout << "\t\t</Configuration>\n";
  887. }
  888. void cmLocalVisualStudio7Generator::OutputBuildTool(
  889. std::ostream& fout, const std::string& linkLanguage,
  890. const std::string& configName, cmGeneratorTarget* target,
  891. const Options& targetOptions)
  892. {
  893. cmGlobalVisualStudio7Generator* gg =
  894. static_cast<cmGlobalVisualStudio7Generator*>(this->GlobalGenerator);
  895. std::string temp;
  896. std::string extraLinkOptions;
  897. if (target->GetType() == cmStateEnums::EXECUTABLE) {
  898. this->AddConfigVariableFlags(extraLinkOptions, "CMAKE_EXE_LINKER_FLAGS",
  899. target, cmBuildStep::Link, linkLanguage,
  900. configName);
  901. }
  902. if (target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  903. this->AddConfigVariableFlags(extraLinkOptions, "CMAKE_SHARED_LINKER_FLAGS",
  904. target, cmBuildStep::Link, linkLanguage,
  905. configName);
  906. }
  907. if (target->GetType() == cmStateEnums::MODULE_LIBRARY) {
  908. this->AddConfigVariableFlags(extraLinkOptions, "CMAKE_MODULE_LINKER_FLAGS",
  909. target, cmBuildStep::Link, linkLanguage,
  910. configName);
  911. }
  912. cmValue targetLinkFlags = target->GetProperty("LINK_FLAGS");
  913. if (targetLinkFlags) {
  914. extraLinkOptions += ' ';
  915. extraLinkOptions += *targetLinkFlags;
  916. }
  917. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  918. std::string linkFlagsConfig = cmStrCat("LINK_FLAGS_", configTypeUpper);
  919. targetLinkFlags = target->GetProperty(linkFlagsConfig);
  920. if (targetLinkFlags) {
  921. extraLinkOptions += ' ';
  922. extraLinkOptions += *targetLinkFlags;
  923. }
  924. std::vector<std::string> opts;
  925. target->GetLinkOptions(opts, configName,
  926. target->GetLinkerLanguage(configName));
  927. // LINK_OPTIONS are escaped.
  928. this->AppendCompileOptions(extraLinkOptions, opts);
  929. this->AppendWarningAsErrorLinkerFlags(extraLinkOptions, target,
  930. linkLanguage);
  931. Options linkOptions(this, Options::Linker);
  932. if (this->FortranProject) {
  933. linkOptions.AddTable(cmLocalVisualStudio7GeneratorFortranLinkFlagTable);
  934. }
  935. linkOptions.AddTable(cmLocalVisualStudio7GeneratorLinkFlagTable);
  936. linkOptions.Parse(extraLinkOptions);
  937. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  938. target->GetModuleDefinitionInfo(configName);
  939. if (mdi && !mdi->DefFile.empty()) {
  940. std::string defFile =
  941. this->ConvertToOutputFormat(mdi->DefFile, cmOutputConverter::SHELL);
  942. linkOptions.AddFlag("ModuleDefinitionFile", defFile);
  943. }
  944. switch (target->GetType()) {
  945. case cmStateEnums::UNKNOWN_LIBRARY:
  946. break;
  947. case cmStateEnums::OBJECT_LIBRARY: {
  948. std::string libpath =
  949. cmStrCat(this->GetTargetDirectory(target), '/', configName, '/',
  950. target->GetName(), ".lib");
  951. const char* tool =
  952. this->FortranProject ? "VFLibrarianTool" : "VCLibrarianTool";
  953. fout << "\t\t\t<Tool\n"
  954. "\t\t\t\tName=\""
  955. << tool
  956. << "\"\n"
  957. "\t\t\t\tOutputFile=\""
  958. << this->ConvertToXMLOutputPathSingle(libpath) << "\"/>\n";
  959. break;
  960. }
  961. case cmStateEnums::STATIC_LIBRARY: {
  962. std::string targetNameFull = target->GetFullName(configName);
  963. std::string libpath =
  964. cmStrCat(target->GetDirectory(configName), '/', targetNameFull);
  965. const char* tool = "VCLibrarianTool";
  966. if (this->FortranProject) {
  967. tool = "VFLibrarianTool";
  968. }
  969. fout << "\t\t\t<Tool\n"
  970. "\t\t\t\tName=\""
  971. << tool << "\"\n";
  972. if (this->FortranProject) {
  973. std::ostringstream libdeps;
  974. this->Internal->OutputObjects(libdeps, target, configName);
  975. if (!libdeps.str().empty()) {
  976. fout << "\t\t\t\tAdditionalDependencies=\"" << libdeps.str()
  977. << "\"\n";
  978. }
  979. }
  980. std::string libflags;
  981. this->GetStaticLibraryFlags(
  982. libflags, configName, target->GetLinkerLanguage(configName), target);
  983. if (!libflags.empty()) {
  984. fout << "\t\t\t\tAdditionalOptions=\"" << this->EscapeForXML(libflags)
  985. << "\"\n";
  986. }
  987. fout << "\t\t\t\tOutputFile=\""
  988. << this->ConvertToXMLOutputPathSingle(libpath) << "\"/>\n";
  989. break;
  990. }
  991. case cmStateEnums::SHARED_LIBRARY:
  992. case cmStateEnums::MODULE_LIBRARY: {
  993. cmGeneratorTarget::Names targetNames =
  994. target->GetLibraryNames(configName);
  995. // Compute the link library and directory information.
  996. cmComputeLinkInformation* pcli = target->GetLinkInformation(configName);
  997. if (!pcli) {
  998. return;
  999. }
  1000. cmComputeLinkInformation& cli = *pcli;
  1001. if (!target->GetLinkerTypeProperty(linkLanguage, configName).empty()) {
  1002. // Visual Studio 10 or upper is required for this feature
  1003. this->GetCMakeInstance()->IssueMessage(
  1004. MessageType::FATAL_ERROR,
  1005. cmStrCat("'LINKER_TYPE' property, specified on target '",
  1006. target->GetName(),
  1007. "', is not supported by this generator."),
  1008. target->GetBacktrace());
  1009. }
  1010. // Compute the variable name to lookup standard libraries for this
  1011. // language.
  1012. std::string standardLibsVar =
  1013. cmStrCat("CMAKE_", linkLanguage, "_STANDARD_LIBRARIES");
  1014. const char* tool = "VCLinkerTool";
  1015. if (this->FortranProject) {
  1016. tool = "VFLinkerTool";
  1017. }
  1018. fout << "\t\t\t<Tool\n"
  1019. "\t\t\t\tName=\""
  1020. << tool << "\"\n";
  1021. if (!gg->NeedLinkLibraryDependencies(target)) {
  1022. fout << "\t\t\t\tLinkLibraryDependencies=\"false\"\n";
  1023. }
  1024. // Use the NOINHERIT macro to avoid getting VS project default
  1025. // libraries which may be set by the user to something bad.
  1026. fout << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  1027. << this->Makefile->GetSafeDefinition(standardLibsVar);
  1028. if (this->FortranProject) {
  1029. this->Internal->OutputObjects(fout, target, configName, " ");
  1030. }
  1031. fout << ' ';
  1032. this->Internal->OutputLibraries(fout, cli.GetItems());
  1033. fout << "\"\n";
  1034. temp =
  1035. cmStrCat(target->GetDirectory(configName), '/', targetNames.Output);
  1036. fout << "\t\t\t\tOutputFile=\""
  1037. << this->ConvertToXMLOutputPathSingle(temp) << "\"\n";
  1038. this->WriteTargetVersionAttribute(fout, target);
  1039. linkOptions.OutputFlagMap(fout, 4);
  1040. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  1041. std::string const& linkDirsString = this->Makefile->GetSafeDefinition(
  1042. cmStrCat("CMAKE_", linkLanguage, "_STANDARD_LINK_DIRECTORIES"));
  1043. this->OutputLibraryDirectories(fout, cmList(linkDirsString),
  1044. cli.GetDirectories());
  1045. fout << "\"\n";
  1046. temp =
  1047. cmStrCat(target->GetPDBDirectory(configName), '/', targetNames.PDB);
  1048. fout << "\t\t\t\tProgramDatabaseFile=\""
  1049. << this->ConvertToXMLOutputPathSingle(temp) << "\"\n";
  1050. if (targetOptions.UsingDebugInfo()) {
  1051. fout << "\t\t\t\tGenerateDebugInformation=\"true\"\n";
  1052. }
  1053. if (this->WindowsCEProject) {
  1054. fout << "\t\t\t\tSubSystem=\"8\"\n";
  1055. }
  1056. std::string stackVar = cmStrCat("CMAKE_", linkLanguage, "_STACK_SIZE");
  1057. cmValue stackVal = this->Makefile->GetDefinition(stackVar);
  1058. if (stackVal) {
  1059. fout << "\t\t\t\tStackReserveSize=\"" << *stackVal << "\"\n";
  1060. }
  1061. if (!targetNames.ImportLibrary.empty()) {
  1062. temp = cmStrCat(target->GetDirectory(
  1063. configName, cmStateEnums::ImportLibraryArtifact),
  1064. '/', targetNames.ImportLibrary);
  1065. fout << "\t\t\t\tImportLibrary=\""
  1066. << this->ConvertToXMLOutputPathSingle(temp) << "\"";
  1067. }
  1068. if (this->FortranProject) {
  1069. fout << "\n\t\t\t\tLinkDLL=\"true\"";
  1070. }
  1071. fout << "/>\n";
  1072. } break;
  1073. case cmStateEnums::EXECUTABLE: {
  1074. cmGeneratorTarget::Names targetNames =
  1075. target->GetExecutableNames(configName);
  1076. // Compute the link library and directory information.
  1077. cmComputeLinkInformation* pcli = target->GetLinkInformation(configName);
  1078. if (!pcli) {
  1079. return;
  1080. }
  1081. cmComputeLinkInformation& cli = *pcli;
  1082. if (!target->GetLinkerTypeProperty(linkLanguage, configName).empty()) {
  1083. // Visual Studio 10 or upper is required for this feature
  1084. this->GetCMakeInstance()->IssueMessage(
  1085. MessageType::FATAL_ERROR,
  1086. cmStrCat("'LINKER_TYPE' property, specified on target '",
  1087. target->GetName(),
  1088. "', is not supported by this generator."),
  1089. target->GetBacktrace());
  1090. }
  1091. bool isWin32Executable = target->IsWin32Executable(configName);
  1092. // Compute the variable name to lookup standard libraries for this
  1093. // language.
  1094. std::string standardLibsVar =
  1095. cmStrCat("CMAKE_", linkLanguage, "_STANDARD_LIBRARIES");
  1096. const char* tool = "VCLinkerTool";
  1097. if (this->FortranProject) {
  1098. tool = "VFLinkerTool";
  1099. }
  1100. fout << "\t\t\t<Tool\n"
  1101. "\t\t\t\tName=\""
  1102. << tool << "\"\n";
  1103. if (!gg->NeedLinkLibraryDependencies(target)) {
  1104. fout << "\t\t\t\tLinkLibraryDependencies=\"false\"\n";
  1105. }
  1106. // Use the NOINHERIT macro to avoid getting VS project default
  1107. // libraries which may be set by the user to something bad.
  1108. fout << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  1109. << this->Makefile->GetSafeDefinition(standardLibsVar);
  1110. if (this->FortranProject) {
  1111. this->Internal->OutputObjects(fout, target, configName, " ");
  1112. }
  1113. fout << " ";
  1114. this->Internal->OutputLibraries(fout, cli.GetItems());
  1115. fout << "\"\n";
  1116. temp =
  1117. cmStrCat(target->GetDirectory(configName), '/', targetNames.Output);
  1118. fout << "\t\t\t\tOutputFile=\""
  1119. << this->ConvertToXMLOutputPathSingle(temp) << "\"\n";
  1120. this->WriteTargetVersionAttribute(fout, target);
  1121. linkOptions.OutputFlagMap(fout, 4);
  1122. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  1123. std::string const& linkDirsString = this->Makefile->GetSafeDefinition(
  1124. cmStrCat("CMAKE_", linkLanguage, "_STANDARD_LINK_DIRECTORIES"));
  1125. this->OutputLibraryDirectories(fout, cmList(linkDirsString),
  1126. cli.GetDirectories());
  1127. fout << "\"\n";
  1128. std::string path = this->ConvertToXMLOutputPathSingle(
  1129. target->GetPDBDirectory(configName));
  1130. fout << "\t\t\t\tProgramDatabaseFile=\"" << path << '/'
  1131. << targetNames.PDB << "\"\n";
  1132. if (targetOptions.UsingDebugInfo()) {
  1133. fout << "\t\t\t\tGenerateDebugInformation=\"true\"\n";
  1134. }
  1135. if (this->WindowsCEProject) {
  1136. fout << "\t\t\t\tSubSystem=\"8\"\n";
  1137. if (!linkOptions.GetFlag("EntryPointSymbol")) {
  1138. const char* entryPointSymbol = targetOptions.UsingUnicode()
  1139. ? (isWin32Executable ? "wWinMainCRTStartup" : "mainWCRTStartup")
  1140. : (isWin32Executable ? "WinMainCRTStartup" : "mainACRTStartup");
  1141. fout << "\t\t\t\tEntryPointSymbol=\"" << entryPointSymbol << "\"\n";
  1142. }
  1143. } else if (this->FortranProject) {
  1144. fout << "\t\t\t\tSubSystem=\""
  1145. << (isWin32Executable ? "subSystemWindows" : "subSystemConsole")
  1146. << "\"\n";
  1147. } else {
  1148. fout << "\t\t\t\tSubSystem=\"" << (isWin32Executable ? "2" : "1")
  1149. << "\"\n";
  1150. }
  1151. std::string stackVar = cmStrCat("CMAKE_", linkLanguage, "_STACK_SIZE");
  1152. cmValue stackVal = this->Makefile->GetDefinition(stackVar);
  1153. if (stackVal) {
  1154. fout << "\t\t\t\tStackReserveSize=\"" << *stackVal << '"';
  1155. }
  1156. temp = cmStrCat(
  1157. target->GetDirectory(configName, cmStateEnums::ImportLibraryArtifact),
  1158. '/', targetNames.ImportLibrary);
  1159. fout << "\t\t\t\tImportLibrary=\""
  1160. << this->ConvertToXMLOutputPathSingle(temp) << "\"/>\n";
  1161. break;
  1162. }
  1163. case cmStateEnums::UTILITY:
  1164. case cmStateEnums::GLOBAL_TARGET:
  1165. case cmStateEnums::INTERFACE_LIBRARY:
  1166. break;
  1167. }
  1168. }
  1169. static std::string cmLocalVisualStudio7GeneratorEscapeForXML(
  1170. const std::string& s)
  1171. {
  1172. std::string ret = s;
  1173. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1174. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1175. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1176. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1177. cmSystemTools::ReplaceString(ret, "\n", "&#x0D;&#x0A;");
  1178. return ret;
  1179. }
  1180. static std::string GetEscapedPropertyIfValueNotNULL(const char* propertyValue)
  1181. {
  1182. return propertyValue
  1183. ? cmLocalVisualStudio7GeneratorEscapeForXML(propertyValue)
  1184. : std::string();
  1185. }
  1186. void cmLocalVisualStudio7Generator::OutputDeploymentDebuggerTool(
  1187. std::ostream& fout, std::string const& config, cmGeneratorTarget* target)
  1188. {
  1189. if (this->WindowsCEProject) {
  1190. cmValue dir = target->GetProperty("DEPLOYMENT_REMOTE_DIRECTORY");
  1191. cmValue additionalFiles =
  1192. target->GetProperty("DEPLOYMENT_ADDITIONAL_FILES");
  1193. if (!dir && !additionalFiles) {
  1194. return;
  1195. }
  1196. fout << "\t\t\t<DeploymentTool\n"
  1197. "\t\t\t\tForceDirty=\"-1\"\n"
  1198. "\t\t\t\tRemoteDirectory=\""
  1199. << GetEscapedPropertyIfValueNotNULL(dir->c_str())
  1200. << "\"\n"
  1201. "\t\t\t\tRegisterOutput=\"0\"\n"
  1202. "\t\t\t\tAdditionalFiles=\""
  1203. << GetEscapedPropertyIfValueNotNULL(additionalFiles->c_str())
  1204. << "\"/>\n";
  1205. if (dir) {
  1206. std::string const exe =
  1207. cmStrCat(*dir, '\\', target->GetFullName(config));
  1208. fout << "\t\t\t<DebuggerTool\n"
  1209. "\t\t\t\tRemoteExecutable=\""
  1210. << this->EscapeForXML(exe)
  1211. << "\"\n"
  1212. "\t\t\t\tArguments=\"\"\n"
  1213. "\t\t\t/>\n";
  1214. }
  1215. }
  1216. }
  1217. void cmLocalVisualStudio7Generator::WriteTargetVersionAttribute(
  1218. std::ostream& fout, cmGeneratorTarget* gt)
  1219. {
  1220. int major;
  1221. int minor;
  1222. gt->GetTargetVersion(major, minor);
  1223. fout << "\t\t\t\tVersion=\"" << major << "." << minor << "\"\n";
  1224. }
  1225. void cmLocalVisualStudio7GeneratorInternals::OutputLibraries(
  1226. std::ostream& fout, ItemVector const& libs)
  1227. {
  1228. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  1229. for (auto const& lib : libs) {
  1230. if (lib.IsPath == cmComputeLinkInformation::ItemIsPath::Yes) {
  1231. std::string rel = lg->MaybeRelativeToCurBinDir(lib.Value.Value);
  1232. rel = lg->ConvertToXMLOutputPath(rel);
  1233. fout << (lib.HasFeature() ? lib.GetFormattedItem(rel).Value : rel)
  1234. << " ";
  1235. } else if (!lib.Target ||
  1236. (lib.Target->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  1237. lib.Target->GetType() != cmStateEnums::OBJECT_LIBRARY)) {
  1238. fout << lib.Value.Value << " ";
  1239. }
  1240. }
  1241. }
  1242. void cmLocalVisualStudio7GeneratorInternals::OutputObjects(
  1243. std::ostream& fout, cmGeneratorTarget* gt, std::string const& configName,
  1244. const char* isep)
  1245. {
  1246. // VS < 8 does not support per-config source locations so we
  1247. // list object library content on the link line instead.
  1248. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  1249. std::vector<cmSourceFile const*> objs;
  1250. gt->GetExternalObjects(objs, configName);
  1251. const char* sep = isep ? isep : "";
  1252. for (cmSourceFile const* obj : objs) {
  1253. if (!obj->GetObjectLibrary().empty()) {
  1254. std::string const& objFile = obj->GetFullPath();
  1255. std::string rel = lg->MaybeRelativeToCurBinDir(objFile);
  1256. fout << sep << lg->ConvertToXMLOutputPath(rel);
  1257. sep = " ";
  1258. }
  1259. }
  1260. }
  1261. void cmLocalVisualStudio7Generator::OutputLibraryDirectories(
  1262. std::ostream& fout, std::vector<std::string> const& stdlink,
  1263. std::vector<std::string> const& dirs)
  1264. {
  1265. const char* comma = "";
  1266. for (std::string dir : dirs) {
  1267. // Remove any trailing slash and skip empty paths.
  1268. if (dir.back() == '/') {
  1269. dir = dir.substr(0, dir.size() - 1);
  1270. }
  1271. if (dir.empty()) {
  1272. continue;
  1273. }
  1274. // Switch to a relative path specification if it is shorter.
  1275. if (cmSystemTools::FileIsFullPath(dir)) {
  1276. std::string rel = this->MaybeRelativeToCurBinDir(dir);
  1277. if (rel.size() < dir.size()) {
  1278. dir = rel;
  1279. }
  1280. }
  1281. // First search a configuration-specific subdirectory and then the
  1282. // original directory.
  1283. fout << comma
  1284. << this->ConvertToXMLOutputPath(
  1285. cmStrCat(dir, "/$(ConfigurationName)"))
  1286. << ',' << this->ConvertToXMLOutputPath(dir);
  1287. comma = ",";
  1288. }
  1289. // No special processing on toolchain-defined standard link directory paths
  1290. for (const auto& dir : stdlink) {
  1291. fout << comma << this->ConvertToXMLOutputPath(dir);
  1292. comma = ",";
  1293. }
  1294. }
  1295. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  1296. const std::string& libName,
  1297. cmGeneratorTarget* target)
  1298. {
  1299. std::vector<std::string> configs =
  1300. this->Makefile->GetGeneratorConfigs(cmMakefile::ExcludeEmptyConfig);
  1301. // We may be modifying the source groups temporarily, so make a copy.
  1302. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  1303. AllConfigSources sources;
  1304. sources.Sources = target->GetAllConfigSources();
  1305. // Add CMakeLists.txt file with rule to re-run CMake for user convenience.
  1306. if (target->GetType() != cmStateEnums::GLOBAL_TARGET &&
  1307. target->GetName() != CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  1308. if (cmSourceFile* sf = this->CreateVCProjBuildRule()) {
  1309. cmGeneratorTarget::AllConfigSource acs;
  1310. acs.Source = sf;
  1311. acs.Kind = cmGeneratorTarget::SourceKindCustomCommand;
  1312. for (size_t ci = 0; ci < configs.size(); ++ci) {
  1313. acs.Configs.push_back(ci);
  1314. }
  1315. bool haveCMakeLists = false;
  1316. for (cmGeneratorTarget::AllConfigSource& si : sources.Sources) {
  1317. if (si.Source == sf) {
  1318. haveCMakeLists = true;
  1319. // Replace the explicit source reference with our generated one.
  1320. si = acs;
  1321. break;
  1322. }
  1323. }
  1324. if (!haveCMakeLists) {
  1325. sources.Sources.emplace_back(std::move(acs));
  1326. }
  1327. }
  1328. }
  1329. for (size_t si = 0; si < sources.Sources.size(); ++si) {
  1330. cmSourceFile const* sf = sources.Sources[si].Source;
  1331. sources.Index[sf] = si;
  1332. if (!sf->GetObjectLibrary().empty()) {
  1333. if (this->FortranProject) {
  1334. // Intel Fortran does not support per-config source locations
  1335. // so we list object library content on the link line instead.
  1336. // See OutputObjects.
  1337. continue;
  1338. }
  1339. }
  1340. // Add the file to the list of sources.
  1341. std::string const source = sf->GetFullPath();
  1342. cmSourceGroup* sourceGroup =
  1343. this->Makefile->FindSourceGroup(source, sourceGroups);
  1344. sourceGroup->AssignSource(sf);
  1345. }
  1346. // open the project
  1347. this->WriteProjectStart(fout, libName, target, sourceGroups);
  1348. // write the configuration information
  1349. this->WriteConfigurations(fout, configs, libName, target);
  1350. fout << "\t<Files>\n";
  1351. // Loop through every source group.
  1352. for (auto const& sg : sourceGroups) {
  1353. this->WriteGroup(&sg, target, fout, libName, configs, sources);
  1354. }
  1355. fout << "\t</Files>\n";
  1356. // Write the VCProj file's footer.
  1357. this->WriteVCProjFooter(fout, target);
  1358. }
  1359. struct cmLVS7GFileConfig
  1360. {
  1361. std::string ObjectName;
  1362. std::string CompileFlags;
  1363. std::string CompileDefs;
  1364. std::string CompileDefsConfig;
  1365. std::string AdditionalDeps;
  1366. std::string IncludeDirs;
  1367. bool ExcludedFromBuild;
  1368. };
  1369. class cmLocalVisualStudio7GeneratorFCInfo
  1370. {
  1371. public:
  1372. cmLocalVisualStudio7GeneratorFCInfo(
  1373. cmLocalVisualStudio7Generator* lg, cmGeneratorTarget* target,
  1374. cmGeneratorTarget::AllConfigSource const& acs,
  1375. std::vector<std::string> const& configs);
  1376. std::map<std::string, cmLVS7GFileConfig> FileConfigMap;
  1377. };
  1378. cmLocalVisualStudio7GeneratorFCInfo::cmLocalVisualStudio7GeneratorFCInfo(
  1379. cmLocalVisualStudio7Generator* lg, cmGeneratorTarget* gt,
  1380. cmGeneratorTarget::AllConfigSource const& acs,
  1381. std::vector<std::string> const& configs)
  1382. {
  1383. cmSourceFile const& sf = *acs.Source;
  1384. std::string objectName;
  1385. if (gt->HasExplicitObjectName(&sf)) {
  1386. objectName = gt->GetObjectName(&sf);
  1387. }
  1388. // Compute per-source, per-config information.
  1389. size_t ci = 0;
  1390. for (std::string const& config : configs) {
  1391. std::string configUpper = cmSystemTools::UpperCase(config);
  1392. cmLVS7GFileConfig fc;
  1393. std::string lang =
  1394. lg->GlobalGenerator->GetLanguageFromExtension(sf.GetExtension().c_str());
  1395. const std::string& sourceLang = lg->GetSourceFileLanguage(sf);
  1396. bool needForceLang = false;
  1397. // source file does not match its extension language
  1398. if (lang != sourceLang) {
  1399. needForceLang = true;
  1400. lang = sourceLang;
  1401. }
  1402. cmGeneratorExpressionInterpreter genexInterpreter(lg, config, gt, lang);
  1403. bool needfc = false;
  1404. if (!objectName.empty()) {
  1405. fc.ObjectName = objectName;
  1406. needfc = true;
  1407. }
  1408. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  1409. if (cmValue cflags = sf.GetProperty(COMPILE_FLAGS)) {
  1410. fc.CompileFlags = genexInterpreter.Evaluate(*cflags, COMPILE_FLAGS);
  1411. needfc = true;
  1412. }
  1413. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  1414. if (cmValue coptions = sf.GetProperty(COMPILE_OPTIONS)) {
  1415. lg->AppendCompileOptions(
  1416. fc.CompileFlags,
  1417. genexInterpreter.Evaluate(*coptions, COMPILE_OPTIONS));
  1418. needfc = true;
  1419. }
  1420. // Add precompile headers compile options.
  1421. const std::string pchSource = gt->GetPchSource(config, lang);
  1422. if (!pchSource.empty() && !sf.GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  1423. std::string pchOptions;
  1424. if (sf.GetFullPath() == pchSource) {
  1425. pchOptions = gt->GetPchCreateCompileOptions(config, lang);
  1426. } else {
  1427. pchOptions = gt->GetPchUseCompileOptions(config, lang);
  1428. }
  1429. lg->AppendCompileOptions(
  1430. fc.CompileFlags,
  1431. genexInterpreter.Evaluate(pchOptions, COMPILE_OPTIONS));
  1432. needfc = true;
  1433. }
  1434. if (lg->FortranProject) {
  1435. switch (cmOutputConverter::GetFortranPreprocess(
  1436. sf.GetSafeProperty("Fortran_PREPROCESS"))) {
  1437. case cmOutputConverter::FortranPreprocess::Needed:
  1438. fc.CompileFlags = cmStrCat("-fpp ", fc.CompileFlags);
  1439. needfc = true;
  1440. break;
  1441. case cmOutputConverter::FortranPreprocess::NotNeeded:
  1442. fc.CompileFlags = cmStrCat("-nofpp ", fc.CompileFlags);
  1443. needfc = true;
  1444. break;
  1445. default:
  1446. break;
  1447. }
  1448. switch (cmOutputConverter::GetFortranFormat(
  1449. sf.GetSafeProperty("Fortran_FORMAT"))) {
  1450. case cmOutputConverter::FortranFormatFixed:
  1451. fc.CompileFlags = cmStrCat("-fixed ", fc.CompileFlags);
  1452. needfc = true;
  1453. break;
  1454. case cmOutputConverter::FortranFormatFree:
  1455. fc.CompileFlags = cmStrCat("-free ", fc.CompileFlags);
  1456. needfc = true;
  1457. break;
  1458. default:
  1459. break;
  1460. }
  1461. }
  1462. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  1463. if (cmValue cdefs = sf.GetProperty(COMPILE_DEFINITIONS)) {
  1464. fc.CompileDefs = genexInterpreter.Evaluate(*cdefs, COMPILE_DEFINITIONS);
  1465. needfc = true;
  1466. }
  1467. std::string defPropName = cmStrCat("COMPILE_DEFINITIONS_", configUpper);
  1468. if (cmValue ccdefs = sf.GetProperty(defPropName)) {
  1469. fc.CompileDefsConfig =
  1470. genexInterpreter.Evaluate(*ccdefs, COMPILE_DEFINITIONS);
  1471. needfc = true;
  1472. }
  1473. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  1474. if (cmValue cincs = sf.GetProperty(INCLUDE_DIRECTORIES)) {
  1475. fc.IncludeDirs = genexInterpreter.Evaluate(*cincs, INCLUDE_DIRECTORIES);
  1476. needfc = true;
  1477. }
  1478. // Check for extra object-file dependencies.
  1479. if (cmValue deps = sf.GetProperty("OBJECT_DEPENDS")) {
  1480. cmList depends{ *deps };
  1481. if (!depends.empty()) {
  1482. for (std::string& d : depends) {
  1483. d = lg->ConvertToXMLOutputPath(d);
  1484. }
  1485. fc.AdditionalDeps += depends.to_string();
  1486. needfc = true;
  1487. }
  1488. }
  1489. const std::string& linkLanguage = gt->GetLinkerLanguage(config);
  1490. // If HEADER_FILE_ONLY is set, we must suppress this generation in
  1491. // the project file
  1492. fc.ExcludedFromBuild = sf.GetPropertyAsBool("HEADER_FILE_ONLY") ||
  1493. !cm::contains(acs.Configs, ci) ||
  1494. (gt->GetPropertyAsBool("UNITY_BUILD") &&
  1495. sf.GetProperty("UNITY_SOURCE_FILE") &&
  1496. !sf.GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION"));
  1497. if (fc.ExcludedFromBuild) {
  1498. needfc = true;
  1499. }
  1500. // if the source file does not match the linker language
  1501. // then force c or c++
  1502. if (needForceLang || (linkLanguage != lang)) {
  1503. if (lang == "CXX") {
  1504. // force a C++ file type
  1505. fc.CompileFlags += " /TP ";
  1506. needfc = true;
  1507. } else if (lang == "C") {
  1508. // force to c
  1509. fc.CompileFlags += " /TC ";
  1510. needfc = true;
  1511. }
  1512. }
  1513. if (needfc) {
  1514. this->FileConfigMap[config] = fc;
  1515. }
  1516. ++ci;
  1517. }
  1518. }
  1519. std::string cmLocalVisualStudio7Generator::ComputeLongestObjectDirectory(
  1520. cmGeneratorTarget const* target) const
  1521. {
  1522. std::vector<std::string> configs =
  1523. target->Target->GetMakefile()->GetGeneratorConfigs(
  1524. cmMakefile::ExcludeEmptyConfig);
  1525. // Compute the maximum length configuration name.
  1526. std::string config_max;
  1527. for (auto& config : configs) {
  1528. if (config.size() > config_max.size()) {
  1529. config_max = config;
  1530. }
  1531. }
  1532. // Compute the maximum length full path to the intermediate
  1533. // files directory for any configuration. This is used to construct
  1534. // object file names that do not produce paths that are too long.
  1535. std::string dir_max =
  1536. cmStrCat(this->GetCurrentBinaryDirectory(), '/',
  1537. this->GetTargetDirectory(target), '/', config_max, '/');
  1538. return dir_max;
  1539. }
  1540. bool cmLocalVisualStudio7Generator::WriteGroup(
  1541. const cmSourceGroup* sg, cmGeneratorTarget* target, std::ostream& fout,
  1542. const std::string& libName, std::vector<std::string> const& configs,
  1543. AllConfigSources const& sources)
  1544. {
  1545. cmGlobalVisualStudio7Generator* gg =
  1546. static_cast<cmGlobalVisualStudio7Generator*>(this->GlobalGenerator);
  1547. const std::vector<const cmSourceFile*>& sourceFiles = sg->GetSourceFiles();
  1548. std::vector<cmSourceGroup> const& children = sg->GetGroupChildren();
  1549. // Write the children to temporary output.
  1550. bool hasChildrenWithSources = false;
  1551. std::ostringstream tmpOut;
  1552. for (const auto& child : children) {
  1553. if (this->WriteGroup(&child, target, tmpOut, libName, configs, sources)) {
  1554. hasChildrenWithSources = true;
  1555. }
  1556. }
  1557. // If the group is empty, don't write it at all.
  1558. if (sourceFiles.empty() && !hasChildrenWithSources) {
  1559. return false;
  1560. }
  1561. // If the group has a name, write the header.
  1562. std::string const& name = sg->GetName();
  1563. if (!name.empty()) {
  1564. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  1565. }
  1566. auto& sourcesVisited = this->GetSourcesVisited(target);
  1567. // Loop through each source in the source group.
  1568. for (const cmSourceFile* sf : sourceFiles) {
  1569. std::string source = sf->GetFullPath();
  1570. if (source != libName || target->GetType() == cmStateEnums::UTILITY ||
  1571. target->GetType() == cmStateEnums::GLOBAL_TARGET ||
  1572. target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  1573. // Look up the source kind and configs.
  1574. auto map_it = sources.Index.find(sf);
  1575. // The map entry must exist because we populated it earlier.
  1576. assert(map_it != sources.Index.end());
  1577. cmGeneratorTarget::AllConfigSource const& acs =
  1578. sources.Sources[map_it->second];
  1579. FCInfo fcinfo(this, target, acs, configs);
  1580. fout << "\t\t\t<File\n";
  1581. std::string d = this->ConvertToXMLOutputPathSingle(source);
  1582. // Tell MS-Dev what the source is. If the compiler knows how to
  1583. // build it, then it will.
  1584. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  1585. if (cmCustomCommand const* command = sf->GetCustomCommand()) {
  1586. if (sourcesVisited.insert(sf).second) {
  1587. this->WriteCustomRule(fout, configs, source.c_str(), *command,
  1588. fcinfo);
  1589. }
  1590. } else if (!fcinfo.FileConfigMap.empty()) {
  1591. const char* aCompilerTool = "VCCLCompilerTool";
  1592. std::string ppLang = "CXX";
  1593. if (this->FortranProject) {
  1594. aCompilerTool = "VFFortranCompilerTool";
  1595. }
  1596. std::string const& lang = sf->GetLanguage();
  1597. std::string ext = sf->GetExtension();
  1598. ext = cmSystemTools::LowerCase(ext);
  1599. if (ext == "idl") {
  1600. aCompilerTool = "VCMIDLTool";
  1601. if (this->FortranProject) {
  1602. aCompilerTool = "VFMIDLTool";
  1603. }
  1604. }
  1605. if (ext == "rc") {
  1606. aCompilerTool = "VCResourceCompilerTool";
  1607. ppLang = "RC";
  1608. if (this->FortranProject) {
  1609. aCompilerTool = "VFResourceCompilerTool";
  1610. }
  1611. }
  1612. if (ext == "def") {
  1613. aCompilerTool = "VCCustomBuildTool";
  1614. if (this->FortranProject) {
  1615. aCompilerTool = "VFCustomBuildTool";
  1616. }
  1617. }
  1618. if (gg->IsMarmasmEnabled() && !this->FortranProject &&
  1619. lang == "ASM_MARMASM") {
  1620. aCompilerTool = "MARMASM";
  1621. }
  1622. if (gg->IsMasmEnabled() && !this->FortranProject &&
  1623. lang == "ASM_MASM") {
  1624. aCompilerTool = "MASM";
  1625. }
  1626. if (acs.Kind == cmGeneratorTarget::SourceKindExternalObject) {
  1627. aCompilerTool = "VCCustomBuildTool";
  1628. }
  1629. for (auto const& fci : fcinfo.FileConfigMap) {
  1630. cmLVS7GFileConfig const& fc = fci.second;
  1631. fout << "\t\t\t\t<FileConfiguration\n"
  1632. "\t\t\t\t\tName=\""
  1633. << fci.first << '|' << gg->GetPlatformName() << '"';
  1634. if (fc.ExcludedFromBuild) {
  1635. fout << " ExcludedFromBuild=\"true\"";
  1636. }
  1637. fout << ">\n"
  1638. "\t\t\t\t\t<Tool\n"
  1639. "\t\t\t\t\tName=\""
  1640. << aCompilerTool << "\"\n";
  1641. if (!fc.CompileFlags.empty() || !fc.CompileDefs.empty() ||
  1642. !fc.CompileDefsConfig.empty() || !fc.IncludeDirs.empty()) {
  1643. Options::Tool tool = Options::Compiler;
  1644. cmVS7FlagTable const* table =
  1645. cmLocalVisualStudio7GeneratorFlagTable;
  1646. if (this->FortranProject) {
  1647. tool = Options::FortranCompiler;
  1648. table = cmLocalVisualStudio7GeneratorFortranFlagTable;
  1649. }
  1650. Options fileOptions(this, tool, table, gg->ExtraFlagTable);
  1651. fileOptions.Parse(fc.CompileFlags);
  1652. fileOptions.AddDefines(fc.CompileDefs);
  1653. fileOptions.AddDefines(fc.CompileDefsConfig);
  1654. // validate source level include directories
  1655. std::vector<std::string> includes;
  1656. this->AppendIncludeDirectories(includes, fc.IncludeDirs, *sf);
  1657. fileOptions.AddIncludes(includes);
  1658. fileOptions.OutputFlagMap(fout, 5);
  1659. fileOptions.OutputAdditionalIncludeDirectories(
  1660. fout, 5,
  1661. ppLang == "CXX" && this->FortranProject ? "Fortran" : ppLang);
  1662. fileOptions.OutputPreprocessorDefinitions(fout, 5, ppLang);
  1663. }
  1664. if (!fc.AdditionalDeps.empty()) {
  1665. fout << "\t\t\t\t\tAdditionalDependencies=\"" << fc.AdditionalDeps
  1666. << "\"\n";
  1667. }
  1668. if (!fc.ObjectName.empty()) {
  1669. fout << "\t\t\t\t\tObjectFile=\"$(IntDir)/" << fc.ObjectName
  1670. << "\"\n";
  1671. }
  1672. fout << "\t\t\t\t\t/>\n"
  1673. "\t\t\t\t</FileConfiguration>\n";
  1674. }
  1675. }
  1676. fout << "\t\t\t</File>\n";
  1677. }
  1678. }
  1679. // If the group has children with source files, write the children.
  1680. if (hasChildrenWithSources) {
  1681. fout << tmpOut.str();
  1682. }
  1683. // If the group has a name, write the footer.
  1684. if (!name.empty()) {
  1685. this->WriteVCProjEndGroup(fout);
  1686. }
  1687. return true;
  1688. }
  1689. void cmLocalVisualStudio7Generator::WriteCustomRule(
  1690. std::ostream& fout, std::vector<std::string> const& configs,
  1691. const char* source, const cmCustomCommand& command, FCInfo& fcinfo)
  1692. {
  1693. cmGlobalVisualStudio7Generator* gg =
  1694. static_cast<cmGlobalVisualStudio7Generator*>(this->GlobalGenerator);
  1695. // Write the rule for each configuration.
  1696. const char* compileTool = "VCCLCompilerTool";
  1697. if (this->FortranProject) {
  1698. compileTool = "VFCLCompilerTool";
  1699. }
  1700. const char* customTool = "VCCustomBuildTool";
  1701. if (this->FortranProject) {
  1702. customTool = "VFCustomBuildTool";
  1703. }
  1704. for (std::string const& config : configs) {
  1705. cmCustomCommandGenerator ccg(command, config, this);
  1706. cmLVS7GFileConfig const& fc = fcinfo.FileConfigMap[config];
  1707. fout << "\t\t\t\t<FileConfiguration\n"
  1708. "\t\t\t\t\tName=\""
  1709. << config << '|' << gg->GetPlatformName() << "\">\n";
  1710. if (!fc.CompileFlags.empty()) {
  1711. fout << "\t\t\t\t\t<Tool\n"
  1712. "\t\t\t\t\tName=\""
  1713. << compileTool
  1714. << "\"\n"
  1715. "\t\t\t\t\tAdditionalOptions=\""
  1716. << this->EscapeForXML(fc.CompileFlags) << "\"/>\n";
  1717. }
  1718. std::string comment = this->ConstructComment(ccg);
  1719. std::string script = this->ConstructScript(ccg);
  1720. if (this->FortranProject) {
  1721. cmSystemTools::ReplaceString(script, "$(Configuration)", config);
  1722. }
  1723. script += this->FinishConstructScript(VsProjectType::vcxproj);
  1724. /* clang-format off */
  1725. fout << "\t\t\t\t\t<Tool\n"
  1726. "\t\t\t\t\tName=\"" << customTool << "\"\n"
  1727. << "\t\t\t\t\tDescription=\""
  1728. << this->EscapeForXML(comment) << "\"\n"
  1729. "\t\t\t\t\tCommandLine=\""
  1730. << this->EscapeForXML(script) << "\"\n"
  1731. "\t\t\t\t\tAdditionalDependencies=\"";
  1732. /* clang-format on */
  1733. if (ccg.GetDepends().empty()) {
  1734. // There are no real dependencies. Produce an artificial one to
  1735. // make sure the rule runs reliably.
  1736. if (!cmSystemTools::FileExists(source)) {
  1737. cmsys::ofstream depout(source);
  1738. depout << "Artificial dependency for a custom command.\n";
  1739. }
  1740. fout << this->ConvertToXMLOutputPath(source);
  1741. } else {
  1742. // Write out the dependencies for the rule.
  1743. for (std::string const& d : ccg.GetDepends()) {
  1744. // Get the real name of the dependency in case it is a CMake target.
  1745. std::string dep;
  1746. if (this->GetRealDependency(d, config, dep)) {
  1747. fout << this->ConvertToXMLOutputPath(dep) << ";";
  1748. }
  1749. }
  1750. }
  1751. fout << "\"\n"
  1752. "\t\t\t\t\tOutputs=\"";
  1753. if (ccg.GetOutputs().empty()) {
  1754. fout << source << "_force";
  1755. } else {
  1756. // Write a rule for the output generated by this command.
  1757. const char* sep = "";
  1758. for (std::string const& output : ccg.GetOutputs()) {
  1759. fout << sep << this->ConvertToXMLOutputPathSingle(output);
  1760. sep = ";";
  1761. }
  1762. }
  1763. fout << "\"/>\n"
  1764. "\t\t\t\t</FileConfiguration>\n";
  1765. }
  1766. }
  1767. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  1768. const char* group,
  1769. const char*)
  1770. {
  1771. fout << "\t\t<Filter\n"
  1772. "\t\t\tName=\""
  1773. << group
  1774. << "\"\n"
  1775. "\t\t\tFilter=\"\">\n";
  1776. }
  1777. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  1778. {
  1779. fout << "\t\t</Filter>\n";
  1780. }
  1781. // look for custom rules on a target and collect them together
  1782. void cmLocalVisualStudio7Generator::OutputTargetRules(
  1783. std::ostream& fout, const std::string& configName, cmGeneratorTarget* target,
  1784. const std::string& /*libName*/)
  1785. {
  1786. if (target->GetType() > cmStateEnums::GLOBAL_TARGET) {
  1787. return;
  1788. }
  1789. EventWriter event(this, configName, fout);
  1790. // Add pre-build event.
  1791. const char* tool =
  1792. this->FortranProject ? "VFPreBuildEventTool" : "VCPreBuildEventTool";
  1793. event.Start(tool);
  1794. event.Write(target->GetPreBuildCommands());
  1795. event.Finish();
  1796. // Add pre-link event.
  1797. tool = this->FortranProject ? "VFPreLinkEventTool" : "VCPreLinkEventTool";
  1798. event.Start(tool);
  1799. bool addedPrelink = false;
  1800. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  1801. target->GetModuleDefinitionInfo(configName);
  1802. if (mdi && mdi->DefFileGenerated) {
  1803. addedPrelink = true;
  1804. std::vector<cmCustomCommand> commands = target->GetPreLinkCommands();
  1805. cmGlobalVisualStudioGenerator* gg =
  1806. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  1807. gg->AddSymbolExportCommand(target, commands, configName);
  1808. event.Write(commands);
  1809. }
  1810. if (!addedPrelink) {
  1811. event.Write(target->GetPreLinkCommands());
  1812. }
  1813. std::unique_ptr<cmCustomCommand> pcc(
  1814. this->MaybeCreateImplibDir(target, configName, this->FortranProject));
  1815. if (pcc) {
  1816. event.Write(*pcc);
  1817. }
  1818. event.Finish();
  1819. // Add post-build event.
  1820. tool =
  1821. this->FortranProject ? "VFPostBuildEventTool" : "VCPostBuildEventTool";
  1822. event.Start(tool);
  1823. event.Write(target->GetPostBuildCommands());
  1824. event.Finish();
  1825. }
  1826. void cmLocalVisualStudio7Generator::WriteProjectSCC(std::ostream& fout,
  1827. cmGeneratorTarget* target)
  1828. {
  1829. // if we have all the required Source code control tags
  1830. // then add that to the project
  1831. cmValue vsProjectname = target->GetProperty("VS_SCC_PROJECTNAME");
  1832. cmValue vsLocalpath = target->GetProperty("VS_SCC_LOCALPATH");
  1833. cmValue vsProvider = target->GetProperty("VS_SCC_PROVIDER");
  1834. if (vsProvider && vsLocalpath && vsProjectname) {
  1835. /* clang-format off */
  1836. fout << "\tSccProjectName=\"" << *vsProjectname << "\"\n"
  1837. "\tSccLocalPath=\"" << *vsLocalpath << "\"\n"
  1838. "\tSccProvider=\"" << *vsProvider << "\"\n";
  1839. /* clang-format on */
  1840. cmValue vsAuxPath = target->GetProperty("VS_SCC_AUXPATH");
  1841. if (vsAuxPath) {
  1842. fout << "\tSccAuxPath=\"" << *vsAuxPath << "\"\n";
  1843. }
  1844. }
  1845. }
  1846. void cmLocalVisualStudio7Generator::WriteProjectStartFortran(
  1847. std::ostream& fout, const std::string& libName, cmGeneratorTarget* target)
  1848. {
  1849. cmGlobalVisualStudio7Generator* gg =
  1850. static_cast<cmGlobalVisualStudio7Generator*>(this->GlobalGenerator);
  1851. fout << R"(<?xml version="1.0" encoding = ")" << gg->Encoding()
  1852. << "\"?>\n"
  1853. "<VisualStudioProject\n"
  1854. "\tProjectCreator=\"Intel Fortran\"\n"
  1855. "\tVersion=\""
  1856. << gg->GetIntelProjectVersion() << "\"\n";
  1857. cmValue p = target->GetProperty("VS_KEYWORD");
  1858. const char* keyword = p ? p->c_str() : "Console Application";
  1859. const char* projectType = nullptr;
  1860. switch (target->GetType()) {
  1861. case cmStateEnums::OBJECT_LIBRARY:
  1862. case cmStateEnums::STATIC_LIBRARY:
  1863. projectType = "typeStaticLibrary";
  1864. if (keyword) {
  1865. keyword = "Static Library";
  1866. }
  1867. break;
  1868. case cmStateEnums::SHARED_LIBRARY:
  1869. case cmStateEnums::MODULE_LIBRARY:
  1870. projectType = "typeDynamicLibrary";
  1871. if (!keyword) {
  1872. keyword = "Dll";
  1873. }
  1874. break;
  1875. case cmStateEnums::EXECUTABLE:
  1876. if (!keyword) {
  1877. keyword = "Console Application";
  1878. }
  1879. projectType = nullptr;
  1880. break;
  1881. case cmStateEnums::UTILITY:
  1882. case cmStateEnums::GLOBAL_TARGET:
  1883. case cmStateEnums::INTERFACE_LIBRARY:
  1884. case cmStateEnums::UNKNOWN_LIBRARY:
  1885. break;
  1886. }
  1887. if (projectType) {
  1888. fout << "\tProjectType=\"" << projectType << "\"\n";
  1889. }
  1890. this->WriteProjectSCC(fout, target);
  1891. /* clang-format off */
  1892. fout<< "\tKeyword=\"" << keyword << "\">\n"
  1893. "\tProjectGUID=\"{" << gg->GetGUID(libName) << "}\">\n"
  1894. "\t<Platforms>\n"
  1895. "\t\t<Platform\n\t\t\tName=\"" << gg->GetPlatformName() << "\"/>\n"
  1896. "\t</Platforms>\n";
  1897. /* clang-format on */
  1898. }
  1899. void cmLocalVisualStudio7Generator::WriteProjectStart(
  1900. std::ostream& fout, const std::string& libName, cmGeneratorTarget* target,
  1901. std::vector<cmSourceGroup>&)
  1902. {
  1903. if (this->FortranProject) {
  1904. this->WriteProjectStartFortran(fout, libName, target);
  1905. return;
  1906. }
  1907. cmGlobalVisualStudio7Generator* gg =
  1908. static_cast<cmGlobalVisualStudio7Generator*>(this->GlobalGenerator);
  1909. fout << R"(<?xml version="1.0" encoding = ")" << gg->Encoding()
  1910. << "\"?>\n"
  1911. "<VisualStudioProject\n"
  1912. "\tProjectType=\"Visual C++\"\n"
  1913. "\tVersion=\""
  1914. << (static_cast<uint16_t>(gg->GetVersion()) / 10) << ".00\"\n";
  1915. cmValue p = target->GetProperty("PROJECT_LABEL");
  1916. const std::string projLabel = p ? *p : libName;
  1917. p = target->GetProperty("VS_KEYWORD");
  1918. const std::string keyword = p ? *p : "Win32Proj";
  1919. fout << "\tName=\"" << projLabel
  1920. << "\"\n"
  1921. "\tProjectGUID=\"{"
  1922. << gg->GetGUID(libName) << "}\"\n";
  1923. this->WriteProjectSCC(fout, target);
  1924. if (cmValue targetFrameworkVersion =
  1925. target->GetProperty("VS_DOTNET_TARGET_FRAMEWORK_VERSION")) {
  1926. fout << "\tTargetFrameworkVersion=\"" << *targetFrameworkVersion << "\"\n";
  1927. }
  1928. /* clang-format off */
  1929. fout << "\tKeyword=\"" << keyword << "\">\n"
  1930. "\t<Platforms>\n"
  1931. "\t\t<Platform\n\t\t\tName=\"" << gg->GetPlatformName() << "\"/>\n"
  1932. "\t</Platforms>\n";
  1933. /* clang-format on */
  1934. if (gg->IsMarmasmEnabled()) {
  1935. fout << "\t<ToolFiles>\n"
  1936. "\t\t<DefaultToolFile\n"
  1937. "\t\t\tFileName=\"marmasm.rules\"\n"
  1938. "\t\t/>\n"
  1939. "\t</ToolFiles>\n";
  1940. }
  1941. if (gg->IsMasmEnabled()) {
  1942. fout << "\t<ToolFiles>\n"
  1943. "\t\t<DefaultToolFile\n"
  1944. "\t\t\tFileName=\"masm.rules\"\n"
  1945. "\t\t/>\n"
  1946. "\t</ToolFiles>\n";
  1947. }
  1948. }
  1949. void cmLocalVisualStudio7Generator::WriteVCProjFooter(
  1950. std::ostream& fout, cmGeneratorTarget* target)
  1951. {
  1952. fout << "\t<Globals>\n";
  1953. for (std::string const& key : target->GetPropertyKeys()) {
  1954. if (cmHasLiteralPrefix(key, "VS_GLOBAL_")) {
  1955. std::string name = key.substr(10);
  1956. if (!name.empty()) {
  1957. fout << "\t\t<Global\n"
  1958. "\t\t\tName=\""
  1959. << name
  1960. << "\"\n"
  1961. "\t\t\tValue=\""
  1962. << target->GetProperty(key)
  1963. << "\"\n"
  1964. "\t\t/>\n";
  1965. }
  1966. }
  1967. }
  1968. fout << "\t</Globals>\n"
  1969. "</VisualStudioProject>\n";
  1970. }
  1971. std::string cmLocalVisualStudio7Generator::EscapeForXML(const std::string& s)
  1972. {
  1973. return cmLocalVisualStudio7GeneratorEscapeForXML(s);
  1974. }
  1975. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPath(
  1976. const std::string& path)
  1977. {
  1978. std::string ret =
  1979. this->ConvertToOutputFormat(path, cmOutputConverter::SHELL);
  1980. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1981. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1982. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1983. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1984. return ret;
  1985. }
  1986. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPathSingle(
  1987. const std::string& path)
  1988. {
  1989. std::string ret =
  1990. this->ConvertToOutputFormat(path, cmOutputConverter::SHELL);
  1991. cmSystemTools::ReplaceString(ret, "\"", "");
  1992. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1993. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1994. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1995. return ret;
  1996. }
  1997. void cmVS7GeneratorOptions::OutputFlag(std::ostream& fout, int indent,
  1998. const std::string& flag,
  1999. const std::string& content)
  2000. {
  2001. fout.fill('\t');
  2002. fout.width(indent);
  2003. // write an empty string to get the fill level indent to print
  2004. fout << "" << flag << "=\""
  2005. << cmLocalVisualStudio7GeneratorEscapeForXML(content) << "\"\n";
  2006. }
  2007. // This class is used to parse an existing vs 7 project
  2008. // and extract the GUID
  2009. class cmVS7XMLParser : public cmXMLParser
  2010. {
  2011. public:
  2012. void EndElement(const std::string& /* name */) override {}
  2013. void StartElement(const std::string& name, const char** atts) override
  2014. {
  2015. // once the GUID is found do nothing
  2016. if (!this->GUID.empty()) {
  2017. return;
  2018. }
  2019. int i = 0;
  2020. if ("VisualStudioProject" == name) {
  2021. while (atts[i]) {
  2022. if (strcmp(atts[i], "ProjectGUID") == 0) {
  2023. if (atts[i + 1]) {
  2024. this->GUID = atts[i + 1];
  2025. if (this->GUID[0] == '{') {
  2026. // remove surrounding curly brackets
  2027. this->GUID = this->GUID.substr(1, this->GUID.size() - 2);
  2028. }
  2029. } else {
  2030. this->GUID.clear();
  2031. }
  2032. return;
  2033. }
  2034. ++i;
  2035. }
  2036. }
  2037. }
  2038. int InitializeParser() override
  2039. {
  2040. int ret = cmXMLParser::InitializeParser();
  2041. if (ret == 0) {
  2042. return ret;
  2043. }
  2044. // visual studio projects have a strange encoding, but it is
  2045. // really utf-8
  2046. XML_SetEncoding(static_cast<XML_Parser>(this->Parser), "utf-8");
  2047. return 1;
  2048. }
  2049. std::string GUID;
  2050. };
  2051. void cmLocalVisualStudio7Generator::ReadAndStoreExternalGUID(
  2052. const std::string& name, const char* path)
  2053. {
  2054. cmVS7XMLParser parser;
  2055. parser.ParseFile(path);
  2056. // if we can not find a GUID then we will generate one later
  2057. if (parser.GUID.empty()) {
  2058. return;
  2059. }
  2060. std::string guidStoreName = cmStrCat(name, "_GUID_CMAKE");
  2061. // save the GUID in the cache
  2062. this->GlobalGenerator->GetCMakeInstance()->AddCacheEntry(
  2063. guidStoreName, parser.GUID, "Stored GUID", cmStateEnums::INTERNAL);
  2064. }
  2065. std::string cmLocalVisualStudio7Generator::GetTargetDirectory(
  2066. cmGeneratorTarget const* target) const
  2067. {
  2068. std::string dir = cmStrCat(target->GetName(), ".dir");
  2069. return dir;
  2070. }
  2071. static bool cmLVS7G_IsFAT(const char* dir)
  2072. {
  2073. if (dir[0] && dir[1] == ':') {
  2074. char volRoot[4] = "_:/";
  2075. volRoot[0] = dir[0];
  2076. char fsName[16];
  2077. if (GetVolumeInformationA(volRoot, 0, 0, 0, 0, 0, fsName, 16) &&
  2078. strstr(fsName, "FAT") != nullptr) {
  2079. return true;
  2080. }
  2081. }
  2082. return false;
  2083. }