t_palmos.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 --embedded-relocs --no-check-sections -N -dy -o $EXE $RES';
  57. ExeCmd[2]:='build-prc $EXE.prc "$APPNAME" $APPID $EXE';
  58. end;
  59. end;
  60. procedure TLinkerPalmOS.InitSysInitUnitName;
  61. begin
  62. sysinitunit:='si_prc';
  63. end;
  64. Function TLinkerPalmOS.WriteResponseFile : Boolean;
  65. Var
  66. linkres : TLinkRes;
  67. i : longint;
  68. HPath : TCmdStrListItem;
  69. s : string;
  70. linklibc : boolean;
  71. begin
  72. WriteResponseFile:=False;
  73. { Open link.res file }
  74. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  75. { Write path to search libraries }
  76. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  77. while assigned(HPath) do
  78. begin
  79. LinkRes.Add('-L'+HPath.Str);
  80. HPath:=TCmdStrListItem(HPath.Next);
  81. end;
  82. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  83. while assigned(HPath) do
  84. begin
  85. LinkRes.Add('SEARCH_DIR('+HPath.Str+')');
  86. HPath:=TCmdStrListItem(HPath.Next);
  87. end;
  88. LinkRes.Add('INPUT (');
  89. { add objectfiles, start with prt0 always }
  90. if not (target_info.system in systems_internal_sysinit) then
  91. begin
  92. { add objectfiles, start with crt0 always }
  93. { using crt0, we should stick C compatible }
  94. LinkRes.AddFileName(FindObjectFile('crt0','',false));
  95. end;
  96. { main objectfiles }
  97. while not ObjectFiles.Empty do
  98. begin
  99. s:=ObjectFiles.GetFirst;
  100. if s<>'' then
  101. LinkRes.AddFileName(s);
  102. end;
  103. LinkRes.Add(')');
  104. { Write staticlibraries }
  105. if not StaticLibFiles.Empty then
  106. begin
  107. LinkRes.Add('GROUP(');
  108. While not StaticLibFiles.Empty do
  109. begin
  110. S:=StaticLibFiles.GetFirst;
  111. LinkRes.AddFileName(s)
  112. end;
  113. LinkRes.Add(')');
  114. end;
  115. { currently the PalmOS target must be linked always against the C lib }
  116. {LinkRes.Add('-lcrt');}
  117. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  118. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  119. linklibc:=false;
  120. While not SharedLibFiles.Empty do
  121. begin
  122. S:=SharedLibFiles.GetFirst;
  123. if s<>'c' then
  124. begin
  125. i:=Pos(target_info.sharedlibext,S);
  126. if i>0 then
  127. Delete(S,i,255);
  128. LinkRes.Add('-l'+s);
  129. end
  130. else
  131. linklibc:=true;
  132. end;
  133. { be sure that libc is the last lib }
  134. if linklibc then
  135. begin
  136. LinkRes.Add('-lc');
  137. LinkRes.Add('-lgcc');
  138. end;
  139. { Write and Close response }
  140. linkres.writetodisk;
  141. linkres.Free;
  142. WriteResponseFile:=True;
  143. end;
  144. function TLinkerPalmOS.MakeExecutable:boolean;
  145. var
  146. binstr,
  147. cmdstr : TCmdStr;
  148. success : boolean;
  149. StripStr : string[40];
  150. i : longint;
  151. begin
  152. if not(cs_link_nolink in current_settings.globalswitches) then
  153. Message1(exec_i_linking,current_module.exefilename);
  154. { Create some replacements }
  155. StripStr:='';
  156. if (cs_link_strip in current_settings.globalswitches) then
  157. StripStr:='-s';
  158. { Write used files and libraries }
  159. WriteResponseFile;
  160. { Call linker }
  161. success:=false;
  162. for i:=1 to 2 do
  163. begin
  164. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  165. binstr:=FindUtil(utilsprefix+BinStr);
  166. if binstr<>'' then
  167. begin
  168. Replace(cmdstr,'$EXE',MaybeQuoted(current_module.exefilename));
  169. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  170. Replace(cmdstr,'$RES',MaybeQuoted(outputexedir+Info.ResName));
  171. Replace(cmdstr,'$STRIP',StripStr);
  172. // Replace(cmdstr,'$SCRIPT',FindUtil('palm.ld'));
  173. Replace(cmdstr,'$APPNAME',palmos_applicationname);
  174. Replace(cmdstr,'$APPID',palmos_applicationid);
  175. success:=DoExec(binstr,cmdstr,(i=1),false);
  176. if not success then
  177. break;
  178. end;
  179. end;
  180. { Remove ReponseFile }
  181. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  182. DeleteFile(outputexedir+Info.ResName);
  183. MakeExecutable:=success; { otherwise a recursive call to link method }
  184. end;
  185. {*****************************************************************************
  186. Initialize
  187. *****************************************************************************}
  188. initialization
  189. {$ifdef m68k}
  190. RegisterTarget(system_m68k_palmos_info);
  191. RegisterLinker(ld_palmos,TLinkerPalmOS);
  192. RegisterRes(res_m68k_palmos_info,TResourceFile);
  193. {$endif m68k}
  194. {$ifdef arm}
  195. RegisterTarget(system_arm_palmos_info);
  196. RegisterLinker(ld_palmos,TLinkerPalmOS);
  197. RegisterRes(res_arm_palmos_info,TResourceFile);
  198. {$endif arm}
  199. end.