cmLocalVisualStudio7Generator.cxx 82 KB

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