cmDSPWriter.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmDSPWriter.h"
  33. #include "cmStandardIncludes.h"
  34. #include "cmSystemTools.h"
  35. #include "cmRegularExpression.h"
  36. cmDSPWriter::~cmDSPWriter()
  37. {
  38. }
  39. cmDSPWriter::cmDSPWriter(cmMakefile*mf)
  40. {
  41. m_Makefile = mf;
  42. }
  43. void cmDSPWriter::OutputDSPFile()
  44. {
  45. // If not an in source build, then create the output directory
  46. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  47. m_Makefile->GetHomeDirectory()) != 0)
  48. {
  49. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  50. {
  51. cmSystemTools::Error("Error creating directory ",
  52. m_Makefile->GetStartOutputDirectory());
  53. }
  54. }
  55. // Setup /I and /LIBPATH options for the resulting DSP file
  56. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  57. std::vector<std::string>::iterator i;
  58. for(i = includes.begin(); i != includes.end(); ++i)
  59. {
  60. m_IncludeOptions += " /I ";
  61. std::string tmp = cmSystemTools::EscapeSpaces(i->c_str());
  62. cmSystemTools::ConvertToWindowsSlashesAndCleanUp(tmp);
  63. // quote if not already quoted
  64. if (tmp[0] != '"')
  65. {
  66. m_IncludeOptions += "\"";
  67. m_IncludeOptions += tmp;
  68. m_IncludeOptions += "\"";
  69. }
  70. else
  71. {
  72. m_IncludeOptions += tmp;
  73. }
  74. }
  75. // Create the DSP or set of DSP's for libraries and executables
  76. // clear project names
  77. m_CreatedProjectNames.clear();
  78. // build any targets
  79. cmTargets &tgts = m_Makefile->GetTargets();
  80. for(cmTargets::iterator l = tgts.begin();
  81. l != tgts.end(); l++)
  82. {
  83. switch(l->second.GetType())
  84. {
  85. case cmTarget::STATIC_LIBRARY:
  86. this->SetBuildType(STATIC_LIBRARY, l->first.c_str());
  87. break;
  88. case cmTarget::SHARED_LIBRARY:
  89. this->SetBuildType(DLL, l->first.c_str());
  90. break;
  91. case cmTarget::EXECUTABLE:
  92. this->SetBuildType(EXECUTABLE,l->first.c_str());
  93. break;
  94. case cmTarget::WIN32_EXECUTABLE:
  95. this->SetBuildType(WIN32_EXECUTABLE,l->first.c_str());
  96. break;
  97. case cmTarget::UTILITY:
  98. this->SetBuildType(UTILITY, l->first.c_str());
  99. break;
  100. case cmTarget::INSTALL_FILES:
  101. break;
  102. case cmTarget::INSTALL_PROGRAMS:
  103. break;
  104. default:
  105. cmSystemTools::Error("Bad target type", l->first.c_str());
  106. break;
  107. }
  108. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  109. // so don't build a projectfile for it
  110. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  111. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  112. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  113. {
  114. this->CreateSingleDSP(l->first.c_str(),l->second);
  115. }
  116. }
  117. }
  118. void cmDSPWriter::CreateSingleDSP(const char *lname, cmTarget &target)
  119. {
  120. // add to the list of projects
  121. std::string pname = lname;
  122. m_CreatedProjectNames.push_back(pname);
  123. // create the dsp.cmake file
  124. std::string fname;
  125. fname = m_Makefile->GetStartOutputDirectory();
  126. fname += "/";
  127. fname += lname;
  128. fname += ".dsp";
  129. // save the name of the real dsp file
  130. std::string realDSP = fname;
  131. fname += ".cmake";
  132. std::ofstream fout(fname.c_str());
  133. if(!fout)
  134. {
  135. cmSystemTools::Error("Error Writing ", fname.c_str());
  136. }
  137. this->WriteDSPFile(fout,lname,target);
  138. fout.close();
  139. // if the dsp file has changed, then write it.
  140. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  141. }
  142. void cmDSPWriter::AddDSPBuildRule(cmSourceGroup& sourceGroup)
  143. {
  144. std::string dspname = *(m_CreatedProjectNames.end()-1);
  145. if(dspname == "ALL_BUILD")
  146. {
  147. return;
  148. }
  149. dspname += ".dsp.cmake";
  150. std::string makefileIn = m_Makefile->GetStartDirectory();
  151. makefileIn += "/";
  152. makefileIn += "CMakeLists.txt";
  153. makefileIn = cmSystemTools::HandleNetworkPaths(makefileIn.c_str());
  154. makefileIn = cmSystemTools::EscapeSpaces(makefileIn.c_str());
  155. std::string dsprule = "${CMAKE_COMMAND}";
  156. m_Makefile->ExpandVariablesInString(dsprule);
  157. dsprule = cmSystemTools::HandleNetworkPaths(dsprule.c_str());
  158. std::string args = makefileIn;
  159. args += " -H\"";
  160. args += cmSystemTools::HandleNetworkPaths(m_Makefile->GetHomeDirectory());
  161. args += "\" -S\"";
  162. args += cmSystemTools::HandleNetworkPaths(m_Makefile->GetStartDirectory());
  163. args += "\" -O\"";
  164. args += cmSystemTools::HandleNetworkPaths(m_Makefile->GetStartOutputDirectory());
  165. args += "\" -B\"";
  166. args += cmSystemTools::HandleNetworkPaths(m_Makefile->GetHomeOutputDirectory());
  167. args += "\"";
  168. m_Makefile->ExpandVariablesInString(args);
  169. std::string configFile =
  170. m_Makefile->GetDefinition("CMAKE_ROOT");
  171. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  172. std::vector<std::string> listFiles = m_Makefile->GetListFiles();
  173. bool found = false;
  174. for(std::vector<std::string>::iterator i = listFiles.begin();
  175. i != listFiles.end(); ++i)
  176. {
  177. if(*i == configFile)
  178. {
  179. found = true;
  180. }
  181. }
  182. if(!found)
  183. {
  184. listFiles.push_back(configFile);
  185. }
  186. std::vector<std::string> outputs;
  187. outputs.push_back(dspname);
  188. cmCustomCommand cc(makefileIn.c_str(), dsprule.c_str(),
  189. args.c_str(),
  190. listFiles,
  191. outputs);
  192. sourceGroup.AddCustomCommand(cc);
  193. }
  194. void cmDSPWriter::WriteDSPFile(std::ostream& fout,
  195. const char *libName,
  196. cmTarget &target)
  197. {
  198. // We may be modifying the source groups temporarily, so make a copy.
  199. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  200. // get the classes from the source lists then add them to the groups
  201. std::vector<cmSourceFile> classes = target.GetSourceFiles();
  202. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  203. i != classes.end(); i++)
  204. {
  205. // Add the file to the list of sources.
  206. std::string source = i->GetFullPath();
  207. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  208. sourceGroups);
  209. sourceGroup.AddSource(source.c_str());
  210. }
  211. // add any custom rules to the source groups
  212. for (std::vector<cmCustomCommand>::const_iterator cr =
  213. target.GetCustomCommands().begin();
  214. cr != target.GetCustomCommands().end(); ++cr)
  215. {
  216. cmSourceGroup& sourceGroup =
  217. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  218. sourceGroups);
  219. cmCustomCommand cc(*cr);
  220. cc.ExpandVariables(*m_Makefile);
  221. sourceGroup.AddCustomCommand(cc);
  222. }
  223. // Write the DSP file's header.
  224. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  225. // Find the group in which the CMakeLists.txt source belongs, and add
  226. // the rule to generate this DSP file.
  227. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  228. sg != sourceGroups.rend(); ++sg)
  229. {
  230. if(sg->Matches("CMakeLists.txt"))
  231. {
  232. this->AddDSPBuildRule(*sg);
  233. break;
  234. }
  235. }
  236. // Loop through every source group.
  237. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  238. sg != sourceGroups.end(); ++sg)
  239. {
  240. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  241. // If the group is empty, don't write it at all.
  242. if(buildRules.empty())
  243. { continue; }
  244. // If the group has a name, write the header.
  245. std::string name = sg->GetName();
  246. if(name != "")
  247. {
  248. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  249. }
  250. // Loop through each build rule in the source group.
  251. for(cmSourceGroup::BuildRules::const_iterator cc =
  252. buildRules.begin(); cc != buildRules.end(); ++ cc)
  253. {
  254. std::string source = cc->first;
  255. const cmSourceGroup::Commands& commands = cc->second;
  256. if (source != libName || target.GetType() == cmTarget::UTILITY)
  257. {
  258. fout << "# Begin Source File\n\n";
  259. // Tell MS-Dev what the source is. If the compiler knows how to
  260. // build it, then it will.
  261. fout << "SOURCE=" << cmSystemTools::EscapeSpaces(source.c_str()) << "\n\n";
  262. if (!commands.empty())
  263. {
  264. cmSourceGroup::CommandFiles totalCommand;
  265. std::string totalCommandStr;
  266. totalCommandStr = this->CombineCommands(commands, totalCommand,
  267. source.c_str());
  268. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  269. totalCommand.m_Depends,
  270. totalCommand.m_Outputs);
  271. }
  272. fout << "# End Source File\n";
  273. }
  274. }
  275. // If the group has a name, write the footer.
  276. if(name != "")
  277. {
  278. this->WriteDSPEndGroup(fout);
  279. }
  280. }
  281. // Write the DSP file's footer.
  282. this->WriteDSPFooter(fout);
  283. }
  284. void cmDSPWriter::WriteCustomRule(std::ostream& fout,
  285. const char* source,
  286. const char* command,
  287. const std::set<std::string>& depends,
  288. const std::set<std::string>& outputs)
  289. {
  290. std::vector<std::string>::iterator i;
  291. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  292. {
  293. if (i == m_Configurations.begin())
  294. {
  295. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  296. }
  297. else
  298. {
  299. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  300. }
  301. // Write out the dependencies for the rule.
  302. fout << "USERDEP__HACK=";
  303. std::string temp;
  304. for(std::set<std::string>::const_iterator d = depends.begin();
  305. d != depends.end(); ++d)
  306. {
  307. temp = *d;
  308. fout << "\\\n\t" << cmSystemTools::EscapeSpaces(cmSystemTools::ConvertToWindowsSlashes(temp));
  309. }
  310. fout << "\n";
  311. fout << "# PROP Ignore_Default_Tool 1\n";
  312. fout << "# Begin Custom Build\n\n";
  313. if(outputs.size() == 0)
  314. {
  315. fout << source << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  316. fout << command << "\n\n";
  317. }
  318. // Write a rule for every output generated by this command.
  319. for(std::set<std::string>::const_iterator output = outputs.begin();
  320. output != outputs.end(); ++output)
  321. {
  322. fout << "\"" << output->c_str()
  323. << "\" : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  324. fout << command << "\n\n";
  325. }
  326. fout << "# End Custom Build\n\n";
  327. }
  328. fout << "!ENDIF\n\n";
  329. }
  330. void cmDSPWriter::WriteDSPBeginGroup(std::ostream& fout,
  331. const char* group,
  332. const char* filter)
  333. {
  334. fout << "# Begin Group \"" << group << "\"\n"
  335. "# PROP Default_Filter \"" << filter << "\"\n";
  336. }
  337. void cmDSPWriter::WriteDSPEndGroup(std::ostream& fout)
  338. {
  339. fout << "# End Group\n";
  340. }
  341. void cmDSPWriter::SetBuildType(BuildType b, const char *libName)
  342. {
  343. std::string root= m_Makefile->GetDefinition("CMAKE_ROOT");
  344. const char *def= m_Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  345. if( def)
  346. {
  347. root = def;
  348. }
  349. else
  350. {
  351. root += "/Templates";
  352. }
  353. switch(b)
  354. {
  355. case STATIC_LIBRARY:
  356. m_DSPHeaderTemplate = root;
  357. m_DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  358. m_DSPFooterTemplate = root;
  359. m_DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  360. break;
  361. case DLL:
  362. m_DSPHeaderTemplate = root;
  363. m_DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  364. m_DSPFooterTemplate = root;
  365. m_DSPFooterTemplate += "/DLLFooter.dsptemplate";
  366. break;
  367. case EXECUTABLE:
  368. m_DSPHeaderTemplate = root;
  369. m_DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  370. m_DSPFooterTemplate = root;
  371. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  372. break;
  373. case WIN32_EXECUTABLE:
  374. m_DSPHeaderTemplate = root;
  375. m_DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  376. m_DSPFooterTemplate = root;
  377. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  378. break;
  379. case UTILITY:
  380. m_DSPHeaderTemplate = root;
  381. m_DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  382. m_DSPFooterTemplate = root;
  383. m_DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  384. break;
  385. }
  386. // once the build type is set, determine what configurations are
  387. // possible
  388. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  389. cmRegularExpression reg("# Name ");
  390. if(!fin)
  391. {
  392. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  393. }
  394. // reset m_Configurations
  395. m_Configurations.erase(m_Configurations.begin(), m_Configurations.end());
  396. // now add all the configurations possible
  397. char buffer[2048];
  398. while(fin)
  399. {
  400. fin.getline(buffer, 2048);
  401. std::string line = buffer;
  402. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  403. if (reg.find(line))
  404. {
  405. m_Configurations.push_back(line.substr(reg.end()));
  406. }
  407. }
  408. }
  409. std::string
  410. cmDSPWriter::CombineCommands(const cmSourceGroup::Commands &commands,
  411. cmSourceGroup::CommandFiles &totalCommand,
  412. const char *source)
  413. {
  414. // Loop through every custom command generating code from the
  415. // current source.
  416. // build up the depends and outputs and commands
  417. std::string totalCommandStr = "";
  418. std::string temp;
  419. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  420. c != commands.end(); ++c)
  421. {
  422. totalCommandStr += "\n\t";
  423. temp= c->second.m_Command;
  424. cmSystemTools::ConvertToWindowsSlashes(temp);
  425. temp = cmSystemTools::EscapeSpaces(temp.c_str());
  426. totalCommandStr += temp;
  427. totalCommandStr += " ";
  428. totalCommandStr += c->second.m_Arguments;
  429. totalCommand.Merge(c->second);
  430. }
  431. // Create a dummy file with the name of the source if it does
  432. // not exist
  433. if(totalCommand.m_Outputs.empty())
  434. {
  435. std::string dummyFile = m_Makefile->GetStartOutputDirectory();
  436. dummyFile += "/";
  437. dummyFile += source;
  438. if(!cmSystemTools::FileExists(dummyFile.c_str()))
  439. {
  440. std::ofstream fout(dummyFile.c_str());
  441. fout << "Dummy file created by cmake as unused source for utility command.\n";
  442. }
  443. }
  444. return totalCommandStr;
  445. }
  446. // look for custom rules on a target and collect them together
  447. std::string
  448. cmDSPWriter::CreateTargetRules(const cmTarget &target,
  449. const char *libName)
  450. {
  451. std::string customRuleCode = "";
  452. if (target.GetType() >= cmTarget::UTILITY)
  453. {
  454. return customRuleCode;
  455. }
  456. // Find the group in which the lix exe custom rules belong
  457. bool init = false;
  458. for (std::vector<cmCustomCommand>::const_iterator cr =
  459. target.GetCustomCommands().begin();
  460. cr != target.GetCustomCommands().end(); ++cr)
  461. {
  462. cmCustomCommand cc(*cr);
  463. cc.ExpandVariables(*m_Makefile);
  464. if (cc.GetSourceName() == libName)
  465. {
  466. if (!init)
  467. {
  468. // header stuff
  469. customRuleCode = "# Begin Special Build Tool\nPostBuild_Cmds=";
  470. init = true;
  471. }
  472. else
  473. {
  474. customRuleCode += "\t";
  475. }
  476. customRuleCode += cc.GetCommand() + " " + cc.GetArguments();
  477. }
  478. }
  479. if (init)
  480. {
  481. customRuleCode += "\n# End Special Build Tool\n";
  482. }
  483. return customRuleCode;
  484. }
  485. void cmDSPWriter::WriteDSPHeader(std::ostream& fout, const char *libName,
  486. const cmTarget &target,
  487. std::vector<cmSourceGroup> &)
  488. {
  489. std::set<std::string> pathEmitted;
  490. // determine the link directories
  491. std::string libOptions;
  492. std::string libDebugOptions;
  493. std::string libOptimizedOptions;
  494. std::string libMultiLineOptions;
  495. std::string libMultiLineDebugOptions;
  496. std::string libMultiLineOptimizedOptions;
  497. // suppoirt override in output directory
  498. std::string libPath = "";
  499. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  500. {
  501. libPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  502. }
  503. std::string exePath = "";
  504. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  505. {
  506. exePath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  507. }
  508. if(libPath.size())
  509. {
  510. // make sure there is a trailing slash
  511. if(libPath[libPath.size()-1] != '/')
  512. {
  513. libPath += "/";
  514. }
  515. std::string lpath = cmSystemTools::HandleNetworkPaths(libPath.c_str());
  516. if(pathEmitted.insert(lpath).second)
  517. {
  518. libOptions += " /LIBPATH:\"";
  519. libOptions += lpath;
  520. libOptions += "$(INTDIR)\" ";
  521. libOptions += " /LIBPATH:\"";
  522. libOptions += lpath;
  523. libOptions += "\" ";
  524. libMultiLineOptions += "# ADD LINK32 /LIBPATH:\"";
  525. libMultiLineOptions += lpath;
  526. libMultiLineOptions += "$(INTDIR)\" ";
  527. libMultiLineOptions += " /LIBPATH:\"";
  528. libMultiLineOptions += lpath;
  529. libMultiLineOptions += "\" \n";
  530. }
  531. }
  532. if(exePath.size())
  533. {
  534. // make sure there is a trailing slash
  535. if(exePath[exePath.size()-1] != '/')
  536. {
  537. exePath += "/";
  538. }
  539. std::string lpath = cmSystemTools::HandleNetworkPaths(exePath.c_str());
  540. if(pathEmitted.insert(lpath).second)
  541. {
  542. libOptions += " /LIBPATH:\"";
  543. libOptions += lpath;
  544. libOptions += "$(INTDIR)\" ";
  545. libOptions += " /LIBPATH:\"";
  546. libOptions += lpath;
  547. libOptions += "\" ";
  548. libMultiLineOptions += "# ADD LINK32 /LIBPATH:\"";
  549. libMultiLineOptions += lpath;
  550. libMultiLineOptions += "$(INTDIR)\" ";
  551. libMultiLineOptions += " /LIBPATH:\"";
  552. libMultiLineOptions += lpath;
  553. libMultiLineOptions += "\" \n";
  554. }
  555. }
  556. std::vector<std::string>::iterator i;
  557. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  558. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  559. {
  560. std::string lpath = cmSystemTools::HandleNetworkPaths(i->c_str());
  561. if(lpath[lpath.size()-1] != '/')
  562. {
  563. lpath += "/";
  564. }
  565. if(pathEmitted.insert(lpath).second)
  566. {
  567. libOptions += " /LIBPATH:\"";
  568. libOptions += lpath;
  569. libOptions += "/$(INTDIR)\" ";
  570. libOptions += " /LIBPATH:\"";
  571. libOptions += lpath;
  572. libOptions += "\" ";
  573. libMultiLineOptions += "# ADD LINK32 /LIBPATH:\"";
  574. libMultiLineOptions += lpath;
  575. libMultiLineOptions += "/$(INTDIR)\" ";
  576. libMultiLineOptions += " /LIBPATH:\"";
  577. libMultiLineOptions += lpath;
  578. libMultiLineOptions += "\" \n";
  579. }
  580. }
  581. // find link libraries
  582. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  583. cmTarget::LinkLibraries::const_iterator j;
  584. for(j = libs.begin(); j != libs.end(); ++j)
  585. {
  586. // add libraries to executables and dlls (but never include
  587. // a library in a library, bad recursion)
  588. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  589. && target.GetType() != cmTarget::STATIC_LIBRARY) ||
  590. (target.GetType() == cmTarget::SHARED_LIBRARY && libName != j->first))
  591. {
  592. std::string lib = j->first;
  593. if(j->first.find(".lib") == std::string::npos)
  594. {
  595. lib += ".lib";
  596. }
  597. lib = cmSystemTools::EscapeSpaces(lib.c_str());
  598. if (j->second == cmTarget::GENERAL)
  599. {
  600. libOptions += " ";
  601. libOptions += lib;
  602. libMultiLineOptions += "# ADD LINK32 ";
  603. libMultiLineOptions += lib;
  604. libMultiLineOptions += "\n";
  605. }
  606. if (j->second == cmTarget::DEBUG)
  607. {
  608. libDebugOptions += " ";
  609. libDebugOptions += lib;
  610. libMultiLineDebugOptions += "# ADD LINK32 ";
  611. libMultiLineDebugOptions += lib;
  612. libMultiLineDebugOptions += "\n";
  613. }
  614. if (j->second == cmTarget::OPTIMIZED)
  615. {
  616. libOptimizedOptions += " ";
  617. libOptimizedOptions += lib;
  618. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  619. libMultiLineOptimizedOptions += lib;
  620. libMultiLineOptimizedOptions += "\n";
  621. }
  622. }
  623. }
  624. std::string extraLinkOptions =
  625. m_Makefile->GetDefinition("CMAKE_EXTRA_LINK_FLAGS");
  626. if(extraLinkOptions.size())
  627. {
  628. libOptions += " ";
  629. libOptions += extraLinkOptions;
  630. libOptions += " ";
  631. libMultiLineOptions += "# ADD LINK32 ";
  632. libMultiLineOptions += extraLinkOptions;
  633. libMultiLineOptions += " \n";
  634. }
  635. // are there any custom rules on the target itself
  636. // only if the target is a lib or exe
  637. std::string customRuleCode = this->CreateTargetRules(target, libName);
  638. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  639. if(!fin)
  640. {
  641. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  642. }
  643. char buffer[2048];
  644. while(fin)
  645. {
  646. fin.getline(buffer, 2048);
  647. std::string line = buffer;
  648. const char* mfcFlag = m_Makefile->GetDefinition("CMAKE_MFC_FLAG");
  649. if(!mfcFlag)
  650. {
  651. mfcFlag = "0";
  652. }
  653. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE",
  654. customRuleCode.c_str());
  655. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  656. mfcFlag);
  657. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  658. libOptions.c_str());
  659. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  660. libDebugOptions.c_str());
  661. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  662. libOptimizedOptions.c_str());
  663. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  664. libMultiLineOptions.c_str());
  665. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  666. libMultiLineDebugOptions.c_str());
  667. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  668. libMultiLineOptimizedOptions.c_str());
  669. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  670. m_IncludeOptions.c_str());
  671. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  672. cmSystemTools::ReplaceString(line, "LIBRARY_OUTPUT_PATH",
  673. cmSystemTools::HandleNetworkPaths(libPath.c_str()).c_str());
  674. cmSystemTools::ReplaceString(line, "EXECUTABLE_OUTPUT_PATH",
  675. cmSystemTools::HandleNetworkPaths(exePath.c_str()).c_str());
  676. cmSystemTools::ReplaceString(line,
  677. "EXTRA_DEFINES",
  678. m_Makefile->GetDefineFlags());
  679. cmSystemTools::ReplaceString(line,
  680. "CMAKE_CXX_FLAGS_RELEASE",
  681. m_Makefile->
  682. GetDefinition("CMAKE_CXX_FLAGS_RELEASE"));
  683. cmSystemTools::ReplaceString(line,
  684. "CMAKE_CXX_FLAGS_MINSIZEREL",
  685. m_Makefile->
  686. GetDefinition("CMAKE_CXX_FLAGS_MINSIZEREL")
  687. );
  688. cmSystemTools::ReplaceString(line,
  689. "CMAKE_CXX_FLAGS_DEBUG",
  690. m_Makefile->
  691. GetDefinition("CMAKE_CXX_FLAGS_DEBUG"));
  692. cmSystemTools::ReplaceString(line,
  693. "CMAKE_CXX_FLAGS_RELWITHDEBINFO",
  694. m_Makefile->
  695. GetDefinition("CMAKE_CXX_FLAGS_RELWITHDEBINFO"));
  696. cmSystemTools::ReplaceString(line,
  697. "CMAKE_CXX_FLAGS",
  698. m_Makefile->
  699. GetDefinition("CMAKE_CXX_FLAGS"));
  700. fout << line.c_str() << std::endl;
  701. }
  702. }
  703. void cmDSPWriter::WriteDSPFooter(std::ostream& fout)
  704. {
  705. std::ifstream fin(m_DSPFooterTemplate.c_str());
  706. if(!fin)
  707. {
  708. cmSystemTools::Error("Error Reading ",
  709. m_DSPFooterTemplate.c_str());
  710. }
  711. char buffer[2048];
  712. while(fin)
  713. {
  714. fin.getline(buffer, 2048);
  715. fout << buffer << std::endl;
  716. }
  717. }