t_msdos.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_ALINK}
  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. {****************************************************************************
  49. TExternalLinkerMsDosTLink
  50. ****************************************************************************}
  51. Constructor TExternalLinkerMsDosTLink.Create;
  52. begin
  53. Inherited Create;
  54. { allow duplicated libs (PM) }
  55. SharedLibFiles.doubles:=true;
  56. StaticLibFiles.doubles:=true;
  57. end;
  58. procedure TExternalLinkerMsDosTLink.SetDefaultInfo;
  59. begin
  60. with Info do
  61. begin
  62. ExeCmd[1]:='tlink $RES';
  63. end;
  64. end;
  65. Function TExternalLinkerMsDosTLink.WriteResponseFile(isdll:boolean) : Boolean;
  66. Var
  67. linkres : TLinkRes;
  68. s : string;
  69. begin
  70. WriteResponseFile:=False;
  71. { Open link.res file }
  72. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  73. { Add all options to link.res instead of passing them via command line:
  74. DOS command line is limited to 126 characters! }
  75. { add objectfiles, start with prt0 always }
  76. LinkRes.Add(GetShortName(FindObjectFile('prt0','',false)) + ' +');
  77. while not ObjectFiles.Empty do
  78. begin
  79. s:=ObjectFiles.GetFirst;
  80. if s<>'' then
  81. LinkRes.Add(GetShortName(s) + ' +');
  82. end;
  83. LinkRes.Add(', ' + maybequoted(current_module.exefilename));
  84. { Write and Close response }
  85. linkres.writetodisk;
  86. LinkRes.Free;
  87. WriteResponseFile:=True;
  88. end;
  89. function TExternalLinkerMsDosTLink.MakeExecutable:boolean;
  90. var
  91. binstr,
  92. cmdstr : TCmdStr;
  93. success : boolean;
  94. begin
  95. if not(cs_link_nolink in current_settings.globalswitches) then
  96. Message1(exec_i_linking,current_module.exefilename);
  97. { Write used files and libraries and our own tlink script }
  98. WriteResponsefile(false);
  99. { Call linker }
  100. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  101. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  102. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  103. { Remove ReponseFile }
  104. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  105. DeleteFile(outputexedir+Info.ResName);
  106. MakeExecutable:=success; { otherwise a recursive call to link method }
  107. end;
  108. {****************************************************************************
  109. TExternalLinkerMsDosALink
  110. ****************************************************************************}
  111. { TExternalLinkerMsDosALink }
  112. function TExternalLinkerMsDosALink.WriteResponseFile(isdll: boolean): Boolean;
  113. Var
  114. linkres : TLinkRes;
  115. s : string;
  116. begin
  117. WriteResponseFile:=False;
  118. { Open link.res file }
  119. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  120. { Add all options to link.res instead of passing them via command line:
  121. DOS command line is limited to 126 characters! }
  122. { add objectfiles, start with prt0 always }
  123. LinkRes.Add(maybequoted(FindObjectFile('prt0','',false)));
  124. while not ObjectFiles.Empty do
  125. begin
  126. s:=ObjectFiles.GetFirst;
  127. if s<>'' then
  128. LinkRes.Add(maybequoted(s));
  129. end;
  130. LinkRes.Add('-oEXE');
  131. LinkRes.Add('-o ' + maybequoted(current_module.exefilename));
  132. { Write and Close response }
  133. linkres.writetodisk;
  134. LinkRes.Free;
  135. WriteResponseFile:=True;
  136. end;
  137. constructor TExternalLinkerMsDosALink.Create;
  138. begin
  139. Inherited Create;
  140. { allow duplicated libs (PM) }
  141. SharedLibFiles.doubles:=true;
  142. StaticLibFiles.doubles:=true;
  143. end;
  144. procedure TExternalLinkerMsDosALink.SetDefaultInfo;
  145. begin
  146. with Info do
  147. begin
  148. ExeCmd[1]:='alink $RES';
  149. end;
  150. end;
  151. function TExternalLinkerMsDosALink.MakeExecutable: boolean;
  152. var
  153. binstr,
  154. cmdstr : TCmdStr;
  155. success : boolean;
  156. begin
  157. if not(cs_link_nolink in current_settings.globalswitches) then
  158. Message1(exec_i_linking,current_module.exefilename);
  159. { Write used files and libraries and our own tlink script }
  160. WriteResponsefile(false);
  161. { Call linker }
  162. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  163. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  164. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  165. { Remove ReponseFile }
  166. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  167. DeleteFile(outputexedir+Info.ResName);
  168. MakeExecutable:=success; { otherwise a recursive call to link method }
  169. end;
  170. {*****************************************************************************
  171. Initialize
  172. *****************************************************************************}
  173. initialization
  174. {$if defined(USE_LINKER_TLINK)}
  175. RegisterExternalLinker(system_i8086_msdos_info,TExternalLinkerMsDosTLink);
  176. {$elseif defined(USE_LINKER_ALINK)}
  177. RegisterExternalLinker(system_i8086_msdos_info,TExternalLinkerMsDosALink);
  178. {$else}
  179. {$fatal no linker defined}
  180. {$endif}
  181. RegisterTarget(system_i8086_msdos_info);
  182. end.