cmLocalVisualStudio7Generator.cxx 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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 lang is set, the compiler will generate code automatically.
  1076. // If HEADER_FILE_ONLY is set, we must suppress this generation in
  1077. // the project file
  1078. bool excludedFromBuild =
  1079. (lang && (*sf)->GetPropertyAsBool("HEADER_FILE_ONLY"));
  1080. // if the source file does not match the linker language
  1081. // then force c or c++
  1082. if(linkLanguage && lang && strcmp(lang, linkLanguage) != 0)
  1083. {
  1084. if(strcmp(lang, "CXX") == 0)
  1085. {
  1086. // force a C++ file type
  1087. compileFlags += " /TP ";
  1088. }
  1089. else if(strcmp(lang, "C") == 0)
  1090. {
  1091. // force to c
  1092. compileFlags += " /TC ";
  1093. }
  1094. }
  1095. // Check for extra object-file dependencies.
  1096. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  1097. if(deps)
  1098. {
  1099. std::vector<std::string> depends;
  1100. cmSystemTools::ExpandListArgument(deps, depends);
  1101. if(!depends.empty())
  1102. {
  1103. std::vector<std::string>::iterator i = depends.begin();
  1104. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  1105. for(++i;i != depends.end(); ++i)
  1106. {
  1107. additionalDeps += ";";
  1108. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  1109. }
  1110. }
  1111. }
  1112. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  1113. target.GetType() == cmTarget::GLOBAL_TARGET )
  1114. {
  1115. fout << "\t\t\t<File\n";
  1116. std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
  1117. // Tell MS-Dev what the source is. If the compiler knows how to
  1118. // build it, then it will.
  1119. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  1120. if (command)
  1121. {
  1122. // Construct the entire set of commands in one string.
  1123. std::string script =
  1124. this->ConstructScript(command->GetCommandLines(),
  1125. command->GetWorkingDirectory(),
  1126. command->GetEscapeOldStyle(),
  1127. command->GetEscapeAllowMakeVars());
  1128. std::string comment = this->ConstructComment(*command);
  1129. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  1130. this->WriteCustomRule(fout, source.c_str(), script.c_str(),
  1131. comment.c_str(), command->GetDepends(),
  1132. command->GetOutputs(), flags);
  1133. }
  1134. else if(compileFlags.size() || additionalDeps.length()
  1135. || objectName.size() || excludedFromBuild)
  1136. {
  1137. const char* aCompilerTool = "VCCLCompilerTool";
  1138. std::string ext = (*sf)->GetSourceExtension();
  1139. ext = cmSystemTools::LowerCase(ext);
  1140. if(ext == "idl")
  1141. {
  1142. aCompilerTool = "VCMIDLTool";
  1143. }
  1144. if(ext == "rc")
  1145. {
  1146. aCompilerTool = "VCResourceCompilerTool";
  1147. }
  1148. if(ext == "def")
  1149. {
  1150. aCompilerTool = "VCCustomBuildTool";
  1151. }
  1152. for(std::vector<std::string>::iterator i = configs->begin();
  1153. i != configs->end(); ++i)
  1154. {
  1155. fout << "\t\t\t\t<FileConfiguration\n"
  1156. << "\t\t\t\t\tName=\"" << *i
  1157. << "|" << this->PlatformName << "\"";
  1158. if(excludedFromBuild)
  1159. {
  1160. fout << " ExcludedFromBuild=\"true\"";
  1161. }
  1162. fout << ">\n";
  1163. fout << "\t\t\t\t\t<Tool\n"
  1164. << "\t\t\t\t\tName=\"" << aCompilerTool << "\"\n";
  1165. if(compileFlags.size())
  1166. {
  1167. std::string compileFlagsCopy = compileFlags;
  1168. std::map<cmStdString, cmStdString> fileFlagMap;
  1169. this->FillFlagMapFromCommandFlags
  1170. (fileFlagMap,
  1171. &cmLocalVisualStudio7GeneratorFlagTable[0],
  1172. compileFlagsCopy);
  1173. if(compileFlagsCopy.size() &&
  1174. compileFlagsCopy.find_first_not_of(" ")
  1175. != compileFlagsCopy.npos)
  1176. {
  1177. fout << "\t\t\t\t\tAdditionalOptions=\""
  1178. << this->EscapeForXML(compileFlagsCopy.c_str()) << "\"\n";
  1179. }
  1180. for(std::map<cmStdString,
  1181. cmStdString>::iterator m = fileFlagMap.begin();
  1182. m != fileFlagMap.end(); ++m)
  1183. {
  1184. fout << "\t\t\t\t\t" << m->first << "=\""
  1185. << m->second << "\"\n";
  1186. }
  1187. }
  1188. if(additionalDeps.length())
  1189. {
  1190. fout << "\t\t\t\t\tAdditionalDependencies=\""
  1191. << additionalDeps.c_str() << "\"\n";
  1192. }
  1193. if(objectName.size())
  1194. {
  1195. fout << "\t\t\t\t\tObjectFile=\"$(IntDir)/"
  1196. << objectName.c_str() << "\"\n";
  1197. }
  1198. fout << "\t\t\t\t\t/>\n"
  1199. << "\t\t\t\t</FileConfiguration>\n";
  1200. }
  1201. }
  1202. fout << "\t\t\t</File>\n";
  1203. }
  1204. }
  1205. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  1206. for(unsigned int i=0;i<children.size();++i)
  1207. {
  1208. this->WriteGroup(&children[i], target, fout, libName, configs);
  1209. }
  1210. // If the group has a name, write the footer.
  1211. if(name != "")
  1212. {
  1213. this->WriteVCProjEndGroup(fout);
  1214. }
  1215. }
  1216. void cmLocalVisualStudio7Generator::
  1217. WriteCustomRule(std::ostream& fout,
  1218. const char* source,
  1219. const char* command,
  1220. const char* comment,
  1221. const std::vector<std::string>& depends,
  1222. const std::vector<std::string>& outputs,
  1223. const char* compileFlags)
  1224. {
  1225. // Write the rule for each configuration.
  1226. std::vector<std::string>::iterator i;
  1227. std::vector<std::string> *configs =
  1228. static_cast<cmGlobalVisualStudio7Generator *>
  1229. (this->GlobalGenerator)->GetConfigurations();
  1230. for(i = configs->begin(); i != configs->end(); ++i)
  1231. {
  1232. fout << "\t\t\t\t<FileConfiguration\n";
  1233. fout << "\t\t\t\t\tName=\"" << *i << "|" << this->PlatformName << "\">\n";
  1234. if(compileFlags)
  1235. {
  1236. fout << "\t\t\t\t\t<Tool\n"
  1237. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1238. << "\t\t\t\t\tAdditionalOptions=\""
  1239. << this->EscapeForXML(compileFlags) << "\"/>\n";
  1240. }
  1241. fout << "\t\t\t\t\t<Tool\n"
  1242. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  1243. << "\t\t\t\t\tDescription=\""
  1244. << this->EscapeForXML(comment) << "\"\n"
  1245. << "\t\t\t\t\tCommandLine=\""
  1246. << this->EscapeForXML(command) << "\"\n"
  1247. << "\t\t\t\t\tAdditionalDependencies=\"";
  1248. if(depends.empty())
  1249. {
  1250. // There are no real dependencies. Produce an artificial one to
  1251. // make sure the rule runs reliably.
  1252. if(!cmSystemTools::FileExists(source))
  1253. {
  1254. std::ofstream depout(source);
  1255. depout << "Artificial dependency for a custom command.\n";
  1256. }
  1257. fout << this->ConvertToXMLOutputPath(source);
  1258. }
  1259. else
  1260. {
  1261. // Write out the dependencies for the rule.
  1262. std::string temp;
  1263. for(std::vector<std::string>::const_iterator d = depends.begin();
  1264. d != depends.end(); ++d)
  1265. {
  1266. // Get the real name of the dependency in case it is a CMake target.
  1267. std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
  1268. fout << this->ConvertToXMLOutputPath(dep.c_str())
  1269. << ";";
  1270. }
  1271. }
  1272. fout << "\"\n";
  1273. fout << "\t\t\t\t\tOutputs=\"";
  1274. if(outputs.empty())
  1275. {
  1276. fout << source << "_force";
  1277. }
  1278. else
  1279. {
  1280. // Write a rule for the output generated by this command.
  1281. const char* sep = "";
  1282. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1283. o != outputs.end(); ++o)
  1284. {
  1285. fout << sep << this->ConvertToXMLOutputPathSingle(o->c_str());
  1286. sep = ";";
  1287. }
  1288. }
  1289. fout << "\"/>\n";
  1290. fout << "\t\t\t\t</FileConfiguration>\n";
  1291. }
  1292. }
  1293. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  1294. const char* group,
  1295. const char* )
  1296. {
  1297. fout << "\t\t<Filter\n"
  1298. << "\t\t\tName=\"" << group << "\"\n"
  1299. << "\t\t\tFilter=\"\">\n";
  1300. }
  1301. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  1302. {
  1303. fout << "\t\t</Filter>\n";
  1304. }
  1305. // look for custom rules on a target and collect them together
  1306. void cmLocalVisualStudio7Generator
  1307. ::OutputTargetRules(std::ostream& fout,
  1308. cmTarget &target,
  1309. const char * /*libName*/)
  1310. {
  1311. if (target.GetType() > cmTarget::GLOBAL_TARGET)
  1312. {
  1313. return;
  1314. }
  1315. // add the pre build rules
  1316. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  1317. bool init = false;
  1318. for (std::vector<cmCustomCommand>::const_iterator cr =
  1319. target.GetPreBuildCommands().begin();
  1320. cr != target.GetPreBuildCommands().end(); ++cr)
  1321. {
  1322. if(!init)
  1323. {
  1324. fout << "\nCommandLine=\"";
  1325. init = true;
  1326. }
  1327. std::string script =
  1328. this->ConstructScript(cr->GetCommandLines(),
  1329. cr->GetWorkingDirectory(),
  1330. cr->GetEscapeOldStyle(),
  1331. cr->GetEscapeAllowMakeVars());
  1332. fout << this->EscapeForXML(script.c_str()).c_str();
  1333. }
  1334. if (init)
  1335. {
  1336. fout << "\"";
  1337. }
  1338. fout << "/>\n";
  1339. // add the pre Link rules
  1340. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  1341. init = false;
  1342. for (std::vector<cmCustomCommand>::const_iterator cr =
  1343. target.GetPreLinkCommands().begin();
  1344. cr != target.GetPreLinkCommands().end(); ++cr)
  1345. {
  1346. if(!init)
  1347. {
  1348. fout << "\nCommandLine=\"";
  1349. init = true;
  1350. }
  1351. std::string script =
  1352. this->ConstructScript(cr->GetCommandLines(),
  1353. cr->GetWorkingDirectory(),
  1354. cr->GetEscapeOldStyle(),
  1355. cr->GetEscapeAllowMakeVars());
  1356. fout << this->EscapeForXML(script.c_str()).c_str();
  1357. }
  1358. if (init)
  1359. {
  1360. fout << "\"";
  1361. }
  1362. fout << "/>\n";
  1363. // add the PostBuild rules
  1364. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  1365. init = false;
  1366. for (std::vector<cmCustomCommand>::const_iterator cr =
  1367. target.GetPostBuildCommands().begin();
  1368. cr != target.GetPostBuildCommands().end(); ++cr)
  1369. {
  1370. if(!init)
  1371. {
  1372. fout << "\nCommandLine=\"";
  1373. init = true;
  1374. }
  1375. std::string script =
  1376. this->ConstructScript(cr->GetCommandLines(),
  1377. cr->GetWorkingDirectory(),
  1378. cr->GetEscapeOldStyle(),
  1379. cr->GetEscapeAllowMakeVars());
  1380. fout << this->EscapeForXML(script.c_str()).c_str();
  1381. }
  1382. if (init)
  1383. {
  1384. fout << "\"";
  1385. }
  1386. fout << "/>\n";
  1387. }
  1388. void
  1389. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  1390. const char *libName,
  1391. cmTarget & target,
  1392. std::vector<cmSourceGroup> &)
  1393. {
  1394. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  1395. << "<VisualStudioProject\n"
  1396. << "\tProjectType=\"Visual C++\"\n";
  1397. if(this->Version == 71)
  1398. {
  1399. fout << "\tVersion=\"7.10\"\n";
  1400. }
  1401. else
  1402. {
  1403. if (this->Version == 8)
  1404. {
  1405. fout << "\tVersion=\"8.00\"\n";
  1406. }
  1407. else
  1408. {
  1409. fout << "\tVersion=\"7.00\"\n";
  1410. }
  1411. }
  1412. const char* projLabel = target.GetProperty("PROJECT_LABEL");
  1413. if(!projLabel)
  1414. {
  1415. projLabel = libName;
  1416. }
  1417. const char* keyword = target.GetProperty("VS_KEYWORD");
  1418. if(!keyword)
  1419. {
  1420. keyword = "Win32Proj";
  1421. }
  1422. cmGlobalVisualStudio7Generator* gg =
  1423. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1424. fout << "\tName=\"" << projLabel << "\"\n";
  1425. if(this->Version == 8)
  1426. {
  1427. fout << "\tProjectGUID=\"{" << gg->GetGUID(libName) << "}\"\n";
  1428. }
  1429. fout << "\tSccProjectName=\"\"\n"
  1430. << "\tSccLocalPath=\"\"\n"
  1431. << "\tKeyword=\"" << keyword << "\">\n"
  1432. << "\t<Platforms>\n"
  1433. << "\t\t<Platform\n\t\t\tName=\"" << this->PlatformName << "\"/>\n"
  1434. << "\t</Platforms>\n";
  1435. }
  1436. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  1437. {
  1438. fout << "\t<Globals>\n"
  1439. << "\t</Globals>\n"
  1440. << "</VisualStudioProject>\n";
  1441. }
  1442. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  1443. {
  1444. std::string ret = s;
  1445. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1446. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1447. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1448. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1449. cmSystemTools::ReplaceString(ret, "\n", "&#x0D;&#x0A;");
  1450. return ret;
  1451. }
  1452. std::string cmLocalVisualStudio7Generator
  1453. ::ConvertToXMLOutputPath(const char* path)
  1454. {
  1455. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1456. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1457. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1458. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1459. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1460. return ret;
  1461. }
  1462. std::string cmLocalVisualStudio7Generator
  1463. ::ConvertToXMLOutputPathSingle(const char* path)
  1464. {
  1465. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1466. cmSystemTools::ReplaceString(ret, "\"", "");
  1467. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1468. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1469. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1470. return ret;
  1471. }
  1472. void cmLocalVisualStudio7Generator::ConfigureFinalPass()
  1473. {
  1474. cmLocalGenerator::ConfigureFinalPass();
  1475. cmTargets &tgts = this->Makefile->GetTargets();
  1476. cmGlobalVisualStudio7Generator* gg =
  1477. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1478. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  1479. {
  1480. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  1481. {
  1482. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  1483. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  1484. std::string project_name = cmds[0][0];
  1485. gg->CreateGUID(project_name.c_str());
  1486. }
  1487. else
  1488. {
  1489. gg->CreateGUID(l->first.c_str());
  1490. }
  1491. }
  1492. }
  1493. //----------------------------------------------------------------------------
  1494. std::string cmLocalVisualStudio7Generator
  1495. ::GetTargetDirectory(cmTarget& target)
  1496. {
  1497. std::string dir;
  1498. dir += target.GetName();
  1499. dir += ".dir";
  1500. return dir;
  1501. }