t_amiga.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. {
  2. Copyright (c) 2004-2006 by Free Pascal Development Team
  3. This unit implements support import, export, link routines
  4. for the Amiga targets (AmigaOS/m68k, AmigaOS/PPC)
  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_amiga;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. rescmn, comprsrc, link;
  23. type
  24. PLinkerAmiga = ^TLinkerAmiga;
  25. TLinkerAmiga = class(texternallinker)
  26. private
  27. UseVLink: boolean;
  28. function WriteResponseFile(isdll: boolean): boolean;
  29. procedure SetAmiga68kInfo;
  30. procedure SetAmigaPPCInfo;
  31. function MakeAmiga68kExe: boolean;
  32. function MakeAmigaPPCExe: boolean;
  33. public
  34. constructor Create; override;
  35. procedure SetDefaultInfo; override;
  36. function MakeExecutable: boolean; override;
  37. end;
  38. implementation
  39. uses
  40. SysUtils,
  41. cutils,cfileutl,cclasses,
  42. globtype,globals,systems,verbose,script,fmodule,i_amiga;
  43. {****************************************************************************
  44. TLinkerAmiga
  45. ****************************************************************************}
  46. constructor TLinkerAmiga.Create;
  47. begin
  48. UseVLink:=(cs_link_vlink in current_settings.globalswitches);
  49. Inherited Create;
  50. { allow duplicated libs (PM) }
  51. SharedLibFiles.doubles:=true;
  52. StaticLibFiles.doubles:=true;
  53. end;
  54. procedure TLinkerAmiga.SetAmiga68kInfo;
  55. begin
  56. with Info do
  57. begin
  58. if not UseVLink then
  59. begin
  60. ExeCmd[1]:='ld $DYNLINK $OPT -d -n -o $EXE $RES';
  61. end
  62. else
  63. begin
  64. ExeCmd[1]:='vlink -b amigahunk $OPT $STRIP -o $EXE -T $RES';
  65. end;
  66. end;
  67. end;
  68. procedure TLinkerAmiga.SetAmigaPPCInfo;
  69. begin
  70. with Info do begin
  71. ExeCmd[1]:='ld $DYNLINK $OPT -defsym=__amigaos4__=1 -d -q -n -o $EXE $RES';
  72. end;
  73. end;
  74. procedure TLinkerAmiga.SetDefaultInfo;
  75. begin
  76. case (target_info.system) of
  77. system_m68k_amiga: SetAmiga68kInfo;
  78. system_powerpc_amiga: SetAmigaPPCInfo;
  79. end;
  80. end;
  81. function TLinkerAmiga.WriteResponseFile(isdll: boolean): boolean;
  82. var
  83. linkres : TLinkRes;
  84. i : longint;
  85. HPath : TCmdStrListItem;
  86. s : string;
  87. linklibc : boolean;
  88. begin
  89. WriteResponseFile:=False;
  90. { Open link.res file }
  91. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  92. { Write path to search libraries }
  93. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  94. while assigned(HPath) do
  95. begin
  96. s:=HPath.Str;
  97. if (cs_link_on_target in current_settings.globalswitches) then
  98. s:=ScriptFixFileName(s);
  99. LinkRes.Add('-L'+s);
  100. HPath:=TCmdStrListItem(HPath.Next);
  101. end;
  102. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  103. while assigned(HPath) do
  104. begin
  105. s:=HPath.Str;
  106. if s<>'' then
  107. LinkRes.Add('SEARCH_DIR("'+Unix2AmigaPath(s)+'")');
  108. HPath:=TCmdStrListItem(HPath.Next);
  109. end;
  110. LinkRes.Add('INPUT (');
  111. { add objectfiles, start with prt0 always }
  112. s:=FindObjectFile('prt0','',false);
  113. LinkRes.AddFileName(s);
  114. while not ObjectFiles.Empty do
  115. begin
  116. s:=ObjectFiles.GetFirst;
  117. if s<>'' then
  118. begin
  119. { vlink doesn't use SEARCH_DIR for object files }
  120. if UseVLink then
  121. s:=FindObjectFile(s,'',false);
  122. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  123. end;
  124. end;
  125. { Write staticlibraries }
  126. if not StaticLibFiles.Empty then
  127. begin
  128. { vlink doesn't need, and doesn't support GROUP }
  129. if not UseVLink then
  130. begin
  131. LinkRes.Add(')');
  132. LinkRes.Add('GROUP(');
  133. end;
  134. while not StaticLibFiles.Empty do
  135. begin
  136. S:=StaticLibFiles.GetFirst;
  137. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  138. end;
  139. end;
  140. if not UseVLink then
  141. begin
  142. LinkRes.Add(')');
  143. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  144. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  145. linklibc:=false;
  146. while not SharedLibFiles.Empty do
  147. begin
  148. S:=SharedLibFiles.GetFirst;
  149. if s<>'c' then
  150. begin
  151. i:=Pos(target_info.sharedlibext,S);
  152. if i>0 then
  153. Delete(S,i,255);
  154. LinkRes.Add('-l'+s);
  155. end
  156. else
  157. begin
  158. LinkRes.Add('-l'+s);
  159. linklibc:=true;
  160. end;
  161. end;
  162. { be sure that libc&libgcc is the last lib }
  163. if linklibc then
  164. begin
  165. LinkRes.Add('-lc');
  166. LinkRes.Add('-lgcc');
  167. end;
  168. end
  169. else
  170. begin
  171. while not SharedLibFiles.Empty do
  172. begin
  173. S:=SharedLibFiles.GetFirst;
  174. LinkRes.Add('lib'+s+target_info.staticlibext);
  175. end;
  176. LinkRes.Add(')');
  177. end;
  178. { Write and Close response }
  179. linkres.writetodisk;
  180. linkres.free;
  181. WriteResponseFile:=True;
  182. end;
  183. function TLinkerAmiga.MakeAmiga68kExe: boolean;
  184. var
  185. BinStr,
  186. CmdStr : TCmdStr;
  187. StripStr: string[40];
  188. DynLinkStr : string;
  189. begin
  190. StripStr:='';
  191. if (cs_link_strip in current_settings.globalswitches) then StripStr:='-s';
  192. { Call linker }
  193. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  194. binstr:=FindUtil(utilsprefix+BinStr);
  195. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  196. Replace(cmdstr,'$EXE',Unix2AmigaPath(maybequoted(ScriptFixFileName(current_module.exefilename))));
  197. Replace(cmdstr,'$RES',Unix2AmigaPath(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  198. Replace(cmdstr,'$STRIP',StripStr);
  199. if rlinkpath<>'' Then
  200. DynLinkStr:='--rpath-link '+rlinkpath
  201. else
  202. DynLinkStr:='';
  203. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  204. MakeAmiga68kExe:=DoExec(BinStr,CmdStr,true,false);
  205. end;
  206. function TLinkerAmiga.MakeAmigaPPCExe: boolean;
  207. var
  208. BinStr,
  209. CmdStr : TCmdStr;
  210. StripStr: string[40];
  211. DynLinkStr : string;
  212. begin
  213. StripStr:='';
  214. if (cs_link_strip in current_settings.globalswitches) then StripStr:='-s';
  215. { Call linker }
  216. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  217. binstr:=FindUtil(utilsprefix+BinStr);
  218. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  219. Replace(cmdstr,'$EXE',Unix2AmigaPath(maybequoted(ScriptFixFileName(current_module.exefilename))));
  220. Replace(cmdstr,'$RES',Unix2AmigaPath(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  221. Replace(cmdstr,'$STRIP',StripStr);
  222. if rlinkpath<>'' Then
  223. DynLinkStr:='--rpath-link '+rlinkpath
  224. else
  225. DynLinkStr:='';
  226. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  227. MakeAmigaPPCExe:=DoExec(BinStr,CmdStr,true,false);
  228. end;
  229. function TLinkerAmiga.MakeExecutable:boolean;
  230. var
  231. success : boolean;
  232. begin
  233. if not(cs_link_nolink in current_settings.globalswitches) then
  234. Message1(exec_i_linking,current_module.exefilename);
  235. { Write used files and libraries }
  236. WriteResponseFile(false);
  237. success:=false;
  238. case (target_info.system) of
  239. system_m68k_amiga: success:=MakeAmiga68kExe;
  240. system_powerpc_amiga: success:=MakeAmigaPPCExe;
  241. end;
  242. { Remove ReponseFile }
  243. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  244. DeleteFile(outputexedir+Info.ResName);
  245. MakeExecutable:=success; { otherwise a recursive call to link method }
  246. end;
  247. {*****************************************************************************
  248. Initialize
  249. *****************************************************************************}
  250. initialization
  251. {$ifdef m68k}
  252. RegisterLinker(ld_amiga,TLinkerAmiga);
  253. RegisterTarget(system_m68k_Amiga_info);
  254. RegisterRes(res_ext_info, TWinLikeResourceFile);
  255. {$endif m68k}
  256. {$ifdef powerpc}
  257. RegisterLinker(ld_amiga,TLinkerAmiga);
  258. RegisterTarget(system_powerpc_Amiga_info);
  259. {$endif powerpc}
  260. end.