cmLocalVisualStudio6Generator.cxx 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  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. // Check for extra object-file dependencies.
  391. const char* dependsValue = (*sf)->GetProperty("OBJECT_DEPENDS");
  392. if(dependsValue)
  393. {
  394. cmSystemTools::ExpandListArgument(dependsValue, depends);
  395. }
  396. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  397. target.GetType() == cmTarget::GLOBAL_TARGET)
  398. {
  399. fout << "# Begin Source File\n\n";
  400. // Tell MS-Dev what the source is. If the compiler knows how to
  401. // build it, then it will.
  402. fout << "SOURCE=" <<
  403. this->ConvertToOptionallyRelativeOutputPath(source.c_str()) << "\n\n";
  404. if(!depends.empty())
  405. {
  406. // Write out the dependencies for the rule.
  407. fout << "USERDEP__HACK=";
  408. for(std::vector<std::string>::const_iterator d = depends.begin();
  409. d != depends.end(); ++d)
  410. {
  411. fout << "\\\n\t" <<
  412. this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  413. }
  414. fout << "\n";
  415. }
  416. if (command)
  417. {
  418. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  419. this->WriteCustomRule(fout, source.c_str(), *command, flags);
  420. }
  421. else if(!compileFlags.empty() || !objectNameDir.empty())
  422. {
  423. for(std::vector<std::string>::iterator i
  424. = this->Configurations.begin();
  425. i != this->Configurations.end(); ++i)
  426. {
  427. if (i == this->Configurations.begin())
  428. {
  429. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  430. }
  431. else
  432. {
  433. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  434. }
  435. if(!compileFlags.empty())
  436. {
  437. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  438. }
  439. if(!objectNameDir.empty())
  440. {
  441. // Strip the subdirectory name out of the configuration name.
  442. std::string config = this->GetConfigName(*i);
  443. // Setup an alternate object file directory.
  444. fout << "\n# PROP Intermediate_Dir \""
  445. << config << "/" << objectNameDir << "\"\n\n";
  446. }
  447. }
  448. fout << "!ENDIF\n\n";
  449. }
  450. fout << "# End Source File\n";
  451. }
  452. }
  453. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  454. for(unsigned int i=0;i<children.size();++i)
  455. {
  456. this->WriteGroup(&children[i], target, fout, libName);
  457. }
  458. // If the group has a name, write the footer.
  459. if(name != "")
  460. {
  461. this->WriteDSPEndGroup(fout);
  462. }
  463. }
  464. void
  465. cmLocalVisualStudio6Generator
  466. ::AddUtilityCommandHack(cmTarget& target, int count,
  467. std::vector<std::string>& depends,
  468. const cmCustomCommand& origCommand)
  469. {
  470. // Create a fake output that forces the rule to run.
  471. char* output = new char[(strlen(this->Makefile->GetStartOutputDirectory()) +
  472. strlen(target.GetName()) + 30)];
  473. sprintf(output,"%s/%s_force_%i", this->Makefile->GetStartOutputDirectory(),
  474. target.GetName(), count);
  475. std::string comment = this->ConstructComment(origCommand, "<hack>");
  476. // Add the rule with the given dependencies and commands.
  477. const char* no_main_dependency = 0;
  478. this->Makefile->AddCustomCommandToOutput(output,
  479. depends,
  480. no_main_dependency,
  481. origCommand.GetCommandLines(),
  482. comment.c_str(),
  483. origCommand.GetWorkingDirectory());
  484. // Replace the dependencies with the output of this rule so that the
  485. // next rule added will run after this one.
  486. depends.clear();
  487. depends.push_back(output);
  488. // Add a source file representing this output to the project.
  489. cmSourceFile* outsf = this->Makefile->GetSourceFileWithOutput(output);
  490. target.AddSourceFile(outsf);
  491. // Free the fake output name.
  492. delete [] output;
  493. }
  494. void
  495. cmLocalVisualStudio6Generator
  496. ::WriteCustomRule(std::ostream& fout,
  497. const char* source,
  498. const cmCustomCommand& command,
  499. const char* flags)
  500. {
  501. std::string comment =
  502. this->ConstructComment(command, "Building Custom Rule $(InputPath)");
  503. if(comment == "<hack>")
  504. {
  505. comment = "";
  506. }
  507. // Write the rule for each configuration.
  508. std::vector<std::string>::iterator i;
  509. for(i = this->Configurations.begin(); i != this->Configurations.end(); ++i)
  510. {
  511. std::string config = this->GetConfigName(*i);
  512. std::string script =
  513. this->ConstructScript(command.GetCommandLines(),
  514. command.GetWorkingDirectory(),
  515. config.c_str(),
  516. command.GetEscapeOldStyle(),
  517. command.GetEscapeAllowMakeVars(),
  518. "\\\n\t");
  519. if (i == this->Configurations.begin())
  520. {
  521. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  522. }
  523. else
  524. {
  525. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  526. }
  527. if(flags)
  528. {
  529. fout << "\n# ADD CPP " << flags << "\n\n";
  530. }
  531. // Write out the dependencies for the rule.
  532. fout << "USERDEP__HACK=";
  533. for(std::vector<std::string>::const_iterator d =
  534. command.GetDepends().begin();
  535. d != command.GetDepends().end();
  536. ++d)
  537. {
  538. // Lookup the real name of the dependency in case it is a CMake target.
  539. std::string dep = this->GetRealDependency(d->c_str(),
  540. config.c_str());
  541. fout << "\\\n\t" <<
  542. this->ConvertToOptionallyRelativeOutputPath(dep.c_str());
  543. }
  544. fout << "\n";
  545. fout << "# PROP Ignore_Default_Tool 1\n";
  546. fout << "# Begin Custom Build -";
  547. if(!comment.empty())
  548. {
  549. fout << " " << comment.c_str();
  550. }
  551. fout << "\n\n";
  552. if(command.GetOutputs().empty())
  553. {
  554. fout << source
  555. << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  556. fout << script.c_str() << "\n\n";
  557. }
  558. else
  559. {
  560. for(std::vector<std::string>::const_iterator o =
  561. command.GetOutputs().begin();
  562. o != command.GetOutputs().end();
  563. ++o)
  564. {
  565. // Write a rule for every output generated by this command.
  566. fout << this->ConvertToOptionallyRelativeOutputPath(o->c_str())
  567. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  568. fout << script.c_str() << "\n\n";
  569. }
  570. }
  571. fout << "# End Custom Build\n\n";
  572. }
  573. fout << "!ENDIF\n\n";
  574. }
  575. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  576. const char* group,
  577. const char* filter)
  578. {
  579. fout << "# Begin Group \"" << group << "\"\n"
  580. "# PROP Default_Filter \"" << filter << "\"\n";
  581. }
  582. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  583. {
  584. fout << "# End Group\n";
  585. }
  586. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b,
  587. const char* libName,
  588. cmTarget& target)
  589. {
  590. std::string root= this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  591. const char *def=
  592. this->Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  593. if( def)
  594. {
  595. root = def;
  596. }
  597. else
  598. {
  599. root += "/Templates";
  600. }
  601. switch(b)
  602. {
  603. case STATIC_LIBRARY:
  604. this->DSPHeaderTemplate = root;
  605. this->DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  606. this->DSPFooterTemplate = root;
  607. this->DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  608. break;
  609. case DLL:
  610. this->DSPHeaderTemplate = root;
  611. this->DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  612. this->DSPFooterTemplate = root;
  613. this->DSPFooterTemplate += "/DLLFooter.dsptemplate";
  614. break;
  615. case EXECUTABLE:
  616. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  617. {
  618. this->DSPHeaderTemplate = root;
  619. this->DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  620. this->DSPFooterTemplate = root;
  621. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  622. }
  623. else
  624. {
  625. this->DSPHeaderTemplate = root;
  626. this->DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  627. this->DSPFooterTemplate = root;
  628. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  629. }
  630. break;
  631. case UTILITY:
  632. this->DSPHeaderTemplate = root;
  633. this->DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  634. this->DSPFooterTemplate = root;
  635. this->DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  636. break;
  637. }
  638. // once the build type is set, determine what configurations are
  639. // possible
  640. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  641. cmsys::RegularExpression reg("# Name ");
  642. if(!fin)
  643. {
  644. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  645. }
  646. // reset this->Configurations
  647. this->Configurations.erase(this->Configurations.begin(),
  648. this->Configurations.end());
  649. // now add all the configurations possible
  650. std::string line;
  651. while(cmSystemTools::GetLineFromStream(fin, line))
  652. {
  653. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  654. if (reg.find(line))
  655. {
  656. this->Configurations.push_back(line.substr(reg.end()));
  657. }
  658. }
  659. }
  660. // look for custom rules on a target and collect them together
  661. std::string
  662. cmLocalVisualStudio6Generator::CreateTargetRules(cmTarget &target,
  663. const char* configName,
  664. const char * /* libName */)
  665. {
  666. std::string customRuleCode = "";
  667. if (target.GetType() >= cmTarget::UTILITY )
  668. {
  669. return customRuleCode;
  670. }
  671. // are there any rules?
  672. if (target.GetPreBuildCommands().size() +
  673. target.GetPreLinkCommands().size() +
  674. target.GetPostBuildCommands().size() == 0)
  675. {
  676. return customRuleCode;
  677. }
  678. customRuleCode = "# Begin Special Build Tool\n";
  679. // Write the pre-build and pre-link together (VS6 does not support
  680. // both). Make sure no continuation character is put on the last
  681. // line.
  682. int prelink_total = (static_cast<int>(target.GetPreBuildCommands().size())+
  683. static_cast<int>(target.GetPreLinkCommands().size()));
  684. int prelink_count = 0;
  685. if(prelink_total > 0)
  686. {
  687. // header stuff
  688. customRuleCode += "PreLink_Cmds=";
  689. }
  690. for (std::vector<cmCustomCommand>::const_iterator cr =
  691. target.GetPreBuildCommands().begin();
  692. cr != target.GetPreBuildCommands().end(); ++cr)
  693. {
  694. if(prelink_count++ > 0)
  695. {
  696. customRuleCode += "\\\n\t";
  697. }
  698. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  699. cr->GetWorkingDirectory(),
  700. configName,
  701. cr->GetEscapeOldStyle(),
  702. cr->GetEscapeAllowMakeVars(),
  703. "\\\n\t");
  704. }
  705. for (std::vector<cmCustomCommand>::const_iterator cr =
  706. target.GetPreLinkCommands().begin();
  707. cr != target.GetPreLinkCommands().end(); ++cr)
  708. {
  709. if(prelink_count++ > 0)
  710. {
  711. customRuleCode += "\\\n\t";
  712. }
  713. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  714. cr->GetWorkingDirectory(),
  715. configName,
  716. cr->GetEscapeOldStyle(),
  717. cr->GetEscapeAllowMakeVars(),
  718. "\\\n\t");
  719. }
  720. if(prelink_total > 0)
  721. {
  722. customRuleCode += "\n";
  723. }
  724. // Write the post-build rules. Make sure no continuation character
  725. // is put on the last line.
  726. int postbuild_total =
  727. static_cast<int>(target.GetPostBuildCommands().size());
  728. int postbuild_count = 0;
  729. if(postbuild_total > 0)
  730. {
  731. customRuleCode += "PostBuild_Cmds=";
  732. }
  733. for (std::vector<cmCustomCommand>::const_iterator cr =
  734. target.GetPostBuildCommands().begin();
  735. cr != target.GetPostBuildCommands().end(); ++cr)
  736. {
  737. if(postbuild_count++ > 0)
  738. {
  739. customRuleCode += "\\\n\t";
  740. }
  741. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  742. cr->GetWorkingDirectory(),
  743. configName,
  744. cr->GetEscapeOldStyle(),
  745. cr->GetEscapeAllowMakeVars(),
  746. "\\\n\t");
  747. }
  748. if(postbuild_total > 0)
  749. {
  750. customRuleCode += "\n";
  751. }
  752. customRuleCode += "# End Special Build Tool\n";
  753. return customRuleCode;
  754. }
  755. inline std::string removeQuotes(const std::string& s)
  756. {
  757. if(s[0] == '\"' && s[s.size()-1] == '\"')
  758. {
  759. return s.substr(1, s.size()-2);
  760. }
  761. return s;
  762. }
  763. // Code in blocks surrounded by a test for this definition is needed
  764. // only for compatibility with user project's replacement DSP
  765. // templates. The CMake templates no longer use them.
  766. #define CM_USE_OLD_VS6
  767. void cmLocalVisualStudio6Generator
  768. ::WriteDSPHeader(std::ostream& fout,
  769. const char *libName, cmTarget &target,
  770. std::vector<cmSourceGroup> &)
  771. {
  772. // Lookup the output directory for the target.
  773. std::string outPath = target.GetDirectory();
  774. #ifdef CM_USE_OLD_VS6
  775. // Lookup the library and executable output directories.
  776. std::string libPath;
  777. if(this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  778. {
  779. libPath = this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  780. }
  781. std::string exePath;
  782. if(this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  783. {
  784. exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  785. }
  786. // Make sure there are trailing slashes.
  787. if(!libPath.empty())
  788. {
  789. if(libPath[libPath.size()-1] != '/')
  790. {
  791. libPath += "/";
  792. }
  793. }
  794. if(!exePath.empty())
  795. {
  796. if(exePath[exePath.size()-1] != '/')
  797. {
  798. exePath += "/";
  799. }
  800. }
  801. std::set<std::string> pathEmitted;
  802. // determine the link directories
  803. std::string libOptions;
  804. std::string libDebugOptions;
  805. std::string libOptimizedOptions;
  806. std::string libMultiLineOptions;
  807. std::string libMultiLineOptionsForDebug;
  808. std::string libMultiLineDebugOptions;
  809. std::string libMultiLineOptimizedOptions;
  810. if(libPath.size())
  811. {
  812. std::string lpath =
  813. this->ConvertToOptionallyRelativeOutputPath(libPath.c_str());
  814. if(lpath.size() == 0)
  815. {
  816. lpath = ".";
  817. }
  818. std::string lpathIntDir = libPath + "$(INTDIR)";
  819. lpathIntDir =
  820. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  821. if(pathEmitted.insert(lpath).second)
  822. {
  823. libOptions += " /LIBPATH:";
  824. libOptions += lpathIntDir;
  825. libOptions += " ";
  826. libOptions += " /LIBPATH:";
  827. libOptions += lpath;
  828. libOptions += " ";
  829. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  830. libMultiLineOptions += lpathIntDir;
  831. libMultiLineOptions += " ";
  832. libMultiLineOptions += " /LIBPATH:";
  833. libMultiLineOptions += lpath;
  834. libMultiLineOptions += " \n";
  835. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  836. libMultiLineOptionsForDebug += lpathIntDir;
  837. libMultiLineOptionsForDebug += " ";
  838. libMultiLineOptionsForDebug += " /LIBPATH:";
  839. libMultiLineOptionsForDebug += lpath;
  840. libMultiLineOptionsForDebug += " \n";
  841. }
  842. }
  843. if(exePath.size())
  844. {
  845. std::string lpath =
  846. this->ConvertToOptionallyRelativeOutputPath(exePath.c_str());
  847. if(lpath.size() == 0)
  848. {
  849. lpath = ".";
  850. }
  851. std::string lpathIntDir = exePath + "$(INTDIR)";
  852. lpathIntDir =
  853. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  854. if(pathEmitted.insert(lpath).second)
  855. {
  856. libOptions += " /LIBPATH:";
  857. libOptions += lpathIntDir;
  858. libOptions += " ";
  859. libOptions += " /LIBPATH:";
  860. libOptions += lpath;
  861. libOptions += " ";
  862. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  863. libMultiLineOptions += lpathIntDir;
  864. libMultiLineOptions += " ";
  865. libMultiLineOptions += " /LIBPATH:";
  866. libMultiLineOptions += lpath;
  867. libMultiLineOptions += " \n";
  868. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  869. libMultiLineOptionsForDebug += lpathIntDir;
  870. libMultiLineOptionsForDebug += " ";
  871. libMultiLineOptionsForDebug += " /LIBPATH:";
  872. libMultiLineOptionsForDebug += lpath;
  873. libMultiLineOptionsForDebug += " \n";
  874. }
  875. }
  876. std::vector<std::string>::const_iterator i;
  877. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  878. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  879. {
  880. std::string path = *i;
  881. if(path[path.size()-1] != '/')
  882. {
  883. path += "/";
  884. }
  885. std::string lpath =
  886. this->ConvertToOptionallyRelativeOutputPath(path.c_str());
  887. if(lpath.size() == 0)
  888. {
  889. lpath = ".";
  890. }
  891. std::string lpathIntDir = path + "$(INTDIR)";
  892. lpathIntDir =
  893. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  894. if(pathEmitted.insert(lpath).second)
  895. {
  896. libOptions += " /LIBPATH:";
  897. libOptions += lpathIntDir;
  898. libOptions += " ";
  899. libOptions += " /LIBPATH:";
  900. libOptions += lpath;
  901. libOptions += " ";
  902. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  903. libMultiLineOptions += lpathIntDir;
  904. libMultiLineOptions += " ";
  905. libMultiLineOptions += " /LIBPATH:";
  906. libMultiLineOptions += lpath;
  907. libMultiLineOptions += " \n";
  908. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  909. libMultiLineOptionsForDebug += lpathIntDir;
  910. libMultiLineOptionsForDebug += " ";
  911. libMultiLineOptionsForDebug += " /LIBPATH:";
  912. libMultiLineOptionsForDebug += lpath;
  913. libMultiLineOptionsForDebug += " \n";
  914. }
  915. }
  916. // find link libraries
  917. const cmTarget::LinkLibraryVectorType& libs = target.GetLinkLibraries();
  918. cmTarget::LinkLibraryVectorType::const_iterator j;
  919. for(j = libs.begin(); j != libs.end(); ++j)
  920. {
  921. // add libraries to executables and dlls (but never include
  922. // a library in a library, bad recursion)
  923. // NEVER LINK STATIC LIBRARIES TO OTHER STATIC LIBRARIES
  924. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  925. && target.GetType() != cmTarget::STATIC_LIBRARY
  926. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  927. (target.GetType()==cmTarget::SHARED_LIBRARY && libName != j->first) ||
  928. (target.GetType()==cmTarget::MODULE_LIBRARY && libName != j->first))
  929. {
  930. // Compute the proper name to use to link this library.
  931. std::string lib;
  932. std::string libDebug;
  933. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, j->first.c_str(),
  934. false);
  935. if(tgt)
  936. {
  937. lib = cmSystemTools::GetFilenameWithoutExtension
  938. (tgt->GetFullName().c_str());
  939. libDebug = cmSystemTools::GetFilenameWithoutExtension
  940. (tgt->GetFullName("Debug").c_str());
  941. lib += ".lib";
  942. libDebug += ".lib";
  943. }
  944. else
  945. {
  946. lib = j->first.c_str();
  947. libDebug = j->first.c_str();
  948. if(j->first.find(".lib") == std::string::npos)
  949. {
  950. lib += ".lib";
  951. libDebug += ".lib";
  952. }
  953. }
  954. lib = this->ConvertToOptionallyRelativeOutputPath(lib.c_str());
  955. libDebug =
  956. this->ConvertToOptionallyRelativeOutputPath(libDebug.c_str());
  957. if (j->second == cmTarget::GENERAL)
  958. {
  959. libOptions += " ";
  960. libOptions += lib;
  961. libMultiLineOptions += "# ADD LINK32 ";
  962. libMultiLineOptions += lib;
  963. libMultiLineOptions += "\n";
  964. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  965. libMultiLineOptionsForDebug += libDebug;
  966. libMultiLineOptionsForDebug += "\n";
  967. }
  968. if (j->second == cmTarget::DEBUG)
  969. {
  970. libDebugOptions += " ";
  971. libDebugOptions += lib;
  972. libMultiLineDebugOptions += "# ADD LINK32 ";
  973. libMultiLineDebugOptions += libDebug;
  974. libMultiLineDebugOptions += "\n";
  975. }
  976. if (j->second == cmTarget::OPTIMIZED)
  977. {
  978. libOptimizedOptions += " ";
  979. libOptimizedOptions += lib;
  980. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  981. libMultiLineOptimizedOptions += lib;
  982. libMultiLineOptimizedOptions += "\n";
  983. }
  984. }
  985. }
  986. #endif
  987. // Get extra linker options for this target type.
  988. std::string extraLinkOptions;
  989. if(target.GetType() == cmTarget::EXECUTABLE)
  990. {
  991. extraLinkOptions =
  992. this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  993. }
  994. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  995. {
  996. extraLinkOptions =
  997. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  998. }
  999. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1000. {
  1001. extraLinkOptions =
  1002. this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1003. }
  1004. // Get extra linker options for this target.
  1005. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  1006. {
  1007. extraLinkOptions += " ";
  1008. extraLinkOptions += targetLinkFlags;
  1009. }
  1010. // Get standard libraries for this language.
  1011. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1012. target.GetType() <= cmTarget::MODULE_LIBRARY)
  1013. {
  1014. // Get the language to use for linking.
  1015. const char* linkLanguage =
  1016. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1017. if(!linkLanguage)
  1018. {
  1019. cmSystemTools::Error
  1020. ("CMake can not determine linker language for target:",
  1021. target.GetName());
  1022. return;
  1023. }
  1024. // Compute the variable name to lookup standard libraries for this
  1025. // language.
  1026. std::string standardLibsVar = "CMAKE_";
  1027. standardLibsVar += linkLanguage;
  1028. standardLibsVar += "_STANDARD_LIBRARIES";
  1029. // Add standard libraries.
  1030. if(const char* stdLibs =
  1031. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1032. {
  1033. extraLinkOptions += " ";
  1034. extraLinkOptions += stdLibs;
  1035. }
  1036. }
  1037. // Compute version number information.
  1038. std::string targetVersionFlag;
  1039. if(target.GetType() == cmTarget::EXECUTABLE ||
  1040. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1041. target.GetType() == cmTarget::MODULE_LIBRARY)
  1042. {
  1043. int major;
  1044. int minor;
  1045. target.GetTargetVersion(major, minor);
  1046. cmOStringStream targetVersionStream;
  1047. targetVersionStream << "/version:" << major << "." << minor;
  1048. targetVersionFlag = targetVersionStream.str();
  1049. }
  1050. // Compute the real name of the target.
  1051. std::string outputName =
  1052. "(OUTPUT_NAME is for libraries and executables only)";
  1053. std::string outputNameDebug = outputName;
  1054. std::string outputNameRelease = outputName;
  1055. std::string outputNameMinSizeRel = outputName;
  1056. std::string outputNameRelWithDebInfo = outputName;
  1057. if(target.GetType() == cmTarget::EXECUTABLE ||
  1058. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1059. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1060. target.GetType() == cmTarget::MODULE_LIBRARY)
  1061. {
  1062. outputName = target.GetFullName();
  1063. outputNameDebug = target.GetFullName("Debug");
  1064. outputNameRelease = target.GetFullName("Release");
  1065. outputNameMinSizeRel = target.GetFullName("MinSizeRel");
  1066. outputNameRelWithDebInfo = target.GetFullName("RelWithDebInfo");
  1067. }
  1068. // Compute the proper link information for the target.
  1069. std::string optionsDebug;
  1070. std::string optionsRelease;
  1071. std::string optionsMinSizeRel;
  1072. std::string optionsRelWithDebInfo;
  1073. if(target.GetType() == cmTarget::EXECUTABLE ||
  1074. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1075. target.GetType() == cmTarget::MODULE_LIBRARY)
  1076. {
  1077. this->ComputeLinkOptions(target, "Debug", extraLinkOptions,
  1078. optionsDebug);
  1079. this->ComputeLinkOptions(target, "Release", extraLinkOptions,
  1080. optionsRelease);
  1081. this->ComputeLinkOptions(target, "MinSizeRel", extraLinkOptions,
  1082. optionsMinSizeRel);
  1083. this->ComputeLinkOptions(target, "RelWithDebInfo", extraLinkOptions,
  1084. optionsRelWithDebInfo);
  1085. }
  1086. // Compute the path of the import library.
  1087. std::string targetImplibFlagDebug;
  1088. std::string targetImplibFlagRelease;
  1089. std::string targetImplibFlagMinSizeRel;
  1090. std::string targetImplibFlagRelWithDebInfo;
  1091. if(target.GetType() == cmTarget::SHARED_LIBRARY ||
  1092. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1093. target.GetType() == cmTarget::EXECUTABLE)
  1094. {
  1095. std::string fullPathImpDebug = target.GetDirectory("Debug", true);
  1096. std::string fullPathImpRelease = target.GetDirectory("Release", true);
  1097. std::string fullPathImpMinSizeRel =
  1098. target.GetDirectory("MinSizeRel", true);
  1099. std::string fullPathImpRelWithDebInfo =
  1100. target.GetDirectory("RelWithDebInfo", true);
  1101. fullPathImpDebug += "/";
  1102. fullPathImpRelease += "/";
  1103. fullPathImpMinSizeRel += "/";
  1104. fullPathImpRelWithDebInfo += "/";
  1105. fullPathImpDebug += target.GetFullName("Debug", true);
  1106. fullPathImpRelease += target.GetFullName("Release", true);
  1107. fullPathImpMinSizeRel += target.GetFullName("MinSizeRel", true);
  1108. fullPathImpRelWithDebInfo += target.GetFullName("RelWithDebInfo", true);
  1109. targetImplibFlagDebug = "/implib:";
  1110. targetImplibFlagRelease = "/implib:";
  1111. targetImplibFlagMinSizeRel = "/implib:";
  1112. targetImplibFlagRelWithDebInfo = "/implib:";
  1113. targetImplibFlagDebug +=
  1114. this->ConvertToOptionallyRelativeOutputPath(fullPathImpDebug.c_str());
  1115. targetImplibFlagRelease +=
  1116. this->ConvertToOptionallyRelativeOutputPath(fullPathImpRelease.c_str());
  1117. targetImplibFlagMinSizeRel +=
  1118. this->ConvertToOptionallyRelativeOutputPath(
  1119. fullPathImpMinSizeRel.c_str());
  1120. targetImplibFlagRelWithDebInfo +=
  1121. this->ConvertToOptionallyRelativeOutputPath(
  1122. fullPathImpRelWithDebInfo.c_str());
  1123. }
  1124. #ifdef CM_USE_OLD_VS6
  1125. // Compute link information for the target.
  1126. if(extraLinkOptions.size())
  1127. {
  1128. libOptions += " ";
  1129. libOptions += extraLinkOptions;
  1130. libOptions += " ";
  1131. libMultiLineOptions += "# ADD LINK32 ";
  1132. libMultiLineOptions += extraLinkOptions;
  1133. libMultiLineOptions += " \n";
  1134. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1135. libMultiLineOptionsForDebug += extraLinkOptions;
  1136. libMultiLineOptionsForDebug += " \n";
  1137. }
  1138. #endif
  1139. // are there any custom rules on the target itself
  1140. // only if the target is a lib or exe
  1141. std::string customRuleCodeRelease
  1142. = this->CreateTargetRules(target, "RELEASE", libName);
  1143. std::string customRuleCodeDebug
  1144. = this->CreateTargetRules(target, "DEBUG", libName);
  1145. std::string customRuleCodeMinSizeRel
  1146. = this->CreateTargetRules(target, "MINSIZEREL", libName);
  1147. std::string customRuleCodeRelWithDebInfo
  1148. = this->CreateTargetRules(target, "RELWITHDEBINFO", libName);
  1149. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  1150. if(!fin)
  1151. {
  1152. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  1153. }
  1154. std::string staticLibOptions;
  1155. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1156. {
  1157. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  1158. {
  1159. staticLibOptions = libflags;
  1160. }
  1161. }
  1162. // Add the export symbol definition for shared library objects.
  1163. std::string exportSymbol;
  1164. if(const char* exportMacro = target.GetExportMacro())
  1165. {
  1166. exportSymbol = exportMacro;
  1167. }
  1168. std::string line;
  1169. while(cmSystemTools::GetLineFromStream(fin, line))
  1170. {
  1171. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  1172. if(!mfcFlag)
  1173. {
  1174. mfcFlag = "0";
  1175. }
  1176. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  1177. exportSymbol.c_str());
  1178. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  1179. mfcFlag);
  1180. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1181. {
  1182. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS",
  1183. staticLibOptions.c_str());
  1184. }
  1185. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  1186. {
  1187. cmSystemTools::ReplaceString(line, "/nologo", "");
  1188. }
  1189. #ifdef CM_USE_OLD_VS6
  1190. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  1191. libOptions.c_str());
  1192. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  1193. libDebugOptions.c_str());
  1194. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  1195. libOptimizedOptions.c_str());
  1196. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES_FOR_DEBUG",
  1197. libMultiLineOptionsForDebug.c_str());
  1198. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  1199. libMultiLineOptions.c_str());
  1200. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  1201. libMultiLineDebugOptions.c_str());
  1202. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  1203. libMultiLineOptimizedOptions.c_str());
  1204. #endif
  1205. // Substitute the rules for custom command. When specifying just the
  1206. // target name for the command the command can be different for
  1207. // different configs
  1208. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_RELEASE",
  1209. customRuleCodeRelease.c_str());
  1210. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_DEBUG",
  1211. customRuleCodeDebug.c_str());
  1212. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_MINSIZEREL",
  1213. customRuleCodeMinSizeRel.c_str());
  1214. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_RELWITHDEBINFO",
  1215. customRuleCodeRelWithDebInfo.c_str());
  1216. // Substitute the real output name into the template.
  1217. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_DEBUG",
  1218. outputNameDebug.c_str());
  1219. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELEASE",
  1220. outputNameRelease.c_str());
  1221. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_MINSIZEREL",
  1222. outputNameMinSizeRel.c_str());
  1223. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELWITHDEBINFO",
  1224. outputNameRelWithDebInfo.c_str());
  1225. cmSystemTools::ReplaceString(line, "OUTPUT_NAME", outputName.c_str());
  1226. // Substitute the proper link information into the template.
  1227. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_DEBUG",
  1228. optionsDebug.c_str());
  1229. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELEASE",
  1230. optionsRelease.c_str());
  1231. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_MINSIZEREL",
  1232. optionsMinSizeRel.c_str());
  1233. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELWITHDEBINFO",
  1234. optionsRelWithDebInfo.c_str());
  1235. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  1236. this->IncludeOptions.c_str());
  1237. cmSystemTools::ReplaceString(line, "TARGET_VERSION_FLAG",
  1238. targetVersionFlag.c_str());
  1239. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_DEBUG",
  1240. targetImplibFlagDebug.c_str());
  1241. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELEASE",
  1242. targetImplibFlagRelease.c_str());
  1243. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_MINSIZEREL",
  1244. targetImplibFlagMinSizeRel.c_str());
  1245. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELWITHDEBINFO",
  1246. targetImplibFlagRelWithDebInfo.c_str());
  1247. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  1248. #ifdef CM_USE_OLD_VS6
  1249. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  1250. // are already quoted in the template file,
  1251. // we need to remove the quotes here, we still need
  1252. // to convert to output path for unix to win32 conversion
  1253. cmSystemTools::ReplaceString
  1254. (line, "LIBRARY_OUTPUT_PATH",
  1255. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1256. (libPath.c_str())).c_str());
  1257. cmSystemTools::ReplaceString
  1258. (line, "EXECUTABLE_OUTPUT_PATH",
  1259. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1260. (exePath.c_str())).c_str());
  1261. #endif
  1262. cmSystemTools::ReplaceString
  1263. (line, "OUTPUT_DIRECTORY",
  1264. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1265. (outPath.c_str())).c_str());
  1266. cmSystemTools::ReplaceString(line,
  1267. "EXTRA_DEFINES",
  1268. this->Makefile->GetDefineFlags());
  1269. const char* debugPostfix
  1270. = this->Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  1271. cmSystemTools::ReplaceString(line, "DEBUG_POSTFIX",
  1272. debugPostfix?debugPostfix:"");
  1273. // store flags for each configuration
  1274. std::string flags = " ";
  1275. std::string flagsRelease = " ";
  1276. std::string flagsMinSize = " ";
  1277. std::string flagsDebug = " ";
  1278. std::string flagsDebugRel = " ";
  1279. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1280. target.GetType() <= cmTarget::MODULE_LIBRARY)
  1281. {
  1282. const char* linkLanguage =
  1283. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1284. if(!linkLanguage)
  1285. {
  1286. cmSystemTools::Error
  1287. ("CMake can not determine linker language for target:",
  1288. target.GetName());
  1289. return;
  1290. }
  1291. // if CXX is on and the target contains cxx code then add the cxx flags
  1292. std::string baseFlagVar = "CMAKE_";
  1293. baseFlagVar += linkLanguage;
  1294. baseFlagVar += "_FLAGS";
  1295. flags = this->Makefile->GetSafeDefinition(baseFlagVar.c_str());
  1296. std::string flagVar = baseFlagVar + "_RELEASE";
  1297. flagsRelease = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1298. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  1299. if(const char* targetLinkFlags =
  1300. target.GetProperty("LINK_FLAGS_RELEASE"))
  1301. {
  1302. flagsRelease += targetLinkFlags;
  1303. flagsRelease += " ";
  1304. }
  1305. flagVar = baseFlagVar + "_MINSIZEREL";
  1306. flagsMinSize = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1307. flagsMinSize += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  1308. if(const char* targetLinkFlags =
  1309. target.GetProperty("LINK_FLAGS_MINSIZEREL"))
  1310. {
  1311. flagsMinSize += targetLinkFlags;
  1312. flagsMinSize += " ";
  1313. }
  1314. flagVar = baseFlagVar + "_DEBUG";
  1315. flagsDebug = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1316. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  1317. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_DEBUG"))
  1318. {
  1319. flagsDebug += targetLinkFlags;
  1320. flagsDebug += " ";
  1321. }
  1322. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  1323. flagsDebugRel = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1324. flagsDebugRel += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  1325. if(const char* targetLinkFlags =
  1326. target.GetProperty("LINK_FLAGS_RELWITHDEBINFO"))
  1327. {
  1328. flagsDebugRel += targetLinkFlags;
  1329. flagsDebugRel += " ";
  1330. }
  1331. }
  1332. // if unicode is not found, then add -D_MBCS
  1333. std::string defs = this->Makefile->GetDefineFlags();
  1334. if(flags.find("D_UNICODE") == flags.npos &&
  1335. defs.find("D_UNICODE") == flags.npos)
  1336. {
  1337. flags += " /D \"_MBCS\"";
  1338. }
  1339. // Add per-target flags.
  1340. if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
  1341. {
  1342. flags += " ";
  1343. flags += targetFlags;
  1344. }
  1345. // The template files have CXX FLAGS in them, that need to be replaced.
  1346. // There are not separate CXX and C template files, so we use the same
  1347. // variable names. The previous code sets up flags* variables to contain
  1348. // the correct C or CXX flags
  1349. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL",
  1350. flagsMinSize.c_str());
  1351. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG",
  1352. flagsDebug.c_str());
  1353. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELWITHDEBINFO",
  1354. flagsDebugRel.c_str());
  1355. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE",
  1356. flagsRelease.c_str());
  1357. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  1358. fout << line.c_str() << std::endl;
  1359. }
  1360. }
  1361. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  1362. {
  1363. std::ifstream fin(this->DSPFooterTemplate.c_str());
  1364. if(!fin)
  1365. {
  1366. cmSystemTools::Error("Error Reading ",
  1367. this->DSPFooterTemplate.c_str());
  1368. }
  1369. std::string line;
  1370. while(cmSystemTools::GetLineFromStream(fin, line))
  1371. {
  1372. fout << line << std::endl;
  1373. }
  1374. }
  1375. //----------------------------------------------------------------------------
  1376. void cmLocalVisualStudio6Generator
  1377. ::ComputeLinkOptions(cmTarget& target,
  1378. const char* configName,
  1379. const std::string extraOptions,
  1380. std::string& options)
  1381. {
  1382. // Compute the link information for this configuration.
  1383. std::vector<cmStdString> linkLibs;
  1384. std::vector<cmStdString> linkDirs;
  1385. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  1386. // Build the link options code.
  1387. for(std::vector<cmStdString>::const_iterator d = linkDirs.begin();
  1388. d != linkDirs.end(); ++d)
  1389. {
  1390. std::string dir = *d;
  1391. if(!dir.empty())
  1392. {
  1393. if(dir[dir.size()-1] != '/')
  1394. {
  1395. dir += "/";
  1396. }
  1397. dir += "$(IntDir)";
  1398. options += "# ADD LINK32 /LIBPATH:";
  1399. options += this->ConvertToOptionallyRelativeOutputPath(dir.c_str());
  1400. options += " /LIBPATH:";
  1401. options += this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  1402. options += "\n";
  1403. }
  1404. }
  1405. for(std::vector<cmStdString>::const_iterator l = linkLibs.begin();
  1406. l != linkLibs.end(); ++l)
  1407. {
  1408. options += "# ADD LINK32 ";
  1409. options += this->ConvertToOptionallyRelativeOutputPath(l->c_str());
  1410. options += "\n";
  1411. }
  1412. // Add extra options if any.
  1413. if(!extraOptions.empty())
  1414. {
  1415. options += "# ADD LINK32 ";
  1416. options += extraOptions;
  1417. options += "\n";
  1418. }
  1419. }
  1420. std::string
  1421. cmLocalVisualStudio6Generator
  1422. ::GetTargetDirectory(cmTarget const&) const
  1423. {
  1424. // No per-target directory for this generator (yet).
  1425. return "";
  1426. }
  1427. void cmLocalVisualStudio6Generator
  1428. ::GetTargetObjectFileDirectories(cmTarget* ,
  1429. std::vector<std::string>&
  1430. dirs)
  1431. {
  1432. std::string dir = this->Makefile->GetCurrentOutputDirectory();
  1433. dir += "/";
  1434. dir += this->GetGlobalGenerator()->GetCMakeCFGInitDirectory();
  1435. dirs.push_back(dir);
  1436. }
  1437. std::string
  1438. cmLocalVisualStudio6Generator
  1439. ::GetConfigName(std::string const& configuration) const
  1440. {
  1441. // Strip the subdirectory name out of the configuration name.
  1442. std::string config = configuration;
  1443. std::string::size_type pos = config.find_last_of(" ");
  1444. config = config.substr(pos+1, std::string::npos);
  1445. config = config.substr(0, config.size()-1);
  1446. return config;
  1447. }