t_amiga.pas 6.7 KB

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