cmNMakeMakefileGenerator.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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@ -DWIN32\n"
  125. "CMAKE_C_LINK_EXECUTABLE_FLAG = @CMAKE_C_LINK_EXECUTABLE_FLAG@\n"
  126. "CMAKE_CXX_FLAGS = @CMAKE_CXX_FLAGS@ -DWIN32 @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 ccompiler = m_Makefile->GetDefinition("CMAKE_C_COMPILER");
  155. fout << "CMAKE_C_COMPILER = "
  156. << this->ShortPath(ccompiler.c_str()) << "\n";
  157. std::string cxxcompiler = m_Makefile->GetDefinition("CMAKE_CXX_COMPILER");
  158. fout << "CMAKE_CXX_COMPILER = "
  159. << this->ShortPath(cxxcompiler.c_str()) << "\n";
  160. std::string linker = m_Makefile->GetDefinition("CMAKE_LINKER");
  161. fout << "CMAKE_LINKER = " <<
  162. this->ShortPath(linker.c_str()) << "\n";
  163. std::string lib_manager = m_Makefile->GetDefinition("CMAKE_LIBRARY_MANAGER");
  164. fout << "CMAKE_LIBRARY_MANAGER = "
  165. << this->ShortPath(lib_manager.c_str()) << "\n";
  166. std::string cmakecommand = m_Makefile->GetDefinition("CMAKE_COMMAND");
  167. fout << "CMAKE_COMMAND = "
  168. << this->ShortPath(cmakecommand.c_str()) << "\n";
  169. fout << "RM = " << this->ShortPath(cmakecommand.c_str()) << " -E remove -f\n";
  170. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  171. {
  172. fout << "CMAKE_EDIT_COMMAND = "
  173. << this->ShortPath(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  174. << "\n";
  175. }
  176. fout << "CMAKE_CURRENT_SOURCE = "
  177. << this->ShortPath(m_Makefile->GetStartDirectory() )
  178. << "\n";
  179. fout << "CMAKE_CURRENT_BINARY = "
  180. << this->ShortPath(m_Makefile->GetStartOutputDirectory())
  181. << "\n";
  182. fout << "CMAKE_SOURCE_DIR = "
  183. << this->ShortPath(m_Makefile->GetHomeDirectory()) << "\n";
  184. fout << "CMAKE_BINARY_DIR = "
  185. << this->ShortPath(m_Makefile->GetHomeOutputDirectory() )
  186. << "\n";
  187. // Output Include paths
  188. fout << "INCLUDE_FLAGS = ";
  189. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  190. std::vector<std::string>::iterator i;
  191. fout << "-I" <<
  192. this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << " ";
  193. for(i = includes.begin(); i != includes.end(); ++i)
  194. {
  195. std::string include = *i;
  196. // Don't output a -I for the standard include path "/usr/include".
  197. // This can cause problems with certain standard library
  198. // implementations because the wrong headers may be found first.
  199. fout << "-I" << this->ConvertToOutputPath(i->c_str()).c_str() << " ";
  200. }
  201. fout << m_Makefile->GetDefineFlags();
  202. fout << "\n\n";
  203. }
  204. void cmNMakeMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  205. const char* directory,
  206. const char* target1,
  207. const char* target2,
  208. bool silent)
  209. {
  210. if(target1)
  211. {
  212. std::string dir = this->ConvertToOutputPath(directory);
  213. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  214. << " "
  215. << "$(MAKE) $(MAKESILENT) rebuild_cache\n";
  216. if (!silent)
  217. {
  218. fout << "\techo " << directory << ": building " << target1 << "\n";
  219. }
  220. fout << "\tcd " << dir << "\n"
  221. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  222. }
  223. if(target2)
  224. {
  225. if (!silent)
  226. {
  227. fout << "\techo " << directory << ": building " << target2 << "\n";
  228. }
  229. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  230. }
  231. std::string currentDir = m_Makefile->GetCurrentOutputDirectory();
  232. fout << "\tcd " << this->ConvertToOutputPath(currentDir.c_str()) << "\n\n";
  233. }
  234. // This needs to be overriden because nmake requires commands to be quoted
  235. // if the are full paths to the executable????
  236. void cmNMakeMakefileGenerator::OutputMakeRule(std::ostream& fout,
  237. const char* comment,
  238. const char* target,
  239. const char* depends,
  240. const char* command,
  241. const char* command2,
  242. const char* command3,
  243. const char* command4)
  244. {
  245. std::string short_command;
  246. if (command)
  247. {
  248. short_command = ShortPathCommand(command);
  249. command = short_command.c_str();
  250. }
  251. std::string short_command2;
  252. if (command2)
  253. {
  254. short_command2 = ShortPathCommand(command2);
  255. command2 = short_command2.c_str();
  256. }
  257. std::string short_command3;
  258. if (command3)
  259. {
  260. short_command3 = ShortPathCommand(command3);
  261. command3 = short_command3.c_str();
  262. }
  263. std::string short_command4;
  264. if (command4)
  265. {
  266. short_command4 = ShortPathCommand(command4);
  267. command4 = short_command4.c_str();
  268. }
  269. cmUnixMakefileGenerator::OutputMakeRule(fout,
  270. comment,
  271. target,
  272. depends,
  273. command,
  274. command2,
  275. command3,
  276. command4);
  277. return;
  278. }
  279. void
  280. cmNMakeMakefileGenerator::
  281. OutputBuildObjectFromSource(std::ostream& fout,
  282. const char* shortName,
  283. const cmSourceFile& source,
  284. const char* extraCompileFlags,
  285. bool shared)
  286. {
  287. // Header files shouldn't have build rules.
  288. if(source.GetPropertyAsBool("HEADER_FILE_ONLY"))
  289. {
  290. return;
  291. }
  292. std::string comment = "Build ";
  293. std::string objectFile = std::string(shortName) +
  294. this->GetOutputExtension(source.GetSourceExtension().c_str());
  295. comment += objectFile + " From ";
  296. comment += source.GetFullPath();
  297. std::string compileCommand;
  298. std::string ext = source.GetSourceExtension();
  299. if(ext == "c" )
  300. {
  301. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  302. compileCommand += extraCompileFlags;
  303. if(shared)
  304. {
  305. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  306. }
  307. compileCommand += "$(INCLUDE_FLAGS) -c ";
  308. compileCommand +=
  309. this->ConvertToOutputPath(source.GetFullPath().c_str());
  310. // Need to get the definition here because this value might have
  311. // trailing space (since it is directly prepended to the filename)
  312. std::string output_object_file_flag =
  313. m_Makefile->GetDefinition("CMAKE_C_OUTPUT_OBJECT_FILE_FLAG");
  314. m_Makefile->ExpandVariablesInString(output_object_file_flag);
  315. compileCommand += " " + output_object_file_flag;
  316. compileCommand += objectFile;
  317. }
  318. else if (ext == "rc")
  319. {
  320. compileCommand = "$(RC) /fo\"";
  321. compileCommand += objectFile;
  322. compileCommand += "\" ";
  323. compileCommand +=
  324. this->ConvertToOutputPath(source.GetFullPath().c_str());
  325. }
  326. else if (ext == "def")
  327. {
  328. // no rule to output for this one
  329. return;
  330. }
  331. // assume c++ if not c rc or def
  332. else
  333. {
  334. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  335. compileCommand += extraCompileFlags;
  336. if(shared)
  337. {
  338. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  339. }
  340. compileCommand += "$(INCLUDE_FLAGS) -c ";
  341. compileCommand +=
  342. this->ConvertToOutputPath(source.GetFullPath().c_str());
  343. // Need to get the definition here because this value might have
  344. // trailing space (since it is directly prepended to the filename)
  345. std::string output_object_file_flag =
  346. m_Makefile->GetDefinition("CMAKE_C_OUTPUT_OBJECT_FILE_FLAG");
  347. m_Makefile->ExpandVariablesInString(output_object_file_flag);
  348. compileCommand += " " + output_object_file_flag;
  349. compileCommand += objectFile;
  350. }
  351. this->OutputMakeRule(fout,
  352. comment.c_str(),
  353. objectFile.c_str(),
  354. this->ConvertToOutputPath(
  355. source.GetFullPath().c_str()).c_str(),
  356. compileCommand.c_str());
  357. }
  358. void cmNMakeMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  359. const char* name,
  360. const cmTarget &t)
  361. {
  362. std::string target = m_LibraryOutputPath + name + m_SharedLibraryExtension;
  363. std::string depend = "$(";
  364. depend += this->CreateMakeVariable(name, "_SRC_OBJS");
  365. depend += ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  366. // Need to get the definition here because this value might have
  367. // trailing space (since it is directly prepended to the filename)
  368. std::string linker_output_file_flag =
  369. m_Makefile->GetDefinition("CMAKE_LINKER_OUTPUT_FILE_FLAG");
  370. m_Makefile->ExpandVariablesInString(linker_output_file_flag);
  371. std::string command = "$(CMAKE_LINKER) $(CMAKE_LINKER_SHARED_LIBRARY_FLAG)";
  372. bool hide_param = m_Makefile->IsOn("CMAKE_LINKER_HIDE_PARAMETERS");
  373. if (hide_param)
  374. {
  375. command += " @<<\n\t";
  376. }
  377. command += " $(CMAKE_LINKER_FLAGS) " + linker_output_file_flag;
  378. std::string dllpath = m_LibraryOutputPath + std::string(name) + m_SharedLibraryExtension;
  379. command += this->ConvertToOutputPath(dllpath.c_str());
  380. command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  381. cmStringStream linklibs;
  382. this->OutputLinkLibraries(linklibs, name, t);
  383. command += linklibs.str();
  384. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  385. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  386. i != sources.end(); ++i)
  387. {
  388. if((*i)->GetSourceExtension() == "def")
  389. {
  390. command += "/DEF:";
  391. command += (*i)->GetFullPath();
  392. }
  393. }
  394. command += "\n";
  395. if (hide_param)
  396. {
  397. command += "<<\n";
  398. }
  399. std::string customCommands = this->CreateTargetRules(t, name);
  400. const char* cc = 0;
  401. if(customCommands.size() > 0)
  402. {
  403. cc = customCommands.c_str();
  404. }
  405. this->OutputMakeRule(fout, "rules for a shared library",
  406. target.c_str(),
  407. depend.c_str(),
  408. command.c_str(), cc);
  409. }
  410. void cmNMakeMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  411. const char* name,
  412. const cmTarget &target)
  413. {
  414. this->OutputSharedLibraryRule(fout, name, target);
  415. }
  416. void cmNMakeMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  417. const char* name,
  418. const cmTarget &t)
  419. {
  420. std::string target = m_LibraryOutputPath + std::string(name) + m_StaticLibraryExtension;
  421. std::string depend = "$(";
  422. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  423. // Need to get the definition here because this value might have
  424. // trailing space (since it is directly prepended to the filename)
  425. std::string library_manager_output_file_flag =
  426. m_Makefile->GetDefinition("CMAKE_LIBRARY_MANAGER_OUTPUT_FILE_FLAG");
  427. m_Makefile->ExpandVariablesInString(library_manager_output_file_flag);
  428. std::string command = "$(CMAKE_LIBRARY_MANAGER) $(CMAKE_LIBRARY_MANAGER_FLAGS) @<<\n\t " + library_manager_output_file_flag;
  429. std::string libpath = m_LibraryOutputPath + std::string(name) + m_StaticLibraryExtension;
  430. command += this->ConvertToOutputPath(libpath.c_str());
  431. command += " $(";
  432. command += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  433. command += "\n<<\n";
  434. std::string comment = "rule to build static library: ";
  435. comment += name;
  436. std::string customCommands = this->CreateTargetRules(t, name);
  437. const char* cc = 0;
  438. if(customCommands.size() > 0)
  439. {
  440. cc = customCommands.c_str();
  441. }
  442. this->OutputMakeRule(fout,
  443. comment.c_str(),
  444. target.c_str(),
  445. depend.c_str(),
  446. command.c_str(), cc);
  447. }
  448. void cmNMakeMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  449. const char* name,
  450. const cmTarget &t)
  451. {
  452. std::string target = m_ExecutableOutputPath + name;
  453. target += m_ExecutableExtension;
  454. std::string depend = "$(";
  455. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") $(" +
  456. this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  457. std::string command;
  458. if(t.HasCxx())
  459. {
  460. command = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  461. }
  462. else
  463. {
  464. command = "${CMAKE_C_COMPILER} $(CMAKE_C_FLAGS) ";
  465. }
  466. bool hide_param = m_Makefile->IsOn("CMAKE_LINKER_HIDE_PARAMETERS");
  467. if (hide_param)
  468. {
  469. command += " @<<\n\t";
  470. }
  471. command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  472. std::string path = m_ExecutableOutputPath + name + m_ExecutableExtension;
  473. // Need to get the definition here because this value might have
  474. // trailing space (since it is directly prepended to the filename)
  475. std::string output_executable_file_flag =
  476. m_Makefile->GetDefinition("CMAKE_C_OUTPUT_EXECUTABLE_FILE_FLAG");
  477. m_Makefile->ExpandVariablesInString(output_executable_file_flag);
  478. command += " " + output_executable_file_flag +
  479. this->ConvertToOutputPath(path.c_str());
  480. command += " $(CMAKE_C_LINK_EXECUTABLE_FLAG) $(CMAKE_LINKER_FLAGS) ";
  481. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  482. {
  483. command += " /subsystem:windows ";
  484. }
  485. cmStringStream linklibs;
  486. this->OutputLinkLibraries(linklibs, 0, t);
  487. command += linklibs.str();
  488. std::string comment = "rule to build executable: ";
  489. comment += name;
  490. std::string customCommands = this->CreateTargetRules(t, name);
  491. const char* cc = 0;
  492. if(customCommands.size() > 0)
  493. {
  494. cc = customCommands.c_str();
  495. }
  496. if (hide_param)
  497. {
  498. command += "\n";
  499. command += "<<\n";
  500. }
  501. this->OutputMakeRule(fout,
  502. comment.c_str(),
  503. target.c_str(),
  504. depend.c_str(),
  505. command.c_str(), cc);
  506. }
  507. void cmNMakeMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  508. const char* targetLibrary,
  509. const cmTarget &tgt)
  510. {
  511. // Try to emit each search path once
  512. std::set<std::string> emitted;
  513. // Embed runtime search paths if possible and if required.
  514. // collect all the flags needed for linking libraries
  515. // Do not try if there is no library path option (it is set to -L or
  516. // -LIBPATH for some linker, but some others do not even support link
  517. // search path).
  518. std::string linkLibs;
  519. // Expand content because this value might have
  520. // trailing space (since it is directly prepended to the filename)
  521. std::string lib_path_opt = m_LibraryPathOption;
  522. m_Makefile->ExpandVariablesInString(lib_path_opt);
  523. if (lib_path_opt.size())
  524. {
  525. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  526. for(std::vector<std::string>::const_iterator libDir = libdirs.begin();
  527. libDir != libdirs.end(); ++libDir)
  528. {
  529. std::string libpath = ShortPath(libDir->c_str());
  530. if(emitted.insert(libpath).second)
  531. {
  532. linkLibs += lib_path_opt;
  533. this->ConvertToOutputPath(libpath.c_str());
  534. linkLibs += libpath;
  535. linkLibs += " ";
  536. }
  537. }
  538. }
  539. std::string librariesLinked;
  540. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  541. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  542. lib != libs.end(); ++lib)
  543. {
  544. // Don't link the library against itself!
  545. if(targetLibrary && (lib->first == targetLibrary)) continue;
  546. // ** should fix this later, it should check to see if this is
  547. // a debug build and add the library
  548. // don't look at debug libraries
  549. // if (lib->second == cmTarget::DEBUG) continue;
  550. // skip zero size library entries, this may happen
  551. // if a variable expands to nothing.
  552. if (lib->first.size() == 0) continue;
  553. if(emitted.insert(lib->first).second)
  554. {
  555. std::string regexp = ".*\\";
  556. regexp += m_Makefile->GetDefinition("CMAKE_STATICLIB_SUFFIX");
  557. regexp += "$";
  558. cmRegularExpression reg(regexp.c_str());
  559. // if it ends in .lib, then it is a full path and should
  560. // be escaped, and does not need .lib added
  561. if(reg.find(lib->first))
  562. {
  563. librariesLinked += ShortPath(lib->first.c_str());
  564. librariesLinked += " ";
  565. }
  566. else
  567. {
  568. librariesLinked += m_LibraryLinkOption;
  569. librariesLinked += lib->first;
  570. librariesLinked += m_StaticLibraryExtension + " ";
  571. }
  572. }
  573. }
  574. linkLibs += librariesLinked;
  575. fout << linkLibs;
  576. fout << "$(CMAKE_STANDARD_WINDOWS_LIBRARIES) ";
  577. }
  578. std::string cmNMakeMakefileGenerator::GetOutputExtension(const char* s)
  579. {
  580. std::string sourceExtension = s;
  581. if(sourceExtension == "def")
  582. {
  583. return "";
  584. }
  585. if(sourceExtension == "ico" || sourceExtension == "rc2")
  586. {
  587. return "";
  588. }
  589. if(sourceExtension == "rc")
  590. {
  591. return ".res";
  592. }
  593. return m_ObjectFileExtension;
  594. }
  595. void cmNMakeMakefileGenerator::OutputIncludeMakefile(std::ostream& fout,
  596. const char* file)
  597. {
  598. fout << "!include " << file << "\n";
  599. }
  600. bool cmNMakeMakefileGenerator::SamePath(const char* path1, const char* path2)
  601. {
  602. // first check to see if they are the same anyway
  603. if (strcmp(path1, path2) == 0)
  604. {
  605. return true;
  606. }
  607. // next short path and lower case both of them for the compare
  608. return
  609. cmSystemTools::LowerCase(ShortPath(path1)) ==
  610. cmSystemTools::LowerCase(ShortPath(path2));
  611. }
  612. void cmNMakeMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  613. const char* path,
  614. const char* library,
  615. const char* fullpath,
  616. const char* libOutPath)
  617. {
  618. const char* makeTarget = library;
  619. std::string currentDir =
  620. this->ConvertToOutputPath(m_Makefile->GetCurrentOutputDirectory());
  621. std::string wpath = this->ConvertToOutputPath(path);
  622. std::string wfullpath = this->ConvertToOutputPath(fullpath);
  623. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  624. {
  625. makeTarget = wfullpath.c_str();
  626. }
  627. fout << wfullpath
  628. << ":\n\tcd " << wpath << "\n"
  629. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  630. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  631. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  632. << "\t$(MAKE) $(MAKESILENT) " << makeTarget
  633. << "\n\tcd " << currentDir << "\n";
  634. }
  635. std::string cmNMakeMakefileGenerator::ConvertToOutputPath(const char* s)
  636. {
  637. return cmSystemTools::ConvertToOutputPath(s);
  638. }
  639. std::string cmNMakeMakefileGenerator::CreateMakeVariable(const char* s, const char* s2)
  640. {
  641. std::string ret= std::string(s) + std::string(s2);
  642. cmSystemTools::ReplaceString(ret, "-", "_");
  643. return ret;
  644. }
  645. std::string cmNMakeMakefileGenerator::LowerCasePath(const char* path)
  646. {
  647. return cmSystemTools::LowerCase(path);
  648. }