t_msdos.pas 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 $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. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  112. { Remove ReponseFile }
  113. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  114. DeleteFile(outputexedir+Info.ResName);
  115. MakeExecutable:=success; { otherwise a recursive call to link method }
  116. end;
  117. {****************************************************************************
  118. TExternalLinkerMsDosALink
  119. ****************************************************************************}
  120. { TExternalLinkerMsDosALink }
  121. function TExternalLinkerMsDosALink.WriteResponseFile(isdll: boolean): Boolean;
  122. Var
  123. linkres : TLinkRes;
  124. s : string;
  125. begin
  126. WriteResponseFile:=False;
  127. { Open link.res file }
  128. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  129. { Add all options to link.res instead of passing them via command line:
  130. DOS command line is limited to 126 characters! }
  131. { add objectfiles, start with prt0 always }
  132. LinkRes.Add(maybequoted(FindObjectFile('prt0','',false)));
  133. while not ObjectFiles.Empty do
  134. begin
  135. s:=ObjectFiles.GetFirst;
  136. if s<>'' then
  137. LinkRes.Add(maybequoted(s));
  138. end;
  139. LinkRes.Add('-oEXE');
  140. LinkRes.Add('-o ' + maybequoted(current_module.exefilename));
  141. { Write and Close response }
  142. linkres.writetodisk;
  143. LinkRes.Free;
  144. WriteResponseFile:=True;
  145. end;
  146. constructor TExternalLinkerMsDosALink.Create;
  147. begin
  148. Inherited Create;
  149. { allow duplicated libs (PM) }
  150. SharedLibFiles.doubles:=true;
  151. StaticLibFiles.doubles:=true;
  152. end;
  153. procedure TExternalLinkerMsDosALink.SetDefaultInfo;
  154. begin
  155. with Info do
  156. begin
  157. ExeCmd[1]:='alink $RES';
  158. end;
  159. end;
  160. function TExternalLinkerMsDosALink.MakeExecutable: boolean;
  161. var
  162. binstr,
  163. cmdstr : TCmdStr;
  164. success : boolean;
  165. begin
  166. if not(cs_link_nolink in current_settings.globalswitches) then
  167. Message1(exec_i_linking,current_module.exefilename);
  168. { Write used files and libraries and our own tlink script }
  169. WriteResponsefile(false);
  170. { Call linker }
  171. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  172. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  173. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  174. { Remove ReponseFile }
  175. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  176. DeleteFile(outputexedir+Info.ResName);
  177. MakeExecutable:=success; { otherwise a recursive call to link method }
  178. end;
  179. {****************************************************************************
  180. TExternalLinkerMsDosWLink
  181. ****************************************************************************}
  182. { TExternalLinkerMsDosWLink }
  183. function TExternalLinkerMsDosWLink.WriteResponseFile(isdll: boolean): Boolean;
  184. Var
  185. linkres : TLinkRes;
  186. s : string;
  187. begin
  188. WriteResponseFile:=False;
  189. { Open link.res file }
  190. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  191. { Add all options to link.res instead of passing them via command line:
  192. DOS command line is limited to 126 characters! }
  193. { add objectfiles, start with prt0 always }
  194. case current_settings.x86memorymodel of
  195. mm_tiny: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0t','',false)));
  196. mm_small: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0s','',false)));
  197. mm_medium: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0m','',false)));
  198. mm_compact: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0c','',false)));
  199. mm_large: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0l','',false)));
  200. mm_huge: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0h','',false)));
  201. end;
  202. while not ObjectFiles.Empty do
  203. begin
  204. s:=ObjectFiles.GetFirst;
  205. if s<>'' then
  206. LinkRes.Add('file ' + maybequoted(s));
  207. end;
  208. while not StaticLibFiles.Empty do
  209. begin
  210. s:=StaticLibFiles.GetFirst;
  211. if s<>'' then
  212. LinkRes.Add('library '+MaybeQuoted(s));
  213. end;
  214. if current_settings.x86memorymodel=mm_tiny then
  215. LinkRes.Add('format dos com')
  216. else
  217. LinkRes.Add('format dos');
  218. LinkRes.Add('option dosseg');
  219. { if current_settings.x86memorymodel=mm_tiny then
  220. LinkRes.Add('system com');}
  221. LinkRes.Add('name ' + maybequoted(current_module.exefilename));
  222. { Write and Close response }
  223. linkres.writetodisk;
  224. LinkRes.Free;
  225. WriteResponseFile:=True;
  226. end;
  227. constructor TExternalLinkerMsDosWLink.Create;
  228. begin
  229. Inherited Create;
  230. { allow duplicated libs (PM) }
  231. SharedLibFiles.doubles:=true;
  232. StaticLibFiles.doubles:=true;
  233. end;
  234. procedure TExternalLinkerMsDosWLink.SetDefaultInfo;
  235. begin
  236. with Info do
  237. begin
  238. ExeCmd[1]:='wlink $RES';
  239. end;
  240. end;
  241. function TExternalLinkerMsDosWLink.MakeExecutable: boolean;
  242. var
  243. binstr,
  244. cmdstr : TCmdStr;
  245. success : boolean;
  246. begin
  247. if not(cs_link_nolink in current_settings.globalswitches) then
  248. Message1(exec_i_linking,current_module.exefilename);
  249. { Write used files and libraries and our own tlink script }
  250. WriteResponsefile(false);
  251. { Call linker }
  252. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  253. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  254. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  255. { Remove ReponseFile }
  256. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  257. DeleteFile(outputexedir+Info.ResName);
  258. MakeExecutable:=success; { otherwise a recursive call to link method }
  259. end;
  260. {*****************************************************************************
  261. Initialize
  262. *****************************************************************************}
  263. initialization
  264. {$if defined(USE_LINKER_TLINK)}
  265. RegisterExternalLinker(system_i8086_msdos_info,TExternalLinkerMsDosTLink);
  266. {$elseif defined(USE_LINKER_ALINK)}
  267. RegisterExternalLinker(system_i8086_msdos_info,TExternalLinkerMsDosALink);
  268. {$elseif defined(USE_LINKER_WLINK)}
  269. RegisterExternalLinker(system_i8086_msdos_info,TExternalLinkerMsDosWLink);
  270. {$else}
  271. {$fatal no linker defined}
  272. {$endif}
  273. RegisterTarget(system_i8086_msdos_info);
  274. end.