cmBorlandMakefileGenerator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 "cmBorlandMakefileGenerator.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. cmBorlandMakefileGenerator::cmBorlandMakefileGenerator()
  23. {
  24. this->SetLibraryPathOption("-L");
  25. this->SetLibraryLinkOption("");
  26. }
  27. cmBorlandMakefileGenerator::~cmBorlandMakefileGenerator()
  28. {
  29. }
  30. void cmBorlandMakefileGenerator::ComputeSystemInfo()
  31. {
  32. // now load the settings
  33. if(!m_Makefile->GetDefinition("CMAKE_ROOT"))
  34. {
  35. cmSystemTools::Error(
  36. "CMAKE_ROOT has not been defined, bad GUI or driver program");
  37. return;
  38. }
  39. std::string outdir = m_Makefile->GetCurrentOutputDirectory();
  40. if(outdir.find('-') != std::string::npos)
  41. {
  42. std::string message = "The Borland command line tools do not support path names that have - in them. Please re-name your output directory and use _ instead of -.";
  43. message += "\nYour path currently is: ";
  44. message += outdir;
  45. cmSystemTools::Error(message.c_str());
  46. }
  47. std::string fpath =
  48. m_Makefile->GetDefinition("CMAKE_ROOT");
  49. fpath += "/Templates/CMakeBorlandWindowsSystemConfig.cmake";
  50. m_Makefile->ReadListFile(NULL,fpath.c_str());
  51. }
  52. void cmBorlandMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  53. {
  54. fout << "# NMake Makefile generated by cmake\n";
  55. const char* variables =
  56. "# general varibles used in the makefile\n"
  57. "\n"
  58. "CMAKE_STANDARD_WINDOWS_LIBRARIES = @CMAKE_STANDARD_WINDOWS_LIBRARIES@\n"
  59. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@ @BUILD_FLAGS@\n"
  60. "CMAKE_OBJECT_FILE_SUFFIX = @CMAKE_OBJECT_FILE_SUFFIX@\n"
  61. "CMAKE_EXECUTABLE_SUFFIX = @CMAKE_EXECUTABLE_SUFFIX@\n"
  62. "CMAKE_STATICLIB_SUFFIX = @CMAKE_STATICLIB_SUFFIX@\n"
  63. "CMAKE_SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  64. "CMAKE_SHLIB_CFLAGS = -tWR\n"
  65. "CMAKE_LINKER_FLAGS = @CMAKE_LINKER_FLAGS@ @LINKER_BUILD_FLAGS@\n"
  66. "CMAKE_CXX_FLAGS = -P @CMAKE_CXX_FLAGS@ @BUILD_FLAGS@\n"
  67. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  68. "NULL=\n"
  69. "!ELSE \n"
  70. "NULL=nul\n"
  71. "!ENDIF \n"
  72. "RM = del\n";
  73. std::string buildType = "CMAKE_CXX_FLAGS_";
  74. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  75. buildType = cmSystemTools::UpperCase(buildType);
  76. m_Makefile->AddDefinition("BUILD_FLAGS",
  77. m_Makefile->GetDefinition(
  78. buildType.c_str()));
  79. buildType = "CMAKE_LINKER_FLAGS_";
  80. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  81. buildType = cmSystemTools::UpperCase(buildType);
  82. m_Makefile->AddDefinition("LINKER_BUILD_FLAGS",
  83. m_Makefile->GetDefinition(
  84. buildType.c_str()));
  85. std::string replaceVars = variables;
  86. m_Makefile->ExpandVariablesInString(replaceVars);
  87. std::string ccompiler = m_Makefile->GetDefinition("CMAKE_C_COMPILER");
  88. cmSystemTools::ConvertToWindowsSlashes(ccompiler);
  89. fout << "CMAKE_C_COMPILER = " << cmSystemTools::EscapeSpaces(ccompiler.c_str())
  90. << "\n";
  91. std::string cxxcompiler = m_Makefile->GetDefinition("CMAKE_CXX_COMPILER");
  92. cmSystemTools::ConvertToWindowsSlashes(cxxcompiler);
  93. fout << "CMAKE_CXX_COMPILER = " << cmSystemTools::EscapeSpaces(cxxcompiler.c_str())
  94. << "\n";
  95. std::string cmakecommand = m_Makefile->GetDefinition("CMAKE_COMMAND");
  96. cmSystemTools::ConvertToWindowsSlashes(cmakecommand);
  97. fout << "CMAKE_COMMAND = " << cmSystemTools::EscapeSpaces(cmakecommand.c_str()) << "\n";
  98. fout << replaceVars.c_str();
  99. fout << "CMAKE_CURRENT_SOURCE = "
  100. << ShortPath(m_Makefile->GetStartDirectory() )
  101. << "\n";
  102. fout << "CMAKE_CURRENT_BINARY = "
  103. << ShortPath(m_Makefile->GetStartOutputDirectory())
  104. << "\n";
  105. fout << "CMAKE_SOURCE_DIR = "
  106. << ShortPath(m_Makefile->GetHomeDirectory()) << "\n";
  107. fout << "CMAKE_BINARY_DIR = "
  108. << ShortPath(m_Makefile->GetHomeOutputDirectory() )
  109. << "\n";
  110. // Output Include paths
  111. fout << "INCLUDE_FLAGS = ";
  112. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  113. std::vector<std::string>::iterator i;
  114. fout << "-I" << cmSystemTools::EscapeSpaces(m_Makefile->GetStartDirectory()) << " ";
  115. for(i = includes.begin(); i != includes.end(); ++i)
  116. {
  117. std::string include = *i;
  118. // Don't output a -I for the standard include path "/usr/include".
  119. // This can cause problems with certain standard library
  120. // implementations because the wrong headers may be found first.
  121. if(include != "/usr/include")
  122. {
  123. fout << "-I" << cmSystemTools::EscapeSpaces(i->c_str()).c_str() << " ";
  124. }
  125. }
  126. fout << m_Makefile->GetDefineFlags();
  127. fout << "\n\n";
  128. }
  129. void
  130. cmBorlandMakefileGenerator::
  131. OutputBuildObjectFromSource(std::ostream& fout,
  132. const char* shortName,
  133. const cmSourceFile& source,
  134. const char* extraCompileFlags,
  135. bool shared)
  136. {
  137. // force shared flag if building shared libraries
  138. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  139. {
  140. shared = true;
  141. }
  142. // Header files shouldn't have build rules.
  143. if(source.IsAHeaderFileOnly())
  144. return;
  145. std::string comment = "Build ";
  146. std::string objectFile = std::string(shortName) +
  147. this->GetOutputExtension(source.GetSourceExtension().c_str());
  148. cmSystemTools::ConvertToWindowsSlashes(objectFile);
  149. comment += objectFile + " From ";
  150. comment += source.GetFullPath();
  151. std::string compileCommand;
  152. std::string ext = source.GetSourceExtension();
  153. if(ext == "c" )
  154. {
  155. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  156. compileCommand += extraCompileFlags;
  157. if(shared)
  158. {
  159. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  160. }
  161. compileCommand += " -o";
  162. compileCommand += objectFile;
  163. compileCommand += " $(INCLUDE_FLAGS) -c ";
  164. compileCommand +=
  165. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  166. }
  167. else if (ext == "rc")
  168. {
  169. compileCommand = "$(RC) -o\"";
  170. compileCommand += objectFile;
  171. compileCommand += "\" ";
  172. compileCommand +=
  173. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  174. }
  175. else if (ext == "def")
  176. {
  177. // no rule to output for this one
  178. return;
  179. }
  180. // assume c++ if not c rc or def
  181. else
  182. {
  183. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  184. compileCommand += extraCompileFlags;
  185. if(shared)
  186. {
  187. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  188. }
  189. compileCommand += " -o";
  190. compileCommand += objectFile;
  191. compileCommand += " $(INCLUDE_FLAGS) -c ";
  192. compileCommand +=
  193. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  194. }
  195. this->OutputMakeRule(fout,
  196. comment.c_str(),
  197. objectFile.c_str(),
  198. cmSystemTools::EscapeSpaces(
  199. source.GetFullPath().c_str()).c_str(),
  200. compileCommand.c_str());
  201. }
  202. void cmBorlandMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  203. const char* name,
  204. const cmTarget &t)
  205. {
  206. std::string target = m_LibraryOutputPath + name;
  207. std::string libpath = target + ".lib";
  208. target += ".dll";
  209. cmSystemTools::ConvertToWindowsSlashes(libpath);
  210. cmSystemTools::ConvertToWindowsSlashes(target);
  211. target = cmSystemTools::EscapeSpaces(target.c_str());
  212. libpath = cmSystemTools::EscapeSpaces(libpath.c_str());
  213. std::string depend = "$(";
  214. depend += name;
  215. depend += "_SRC_OBJS) $(" + std::string(name) + "_DEPEND_LIBS)";
  216. std::string command = "$(CMAKE_CXX_COMPILER) -tWD $(CMAKE_SHLIB_CFLAGS) $(CMAKE_LINKER_FLAGS) @&&|\n";
  217. // must be executable name
  218. command += "-e";
  219. command += target;
  220. command += " ";
  221. std::strstream linklibs;
  222. this->OutputLinkLibraries(linklibs, name, t);
  223. linklibs << std::ends;
  224. // then the linker options -L and libraries (any other order will fail!)
  225. command += linklibs.str();
  226. delete [] linklibs.str();
  227. // then list of object files
  228. command += " $(" + std::string(name) + "_SRC_OBJS) ";
  229. std::string command2 = "implib -w ";
  230. command2 += libpath + " " + target;
  231. const std::vector<cmSourceFile>& sources = t.GetSourceFiles();
  232. for(std::vector<cmSourceFile>::const_iterator i = sources.begin();
  233. i != sources.end(); ++i)
  234. {
  235. if(i->GetSourceExtension() == "def")
  236. {
  237. command += "";
  238. command += i->GetFullPath();
  239. }
  240. }
  241. command += "\n|\n";
  242. std::string customCommands = this->CreateTargetRules(t, name);
  243. const char* cc = 0;
  244. if(customCommands.size() > 0)
  245. {
  246. cc = customCommands.c_str();
  247. }
  248. this->OutputMakeRule(fout, "rules for a shared library",
  249. target.c_str(),
  250. depend.c_str(),
  251. command.c_str(),
  252. command2.c_str(),
  253. cc);
  254. }
  255. void cmBorlandMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  256. const char* name,
  257. const cmTarget &target)
  258. {
  259. this->OutputSharedLibraryRule(fout, name, target);
  260. }
  261. void cmBorlandMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  262. const char* name,
  263. const cmTarget &t)
  264. {
  265. std::string target = m_LibraryOutputPath + std::string(name) + ".lib";
  266. cmSystemTools::ConvertToWindowsSlashes(target);
  267. target = cmSystemTools::EscapeSpaces(target.c_str());
  268. std::string depend = "$(";
  269. depend += std::string(name) + "_SRC_OBJS)";
  270. std::string command = "tlib @&&|\n\t /p512 /a ";
  271. command += target;
  272. command += " ";
  273. std::string deleteCommand = "if exist ";
  274. deleteCommand += target;
  275. deleteCommand += " del ";
  276. deleteCommand += target;
  277. command += " $(";
  278. command += std::string(name) + "_SRC_OBJS)";
  279. command += "\n|\n";
  280. std::string comment = "rule to build static library: ";
  281. comment += name;
  282. std::string customCommands = this->CreateTargetRules(t, name);
  283. const char* cc = 0;
  284. if(customCommands.size() > 0)
  285. {
  286. cc = customCommands.c_str();
  287. }
  288. this->OutputMakeRule(fout,
  289. comment.c_str(),
  290. target.c_str(),
  291. depend.c_str(),
  292. deleteCommand.c_str(),
  293. command.c_str(), cc);
  294. }
  295. void cmBorlandMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  296. const char* name,
  297. const cmTarget &t)
  298. {
  299. std::string target = m_ExecutableOutputPath + name + m_ExecutableExtension;
  300. cmSystemTools::ConvertToWindowsSlashes(target);
  301. target = cmSystemTools::EscapeSpaces(target.c_str());
  302. std::string depend = "$(";
  303. depend += std::string(name) + "_SRC_OBJS) $(" + std::string(name) + "_DEPEND_LIBS)";
  304. std::string command =
  305. "$(CMAKE_CXX_COMPILER) ";
  306. command += " $(CMAKE_LINKER_FLAGS) -e" + target;
  307. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  308. {
  309. command += " $(CMAKE_SHLIB_CFLAGS) ";
  310. }
  311. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  312. {
  313. command += " -tWM ";
  314. }
  315. else
  316. {
  317. command += " -tWC ";
  318. }
  319. std::strstream linklibs;
  320. this->OutputLinkLibraries(linklibs, 0, t);
  321. linklibs << std::ends;
  322. command += linklibs.str();
  323. delete [] linklibs.str();
  324. command += " $(" + std::string(name) + "_SRC_OBJS) ";
  325. std::string comment = "rule to build executable: ";
  326. comment += name;
  327. std::string customCommands = this->CreateTargetRules(t, name);
  328. const char* cc = 0;
  329. if(customCommands.size() > 0)
  330. {
  331. cc = customCommands.c_str();
  332. }
  333. this->OutputMakeRule(fout,
  334. comment.c_str(),
  335. target.c_str(),
  336. depend.c_str(),
  337. command.c_str(), cc);
  338. }
  339. std::string cmBorlandMakefileGenerator::GetOutputExtension(const char* s)
  340. {
  341. std::string sourceExtension = s;
  342. if(sourceExtension == "def")
  343. {
  344. return "";
  345. }
  346. if(sourceExtension == "ico" || sourceExtension == "rc2")
  347. {
  348. return "";
  349. }
  350. if(sourceExtension == "rc")
  351. {
  352. return ".res";
  353. }
  354. return ".obj";
  355. }
  356. bool cmBorlandMakefileGenerator::SamePath(const char* path1, const char* path2)
  357. {
  358. // first check to see if they are the same anyway
  359. if (strcmp(path1, path2) == 0)
  360. {
  361. return true;
  362. }
  363. // next short path and lower case both of them for the compare
  364. return
  365. cmSystemTools::LowerCase(ShortPath(path1)) ==
  366. cmSystemTools::LowerCase(ShortPath(path2));
  367. }