t_aros.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 (arosOS/i386, arosOS/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. procedure Setaros386Info;
  32. procedure Setarosx86_64Info;
  33. function Makearos386Exe: boolean;
  34. function Makearosx86_64Exe: boolean;
  35. public
  36. constructor Create; override;
  37. procedure SetDefaultInfo; override;
  38. function MakeExecutable: boolean; override;
  39. end;
  40. implementation
  41. uses
  42. SysUtils,
  43. cutils,cfileutl,cclasses,
  44. globtype,globals,systems,verbose,script,fmodule,i_aros;
  45. procedure timportlibaros.generatelib;
  46. var
  47. i: longint;
  48. ImportLibrary: TImportLibrary;
  49. begin
  50. for i:=0 to current_module.ImportLibraryList.count -1 do
  51. begin
  52. ImportLibrary := TImportlibrary(current_module.ImportLibraryList[i]);
  53. current_module.linkothersharedlibs.add(ImportLibrary.Name, link_always);
  54. end;
  55. end;
  56. {****************************************************************************
  57. TLinkeraros
  58. ****************************************************************************}
  59. constructor TLinkeraros.Create;
  60. begin
  61. Inherited Create;
  62. { allow duplicated libs (PM) }
  63. SharedLibFiles.doubles:=true;
  64. StaticLibFiles.doubles:=true;
  65. end;
  66. procedure TLinkeraros.Setaros386Info;
  67. begin
  68. with Info do begin
  69. { Note: collect-aros seems to be buggy, and doesn't forward options }
  70. { properly when calling the underlying GNU LD. (FIXME?) }
  71. { This means paths with spaces in them are not supported for now on AROS. }
  72. { So for example no Ram Disk: usage for anything which must be linked. (KB) }
  73. ExeCmd[1]:='collect-aros $OPT $STRIP -d -n -o $EXE $RES';
  74. //ExeCmd[1]:='ld $OPT -d -n -o $EXE $RES';
  75. end;
  76. end;
  77. procedure TLinkeraros.Setarosx86_64Info;
  78. begin
  79. with Info do begin
  80. ExeCmd[1]:='ld $OPT -defsym=__AROS__=1 -d -q -n -o $EXE $RES';
  81. end;
  82. end;
  83. procedure TLinkeraros.SetDefaultInfo;
  84. begin
  85. case (target_info.system) of
  86. system_i386_aros: Setaros386Info;
  87. system_x86_64_aros: Setarosx86_64Info;
  88. end;
  89. end;
  90. function TLinkeraros.WriteResponseFile(isdll: boolean): boolean;
  91. var
  92. linkres : TLinkRes;
  93. i : longint;
  94. HPath : TCmdStrListItem;
  95. s,s1 : string;
  96. linklibc : boolean;
  97. begin
  98. WriteResponseFile:=False;
  99. { Open link.res file }
  100. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  101. { Write path to search libraries }
  102. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  103. while assigned(HPath) do
  104. begin
  105. s:=HPath.Str;
  106. if (cs_link_on_target in current_settings.globalswitches) then
  107. s:=ScriptFixFileName(s);
  108. LinkRes.Add('-L'+s);
  109. HPath:=TCmdStrListItem(HPath.Next);
  110. end;
  111. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  112. while assigned(HPath) do
  113. begin
  114. s:=HPath.Str;
  115. s1 := Unix2AmigaPath(maybequoted(s));
  116. if trim(s1)<>'' then
  117. LinkRes.Add('SEARCH_DIR('+s1+')');
  118. HPath:=TCmdStrListItem(HPath.Next);
  119. end;
  120. LinkRes.Add('INPUT (');
  121. { add objectfiles, start with prt0 always }
  122. s:=FindObjectFile('prt0','',false);
  123. LinkRes.AddFileName(s);
  124. while not ObjectFiles.Empty do
  125. begin
  126. s:=ObjectFiles.GetFirst;
  127. if s<>'' then
  128. begin
  129. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  130. end;
  131. end;
  132. { Write staticlibraries }
  133. if not StaticLibFiles.Empty then
  134. begin
  135. LinkRes.Add(')');
  136. LinkRes.Add('GROUP(');
  137. while not StaticLibFiles.Empty do
  138. begin
  139. S:=StaticLibFiles.GetFirst;
  140. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  141. end;
  142. end;
  143. if (cs_link_on_target in current_settings.globalswitches) then
  144. begin
  145. LinkRes.Add(')');
  146. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  147. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  148. linklibc:=false;
  149. while not SharedLibFiles.Empty do
  150. begin
  151. S:=SharedLibFiles.GetFirst;
  152. if s<>'c' then
  153. begin
  154. i:=Pos(target_info.sharedlibext,S);
  155. if i>0 then
  156. Delete(S,i,255);
  157. LinkRes.Add('-l'+s);
  158. end
  159. else
  160. begin
  161. LinkRes.Add('-l'+s);
  162. linklibc:=true;
  163. end;
  164. end;
  165. { be sure that libc&libgcc is the last lib }
  166. if linklibc then
  167. begin
  168. LinkRes.Add('-lc');
  169. LinkRes.Add('-lgcc');
  170. end;
  171. end
  172. else
  173. begin
  174. while not SharedLibFiles.Empty do
  175. begin
  176. S:=SharedLibFiles.GetFirst;
  177. LinkRes.Add('lib'+s+target_info.staticlibext);
  178. end;
  179. LinkRes.Add(')');
  180. end;
  181. { Write and Close response }
  182. linkres.writetodisk;
  183. linkres.free;
  184. WriteResponseFile:=True;
  185. end;
  186. function TLinkeraros.Makearos386Exe: boolean;
  187. var
  188. BinStr,
  189. CmdStr : TCmdStr;
  190. StripStr: string[40];
  191. begin
  192. StripStr:='';
  193. if (cs_link_strip in current_settings.globalswitches) then StripStr:='-s';
  194. { Call linker }
  195. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  196. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  197. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename)));
  198. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  199. Replace(cmdstr,'$STRIP',StripStr);
  200. { Replace(cmdstr,'$EXE',Unix2AmigaPath(maybequoted(ScriptFixFileName(current_module.exefilename^))));
  201. Replace(cmdstr,'$RES',Unix2AmigaPath(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));}
  202. Makearos386Exe:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  203. end;
  204. function TLinkeraros.Makearosx86_64Exe: boolean;
  205. var
  206. BinStr,
  207. CmdStr : TCmdStr;
  208. StripStr: string[40];
  209. begin
  210. StripStr:='';
  211. if (cs_link_strip in current_settings.globalswitches) then StripStr:='-s';
  212. { Call linker }
  213. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  214. binstr:=FindUtil(utilsprefix+BinStr);
  215. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  216. Replace(cmdstr,'$EXE',Unix2AmigaPath(maybequoted(ScriptFixFileName(current_module.exefilename))));
  217. Replace(cmdstr,'$RES',Unix2AmigaPath(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  218. Replace(cmdstr,'$STRIP',StripStr);
  219. Makearosx86_64Exe:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  220. end;
  221. function TLinkeraros.MakeExecutable:boolean;
  222. var
  223. success : boolean;
  224. begin
  225. success:=false;
  226. if not(cs_link_nolink in current_settings.globalswitches) then
  227. Message1(exec_i_linking,current_module.exefilename);
  228. { Write used files and libraries }
  229. WriteResponseFile(false);
  230. case (target_info.system) of
  231. system_i386_aros: success:=Makearos386Exe;
  232. system_x86_64_aros: success:=Makearosx86_64Exe;
  233. end;
  234. { Remove ReponseFile }
  235. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  236. DeleteFile(outputexedir+Info.ResName);
  237. MakeExecutable:=success; { otherwise a recursive call to link method }
  238. end;
  239. {*****************************************************************************
  240. Initialize
  241. *****************************************************************************}
  242. initialization
  243. {$ifdef i386}
  244. RegisterLinker(ld_aros,TLinkeraros);
  245. RegisterTarget(system_i386_aros_info);
  246. {$endif i386}
  247. {$ifdef x86_64}
  248. RegisterLinker(ld_aros,TLinkeraros);
  249. RegisterTarget(system_x86_64_aros_info);
  250. {$endif x86_64}
  251. RegisterRes(res_elf_info, TWinLikeResourceFile);
  252. end.