cmLocalVisualStudio7Generator.cxx 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGlobalVisualStudio7Generator.h"
  14. #include "cmLocalVisualStudio7Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmCacheManager.h"
  19. #include "cmake.h"
  20. #include <ctype.h> // for isspace
  21. cmLocalVisualStudio7Generator::cmLocalVisualStudio7Generator()
  22. {
  23. this->Version = 7;
  24. this->PlatformName = "Win32";
  25. }
  26. cmLocalVisualStudio7Generator::~cmLocalVisualStudio7Generator()
  27. {
  28. }
  29. void cmLocalVisualStudio7Generator::Generate()
  30. {
  31. std::set<cmStdString> lang;
  32. lang.insert("C");
  33. lang.insert("CXX");
  34. lang.insert("RC");
  35. lang.insert("IDL");
  36. lang.insert("DEF");
  37. this->CreateCustomTargetsAndCommands(lang);
  38. this->FixGlobalTargets();
  39. this->OutputVCProjFile();
  40. }
  41. void cmLocalVisualStudio7Generator::FixGlobalTargets()
  42. {
  43. // Visual Studio .NET 2003 Service Pack 1 will not run post-build
  44. // commands for targets in which no sources are built. Add dummy
  45. // rules to force these targets to build.
  46. cmTargets &tgts = this->Makefile->GetTargets();
  47. for(cmTargets::iterator l = tgts.begin();
  48. l != tgts.end(); l++)
  49. {
  50. cmTarget& tgt = l->second;
  51. if(tgt.GetType() == cmTarget::GLOBAL_TARGET)
  52. {
  53. std::vector<std::string> no_depends;
  54. cmCustomCommandLine force_command;
  55. force_command.push_back(";");
  56. cmCustomCommandLines force_commands;
  57. force_commands.push_back(force_command);
  58. const char* no_main_dependency = 0;
  59. std::string force = this->Makefile->GetStartOutputDirectory();
  60. force += cmake::GetCMakeFilesDirectory();
  61. force += "/";
  62. force += tgt.GetName();
  63. force += "_force";
  64. this->Makefile->AddCustomCommandToOutput(force.c_str(), no_depends,
  65. no_main_dependency,
  66. force_commands, " ", 0, true);
  67. if(cmSourceFile* file =
  68. this->Makefile->GetSourceFileWithOutput(force.c_str()))
  69. {
  70. tgt.GetSourceFiles().push_back(file);
  71. }
  72. }
  73. }
  74. }
  75. // TODO
  76. // for CommandLine= need to repleace quotes with &quot
  77. // write out configurations
  78. void cmLocalVisualStudio7Generator::OutputVCProjFile()
  79. {
  80. // If not an in source build, then create the output directory
  81. if(strcmp(this->Makefile->GetStartOutputDirectory(),
  82. this->Makefile->GetHomeDirectory()) != 0)
  83. {
  84. if(!cmSystemTools::MakeDirectory
  85. (this->Makefile->GetStartOutputDirectory()))
  86. {
  87. cmSystemTools::Error("Error creating directory ",
  88. this->Makefile->GetStartOutputDirectory());
  89. }
  90. }
  91. this->LibraryOutputPath = "";
  92. if (this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  93. {
  94. this->LibraryOutputPath =
  95. this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  96. }
  97. if(this->LibraryOutputPath.size())
  98. {
  99. // make sure there is a trailing slash
  100. if(this->LibraryOutputPath[this->LibraryOutputPath.size()-1] != '/')
  101. {
  102. this->LibraryOutputPath += "/";
  103. }
  104. }
  105. this->ExecutableOutputPath = "";
  106. if (this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  107. {
  108. this->ExecutableOutputPath =
  109. this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  110. }
  111. if(this->ExecutableOutputPath.size())
  112. {
  113. // make sure there is a trailing slash
  114. if(this->ExecutableOutputPath[this->ExecutableOutputPath.size()-1] != '/')
  115. {
  116. this->ExecutableOutputPath += "/";
  117. }
  118. }
  119. // Create the VCProj or set of VCProj's for libraries and executables
  120. // clear project names
  121. this->CreatedProjectNames.clear();
  122. // Call TraceVSDependencies on all targets
  123. cmTargets &tgts = this->Makefile->GetTargets();
  124. for(cmTargets::iterator l = tgts.begin();
  125. l != tgts.end(); l++)
  126. {
  127. // Add a rule to regenerate the build system when the target
  128. // specification source changes.
  129. const char* suppRegenRule =
  130. this->Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  131. if (!cmSystemTools::IsOn(suppRegenRule) &&
  132. (strcmp(l->first.c_str(), CMAKE_CHECK_BUILD_SYSTEM_TARGET) != 0))
  133. {
  134. this->AddVCProjBuildRule(l->second);
  135. }
  136. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  137. // so don't build a projectfile for it
  138. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  139. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  140. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  141. {
  142. cmTarget& target = l->second;
  143. target.TraceVSDependencies(target.GetName(), this->Makefile);
  144. }
  145. }
  146. for(cmTargets::iterator l = tgts.begin();
  147. l != tgts.end(); l++)
  148. {
  149. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  150. // so don't build a projectfile for it
  151. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  152. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  153. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  154. {
  155. this->CreateSingleVCProj(l->first.c_str(),l->second);
  156. }
  157. }
  158. }
  159. void cmLocalVisualStudio7Generator
  160. ::CreateSingleVCProj(const char *lname, cmTarget &target)
  161. {
  162. // add to the list of projects
  163. std::string pname = lname;
  164. this->CreatedProjectNames.push_back(pname);
  165. // create the dsp.cmake file
  166. std::string fname;
  167. fname = this->Makefile->GetStartOutputDirectory();
  168. fname += "/";
  169. fname += lname;
  170. fname += ".vcproj";
  171. // save the name of the real dsp file
  172. std::string realVCProj = fname;
  173. fname += ".cmake";
  174. std::ofstream fout(fname.c_str());
  175. if(!fout)
  176. {
  177. cmSystemTools::Error("Error Writing ", fname.c_str());
  178. }
  179. this->WriteVCProjFile(fout,lname,target);
  180. fout.close();
  181. // if the dsp file has changed, then write it.
  182. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realVCProj.c_str());
  183. }
  184. void cmLocalVisualStudio7Generator::AddVCProjBuildRule(cmTarget& tgt)
  185. {
  186. std::string dspname = tgt.GetName();
  187. dspname += ".vcproj.cmake";
  188. const char* dsprule =
  189. this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  190. cmCustomCommandLine commandLine;
  191. commandLine.push_back(dsprule);
  192. std::string makefileIn = this->Makefile->GetStartDirectory();
  193. makefileIn += "/";
  194. makefileIn += "CMakeLists.txt";
  195. makefileIn = cmSystemTools::CollapseFullPath(makefileIn.c_str());
  196. std::string comment = "Building Custom Rule ";
  197. comment += makefileIn;
  198. std::string args;
  199. args = "-H";
  200. args += this->Convert(this->Makefile->GetHomeDirectory(),
  201. START_OUTPUT, UNCHANGED, true);
  202. commandLine.push_back(args);
  203. args = "-B";
  204. args +=
  205. this->Convert(this->Makefile->GetHomeOutputDirectory(),
  206. START_OUTPUT, UNCHANGED, true);
  207. commandLine.push_back(args);
  208. std::string configFile =
  209. this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  210. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  211. std::vector<std::string> listFiles = this->Makefile->GetListFiles();
  212. bool found = false;
  213. for(std::vector<std::string>::iterator i = listFiles.begin();
  214. i != listFiles.end(); ++i)
  215. {
  216. if(*i == configFile)
  217. {
  218. found = true;
  219. }
  220. }
  221. if(!found)
  222. {
  223. listFiles.push_back(configFile);
  224. }
  225. cmCustomCommandLines commandLines;
  226. commandLines.push_back(commandLine);
  227. const char* no_working_directory = 0;
  228. this->Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles,
  229. makefileIn.c_str(), commandLines,
  230. comment.c_str(),
  231. no_working_directory, true);
  232. if(cmSourceFile* file = this->Makefile->GetSource(makefileIn.c_str()))
  233. {
  234. tgt.GetSourceFiles().push_back(file);
  235. }
  236. else
  237. {
  238. cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
  239. }
  240. }
  241. void cmLocalVisualStudio7Generator::WriteConfigurations(std::ostream& fout,
  242. const char *libName,
  243. cmTarget &target)
  244. {
  245. std::vector<std::string> *configs =
  246. static_cast<cmGlobalVisualStudio7Generator *>
  247. (this->GlobalGenerator)->GetConfigurations();
  248. fout << "\t<Configurations>\n";
  249. for( std::vector<std::string>::iterator i = configs->begin();
  250. i != configs->end(); ++i)
  251. {
  252. this->WriteConfiguration(fout, i->c_str(), libName, target);
  253. }
  254. fout << "\t</Configurations>\n";
  255. }
  256. // This is a table mapping XML tag IDE names to command line options
  257. struct cmVS7FlagTable
  258. {
  259. const char* IDEName; // name used in the IDE xml file
  260. const char* commandFlag; // command line flag
  261. const char* comment; // comment
  262. const char* value; // string value
  263. };
  264. // fill the table here currently the comment field is not used for
  265. // anything other than documentation NOTE: Make sure the longer
  266. // commandFlag comes FIRST!
  267. cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[] =
  268. {
  269. // option flags (some flags map to the same option)
  270. {"BasicRuntimeChecks", "GZ", "Stack frame checks", "1"},
  271. {"BasicRuntimeChecks", "RTCsu", "Both stack and uninitialized checks", "3"},
  272. {"BasicRuntimeChecks", "RTCs", "Stack frame checks", "1"},
  273. {"BasicRuntimeChecks", "RTCu", "Uninitialized Variables ", "2"},
  274. {"BasicRuntimeChecks", "RTC1", "Both stack and uninitialized checks ", "3"},
  275. {"DebugInformationFormat", "Z7", "debug format", "1"},
  276. {"DebugInformationFormat", "Zd", "debug format", "2"},
  277. {"DebugInformationFormat", "Zi", "debug format", "3"},
  278. {"DebugInformationFormat", "ZI", "debug format", "4"},
  279. {"EnableEnhancedInstructionSet", "arch:SSE2", "Use sse2 instructions", "2"},
  280. {"EnableEnhancedInstructionSet", "arch:SSE", "Use sse instructions", "1"},
  281. {"FavorSizeOrSpeed", "Ot", "Favor fast code", "1"},
  282. {"FavorSizeOrSpeed", "Os", "Favor small code", "2"},
  283. {"CompileAs", "TC", "Compile as c code", "1"},
  284. {"CompileAs", "TP", "Compile as c++ code", "2"},
  285. {"Optimization", "Od", "Non Debug", "0"},
  286. {"Optimization", "O1", "Min Size", "1"},
  287. {"Optimization", "O2", "Max Speed", "2"},
  288. {"Optimization", "Ox", "Max Optimization", "3"},
  289. {"OptimizeForProcessor", "GB", "Blended processor mode", "0"},
  290. {"OptimizeForProcessor", "G5", "Pentium", "1"},
  291. {"OptimizeForProcessor", "G6", "PPro PII PIII", "2"},
  292. {"OptimizeForProcessor", "G7", "Pentium 4 or Athlon", "3"},
  293. {"InlineFunctionExpansion", "Ob0", "no inlines", "0"},
  294. {"InlineFunctionExpansion", "Ob1", "when inline keyword", "1"},
  295. {"InlineFunctionExpansion", "Ob2", "any time you can inline", "2"},
  296. {"RuntimeLibrary", "MTd", "Multithreded debug", "1"},
  297. {"RuntimeLibrary", "MT", "Multithreded", "0"},
  298. {"RuntimeLibrary", "MDd", "Multithreded dll debug", "3"},
  299. {"RuntimeLibrary", "MD", "Multithreded dll", "2"},
  300. {"RuntimeLibrary", "MLd", "Sinble Thread debug", "5"},
  301. {"RuntimeLibrary", "ML", "Sinble Thread", "4"},
  302. {"StructMemberAlignment", "Zp16", "struct align 16 byte ", "5"},
  303. {"StructMemberAlignment", "Zp1", "struct align 1 byte ", "1"},
  304. {"StructMemberAlignment", "Zp2", "struct align 2 byte ", "2"},
  305. {"StructMemberAlignment", "Zp4", "struct align 4 byte ", "3"},
  306. {"StructMemberAlignment", "Zp8", "struct align 8 byte ", "4"},
  307. {"WarningLevel", "W1", "Warning level", "1"},
  308. {"WarningLevel", "W2", "Warning level", "2"},
  309. {"WarningLevel", "W3", "Warning level", "3"},
  310. {"WarningLevel", "W4", "Warning level", "4"},
  311. // boolean flags
  312. {"BufferSecurityCheck", "GS", "Buffer security check", "TRUE"},
  313. {"EnableFibreSafeOptimization", "GT", "OmitFramePointers", "TRUE"},
  314. {"EnableFunctionLevelLinking", "Gy", "EnableFunctionLevelLinking", "TRUE"},
  315. {"EnableIntrinsicFunctions", "Oi", "EnableIntrinsicFunctions", "TRUE"},
  316. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "TRUE"},
  317. {"ExceptionHandling", "EHa", "enable c++ exceptions", "2"},
  318. {"ExceptionHandling", "GX", "enable c++ exceptions", "TRUE"},
  319. {"GlobalOptimizations", "Og", "Global Optimize", "TRUE"},
  320. {"ImproveFloatingPointConsistency", "Op",
  321. "ImproveFloatingPointConsistency", "TRUE"},
  322. {"MinimalRebuild", "Gm", "minimal rebuild", "TRUE"},
  323. {"OmitFramePointers", "Oy", "OmitFramePointers", "TRUE"},
  324. {"OptimizeForWindowsApplication", "GA", "Optimize for windows", "TRUE"},
  325. {"RuntimeTypeInfo", "GR",
  326. "Turn on Run time type information for c++", "TRUE"},
  327. {"SmallerTypeCheck", "RTCc", "smaller type check", "TRUE"},
  328. {"SuppressStartupBanner", "nologo", "SuppressStartupBanner", "TRUE"},
  329. {"WarnAsError", "WX", "Treat warnings as errors", "TRUE"},
  330. {0,0,0,0 }
  331. };
  332. cmVS7FlagTable cmLocalVisualStudio7GeneratorLinkFlagTable[] =
  333. {
  334. // option flags (some flags map to the same option)
  335. {"GenerateManifest", "MANIFEST:NO", "disable manifest generation", "FALSE"},
  336. {"GenerateManifest", "MANIFEST", "enable manifest generation", "TRUE"},
  337. {"LinkIncremental", "INCREMENTAL:NO", "link incremental", "1"},
  338. {"LinkIncremental", "INCREMENTAL:YES", "link incremental", "2"},
  339. {0,0,0,0 }
  340. };
  341. void cmLocalVisualStudio7Generator::WriteConfiguration(std::ostream& fout,
  342. const char* configName,
  343. const char *libName,
  344. cmTarget &target)
  345. {
  346. // create a map of xml tags to the values they should have in the output
  347. // for example, "BufferSecurityCheck" = "TRUE"
  348. // first fill this table with the values for the configuration
  349. // Debug, Release, etc,
  350. // Then parse the command line flags specified in CMAKE_CXX_FLAGS
  351. // and CMAKE_C_FLAGS
  352. // and overwrite or add new values to this map
  353. std::map<cmStdString, cmStdString> flagMap;
  354. // since the default is on for this, but if /EHsc is found
  355. // in the flags it will be turned on and we have /EHSC on by
  356. // default in the CXX flags, then this is the only way to turn this off
  357. flagMap["ExceptionHandling"] = "FALSE";
  358. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  359. if(!mfcFlag)
  360. {
  361. mfcFlag = "0";
  362. }
  363. fout << "\t\t<Configuration\n"
  364. << "\t\t\tName=\"" << configName << "|" << this->PlatformName << "\"\n"
  365. << "\t\t\tOutputDirectory=\"" << configName << "\"\n";
  366. // This is an internal type to Visual Studio, it seems that:
  367. // 4 == static library
  368. // 2 == dll
  369. // 1 == executable
  370. // 10 == utility
  371. const char* configType = "10";
  372. switch(target.GetType())
  373. {
  374. case cmTarget::STATIC_LIBRARY:
  375. configType = "4";
  376. break;
  377. case cmTarget::SHARED_LIBRARY:
  378. case cmTarget::MODULE_LIBRARY:
  379. configType = "2";
  380. break;
  381. case cmTarget::EXECUTABLE:
  382. configType = "1";
  383. break;
  384. case cmTarget::UTILITY:
  385. case cmTarget::GLOBAL_TARGET:
  386. configType = "10";
  387. default:
  388. break;
  389. }
  390. std::string flags;
  391. if(strcmp(configType, "10") != 0)
  392. {
  393. const char* linkLanguage =
  394. target.GetLinkerLanguage(this->GetGlobalGenerator());
  395. if(!linkLanguage)
  396. {
  397. cmSystemTools::Error
  398. ("CMake can not determine linker language for target:",
  399. target.GetName());
  400. return;
  401. }
  402. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0)
  403. {
  404. std::string baseFlagVar = "CMAKE_";
  405. baseFlagVar += linkLanguage;
  406. baseFlagVar += "_FLAGS";
  407. flags = this->Makefile->GetRequiredDefinition(baseFlagVar.c_str());
  408. std::string flagVar = baseFlagVar + std::string("_") +
  409. cmSystemTools::UpperCase(configName);
  410. flags += " ";
  411. flags += this->Makefile->GetRequiredDefinition(flagVar.c_str());
  412. }
  413. // set the correct language
  414. if(strcmp(linkLanguage, "C") == 0)
  415. {
  416. flags += " /TC ";
  417. }
  418. if(strcmp(linkLanguage, "CXX") == 0)
  419. {
  420. flags += " /TP ";
  421. }
  422. }
  423. // Add the target-specific flags.
  424. if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
  425. {
  426. flags += " ";
  427. flags += targetFlags;
  428. }
  429. // The intermediate directory name consists of a directory for the
  430. // target and a subdirectory for the configuration name.
  431. std::string intermediateDir = this->GetTargetDirectory(target);
  432. intermediateDir += "/";
  433. intermediateDir += configName;
  434. fout << "\t\t\tIntermediateDirectory=\""
  435. << this->ConvertToXMLOutputPath(intermediateDir.c_str())
  436. << "\"\n"
  437. << "\t\t\tConfigurationType=\"" << configType << "\"\n"
  438. << "\t\t\tUseOfMFC=\"" << mfcFlag << "\"\n"
  439. << "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n";
  440. // if -D_UNICODE or /D_UNICODE is found in the flags
  441. // change the character set to unicode, if not then
  442. // default to MBCS
  443. std::string defs = this->Makefile->GetDefineFlags();
  444. if(flags.find("D_UNICODE") != flags.npos ||
  445. defs.find("D_UNICODE") != flags.npos)
  446. {
  447. fout << "\t\t\tCharacterSet=\"1\">\n";
  448. }
  449. else
  450. {
  451. fout << "\t\t\tCharacterSet=\"2\">\n";
  452. }
  453. fout << "\t\t\t<Tool\n"
  454. << "\t\t\t\tName=\"VCCLCompilerTool\"\n"
  455. << "\t\t\t\tAdditionalOptions=\"";
  456. // now fill the flagMap from the command line flags, and
  457. // if a flag is used, it will be removed from the flags string by
  458. // this function call
  459. this->FillFlagMapFromCommandFlags
  460. (flagMap, &cmLocalVisualStudio7GeneratorFlagTable[0], flags);
  461. std::string defineFlags = this->Makefile->GetDefineFlags();
  462. // now check the define flags for flags other than -D and
  463. // put them in the map, the -D flags will be left in the defineFlags
  464. // variable as -D is not in the flagMap
  465. this->FillFlagMapFromCommandFlags
  466. (flagMap, &cmLocalVisualStudio7GeneratorFlagTable[0], defineFlags);
  467. // output remaining flags that were not mapped to anything
  468. fout << this->EscapeForXML(flags.c_str()).c_str();
  469. fout << " -DCMAKE_INTDIR=\\&quot;" << configName << "\\&quot;"
  470. << "\"\n";
  471. fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
  472. std::vector<std::string> includes;
  473. this->GetIncludeDirectories(includes);
  474. std::vector<std::string>::iterator i = includes.begin();
  475. for(;i != includes.end(); ++i)
  476. {
  477. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  478. fout << ipath << ";";
  479. }
  480. fout << "\"\n";
  481. // set a few cmake specific flags
  482. if(this->Makefile->IsOn("CMAKE_CXX_USE_RTTI"))
  483. {
  484. flagMap["RuntimeTypeInfo"] = "TRUE";
  485. }
  486. if ( this->Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL") )
  487. {
  488. flagMap["WarningLevel"] =
  489. this->Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL");
  490. }
  491. // Now copy the flag map into the xml for the file
  492. for(std::map<cmStdString, cmStdString>::iterator m = flagMap.begin();
  493. m != flagMap.end(); ++m)
  494. {
  495. fout << "\t\t\t\t" << m->first << "=\"" << m->second << "\"\n";
  496. }
  497. fout << "\t\t\t\tPreprocessorDefinitions=\"";
  498. if(target.GetType() == cmTarget::SHARED_LIBRARY
  499. || target.GetType() == cmTarget::MODULE_LIBRARY)
  500. {
  501. std::string exportSymbol;
  502. if (const char* custom_export_name =
  503. target.GetProperty("DEFINE_SYMBOL"))
  504. {
  505. exportSymbol = custom_export_name;
  506. }
  507. else
  508. {
  509. std::string id = libName;
  510. id += "_EXPORTS";
  511. exportSymbol = cmSystemTools::MakeCindentifier(id.c_str());
  512. }
  513. fout << "," << exportSymbol;
  514. }
  515. this->OutputDefineFlags(defineFlags.c_str(), fout);
  516. fout << "\"\n";
  517. fout << "\t\t\t\tAssemblerListingLocation=\"" << configName << "\"\n";
  518. fout << "\t\t\t\tObjectFile=\"$(IntDir)\\\"\n";
  519. std::map<cmStdString, cmStdString>::iterator mi =
  520. flagMap.find("DebugInformationFormat");
  521. if(mi != flagMap.end() && mi->second != "1")
  522. {
  523. fout << "\t\t\t\tProgramDataBaseFileName=\""
  524. << this->LibraryOutputPath
  525. << "$(OutDir)/" << libName << ".pdb\"\n";
  526. }
  527. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  528. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  529. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCResourceCompilerTool\"\n"
  530. << "\t\t\t\tAdditionalIncludeDirectories=\"";
  531. for(i = includes.begin();i != includes.end(); ++i)
  532. {
  533. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  534. fout << ipath << ";";
  535. }
  536. // add the -D flags to the RC tool
  537. fout << "\"\n"
  538. << "\t\t\t\tPreprocessorDefinitions=\"";
  539. this->OutputDefineFlags(defineFlags.c_str(), fout);
  540. fout << "\" />\n";
  541. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCMIDLTool\"\n";
  542. fout << "\t\t\t\tPreprocessorDefinitions=\"";
  543. this->OutputDefineFlags(defineFlags.c_str(), fout);
  544. fout << "\"\n";
  545. fout << "\t\t\t\tMkTypLibCompatible=\"FALSE\"\n";
  546. if( this->PlatformName == "x64" )
  547. {
  548. fout << "\t\t\t\tTargetEnvironment=\"3\"\n";
  549. }
  550. else if( this->PlatformName == "ia64" )
  551. {
  552. fout << "\t\t\t\tTargetEnvironment=\"2\"\n";
  553. }
  554. else
  555. {
  556. fout << "\t\t\t\tTargetEnvironment=\"1\"\n";
  557. }
  558. fout << "\t\t\t\tGenerateStublessProxies=\"TRUE\"\n";
  559. fout << "\t\t\t\tTypeLibraryName=\"$(InputName).tlb\"\n";
  560. fout << "\t\t\t\tOutputDirectory=\"$(IntDir)\"\n";
  561. fout << "\t\t\t\tHeaderFileName=\"$(InputName).h\"\n";
  562. fout << "\t\t\t\tDLLDataFileName=\"\"\n";
  563. fout << "\t\t\t\tInterfaceIdentifierFileName=\"$(InputName)_i.c\"\n";
  564. fout << "\t\t\t\tProxyFileName=\"$(InputName)_p.c\"/>\n";
  565. // end of <Tool Name=VCMIDLTool
  566. // If we are building a version 8 project file, add a flag telling the
  567. // manifest tool to use a workaround for FAT32 file systems, which can cause
  568. // an empty manifest to be embedded into the resulting executable.
  569. // See CMake bug #2617.
  570. if ( this->Version == 8 )
  571. {
  572. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCManifestTool\"\n"
  573. << "\t\t\t\tUseFAT32Workaround=\"true\"\n"
  574. << "\t\t\t/>\n";
  575. }
  576. this->OutputTargetRules(fout, target, libName);
  577. this->OutputBuildTool(fout, configName, libName, target);
  578. fout << "\t\t</Configuration>\n";
  579. }
  580. void cmLocalVisualStudio7Generator::FillFlagMapFromCommandFlags(
  581. std::map<cmStdString, cmStdString>& flagMap,
  582. cmVS7FlagTable* flagTable,
  583. std::string& flags)
  584. {
  585. std::string replace;
  586. std::string option;
  587. while(flagTable->IDEName)
  588. {
  589. option.reserve(strlen(flagTable->commandFlag)+2);
  590. // first do the - version
  591. option = "-";
  592. option += flagTable->commandFlag;
  593. while(flags.find(option) != flags.npos)
  594. {
  595. // replace the flag
  596. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  597. // now put value into flag map
  598. flagMap[flagTable->IDEName] = flagTable->value;
  599. }
  600. // now do the / version
  601. option[0] = '/';
  602. while(flags.find(option) != flags.npos)
  603. {
  604. // replace the flag
  605. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  606. // now put value into flag map
  607. flagMap[flagTable->IDEName] = flagTable->value;
  608. }
  609. // move to next flag
  610. flagTable++;
  611. }
  612. // If verbose makefiles have been requested and the /nologo option
  613. // was not given explicitly in the flags we want to add an attribute
  614. // to the generated project to disable logo suppression. Otherwise
  615. // the GUI default is to enable suppression.
  616. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  617. {
  618. if(flagMap.find("SuppressStartupBanner") == flagMap.end())
  619. {
  620. flagMap["SuppressStartupBanner"] = "FALSE";
  621. }
  622. }
  623. }
  624. //----------------------------------------------------------------------------
  625. std::string
  626. cmLocalVisualStudio7Generator
  627. ::GetBuildTypeLinkerFlags(std::string rootLinkerFlags, const char* configName)
  628. {
  629. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  630. std::string extraLinkOptionsBuildTypeDef =
  631. rootLinkerFlags + "_" + configTypeUpper;
  632. std::string extraLinkOptionsBuildType =
  633. this->Makefile->GetRequiredDefinition
  634. (extraLinkOptionsBuildTypeDef.c_str());
  635. return extraLinkOptionsBuildType;
  636. }
  637. void cmLocalVisualStudio7Generator::OutputBuildTool(std::ostream& fout,
  638. const char* configName,
  639. const char *libName,
  640. cmTarget &target)
  641. {
  642. std::string targetFullName = target.GetFullName(configName);
  643. std::string temp;
  644. std::string extraLinkOptions;
  645. if(target.GetType() == cmTarget::EXECUTABLE)
  646. {
  647. extraLinkOptions =
  648. this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS")
  649. + std::string(" ")
  650. + GetBuildTypeLinkerFlags("CMAKE_EXE_LINKER_FLAGS", configName);
  651. }
  652. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  653. {
  654. extraLinkOptions =
  655. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS")
  656. + std::string(" ")
  657. + GetBuildTypeLinkerFlags("CMAKE_SHARED_LINKER_FLAGS", configName);
  658. }
  659. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  660. {
  661. extraLinkOptions =
  662. this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS")
  663. + std::string(" ")
  664. + GetBuildTypeLinkerFlags("CMAKE_MODULE_LINKER_FLAGS", configName);
  665. }
  666. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  667. if(targetLinkFlags)
  668. {
  669. extraLinkOptions += " ";
  670. extraLinkOptions += targetLinkFlags;
  671. }
  672. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  673. std::string linkFlagsConfig = "LINK_FLAGS_";
  674. linkFlagsConfig += configTypeUpper;
  675. targetLinkFlags = target.GetProperty(linkFlagsConfig.c_str());
  676. if(targetLinkFlags)
  677. {
  678. extraLinkOptions += " ";
  679. extraLinkOptions += targetLinkFlags;
  680. }
  681. std::map<cmStdString, cmStdString> flagMap;
  682. this->FillFlagMapFromCommandFlags
  683. (flagMap, &cmLocalVisualStudio7GeneratorLinkFlagTable[0],
  684. extraLinkOptions);
  685. switch(target.GetType())
  686. {
  687. case cmTarget::STATIC_LIBRARY:
  688. {
  689. std::string libpath = this->LibraryOutputPath +
  690. "$(OutDir)/" + targetFullName;
  691. fout << "\t\t\t<Tool\n"
  692. << "\t\t\t\tName=\"VCLibrarianTool\"\n";
  693. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  694. {
  695. fout << "\t\t\t\tAdditionalOptions=\"" << libflags << "\"\n";
  696. }
  697. fout << "\t\t\t\tOutputFile=\""
  698. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << "\"/>\n";
  699. break;
  700. }
  701. case cmTarget::SHARED_LIBRARY:
  702. case cmTarget::MODULE_LIBRARY:
  703. {
  704. // Compute the link library and directory information.
  705. std::vector<cmStdString> linkLibs;
  706. std::vector<cmStdString> linkDirs;
  707. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  708. // Get the language to use for linking.
  709. const char* linkLanguage =
  710. target.GetLinkerLanguage(this->GetGlobalGenerator());
  711. if(!linkLanguage)
  712. {
  713. cmSystemTools::Error
  714. ("CMake can not determine linker language for target:",
  715. target.GetName());
  716. return;
  717. }
  718. // Compute the variable name to lookup standard libraries for this
  719. // language.
  720. std::string standardLibsVar = "CMAKE_";
  721. standardLibsVar += linkLanguage;
  722. standardLibsVar += "_STANDARD_LIBRARIES";
  723. fout << "\t\t\t<Tool\n"
  724. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  725. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  726. if(extraLinkOptions.size())
  727. {
  728. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  729. extraLinkOptions.c_str()).c_str();
  730. }
  731. // Use the NOINHERIT macro to avoid getting VS project default
  732. // libraries which may be set by the user to something bad.
  733. fout << "\"\n"
  734. << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  735. << this->Makefile->GetSafeDefinition(standardLibsVar.c_str())
  736. << " ";
  737. this->OutputLibraries(fout, linkLibs);
  738. fout << "\"\n";
  739. temp = this->LibraryOutputPath;
  740. temp += configName;
  741. temp += "/";
  742. temp += targetFullName;
  743. fout << "\t\t\t\tOutputFile=\""
  744. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  745. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  746. i != flagMap.end(); ++i)
  747. {
  748. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  749. }
  750. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  751. this->OutputLibraryDirectories(fout, linkDirs);
  752. fout << "\"\n";
  753. this->OutputModuleDefinitionFile(fout, target);
  754. temp = this->LibraryOutputPath;
  755. temp += "$(OutDir)/";
  756. temp += libName;
  757. temp += ".pdb";
  758. fout << "\t\t\t\tProgramDataBaseFile=\"" <<
  759. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  760. if(strcmp(configName, "Debug") == 0
  761. || strcmp(configName, "RelWithDebInfo") == 0)
  762. {
  763. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  764. }
  765. std::string stackVar = "CMAKE_";
  766. stackVar += linkLanguage;
  767. stackVar += "_STACK_SIZE";
  768. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  769. if(stackVal)
  770. {
  771. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"\n";
  772. }
  773. temp = this->LibraryOutputPath;
  774. temp += configName;
  775. temp += "/";
  776. temp +=
  777. cmSystemTools::GetFilenameWithoutLastExtension(targetFullName.c_str());
  778. temp += ".lib";
  779. fout << "\t\t\t\tImportLibrary=\""
  780. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  781. }
  782. break;
  783. case cmTarget::EXECUTABLE:
  784. {
  785. // Compute the link library and directory information.
  786. std::vector<cmStdString> linkLibs;
  787. std::vector<cmStdString> linkDirs;
  788. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  789. // Get the language to use for linking.
  790. const char* linkLanguage =
  791. target.GetLinkerLanguage(this->GetGlobalGenerator());
  792. if(!linkLanguage)
  793. {
  794. cmSystemTools::Error
  795. ("CMake can not determine linker language for target:",
  796. target.GetName());
  797. return;
  798. }
  799. // Compute the variable name to lookup standard libraries for this
  800. // language.
  801. std::string standardLibsVar = "CMAKE_";
  802. standardLibsVar += linkLanguage;
  803. standardLibsVar += "_STANDARD_LIBRARIES";
  804. fout << "\t\t\t<Tool\n"
  805. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  806. << "\t\t\t\tAdditionalOptions=\"";
  807. if(extraLinkOptions.size())
  808. {
  809. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  810. extraLinkOptions.c_str()).c_str();
  811. }
  812. // Use the NOINHERIT macro to avoid getting VS project default
  813. // libraries which may be set by the user to something bad.
  814. fout << "\"\n"
  815. << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  816. << this->Makefile->GetSafeDefinition(standardLibsVar.c_str())
  817. << " ";
  818. this->OutputLibraries(fout, linkLibs);
  819. fout << "\"\n";
  820. temp = this->ExecutableOutputPath;
  821. temp += configName;
  822. temp += "/";
  823. temp += targetFullName;
  824. fout << "\t\t\t\tOutputFile=\""
  825. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  826. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  827. i != flagMap.end(); ++i)
  828. {
  829. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  830. }
  831. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  832. this->OutputLibraryDirectories(fout, linkDirs);
  833. fout << "\"\n";
  834. fout << "\t\t\t\tProgramDataBaseFile=\"" << this->LibraryOutputPath
  835. << "$(OutDir)\\" << libName << ".pdb\"\n";
  836. if(strcmp(configName, "Debug") == 0
  837. || strcmp(configName, "RelWithDebInfo") == 0)
  838. {
  839. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  840. }
  841. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  842. {
  843. fout << "\t\t\t\tSubSystem=\"2\"\n";
  844. }
  845. else
  846. {
  847. fout << "\t\t\t\tSubSystem=\"1\"\n";
  848. }
  849. std::string stackVar = "CMAKE_";
  850. stackVar += linkLanguage;
  851. stackVar += "_STACK_SIZE";
  852. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  853. if(stackVal)
  854. {
  855. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"";
  856. }
  857. fout << "/>\n";
  858. break;
  859. }
  860. case cmTarget::UTILITY:
  861. case cmTarget::GLOBAL_TARGET:
  862. break;
  863. }
  864. }
  865. void cmLocalVisualStudio7Generator
  866. ::OutputModuleDefinitionFile(std::ostream& fout,
  867. cmTarget &target)
  868. {
  869. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  870. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  871. i != classes.end(); i++)
  872. {
  873. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  874. {
  875. fout << "\t\t\t\tModuleDefinitionFile=\""
  876. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  877. << "\"\n";
  878. return;
  879. }
  880. }
  881. }
  882. //----------------------------------------------------------------------------
  883. void
  884. cmLocalVisualStudio7Generator
  885. ::OutputLibraries(std::ostream& fout,
  886. std::vector<cmStdString> const& libs)
  887. {
  888. for(std::vector<cmStdString>::const_iterator l = libs.begin();
  889. l != libs.end(); ++l)
  890. {
  891. fout << this->ConvertToXMLOutputPath(l->c_str()) << " ";
  892. }
  893. }
  894. //----------------------------------------------------------------------------
  895. void
  896. cmLocalVisualStudio7Generator
  897. ::OutputLibraryDirectories(std::ostream& fout,
  898. std::vector<cmStdString> const& dirs)
  899. {
  900. const char* comma = "";
  901. for(std::vector<cmStdString>::const_iterator d = dirs.begin();
  902. d != dirs.end(); ++d)
  903. {
  904. // Remove any trailing slash and skip empty paths.
  905. std::string dir = *d;
  906. if(dir[dir.size()-1] == '/')
  907. {
  908. dir = dir.substr(0, dir.size()-1);
  909. }
  910. if(dir.empty())
  911. {
  912. continue;
  913. }
  914. // Switch to a relative path specification if it is shorter.
  915. if(cmSystemTools::FileIsFullPath(dir.c_str()))
  916. {
  917. std::string rel = this->Convert(dir.c_str(), START_OUTPUT, UNCHANGED);
  918. if(rel.size() < dir.size())
  919. {
  920. dir = rel;
  921. }
  922. }
  923. // First search a configuration-specific subdirectory and then the
  924. // original directory.
  925. fout << comma << this->ConvertToXMLOutputPath((dir+"/$(OutDir)").c_str())
  926. << "," << this->ConvertToXMLOutputPath(dir.c_str());
  927. comma = ",";
  928. }
  929. }
  930. //----------------------------------------------------------------------------
  931. void cmLocalVisualStudio7Generator::OutputDefineFlags(const char* flags,
  932. std::ostream& fout)
  933. {
  934. std::string defs = flags;
  935. cmSystemTools::ReplaceString(defs, "/D","-D");
  936. std::string::size_type pos = defs.find("-D");
  937. bool done = pos == std::string::npos;
  938. if(!done)
  939. {
  940. fout << ",";
  941. }
  942. while(!done)
  943. {
  944. std::string::size_type nextpos = defs.find("-D", pos+2);
  945. std::string define;
  946. if(nextpos != std::string::npos)
  947. {
  948. define = defs.substr(pos+2, nextpos - pos -3);
  949. }
  950. else
  951. {
  952. define = defs.substr(pos+2);
  953. done = true;
  954. }
  955. // Remove trailing whitespace from the definition.
  956. while(!define.empty() && isspace(define[define.size()-1]))
  957. {
  958. define = define.substr(0, define.size()-1);
  959. }
  960. // Double-quotes in the value of the definition must be escaped
  961. // with a backslash. The entire definition should be quoted in
  962. // the generated xml attribute to avoid confusing the VS parser.
  963. define = this->EscapeForXML(define.c_str());
  964. // if the define has something in it that is not a letter or a number
  965. // then quote it
  966. if(define.
  967. find_first_not_of(
  968. "-_abcdefghigklmnopqrstuvwxyz1234567890ABCDEFGHIGKLMNOPQRSTUVWXYZ")
  969. != define.npos)
  970. {
  971. fout << "&quot;" << define << "&quot;,";
  972. }
  973. else
  974. {
  975. fout << define << ",";
  976. }
  977. if(!done)
  978. {
  979. pos = defs.find("-D", nextpos);
  980. }
  981. }
  982. }
  983. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  984. const char *libName,
  985. cmTarget &target)
  986. {
  987. // get the configurations
  988. std::vector<std::string> *configs =
  989. static_cast<cmGlobalVisualStudio7Generator *>
  990. (this->GlobalGenerator)->GetConfigurations();
  991. // trace the visual studio dependencies
  992. std::string name = libName;
  993. name += ".vcproj.cmake";
  994. // We may be modifying the source groups temporarily, so make a copy.
  995. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  996. // get the classes from the source lists then add them to the groups
  997. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  998. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  999. i != classes.end(); i++)
  1000. {
  1001. // Add the file to the list of sources.
  1002. std::string source = (*i)->GetFullPath();
  1003. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  1004. {
  1005. this->ModuleDefinitionFile = (*i)->GetFullPath();
  1006. }
  1007. cmSourceGroup& sourceGroup =
  1008. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  1009. sourceGroup.AssignSource(*i);
  1010. }
  1011. // Compute which sources need unique object computation.
  1012. this->ComputeObjectNameRequirements(sourceGroups);
  1013. // open the project
  1014. this->WriteProjectStart(fout, libName, target, sourceGroups);
  1015. // write the configuration information
  1016. this->WriteConfigurations(fout, libName, target);
  1017. fout << "\t<Files>\n";
  1018. // Loop through every source group.
  1019. for(unsigned int i = 0; i < sourceGroups.size(); ++i)
  1020. {
  1021. cmSourceGroup sg = sourceGroups[i];
  1022. this->WriteGroup(&sg, target, fout, libName, configs);
  1023. }
  1024. //}
  1025. fout << "\t</Files>\n";
  1026. // Write the VCProj file's footer.
  1027. this->WriteVCProjFooter(fout);
  1028. }
  1029. void cmLocalVisualStudio7Generator
  1030. ::WriteGroup(const cmSourceGroup *sg, cmTarget target,
  1031. std::ostream &fout, const char *libName,
  1032. std::vector<std::string> *configs)
  1033. {
  1034. const std::vector<const cmSourceFile *> &sourceFiles =
  1035. sg->GetSourceFiles();
  1036. // If the group is empty, don't write it at all.
  1037. if(sourceFiles.empty() && sg->GetGroupChildren().empty())
  1038. {
  1039. return;
  1040. }
  1041. // If the group has a name, write the header.
  1042. std::string name = sg->GetName();
  1043. if(name != "")
  1044. {
  1045. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  1046. }
  1047. // Loop through each source in the source group.
  1048. std::string objectName;
  1049. for(std::vector<const cmSourceFile *>::const_iterator sf =
  1050. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  1051. {
  1052. std::string source = (*sf)->GetFullPath();
  1053. const cmCustomCommand *command = (*sf)->GetCustomCommand();
  1054. std::string compileFlags;
  1055. std::string additionalDeps;
  1056. if(this->NeedObjectName.find(*sf) != this->NeedObjectName.end())
  1057. {
  1058. objectName = this->GetObjectFileNameWithoutTarget(*(*sf));
  1059. }
  1060. else
  1061. {
  1062. objectName = "";
  1063. }
  1064. // Add per-source flags.
  1065. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  1066. if(cflags)
  1067. {
  1068. compileFlags += " ";
  1069. compileFlags += cflags;
  1070. }
  1071. const char* lang = this->GlobalGenerator->GetLanguageFromExtension
  1072. ((*sf)->GetSourceExtension().c_str());
  1073. const char* linkLanguage = target.GetLinkerLanguage
  1074. (this->GetGlobalGenerator());
  1075. // if the source file does not match the linker language
  1076. // then force c or c++
  1077. if(linkLanguage && lang && strcmp(lang, linkLanguage) != 0)
  1078. {
  1079. if(strcmp(lang, "CXX") == 0)
  1080. {
  1081. // force a C++ file type
  1082. compileFlags += " /TP ";
  1083. }
  1084. else if(strcmp(lang, "C") == 0)
  1085. {
  1086. // force to c
  1087. compileFlags += " /TC ";
  1088. }
  1089. }
  1090. // Check for extra object-file dependencies.
  1091. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  1092. if(deps)
  1093. {
  1094. std::vector<std::string> depends;
  1095. cmSystemTools::ExpandListArgument(deps, depends);
  1096. if(!depends.empty())
  1097. {
  1098. std::vector<std::string>::iterator i = depends.begin();
  1099. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  1100. for(++i;i != depends.end(); ++i)
  1101. {
  1102. additionalDeps += ";";
  1103. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  1104. }
  1105. }
  1106. }
  1107. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  1108. target.GetType() == cmTarget::GLOBAL_TARGET )
  1109. {
  1110. fout << "\t\t\t<File\n";
  1111. std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
  1112. // Tell MS-Dev what the source is. If the compiler knows how to
  1113. // build it, then it will.
  1114. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  1115. if (command)
  1116. {
  1117. // Construct the entire set of commands in one string.
  1118. std::string script =
  1119. this->ConstructScript(command->GetCommandLines(),
  1120. command->GetWorkingDirectory(),
  1121. command->GetEscapeOldStyle(),
  1122. command->GetEscapeAllowMakeVars());
  1123. std::string comment = this->ConstructComment(*command);
  1124. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  1125. this->WriteCustomRule(fout, source.c_str(), script.c_str(),
  1126. comment.c_str(), command->GetDepends(),
  1127. command->GetOutputs(), flags);
  1128. }
  1129. else if(compileFlags.size() || additionalDeps.length()
  1130. || objectName.size())
  1131. {
  1132. const char* aCompilerTool = "VCCLCompilerTool";
  1133. std::string ext = (*sf)->GetSourceExtension();
  1134. ext = cmSystemTools::LowerCase(ext);
  1135. if(ext == "idl")
  1136. {
  1137. aCompilerTool = "VCMIDLTool";
  1138. }
  1139. if(ext == "rc")
  1140. {
  1141. aCompilerTool = "VCResourceCompilerTool";
  1142. }
  1143. if(ext == "def")
  1144. {
  1145. aCompilerTool = "VCCustomBuildTool";
  1146. }
  1147. for(std::vector<std::string>::iterator i = configs->begin();
  1148. i != configs->end(); ++i)
  1149. {
  1150. fout << "\t\t\t\t<FileConfiguration\n"
  1151. << "\t\t\t\t\tName=\"" << *i
  1152. << "|" << this->PlatformName << "\">\n"
  1153. << "\t\t\t\t\t<Tool\n"
  1154. << "\t\t\t\t\tName=\"" << aCompilerTool << "\"\n";
  1155. if(compileFlags.size())
  1156. {
  1157. std::string compileFlagsCopy = compileFlags;
  1158. std::map<cmStdString, cmStdString> fileFlagMap;
  1159. this->FillFlagMapFromCommandFlags
  1160. (fileFlagMap,
  1161. &cmLocalVisualStudio7GeneratorFlagTable[0],
  1162. compileFlagsCopy);
  1163. if(compileFlagsCopy.size() &&
  1164. compileFlagsCopy.find_first_not_of(" ")
  1165. != compileFlagsCopy.npos)
  1166. {
  1167. fout << "\t\t\t\t\tAdditionalOptions=\""
  1168. << this->EscapeForXML(compileFlagsCopy.c_str()) << "\"\n";
  1169. }
  1170. for(std::map<cmStdString,
  1171. cmStdString>::iterator m = fileFlagMap.begin();
  1172. m != fileFlagMap.end(); ++m)
  1173. {
  1174. fout << "\t\t\t\t\t" << m->first << "=\""
  1175. << m->second << "\"\n";
  1176. }
  1177. }
  1178. if(additionalDeps.length())
  1179. {
  1180. fout << "\t\t\t\t\tAdditionalDependencies=\""
  1181. << additionalDeps.c_str() << "\"\n";
  1182. }
  1183. if(objectName.size())
  1184. {
  1185. fout << "\t\t\t\t\tObjectFile=\"$(IntDir)/"
  1186. << objectName.c_str() << "\"\n";
  1187. }
  1188. fout << "\t\t\t\t\t/>\n"
  1189. << "\t\t\t\t</FileConfiguration>\n";
  1190. }
  1191. }
  1192. fout << "\t\t\t</File>\n";
  1193. }
  1194. }
  1195. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  1196. for(unsigned int i=0;i<children.size();++i)
  1197. {
  1198. this->WriteGroup(&children[i], target, fout, libName, configs);
  1199. }
  1200. // If the group has a name, write the footer.
  1201. if(name != "")
  1202. {
  1203. this->WriteVCProjEndGroup(fout);
  1204. }
  1205. }
  1206. void cmLocalVisualStudio7Generator::
  1207. WriteCustomRule(std::ostream& fout,
  1208. const char* source,
  1209. const char* command,
  1210. const char* comment,
  1211. const std::vector<std::string>& depends,
  1212. const std::vector<std::string>& outputs,
  1213. const char* compileFlags)
  1214. {
  1215. // Write the rule for each configuration.
  1216. std::vector<std::string>::iterator i;
  1217. std::vector<std::string> *configs =
  1218. static_cast<cmGlobalVisualStudio7Generator *>
  1219. (this->GlobalGenerator)->GetConfigurations();
  1220. for(i = configs->begin(); i != configs->end(); ++i)
  1221. {
  1222. fout << "\t\t\t\t<FileConfiguration\n";
  1223. fout << "\t\t\t\t\tName=\"" << *i << "|" << this->PlatformName << "\">\n";
  1224. if(compileFlags)
  1225. {
  1226. fout << "\t\t\t\t\t<Tool\n"
  1227. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1228. << "\t\t\t\t\tAdditionalOptions=\""
  1229. << this->EscapeForXML(compileFlags) << "\"/>\n";
  1230. }
  1231. fout << "\t\t\t\t\t<Tool\n"
  1232. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  1233. << "\t\t\t\t\tDescription=\""
  1234. << this->EscapeForXML(comment) << "\"\n"
  1235. << "\t\t\t\t\tCommandLine=\""
  1236. << this->EscapeForXML(command) << "\"\n"
  1237. << "\t\t\t\t\tAdditionalDependencies=\"";
  1238. if(depends.empty())
  1239. {
  1240. // There are no real dependencies. Produce an artificial one to
  1241. // make sure the rule runs reliably.
  1242. if(!cmSystemTools::FileExists(source))
  1243. {
  1244. std::ofstream depout(source);
  1245. depout << "Artificial dependency for a custom command.\n";
  1246. }
  1247. fout << this->ConvertToXMLOutputPath(source);
  1248. }
  1249. else
  1250. {
  1251. // Write out the dependencies for the rule.
  1252. std::string temp;
  1253. for(std::vector<std::string>::const_iterator d = depends.begin();
  1254. d != depends.end(); ++d)
  1255. {
  1256. // Get the real name of the dependency in case it is a CMake target.
  1257. std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
  1258. fout << this->ConvertToXMLOutputPath(dep.c_str())
  1259. << ";";
  1260. }
  1261. }
  1262. fout << "\"\n";
  1263. fout << "\t\t\t\t\tOutputs=\"";
  1264. if(outputs.empty())
  1265. {
  1266. fout << source << "_force";
  1267. }
  1268. else
  1269. {
  1270. // Write a rule for the output generated by this command.
  1271. const char* sep = "";
  1272. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1273. o != outputs.end(); ++o)
  1274. {
  1275. fout << sep << this->ConvertToXMLOutputPathSingle(o->c_str());
  1276. sep = ";";
  1277. }
  1278. }
  1279. fout << "\"/>\n";
  1280. fout << "\t\t\t\t</FileConfiguration>\n";
  1281. }
  1282. }
  1283. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  1284. const char* group,
  1285. const char* )
  1286. {
  1287. fout << "\t\t<Filter\n"
  1288. << "\t\t\tName=\"" << group << "\"\n"
  1289. << "\t\t\tFilter=\"\">\n";
  1290. }
  1291. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  1292. {
  1293. fout << "\t\t</Filter>\n";
  1294. }
  1295. // look for custom rules on a target and collect them together
  1296. void cmLocalVisualStudio7Generator
  1297. ::OutputTargetRules(std::ostream& fout,
  1298. cmTarget &target,
  1299. const char * /*libName*/)
  1300. {
  1301. if (target.GetType() > cmTarget::GLOBAL_TARGET)
  1302. {
  1303. return;
  1304. }
  1305. // add the pre build rules
  1306. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  1307. bool init = false;
  1308. for (std::vector<cmCustomCommand>::const_iterator cr =
  1309. target.GetPreBuildCommands().begin();
  1310. cr != target.GetPreBuildCommands().end(); ++cr)
  1311. {
  1312. if(!init)
  1313. {
  1314. fout << "\nCommandLine=\"";
  1315. init = true;
  1316. }
  1317. std::string script =
  1318. this->ConstructScript(cr->GetCommandLines(),
  1319. cr->GetWorkingDirectory(),
  1320. cr->GetEscapeOldStyle(),
  1321. cr->GetEscapeAllowMakeVars());
  1322. fout << this->EscapeForXML(script.c_str()).c_str();
  1323. }
  1324. if (init)
  1325. {
  1326. fout << "\"";
  1327. }
  1328. fout << "/>\n";
  1329. // add the pre Link rules
  1330. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  1331. init = false;
  1332. for (std::vector<cmCustomCommand>::const_iterator cr =
  1333. target.GetPreLinkCommands().begin();
  1334. cr != target.GetPreLinkCommands().end(); ++cr)
  1335. {
  1336. if(!init)
  1337. {
  1338. fout << "\nCommandLine=\"";
  1339. init = true;
  1340. }
  1341. std::string script =
  1342. this->ConstructScript(cr->GetCommandLines(),
  1343. cr->GetWorkingDirectory(),
  1344. cr->GetEscapeOldStyle(),
  1345. cr->GetEscapeAllowMakeVars());
  1346. fout << this->EscapeForXML(script.c_str()).c_str();
  1347. }
  1348. if (init)
  1349. {
  1350. fout << "\"";
  1351. }
  1352. fout << "/>\n";
  1353. // add the PostBuild rules
  1354. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  1355. init = false;
  1356. for (std::vector<cmCustomCommand>::const_iterator cr =
  1357. target.GetPostBuildCommands().begin();
  1358. cr != target.GetPostBuildCommands().end(); ++cr)
  1359. {
  1360. if(!init)
  1361. {
  1362. fout << "\nCommandLine=\"";
  1363. init = true;
  1364. }
  1365. std::string script =
  1366. this->ConstructScript(cr->GetCommandLines(),
  1367. cr->GetWorkingDirectory(),
  1368. cr->GetEscapeOldStyle(),
  1369. cr->GetEscapeAllowMakeVars());
  1370. fout << this->EscapeForXML(script.c_str()).c_str();
  1371. }
  1372. if (init)
  1373. {
  1374. fout << "\"";
  1375. }
  1376. fout << "/>\n";
  1377. }
  1378. void
  1379. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  1380. const char *libName,
  1381. cmTarget & target,
  1382. std::vector<cmSourceGroup> &)
  1383. {
  1384. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  1385. << "<VisualStudioProject\n"
  1386. << "\tProjectType=\"Visual C++\"\n";
  1387. if(this->Version == 71)
  1388. {
  1389. fout << "\tVersion=\"7.10\"\n";
  1390. }
  1391. else
  1392. {
  1393. if (this->Version == 8)
  1394. {
  1395. fout << "\tVersion=\"8.00\"\n";
  1396. }
  1397. else
  1398. {
  1399. fout << "\tVersion=\"7.00\"\n";
  1400. }
  1401. }
  1402. const char* projLabel = target.GetProperty("PROJECT_LABEL");
  1403. if(!projLabel)
  1404. {
  1405. projLabel = libName;
  1406. }
  1407. const char* keyword = target.GetProperty("VS_KEYWORD");
  1408. if(!keyword)
  1409. {
  1410. keyword = "Win32Proj";
  1411. }
  1412. cmGlobalVisualStudio7Generator* gg =
  1413. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1414. fout << "\tName=\"" << projLabel << "\"\n";
  1415. if(this->Version == 8)
  1416. {
  1417. fout << "\tProjectGUID=\"{" << gg->GetGUID(libName) << "}\"\n";
  1418. }
  1419. fout << "\tSccProjectName=\"\"\n"
  1420. << "\tSccLocalPath=\"\"\n"
  1421. << "\tKeyword=\"" << keyword << "\">\n"
  1422. << "\t<Platforms>\n"
  1423. << "\t\t<Platform\n\t\t\tName=\"" << this->PlatformName << "\"/>\n"
  1424. << "\t</Platforms>\n";
  1425. }
  1426. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  1427. {
  1428. fout << "\t<Globals>\n"
  1429. << "\t</Globals>\n"
  1430. << "</VisualStudioProject>\n";
  1431. }
  1432. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  1433. {
  1434. std::string ret = s;
  1435. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1436. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1437. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1438. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1439. cmSystemTools::ReplaceString(ret, "\n", "&#x0D;&#x0A;");
  1440. return ret;
  1441. }
  1442. std::string cmLocalVisualStudio7Generator
  1443. ::ConvertToXMLOutputPath(const char* path)
  1444. {
  1445. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1446. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1447. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1448. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1449. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1450. return ret;
  1451. }
  1452. std::string cmLocalVisualStudio7Generator
  1453. ::ConvertToXMLOutputPathSingle(const char* path)
  1454. {
  1455. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1456. cmSystemTools::ReplaceString(ret, "\"", "");
  1457. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1458. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1459. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1460. return ret;
  1461. }
  1462. void cmLocalVisualStudio7Generator::ConfigureFinalPass()
  1463. {
  1464. cmLocalGenerator::ConfigureFinalPass();
  1465. cmTargets &tgts = this->Makefile->GetTargets();
  1466. cmGlobalVisualStudio7Generator* gg =
  1467. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1468. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  1469. {
  1470. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  1471. {
  1472. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  1473. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  1474. std::string project_name = cmds[0][0];
  1475. gg->CreateGUID(project_name.c_str());
  1476. }
  1477. else
  1478. {
  1479. gg->CreateGUID(l->first.c_str());
  1480. }
  1481. }
  1482. }
  1483. //----------------------------------------------------------------------------
  1484. std::string cmLocalVisualStudio7Generator
  1485. ::GetTargetDirectory(cmTarget& target)
  1486. {
  1487. std::string dir;
  1488. dir += target.GetName();
  1489. dir += ".dir";
  1490. return dir;
  1491. }