cmNMakeMakefileGenerator.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmNMakeMakefileGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmStandardIncludes.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmMakeDepend.h"
  19. #include "cmCacheManager.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include "windows.h"
  22. cmNMakeMakefileGenerator::cmNMakeMakefileGenerator()
  23. {
  24. this->SetLibraryPathOption("@CMAKE_C_LIBPATH_FLAG@"); // Use @ here
  25. this->SetObjectFileExtension("$(CMAKE_OBJECT_FILE_SUFFIX)");
  26. this->SetExecutableExtension("$(CMAKE_EXECUTABLE_SUFFIX)");
  27. this->SetLibraryPrefix("");
  28. this->SetStaticLibraryExtension("$(CMAKE_STATICLIB_SUFFIX)");
  29. this->SetSharedLibraryExtension("$(CMAKE_SHLIB_SUFFIX)");
  30. }
  31. cmNMakeMakefileGenerator::~cmNMakeMakefileGenerator()
  32. {
  33. }
  34. // convert to windows short paths if there are spaces
  35. // in path
  36. std::string cmNMakeMakefileGenerator::ShortPath(const char* path)
  37. {
  38. std::string ret = path;
  39. // if there are no spaces in path, then just
  40. // call ConvertToOutputPath
  41. if(ret.find(' ') == std::string::npos)
  42. {
  43. return this->ConvertToOutputPath(path);
  44. }
  45. // if there are spaces then call GetShortPathName to get rid of them
  46. if(!cmSystemTools::GetShortPath(path, ret))
  47. {
  48. // if GetShortPathName failed for some reason use
  49. // ConvertToOutputPath instead which will at least escape the spaces
  50. ret = this->ConvertToOutputPath(path);
  51. return ret;
  52. }
  53. ret = this->ConvertToOutputPath(ret.c_str());
  54. return ret;
  55. }
  56. // convert a command to a short path if it has spaces
  57. // this separates the arguments from the command and puts
  58. // them back together
  59. std::string cmNMakeMakefileGenerator::ShortPathCommand(const char* command)
  60. {
  61. if (!command)
  62. {
  63. return "";
  64. }
  65. if(!strchr(command, ' '))
  66. {
  67. return command;
  68. }
  69. cmRegularExpression reg("^\"([^\"]*)\"(.*)");
  70. if(reg.find(command))
  71. {
  72. std::string c = reg.match(1);
  73. cmRegularExpression removeIntDir("(.*)(/|\\\\)\\$\\(IntDir\\)(.*)");
  74. if(removeIntDir.find(c))
  75. {
  76. c = removeIntDir.match(1) + removeIntDir.match(3);
  77. }
  78. std::string unixPath = c;
  79. // since the command may already be a windows path, convert it
  80. // to unix so we can use SplitProgramPath on it.
  81. cmSystemTools::ConvertToUnixSlashes(unixPath);
  82. std::string path, file;
  83. cmSystemTools::SplitProgramPath(unixPath.c_str(), path, file);
  84. // do a short path on the directory, because ShortPath will
  85. // not work for files that do not exist
  86. path = this->ShortPath(path.c_str());
  87. // now put the two back together
  88. path += "\\";
  89. path += file;
  90. std::string ret = path;
  91. std::string args = reg.match(2);
  92. ret += args;
  93. return ret;
  94. }
  95. return command;
  96. }
  97. void cmNMakeMakefileGenerator::EnableLanguage(const char*)
  98. {
  99. // now load the settings
  100. if(!m_Makefile->GetDefinition("CMAKE_ROOT"))
  101. {
  102. cmSystemTools::Error(
  103. "CMAKE_ROOT has not been defined, bad GUI or driver program");
  104. return;
  105. }
  106. if(!this->GetLanguageEnabled("CXX"))
  107. {
  108. std::string fpath =
  109. m_Makefile->GetDefinition("CMAKE_ROOT");
  110. fpath += "/Templates/CMakeNMakeWindowsSystemConfig.cmake";
  111. m_Makefile->ReadListFile(NULL,fpath.c_str());
  112. this->SetLanguageEnabled("CXX");
  113. }
  114. }
  115. void cmNMakeMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  116. {
  117. fout << "# NMake Makefile generated by cmake\n";
  118. const char* variables =
  119. "# general variables used in the makefile\n"
  120. "\n"
  121. "# Path to cmake\n"
  122. "MAKESILENT = /nologo\n"
  123. "CMAKE_STANDARD_WINDOWS_LIBRARIES = @CMAKE_STANDARD_WINDOWS_LIBRARIES@\n"
  124. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@ @BUILD_FLAGS@\n"
  125. "CMAKE_C_LINK_EXECUTABLE_FLAG = @CMAKE_C_LINK_EXECUTABLE_FLAG@\n"
  126. "CMAKE_CXX_FLAGS = @CMAKE_CXX_FLAGS@ @BUILD_FLAGS@\n"
  127. "CMAKE_LINKER_FLAGS = @CMAKE_LINKER_FLAGS@ @LINKER_BUILD_FLAGS@\n"
  128. "CMAKE_LINKER_SHARED_LIBRARY_FLAG = @CMAKE_LINKER_SHARED_LIBRARY_FLAG@\n"
  129. "CMAKE_LIBRARY_MANAGER_FLAGS = @CMAKE_LIBRARY_MANAGER_FLAGS@\n"
  130. "CMAKE_OBJECT_FILE_SUFFIX = @CMAKE_OBJECT_FILE_SUFFIX@\n"
  131. "CMAKE_EXECUTABLE_SUFFIX = @CMAKE_EXECUTABLE_SUFFIX@\n"
  132. "CMAKE_STATICLIB_SUFFIX = @CMAKE_STATICLIB_SUFFIX@\n"
  133. "CMAKE_SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  134. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  135. "NULL=\n"
  136. "!ELSE \n"
  137. "NULL=nul\n"
  138. "!ENDIF \n";
  139. std::string buildType = "CMAKE_CXX_FLAGS_";
  140. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  141. buildType = cmSystemTools::UpperCase(buildType);
  142. m_Makefile->AddDefinition("BUILD_FLAGS",
  143. m_Makefile->GetDefinition(
  144. buildType.c_str()));
  145. buildType = "CMAKE_LINKER_FLAGS_";
  146. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  147. buildType = cmSystemTools::UpperCase(buildType);
  148. m_Makefile->AddDefinition("LINKER_BUILD_FLAGS",
  149. m_Makefile->GetDefinition(
  150. buildType.c_str()));
  151. std::string replaceVars = variables;
  152. m_Makefile->ExpandVariablesInString(replaceVars);
  153. fout << replaceVars.c_str();
  154. std::string ccommand = m_Makefile->GetDefinition("CCOMMAND_COMMAND");
  155. fout << "RM = " << this->ShortPath(ccommand.c_str()) << " remove -f\n";
  156. std::string ccompiler = m_Makefile->GetDefinition("CMAKE_C_COMPILER");
  157. fout << "CMAKE_C_COMPILER = "
  158. << this->ShortPath(ccompiler.c_str()) << "\n";
  159. std::string cxxcompiler = m_Makefile->GetDefinition("CMAKE_CXX_COMPILER");
  160. fout << "CMAKE_CXX_COMPILER = "
  161. << this->ShortPath(cxxcompiler.c_str()) << "\n";
  162. std::string linker = m_Makefile->GetDefinition("CMAKE_LINKER");
  163. fout << "CMAKE_LINKER = " <<
  164. this->ShortPath(linker.c_str()) << "\n";
  165. std::string lib_manager = m_Makefile->GetDefinition("CMAKE_LIBRARY_MANAGER");
  166. fout << "CMAKE_LIBRARY_MANAGER = "
  167. << this->ShortPath(lib_manager.c_str()) << "\n";
  168. std::string cmakecommand = m_Makefile->GetDefinition("CMAKE_COMMAND");
  169. fout << "CMAKE_COMMAND = "
  170. << this->ShortPath(cmakecommand.c_str()) << "\n";
  171. fout << "CMAKE_CURRENT_SOURCE = "
  172. << this->ShortPath(m_Makefile->GetStartDirectory() )
  173. << "\n";
  174. fout << "CMAKE_CURRENT_BINARY = "
  175. << this->ShortPath(m_Makefile->GetStartOutputDirectory())
  176. << "\n";
  177. fout << "CMAKE_SOURCE_DIR = "
  178. << this->ShortPath(m_Makefile->GetHomeDirectory()) << "\n";
  179. fout << "CMAKE_BINARY_DIR = "
  180. << this->ShortPath(m_Makefile->GetHomeOutputDirectory() )
  181. << "\n";
  182. // Output Include paths
  183. fout << "INCLUDE_FLAGS = ";
  184. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  185. std::vector<std::string>::iterator i;
  186. fout << "-I" <<
  187. this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << " ";
  188. for(i = includes.begin(); i != includes.end(); ++i)
  189. {
  190. std::string include = *i;
  191. // Don't output a -I for the standard include path "/usr/include".
  192. // This can cause problems with certain standard library
  193. // implementations because the wrong headers may be found first.
  194. fout << "-I" << this->ConvertToOutputPath(i->c_str()).c_str() << " ";
  195. }
  196. fout << m_Makefile->GetDefineFlags();
  197. fout << "\n\n";
  198. }
  199. void cmNMakeMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  200. const char* directory,
  201. const char* target1,
  202. const char* target2,
  203. bool silent)
  204. {
  205. if(target1)
  206. {
  207. std::string dir = this->ConvertToOutputPath(directory);
  208. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  209. << " "
  210. << "$(MAKE) $(MAKESILENT) rebuild_cache\n";
  211. if (!silent)
  212. {
  213. fout << "\techo " << directory << ": building " << target1 << "\n";
  214. }
  215. fout << "\tcd " << dir << "\n"
  216. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  217. }
  218. if(target2)
  219. {
  220. if (!silent)
  221. {
  222. fout << "\techo " << directory << ": building " << target2 << "\n";
  223. }
  224. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  225. }
  226. std::string currentDir = m_Makefile->GetCurrentOutputDirectory();
  227. fout << "\tcd " << this->ConvertToOutputPath(currentDir.c_str()) << "\n\n";
  228. }
  229. // This needs to be overriden because nmake requires commands to be quoted
  230. // if the are full paths to the executable????
  231. void cmNMakeMakefileGenerator::OutputMakeRule(std::ostream& fout,
  232. const char* comment,
  233. const char* target,
  234. const char* depends,
  235. const char* command,
  236. const char* command2,
  237. const char* command3,
  238. const char* command4)
  239. {
  240. std::string short_command;
  241. if (command)
  242. {
  243. short_command = ShortPathCommand(command);
  244. command = short_command.c_str();
  245. }
  246. std::string short_command2;
  247. if (command2)
  248. {
  249. short_command2 = ShortPathCommand(command2);
  250. command2 = short_command2.c_str();
  251. }
  252. std::string short_command3;
  253. if (command3)
  254. {
  255. short_command3 = ShortPathCommand(command3);
  256. command3 = short_command3.c_str();
  257. }
  258. std::string short_command4;
  259. if (command4)
  260. {
  261. short_command4 = ShortPathCommand(command4);
  262. command4 = short_command4.c_str();
  263. }
  264. cmUnixMakefileGenerator::OutputMakeRule(fout,
  265. comment,
  266. target,
  267. depends,
  268. command,
  269. command2,
  270. command3,
  271. command4);
  272. return;
  273. }
  274. void
  275. cmNMakeMakefileGenerator::
  276. OutputBuildObjectFromSource(std::ostream& fout,
  277. const char* shortName,
  278. const cmSourceFile& source,
  279. const char* extraCompileFlags,
  280. bool shared)
  281. {
  282. // Header files shouldn't have build rules.
  283. if(source.IsAHeaderFileOnly())
  284. return;
  285. std::string comment = "Build ";
  286. std::string objectFile = std::string(shortName) +
  287. this->GetOutputExtension(source.GetSourceExtension().c_str());
  288. comment += objectFile + " From ";
  289. comment += source.GetFullPath();
  290. std::string compileCommand;
  291. std::string ext = source.GetSourceExtension();
  292. if(ext == "c" )
  293. {
  294. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  295. compileCommand += extraCompileFlags;
  296. if(shared)
  297. {
  298. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  299. }
  300. compileCommand += "$(INCLUDE_FLAGS) -c ";
  301. compileCommand +=
  302. this->ConvertToOutputPath(source.GetFullPath().c_str());
  303. // Need to get the definition here because this value might have
  304. // trailing space (since it is directly prepended to the filename)
  305. std::string output_object_file_flag =
  306. m_Makefile->GetDefinition("CMAKE_C_OUTPUT_OBJECT_FILE_FLAG");
  307. m_Makefile->ExpandVariablesInString(output_object_file_flag);
  308. compileCommand += " " + output_object_file_flag;
  309. compileCommand += objectFile;
  310. }
  311. else if (ext == "rc")
  312. {
  313. compileCommand = "$(RC) /fo\"";
  314. compileCommand += objectFile;
  315. compileCommand += "\" ";
  316. compileCommand +=
  317. this->ConvertToOutputPath(source.GetFullPath().c_str());
  318. }
  319. else if (ext == "def")
  320. {
  321. // no rule to output for this one
  322. return;
  323. }
  324. // assume c++ if not c rc or def
  325. else
  326. {
  327. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  328. compileCommand += extraCompileFlags;
  329. if(shared)
  330. {
  331. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  332. }
  333. compileCommand += "$(INCLUDE_FLAGS) -c ";
  334. compileCommand +=
  335. this->ConvertToOutputPath(source.GetFullPath().c_str());
  336. // Need to get the definition here because this value might have
  337. // trailing space (since it is directly prepended to the filename)
  338. std::string output_object_file_flag =
  339. m_Makefile->GetDefinition("CMAKE_C_OUTPUT_OBJECT_FILE_FLAG");
  340. m_Makefile->ExpandVariablesInString(output_object_file_flag);
  341. compileCommand += " " + output_object_file_flag;
  342. compileCommand += objectFile;
  343. }
  344. this->OutputMakeRule(fout,
  345. comment.c_str(),
  346. objectFile.c_str(),
  347. this->ConvertToOutputPath(
  348. source.GetFullPath().c_str()).c_str(),
  349. compileCommand.c_str());
  350. }
  351. void cmNMakeMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  352. const char* name,
  353. const cmTarget &t)
  354. {
  355. std::string target = m_LibraryOutputPath + name + m_SharedLibraryExtension;
  356. std::string depend = "$(";
  357. depend += this->CreateMakeVariable(name, "_SRC_OBJS");
  358. depend += ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  359. // Need to get the definition here because this value might have
  360. // trailing space (since it is directly prepended to the filename)
  361. std::string linker_output_file_flag =
  362. m_Makefile->GetDefinition("CMAKE_LINKER_OUTPUT_FILE_FLAG");
  363. m_Makefile->ExpandVariablesInString(linker_output_file_flag);
  364. std::string command = "$(CMAKE_LINKER) $(CMAKE_LINKER_SHARED_LIBRARY_FLAG)";
  365. bool hide_param = m_Makefile->IsOn("CMAKE_LINKER_HIDE_PARAMETERS");
  366. if (hide_param)
  367. {
  368. command += " @<<\n\t";
  369. }
  370. command += " $(CMAKE_LINKER_FLAGS) " + linker_output_file_flag;
  371. std::string dllpath = m_LibraryOutputPath + std::string(name) + m_SharedLibraryExtension;
  372. command += this->ConvertToOutputPath(dllpath.c_str());
  373. command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  374. std::strstream linklibs;
  375. this->OutputLinkLibraries(linklibs, name, t);
  376. linklibs << std::ends;
  377. command += linklibs.str();
  378. delete [] linklibs.str();
  379. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  380. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  381. i != sources.end(); ++i)
  382. {
  383. if((*i)->GetSourceExtension() == "def")
  384. {
  385. command += "/DEF:";
  386. command += (*i)->GetFullPath();
  387. }
  388. }
  389. command += "\n";
  390. if (hide_param)
  391. {
  392. command += "<<\n";
  393. }
  394. std::string customCommands = this->CreateTargetRules(t, name);
  395. const char* cc = 0;
  396. if(customCommands.size() > 0)
  397. {
  398. cc = customCommands.c_str();
  399. }
  400. this->OutputMakeRule(fout, "rules for a shared library",
  401. target.c_str(),
  402. depend.c_str(),
  403. command.c_str(), cc);
  404. }
  405. void cmNMakeMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  406. const char* name,
  407. const cmTarget &target)
  408. {
  409. this->OutputSharedLibraryRule(fout, name, target);
  410. }
  411. void cmNMakeMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  412. const char* name,
  413. const cmTarget &t)
  414. {
  415. std::string target = m_LibraryOutputPath + std::string(name) + m_StaticLibraryExtension;
  416. std::string depend = "$(";
  417. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  418. // Need to get the definition here because this value might have
  419. // trailing space (since it is directly prepended to the filename)
  420. std::string library_manager_output_file_flag =
  421. m_Makefile->GetDefinition("CMAKE_LIBRARY_MANAGER_OUTPUT_FILE_FLAG");
  422. m_Makefile->ExpandVariablesInString(library_manager_output_file_flag);
  423. std::string command = "$(CMAKE_LIBRARY_MANAGER) $(CMAKE_LIBRARY_MANAGER_FLAGS) @<<\n\t " + library_manager_output_file_flag;
  424. std::string libpath = m_LibraryOutputPath + std::string(name) + m_StaticLibraryExtension;
  425. command += this->ConvertToOutputPath(libpath.c_str());
  426. command += " $(";
  427. command += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  428. command += "\n<<\n";
  429. std::string comment = "rule to build static library: ";
  430. comment += name;
  431. std::string customCommands = this->CreateTargetRules(t, name);
  432. const char* cc = 0;
  433. if(customCommands.size() > 0)
  434. {
  435. cc = customCommands.c_str();
  436. }
  437. this->OutputMakeRule(fout,
  438. comment.c_str(),
  439. target.c_str(),
  440. depend.c_str(),
  441. command.c_str(), cc);
  442. }
  443. void cmNMakeMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  444. const char* name,
  445. const cmTarget &t)
  446. {
  447. std::string target = m_ExecutableOutputPath + name;
  448. target += m_ExecutableExtension;
  449. std::string depend = "$(";
  450. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") $(" +
  451. this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  452. std::string command =
  453. "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  454. command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  455. std::string path = m_ExecutableOutputPath + name + m_ExecutableExtension;
  456. // Need to get the definition here because this value might have
  457. // trailing space (since it is directly prepended to the filename)
  458. std::string output_executable_file_flag =
  459. m_Makefile->GetDefinition("CMAKE_C_OUTPUT_EXECUTABLE_FILE_FLAG");
  460. m_Makefile->ExpandVariablesInString(output_executable_file_flag);
  461. command += " " + output_executable_file_flag +
  462. this->ConvertToOutputPath(path.c_str());
  463. command += " $(CMAKE_C_LINK_EXECUTABLE_FLAG) $(CMAKE_LINKER_FLAGS) ";
  464. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  465. {
  466. command += " /subsystem:windows ";
  467. }
  468. std::strstream linklibs;
  469. this->OutputLinkLibraries(linklibs, 0, t);
  470. linklibs << std::ends;
  471. command += linklibs.str();
  472. std::string comment = "rule to build executable: ";
  473. comment += name;
  474. std::string customCommands = this->CreateTargetRules(t, name);
  475. const char* cc = 0;
  476. if(customCommands.size() > 0)
  477. {
  478. cc = customCommands.c_str();
  479. }
  480. this->OutputMakeRule(fout,
  481. comment.c_str(),
  482. target.c_str(),
  483. depend.c_str(),
  484. command.c_str(), cc);
  485. }
  486. void cmNMakeMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  487. const char* targetLibrary,
  488. const cmTarget &tgt)
  489. {
  490. // Try to emit each search path once
  491. std::set<std::string> emitted;
  492. // Embed runtime search paths if possible and if required.
  493. // collect all the flags needed for linking libraries
  494. // Do not try if there is no library path option (it is set to -L or
  495. // -LIBPATH for some linker, but some others do not even support link
  496. // search path).
  497. std::string linkLibs;
  498. // Expand content because this value might have
  499. // trailing space (since it is directly prepended to the filename)
  500. std::string lib_path_opt = m_LibraryPathOption;
  501. m_Makefile->ExpandVariablesInString(lib_path_opt);
  502. if (lib_path_opt.size())
  503. {
  504. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  505. for(std::vector<std::string>::iterator libDir = libdirs.begin();
  506. libDir != libdirs.end(); ++libDir)
  507. {
  508. std::string libpath = ShortPath(libDir->c_str());
  509. if(emitted.insert(libpath).second)
  510. {
  511. linkLibs += lib_path_opt;
  512. this->ConvertToOutputPath(libpath.c_str());
  513. linkLibs += libpath;
  514. linkLibs += " ";
  515. }
  516. }
  517. }
  518. std::string librariesLinked;
  519. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  520. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  521. lib != libs.end(); ++lib)
  522. {
  523. // Don't link the library against itself!
  524. if(targetLibrary && (lib->first == targetLibrary)) continue;
  525. // ** should fix this later, it should check to see if this is
  526. // a debug build and add the library
  527. // don't look at debug libraries
  528. // if (lib->second == cmTarget::DEBUG) continue;
  529. // skip zero size library entries, this may happen
  530. // if a variable expands to nothing.
  531. if (lib->first.size() == 0) continue;
  532. if(emitted.insert(lib->first).second)
  533. {
  534. std::string regexp = ".*\\";
  535. regexp += m_Makefile->GetDefinition("CMAKE_STATICLIB_SUFFIX");
  536. regexp += "$";
  537. cmRegularExpression reg(regexp.c_str());
  538. // if it ends in .lib, then it is a full path and should
  539. // be escaped, and does not need .lib added
  540. if(reg.find(lib->first))
  541. {
  542. librariesLinked += ShortPath(lib->first.c_str());
  543. librariesLinked += " ";
  544. }
  545. else
  546. {
  547. librariesLinked += m_LibraryLinkOption;
  548. librariesLinked += lib->first;
  549. librariesLinked += m_StaticLibraryExtension + " ";
  550. }
  551. }
  552. }
  553. linkLibs += librariesLinked;
  554. fout << linkLibs;
  555. fout << "$(CMAKE_STANDARD_WINDOWS_LIBRARIES) ";
  556. }
  557. std::string cmNMakeMakefileGenerator::GetOutputExtension(const char* s)
  558. {
  559. std::string sourceExtension = s;
  560. if(sourceExtension == "def")
  561. {
  562. return "";
  563. }
  564. if(sourceExtension == "ico" || sourceExtension == "rc2")
  565. {
  566. return "";
  567. }
  568. if(sourceExtension == "rc")
  569. {
  570. return ".res";
  571. }
  572. return m_ObjectFileExtension;
  573. }
  574. void cmNMakeMakefileGenerator::OutputIncludeMakefile(std::ostream& fout,
  575. const char* file)
  576. {
  577. fout << "!include " << file << "\n";
  578. }
  579. bool cmNMakeMakefileGenerator::SamePath(const char* path1, const char* path2)
  580. {
  581. // first check to see if they are the same anyway
  582. if (strcmp(path1, path2) == 0)
  583. {
  584. return true;
  585. }
  586. // next short path and lower case both of them for the compare
  587. return
  588. cmSystemTools::LowerCase(ShortPath(path1)) ==
  589. cmSystemTools::LowerCase(ShortPath(path2));
  590. }
  591. void cmNMakeMakefileGenerator::OutputBuildLibraryInDir(std::ostream& fout,
  592. const char* path,
  593. const char* ,
  594. const char* fullpath)
  595. {
  596. std::string currentDir =
  597. this->ConvertToOutputPath(m_Makefile->GetCurrentOutputDirectory());
  598. std::string wpath = this->ConvertToOutputPath(path);
  599. std::string wfullpath = this->ConvertToOutputPath(fullpath);
  600. fout << wfullpath
  601. << ":\n\tcd " << wpath << "\n"
  602. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  603. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  604. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  605. << "\t$(MAKE) $(MAKESILENT) " << wfullpath
  606. << "\n\tcd " << currentDir << "\n";
  607. }
  608. std::string cmNMakeMakefileGenerator::ConvertToOutputPath(const char* s)
  609. {
  610. return cmSystemTools::ConvertToOutputPath(s);
  611. }
  612. std::string cmNMakeMakefileGenerator::CreateMakeVariable(const char* s, const char* s2)
  613. {
  614. std::string ret= std::string(s) + std::string(s2);
  615. cmSystemTools::ReplaceString(ret, "-", "_");
  616. return ret;
  617. }