t_aros.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 -d -n -o $EXE $RES';
  74. ExeCmd[2]:='strip --strip-unneeded $EXE';
  75. //ExeCmd[1]:='ld $OPT -d -n -o $EXE $RES';
  76. end;
  77. end;
  78. procedure TLinkeraros.Setarosx86_64Info;
  79. begin
  80. with Info do begin
  81. ExeCmd[1]:='ld $OPT -defsym=__AROS__=1 -d -q -n -o $EXE $RES';
  82. end;
  83. end;
  84. procedure TLinkeraros.SetDefaultInfo;
  85. begin
  86. case (target_info.system) of
  87. system_i386_aros: Setaros386Info;
  88. system_x86_64_aros: Setarosx86_64Info;
  89. end;
  90. end;
  91. function TLinkeraros.WriteResponseFile(isdll: boolean): boolean;
  92. var
  93. linkres : TLinkRes;
  94. i : longint;
  95. HPath : TCmdStrListItem;
  96. s,s1 : string;
  97. linklibc : boolean;
  98. begin
  99. WriteResponseFile:=False;
  100. { Open link.res file }
  101. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  102. { Write path to search libraries }
  103. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  104. while assigned(HPath) do
  105. begin
  106. s:=HPath.Str;
  107. if (cs_link_on_target in current_settings.globalswitches) then
  108. s:=ScriptFixFileName(s);
  109. LinkRes.Add('-L'+s);
  110. HPath:=TCmdStrListItem(HPath.Next);
  111. end;
  112. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  113. while assigned(HPath) do
  114. begin
  115. s:=HPath.Str;
  116. s1 := Unix2AmigaPath(maybequoted(s));
  117. if trim(s1)<>'' then
  118. LinkRes.Add('SEARCH_DIR('+s1+')');
  119. HPath:=TCmdStrListItem(HPath.Next);
  120. end;
  121. LinkRes.Add('INPUT (');
  122. { add objectfiles, start with prt0 always }
  123. s:=FindObjectFile('prt0','',false);
  124. LinkRes.AddFileName(s);
  125. while not ObjectFiles.Empty do
  126. begin
  127. s:=ObjectFiles.GetFirst;
  128. if s<>'' then
  129. begin
  130. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  131. end;
  132. end;
  133. { Write staticlibraries }
  134. if not StaticLibFiles.Empty then
  135. begin
  136. LinkRes.Add(')');
  137. LinkRes.Add('GROUP(');
  138. while not StaticLibFiles.Empty do
  139. begin
  140. S:=StaticLibFiles.GetFirst;
  141. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  142. end;
  143. end;
  144. if (cs_link_on_target in current_settings.globalswitches) then
  145. begin
  146. LinkRes.Add(')');
  147. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  148. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  149. linklibc:=false;
  150. while not SharedLibFiles.Empty do
  151. begin
  152. S:=SharedLibFiles.GetFirst;
  153. if s<>'c' then
  154. begin
  155. i:=Pos(target_info.sharedlibext,S);
  156. if i>0 then
  157. Delete(S,i,255);
  158. LinkRes.Add('-l'+s);
  159. end
  160. else
  161. begin
  162. LinkRes.Add('-l'+s);
  163. linklibc:=true;
  164. end;
  165. end;
  166. { be sure that libc&libgcc is the last lib }
  167. if linklibc then
  168. begin
  169. LinkRes.Add('-lc');
  170. LinkRes.Add('-lgcc');
  171. end;
  172. end
  173. else
  174. begin
  175. while not SharedLibFiles.Empty do
  176. begin
  177. S:=SharedLibFiles.GetFirst;
  178. LinkRes.Add('lib'+s+target_info.staticlibext);
  179. end;
  180. LinkRes.Add(')');
  181. end;
  182. { Write and Close response }
  183. linkres.writetodisk;
  184. linkres.free;
  185. WriteResponseFile:=True;
  186. end;
  187. function TLinkeraros.Makearos386Exe: boolean;
  188. var
  189. success: boolean;
  190. BinStr,
  191. CmdStr : TCmdStr;
  192. StripStr: string[40];
  193. begin
  194. StripStr:='';
  195. { Call linker }
  196. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  197. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  198. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename)));
  199. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  200. { Replace(cmdstr,'$EXE',Unix2AmigaPath(maybequoted(ScriptFixFileName(current_module.exefilename^))));
  201. Replace(cmdstr,'$RES',Unix2AmigaPath(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  202. Replace(cmdstr,'$STRIP',StripStr); }
  203. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  204. { AROS seems to need a separate strip command, it may be possible to do it
  205. in the linking command, but this works so fine for now. (KB) }
  206. if success and (cs_link_strip in current_settings.globalswitches) then
  207. begin
  208. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  209. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  210. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  211. end;
  212. Makearos386Exe:=success;
  213. end;
  214. function TLinkeraros.Makearosx86_64Exe: boolean;
  215. var
  216. BinStr,
  217. CmdStr : TCmdStr;
  218. StripStr: string[40];
  219. begin
  220. StripStr:='';
  221. if (cs_link_strip in current_settings.globalswitches) then StripStr:='-s';
  222. { Call linker }
  223. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  224. binstr:=FindUtil(utilsprefix+BinStr);
  225. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  226. Replace(cmdstr,'$EXE',Unix2AmigaPath(maybequoted(ScriptFixFileName(current_module.exefilename))));
  227. Replace(cmdstr,'$RES',Unix2AmigaPath(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  228. Replace(cmdstr,'$STRIP',StripStr);
  229. Makearosx86_64Exe:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  230. end;
  231. function TLinkeraros.MakeExecutable:boolean;
  232. var
  233. success : boolean;
  234. begin
  235. if not(cs_link_nolink in current_settings.globalswitches) then
  236. Message1(exec_i_linking,current_module.exefilename);
  237. { Write used files and libraries }
  238. WriteResponseFile(false);
  239. case (target_info.system) of
  240. system_i386_aros: success:=Makearos386Exe;
  241. system_x86_64_aros: success:=Makearosx86_64Exe;
  242. end;
  243. { Remove ReponseFile }
  244. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  245. DeleteFile(outputexedir+Info.ResName);
  246. MakeExecutable:=success; { otherwise a recursive call to link method }
  247. end;
  248. {*****************************************************************************
  249. Initialize
  250. *****************************************************************************}
  251. initialization
  252. {$ifdef i386}
  253. RegisterLinker(ld_aros,TLinkeraros);
  254. RegisterTarget(system_i386_aros_info);
  255. {$endif i386}
  256. {$ifdef x86_64}
  257. RegisterLinker(ld_aros,TLinkeraros);
  258. RegisterTarget(system_x86_64_aros_info);
  259. {$endif x86_64}
  260. RegisterRes(res_elf_info, TWinLikeResourceFile);
  261. end.