t_amiga.pas 7.5 KB

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