t_morph.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. {
  2. Copyright (c) 2004 by Free Pascal Development Team
  3. This unit implements support import, export, link routines
  4. for the MorphOS (PowerPC) target
  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_morph;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. SysUtils,
  24. cutils,cfileutils,cclasses,
  25. globtype,globals,systems,verbose,script,fmodule,i_morph,link;
  26. type
  27. PlinkerMorphOS=^TlinkerMorphOS;
  28. TlinkerMorphOS=class(texternallinker)
  29. private
  30. Function WriteResponseFile(isdll:boolean) : Boolean;
  31. public
  32. constructor Create; override;
  33. procedure SetDefaultInfo; override;
  34. function MakeExecutable:boolean; override;
  35. end;
  36. {$IFDEF MORPHOS}
  37. { * PathConv is implemented in the system unit! * }
  38. function PathConv(path: string): string; external name 'PATHCONV';
  39. {$ELSE}
  40. function PathConv(path: string): string;
  41. begin
  42. PathConv:=path;
  43. end;
  44. {$ENDIF}
  45. {****************************************************************************
  46. TLinkerMorphOS
  47. ****************************************************************************}
  48. Constructor TLinkerMorphOS.Create;
  49. begin
  50. Inherited Create;
  51. { allow duplicated libs (PM) }
  52. SharedLibFiles.doubles:=true;
  53. StaticLibFiles.doubles:=true;
  54. end;
  55. procedure TLinkerMorphOS.SetDefaultInfo;
  56. begin
  57. with Info do
  58. begin
  59. if (cs_link_on_target in current_settings.globalswitches) then
  60. begin
  61. ExeCmd[1]:='ld $OPT -o $EXE $RES';
  62. ExeCmd[2]:='strip --strip-unneeded --remove-section .comment $EXE';
  63. end
  64. else
  65. begin
  66. ExeCmd[1]:='fpcvlink -b elf32amiga $OPT $STRIP -o $EXE -T $RES';
  67. end;
  68. end;
  69. end;
  70. Function TLinkerMorphOS.WriteResponseFile(isdll:boolean) : Boolean;
  71. Var
  72. linkres : TLinkRes;
  73. i : longint;
  74. HPath : TCmdStrListItem;
  75. s : string;
  76. linklibc : boolean;
  77. begin
  78. WriteResponseFile:=False;
  79. { Open link.res file }
  80. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  81. { Write path to search libraries }
  82. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  83. while assigned(HPath) do
  84. begin
  85. s:=HPath.Str;
  86. if (cs_link_on_target in current_settings.globalswitches) then
  87. s:=ScriptFixFileName(s);
  88. LinkRes.Add('-L'+s);
  89. HPath:=TCmdStrListItem(HPath.Next);
  90. end;
  91. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  92. while assigned(HPath) do
  93. begin
  94. s:=HPath.Str;
  95. if s<>'' then
  96. LinkRes.Add('SEARCH_DIR('+PathConv(maybequoted(s))+')');
  97. HPath:=TCmdStrListItem(HPath.Next);
  98. end;
  99. LinkRes.Add('INPUT (');
  100. { add objectfiles, start with prt0 always }
  101. s:=FindObjectFile('prt0','',false);
  102. LinkRes.AddFileName(s);
  103. while not ObjectFiles.Empty do
  104. begin
  105. s:=ObjectFiles.GetFirst;
  106. if s<>'' then
  107. begin
  108. { vlink doesn't use SEARCH_DIR for object files }
  109. if not(cs_link_on_target in current_settings.globalswitches) then
  110. s:=FindObjectFile(s,'',false);
  111. LinkRes.AddFileName(PathConv(maybequoted(s)));
  112. end;
  113. end;
  114. { Write staticlibraries }
  115. if not StaticLibFiles.Empty then
  116. begin
  117. { vlink doesn't need, and doesn't support GROUP }
  118. if (cs_link_on_target in current_settings.globalswitches) then
  119. 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(PathConv(maybequoted(s)));
  127. end;
  128. end;
  129. if (cs_link_on_target in current_settings.globalswitches) then
  130. begin
  131. LinkRes.Add(')');
  132. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  133. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  134. linklibc:=false;
  135. while not SharedLibFiles.Empty do
  136. begin
  137. S:=SharedLibFiles.GetFirst;
  138. if s<>'c' then
  139. begin
  140. i:=Pos(target_info.sharedlibext,S);
  141. if i>0 then
  142. Delete(S,i,255);
  143. LinkRes.Add('-l'+s);
  144. end
  145. else
  146. begin
  147. LinkRes.Add('-l'+s);
  148. linklibc:=true;
  149. end;
  150. end;
  151. { be sure that libc&libgcc is the last lib }
  152. if linklibc then
  153. begin
  154. LinkRes.Add('-lc');
  155. LinkRes.Add('-lgcc');
  156. end;
  157. end
  158. else
  159. begin
  160. while not SharedLibFiles.Empty do
  161. begin
  162. S:=SharedLibFiles.GetFirst;
  163. LinkRes.Add('lib'+s+target_info.staticlibext);
  164. end;
  165. LinkRes.Add(')');
  166. end;
  167. { Write and Close response }
  168. linkres.writetodisk;
  169. linkres.free;
  170. WriteResponseFile:=True;
  171. end;
  172. function TLinkerMorphOS.MakeExecutable:boolean;
  173. var
  174. binstr,
  175. cmdstr : TCmdStr;
  176. success : boolean;
  177. StripStr: string[40];
  178. begin
  179. if not(cs_link_nolink in current_settings.globalswitches) then
  180. Message1(exec_i_linking,current_module.exefilename^);
  181. if not (cs_link_on_target in current_settings.globalswitches) then
  182. begin
  183. StripStr:='';
  184. if (cs_link_strip in current_settings.globalswitches) then
  185. StripStr:='-s -P __abox__';
  186. end;
  187. { Write used files and libraries }
  188. WriteResponseFile(false);
  189. { Call linker }
  190. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  191. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  192. if not(cs_link_on_target in current_settings.globalswitches) then
  193. begin
  194. Replace(cmdstr,'$EXE',PathConv(maybequoted(ScriptFixFileName(current_module.exefilename^))));
  195. Replace(cmdstr,'$RES',PathConv(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  196. Replace(cmdstr,'$STRIP',StripStr);
  197. end
  198. else
  199. begin
  200. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename^)));
  201. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  202. end;
  203. success:=DoExec(FindUtil(BinStr),cmdstr,true,false);
  204. { Stripping Enabled? }
  205. { For MorphOS a separate strip command is needed, to avoid stripping }
  206. { __abox__ symbol, which is required to be present in current MorphOS }
  207. { executables. }
  208. if (cs_link_on_target in current_settings.globalswitches) then
  209. begin
  210. if success and (cs_link_strip in current_settings.globalswitches) then
  211. begin
  212. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  213. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  214. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  215. end;
  216. end;
  217. { Remove ReponseFile }
  218. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  219. DeleteFile(outputexedir+Info.ResName);
  220. MakeExecutable:=success; { otherwise a recursive call to link method }
  221. end;
  222. {*****************************************************************************
  223. Initialize
  224. *****************************************************************************}
  225. initialization
  226. RegisterExternalLinker(system_powerpc_morphos_info,TLinkerMorphOS);
  227. RegisterTarget(system_powerpc_morphos_info);
  228. end.