cmLocalVisualStudio6Generator.cxx 63 KB

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