t_aros.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. {
  2. Copyright (c) 2004-2006 by Free Pascal Development Team
  3. This unit implements support import, export, link routines
  4. for the AROS targets (AROS/i386, AROS/x86_64)
  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_aros;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. rescmn, comprsrc, import, export, link, ogbase;
  23. type
  24. timportlibaros=class(timportlib)
  25. procedure generatelib; override;
  26. end;
  27. PLinkeraros = ^TLinkeraros;
  28. TLinkeraros = class(texternallinker)
  29. private
  30. function WriteResponseFile(isdll: boolean): boolean;
  31. function MakeAROSExe: boolean;
  32. public
  33. constructor Create; override;
  34. procedure SetDefaultInfo; override;
  35. function MakeExecutable: boolean; override;
  36. end;
  37. implementation
  38. uses
  39. SysUtils,
  40. cutils,cfileutl,cclasses,
  41. globtype,globals,systems,verbose,script,fmodule,i_aros;
  42. procedure timportlibaros.generatelib;
  43. var
  44. i: longint;
  45. ImportLibrary: TImportLibrary;
  46. begin
  47. for i:=0 to current_module.ImportLibraryList.count -1 do
  48. begin
  49. ImportLibrary := TImportlibrary(current_module.ImportLibraryList[i]);
  50. current_module.linkothersharedlibs.add(ImportLibrary.Name, link_always);
  51. end;
  52. end;
  53. {****************************************************************************
  54. TLinkeraros
  55. ****************************************************************************}
  56. constructor TLinkeraros.Create;
  57. begin
  58. Inherited Create;
  59. { allow duplicated libs (PM) }
  60. SharedLibFiles.doubles:=true;
  61. StaticLibFiles.doubles:=true;
  62. end;
  63. procedure TLinkeraros.SetDefaultInfo;
  64. begin
  65. with Info do begin
  66. { Note: collect-aros seems to be buggy, and doesn't forward options }
  67. { properly when calling the underlying GNU LD. (FIXME?) }
  68. { This means paths with spaces in them are not supported for now on AROS. }
  69. { So for example no Ram Disk: usage for anything which must be linked. (KB) }
  70. ExeCmd[1]:='collect-aros $OPT $STRIP -d -n -o $EXE $RES';
  71. //ExeCmd[1]:='ld $OPT -d -n -o $EXE $RES';
  72. end;
  73. end;
  74. function TLinkeraros.WriteResponseFile(isdll: boolean): boolean;
  75. var
  76. linkres : TLinkRes;
  77. i : longint;
  78. HPath : TCmdStrListItem;
  79. s,s1 : string;
  80. linklibc : boolean;
  81. begin
  82. WriteResponseFile:=False;
  83. { Open link.res file }
  84. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  85. { Write path to search libraries }
  86. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  87. while assigned(HPath) do
  88. begin
  89. s:=HPath.Str;
  90. if (cs_link_on_target in current_settings.globalswitches) then
  91. s:=ScriptFixFileName(s);
  92. LinkRes.Add('-L'+s);
  93. HPath:=TCmdStrListItem(HPath.Next);
  94. end;
  95. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  96. while assigned(HPath) do
  97. begin
  98. s:=HPath.Str;
  99. s1 := Unix2AmigaPath(maybequoted(s));
  100. if trim(s1)<>'' then
  101. LinkRes.Add('SEARCH_DIR('+s1+')');
  102. HPath:=TCmdStrListItem(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. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  114. end;
  115. end;
  116. { Write staticlibraries }
  117. if not StaticLibFiles.Empty then
  118. begin
  119. LinkRes.Add(')');
  120. LinkRes.Add('GROUP(');
  121. while not StaticLibFiles.Empty do
  122. begin
  123. S:=StaticLibFiles.GetFirst;
  124. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  125. end;
  126. end;
  127. if (cs_link_on_target in current_settings.globalswitches) then
  128. begin
  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. end
  156. else
  157. begin
  158. while not SharedLibFiles.Empty do
  159. begin
  160. S:=SharedLibFiles.GetFirst;
  161. LinkRes.Add('lib'+s+target_info.staticlibext);
  162. end;
  163. LinkRes.Add(')');
  164. end;
  165. { Write and Close response }
  166. linkres.writetodisk;
  167. linkres.free;
  168. WriteResponseFile:=True;
  169. end;
  170. function TLinkeraros.MakeAROSExe: boolean;
  171. var
  172. BinStr,
  173. CmdStr : TCmdStr;
  174. StripStr: string[40];
  175. begin
  176. StripStr:='';
  177. if (cs_link_strip in current_settings.globalswitches) then StripStr:='-s';
  178. { Call linker }
  179. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  180. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  181. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename)));
  182. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  183. Replace(cmdstr,'$STRIP',StripStr);
  184. { Replace(cmdstr,'$EXE',Unix2AmigaPath(maybequoted(ScriptFixFileName(current_module.exefilename^))));
  185. Replace(cmdstr,'$RES',Unix2AmigaPath(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));}
  186. MakeAROSExe:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  187. end;
  188. function TLinkeraros.MakeExecutable:boolean;
  189. var
  190. success : boolean;
  191. begin
  192. success:=false;
  193. if not(cs_link_nolink in current_settings.globalswitches) then
  194. Message1(exec_i_linking,current_module.exefilename);
  195. { Write used files and libraries }
  196. WriteResponseFile(false);
  197. success:=MakeAROSExe;
  198. { Remove ReponseFile }
  199. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  200. DeleteFile(outputexedir+Info.ResName);
  201. MakeExecutable:=success; { otherwise a recursive call to link method }
  202. end;
  203. {*****************************************************************************
  204. Initialize
  205. *****************************************************************************}
  206. initialization
  207. {$ifdef i386}
  208. RegisterLinker(ld_aros,TLinkeraros);
  209. RegisterTarget(system_i386_aros_info);
  210. {$endif i386}
  211. {$ifdef x86_64}
  212. RegisterLinker(ld_aros,TLinkeraros);
  213. RegisterTarget(system_x86_64_aros_info);
  214. {$endif x86_64}
  215. RegisterRes(res_elf_info, TWinLikeResourceFile);
  216. end.