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