cmLocalVisualStudio6Generator.cxx 56 KB

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