cmLocalVisualStudio7Generator.cxx 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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. this->OutputTargetRules(fout, target, libName);
  567. this->OutputBuildTool(fout, configName, libName, target);
  568. fout << "\t\t</Configuration>\n";
  569. }
  570. void cmLocalVisualStudio7Generator::FillFlagMapFromCommandFlags(
  571. std::map<cmStdString, cmStdString>& flagMap,
  572. cmVS7FlagTable* flagTable,
  573. std::string& flags)
  574. {
  575. std::string replace;
  576. std::string option;
  577. while(flagTable->IDEName)
  578. {
  579. option.reserve(strlen(flagTable->commandFlag)+2);
  580. // first do the - version
  581. option = "-";
  582. option += flagTable->commandFlag;
  583. while(flags.find(option) != flags.npos)
  584. {
  585. // replace the flag
  586. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  587. // now put value into flag map
  588. flagMap[flagTable->IDEName] = flagTable->value;
  589. }
  590. // now do the / version
  591. option[0] = '/';
  592. while(flags.find(option) != flags.npos)
  593. {
  594. // replace the flag
  595. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  596. // now put value into flag map
  597. flagMap[flagTable->IDEName] = flagTable->value;
  598. }
  599. // move to next flag
  600. flagTable++;
  601. }
  602. // If verbose makefiles have been requested and the /nologo option
  603. // was not given explicitly in the flags we want to add an attribute
  604. // to the generated project to disable logo suppression. Otherwise
  605. // the GUI default is to enable suppression.
  606. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  607. {
  608. if(flagMap.find("SuppressStartupBanner") == flagMap.end())
  609. {
  610. flagMap["SuppressStartupBanner"] = "FALSE";
  611. }
  612. }
  613. }
  614. //----------------------------------------------------------------------------
  615. std::string
  616. cmLocalVisualStudio7Generator
  617. ::GetBuildTypeLinkerFlags(std::string rootLinkerFlags, const char* configName)
  618. {
  619. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  620. std::string extraLinkOptionsBuildTypeDef =
  621. rootLinkerFlags + "_" + configTypeUpper;
  622. std::string extraLinkOptionsBuildType =
  623. this->Makefile->GetRequiredDefinition
  624. (extraLinkOptionsBuildTypeDef.c_str());
  625. return extraLinkOptionsBuildType;
  626. }
  627. void cmLocalVisualStudio7Generator::OutputBuildTool(std::ostream& fout,
  628. const char* configName,
  629. const char *libName,
  630. cmTarget &target)
  631. {
  632. std::string targetFullName = target.GetFullName(configName);
  633. std::string temp;
  634. std::string extraLinkOptions;
  635. if(target.GetType() == cmTarget::EXECUTABLE)
  636. {
  637. extraLinkOptions =
  638. this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS")
  639. + std::string(" ")
  640. + GetBuildTypeLinkerFlags("CMAKE_EXE_LINKER_FLAGS", configName);
  641. }
  642. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  643. {
  644. extraLinkOptions =
  645. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS")
  646. + std::string(" ")
  647. + GetBuildTypeLinkerFlags("CMAKE_SHARED_LINKER_FLAGS", configName);
  648. }
  649. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  650. {
  651. extraLinkOptions =
  652. this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS")
  653. + std::string(" ")
  654. + GetBuildTypeLinkerFlags("CMAKE_MODULE_LINKER_FLAGS", configName);
  655. }
  656. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  657. if(targetLinkFlags)
  658. {
  659. extraLinkOptions += " ";
  660. extraLinkOptions += targetLinkFlags;
  661. }
  662. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  663. std::string linkFlagsConfig = "LINK_FLAGS_";
  664. linkFlagsConfig += configTypeUpper;
  665. targetLinkFlags = target.GetProperty(linkFlagsConfig.c_str());
  666. if(targetLinkFlags)
  667. {
  668. extraLinkOptions += " ";
  669. extraLinkOptions += targetLinkFlags;
  670. }
  671. std::map<cmStdString, cmStdString> flagMap;
  672. this->FillFlagMapFromCommandFlags
  673. (flagMap, &cmLocalVisualStudio7GeneratorLinkFlagTable[0],
  674. extraLinkOptions);
  675. switch(target.GetType())
  676. {
  677. case cmTarget::STATIC_LIBRARY:
  678. {
  679. std::string libpath = this->LibraryOutputPath +
  680. "$(OutDir)/" + targetFullName;
  681. fout << "\t\t\t<Tool\n"
  682. << "\t\t\t\tName=\"VCLibrarianTool\"\n";
  683. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  684. {
  685. fout << "\t\t\t\tAdditionalOptions=\"" << libflags << "\"\n";
  686. }
  687. fout << "\t\t\t\tOutputFile=\""
  688. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << "\"/>\n";
  689. break;
  690. }
  691. case cmTarget::SHARED_LIBRARY:
  692. case cmTarget::MODULE_LIBRARY:
  693. {
  694. // Compute the link library and directory information.
  695. std::vector<cmStdString> linkLibs;
  696. std::vector<cmStdString> linkDirs;
  697. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  698. // Get the language to use for linking.
  699. const char* linkLanguage =
  700. target.GetLinkerLanguage(this->GetGlobalGenerator());
  701. if(!linkLanguage)
  702. {
  703. cmSystemTools::Error
  704. ("CMake can not determine linker language for target:",
  705. target.GetName());
  706. return;
  707. }
  708. // Compute the variable name to lookup standard libraries for this
  709. // language.
  710. std::string standardLibsVar = "CMAKE_";
  711. standardLibsVar += linkLanguage;
  712. standardLibsVar += "_STANDARD_LIBRARIES";
  713. fout << "\t\t\t<Tool\n"
  714. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  715. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  716. if(extraLinkOptions.size())
  717. {
  718. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  719. extraLinkOptions.c_str()).c_str();
  720. }
  721. // Use the NOINHERIT macro to avoid getting VS project default
  722. // libraries which may be set by the user to something bad.
  723. fout << "\"\n"
  724. << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  725. << this->Makefile->GetSafeDefinition(standardLibsVar.c_str())
  726. << " ";
  727. this->OutputLibraries(fout, linkLibs);
  728. fout << "\"\n";
  729. temp = this->LibraryOutputPath;
  730. temp += configName;
  731. temp += "/";
  732. temp += targetFullName;
  733. fout << "\t\t\t\tOutputFile=\""
  734. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  735. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  736. i != flagMap.end(); ++i)
  737. {
  738. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  739. }
  740. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  741. this->OutputLibraryDirectories(fout, linkDirs);
  742. fout << "\"\n";
  743. this->OutputModuleDefinitionFile(fout, target);
  744. temp = this->LibraryOutputPath;
  745. temp += "$(OutDir)/";
  746. temp += libName;
  747. temp += ".pdb";
  748. fout << "\t\t\t\tProgramDatabaseFile=\"" <<
  749. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  750. if(strcmp(configName, "Debug") == 0
  751. || strcmp(configName, "RelWithDebInfo") == 0)
  752. {
  753. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  754. }
  755. std::string stackVar = "CMAKE_";
  756. stackVar += linkLanguage;
  757. stackVar += "_STACK_SIZE";
  758. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  759. if(stackVal)
  760. {
  761. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"\n";
  762. }
  763. temp = this->LibraryOutputPath;
  764. temp += configName;
  765. temp += "/";
  766. temp +=
  767. cmSystemTools::GetFilenameWithoutLastExtension(targetFullName.c_str());
  768. temp += ".lib";
  769. fout << "\t\t\t\tImportLibrary=\""
  770. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  771. }
  772. break;
  773. case cmTarget::EXECUTABLE:
  774. {
  775. // Compute the link library and directory information.
  776. std::vector<cmStdString> linkLibs;
  777. std::vector<cmStdString> linkDirs;
  778. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  779. // Get the language to use for linking.
  780. const char* linkLanguage =
  781. target.GetLinkerLanguage(this->GetGlobalGenerator());
  782. if(!linkLanguage)
  783. {
  784. cmSystemTools::Error
  785. ("CMake can not determine linker language for target:",
  786. target.GetName());
  787. return;
  788. }
  789. // Compute the variable name to lookup standard libraries for this
  790. // language.
  791. std::string standardLibsVar = "CMAKE_";
  792. standardLibsVar += linkLanguage;
  793. standardLibsVar += "_STANDARD_LIBRARIES";
  794. fout << "\t\t\t<Tool\n"
  795. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  796. << "\t\t\t\tAdditionalOptions=\"";
  797. if(extraLinkOptions.size())
  798. {
  799. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  800. extraLinkOptions.c_str()).c_str();
  801. }
  802. // Use the NOINHERIT macro to avoid getting VS project default
  803. // libraries which may be set by the user to something bad.
  804. fout << "\"\n"
  805. << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  806. << this->Makefile->GetSafeDefinition(standardLibsVar.c_str())
  807. << " ";
  808. this->OutputLibraries(fout, linkLibs);
  809. fout << "\"\n";
  810. temp = this->ExecutableOutputPath;
  811. temp += configName;
  812. temp += "/";
  813. temp += targetFullName;
  814. fout << "\t\t\t\tOutputFile=\""
  815. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  816. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  817. i != flagMap.end(); ++i)
  818. {
  819. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  820. }
  821. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  822. this->OutputLibraryDirectories(fout, linkDirs);
  823. fout << "\"\n";
  824. fout << "\t\t\t\tProgramDatabaseFile=\"" << this->LibraryOutputPath
  825. << "$(OutDir)\\" << libName << ".pdb\"\n";
  826. if(strcmp(configName, "Debug") == 0
  827. || strcmp(configName, "RelWithDebInfo") == 0)
  828. {
  829. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  830. }
  831. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  832. {
  833. fout << "\t\t\t\tSubSystem=\"2\"\n";
  834. }
  835. else
  836. {
  837. fout << "\t\t\t\tSubSystem=\"1\"\n";
  838. }
  839. std::string stackVar = "CMAKE_";
  840. stackVar += linkLanguage;
  841. stackVar += "_STACK_SIZE";
  842. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  843. if(stackVal)
  844. {
  845. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"";
  846. }
  847. fout << "/>\n";
  848. break;
  849. }
  850. case cmTarget::UTILITY:
  851. case cmTarget::GLOBAL_TARGET:
  852. break;
  853. }
  854. }
  855. void cmLocalVisualStudio7Generator
  856. ::OutputModuleDefinitionFile(std::ostream& fout,
  857. cmTarget &target)
  858. {
  859. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  860. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  861. i != classes.end(); i++)
  862. {
  863. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  864. {
  865. fout << "\t\t\t\tModuleDefinitionFile=\""
  866. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  867. << "\"\n";
  868. return;
  869. }
  870. }
  871. }
  872. //----------------------------------------------------------------------------
  873. void
  874. cmLocalVisualStudio7Generator
  875. ::OutputLibraries(std::ostream& fout,
  876. std::vector<cmStdString> const& libs)
  877. {
  878. for(std::vector<cmStdString>::const_iterator l = libs.begin();
  879. l != libs.end(); ++l)
  880. {
  881. fout << this->ConvertToXMLOutputPath(l->c_str()) << " ";
  882. }
  883. }
  884. //----------------------------------------------------------------------------
  885. void
  886. cmLocalVisualStudio7Generator
  887. ::OutputLibraryDirectories(std::ostream& fout,
  888. std::vector<cmStdString> const& dirs)
  889. {
  890. const char* comma = "";
  891. for(std::vector<cmStdString>::const_iterator d = dirs.begin();
  892. d != dirs.end(); ++d)
  893. {
  894. // Remove any trailing slash and skip empty paths.
  895. std::string dir = *d;
  896. if(dir[dir.size()-1] == '/')
  897. {
  898. dir = dir.substr(0, dir.size()-1);
  899. }
  900. if(dir.empty())
  901. {
  902. continue;
  903. }
  904. // Switch to a relative path specification if it is shorter.
  905. if(cmSystemTools::FileIsFullPath(dir.c_str()))
  906. {
  907. std::string rel = this->Convert(dir.c_str(), START_OUTPUT, UNCHANGED);
  908. if(rel.size() < dir.size())
  909. {
  910. dir = rel;
  911. }
  912. }
  913. // First search a configuration-specific subdirectory and then the
  914. // original directory.
  915. fout << comma << this->ConvertToXMLOutputPath((dir+"/$(OutDir)").c_str())
  916. << "," << this->ConvertToXMLOutputPath(dir.c_str());
  917. comma = ",";
  918. }
  919. }
  920. //----------------------------------------------------------------------------
  921. void cmLocalVisualStudio7Generator::OutputDefineFlags(const char* flags,
  922. std::ostream& fout)
  923. {
  924. std::string defs = flags;
  925. cmSystemTools::ReplaceString(defs, "/D","-D");
  926. std::string::size_type pos = defs.find("-D");
  927. bool done = pos == std::string::npos;
  928. if(!done)
  929. {
  930. fout << ",";
  931. }
  932. while(!done)
  933. {
  934. std::string::size_type nextpos = defs.find("-D", pos+2);
  935. std::string define;
  936. if(nextpos != std::string::npos)
  937. {
  938. define = defs.substr(pos+2, nextpos - pos -3);
  939. }
  940. else
  941. {
  942. define = defs.substr(pos+2);
  943. done = true;
  944. }
  945. // Remove trailing whitespace from the definition.
  946. while(!define.empty() && isspace(define[define.size()-1]))
  947. {
  948. define = define.substr(0, define.size()-1);
  949. }
  950. // Double-quotes in the value of the definition must be escaped
  951. // with a backslash. The entire definition should be quoted in
  952. // the generated xml attribute to avoid confusing the VS parser.
  953. cmSystemTools::ReplaceString(define, "\"", "\\&quot;");
  954. fout << "&quot;" << define << "&quot;,";
  955. if(!done)
  956. {
  957. pos = defs.find("-D", nextpos);
  958. }
  959. }
  960. }
  961. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  962. const char *libName,
  963. cmTarget &target)
  964. {
  965. // get the configurations
  966. std::vector<std::string> *configs =
  967. static_cast<cmGlobalVisualStudio7Generator *>
  968. (this->GlobalGenerator)->GetConfigurations();
  969. // trace the visual studio dependencies
  970. std::string name = libName;
  971. name += ".vcproj.cmake";
  972. // We may be modifying the source groups temporarily, so make a copy.
  973. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  974. // get the classes from the source lists then add them to the groups
  975. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  976. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  977. i != classes.end(); i++)
  978. {
  979. // Add the file to the list of sources.
  980. std::string source = (*i)->GetFullPath();
  981. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  982. {
  983. this->ModuleDefinitionFile = (*i)->GetFullPath();
  984. }
  985. cmSourceGroup& sourceGroup =
  986. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  987. sourceGroup.AssignSource(*i);
  988. }
  989. // Compute which sources need unique object computation.
  990. this->ComputeObjectNameRequirements(sourceGroups);
  991. // open the project
  992. this->WriteProjectStart(fout, libName, target, sourceGroups);
  993. // write the configuration information
  994. this->WriteConfigurations(fout, libName, target);
  995. fout << "\t<Files>\n";
  996. // Loop through every source group.
  997. for(unsigned int i = 0; i < sourceGroups.size(); ++i)
  998. {
  999. cmSourceGroup sg = sourceGroups[i];
  1000. this->WriteGroup(&sg, target, fout, libName, configs);
  1001. }
  1002. //}
  1003. fout << "\t</Files>\n";
  1004. // Write the VCProj file's footer.
  1005. this->WriteVCProjFooter(fout);
  1006. }
  1007. void cmLocalVisualStudio7Generator
  1008. ::WriteGroup(const cmSourceGroup *sg, cmTarget target,
  1009. std::ostream &fout, const char *libName,
  1010. std::vector<std::string> *configs)
  1011. {
  1012. const std::vector<const cmSourceFile *> &sourceFiles =
  1013. sg->GetSourceFiles();
  1014. // If the group is empty, don't write it at all.
  1015. if(sourceFiles.empty() && sg->GetGroupChildren().empty())
  1016. {
  1017. return;
  1018. }
  1019. // If the group has a name, write the header.
  1020. std::string name = sg->GetName();
  1021. if(name != "")
  1022. {
  1023. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  1024. }
  1025. // Loop through each source in the source group.
  1026. std::string objectName;
  1027. for(std::vector<const cmSourceFile *>::const_iterator sf =
  1028. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  1029. {
  1030. std::string source = (*sf)->GetFullPath();
  1031. const cmCustomCommand *command = (*sf)->GetCustomCommand();
  1032. std::string compileFlags;
  1033. std::string additionalDeps;
  1034. if(this->NeedObjectName.find(*sf) != this->NeedObjectName.end())
  1035. {
  1036. objectName = this->GetObjectFileNameWithoutTarget(*(*sf));
  1037. }
  1038. else
  1039. {
  1040. objectName = "";
  1041. }
  1042. // Add per-source flags.
  1043. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  1044. if(cflags)
  1045. {
  1046. compileFlags += " ";
  1047. compileFlags += cflags;
  1048. }
  1049. const char* lang = this->GlobalGenerator->GetLanguageFromExtension
  1050. ((*sf)->GetSourceExtension().c_str());
  1051. const char* linkLanguage = target.GetLinkerLanguage
  1052. (this->GetGlobalGenerator());
  1053. // if the source file does not match the linker language
  1054. // then force c or c++
  1055. if(linkLanguage && lang && strcmp(lang, linkLanguage) != 0)
  1056. {
  1057. if(strcmp(lang, "CXX") == 0)
  1058. {
  1059. // force a C++ file type
  1060. compileFlags += " /TP ";
  1061. }
  1062. else if(strcmp(lang, "C") == 0)
  1063. {
  1064. // force to c
  1065. compileFlags += " /TC ";
  1066. }
  1067. }
  1068. // Check for extra object-file dependencies.
  1069. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  1070. if(deps)
  1071. {
  1072. std::vector<std::string> depends;
  1073. cmSystemTools::ExpandListArgument(deps, depends);
  1074. if(!depends.empty())
  1075. {
  1076. std::vector<std::string>::iterator i = depends.begin();
  1077. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  1078. for(++i;i != depends.end(); ++i)
  1079. {
  1080. additionalDeps += ";";
  1081. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  1082. }
  1083. }
  1084. }
  1085. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  1086. target.GetType() == cmTarget::GLOBAL_TARGET )
  1087. {
  1088. fout << "\t\t\t<File\n";
  1089. std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
  1090. // Tell MS-Dev what the source is. If the compiler knows how to
  1091. // build it, then it will.
  1092. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  1093. if (command)
  1094. {
  1095. // Construct the entire set of commands in one string.
  1096. std::string script =
  1097. this->ConstructScript(command->GetCommandLines(),
  1098. command->GetWorkingDirectory(),
  1099. command->GetEscapeOldStyle(),
  1100. command->GetEscapeAllowMakeVars());
  1101. std::string comment = this->ConstructComment(*command);
  1102. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  1103. this->WriteCustomRule(fout, source.c_str(), script.c_str(),
  1104. comment.c_str(), command->GetDepends(),
  1105. command->GetOutputs(), flags);
  1106. }
  1107. else if(compileFlags.size() || additionalDeps.length()
  1108. || objectName.size())
  1109. {
  1110. const char* aCompilerTool = "VCCLCompilerTool";
  1111. std::string ext = (*sf)->GetSourceExtension();
  1112. ext = cmSystemTools::LowerCase(ext);
  1113. if(ext == "idl")
  1114. {
  1115. aCompilerTool = "VCMIDLTool";
  1116. }
  1117. if(ext == "rc")
  1118. {
  1119. aCompilerTool = "VCResourceCompilerTool";
  1120. }
  1121. if(ext == "def")
  1122. {
  1123. aCompilerTool = "VCCustomBuildTool";
  1124. }
  1125. for(std::vector<std::string>::iterator i = configs->begin();
  1126. i != configs->end(); ++i)
  1127. {
  1128. fout << "\t\t\t\t<FileConfiguration\n"
  1129. << "\t\t\t\t\tName=\"" << *i
  1130. << "|" << this->PlatformName << "\">\n"
  1131. << "\t\t\t\t\t<Tool\n"
  1132. << "\t\t\t\t\tName=\"" << aCompilerTool << "\"\n";
  1133. if(compileFlags.size())
  1134. {
  1135. std::string compileFlagsCopy = compileFlags;
  1136. std::map<cmStdString, cmStdString> fileFlagMap;
  1137. this->FillFlagMapFromCommandFlags
  1138. (fileFlagMap,
  1139. &cmLocalVisualStudio7GeneratorFlagTable[0],
  1140. compileFlagsCopy);
  1141. if(compileFlagsCopy.size() &&
  1142. compileFlagsCopy.find_first_not_of(" ")
  1143. != compileFlagsCopy.npos)
  1144. {
  1145. fout << "\t\t\t\t\tAdditionalOptions=\""
  1146. << this->EscapeForXML(compileFlagsCopy.c_str()) << "\"\n";
  1147. }
  1148. for(std::map<cmStdString,
  1149. cmStdString>::iterator m = fileFlagMap.begin();
  1150. m != fileFlagMap.end(); ++m)
  1151. {
  1152. fout << "\t\t\t\t\t" << m->first << "=\""
  1153. << m->second << "\"\n";
  1154. }
  1155. }
  1156. if(additionalDeps.length())
  1157. {
  1158. fout << "\t\t\t\t\tAdditionalDependencies=\""
  1159. << additionalDeps.c_str() << "\"\n";
  1160. }
  1161. if(objectName.size())
  1162. {
  1163. fout << "\t\t\t\t\tObjectFile=\"$(IntDir)/"
  1164. << objectName.c_str() << "\"\n";
  1165. }
  1166. fout << "\t\t\t\t\t/>\n"
  1167. << "\t\t\t\t</FileConfiguration>\n";
  1168. }
  1169. }
  1170. fout << "\t\t\t</File>\n";
  1171. }
  1172. }
  1173. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  1174. for(unsigned int i=0;i<children.size();++i)
  1175. {
  1176. this->WriteGroup(&children[i], target, fout, libName, configs);
  1177. }
  1178. // If the group has a name, write the footer.
  1179. if(name != "")
  1180. {
  1181. this->WriteVCProjEndGroup(fout);
  1182. }
  1183. }
  1184. void cmLocalVisualStudio7Generator::
  1185. WriteCustomRule(std::ostream& fout,
  1186. const char* source,
  1187. const char* command,
  1188. const char* comment,
  1189. const std::vector<std::string>& depends,
  1190. const std::vector<std::string>& outputs,
  1191. const char* compileFlags)
  1192. {
  1193. // Write the rule for each configuration.
  1194. std::vector<std::string>::iterator i;
  1195. std::vector<std::string> *configs =
  1196. static_cast<cmGlobalVisualStudio7Generator *>
  1197. (this->GlobalGenerator)->GetConfigurations();
  1198. for(i = configs->begin(); i != configs->end(); ++i)
  1199. {
  1200. fout << "\t\t\t\t<FileConfiguration\n";
  1201. fout << "\t\t\t\t\tName=\"" << *i << "|" << this->PlatformName << "\">\n";
  1202. if(compileFlags)
  1203. {
  1204. fout << "\t\t\t\t\t<Tool\n"
  1205. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1206. << "\t\t\t\t\tAdditionalOptions=\""
  1207. << this->EscapeForXML(compileFlags) << "\"/>\n";
  1208. }
  1209. fout << "\t\t\t\t\t<Tool\n"
  1210. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  1211. << "\t\t\t\t\tDescription=\""
  1212. << this->EscapeForXML(comment) << "\"\n"
  1213. << "\t\t\t\t\tCommandLine=\""
  1214. << this->EscapeForXML(command) << "\"\n"
  1215. << "\t\t\t\t\tAdditionalDependencies=\"";
  1216. if(depends.empty())
  1217. {
  1218. // There are no real dependencies. Produce an artificial one to
  1219. // make sure the rule runs reliably.
  1220. if(!cmSystemTools::FileExists(source))
  1221. {
  1222. std::ofstream depout(source);
  1223. depout << "Artificial dependency for a custom command.\n";
  1224. }
  1225. fout << this->ConvertToXMLOutputPath(source);
  1226. }
  1227. else
  1228. {
  1229. // Write out the dependencies for the rule.
  1230. std::string temp;
  1231. for(std::vector<std::string>::const_iterator d = depends.begin();
  1232. d != depends.end(); ++d)
  1233. {
  1234. // Get the real name of the dependency in case it is a CMake target.
  1235. std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
  1236. fout << this->ConvertToXMLOutputPath(dep.c_str())
  1237. << ";";
  1238. }
  1239. }
  1240. fout << "\"\n";
  1241. fout << "\t\t\t\t\tOutputs=\"";
  1242. if(outputs.empty())
  1243. {
  1244. fout << source << "_force";
  1245. }
  1246. else
  1247. {
  1248. // Write a rule for the output generated by this command.
  1249. const char* sep = "";
  1250. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1251. o != outputs.end(); ++o)
  1252. {
  1253. fout << sep << this->ConvertToXMLOutputPathSingle(o->c_str());
  1254. sep = ";";
  1255. }
  1256. }
  1257. fout << "\"/>\n";
  1258. fout << "\t\t\t\t</FileConfiguration>\n";
  1259. }
  1260. }
  1261. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  1262. const char* group,
  1263. const char* )
  1264. {
  1265. fout << "\t\t<Filter\n"
  1266. << "\t\t\tName=\"" << group << "\"\n"
  1267. << "\t\t\tFilter=\"\">\n";
  1268. }
  1269. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  1270. {
  1271. fout << "\t\t</Filter>\n";
  1272. }
  1273. // look for custom rules on a target and collect them together
  1274. void cmLocalVisualStudio7Generator
  1275. ::OutputTargetRules(std::ostream& fout,
  1276. cmTarget &target,
  1277. const char * /*libName*/)
  1278. {
  1279. if (target.GetType() > cmTarget::GLOBAL_TARGET)
  1280. {
  1281. return;
  1282. }
  1283. // add the pre build rules
  1284. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  1285. bool init = false;
  1286. for (std::vector<cmCustomCommand>::const_iterator cr =
  1287. target.GetPreBuildCommands().begin();
  1288. cr != target.GetPreBuildCommands().end(); ++cr)
  1289. {
  1290. if(!init)
  1291. {
  1292. fout << "\nCommandLine=\"";
  1293. init = true;
  1294. }
  1295. std::string script =
  1296. this->ConstructScript(cr->GetCommandLines(),
  1297. cr->GetWorkingDirectory(),
  1298. cr->GetEscapeOldStyle(),
  1299. cr->GetEscapeAllowMakeVars());
  1300. fout << this->EscapeForXML(script.c_str()).c_str();
  1301. }
  1302. if (init)
  1303. {
  1304. fout << "\"";
  1305. }
  1306. fout << "/>\n";
  1307. // add the pre Link rules
  1308. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  1309. init = false;
  1310. for (std::vector<cmCustomCommand>::const_iterator cr =
  1311. target.GetPreLinkCommands().begin();
  1312. cr != target.GetPreLinkCommands().end(); ++cr)
  1313. {
  1314. if(!init)
  1315. {
  1316. fout << "\nCommandLine=\"";
  1317. init = true;
  1318. }
  1319. std::string script =
  1320. this->ConstructScript(cr->GetCommandLines(),
  1321. cr->GetWorkingDirectory(),
  1322. cr->GetEscapeOldStyle(),
  1323. cr->GetEscapeAllowMakeVars());
  1324. fout << this->EscapeForXML(script.c_str()).c_str();
  1325. }
  1326. if (init)
  1327. {
  1328. fout << "\"";
  1329. }
  1330. fout << "/>\n";
  1331. // add the PostBuild rules
  1332. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  1333. init = false;
  1334. for (std::vector<cmCustomCommand>::const_iterator cr =
  1335. target.GetPostBuildCommands().begin();
  1336. cr != target.GetPostBuildCommands().end(); ++cr)
  1337. {
  1338. if(!init)
  1339. {
  1340. fout << "\nCommandLine=\"";
  1341. init = true;
  1342. }
  1343. std::string script =
  1344. this->ConstructScript(cr->GetCommandLines(),
  1345. cr->GetWorkingDirectory(),
  1346. cr->GetEscapeOldStyle(),
  1347. cr->GetEscapeAllowMakeVars());
  1348. fout << this->EscapeForXML(script.c_str()).c_str();
  1349. }
  1350. if (init)
  1351. {
  1352. fout << "\"";
  1353. }
  1354. fout << "/>\n";
  1355. }
  1356. void
  1357. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  1358. const char *libName,
  1359. cmTarget & target,
  1360. std::vector<cmSourceGroup> &)
  1361. {
  1362. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  1363. << "<VisualStudioProject\n"
  1364. << "\tProjectType=\"Visual C++\"\n";
  1365. if(this->Version == 71)
  1366. {
  1367. fout << "\tVersion=\"7.10\"\n";
  1368. }
  1369. else
  1370. {
  1371. if (this->Version == 8)
  1372. {
  1373. fout << "\tVersion=\"8.00\"\n";
  1374. }
  1375. else
  1376. {
  1377. fout << "\tVersion=\"7.00\"\n";
  1378. }
  1379. }
  1380. const char* projLabel = target.GetProperty("PROJECT_LABEL");
  1381. if(!projLabel)
  1382. {
  1383. projLabel = libName;
  1384. }
  1385. const char* keyword = target.GetProperty("VS_KEYWORD");
  1386. if(!keyword)
  1387. {
  1388. keyword = "Win32Proj";
  1389. }
  1390. cmGlobalVisualStudio7Generator* gg =
  1391. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1392. fout << "\tName=\"" << projLabel << "\"\n";
  1393. if(this->Version == 8)
  1394. {
  1395. fout << "\tProjectGUID=\"{" << gg->GetGUID(libName) << "}\"\n";
  1396. }
  1397. fout << "\tSccProjectName=\"\"\n"
  1398. << "\tSccLocalPath=\"\"\n"
  1399. << "\tKeyword=\"" << keyword << "\">\n"
  1400. << "\t<Platforms>\n"
  1401. << "\t\t<Platform\n\t\t\tName=\"" << this->PlatformName << "\"/>\n"
  1402. << "\t</Platforms>\n";
  1403. }
  1404. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  1405. {
  1406. fout << "\t<Globals>\n"
  1407. << "\t</Globals>\n"
  1408. << "</VisualStudioProject>\n";
  1409. }
  1410. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  1411. {
  1412. std::string ret = s;
  1413. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1414. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1415. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1416. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1417. cmSystemTools::ReplaceString(ret, "\n", "&#x0D;&#x0A;");
  1418. return ret;
  1419. }
  1420. std::string cmLocalVisualStudio7Generator
  1421. ::ConvertToXMLOutputPath(const char* path)
  1422. {
  1423. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1424. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1425. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1426. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1427. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1428. return ret;
  1429. }
  1430. std::string cmLocalVisualStudio7Generator
  1431. ::ConvertToXMLOutputPathSingle(const char* path)
  1432. {
  1433. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1434. cmSystemTools::ReplaceString(ret, "\"", "");
  1435. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1436. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1437. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1438. return ret;
  1439. }
  1440. void cmLocalVisualStudio7Generator::ConfigureFinalPass()
  1441. {
  1442. cmLocalGenerator::ConfigureFinalPass();
  1443. cmTargets &tgts = this->Makefile->GetTargets();
  1444. cmGlobalVisualStudio7Generator* gg =
  1445. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1446. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  1447. {
  1448. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  1449. {
  1450. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  1451. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  1452. std::string project_name = cmds[0][0];
  1453. gg->CreateGUID(project_name.c_str());
  1454. }
  1455. else
  1456. {
  1457. gg->CreateGUID(l->first.c_str());
  1458. }
  1459. }
  1460. }
  1461. //----------------------------------------------------------------------------
  1462. std::string cmLocalVisualStudio7Generator
  1463. ::GetTargetDirectory(cmTarget& target)
  1464. {
  1465. std::string dir;
  1466. dir += target.GetName();
  1467. dir += ".dir";
  1468. return dir;
  1469. }