cmNMakeMakefileGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 "cmNMakeMakefileGenerator.h"
  33. #include "cmMakefile.h"
  34. #include "cmStandardIncludes.h"
  35. #include "cmSystemTools.h"
  36. #include "cmSourceFile.h"
  37. #include "cmMakeDepend.h"
  38. #include "cmCacheManager.h"
  39. #include "cmGeneratedFileStream.h"
  40. cmNMakeMakefileGenerator::cmNMakeMakefileGenerator()
  41. {
  42. this->SetObjectFileExtension(".obj");
  43. this->SetExecutableExtension(".exe");
  44. this->SetLibraryPrefix("");
  45. this->SetSharedLibraryExtension(".dll");
  46. this->SetStaticLibraryExtension(".lib");
  47. m_QuoteNextCommand = true; // most of the time command should be quoted
  48. }
  49. cmNMakeMakefileGenerator::~cmNMakeMakefileGenerator()
  50. {
  51. }
  52. void cmNMakeMakefileGenerator::ComputeSystemInfo()
  53. {
  54. // now load the settings
  55. if(!m_Makefile->GetDefinition("CMAKE_ROOT"))
  56. {
  57. cmSystemTools::Error(
  58. "CMAKE_ROOT has not been defined, bad GUI or driver program");
  59. return;
  60. }
  61. std::string fpath =
  62. m_Makefile->GetDefinition("CMAKE_ROOT");
  63. fpath += "/Templates/CMakeNMakeWindowsSystemConfig.cmake";
  64. m_Makefile->ReadListFile(NULL,fpath.c_str());
  65. }
  66. void cmNMakeMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  67. {
  68. fout << "# NMake Makefile generated by cmake\n";
  69. const char* variables =
  70. "# general varibles used in the makefile\n"
  71. "\n"
  72. "# Path to cmake\n"
  73. "CMAKE_COMMAND = ${CMAKE_COMMAND}\n"
  74. "CMAKE_STANDARD_WINDOWS_LIBRARIES = @CMAKE_STANDARD_WINDOWS_LIBRARIES@\n"
  75. "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@ \n"
  76. "CMAKE_CFLAGS = @CMAKE_CFLAGS@ @BUILD_FLAGS@\n"
  77. "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
  78. "CMAKE_CXXFLAGS = @CMAKE_CXX_FLAGS@ @BUILD_FLAGS@\n";
  79. std::string buildType = "CMAKE_CXX_FLAGS_";
  80. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  81. buildType = cmSystemTools::UpperCase(buildType);
  82. m_Makefile->AddDefinition("BUILD_FLAGS",
  83. m_Makefile->GetDefinition(
  84. buildType.c_str()));
  85. std::string replaceVars = variables;
  86. m_Makefile->ExpandVariablesInString(replaceVars);
  87. fout << replaceVars.c_str();
  88. fout << "CMAKE_CURRENT_SOURCE = " << m_Makefile->GetStartDirectory()
  89. << "\n";
  90. fout << "CMAKE_CURRENT_BINARY = " << m_Makefile->GetStartOutputDirectory()
  91. << "\n";
  92. fout << "CMAKE_SOURCE_DIR = " << m_Makefile->GetHomeDirectory() << "\n";
  93. fout << "CMAKE_BINARY_DIR = " << m_Makefile->GetHomeOutputDirectory()
  94. << "\n";
  95. // Output Include paths
  96. fout << "INCLUDE_FLAGS = ";
  97. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  98. std::vector<std::string>::iterator i;
  99. fout << "-I" << m_Makefile->GetStartDirectory() << " ";
  100. for(i = includes.begin(); i != includes.end(); ++i)
  101. {
  102. std::string include = *i;
  103. // Don't output a -I for the standard include path "/usr/include".
  104. // This can cause problems with certain standard library
  105. // implementations because the wrong headers may be found first.
  106. if(include != "/usr/include")
  107. {
  108. fout << "-I" << cmSystemTools::EscapeSpaces(i->c_str()).c_str() << " ";
  109. }
  110. }
  111. fout << m_Makefile->GetDefineFlags();
  112. fout << "\n\n";
  113. }
  114. void cmNMakeMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  115. const char* directory,
  116. const char* target1,
  117. const char* target2)
  118. {
  119. if(target1)
  120. {
  121. std::string dir = directory;
  122. cmSystemTools::ConvertToWindowsSlashes(dir);
  123. fout << "\tif not exist " << dir.c_str() << " "
  124. << "$(MAKE) rebuild_cache\n"
  125. << "\tcd .\\" << directory << "\n"
  126. << "\t$(MAKE) -$(MAKEFLAGS) " << target1 << "\n";
  127. }
  128. if(target2)
  129. {
  130. fout << "\t$(MAKE) -$(MAKEFLAGS) " << target2 << "\n";
  131. }
  132. std::string currentDir = m_Makefile->GetCurrentOutputDirectory();
  133. cmSystemTools::ConvertToWindowsSlashes(currentDir);
  134. fout << "\tcd " << currentDir.c_str() << "\n";
  135. }
  136. // This needs to be overriden because nmake requires commands to be quoted
  137. // if the are full paths to the executable????
  138. void cmNMakeMakefileGenerator::OutputMakeRule(std::ostream& fout,
  139. const char* comment,
  140. const char* target,
  141. const char* depends,
  142. const char* command,
  143. const char* command2,
  144. const char* command3,
  145. const char* command4)
  146. {
  147. if(!target)
  148. {
  149. cmSystemTools::Error("no target for OutputMakeRule");
  150. return;
  151. }
  152. std::string replace;
  153. if(comment)
  154. {
  155. replace = comment;
  156. m_Makefile->ExpandVariablesInString(replace);
  157. fout << "#---------------------------------------------------------\n";
  158. fout << "# " << comment;
  159. fout << "\n#\n";
  160. }
  161. fout << "\n";
  162. replace = target;
  163. m_Makefile->ExpandVariablesInString(replace);
  164. fout << replace.c_str() << ": ";
  165. if(depends)
  166. {
  167. replace = depends;
  168. m_Makefile->ExpandVariablesInString(replace);
  169. fout << replace.c_str();
  170. }
  171. fout << "\n";
  172. const char* startCommand = "\t\"";
  173. const char* endCommand = "\"\n";
  174. if(!m_QuoteNextCommand)
  175. {
  176. startCommand = "\t";
  177. endCommand = "\n";
  178. }
  179. if(command)
  180. {
  181. replace = command;
  182. m_Makefile->ExpandVariablesInString(replace);
  183. fout << startCommand << replace.c_str() << endCommand;
  184. }
  185. if(command2)
  186. {
  187. replace = command2;
  188. m_Makefile->ExpandVariablesInString(replace);
  189. fout << startCommand << replace.c_str() << endCommand;
  190. }
  191. if(command3)
  192. {
  193. replace = command3;
  194. m_Makefile->ExpandVariablesInString(replace);
  195. fout << startCommand << replace.c_str() << endCommand;
  196. }
  197. if(command4)
  198. {
  199. replace = command4;
  200. m_Makefile->ExpandVariablesInString(replace);
  201. fout << startCommand << replace.c_str() << endCommand;
  202. }
  203. fout << "\n";
  204. // reset m_QuoteNextCommand, as the default should be to quote the
  205. // commands. We need the quotes when the command has a full path
  206. // to an executable. However, the quotes break things like the
  207. // linker command.
  208. m_QuoteNextCommand = true;
  209. }
  210. void
  211. cmNMakeMakefileGenerator::
  212. OutputBuildObjectFromSource(std::ostream& fout,
  213. const char* shortName,
  214. const cmSourceFile& source,
  215. const char* extraCompileFlags,
  216. bool shared)
  217. {
  218. std::string comment = "Build ";
  219. std::string objectFile = std::string(shortName) +
  220. this->GetOutputExtension(source.GetSourceExtension().c_str());
  221. comment += objectFile + " From ";
  222. comment += source.GetFullPath();
  223. std::string compileCommand;
  224. std::string ext = source.GetSourceExtension();
  225. if(ext == "c" )
  226. {
  227. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_CFLAGS) ";
  228. compileCommand += extraCompileFlags;
  229. if(shared)
  230. {
  231. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  232. }
  233. compileCommand += "$(INCLUDE_FLAGS) -c ";
  234. compileCommand += source.GetFullPath();
  235. compileCommand += " /Fo";
  236. compileCommand += objectFile;
  237. }
  238. else if (ext == "rc")
  239. {
  240. compileCommand = "$(RC) /fo\"";
  241. compileCommand += objectFile;
  242. compileCommand += "\" ";
  243. compileCommand += source.GetFullPath();
  244. }
  245. else if (ext == "def")
  246. {
  247. // *** do something here??
  248. }
  249. else if (ext == "ico")
  250. {
  251. // do something here??
  252. }
  253. else if (ext == "rc2")
  254. {
  255. // do something here??
  256. }
  257. // assume c++ if not c rc or def
  258. else
  259. {
  260. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXXFLAGS) ";
  261. compileCommand += extraCompileFlags;
  262. if(shared)
  263. {
  264. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  265. }
  266. compileCommand += "$(INCLUDE_FLAGS) -c ";
  267. compileCommand += source.GetFullPath();
  268. compileCommand += " /Fo";
  269. compileCommand += objectFile;
  270. }
  271. m_QuoteNextCommand = false;
  272. this->OutputMakeRule(fout,
  273. comment.c_str(),
  274. objectFile.c_str(),
  275. source.GetFullPath().c_str(),
  276. compileCommand.c_str());
  277. }
  278. void cmNMakeMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  279. const char* name,
  280. const cmTarget &t)
  281. {
  282. std::string target = m_LibraryOutputPath + name + ".dll";
  283. std::string depend = "$(";
  284. depend += name;
  285. depend += "_SRC_OBJS) $(" + std::string(name) + "_DEPEND_LIBS)";
  286. std::string command = "link /dll @<<\n";
  287. command += "$(" + std::string(name) + "_SRC_OBJS) /out:";
  288. command += m_LibraryOutputPath + std::string(name) + ".dll ";
  289. std::strstream linklibs;
  290. this->OutputLinkLibraries(linklibs, name, t);
  291. linklibs << std::ends;
  292. command += linklibs.str();
  293. delete [] linklibs.str();
  294. command += "\n<<\n";
  295. m_QuoteNextCommand = false;
  296. this->OutputMakeRule(fout, "rules for a shared library",
  297. target.c_str(),
  298. depend.c_str(),
  299. command.c_str());
  300. }
  301. void cmNMakeMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  302. const char* name,
  303. const cmTarget &target)
  304. {
  305. this->OutputSharedLibraryRule(fout, name, target);
  306. }
  307. void cmNMakeMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  308. const char* name,
  309. const cmTarget &)
  310. {
  311. std::string target = m_LibraryOutputPath + std::string(name) + ".lib";
  312. std::string depend = "$(";
  313. depend += std::string(name) + "_SRC_OBJS)";
  314. std::string command = "link -lib @<<\n\t/nologo /out:";
  315. command += m_LibraryOutputPath;
  316. command += name;
  317. command += ".lib $(";
  318. command += std::string(name) + "_SRC_OBJS)";
  319. command += "\n<<\n";
  320. std::string comment = "rule to build static library: ";
  321. comment += name;
  322. m_QuoteNextCommand = false;
  323. this->OutputMakeRule(fout,
  324. comment.c_str(),
  325. target.c_str(),
  326. depend.c_str(),
  327. command.c_str());
  328. }
  329. void cmNMakeMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  330. const char* name,
  331. const cmTarget &t)
  332. {
  333. std::string target = m_ExecutableOutputPath + name;
  334. target += ".exe";
  335. std::string depend = "$(";
  336. depend += std::string(name) + "_SRC_OBJS) $(" + std::string(name) + "_DEPEND_LIBS)";
  337. std::string command =
  338. "$(CMAKE_CXX_COMPILER) $(CMAKE_CXXFLAGS) ";
  339. command += "$(" + std::string(name) + "_SRC_OBJS) ";
  340. command += " /Fe" + m_ExecutableOutputPath + name;
  341. command += ".exe /link ";
  342. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  343. {
  344. command += " /subsystem:windows ";
  345. }
  346. std::strstream linklibs;
  347. this->OutputLinkLibraries(linklibs, 0, t);
  348. linklibs << std::ends;
  349. command += linklibs.str();
  350. std::string comment = "rule to build executable: ";
  351. comment += name;
  352. m_QuoteNextCommand = false;
  353. this->OutputMakeRule(fout,
  354. comment.c_str(),
  355. target.c_str(),
  356. depend.c_str(),
  357. command.c_str());
  358. }
  359. void cmNMakeMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  360. const char* targetLibrary,
  361. const cmTarget &tgt)
  362. {
  363. // Try to emit each search path once
  364. std::set<std::string> emitted;
  365. // Embed runtime search paths if possible and if required.
  366. // collect all the flags needed for linking libraries
  367. std::string linkLibs;
  368. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  369. for(std::vector<std::string>::iterator libDir = libdirs.begin();
  370. libDir != libdirs.end(); ++libDir)
  371. {
  372. std::string libpath = cmSystemTools::EscapeSpaces(libDir->c_str());
  373. if(emitted.insert(libpath).second)
  374. {
  375. linkLibs += "-LIBPATH:";
  376. linkLibs += libpath;
  377. linkLibs += " ";
  378. }
  379. }
  380. std::string librariesLinked;
  381. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  382. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  383. lib != libs.end(); ++lib)
  384. {
  385. // Don't link the library against itself!
  386. if(targetLibrary && (lib->first == targetLibrary)) continue;
  387. // ** should fix this later, it should check to see if this is
  388. // a debug build and add the library
  389. // don't look at debug libraries
  390. // if (lib->second == cmTarget::DEBUG) continue;
  391. // skip zero size library entries, this may happen
  392. // if a variable expands to nothing.
  393. if (lib->first.size() == 0) continue;
  394. // if it is a full path break it into -L and -l
  395. if(emitted.insert(lib->first).second)
  396. {
  397. linkLibs += lib->first;
  398. linkLibs += ".lib ";
  399. }
  400. linkLibs += librariesLinked;
  401. fout << linkLibs << "$(CMAKE_STANDARD_WINDOWS_LIBRARIES) ";
  402. }
  403. }
  404. std::string cmNMakeMakefileGenerator::GetOutputExtension(const char* s)
  405. {
  406. std::string sourceExtension = s;
  407. if(sourceExtension == "def" || sourceExtension == "ico" || sourceExtension == "rc2")
  408. {
  409. return "";
  410. }
  411. if(sourceExtension == "rc")
  412. {
  413. return ".res";
  414. }
  415. return ".obj";
  416. }
  417. void cmNMakeMakefileGenerator::OutputIncludeMakefile(std::ostream& fout,
  418. const char* file)
  419. {
  420. fout << "!include " << file << "\n";
  421. }