t_amiga.pas 6.6 KB

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