t_morph.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. {
  2. $Id$
  3. Copyright (c) 2004 by Free Pascal Development Team
  4. This unit implements support import, export, link routines
  5. for the MorphOS (PowerPC) target
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit t_morph;
  20. {$i fpcdefs.inc}
  21. interface
  22. implementation
  23. uses
  24. link,
  25. cutils,cclasses,
  26. globtype,globals,systems,verbose,script,fmodule,i_morph;
  27. type
  28. PlinkerMorphOS=^TlinkerMorphOS;
  29. TlinkerMorphOS=class(texternallinker)
  30. private
  31. Function WriteResponseFile(isdll:boolean) : Boolean;
  32. public
  33. constructor Create; override;
  34. procedure SetDefaultInfo; override;
  35. function MakeExecutable:boolean; override;
  36. end;
  37. {$IFDEF MORPHOS}
  38. { * PathConv is implemented in the system unit! * }
  39. function PathConv(path: string): string; external name 'PATHCONV';
  40. {$ELSE}
  41. function PathConv(path: string): string;
  42. begin
  43. PathConv:=path;
  44. end;
  45. {$ENDIF}
  46. {****************************************************************************
  47. TLinkerMorphOS
  48. ****************************************************************************}
  49. Constructor TLinkerMorphOS.Create;
  50. begin
  51. Inherited Create;
  52. { allow duplicated libs (PM) }
  53. SharedLibFiles.doubles:=true;
  54. StaticLibFiles.doubles:=true;
  55. end;
  56. procedure TLinkerMorphOS.SetDefaultInfo;
  57. begin
  58. with Info do
  59. begin
  60. if (cs_link_on_target in aktglobalswitches) then
  61. begin
  62. ExeCmd[1]:='ld $OPT -o $EXE $RES';
  63. ExeCmd[2]:='strip --strip-unneeded --remove-section .comment $EXE';
  64. end
  65. else
  66. begin
  67. ExeCmd[1]:='fpcvlink -b elf32amiga $OPT $STRIP -o $EXE -T $RES';
  68. end;
  69. end;
  70. end;
  71. Function TLinkerMorphOS.WriteResponseFile(isdll:boolean) : Boolean;
  72. Var
  73. linkres : TLinkRes;
  74. i : longint;
  75. HPath : TStringListItem;
  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:=TStringListItem(current_module.locallibrarysearchpath.First);
  84. while assigned(HPath) do
  85. begin
  86. s:=HPath.Str;
  87. if (cs_link_on_target in aktglobalswitches) then
  88. s:=ScriptFixFileName(s);
  89. LinkRes.Add('-L'+s);
  90. HPath:=TStringListItem(HPath.Next);
  91. end;
  92. HPath:=TStringListItem(LibrarySearchPath.First);
  93. while assigned(HPath) do
  94. begin
  95. s:=HPath.Str;
  96. if s<>'' then
  97. LinkRes.Add('SEARCH_DIR('+maybequoted(s)+')');
  98. HPath:=TStringListItem(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. { vlink doesn't use SEARCH_DIR for object files }
  110. if not(cs_link_on_target in aktglobalswitches) then
  111. s:=FindObjectFile(s,'',false);
  112. LinkRes.AddFileName(maybequoted(s));
  113. end;
  114. end;
  115. { Write staticlibraries }
  116. if not StaticLibFiles.Empty then
  117. begin
  118. { vlink doesn't need, and doesn't support GROUP }
  119. if (cs_link_on_target in aktglobalswitches) then begin
  120. LinkRes.Add(')');
  121. LinkRes.Add('GROUP(');
  122. end;
  123. while not StaticLibFiles.Empty do
  124. begin
  125. S:=StaticLibFiles.GetFirst;
  126. LinkRes.AddFileName(maybequoted(s));
  127. end;
  128. end;
  129. LinkRes.Add(')');
  130. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  131. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  132. linklibc:=false;
  133. while not SharedLibFiles.Empty do
  134. begin
  135. S:=SharedLibFiles.GetFirst;
  136. if s<>'c' then
  137. begin
  138. i:=Pos(target_info.sharedlibext,S);
  139. if i>0 then
  140. Delete(S,i,255);
  141. LinkRes.Add('-l'+s);
  142. end
  143. else
  144. begin
  145. LinkRes.Add('-l'+s);
  146. linklibc:=true;
  147. end;
  148. end;
  149. { be sure that libc&libgcc is the last lib }
  150. if linklibc then
  151. begin
  152. LinkRes.Add('-lc');
  153. LinkRes.Add('-lgcc');
  154. end;
  155. { Write and Close response }
  156. linkres.writetodisk;
  157. linkres.free;
  158. WriteResponseFile:=True;
  159. end;
  160. function TLinkerMorphOS.MakeExecutable:boolean;
  161. var
  162. binstr,
  163. cmdstr : string;
  164. success : boolean;
  165. StripStr: string[40];
  166. begin
  167. if not(cs_link_extern in aktglobalswitches) then
  168. Message1(exec_i_linking,current_module.exefilename^);
  169. if not (cs_link_on_target in aktglobalswitches) then
  170. begin
  171. StripStr:='';
  172. if (cs_link_strip in aktglobalswitches) then
  173. StripStr:='-s -P __abox__';
  174. end;
  175. { Write used files and libraries }
  176. WriteResponseFile(false);
  177. { Call linker }
  178. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  179. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  180. if not(cs_link_on_target in aktglobalswitches) then
  181. begin
  182. Replace(cmdstr,'$EXE',PathConv(maybequoted(ScriptFixFileName(current_module.exefilename^))));
  183. Replace(cmdstr,'$RES',PathConv(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  184. Replace(cmdstr,'$STRIP',StripStr);
  185. end
  186. else
  187. begin
  188. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename^)));
  189. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  190. end;
  191. success:=DoExec(FindUtil(BinStr),cmdstr,true,false);
  192. { Stripping Enabled? }
  193. { For MorphOS a separate strip command is needed, to avoid stripping }
  194. { __abox__ symbol, which is required to be present in current MorphOS }
  195. { executables. }
  196. if (cs_link_on_target in aktglobalswitches) then
  197. begin
  198. if success and (cs_link_strip in aktglobalswitches) then
  199. begin
  200. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  201. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  202. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  203. end;
  204. end;
  205. { Remove ReponseFile }
  206. if (success) and not(cs_link_extern in aktglobalswitches) then
  207. RemoveFile(outputexedir+Info.ResName);
  208. MakeExecutable:=success; { otherwise a recursive call to link method }
  209. end;
  210. {*****************************************************************************
  211. Initialize
  212. *****************************************************************************}
  213. initialization
  214. RegisterExternalLinker(system_powerpc_morphos_info,TLinkerMorphOS);
  215. RegisterTarget(system_powerpc_morphos_info);
  216. end.
  217. {
  218. $Log$
  219. Revision 1.13 2005-02-14 17:13:10 peter
  220. * truncate log
  221. Revision 1.12 2005/02/11 07:23:22 karoly
  222. * cleanups, finalized vlink support
  223. Revision 1.11 2005/02/03 03:54:07 karoly
  224. t_morph.pas
  225. }