t_morph.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. {****************************************************************************
  38. TLinkerMorphOS
  39. ****************************************************************************}
  40. Constructor TLinkerMorphOS.Create;
  41. begin
  42. Inherited Create;
  43. { allow duplicated libs (PM) }
  44. SharedLibFiles.doubles:=true;
  45. StaticLibFiles.doubles:=true;
  46. end;
  47. procedure TLinkerMorphOS.SetDefaultInfo;
  48. begin
  49. with Info do
  50. begin
  51. if (cs_link_on_target in aktglobalswitches) then
  52. ExeCmd[1]:='ld $OPT $STRIP -o $EXE --script $RES'
  53. else
  54. ExeCmd[1]:='ld $OPT $STRIP -o $EXE $RES'
  55. end;
  56. end;
  57. Function TLinkerMorphOS.WriteResponseFile(isdll:boolean) : Boolean;
  58. Var
  59. linkres : TLinkRes;
  60. i : longint;
  61. HPath : TStringListItem;
  62. s : string;
  63. linklibc : boolean;
  64. begin
  65. WriteResponseFile:=False;
  66. { Open link.res file }
  67. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  68. { Write path to search libraries }
  69. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  70. while assigned(HPath) do
  71. begin
  72. s:=HPath.Str;
  73. if not (cs_link_on_target in aktglobalswitches) then
  74. s:=GetShortName(s)
  75. else
  76. s:=ScriptFixFileName(s);
  77. LinkRes.Add('-L'+s);
  78. HPath:=TStringListItem(HPath.Next);
  79. end;
  80. HPath:=TStringListItem(LibrarySearchPath.First);
  81. while assigned(HPath) do
  82. begin
  83. s:=HPath.Str;
  84. if not (cs_link_on_target in aktglobalswitches) then
  85. s:=GetShortName(s);
  86. if s<>'' then
  87. LinkRes.Add('SEARCH_DIR('+s+')');
  88. HPath:=TStringListItem(HPath.Next);
  89. end;
  90. LinkRes.Add('INPUT (');
  91. { add objectfiles, start with prt0 always }
  92. s:=FindObjectFile('prt0','',false);
  93. if not (cs_link_on_target in aktglobalswitches) then
  94. s:=GetShortName(s);
  95. LinkRes.AddFileName(s);
  96. while not ObjectFiles.Empty do
  97. begin
  98. s:=ObjectFiles.GetFirst;
  99. if not (cs_link_on_target in aktglobalswitches) then
  100. s:=GetShortName(s);
  101. if s<>'' then
  102. LinkRes.AddFileName(s);
  103. end;
  104. LinkRes.Add(')');
  105. { Write staticlibraries }
  106. if not StaticLibFiles.Empty then
  107. begin
  108. LinkRes.Add('GROUP(');
  109. While not StaticLibFiles.Empty do
  110. begin
  111. S:=StaticLibFiles.GetFirst;
  112. if not (cs_link_on_target in aktglobalswitches) then
  113. s:=GetShortName(s);
  114. LinkRes.AddFileName(s);
  115. end;
  116. LinkRes.Add(')');
  117. end;
  118. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  119. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  120. linklibc:=false;
  121. While not SharedLibFiles.Empty do
  122. begin
  123. S:=SharedLibFiles.GetFirst;
  124. if s<>'c' then
  125. begin
  126. i:=Pos(target_info.sharedlibext,S);
  127. if i>0 then
  128. Delete(S,i,255);
  129. LinkRes.Add('-l'+s);
  130. end
  131. else
  132. begin
  133. LinkRes.Add('-l'+s);
  134. linklibc:=true;
  135. end;
  136. end;
  137. { be sure that libc&libgcc is the last lib }
  138. if linklibc then
  139. begin
  140. LinkRes.Add('-lc');
  141. LinkRes.Add('-lgcc');
  142. end;
  143. { Write and Close response }
  144. linkres.writetodisk;
  145. linkres.free;
  146. WriteResponseFile:=True;
  147. end;
  148. function TLinkerMorphOS.MakeExecutable:boolean;
  149. var
  150. binstr,
  151. cmdstr : string;
  152. success : boolean;
  153. StripStr : string[40];
  154. begin
  155. if not(cs_link_extern in aktglobalswitches) then
  156. Message1(exec_i_linking,current_module.exefilename^);
  157. { Create some replacements }
  158. StripStr:='';
  159. { FIXME!!! - Need to add proper stripping support with }
  160. { separate strip command, to avoid stripping __abox__ symbol, }
  161. { which is required to be present in current MorphOS executables! }
  162. { if (cs_link_strip in aktglobalswitches) then
  163. StripStr:='-s';}
  164. { Write used files and libraries }
  165. WriteResponseFile(false);
  166. { Call linker }
  167. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  168. if pos(' ',current_module.exefilename^)>0 then
  169. Replace(cmdstr,'$EXE','"'+ScriptFixFileName(current_module.exefilename^)+'"')
  170. else
  171. Replace(cmdstr,'$EXE',ScriptFixFileName(current_module.exefilename^));
  172. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  173. Replace(cmdstr,'$RES',ScriptFixFileName(outputexedir+Info.ResName));
  174. Replace(cmdstr,'$STRIP',StripStr);
  175. success:=DoExec(FindUtil(BinStr),cmdstr,true,false);
  176. { Remove ReponseFile }
  177. if (success) and not(cs_link_extern in aktglobalswitches) then
  178. RemoveFile(outputexedir+Info.ResName);
  179. MakeExecutable:=success; { otherwise a recursive call to link method }
  180. end;
  181. {*****************************************************************************
  182. Initialize
  183. *****************************************************************************}
  184. initialization
  185. RegisterExternalLinker(system_powerpc_morphos_info,TLinkerMorphOS);
  186. RegisterTarget(system_powerpc_morphos_info);
  187. end.
  188. {
  189. $Log$
  190. Revision 1.3 2004-04-09 01:32:46 karoly
  191. * disable stripping in mos linking scripts.
  192. Revision 1.2 2004/04/08 17:11:02 karoly
  193. * added external linker support based on 1.0 amiga support
  194. Revision 1.1 2004/02/13 05:46:58 karoly
  195. * added powerpc-morphos target
  196. }