t_haiku.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. Copyright (c) 2008-2008 by Olivier Coursiere
  4. This unit implements support import,export,link routines
  5. for the (i386) Haiku target.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit t_haiku;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. symsym,symdef,
  24. import,export,link;
  25. type
  26. timportlibhaiku=class(timportlib)
  27. procedure generatelib;override;
  28. end;
  29. texportlibhaiku=class(texportlib)
  30. procedure preparelib(const s : string);override;
  31. procedure exportprocedure(hp : texported_item);override;
  32. procedure exportvar(hp : texported_item);override;
  33. procedure generatelib;override;
  34. end;
  35. tlinkerhaiku=class(texternallinker)
  36. private
  37. Function WriteResponseFile(isdll:boolean;makelib:boolean) : Boolean;
  38. public
  39. constructor Create;override;
  40. procedure SetDefaultInfo;override;
  41. function MakeExecutable:boolean;override;
  42. function MakeSharedLibrary:boolean;override;
  43. end;
  44. implementation
  45. uses
  46. SysUtils,
  47. cutils,cfileutl,cclasses,
  48. verbose,systems,globtype,globals,
  49. symconst,script,
  50. fmodule,aasmbase,aasmtai,aasmdata,aasmcpu,cpubase,i_haiku,ogbase;
  51. {*****************************************************************************
  52. TIMPORTLIBHAIKU
  53. *****************************************************************************}
  54. procedure timportlibhaiku.generatelib;
  55. var
  56. i : longint;
  57. ImportLibrary : TImportLibrary;
  58. begin
  59. for i:=0 to current_module.ImportLibraryList.Count-1 do
  60. begin
  61. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  62. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  63. end;
  64. end;
  65. {*****************************************************************************
  66. TEXPORTLIBHAIKU
  67. *****************************************************************************}
  68. procedure texportlibhaiku.preparelib(const s:string);
  69. begin
  70. end;
  71. procedure texportlibhaiku.exportprocedure(hp : texported_item);
  72. var
  73. hp2 : texported_item;
  74. begin
  75. { first test the index value }
  76. if eo_index in hp.options then
  77. begin
  78. Message1(parser_e_no_export_with_index_for_target,'haiku');
  79. exit;
  80. end;
  81. { now place in correct order }
  82. hp2:=texported_item(current_module._exports.first);
  83. while assigned(hp2) and
  84. (hp.name^>hp2.name^) do
  85. hp2:=texported_item(hp2.next);
  86. { insert hp there !! }
  87. if assigned(hp2) and (hp2.name^=hp.name^) then
  88. begin
  89. { this is not allowed !! }
  90. Message1(parser_e_export_name_double,hp.name^);
  91. exit;
  92. end;
  93. if hp2=texported_item(current_module._exports.first) then
  94. current_module._exports.concat(hp)
  95. else if assigned(hp2) then
  96. begin
  97. hp.next:=hp2;
  98. hp.previous:=hp2.previous;
  99. if assigned(hp2.previous) then
  100. hp2.previous.next:=hp;
  101. hp2.previous:=hp;
  102. end
  103. else
  104. current_module._exports.concat(hp);
  105. end;
  106. procedure texportlibhaiku.exportvar(hp : texported_item);
  107. begin
  108. hp.is_var:=true;
  109. exportprocedure(hp);
  110. end;
  111. procedure texportlibhaiku.generatelib;
  112. var
  113. hp2 : texported_item;
  114. pd : tprocdef;
  115. begin
  116. hp2:=texported_item(current_module._exports.first);
  117. while assigned(hp2) do
  118. begin
  119. if (not hp2.is_var) and
  120. (hp2.sym.typ=procsym) then
  121. begin
  122. { the manglednames can already be the same when the procedure
  123. is declared with cdecl }
  124. pd:=tprocdef(tprocsym(hp2.sym).ProcdefList[0]);
  125. if pd.mangledname<>hp2.name^ then
  126. begin
  127. {$ifdef i386}
  128. { place jump in al_procedures }
  129. current_asmdata.asmlists[al_procedures].concat(Tai_align.Create_op(4,$90));
  130. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  131. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(pd.mangledname)));
  132. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  133. {$endif i386}
  134. end;
  135. end
  136. else
  137. Message1(parser_e_no_export_of_variables_for_target,'haiku');
  138. hp2:=texported_item(hp2.next);
  139. end;
  140. end;
  141. {*****************************************************************************
  142. TLINKERHAIKU
  143. *****************************************************************************}
  144. Constructor TLinkerHaiku.Create;
  145. const
  146. HomeNonPackagedDevLib = '/boot/home/config/non-packaged/develop/lib';
  147. HomeDevLib = '/boot/home/config/develop/lib';
  148. CommonNonPackagedDevLib = '/boot/common/non-packaged/develop/lib';
  149. CommonDevLib = '/boot/common/develop/lib';
  150. SystemDevLib = '/boot/system/develop/lib';
  151. var
  152. s : string;
  153. i : integer;
  154. begin
  155. Inherited Create;
  156. s:=GetEnvironmentVariable('BELIBRARIES');
  157. { convert to correct format in case under unix system }
  158. for i:=1 to length(s) do
  159. if s[i] = ':' then
  160. s[i] := ';';
  161. { just in case we have a single path : add the ending ; }
  162. { since that is what the compiler expects. }
  163. if pos(';',s) = 0 then
  164. s:=s+';';
  165. // Under Haiku with package management, BELIBRARIES is empty by default
  166. // We have to look at those system paths, in this order.
  167. // User can still customize BELIBRARIES. That is why it is looked at first.
  168. LibrarySearchPath.AddPath(sysrootpath,s,true); {format:'path1;path2;...'}
  169. LibrarySearchPath.AddPath(sysrootpath, HomeNonPackagedDevLib, false);
  170. LibrarySearchPath.AddPath(sysrootpath, HomeDevLib, false);
  171. LibrarySearchPath.AddPath(sysrootpath, CommonNonPackagedDevLib, false);
  172. LibrarySearchPath.AddPath(sysrootpath, CommonDevLib, false);
  173. LibrarySearchPath.AddPath(sysrootpath, SystemDevLib, false);
  174. end;
  175. procedure TLinkerHaiku.SetDefaultInfo;
  176. begin
  177. with Info do
  178. begin
  179. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE $CATRES';
  180. DllCmd[1]:='ld $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $CATRES';
  181. DllCmd[2]:='strip --strip-unneeded $EXE';
  182. (*
  183. ExeCmd[1]:='sh $RES $EXE $OPT $STATIC $STRIP -L.';
  184. { ExeCmd[1]:='sh $RES $EXE $OPT $DYNLINK $STATIC $STRIP -L.';}
  185. DllCmd[1]:='sh $RES $EXE $OPT -L.';
  186. { DllCmd[1]:='sh $RES $EXE $OPT -L. -g -nostart -soname=$EXE';
  187. } DllCmd[2]:='strip --strip-unneeded $EXE';
  188. { DynamicLinker:='/lib/ld-beos.so.2';}
  189. *)
  190. end;
  191. end;
  192. function TLinkerHaiku.WriteResponseFile(isdll:boolean;makelib:boolean) : Boolean;
  193. Var
  194. linkres : TLinkRes;
  195. i : integer;
  196. cprtobj,
  197. prtobj : string[80];
  198. HPath : TCmdStrListItem;
  199. s : TCmdStr;
  200. linklibc : boolean;
  201. begin
  202. WriteResponseFile:=False;
  203. { set special options for some targets }
  204. linklibc:=(SharedLibFiles.Find('root')<>nil);
  205. prtobj:='prt0';
  206. cprtobj:='cprt0';
  207. if (cs_profile in current_settings.moduleswitches) or
  208. (not SharedLibFiles.Empty) then
  209. begin
  210. AddSharedLibrary('root');
  211. linklibc:=true;
  212. end;
  213. if (not linklibc) and makelib then
  214. begin
  215. linklibc:=true;
  216. cprtobj:='dllprt.o';
  217. end
  218. else if makelib then
  219. begin
  220. // Making a dll with libc linking. Should be always the case under Haiku.
  221. cprtobj:='dllcprt0';
  222. end;
  223. if linklibc then
  224. prtobj:=cprtobj;
  225. { Open link.res file }
  226. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,false);
  227. {
  228. if not isdll then
  229. LinkRes.Add('ld -o $1 $2 $3 $4 $5 $6 $7 $8 $9 \')
  230. else
  231. LinkRes.Add('ld -o $1 -e 0 $2 $3 $4 $5 $6 $7 $8 $9\');
  232. }
  233. LinkRes.Add('-m');
  234. LinkRes.Add('elf_i386_haiku');
  235. LinkRes.Add('-shared');
  236. LinkRes.Add('-Bsymbolic');
  237. { Write path to search libraries }
  238. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  239. while assigned(HPath) do
  240. begin
  241. LinkRes.Add('-L'+HPath.Str);
  242. HPath:=TCmdStrListItem(HPath.Next);
  243. end;
  244. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  245. while assigned(HPath) do
  246. begin
  247. LinkRes.Add('-L'+HPath.Str);
  248. HPath:=TCmdStrListItem(HPath.Next);
  249. end;
  250. { try to add crti and crtbegin if linking to C }
  251. if linklibc then
  252. begin
  253. if librarysearchpath.FindFile('crti.o',false,s) then
  254. LinkRes.AddFileName(s);
  255. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  256. LinkRes.AddFileName(s);
  257. { s:=librarysearchpath.FindFile('start_dyn.o',found)+'start_dyn.o';
  258. if found then LinkRes.AddFileName(s+' \');}
  259. if prtobj<>'' then
  260. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  261. // if isdll then
  262. // LinkRes.AddFileName(FindObjectFile('func.o','',false));
  263. if librarysearchpath.FindFile('init_term_dyn.o',false,s) then
  264. LinkRes.AddFileName(s);
  265. end
  266. else
  267. begin
  268. if prtobj<>'' then
  269. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  270. end;
  271. { main objectfiles }
  272. while not ObjectFiles.Empty do
  273. begin
  274. s:=ObjectFiles.GetFirst;
  275. if s<>'' then
  276. LinkRes.AddFileName(s);
  277. end;
  278. { LinkRes.Add('-lroot \');
  279. LinkRes.Add('/boot/develop/tools/gnupro/lib/gcc-lib/i586-beos/2.9-beos-991026/crtend.o \');
  280. LinkRes.Add('/boot/develop/lib/x86/crtn.o \');}
  281. { Write staticlibraries }
  282. if not StaticLibFiles.Empty then
  283. begin
  284. While not StaticLibFiles.Empty do
  285. begin
  286. S:=StaticLibFiles.GetFirst;
  287. LinkRes.AddFileName(s)
  288. end;
  289. end;
  290. { Write sharedlibraries like -l<lib> }
  291. if not SharedLibFiles.Empty then
  292. begin
  293. While not SharedLibFiles.Empty do
  294. begin
  295. S:=SharedLibFiles.GetFirst;
  296. if s<>'c' then
  297. begin
  298. i:=Pos(target_info.sharedlibext,S);
  299. if i>0 then
  300. Delete(S,i,255);
  301. LinkRes.Add('-l'+s);
  302. end
  303. else
  304. begin
  305. linklibc:=true;
  306. end;
  307. end;
  308. { be sure that libc is the last lib }
  309. { if linklibc then
  310. LinkRes.Add('-lroot');}
  311. { if linkdynamic and (Info.DynamicLinker<>'') then
  312. LinkRes.AddFileName(Info.DynamicLinker);}
  313. end;
  314. if isdll then
  315. LinkRes.Add('-lroot');
  316. { objects which must be at the end }
  317. if linklibc then
  318. begin
  319. if librarysearchpath.FindFile('crtend.o',false,s) then
  320. LinkRes.AddFileName(s);
  321. if librarysearchpath.FindFile('crtn.o',false,s) then
  322. LinkRes.AddFileName(s);
  323. end;
  324. { Write and Close response }
  325. linkres.writetodisk;
  326. linkres.free;
  327. WriteResponseFile:=True;
  328. end;
  329. function TLinkerHaiku.MakeExecutable:boolean;
  330. var
  331. binstr,
  332. cmdstr : TCmdStr;
  333. success,
  334. useshell : boolean;
  335. DynLinkStr : string[60];
  336. GCSectionsStr,
  337. StaticStr,
  338. StripStr : string[40];
  339. begin
  340. if not(cs_link_nolink in current_settings.globalswitches) then
  341. Message1(exec_i_linking,current_module.exefilename);
  342. { Create some replacements }
  343. StaticStr:='';
  344. StripStr:='';
  345. DynLinkStr:='';
  346. GCSectionsStr:='';
  347. if (cs_link_staticflag in current_settings.globalswitches) then
  348. StaticStr:='-static';
  349. if (cs_link_strip in current_settings.globalswitches) then
  350. StripStr:='-s';
  351. if (cs_link_smart in current_settings.globalswitches) and
  352. (tf_smartlink_sections in target_info.flags) then
  353. GCSectionsStr:='--gc-sections';
  354. If (cs_profile in current_settings.moduleswitches) or
  355. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  356. begin
  357. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  358. if cshared Then
  359. DynLinkStr:='--shared ' + DynLinkStr;
  360. if rlinkpath<>'' Then
  361. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  362. End;
  363. { Write used files and libraries }
  364. WriteResponseFile(false,false);
  365. { Call linker }
  366. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  367. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  368. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  369. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  370. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  371. Replace(cmdstr,'$STATIC',StaticStr);
  372. Replace(cmdstr,'$STRIP',StripStr);
  373. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  374. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  375. useshell:=not (tf_no_backquote_support in source_info.flags);
  376. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,useshell);
  377. { Remove ReponseFile }
  378. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  379. DeleteFile(outputexedir+Info.ResName);
  380. MakeExecutable:=success; { otherwise a recursive call to link method }
  381. end;
  382. Function TLinkerHaiku.MakeSharedLibrary:boolean;
  383. var
  384. binstr,
  385. cmdstr,
  386. SoNameStr : TCmdStr;
  387. success : boolean;
  388. DynLinkStr : string[60];
  389. StaticStr,
  390. StripStr : string[40];
  391. begin
  392. MakeSharedLibrary:=false;
  393. if not(cs_link_nolink in current_settings.globalswitches) then
  394. Message1(exec_i_linking,current_module.sharedlibfilename);
  395. { Create some replacements }
  396. StaticStr:='';
  397. StripStr:='';
  398. DynLinkStr:='';
  399. if (cs_link_staticflag in current_settings.globalswitches) then
  400. StaticStr:='-static';
  401. if (cs_link_strip in current_settings.globalswitches) then
  402. StripStr:='-s';
  403. If (cs_profile in current_settings.moduleswitches) or
  404. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  405. begin
  406. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  407. if cshared Then
  408. DynLinkStr:='--shared ' + DynLinkStr;
  409. if rlinkpath<>'' Then
  410. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  411. End;
  412. { Write used files and libraries }
  413. WriteResponseFile(true,true);
  414. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  415. { Call linker }
  416. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  417. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  418. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  419. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  420. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  421. Replace(cmdstr,'$STATIC',StaticStr);
  422. Replace(cmdstr,'$STRIP',StripStr);
  423. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  424. Replace(cmdstr,'$SONAME',SoNameStr);
  425. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,true);
  426. { Strip the library ? }
  427. if success and (cs_link_strip in current_settings.globalswitches) then
  428. begin
  429. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  430. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  431. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  432. end;
  433. { Remove ReponseFile }
  434. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  435. DeleteFile(outputexedir+Info.ResName);
  436. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  437. end;
  438. {*****************************************************************************
  439. Initialize
  440. *****************************************************************************}
  441. initialization
  442. RegisterLinker(ld_haiku,TLinkerhaiku);
  443. {$ifdef i386}
  444. RegisterImport(system_i386_haiku,timportlibhaiku);
  445. RegisterExport(system_i386_haiku,texportlibhaiku);
  446. RegisterTarget(system_i386_haiku_info);
  447. {$endif i386}
  448. end.