t_morph.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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
  120. begin
  121. LinkRes.Add(')');
  122. LinkRes.Add('GROUP(');
  123. end;
  124. while not StaticLibFiles.Empty do
  125. begin
  126. S:=StaticLibFiles.GetFirst;
  127. LinkRes.AddFileName(maybequoted(s));
  128. end;
  129. end;
  130. if (cs_link_on_target in aktglobalswitches) then
  131. begin
  132. LinkRes.Add(')');
  133. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  134. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  135. linklibc:=false;
  136. while not SharedLibFiles.Empty do
  137. begin
  138. S:=SharedLibFiles.GetFirst;
  139. if s<>'c' then
  140. begin
  141. i:=Pos(target_info.sharedlibext,S);
  142. if i>0 then
  143. Delete(S,i,255);
  144. LinkRes.Add('-l'+s);
  145. end
  146. else
  147. begin
  148. LinkRes.Add('-l'+s);
  149. linklibc:=true;
  150. end;
  151. end;
  152. { be sure that libc&libgcc is the last lib }
  153. if linklibc then
  154. begin
  155. LinkRes.Add('-lc');
  156. LinkRes.Add('-lgcc');
  157. end;
  158. end
  159. else
  160. begin
  161. while not SharedLibFiles.Empty do
  162. begin
  163. S:=SharedLibFiles.GetFirst;
  164. LinkRes.Add('lib'+s+target_info.staticlibext);
  165. end;
  166. LinkRes.Add(')');
  167. end;
  168. { Write and Close response }
  169. linkres.writetodisk;
  170. linkres.free;
  171. WriteResponseFile:=True;
  172. end;
  173. function TLinkerMorphOS.MakeExecutable:boolean;
  174. var
  175. binstr,
  176. cmdstr : string;
  177. success : boolean;
  178. StripStr: string[40];
  179. begin
  180. if not(cs_link_extern in aktglobalswitches) then
  181. Message1(exec_i_linking,current_module.exefilename^);
  182. if not (cs_link_on_target in aktglobalswitches) then
  183. begin
  184. StripStr:='';
  185. if (cs_link_strip in aktglobalswitches) then
  186. StripStr:='-s -P __abox__';
  187. end;
  188. { Write used files and libraries }
  189. WriteResponseFile(false);
  190. { Call linker }
  191. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  192. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  193. if not(cs_link_on_target in aktglobalswitches) then
  194. begin
  195. Replace(cmdstr,'$EXE',PathConv(maybequoted(ScriptFixFileName(current_module.exefilename^))));
  196. Replace(cmdstr,'$RES',PathConv(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  197. Replace(cmdstr,'$STRIP',StripStr);
  198. end
  199. else
  200. begin
  201. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename^)));
  202. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  203. end;
  204. success:=DoExec(FindUtil(BinStr),cmdstr,true,false);
  205. { Stripping Enabled? }
  206. { For MorphOS a separate strip command is needed, to avoid stripping }
  207. { __abox__ symbol, which is required to be present in current MorphOS }
  208. { executables. }
  209. if (cs_link_on_target in aktglobalswitches) then
  210. begin
  211. if success and (cs_link_strip in aktglobalswitches) then
  212. begin
  213. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  214. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  215. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  216. end;
  217. end;
  218. { Remove ReponseFile }
  219. if (success) and not(cs_link_extern in aktglobalswitches) then
  220. RemoveFile(outputexedir+Info.ResName);
  221. MakeExecutable:=success; { otherwise a recursive call to link method }
  222. end;
  223. {*****************************************************************************
  224. Initialize
  225. *****************************************************************************}
  226. initialization
  227. RegisterExternalLinker(system_powerpc_morphos_info,TLinkerMorphOS);
  228. RegisterTarget(system_powerpc_morphos_info);
  229. end.
  230. {
  231. $Log$
  232. Revision 1.14 2005-04-25 15:58:59 karoly
  233. * static linking fixes
  234. Revision 1.13 2005/02/14 17:13:10 peter
  235. * truncate log
  236. Revision 1.12 2005/02/11 07:23:22 karoly
  237. * cleanups, finalized vlink support
  238. Revision 1.11 2005/02/03 03:54:07 karoly
  239. t_morph.pas
  240. }