cmLocalVisualStudio6Generator.cxx 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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 "cmGlobalGenerator.h"
  14. #include "cmLocalVisualStudio6Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmCacheManager.h"
  19. #include "cmake.h"
  20. #include <cmsys/RegularExpression.hxx>
  21. cmLocalVisualStudio6Generator::cmLocalVisualStudio6Generator()
  22. {
  23. }
  24. cmLocalVisualStudio6Generator::~cmLocalVisualStudio6Generator()
  25. {
  26. }
  27. void cmLocalVisualStudio6Generator::AddHelperCommands()
  28. {
  29. std::set<cmStdString> lang;
  30. lang.insert("C");
  31. lang.insert("CXX");
  32. this->CreateCustomTargetsAndCommands(lang);
  33. }
  34. void cmLocalVisualStudio6Generator::Generate()
  35. {
  36. this->OutputDSPFile();
  37. }
  38. void cmLocalVisualStudio6Generator::OutputDSPFile()
  39. {
  40. // If not an in source build, then create the output directory
  41. if(strcmp(this->Makefile->GetStartOutputDirectory(),
  42. this->Makefile->GetHomeDirectory()) != 0)
  43. {
  44. if(!cmSystemTools::MakeDirectory
  45. (this->Makefile->GetStartOutputDirectory()))
  46. {
  47. cmSystemTools::Error("Error creating directory ",
  48. this->Makefile->GetStartOutputDirectory());
  49. }
  50. }
  51. // Setup /I and /LIBPATH options for the resulting DSP file. VS 6
  52. // truncates long include paths so make it as short as possible if
  53. // the length threatents this problem.
  54. unsigned int maxIncludeLength = 3000;
  55. bool useShortPath = false;
  56. for(int j=0; j < 2; ++j)
  57. {
  58. std::vector<std::string> includes;
  59. this->GetIncludeDirectories(includes);
  60. std::vector<std::string>::iterator i;
  61. for(i = includes.begin(); i != includes.end(); ++i)
  62. {
  63. std::string tmp =
  64. this->ConvertToOptionallyRelativeOutputPath(i->c_str());
  65. if(useShortPath)
  66. {
  67. cmSystemTools::GetShortPath(tmp.c_str(), tmp);
  68. }
  69. this->IncludeOptions += " /I ";
  70. // quote if not already quoted
  71. if (tmp[0] != '"')
  72. {
  73. this->IncludeOptions += "\"";
  74. this->IncludeOptions += tmp;
  75. this->IncludeOptions += "\"";
  76. }
  77. else
  78. {
  79. this->IncludeOptions += tmp;
  80. }
  81. }
  82. if(j == 0 && this->IncludeOptions.size() > maxIncludeLength)
  83. {
  84. this->IncludeOptions = "";
  85. useShortPath = true;
  86. }
  87. else
  88. {
  89. break;
  90. }
  91. }
  92. // Create the DSP or set of DSP's for libraries and executables
  93. // clear project names
  94. this->CreatedProjectNames.clear();
  95. cmTargets &tgts = this->Makefile->GetTargets();
  96. for(cmTargets::iterator l = tgts.begin();
  97. l != tgts.end(); l++)
  98. {
  99. // Add a rule to regenerate the build system when the target
  100. // specification source changes.
  101. const char* suppRegenRule =
  102. this->Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  103. if (!cmSystemTools::IsOn(suppRegenRule))
  104. {
  105. this->AddDSPBuildRule(l->second);
  106. }
  107. }
  108. // build any targets
  109. for(cmTargets::iterator l = tgts.begin();
  110. l != tgts.end(); l++)
  111. {
  112. switch(l->second.GetType())
  113. {
  114. case cmTarget::STATIC_LIBRARY:
  115. this->SetBuildType(STATIC_LIBRARY, l->first.c_str(), l->second);
  116. break;
  117. case cmTarget::SHARED_LIBRARY:
  118. case cmTarget::MODULE_LIBRARY:
  119. this->SetBuildType(DLL, l->first.c_str(), l->second);
  120. break;
  121. case cmTarget::EXECUTABLE:
  122. this->SetBuildType(EXECUTABLE,l->first.c_str(), l->second);
  123. break;
  124. case cmTarget::UTILITY:
  125. case cmTarget::GLOBAL_TARGET:
  126. this->SetBuildType(UTILITY, l->first.c_str(), l->second);
  127. break;
  128. default:
  129. cmSystemTools::Error("Bad target type", l->first.c_str());
  130. break;
  131. }
  132. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  133. // so don't build a projectfile for it
  134. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0)
  135. {
  136. // check to see if the dsp is going into a sub-directory
  137. std::string::size_type pos = l->first.rfind('/');
  138. if(pos != std::string::npos)
  139. {
  140. std::string dir = this->Makefile->GetStartOutputDirectory();
  141. dir += "/";
  142. dir += l->first.substr(0, pos);
  143. if(!cmSystemTools::MakeDirectory(dir.c_str()))
  144. {
  145. cmSystemTools::Error("Error creating directory ", dir.c_str());
  146. }
  147. }
  148. this->CreateSingleDSP(l->first.c_str(),l->second);
  149. }
  150. }
  151. }
  152. void cmLocalVisualStudio6Generator::CreateSingleDSP(const char *lname,
  153. cmTarget &target)
  154. {
  155. // add to the list of projects
  156. std::string pname = lname;
  157. this->CreatedProjectNames.push_back(pname);
  158. // create the dsp.cmake file
  159. std::string fname;
  160. fname = this->Makefile->GetStartOutputDirectory();
  161. fname += "/";
  162. fname += lname;
  163. fname += ".dsp";
  164. // save the name of the real dsp file
  165. std::string realDSP = fname;
  166. fname += ".cmake";
  167. std::ofstream fout(fname.c_str());
  168. if(!fout)
  169. {
  170. cmSystemTools::Error("Error Writing ", fname.c_str());
  171. cmSystemTools::ReportLastSystemError("");
  172. }
  173. this->WriteDSPFile(fout,lname,target);
  174. fout.close();
  175. // if the dsp file has changed, then write it.
  176. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  177. }
  178. void cmLocalVisualStudio6Generator::AddDSPBuildRule(cmTarget& tgt)
  179. {
  180. std::string dspname = tgt.GetName();
  181. dspname += ".dsp.cmake";
  182. const char* dsprule =
  183. this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  184. cmCustomCommandLine commandLine;
  185. commandLine.push_back(dsprule);
  186. std::string makefileIn = this->Makefile->GetStartDirectory();
  187. makefileIn += "/";
  188. makefileIn += "CMakeLists.txt";
  189. std::string comment = "Building Custom Rule ";
  190. comment += makefileIn;
  191. std::string args;
  192. args = "-H";
  193. args += this->Convert(this->Makefile->GetHomeDirectory(),
  194. START_OUTPUT, UNCHANGED, true);
  195. commandLine.push_back(args);
  196. args = "-B";
  197. args +=
  198. this->Convert(this->Makefile->GetHomeOutputDirectory(),
  199. START_OUTPUT, UNCHANGED, true);
  200. commandLine.push_back(args);
  201. std::vector<std::string> const& listFiles = this->Makefile->GetListFiles();
  202. cmCustomCommandLines commandLines;
  203. commandLines.push_back(commandLine);
  204. const char* no_working_directory = 0;
  205. this->Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles,
  206. makefileIn.c_str(), commandLines,
  207. comment.c_str(),
  208. no_working_directory, true);
  209. if(cmSourceFile* file = this->Makefile->GetSource(makefileIn.c_str()))
  210. {
  211. tgt.AddSourceFile(file);
  212. }
  213. else
  214. {
  215. cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
  216. }
  217. }
  218. void cmLocalVisualStudio6Generator::WriteDSPFile(std::ostream& fout,
  219. const char *libName,
  220. cmTarget &target)
  221. {
  222. // For utility targets need custom command since pre- and post-
  223. // build does not do anything in Visual Studio 6. In order for the
  224. // rules to run in the correct order as custom commands, we need
  225. // special care for dependencies. The first rule must depend on all
  226. // the dependencies of all the rules. The later rules must each
  227. // depend only on the previous rule.
  228. if ((target.GetType() == cmTarget::UTILITY ||
  229. target.GetType() == cmTarget::GLOBAL_TARGET) &&
  230. (!target.GetPreBuildCommands().empty() ||
  231. !target.GetPostBuildCommands().empty()))
  232. {
  233. // Accumulate the dependencies of all the commands.
  234. std::vector<std::string> depends;
  235. for (std::vector<cmCustomCommand>::const_iterator cr =
  236. target.GetPreBuildCommands().begin();
  237. cr != target.GetPreBuildCommands().end(); ++cr)
  238. {
  239. depends.insert(depends.end(),
  240. cr->GetDepends().begin(), cr->GetDepends().end());
  241. }
  242. for (std::vector<cmCustomCommand>::const_iterator cr =
  243. target.GetPostBuildCommands().begin();
  244. cr != target.GetPostBuildCommands().end(); ++cr)
  245. {
  246. depends.insert(depends.end(),
  247. cr->GetDepends().begin(), cr->GetDepends().end());
  248. }
  249. // Add the pre- and post-build commands in order.
  250. int count = 1;
  251. for (std::vector<cmCustomCommand>::const_iterator cr =
  252. target.GetPreBuildCommands().begin();
  253. cr != target.GetPreBuildCommands().end(); ++cr)
  254. {
  255. this->AddUtilityCommandHack(target, count++, depends, *cr);
  256. }
  257. for (std::vector<cmCustomCommand>::const_iterator cr =
  258. target.GetPostBuildCommands().begin();
  259. cr != target.GetPostBuildCommands().end(); ++cr)
  260. {
  261. this->AddUtilityCommandHack(target, count++, depends, *cr);
  262. }
  263. }
  264. // trace the visual studio dependencies
  265. std::string name = libName;
  266. name += ".dsp.cmake";
  267. // We may be modifying the source groups temporarily, so make a copy.
  268. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  269. // get the classes from the source lists then add them to the groups
  270. std::vector<cmSourceFile*> const & classes = target.GetSourceFiles();
  271. // now all of the source files have been properly assigned to the target
  272. // now stick them into source groups using the reg expressions
  273. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  274. i != classes.end(); i++)
  275. {
  276. // Add the file to the list of sources.
  277. std::string source = (*i)->GetFullPath();
  278. cmSourceGroup& sourceGroup =
  279. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  280. sourceGroup.AssignSource(*i);
  281. // while we are at it, if it is a .rule file then for visual studio 6 we
  282. // must generate it
  283. if ((*i)->GetExtension() == "rule")
  284. {
  285. if(!cmSystemTools::FileExists(source.c_str()))
  286. {
  287. cmSystemTools::ReplaceString(source, "$(IntDir)/", "");
  288. #if defined(_WIN32) || defined(__CYGWIN__)
  289. std::ofstream fout(source.c_str(),
  290. std::ios::binary | std::ios::out
  291. | std::ios::trunc);
  292. #else
  293. std::ofstream fout(source.c_str(),
  294. std::ios::out | std::ios::trunc);
  295. #endif
  296. if(fout)
  297. {
  298. fout.write("# generated from CMake",22);
  299. fout.flush();
  300. fout.close();
  301. }
  302. }
  303. }
  304. }
  305. // Compute which sources need unique object computation.
  306. this->ComputeObjectNameRequirements(sourceGroups);
  307. // Write the DSP file's header.
  308. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  309. // Loop through every source group.
  310. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  311. sg != sourceGroups.end(); ++sg)
  312. {
  313. this->WriteGroup(&(*sg), target, fout, libName);
  314. }
  315. // Write the DSP file's footer.
  316. this->WriteDSPFooter(fout);
  317. }
  318. void cmLocalVisualStudio6Generator
  319. ::WriteGroup(const cmSourceGroup *sg, cmTarget target,
  320. std::ostream &fout, const char *libName)
  321. {
  322. const std::vector<const cmSourceFile *> &sourceFiles =
  323. sg->GetSourceFiles();
  324. // If the group is empty, don't write it at all.
  325. if(sourceFiles.empty())
  326. {
  327. return;
  328. }
  329. // If the group has a name, write the header.
  330. std::string name = sg->GetName();
  331. if(name != "")
  332. {
  333. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  334. }
  335. // Compute the maximum length of a configuration name.
  336. std::string::size_type config_len_max = 0;
  337. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  338. i != this->Configurations.end(); ++i)
  339. {
  340. // Strip the subdirectory name out of the configuration name.
  341. std::string config = this->GetConfigName(*i);
  342. if(config.size() > config_len_max)
  343. {
  344. config_len_max = config.size();
  345. }
  346. }
  347. // Compute the maximum length of the full path to the intermediate
  348. // files directory for any configuration. This is used to construct
  349. // object file names that do not produce paths that are too long.
  350. std::string::size_type dir_len = 0;
  351. dir_len += strlen(this->Makefile->GetCurrentOutputDirectory());
  352. dir_len += 1;
  353. dir_len += config_len_max;
  354. dir_len += 1;
  355. // Loop through each source in the source group.
  356. for(std::vector<const cmSourceFile *>::const_iterator sf =
  357. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  358. {
  359. std::string source = (*sf)->GetFullPath();
  360. const cmCustomCommand *command =
  361. (*sf)->GetCustomCommand();
  362. std::string compileFlags;
  363. std::vector<std::string> depends;
  364. std::string objectNameDir;
  365. if(this->NeedObjectName.find(*sf) != this->NeedObjectName.end())
  366. {
  367. objectNameDir =
  368. cmSystemTools::GetFilenamePath(
  369. this->GetObjectFileNameWithoutTarget(*(*sf), dir_len));
  370. }
  371. // Add per-source file flags.
  372. if(const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS"))
  373. {
  374. compileFlags += cflags;
  375. }
  376. const char* lang = this->GetSourceFileLanguage(*(*sf));
  377. if(lang)
  378. {
  379. if(strcmp(lang, "CXX") == 0)
  380. {
  381. // force a C++ file type
  382. compileFlags += " /TP ";
  383. }
  384. else if(strcmp(lang, "C") == 0)
  385. {
  386. // force to c file type
  387. compileFlags += " /TC ";
  388. }
  389. }
  390. // Add per-source and per-configuration preprocessor definitions.
  391. std::map<cmStdString, cmStdString> cdmap;
  392. this->AppendDefines(compileFlags,
  393. (*sf)->GetProperty("COMPILE_DEFINITIONS"), lang);
  394. if(const char* cdefs = (*sf)->GetProperty("COMPILE_DEFINITIONS_DEBUG"))
  395. {
  396. this->AppendDefines(cdmap["DEBUG"], cdefs, lang);
  397. }
  398. if(const char* cdefs = (*sf)->GetProperty("COMPILE_DEFINITIONS_RELEASE"))
  399. {
  400. this->AppendDefines(cdmap["RELEASE"], cdefs, lang);
  401. }
  402. if(const char* cdefs =
  403. (*sf)->GetProperty("COMPILE_DEFINITIONS_MINSIZEREL"))
  404. {
  405. this->AppendDefines(cdmap["MINSIZEREL"], cdefs, lang);
  406. }
  407. if(const char* cdefs =
  408. (*sf)->GetProperty("COMPILE_DEFINITIONS_RELWITHDEBINFO"))
  409. {
  410. this->AppendDefines(cdmap["RELWITHDEBINFO"], cdefs, lang);
  411. }
  412. bool excludedFromBuild =
  413. (lang && (*sf)->GetPropertyAsBool("HEADER_FILE_ONLY"));
  414. // Check for extra object-file dependencies.
  415. const char* dependsValue = (*sf)->GetProperty("OBJECT_DEPENDS");
  416. if(dependsValue)
  417. {
  418. cmSystemTools::ExpandListArgument(dependsValue, depends);
  419. }
  420. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  421. target.GetType() == cmTarget::GLOBAL_TARGET)
  422. {
  423. fout << "# Begin Source File\n\n";
  424. // Tell MS-Dev what the source is. If the compiler knows how to
  425. // build it, then it will.
  426. fout << "SOURCE=" <<
  427. this->ConvertToOptionallyRelativeOutputPath(source.c_str()) << "\n\n";
  428. if(!depends.empty())
  429. {
  430. // Write out the dependencies for the rule.
  431. fout << "USERDEP__HACK=";
  432. for(std::vector<std::string>::const_iterator d = depends.begin();
  433. d != depends.end(); ++d)
  434. {
  435. fout << "\\\n\t" <<
  436. this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  437. }
  438. fout << "\n";
  439. }
  440. if (command)
  441. {
  442. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  443. this->WriteCustomRule(fout, source.c_str(), *command, flags);
  444. }
  445. else if(!compileFlags.empty() || !objectNameDir.empty() ||
  446. excludedFromBuild || !cdmap.empty())
  447. {
  448. for(std::vector<std::string>::iterator i
  449. = this->Configurations.begin();
  450. i != this->Configurations.end(); ++i)
  451. {
  452. // Strip the subdirectory name out of the configuration name.
  453. std::string config = this->GetConfigName(*i);
  454. if (i == this->Configurations.begin())
  455. {
  456. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  457. }
  458. else
  459. {
  460. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  461. }
  462. if(excludedFromBuild)
  463. {
  464. fout << "# PROP Exclude_From_Build 1\n";
  465. }
  466. if(!compileFlags.empty())
  467. {
  468. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  469. }
  470. std::map<cmStdString, cmStdString>::iterator cdi =
  471. cdmap.find(cmSystemTools::UpperCase(config));
  472. if(cdi != cdmap.end() && !cdi->second.empty())
  473. {
  474. fout << "\n# ADD CPP " << cdi->second << "\n\n";
  475. }
  476. if(!objectNameDir.empty())
  477. {
  478. // Setup an alternate object file directory.
  479. fout << "\n# PROP Intermediate_Dir \""
  480. << config << "/" << objectNameDir << "\"\n\n";
  481. }
  482. }
  483. fout << "!ENDIF\n\n";
  484. }
  485. fout << "# End Source File\n";
  486. }
  487. }
  488. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  489. for(unsigned int i=0;i<children.size();++i)
  490. {
  491. this->WriteGroup(&children[i], target, fout, libName);
  492. }
  493. // If the group has a name, write the footer.
  494. if(name != "")
  495. {
  496. this->WriteDSPEndGroup(fout);
  497. }
  498. }
  499. void
  500. cmLocalVisualStudio6Generator
  501. ::AddUtilityCommandHack(cmTarget& target, int count,
  502. std::vector<std::string>& depends,
  503. const cmCustomCommand& origCommand)
  504. {
  505. // Create a fake output that forces the rule to run.
  506. char* output = new char[(strlen(this->Makefile->GetStartOutputDirectory()) +
  507. strlen(target.GetName()) + 30)];
  508. sprintf(output,"%s/%s_force_%i", this->Makefile->GetStartOutputDirectory(),
  509. target.GetName(), count);
  510. std::string comment = this->ConstructComment(origCommand, "<hack>");
  511. // Add the rule with the given dependencies and commands.
  512. const char* no_main_dependency = 0;
  513. this->Makefile->AddCustomCommandToOutput(output,
  514. depends,
  515. no_main_dependency,
  516. origCommand.GetCommandLines(),
  517. comment.c_str(),
  518. origCommand.GetWorkingDirectory());
  519. // Replace the dependencies with the output of this rule so that the
  520. // next rule added will run after this one.
  521. depends.clear();
  522. depends.push_back(output);
  523. // Add a source file representing this output to the project.
  524. cmSourceFile* outsf = this->Makefile->GetSourceFileWithOutput(output);
  525. target.AddSourceFile(outsf);
  526. // Free the fake output name.
  527. delete [] output;
  528. }
  529. void
  530. cmLocalVisualStudio6Generator
  531. ::WriteCustomRule(std::ostream& fout,
  532. const char* source,
  533. const cmCustomCommand& command,
  534. const char* flags)
  535. {
  536. std::string comment =
  537. this->ConstructComment(command, "Building Custom Rule $(InputPath)");
  538. if(comment == "<hack>")
  539. {
  540. comment = "";
  541. }
  542. // Write the rule for each configuration.
  543. std::vector<std::string>::iterator i;
  544. for(i = this->Configurations.begin(); i != this->Configurations.end(); ++i)
  545. {
  546. std::string config = this->GetConfigName(*i);
  547. std::string script =
  548. this->ConstructScript(command.GetCommandLines(),
  549. command.GetWorkingDirectory(),
  550. config.c_str(),
  551. command.GetEscapeOldStyle(),
  552. command.GetEscapeAllowMakeVars(),
  553. "\\\n\t");
  554. if (i == this->Configurations.begin())
  555. {
  556. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  557. }
  558. else
  559. {
  560. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  561. }
  562. if(flags)
  563. {
  564. fout << "\n# ADD CPP " << flags << "\n\n";
  565. }
  566. // Write out the dependencies for the rule.
  567. fout << "USERDEP__HACK=";
  568. for(std::vector<std::string>::const_iterator d =
  569. command.GetDepends().begin();
  570. d != command.GetDepends().end();
  571. ++d)
  572. {
  573. // Lookup the real name of the dependency in case it is a CMake target.
  574. std::string dep = this->GetRealDependency(d->c_str(),
  575. config.c_str());
  576. fout << "\\\n\t" <<
  577. this->ConvertToOptionallyRelativeOutputPath(dep.c_str());
  578. }
  579. fout << "\n";
  580. fout << "# PROP Ignore_Default_Tool 1\n";
  581. fout << "# Begin Custom Build -";
  582. if(!comment.empty())
  583. {
  584. fout << " " << comment.c_str();
  585. }
  586. fout << "\n\n";
  587. if(command.GetOutputs().empty())
  588. {
  589. fout << source
  590. << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  591. fout << script.c_str() << "\n\n";
  592. }
  593. else
  594. {
  595. for(std::vector<std::string>::const_iterator o =
  596. command.GetOutputs().begin();
  597. o != command.GetOutputs().end();
  598. ++o)
  599. {
  600. // Write a rule for every output generated by this command.
  601. fout << this->ConvertToOptionallyRelativeOutputPath(o->c_str())
  602. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  603. fout << script.c_str() << "\n\n";
  604. }
  605. }
  606. fout << "# End Custom Build\n\n";
  607. }
  608. fout << "!ENDIF\n\n";
  609. }
  610. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  611. const char* group,
  612. const char* filter)
  613. {
  614. fout << "# Begin Group \"" << group << "\"\n"
  615. "# PROP Default_Filter \"" << filter << "\"\n";
  616. }
  617. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  618. {
  619. fout << "# End Group\n";
  620. }
  621. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b,
  622. const char* libName,
  623. cmTarget& target)
  624. {
  625. std::string root= this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  626. const char *def=
  627. this->Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  628. if( def)
  629. {
  630. root = def;
  631. }
  632. else
  633. {
  634. root += "/Templates";
  635. }
  636. switch(b)
  637. {
  638. case STATIC_LIBRARY:
  639. this->DSPHeaderTemplate = root;
  640. this->DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  641. this->DSPFooterTemplate = root;
  642. this->DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  643. break;
  644. case DLL:
  645. this->DSPHeaderTemplate = root;
  646. this->DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  647. this->DSPFooterTemplate = root;
  648. this->DSPFooterTemplate += "/DLLFooter.dsptemplate";
  649. break;
  650. case EXECUTABLE:
  651. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  652. {
  653. this->DSPHeaderTemplate = root;
  654. this->DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  655. this->DSPFooterTemplate = root;
  656. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  657. }
  658. else
  659. {
  660. this->DSPHeaderTemplate = root;
  661. this->DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  662. this->DSPFooterTemplate = root;
  663. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  664. }
  665. break;
  666. case UTILITY:
  667. this->DSPHeaderTemplate = root;
  668. this->DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  669. this->DSPFooterTemplate = root;
  670. this->DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  671. break;
  672. }
  673. // once the build type is set, determine what configurations are
  674. // possible
  675. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  676. cmsys::RegularExpression reg("# Name ");
  677. if(!fin)
  678. {
  679. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  680. }
  681. // reset this->Configurations
  682. this->Configurations.erase(this->Configurations.begin(),
  683. this->Configurations.end());
  684. // now add all the configurations possible
  685. std::string line;
  686. while(cmSystemTools::GetLineFromStream(fin, line))
  687. {
  688. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  689. if (reg.find(line))
  690. {
  691. this->Configurations.push_back(line.substr(reg.end()));
  692. }
  693. }
  694. }
  695. // look for custom rules on a target and collect them together
  696. std::string
  697. cmLocalVisualStudio6Generator::CreateTargetRules(cmTarget &target,
  698. const char* configName,
  699. const char * /* libName */)
  700. {
  701. std::string customRuleCode = "";
  702. if (target.GetType() >= cmTarget::UTILITY )
  703. {
  704. return customRuleCode;
  705. }
  706. // are there any rules?
  707. if (target.GetPreBuildCommands().size() +
  708. target.GetPreLinkCommands().size() +
  709. target.GetPostBuildCommands().size() == 0)
  710. {
  711. return customRuleCode;
  712. }
  713. customRuleCode = "# Begin Special Build Tool\n";
  714. // Write the pre-build and pre-link together (VS6 does not support
  715. // both). Make sure no continuation character is put on the last
  716. // line.
  717. int prelink_total = (static_cast<int>(target.GetPreBuildCommands().size())+
  718. static_cast<int>(target.GetPreLinkCommands().size()));
  719. int prelink_count = 0;
  720. if(prelink_total > 0)
  721. {
  722. // header stuff
  723. customRuleCode += "PreLink_Cmds=";
  724. }
  725. for (std::vector<cmCustomCommand>::const_iterator cr =
  726. target.GetPreBuildCommands().begin();
  727. cr != target.GetPreBuildCommands().end(); ++cr)
  728. {
  729. if(prelink_count++ > 0)
  730. {
  731. customRuleCode += "\\\n\t";
  732. }
  733. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  734. cr->GetWorkingDirectory(),
  735. configName,
  736. cr->GetEscapeOldStyle(),
  737. cr->GetEscapeAllowMakeVars(),
  738. "\\\n\t");
  739. }
  740. for (std::vector<cmCustomCommand>::const_iterator cr =
  741. target.GetPreLinkCommands().begin();
  742. cr != target.GetPreLinkCommands().end(); ++cr)
  743. {
  744. if(prelink_count++ > 0)
  745. {
  746. customRuleCode += "\\\n\t";
  747. }
  748. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  749. cr->GetWorkingDirectory(),
  750. configName,
  751. cr->GetEscapeOldStyle(),
  752. cr->GetEscapeAllowMakeVars(),
  753. "\\\n\t");
  754. }
  755. if(prelink_total > 0)
  756. {
  757. customRuleCode += "\n";
  758. }
  759. // Write the post-build rules. Make sure no continuation character
  760. // is put on the last line.
  761. int postbuild_total =
  762. static_cast<int>(target.GetPostBuildCommands().size());
  763. int postbuild_count = 0;
  764. if(postbuild_total > 0)
  765. {
  766. customRuleCode += "PostBuild_Cmds=";
  767. }
  768. for (std::vector<cmCustomCommand>::const_iterator cr =
  769. target.GetPostBuildCommands().begin();
  770. cr != target.GetPostBuildCommands().end(); ++cr)
  771. {
  772. if(postbuild_count++ > 0)
  773. {
  774. customRuleCode += "\\\n\t";
  775. }
  776. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  777. cr->GetWorkingDirectory(),
  778. configName,
  779. cr->GetEscapeOldStyle(),
  780. cr->GetEscapeAllowMakeVars(),
  781. "\\\n\t");
  782. }
  783. if(postbuild_total > 0)
  784. {
  785. customRuleCode += "\n";
  786. }
  787. customRuleCode += "# End Special Build Tool\n";
  788. return customRuleCode;
  789. }
  790. inline std::string removeQuotes(const std::string& s)
  791. {
  792. if(s[0] == '\"' && s[s.size()-1] == '\"')
  793. {
  794. return s.substr(1, s.size()-2);
  795. }
  796. return s;
  797. }
  798. // Code in blocks surrounded by a test for this definition is needed
  799. // only for compatibility with user project's replacement DSP
  800. // templates. The CMake templates no longer use them.
  801. #define CM_USE_OLD_VS6
  802. void cmLocalVisualStudio6Generator
  803. ::WriteDSPHeader(std::ostream& fout,
  804. const char *libName, cmTarget &target,
  805. std::vector<cmSourceGroup> &)
  806. {
  807. // Lookup the output directory for the target.
  808. std::string outPath = target.GetDirectory();
  809. #ifdef CM_USE_OLD_VS6
  810. // Lookup the library and executable output directories.
  811. std::string libPath;
  812. if(this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  813. {
  814. libPath = this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  815. }
  816. std::string exePath;
  817. if(this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  818. {
  819. exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  820. }
  821. // Make sure there are trailing slashes.
  822. if(!libPath.empty())
  823. {
  824. if(libPath[libPath.size()-1] != '/')
  825. {
  826. libPath += "/";
  827. }
  828. }
  829. if(!exePath.empty())
  830. {
  831. if(exePath[exePath.size()-1] != '/')
  832. {
  833. exePath += "/";
  834. }
  835. }
  836. std::set<std::string> pathEmitted;
  837. // determine the link directories
  838. std::string libOptions;
  839. std::string libDebugOptions;
  840. std::string libOptimizedOptions;
  841. std::string libMultiLineOptions;
  842. std::string libMultiLineOptionsForDebug;
  843. std::string libMultiLineDebugOptions;
  844. std::string libMultiLineOptimizedOptions;
  845. if(libPath.size())
  846. {
  847. std::string lpath =
  848. this->ConvertToOptionallyRelativeOutputPath(libPath.c_str());
  849. if(lpath.size() == 0)
  850. {
  851. lpath = ".";
  852. }
  853. std::string lpathIntDir = libPath + "$(INTDIR)";
  854. lpathIntDir =
  855. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  856. if(pathEmitted.insert(lpath).second)
  857. {
  858. libOptions += " /LIBPATH:";
  859. libOptions += lpathIntDir;
  860. libOptions += " ";
  861. libOptions += " /LIBPATH:";
  862. libOptions += lpath;
  863. libOptions += " ";
  864. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  865. libMultiLineOptions += lpathIntDir;
  866. libMultiLineOptions += " ";
  867. libMultiLineOptions += " /LIBPATH:";
  868. libMultiLineOptions += lpath;
  869. libMultiLineOptions += " \n";
  870. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  871. libMultiLineOptionsForDebug += lpathIntDir;
  872. libMultiLineOptionsForDebug += " ";
  873. libMultiLineOptionsForDebug += " /LIBPATH:";
  874. libMultiLineOptionsForDebug += lpath;
  875. libMultiLineOptionsForDebug += " \n";
  876. }
  877. }
  878. if(exePath.size())
  879. {
  880. std::string lpath =
  881. this->ConvertToOptionallyRelativeOutputPath(exePath.c_str());
  882. if(lpath.size() == 0)
  883. {
  884. lpath = ".";
  885. }
  886. std::string lpathIntDir = exePath + "$(INTDIR)";
  887. lpathIntDir =
  888. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  889. if(pathEmitted.insert(lpath).second)
  890. {
  891. libOptions += " /LIBPATH:";
  892. libOptions += lpathIntDir;
  893. libOptions += " ";
  894. libOptions += " /LIBPATH:";
  895. libOptions += lpath;
  896. libOptions += " ";
  897. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  898. libMultiLineOptions += lpathIntDir;
  899. libMultiLineOptions += " ";
  900. libMultiLineOptions += " /LIBPATH:";
  901. libMultiLineOptions += lpath;
  902. libMultiLineOptions += " \n";
  903. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  904. libMultiLineOptionsForDebug += lpathIntDir;
  905. libMultiLineOptionsForDebug += " ";
  906. libMultiLineOptionsForDebug += " /LIBPATH:";
  907. libMultiLineOptionsForDebug += lpath;
  908. libMultiLineOptionsForDebug += " \n";
  909. }
  910. }
  911. std::vector<std::string>::const_iterator i;
  912. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  913. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  914. {
  915. std::string path = *i;
  916. if(path[path.size()-1] != '/')
  917. {
  918. path += "/";
  919. }
  920. std::string lpath =
  921. this->ConvertToOptionallyRelativeOutputPath(path.c_str());
  922. if(lpath.size() == 0)
  923. {
  924. lpath = ".";
  925. }
  926. std::string lpathIntDir = path + "$(INTDIR)";
  927. lpathIntDir =
  928. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  929. if(pathEmitted.insert(lpath).second)
  930. {
  931. libOptions += " /LIBPATH:";
  932. libOptions += lpathIntDir;
  933. libOptions += " ";
  934. libOptions += " /LIBPATH:";
  935. libOptions += lpath;
  936. libOptions += " ";
  937. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  938. libMultiLineOptions += lpathIntDir;
  939. libMultiLineOptions += " ";
  940. libMultiLineOptions += " /LIBPATH:";
  941. libMultiLineOptions += lpath;
  942. libMultiLineOptions += " \n";
  943. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  944. libMultiLineOptionsForDebug += lpathIntDir;
  945. libMultiLineOptionsForDebug += " ";
  946. libMultiLineOptionsForDebug += " /LIBPATH:";
  947. libMultiLineOptionsForDebug += lpath;
  948. libMultiLineOptionsForDebug += " \n";
  949. }
  950. }
  951. // find link libraries
  952. const cmTarget::LinkLibraryVectorType& libs = target.GetLinkLibraries();
  953. cmTarget::LinkLibraryVectorType::const_iterator j;
  954. for(j = libs.begin(); j != libs.end(); ++j)
  955. {
  956. // add libraries to executables and dlls (but never include
  957. // a library in a library, bad recursion)
  958. // NEVER LINK STATIC LIBRARIES TO OTHER STATIC LIBRARIES
  959. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  960. && target.GetType() != cmTarget::STATIC_LIBRARY
  961. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  962. (target.GetType()==cmTarget::SHARED_LIBRARY && libName != j->first) ||
  963. (target.GetType()==cmTarget::MODULE_LIBRARY && libName != j->first))
  964. {
  965. // Compute the proper name to use to link this library.
  966. std::string lib;
  967. std::string libDebug;
  968. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, j->first.c_str(),
  969. false);
  970. if(tgt)
  971. {
  972. lib = cmSystemTools::GetFilenameWithoutExtension
  973. (tgt->GetFullName().c_str());
  974. libDebug = cmSystemTools::GetFilenameWithoutExtension
  975. (tgt->GetFullName("Debug").c_str());
  976. lib += ".lib";
  977. libDebug += ".lib";
  978. }
  979. else
  980. {
  981. lib = j->first.c_str();
  982. libDebug = j->first.c_str();
  983. if(j->first.find(".lib") == std::string::npos)
  984. {
  985. lib += ".lib";
  986. libDebug += ".lib";
  987. }
  988. }
  989. lib = this->ConvertToOptionallyRelativeOutputPath(lib.c_str());
  990. libDebug =
  991. this->ConvertToOptionallyRelativeOutputPath(libDebug.c_str());
  992. if (j->second == cmTarget::GENERAL)
  993. {
  994. libOptions += " ";
  995. libOptions += lib;
  996. libMultiLineOptions += "# ADD LINK32 ";
  997. libMultiLineOptions += lib;
  998. libMultiLineOptions += "\n";
  999. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1000. libMultiLineOptionsForDebug += libDebug;
  1001. libMultiLineOptionsForDebug += "\n";
  1002. }
  1003. if (j->second == cmTarget::DEBUG)
  1004. {
  1005. libDebugOptions += " ";
  1006. libDebugOptions += lib;
  1007. libMultiLineDebugOptions += "# ADD LINK32 ";
  1008. libMultiLineDebugOptions += libDebug;
  1009. libMultiLineDebugOptions += "\n";
  1010. }
  1011. if (j->second == cmTarget::OPTIMIZED)
  1012. {
  1013. libOptimizedOptions += " ";
  1014. libOptimizedOptions += lib;
  1015. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  1016. libMultiLineOptimizedOptions += lib;
  1017. libMultiLineOptimizedOptions += "\n";
  1018. }
  1019. }
  1020. }
  1021. #endif
  1022. // Get extra linker options for this target type.
  1023. std::string extraLinkOptions;
  1024. if(target.GetType() == cmTarget::EXECUTABLE)
  1025. {
  1026. extraLinkOptions =
  1027. this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  1028. }
  1029. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1030. {
  1031. extraLinkOptions =
  1032. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1033. }
  1034. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1035. {
  1036. extraLinkOptions =
  1037. this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1038. }
  1039. // Get extra linker options for this target.
  1040. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  1041. {
  1042. extraLinkOptions += " ";
  1043. extraLinkOptions += targetLinkFlags;
  1044. }
  1045. // Get standard libraries for this language.
  1046. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1047. target.GetType() <= cmTarget::MODULE_LIBRARY)
  1048. {
  1049. // Get the language to use for linking.
  1050. const char* linkLanguage =
  1051. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1052. if(!linkLanguage)
  1053. {
  1054. cmSystemTools::Error
  1055. ("CMake can not determine linker language for target:",
  1056. target.GetName());
  1057. return;
  1058. }
  1059. // Compute the variable name to lookup standard libraries for this
  1060. // language.
  1061. std::string standardLibsVar = "CMAKE_";
  1062. standardLibsVar += linkLanguage;
  1063. standardLibsVar += "_STANDARD_LIBRARIES";
  1064. // Add standard libraries.
  1065. if(const char* stdLibs =
  1066. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1067. {
  1068. extraLinkOptions += " ";
  1069. extraLinkOptions += stdLibs;
  1070. }
  1071. }
  1072. // Compute version number information.
  1073. std::string targetVersionFlag;
  1074. if(target.GetType() == cmTarget::EXECUTABLE ||
  1075. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1076. target.GetType() == cmTarget::MODULE_LIBRARY)
  1077. {
  1078. int major;
  1079. int minor;
  1080. target.GetTargetVersion(major, minor);
  1081. cmOStringStream targetVersionStream;
  1082. targetVersionStream << "/version:" << major << "." << minor;
  1083. targetVersionFlag = targetVersionStream.str();
  1084. }
  1085. // Compute the real name of the target.
  1086. std::string outputName =
  1087. "(OUTPUT_NAME is for libraries and executables only)";
  1088. std::string outputNameDebug = outputName;
  1089. std::string outputNameRelease = outputName;
  1090. std::string outputNameMinSizeRel = outputName;
  1091. std::string outputNameRelWithDebInfo = outputName;
  1092. if(target.GetType() == cmTarget::EXECUTABLE ||
  1093. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1094. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1095. target.GetType() == cmTarget::MODULE_LIBRARY)
  1096. {
  1097. outputName = target.GetFullName();
  1098. outputNameDebug = target.GetFullName("Debug");
  1099. outputNameRelease = target.GetFullName("Release");
  1100. outputNameMinSizeRel = target.GetFullName("MinSizeRel");
  1101. outputNameRelWithDebInfo = target.GetFullName("RelWithDebInfo");
  1102. }
  1103. // Compute the proper link information for the target.
  1104. std::string optionsDebug;
  1105. std::string optionsRelease;
  1106. std::string optionsMinSizeRel;
  1107. std::string optionsRelWithDebInfo;
  1108. if(target.GetType() == cmTarget::EXECUTABLE ||
  1109. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1110. target.GetType() == cmTarget::MODULE_LIBRARY)
  1111. {
  1112. this->ComputeLinkOptions(target, "Debug", extraLinkOptions,
  1113. optionsDebug);
  1114. this->ComputeLinkOptions(target, "Release", extraLinkOptions,
  1115. optionsRelease);
  1116. this->ComputeLinkOptions(target, "MinSizeRel", extraLinkOptions,
  1117. optionsMinSizeRel);
  1118. this->ComputeLinkOptions(target, "RelWithDebInfo", extraLinkOptions,
  1119. optionsRelWithDebInfo);
  1120. }
  1121. // Compute the path of the import library.
  1122. std::string targetImplibFlagDebug;
  1123. std::string targetImplibFlagRelease;
  1124. std::string targetImplibFlagMinSizeRel;
  1125. std::string targetImplibFlagRelWithDebInfo;
  1126. if(target.GetType() == cmTarget::SHARED_LIBRARY ||
  1127. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1128. target.GetType() == cmTarget::EXECUTABLE)
  1129. {
  1130. std::string fullPathImpDebug = target.GetDirectory("Debug", true);
  1131. std::string fullPathImpRelease = target.GetDirectory("Release", true);
  1132. std::string fullPathImpMinSizeRel =
  1133. target.GetDirectory("MinSizeRel", true);
  1134. std::string fullPathImpRelWithDebInfo =
  1135. target.GetDirectory("RelWithDebInfo", true);
  1136. fullPathImpDebug += "/";
  1137. fullPathImpRelease += "/";
  1138. fullPathImpMinSizeRel += "/";
  1139. fullPathImpRelWithDebInfo += "/";
  1140. fullPathImpDebug += target.GetFullName("Debug", true);
  1141. fullPathImpRelease += target.GetFullName("Release", true);
  1142. fullPathImpMinSizeRel += target.GetFullName("MinSizeRel", true);
  1143. fullPathImpRelWithDebInfo += target.GetFullName("RelWithDebInfo", true);
  1144. targetImplibFlagDebug = "/implib:";
  1145. targetImplibFlagRelease = "/implib:";
  1146. targetImplibFlagMinSizeRel = "/implib:";
  1147. targetImplibFlagRelWithDebInfo = "/implib:";
  1148. targetImplibFlagDebug +=
  1149. this->ConvertToOptionallyRelativeOutputPath(fullPathImpDebug.c_str());
  1150. targetImplibFlagRelease +=
  1151. this->ConvertToOptionallyRelativeOutputPath(fullPathImpRelease.c_str());
  1152. targetImplibFlagMinSizeRel +=
  1153. this->ConvertToOptionallyRelativeOutputPath(
  1154. fullPathImpMinSizeRel.c_str());
  1155. targetImplibFlagRelWithDebInfo +=
  1156. this->ConvertToOptionallyRelativeOutputPath(
  1157. fullPathImpRelWithDebInfo.c_str());
  1158. }
  1159. #ifdef CM_USE_OLD_VS6
  1160. // Compute link information for the target.
  1161. if(extraLinkOptions.size())
  1162. {
  1163. libOptions += " ";
  1164. libOptions += extraLinkOptions;
  1165. libOptions += " ";
  1166. libMultiLineOptions += "# ADD LINK32 ";
  1167. libMultiLineOptions += extraLinkOptions;
  1168. libMultiLineOptions += " \n";
  1169. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1170. libMultiLineOptionsForDebug += extraLinkOptions;
  1171. libMultiLineOptionsForDebug += " \n";
  1172. }
  1173. #endif
  1174. // are there any custom rules on the target itself
  1175. // only if the target is a lib or exe
  1176. std::string customRuleCodeRelease
  1177. = this->CreateTargetRules(target, "RELEASE", libName);
  1178. std::string customRuleCodeDebug
  1179. = this->CreateTargetRules(target, "DEBUG", libName);
  1180. std::string customRuleCodeMinSizeRel
  1181. = this->CreateTargetRules(target, "MINSIZEREL", libName);
  1182. std::string customRuleCodeRelWithDebInfo
  1183. = this->CreateTargetRules(target, "RELWITHDEBINFO", libName);
  1184. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  1185. if(!fin)
  1186. {
  1187. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  1188. }
  1189. std::string staticLibOptions;
  1190. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1191. {
  1192. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  1193. {
  1194. staticLibOptions = libflags;
  1195. }
  1196. }
  1197. // Add the export symbol definition for shared library objects.
  1198. std::string exportSymbol;
  1199. if(const char* exportMacro = target.GetExportMacro())
  1200. {
  1201. exportSymbol = exportMacro;
  1202. }
  1203. std::string line;
  1204. while(cmSystemTools::GetLineFromStream(fin, line))
  1205. {
  1206. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  1207. if(!mfcFlag)
  1208. {
  1209. mfcFlag = "0";
  1210. }
  1211. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  1212. exportSymbol.c_str());
  1213. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  1214. mfcFlag);
  1215. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1216. {
  1217. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS",
  1218. staticLibOptions.c_str());
  1219. }
  1220. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  1221. {
  1222. cmSystemTools::ReplaceString(line, "/nologo", "");
  1223. }
  1224. #ifdef CM_USE_OLD_VS6
  1225. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  1226. libOptions.c_str());
  1227. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  1228. libDebugOptions.c_str());
  1229. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  1230. libOptimizedOptions.c_str());
  1231. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES_FOR_DEBUG",
  1232. libMultiLineOptionsForDebug.c_str());
  1233. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  1234. libMultiLineOptions.c_str());
  1235. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  1236. libMultiLineDebugOptions.c_str());
  1237. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  1238. libMultiLineOptimizedOptions.c_str());
  1239. #endif
  1240. // Substitute the rules for custom command. When specifying just the
  1241. // target name for the command the command can be different for
  1242. // different configs
  1243. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_RELEASE",
  1244. customRuleCodeRelease.c_str());
  1245. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_DEBUG",
  1246. customRuleCodeDebug.c_str());
  1247. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_MINSIZEREL",
  1248. customRuleCodeMinSizeRel.c_str());
  1249. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_RELWITHDEBINFO",
  1250. customRuleCodeRelWithDebInfo.c_str());
  1251. // Substitute the real output name into the template.
  1252. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_DEBUG",
  1253. outputNameDebug.c_str());
  1254. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELEASE",
  1255. outputNameRelease.c_str());
  1256. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_MINSIZEREL",
  1257. outputNameMinSizeRel.c_str());
  1258. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELWITHDEBINFO",
  1259. outputNameRelWithDebInfo.c_str());
  1260. cmSystemTools::ReplaceString(line, "OUTPUT_NAME", outputName.c_str());
  1261. // Substitute the proper link information into the template.
  1262. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_DEBUG",
  1263. optionsDebug.c_str());
  1264. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELEASE",
  1265. optionsRelease.c_str());
  1266. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_MINSIZEREL",
  1267. optionsMinSizeRel.c_str());
  1268. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELWITHDEBINFO",
  1269. optionsRelWithDebInfo.c_str());
  1270. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  1271. this->IncludeOptions.c_str());
  1272. cmSystemTools::ReplaceString(line, "TARGET_VERSION_FLAG",
  1273. targetVersionFlag.c_str());
  1274. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_DEBUG",
  1275. targetImplibFlagDebug.c_str());
  1276. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELEASE",
  1277. targetImplibFlagRelease.c_str());
  1278. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_MINSIZEREL",
  1279. targetImplibFlagMinSizeRel.c_str());
  1280. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELWITHDEBINFO",
  1281. targetImplibFlagRelWithDebInfo.c_str());
  1282. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  1283. #ifdef CM_USE_OLD_VS6
  1284. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  1285. // are already quoted in the template file,
  1286. // we need to remove the quotes here, we still need
  1287. // to convert to output path for unix to win32 conversion
  1288. cmSystemTools::ReplaceString
  1289. (line, "LIBRARY_OUTPUT_PATH",
  1290. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1291. (libPath.c_str())).c_str());
  1292. cmSystemTools::ReplaceString
  1293. (line, "EXECUTABLE_OUTPUT_PATH",
  1294. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1295. (exePath.c_str())).c_str());
  1296. #endif
  1297. cmSystemTools::ReplaceString
  1298. (line, "OUTPUT_DIRECTORY",
  1299. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1300. (outPath.c_str())).c_str());
  1301. cmSystemTools::ReplaceString(line,
  1302. "EXTRA_DEFINES",
  1303. this->Makefile->GetDefineFlags());
  1304. const char* debugPostfix
  1305. = this->Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  1306. cmSystemTools::ReplaceString(line, "DEBUG_POSTFIX",
  1307. debugPostfix?debugPostfix:"");
  1308. // store flags for each configuration
  1309. std::string flags = " ";
  1310. std::string flagsRelease = " ";
  1311. std::string flagsMinSize = " ";
  1312. std::string flagsDebug = " ";
  1313. std::string flagsDebugRel = " ";
  1314. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1315. target.GetType() <= cmTarget::MODULE_LIBRARY)
  1316. {
  1317. const char* linkLanguage =
  1318. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1319. if(!linkLanguage)
  1320. {
  1321. cmSystemTools::Error
  1322. ("CMake can not determine linker language for target:",
  1323. target.GetName());
  1324. return;
  1325. }
  1326. // if CXX is on and the target contains cxx code then add the cxx flags
  1327. std::string baseFlagVar = "CMAKE_";
  1328. baseFlagVar += linkLanguage;
  1329. baseFlagVar += "_FLAGS";
  1330. flags = this->Makefile->GetSafeDefinition(baseFlagVar.c_str());
  1331. std::string flagVar = baseFlagVar + "_RELEASE";
  1332. flagsRelease = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1333. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  1334. if(const char* targetLinkFlags =
  1335. target.GetProperty("LINK_FLAGS_RELEASE"))
  1336. {
  1337. flagsRelease += targetLinkFlags;
  1338. flagsRelease += " ";
  1339. }
  1340. flagVar = baseFlagVar + "_MINSIZEREL";
  1341. flagsMinSize = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1342. flagsMinSize += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  1343. if(const char* targetLinkFlags =
  1344. target.GetProperty("LINK_FLAGS_MINSIZEREL"))
  1345. {
  1346. flagsMinSize += targetLinkFlags;
  1347. flagsMinSize += " ";
  1348. }
  1349. flagVar = baseFlagVar + "_DEBUG";
  1350. flagsDebug = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1351. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  1352. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_DEBUG"))
  1353. {
  1354. flagsDebug += targetLinkFlags;
  1355. flagsDebug += " ";
  1356. }
  1357. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  1358. flagsDebugRel = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1359. flagsDebugRel += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  1360. if(const char* targetLinkFlags =
  1361. target.GetProperty("LINK_FLAGS_RELWITHDEBINFO"))
  1362. {
  1363. flagsDebugRel += targetLinkFlags;
  1364. flagsDebugRel += " ";
  1365. }
  1366. }
  1367. // if unicode is not found, then add -D_MBCS
  1368. std::string defs = this->Makefile->GetDefineFlags();
  1369. if(flags.find("D_UNICODE") == flags.npos &&
  1370. defs.find("D_UNICODE") == flags.npos)
  1371. {
  1372. flags += " /D \"_MBCS\"";
  1373. }
  1374. // Add per-target flags.
  1375. if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
  1376. {
  1377. flags += " ";
  1378. flags += targetFlags;
  1379. }
  1380. // Add per-target and per-configuration preprocessor definitions.
  1381. this->AppendDefines(flags, target.GetProperty("COMPILE_DEFINITIONS"), 0);
  1382. this->AppendDefines(flagsDebug,
  1383. target.GetProperty("COMPILE_DEFINITIONS_DEBUG"), 0);
  1384. this->AppendDefines(flagsRelease,
  1385. target.GetProperty("COMPILE_DEFINITIONS_RELEASE"), 0);
  1386. this->AppendDefines
  1387. (flagsMinSize,
  1388. target.GetProperty("COMPILE_DEFINITIONS_MINSIZEREL"), 0);
  1389. this->AppendDefines
  1390. (flagsDebugRel,
  1391. target.GetProperty("COMPILE_DEFINITIONS_RELWITHDEBINFO"), 0);
  1392. // The template files have CXX FLAGS in them, that need to be replaced.
  1393. // There are not separate CXX and C template files, so we use the same
  1394. // variable names. The previous code sets up flags* variables to contain
  1395. // the correct C or CXX flags
  1396. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL",
  1397. flagsMinSize.c_str());
  1398. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG",
  1399. flagsDebug.c_str());
  1400. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELWITHDEBINFO",
  1401. flagsDebugRel.c_str());
  1402. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE",
  1403. flagsRelease.c_str());
  1404. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  1405. fout << line.c_str() << std::endl;
  1406. }
  1407. }
  1408. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  1409. {
  1410. std::ifstream fin(this->DSPFooterTemplate.c_str());
  1411. if(!fin)
  1412. {
  1413. cmSystemTools::Error("Error Reading ",
  1414. this->DSPFooterTemplate.c_str());
  1415. }
  1416. std::string line;
  1417. while(cmSystemTools::GetLineFromStream(fin, line))
  1418. {
  1419. fout << line << std::endl;
  1420. }
  1421. }
  1422. //----------------------------------------------------------------------------
  1423. void cmLocalVisualStudio6Generator
  1424. ::ComputeLinkOptions(cmTarget& target,
  1425. const char* configName,
  1426. const std::string extraOptions,
  1427. std::string& options)
  1428. {
  1429. // Compute the link information for this configuration.
  1430. std::vector<cmStdString> linkLibs;
  1431. std::vector<cmStdString> linkDirs;
  1432. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  1433. // Build the link options code.
  1434. for(std::vector<cmStdString>::const_iterator d = linkDirs.begin();
  1435. d != linkDirs.end(); ++d)
  1436. {
  1437. std::string dir = *d;
  1438. if(!dir.empty())
  1439. {
  1440. if(dir[dir.size()-1] != '/')
  1441. {
  1442. dir += "/";
  1443. }
  1444. dir += "$(IntDir)";
  1445. options += "# ADD LINK32 /LIBPATH:";
  1446. options += this->ConvertToOptionallyRelativeOutputPath(dir.c_str());
  1447. options += " /LIBPATH:";
  1448. options += this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  1449. options += "\n";
  1450. }
  1451. }
  1452. for(std::vector<cmStdString>::const_iterator l = linkLibs.begin();
  1453. l != linkLibs.end(); ++l)
  1454. {
  1455. options += "# ADD LINK32 ";
  1456. options += this->ConvertToOptionallyRelativeOutputPath(l->c_str());
  1457. options += "\n";
  1458. }
  1459. // Add extra options if any.
  1460. if(!extraOptions.empty())
  1461. {
  1462. options += "# ADD LINK32 ";
  1463. options += extraOptions;
  1464. options += "\n";
  1465. }
  1466. }
  1467. std::string
  1468. cmLocalVisualStudio6Generator
  1469. ::GetTargetDirectory(cmTarget const&) const
  1470. {
  1471. // No per-target directory for this generator (yet).
  1472. return "";
  1473. }
  1474. void cmLocalVisualStudio6Generator
  1475. ::GetTargetObjectFileDirectories(cmTarget* ,
  1476. std::vector<std::string>&
  1477. dirs)
  1478. {
  1479. std::string dir = this->Makefile->GetCurrentOutputDirectory();
  1480. dir += "/";
  1481. dir += this->GetGlobalGenerator()->GetCMakeCFGInitDirectory();
  1482. dirs.push_back(dir);
  1483. }
  1484. std::string
  1485. cmLocalVisualStudio6Generator
  1486. ::GetConfigName(std::string const& configuration) const
  1487. {
  1488. // Strip the subdirectory name out of the configuration name.
  1489. std::string config = configuration;
  1490. std::string::size_type pos = config.find_last_of(" ");
  1491. config = config.substr(pos+1, std::string::npos);
  1492. config = config.substr(0, config.size()-1);
  1493. return config;
  1494. }
  1495. //----------------------------------------------------------------------------
  1496. bool
  1497. cmLocalVisualStudio6Generator
  1498. ::CheckDefinition(std::string const& define) const
  1499. {
  1500. // Perform the standard check first.
  1501. if(!this->cmLocalGenerator::CheckDefinition(define))
  1502. {
  1503. return false;
  1504. }
  1505. // Now do the VS6-specific check.
  1506. if(define.find_first_of("=") != define.npos)
  1507. {
  1508. cmOStringStream e;
  1509. e << "WARNING: The VS6 IDE does not support preprocessor definitions "
  1510. << "with values.\n"
  1511. << "CMake is dropping a preprocessor definition: " << define << "\n"
  1512. << "Consider defining the macro in a (configured) header file.\n";
  1513. cmSystemTools::Message(e.str().c_str());
  1514. return false;
  1515. }
  1516. // Assume it is supported.
  1517. return true;
  1518. }