t_wasm.pas 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. unit t_wasm;
  2. {$i fpcdefs.inc}
  3. interface
  4. uses
  5. systems,
  6. globtype, globals,
  7. aasmbase,
  8. cfileutl, cutils, cclasses,
  9. import, export, aasmdata, aasmcpu,
  10. fmodule, ogbase,
  11. symsym, symdef,
  12. link,
  13. i_wasm, tgcpu;
  14. type
  15. { texportlibwasm }
  16. texportlibwasm=class(texportlib)
  17. procedure preparelib(const s : string);override;
  18. procedure exportprocedure(hp : texported_item);override;
  19. procedure exportvar(hp : texported_item);override;
  20. procedure generatelib;override;
  21. end;
  22. { timportlibwasm }
  23. timportlibwasm = class(timportlib)
  24. procedure generatelib;override;
  25. end;
  26. { tlinkerjvm }
  27. { tlinkerwasm }
  28. tlinkerwasm=class(texternallinker)
  29. public
  30. constructor Create;override;
  31. procedure SetDefaultInfo;override;
  32. //function MakeExecutable:boolean;override;
  33. function MakeSharedLibrary:boolean;override;
  34. end;
  35. implementation
  36. { timportlibwasm }
  37. procedure timportlibwasm.generatelib;
  38. begin
  39. end;
  40. { tlinkerwasm }
  41. constructor tlinkerwasm.Create;
  42. begin
  43. inherited Create;
  44. end;
  45. procedure tlinkerwasm.SetDefaultInfo;
  46. begin
  47. Info.DllCmd[1] := 'wasm-ld $SONAME $GCSECTIONS -o $EXE';
  48. //Info.DllCmd[2] := 'wasmtool --exportrename $INPUT $EXE';
  49. end;
  50. function tlinkerwasm.MakeSharedLibrary: boolean;
  51. var
  52. GCSectionsStr : ansistring;
  53. binstr, cmdstr : Tcmdstr;
  54. InitStr,
  55. FiniStr,
  56. SoNameStr : string[80];
  57. mapstr,ltostr : TCmdStr;
  58. success : Boolean;
  59. tmp : TCmdStrListItem;
  60. tempFileName : ansistring;
  61. begin
  62. //Result := true;
  63. //Result:=inherited MakeSharedLibrary;
  64. if (cs_link_smart in current_settings.globalswitches) and
  65. create_smartlink_sections then
  66. GCSectionsStr:='--gc-sections'
  67. else
  68. GCSectionsStr:='';
  69. SoNameStr:='';
  70. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  71. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  72. tmp := TCmdStrListItem(ObjectFiles.First);
  73. while Assigned(tmp) do begin
  74. cmdstr := tmp.Str+ ' ' + cmdstr;
  75. tmp := TCmdStrListItem(tmp.Next);
  76. end;
  77. if HasExports then
  78. cmdstr := cmdstr + ' --export-dynamic'; //' --export-dynamic';
  79. cmdstr := cmdstr + ' --no-entry --allow-undefined';
  80. if (cs_link_strip in current_settings.globalswitches) then
  81. begin
  82. { only remove non global symbols and debugging info for a library }
  83. cmdstr := cmdstr + ' --strip-all';
  84. end;
  85. //Replace(cmdstr,'$OPT',Info.ExtraOptions);
  86. //Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  87. //Replace(cmdstr,'$INIT',InitStr);
  88. //Replace(cmdstr,'$FINI',FiniStr);
  89. Replace(cmdstr,'$SONAME',SoNameStr);
  90. //Replace(cmdstr,'$MAP',mapstr);
  91. //Replace(cmdstr,'$LTO',ltostr);
  92. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  93. writeln(utilsprefix+binstr,' ',cmdstr);
  94. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  95. //SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  96. //Replace(cmdstr,'$INPUT',current_module.objfilename );
  97. //Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  98. //DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  99. MakeSharedLibrary:=success;
  100. end;
  101. { texportlibwasm }
  102. procedure texportlibwasm.preparelib(const s: string);
  103. begin
  104. //nothing to happen. wasm files are modules
  105. end;
  106. procedure texportlibwasm.exportprocedure(hp: texported_item);
  107. var
  108. nm : TSymStr;
  109. begin
  110. nm := tprocdef(tprocsym(hp.sym).ProcdefList[0]).mangledname;
  111. current_asmdata.asmlists[al_exports].Concat(tai_impexp.create(hp.name^, nm, ie_Func));
  112. end;
  113. procedure texportlibwasm.exportvar(hp: texported_item);
  114. begin
  115. //inherited exportvar(hp);
  116. end;
  117. procedure texportlibwasm.generatelib;
  118. begin
  119. //inherited generatelib;
  120. end;
  121. initialization
  122. RegisterTarget(system_wasm32_embedded_info);
  123. RegisterImport(system_wasm32_embedded, timportlibwasm);
  124. RegisterExport(system_wasm32_embedded, texportlibwasm);
  125. RegisterLinker(ld_wasm, tlinkerwasm);
  126. end.