t_palmos.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. {
  2. Copyright (c) 2001-2002 by Peter Vreman
  3. This unit implements support import, export, link routines
  4. for the PalmOS target
  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_palmos;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. link;
  23. type
  24. tlinkerPalmOS=class(texternallinker)
  25. private
  26. Function WriteResponseFile : Boolean;
  27. public
  28. constructor Create; override;
  29. procedure SetDefaultInfo; override;
  30. procedure InitSysInitUnitName; override;
  31. function MakeExecutable:boolean; override;
  32. end;
  33. implementation
  34. uses
  35. SysUtils,
  36. cutils,cfileutl,cclasses,
  37. globtype,globals,systems,verbose,cscript,fmodule,i_palmos,
  38. comprsrc;
  39. {****************************************************************************
  40. TLinkerPalmOS
  41. ****************************************************************************}
  42. Constructor TLinkerPalmOS.Create;
  43. begin
  44. Inherited Create;
  45. { allow duplicated libs (PM) }
  46. SharedLibFiles.doubles:=true;
  47. StaticLibFiles.doubles:=true;
  48. end;
  49. procedure TLinkerPalmOS.SetDefaultInfo;
  50. begin
  51. with Info do
  52. begin
  53. //ExeCmd[1]:='ldpalm $OPT $STRIP -N -dy -T $SCRIPT -o $EXE @$RES';
  54. { This is based on my successful experiment with prc-tools remix.
  55. Anyone who has more insight into this Palm magic, feel free to fix. (KB) }
  56. ExeCmd[1]:='ld $OPT $STRIP --no-check-sections -N -dy -o $EXE $RES';
  57. ExeCmd[2]:='obj-res $EXE';
  58. ExeCmd[3]:='build-prc $EXE.prc "$APPNAME" $APPID $EXE *.$EXE.grc';
  59. end;
  60. end;
  61. procedure TLinkerPalmOS.InitSysInitUnitName;
  62. begin
  63. sysinitunit:='si_prc';
  64. end;
  65. Function TLinkerPalmOS.WriteResponseFile : Boolean;
  66. Var
  67. linkres : TLinkRes;
  68. i : longint;
  69. HPath : TCmdStrListItem;
  70. s : string;
  71. linklibc : boolean;
  72. begin
  73. WriteResponseFile:=False;
  74. { Open link.res file }
  75. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  76. { Write path to search libraries }
  77. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  78. while assigned(HPath) do
  79. begin
  80. LinkRes.Add('-L'+HPath.Str);
  81. HPath:=TCmdStrListItem(HPath.Next);
  82. end;
  83. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  84. while assigned(HPath) do
  85. begin
  86. LinkRes.Add('SEARCH_DIR('+HPath.Str+')');
  87. HPath:=TCmdStrListItem(HPath.Next);
  88. end;
  89. LinkRes.Add('INPUT (');
  90. { add objectfiles, start with prt0 always }
  91. if not (target_info.system in systems_internal_sysinit) then
  92. begin
  93. { add objectfiles, start with crt0 always }
  94. { using crt0, we should stick C compatible }
  95. LinkRes.AddFileName(FindObjectFile('crt0','',false));
  96. end;
  97. { main objectfiles }
  98. while not ObjectFiles.Empty do
  99. begin
  100. s:=ObjectFiles.GetFirst;
  101. if s<>'' then
  102. LinkRes.AddFileName(s);
  103. end;
  104. LinkRes.Add(')');
  105. { Write staticlibraries }
  106. if not StaticLibFiles.Empty then
  107. begin
  108. LinkRes.Add('GROUP(');
  109. While not StaticLibFiles.Empty do
  110. begin
  111. S:=StaticLibFiles.GetFirst;
  112. LinkRes.AddFileName(s)
  113. end;
  114. LinkRes.Add(')');
  115. end;
  116. { currently the PalmOS target must be linked always against the C lib }
  117. {LinkRes.Add('-lcrt');}
  118. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  119. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  120. linklibc:=false;
  121. While not SharedLibFiles.Empty do
  122. begin
  123. S:=SharedLibFiles.GetFirst;
  124. if s<>'c' then
  125. begin
  126. i:=Pos(target_info.sharedlibext,S);
  127. if i>0 then
  128. Delete(S,i,255);
  129. LinkRes.Add('-l'+s);
  130. end
  131. else
  132. linklibc:=true;
  133. end;
  134. { be sure that libc is the last lib }
  135. if linklibc then
  136. begin
  137. LinkRes.Add('-lc');
  138. LinkRes.Add('-lgcc');
  139. end;
  140. { Write and Close response }
  141. linkres.writetodisk;
  142. linkres.Free;
  143. WriteResponseFile:=True;
  144. end;
  145. function TLinkerPalmOS.MakeExecutable:boolean;
  146. var
  147. binstr,
  148. cmdstr : TCmdStr;
  149. success : boolean;
  150. StripStr : string[40];
  151. i : longint;
  152. begin
  153. if not(cs_link_nolink in current_settings.globalswitches) then
  154. Message1(exec_i_linking,current_module.exefilename);
  155. { Create some replacements }
  156. StripStr:='';
  157. if (cs_link_strip in current_settings.globalswitches) then
  158. StripStr:='-s';
  159. { Write used files and libraries }
  160. WriteResponseFile;
  161. { Call linker }
  162. success:=false;
  163. for i:=1 to 2 do
  164. begin
  165. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  166. binstr:=FindUtil(utilsprefix+BinStr);
  167. if binstr<>'' then
  168. begin
  169. Replace(cmdstr,'$EXE',MaybeQuoted(current_module.exefilename));
  170. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  171. Replace(cmdstr,'$RES',MaybeQuoted(outputexedir+Info.ResName));
  172. Replace(cmdstr,'$STRIP',StripStr);
  173. // Replace(cmdstr,'$SCRIPT',FindUtil('palm.ld'));
  174. Replace(cmdstr,'$APPNAME',palmos_applicationname);
  175. Replace(cmdstr,'$APPID',palmos_applicationid);
  176. success:=DoExec(binstr,cmdstr,(i=1),false);
  177. if not success then
  178. break;
  179. end;
  180. end;
  181. { Remove ReponseFile }
  182. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  183. DeleteFile(outputexedir+Info.ResName);
  184. MakeExecutable:=success; { otherwise a recursive call to link method }
  185. end;
  186. {*****************************************************************************
  187. Initialize
  188. *****************************************************************************}
  189. initialization
  190. {$ifdef m68k}
  191. RegisterTarget(system_m68k_palmos_info);
  192. RegisterLinker(ld_palmos,TLinkerPalmOS);
  193. RegisterRes(res_m68k_palmos_info,TResourceFile);
  194. {$endif m68k}
  195. {$ifdef arm}
  196. RegisterTarget(system_arm_palmos_info);
  197. RegisterLinker(ld_palmos,TLinkerPalmOS);
  198. RegisterRes(res_arm_palmos_info,TResourceFile);
  199. {$endif arm}
  200. end.