cmBorlandMakefileGenerator.cxx 15 KB

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