t_msdos.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i8086) MS-DOS target
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit t_msdos;
  19. {$i fpcdefs.inc}
  20. {$define USE_LINKER_WLINK}
  21. interface
  22. implementation
  23. uses
  24. SysUtils,
  25. cutils,cfileutl,cclasses,
  26. globtype,globals,systems,verbose,script,
  27. fmodule,i_msdos,
  28. link,aasmbase;
  29. type
  30. { Borland TLINK support }
  31. TExternalLinkerMsDosTLink=class(texternallinker)
  32. private
  33. Function WriteResponseFile(isdll:boolean) : Boolean;
  34. public
  35. constructor Create;override;
  36. procedure SetDefaultInfo;override;
  37. function MakeExecutable:boolean;override;
  38. end;
  39. { the ALINK linker from http://alink.sourceforge.net/ }
  40. TExternalLinkerMsDosALink=class(texternallinker)
  41. private
  42. Function WriteResponseFile(isdll:boolean) : Boolean;
  43. public
  44. constructor Create;override;
  45. procedure SetDefaultInfo;override;
  46. function MakeExecutable:boolean;override;
  47. end;
  48. { the (Open) Watcom linker }
  49. TExternalLinkerMsDosWLink=class(texternallinker)
  50. private
  51. Function WriteResponseFile(isdll:boolean) : Boolean;
  52. public
  53. constructor Create;override;
  54. procedure SetDefaultInfo;override;
  55. function MakeExecutable:boolean;override;
  56. end;
  57. {****************************************************************************
  58. TExternalLinkerMsDosTLink
  59. ****************************************************************************}
  60. Constructor TExternalLinkerMsDosTLink.Create;
  61. begin
  62. Inherited Create;
  63. { allow duplicated libs (PM) }
  64. SharedLibFiles.doubles:=true;
  65. StaticLibFiles.doubles:=true;
  66. end;
  67. procedure TExternalLinkerMsDosTLink.SetDefaultInfo;
  68. begin
  69. with Info do
  70. begin
  71. ExeCmd[1]:='tlink $OPT $RES';
  72. end;
  73. end;
  74. Function TExternalLinkerMsDosTLink.WriteResponseFile(isdll:boolean) : Boolean;
  75. Var
  76. linkres : TLinkRes;
  77. s : string;
  78. begin
  79. WriteResponseFile:=False;
  80. { Open link.res file }
  81. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  82. { Add all options to link.res instead of passing them via command line:
  83. DOS command line is limited to 126 characters! }
  84. { add objectfiles, start with prt0 always }
  85. LinkRes.Add(GetShortName(FindObjectFile('prt0','',false)) + ' +');
  86. while not ObjectFiles.Empty do
  87. begin
  88. s:=ObjectFiles.GetFirst;
  89. if s<>'' then
  90. LinkRes.Add(GetShortName(s) + ' +');
  91. end;
  92. LinkRes.Add(', ' + maybequoted(current_module.exefilename));
  93. { Write and Close response }
  94. linkres.writetodisk;
  95. LinkRes.Free;
  96. WriteResponseFile:=True;
  97. end;
  98. function TExternalLinkerMsDosTLink.MakeExecutable:boolean;
  99. var
  100. binstr,
  101. cmdstr : TCmdStr;
  102. success : boolean;
  103. begin
  104. if not(cs_link_nolink in current_settings.globalswitches) then
  105. Message1(exec_i_linking,current_module.exefilename);
  106. { Write used files and libraries and our own tlink script }
  107. WriteResponsefile(false);
  108. { Call linker }
  109. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  110. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  111. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  112. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  113. { Remove ReponseFile }
  114. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  115. DeleteFile(outputexedir+Info.ResName);
  116. MakeExecutable:=success; { otherwise a recursive call to link method }
  117. end;
  118. {****************************************************************************
  119. TExternalLinkerMsDosALink
  120. ****************************************************************************}
  121. { TExternalLinkerMsDosALink }
  122. function TExternalLinkerMsDosALink.WriteResponseFile(isdll: boolean): Boolean;
  123. Var
  124. linkres : TLinkRes;
  125. s : string;
  126. begin
  127. WriteResponseFile:=False;
  128. { Open link.res file }
  129. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  130. { Add all options to link.res instead of passing them via command line:
  131. DOS command line is limited to 126 characters! }
  132. { add objectfiles, start with prt0 always }
  133. LinkRes.Add(maybequoted(FindObjectFile('prt0','',false)));
  134. while not ObjectFiles.Empty do
  135. begin
  136. s:=ObjectFiles.GetFirst;
  137. if s<>'' then
  138. LinkRes.Add(maybequoted(s));
  139. end;
  140. LinkRes.Add('-oEXE');
  141. LinkRes.Add('-o ' + maybequoted(current_module.exefilename));
  142. { Write and Close response }
  143. linkres.writetodisk;
  144. LinkRes.Free;
  145. WriteResponseFile:=True;
  146. end;
  147. constructor TExternalLinkerMsDosALink.Create;
  148. begin
  149. Inherited Create;
  150. { allow duplicated libs (PM) }
  151. SharedLibFiles.doubles:=true;
  152. StaticLibFiles.doubles:=true;
  153. end;
  154. procedure TExternalLinkerMsDosALink.SetDefaultInfo;
  155. begin
  156. with Info do
  157. begin
  158. ExeCmd[1]:='alink $OPT $RES';
  159. end;
  160. end;
  161. function TExternalLinkerMsDosALink.MakeExecutable: boolean;
  162. var
  163. binstr,
  164. cmdstr : TCmdStr;
  165. success : boolean;
  166. begin
  167. if not(cs_link_nolink in current_settings.globalswitches) then
  168. Message1(exec_i_linking,current_module.exefilename);
  169. { Write used files and libraries and our own tlink script }
  170. WriteResponsefile(false);
  171. { Call linker }
  172. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  173. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  174. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  175. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  176. { Remove ReponseFile }
  177. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  178. DeleteFile(outputexedir+Info.ResName);
  179. MakeExecutable:=success; { otherwise a recursive call to link method }
  180. end;
  181. {****************************************************************************
  182. TExternalLinkerMsDosWLink
  183. ****************************************************************************}
  184. { TExternalLinkerMsDosWLink }
  185. function TExternalLinkerMsDosWLink.WriteResponseFile(isdll: boolean): Boolean;
  186. Var
  187. linkres : TLinkRes;
  188. s : string;
  189. begin
  190. WriteResponseFile:=False;
  191. { Open link.res file }
  192. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  193. { Add all options to link.res instead of passing them via command line:
  194. DOS command line is limited to 126 characters! }
  195. if paratargetdbg in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
  196. LinkRes.Add('debug dwarf');
  197. { add objectfiles, start with prt0 always }
  198. case current_settings.x86memorymodel of
  199. mm_tiny: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0t','',false)));
  200. mm_small: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0s','',false)));
  201. mm_medium: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0m','',false)));
  202. mm_compact: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0c','',false)));
  203. mm_large: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0l','',false)));
  204. mm_huge: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0h','',false)));
  205. end;
  206. while not ObjectFiles.Empty do
  207. begin
  208. s:=ObjectFiles.GetFirst;
  209. if s<>'' then
  210. LinkRes.Add('file ' + maybequoted(s));
  211. end;
  212. while not StaticLibFiles.Empty do
  213. begin
  214. s:=StaticLibFiles.GetFirst;
  215. if s<>'' then
  216. LinkRes.Add('library '+MaybeQuoted(s));
  217. end;
  218. if apptype=app_com then
  219. LinkRes.Add('format dos com')
  220. else
  221. LinkRes.Add('format dos');
  222. LinkRes.Add('option dosseg');
  223. if (cs_link_map in current_settings.globalswitches) then
  224. LinkRes.Add('option map='+maybequoted(ChangeFileExt(current_module.exefilename,'.map')));
  225. LinkRes.Add('name ' + maybequoted(current_module.exefilename));
  226. { Write and Close response }
  227. linkres.writetodisk;
  228. LinkRes.Free;
  229. WriteResponseFile:=True;
  230. end;
  231. constructor TExternalLinkerMsDosWLink.Create;
  232. begin
  233. Inherited Create;
  234. { allow duplicated libs (PM) }
  235. SharedLibFiles.doubles:=true;
  236. StaticLibFiles.doubles:=true;
  237. end;
  238. procedure TExternalLinkerMsDosWLink.SetDefaultInfo;
  239. begin
  240. with Info do
  241. begin
  242. ExeCmd[1]:='wlink $OPT $RES';
  243. end;
  244. end;
  245. function TExternalLinkerMsDosWLink.MakeExecutable: boolean;
  246. var
  247. binstr,
  248. cmdstr : TCmdStr;
  249. success : boolean;
  250. begin
  251. if not(cs_link_nolink in current_settings.globalswitches) then
  252. Message1(exec_i_linking,current_module.exefilename);
  253. { Write used files and libraries and our own tlink script }
  254. WriteResponsefile(false);
  255. { Call linker }
  256. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  257. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  258. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  259. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  260. { Remove ReponseFile }
  261. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  262. DeleteFile(outputexedir+Info.ResName);
  263. MakeExecutable:=success; { otherwise a recursive call to link method }
  264. end;
  265. {*****************************************************************************
  266. Initialize
  267. *****************************************************************************}
  268. initialization
  269. {$if defined(USE_LINKER_TLINK)}
  270. RegisterLinker(ld_msdos,TExternalLinkerMsDosTLink);
  271. {$elseif defined(USE_LINKER_ALINK)}
  272. RegisterLinker(ld_msdos,TExternalLinkerMsDosALink);
  273. {$elseif defined(USE_LINKER_WLINK)}
  274. RegisterLinker(ld_msdos,TExternalLinkerMsDosWLink);
  275. {$else}
  276. {$fatal no linker defined}
  277. {$endif}
  278. RegisterTarget(system_i8086_msdos_info);
  279. end.