t_morph.pas 7.5 KB

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