cmLocalVisualStudio7Generator.cxx 51 KB

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