t_amiga.pas 7.6 KB

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