t_amiga.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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]:='m68k-amiga-ld $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 $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);
  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(maybequoted(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. begin
  172. StripStr:='';
  173. if (cs_link_strip in current_settings.globalswitches) then StripStr:='-s';
  174. { Call linker }
  175. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  176. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  177. Replace(cmdstr,'$EXE',Unix2AmigaPath(maybequoted(ScriptFixFileName(current_module.exefilename^))));
  178. Replace(cmdstr,'$RES',Unix2AmigaPath(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  179. Replace(cmdstr,'$STRIP',StripStr);
  180. MakeAmiga68kExe:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  181. end;
  182. function TLinkerAmiga.MakeAmigaPPCExe: boolean;
  183. var
  184. BinStr,
  185. CmdStr : TCmdStr;
  186. StripStr: string[40];
  187. begin
  188. StripStr:='';
  189. if (cs_link_strip in current_settings.globalswitches) then StripStr:='-s';
  190. { Call linker }
  191. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  192. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  193. Replace(cmdstr,'$EXE',Unix2AmigaPath(maybequoted(ScriptFixFileName(current_module.exefilename^))));
  194. Replace(cmdstr,'$RES',Unix2AmigaPath(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  195. Replace(cmdstr,'$STRIP',StripStr);
  196. MakeAmigaPPCExe:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  197. end;
  198. function TLinkerAmiga.MakeExecutable:boolean;
  199. var
  200. success : boolean;
  201. begin
  202. if not(cs_link_nolink in current_settings.globalswitches) then
  203. Message1(exec_i_linking,current_module.exefilename^);
  204. { Write used files and libraries }
  205. WriteResponseFile(false);
  206. case (target_info.system) of
  207. system_m68k_amiga: success:=MakeAmiga68kExe;
  208. system_powerpc_amiga: success:=MakeAmigaPPCExe;
  209. end;
  210. { Remove ReponseFile }
  211. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  212. DeleteFile(outputexedir+Info.ResName);
  213. MakeExecutable:=success; { otherwise a recursive call to link method }
  214. end;
  215. {*****************************************************************************
  216. Initialize
  217. *****************************************************************************}
  218. initialization
  219. {$ifdef m68k}
  220. { TODO: No executable creation support for m68k yet!}
  221. RegisterExternalLinker(system_m68k_Amiga_info,TLinkerAmiga);
  222. RegisterTarget(system_m68k_Amiga_info);
  223. {$endif m68k}
  224. {$ifdef powerpc}
  225. RegisterExternalLinker(system_powerpc_Amiga_info,TLinkerAmiga);
  226. RegisterTarget(system_powerpc_Amiga_info);
  227. {$endif powerpc}
  228. end.