t_amiga.pas 6.7 KB

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