cmBorlandMakefileGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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::ComputeSystemInfo()
  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. std::string fpath =
  49. m_Makefile->GetDefinition("CMAKE_ROOT");
  50. fpath += "/Templates/CMakeBorlandWindowsSystemConfig.cmake";
  51. m_Makefile->ReadListFile(NULL,fpath.c_str());
  52. }
  53. void cmBorlandMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  54. {
  55. fout << "# NMake Makefile generated by cmake\n";
  56. const char* variables =
  57. "# general varibles used in the makefile\n"
  58. "\n"
  59. "CMAKE_STANDARD_WINDOWS_LIBRARIES = @CMAKE_STANDARD_WINDOWS_LIBRARIES@\n"
  60. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@ @BUILD_FLAGS@\n"
  61. "CMAKE_OBJECT_FILE_SUFFIX = @CMAKE_OBJECT_FILE_SUFFIX@\n"
  62. "CMAKE_EXECUTABLE_SUFFIX = @CMAKE_EXECUTABLE_SUFFIX@\n"
  63. "CMAKE_STATICLIB_SUFFIX = @CMAKE_STATICLIB_SUFFIX@\n"
  64. "CMAKE_SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  65. "CMAKE_SHLIB_CFLAGS = -tWR\n"
  66. "CMAKE_LINKER_FLAGS = @CMAKE_LINKER_FLAGS@ @LINKER_BUILD_FLAGS@\n"
  67. "CMAKE_CXX_FLAGS = -P @CMAKE_CXX_FLAGS@ @BUILD_FLAGS@\n"
  68. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  69. "NULL=\n"
  70. "!ELSE \n"
  71. "NULL=nul\n"
  72. "!ENDIF \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 ccommand = m_Makefile->GetDefinition("CCOMMAND_COMMAND");
  88. fout << "RM = " << this->ConvertToOutputPath(ccommand.c_str()) << " remove -f\n";
  89. std::string ccompiler = m_Makefile->GetDefinition("CMAKE_C_COMPILER");
  90. fout << "CMAKE_C_COMPILER = "
  91. << this->ConvertToOutputPath(ccompiler.c_str())
  92. << "\n";
  93. std::string cxxcompiler = m_Makefile->GetDefinition("CMAKE_CXX_COMPILER");
  94. fout << "CMAKE_CXX_COMPILER = "
  95. << this->ConvertToOutputPath(cxxcompiler.c_str())
  96. << "\n";
  97. std::string cmakecommand = m_Makefile->GetDefinition("CMAKE_COMMAND");
  98. fout << "CMAKE_COMMAND = "
  99. << this->ConvertToOutputPath(cmakecommand.c_str()) << "\n";
  100. fout << replaceVars.c_str();
  101. fout << "CMAKE_CURRENT_SOURCE = "
  102. << ShortPath(m_Makefile->GetStartDirectory() )
  103. << "\n";
  104. fout << "CMAKE_CURRENT_BINARY = "
  105. << ShortPath(m_Makefile->GetStartOutputDirectory())
  106. << "\n";
  107. fout << "CMAKE_SOURCE_DIR = "
  108. << ShortPath(m_Makefile->GetHomeDirectory()) << "\n";
  109. fout << "CMAKE_BINARY_DIR = "
  110. << ShortPath(m_Makefile->GetHomeOutputDirectory() )
  111. << "\n";
  112. // Output Include paths
  113. fout << "INCLUDE_FLAGS = ";
  114. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  115. std::vector<std::string>::iterator i;
  116. fout << "-I" <<
  117. this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << " ";
  118. for(i = includes.begin(); i != includes.end(); ++i)
  119. {
  120. std::string include = *i;
  121. // Don't output a -I for the standard include path "/usr/include".
  122. // This can cause problems with certain standard library
  123. // implementations because the wrong headers may be found first.
  124. fout << "-I" << this->ConvertToOutputPath(i->c_str()).c_str() << " ";
  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. {
  145. return;
  146. }
  147. std::string comment = "Build ";
  148. std::string objectFile = std::string(shortName) +
  149. this->GetOutputExtension(source.GetSourceExtension().c_str());
  150. objectFile = this->ConvertToOutputPath(objectFile.c_str());
  151. comment += objectFile + " From ";
  152. comment += source.GetFullPath();
  153. std::string compileCommand;
  154. std::string ext = source.GetSourceExtension();
  155. if(ext == "c" )
  156. {
  157. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  158. compileCommand += extraCompileFlags;
  159. if(shared)
  160. {
  161. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  162. }
  163. compileCommand += " -o";
  164. compileCommand += objectFile;
  165. compileCommand += " $(INCLUDE_FLAGS) -c ";
  166. compileCommand +=
  167. this->ConvertToOutputPath(source.GetFullPath().c_str());
  168. }
  169. else if (ext == "rc")
  170. {
  171. compileCommand = "$(RC) -o\"";
  172. compileCommand += objectFile;
  173. compileCommand += "\" ";
  174. compileCommand +=
  175. this->ConvertToOutputPath(source.GetFullPath().c_str());
  176. }
  177. else if (ext == "def")
  178. {
  179. // no rule to output for this one
  180. return;
  181. }
  182. // assume c++ if not c rc or def
  183. else
  184. {
  185. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  186. compileCommand += extraCompileFlags;
  187. if(shared)
  188. {
  189. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  190. }
  191. compileCommand += " -o";
  192. compileCommand += objectFile;
  193. compileCommand += " $(INCLUDE_FLAGS) -c ";
  194. compileCommand +=
  195. this->ConvertToOutputPath(source.GetFullPath().c_str());
  196. }
  197. this->OutputMakeRule(fout,
  198. comment.c_str(),
  199. objectFile.c_str(),
  200. this->ConvertToOutputPath(
  201. source.GetFullPath().c_str()).c_str(),
  202. compileCommand.c_str());
  203. }
  204. void cmBorlandMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  205. const char* name,
  206. const cmTarget &t)
  207. {
  208. std::string target = m_LibraryOutputPath + name;
  209. std::string libpath = target + ".lib";
  210. target += ".dll";
  211. target = this->ConvertToOutputPath(target.c_str());
  212. libpath = this->ConvertToOutputPath(libpath.c_str());
  213. std::string depend = "$(";
  214. depend += this->CreateMakeVariable(name, "_SRC_OBJS");
  215. depend += ") $(" + this->CreateMakeVariable(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 += " $(" + this->CreateMakeVariable(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. target = this->ConvertToOutputPath(target.c_str());
  267. std::string depend = "$(";
  268. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  269. std::string command = "tlib @&&|\n\t /p512 /a ";
  270. command += target;
  271. command += " ";
  272. std::string deleteCommand = "if exist ";
  273. deleteCommand += target;
  274. deleteCommand += " del ";
  275. deleteCommand += target;
  276. command += " $(";
  277. command += this->CreateMakeVariable(name, "_SRC_OBJS_QUOTED") + ")";
  278. command += "\n|\n";
  279. std::string comment = "rule to build static library: ";
  280. comment += name;
  281. std::string customCommands = this->CreateTargetRules(t, name);
  282. const char* cc = 0;
  283. if(customCommands.size() > 0)
  284. {
  285. cc = customCommands.c_str();
  286. }
  287. this->OutputMakeRule(fout,
  288. comment.c_str(),
  289. target.c_str(),
  290. depend.c_str(),
  291. deleteCommand.c_str(),
  292. command.c_str(), cc);
  293. }
  294. void cmBorlandMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  295. const char* name,
  296. const cmTarget &t)
  297. {
  298. std::string target = m_ExecutableOutputPath + name + m_ExecutableExtension;
  299. target = this->ConvertToOutputPath(target.c_str());
  300. std::string depend = "$(";
  301. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") $(" +
  302. this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  303. std::string command =
  304. "$(CMAKE_CXX_COMPILER) ";
  305. command += " $(CMAKE_LINKER_FLAGS) -e" + target;
  306. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  307. {
  308. command += " $(CMAKE_SHLIB_CFLAGS) ";
  309. }
  310. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  311. {
  312. command += " -tWM ";
  313. }
  314. else
  315. {
  316. command += " -tWC ";
  317. }
  318. std::strstream linklibs;
  319. this->OutputLinkLibraries(linklibs, 0, t);
  320. linklibs << std::ends;
  321. command += linklibs.str();
  322. delete [] linklibs.str();
  323. command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  324. std::string comment = "rule to build executable: ";
  325. comment += name;
  326. std::string customCommands = this->CreateTargetRules(t, name);
  327. const char* cc = 0;
  328. if(customCommands.size() > 0)
  329. {
  330. cc = customCommands.c_str();
  331. }
  332. this->OutputMakeRule(fout,
  333. comment.c_str(),
  334. target.c_str(),
  335. depend.c_str(),
  336. command.c_str(), cc);
  337. }
  338. std::string cmBorlandMakefileGenerator::GetOutputExtension(const char* s)
  339. {
  340. std::string sourceExtension = s;
  341. if(sourceExtension == "def")
  342. {
  343. return "";
  344. }
  345. if(sourceExtension == "ico" || sourceExtension == "rc2")
  346. {
  347. return "";
  348. }
  349. if(sourceExtension == "rc")
  350. {
  351. return ".res";
  352. }
  353. return ".obj";
  354. }
  355. bool cmBorlandMakefileGenerator::SamePath(const char* path1, const char* path2)
  356. {
  357. // first check to see if they are the same anyway
  358. if (strcmp(path1, path2) == 0)
  359. {
  360. return true;
  361. }
  362. // next short path and lower case both of them for the compare
  363. return
  364. cmSystemTools::LowerCase(ShortPath(path1)) ==
  365. cmSystemTools::LowerCase(ShortPath(path2));
  366. }
  367. // borland make does not support variables that are longer than 32
  368. // so use this function to rename any long ones
  369. std::string cmBorlandMakefileGenerator::CreateMakeVariable(const char* s, const char* s2)
  370. {
  371. std::string unmodified = s;
  372. unmodified += s2;
  373. // see if th
  374. if(m_MakeVariableMap.count(unmodified))
  375. {
  376. return m_MakeVariableMap[unmodified];
  377. }
  378. std::string ret = unmodified;
  379. // if the string is greater the 32 chars it is an invalid vairable name
  380. // for borland make
  381. if(ret.size() > 32)
  382. {
  383. std::string str1 = s;
  384. std::string str2 = s2;
  385. // we must shorten the combined string by 4 charactors
  386. // keep no more than 24 charactors from the second string
  387. if(str2.size() > 24)
  388. {
  389. str2 = str2.substr(0, 24);
  390. }
  391. if(str1.size() + str2.size() > 27)
  392. {
  393. str1 = str1.substr(0, 27 - str2.size());
  394. }
  395. char buffer[5];
  396. int i = 0;
  397. sprintf(buffer, "%04d", i);
  398. ret = str1 + str2 + buffer;
  399. while(m_ShortMakeVariableMap.count(ret) && i < 1000)
  400. {
  401. ++i;
  402. sprintf(buffer, "%04d", i);
  403. ret = str1 + str2 + buffer;
  404. }
  405. if(i == 1000)
  406. {
  407. cmSystemTools::Error("Borland makefile varible length too long");
  408. return unmodified;
  409. }
  410. // once an unused variable is found
  411. m_ShortMakeVariableMap[ret] = "1";
  412. }
  413. // always make an entry into the unmodified to varible map
  414. m_MakeVariableMap[unmodified] = ret;
  415. return ret;
  416. }