t_macos.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. {
  2. $Id$
  3. Copyright (c) 2001-2002 by Peter Vreman
  4. This unit implements support import,export,link routines for MacOS.
  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_macos;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. import,symsym,symdef,link;
  23. type
  24. timportlibmacos=class(timportlib)
  25. procedure preparelib(const s:string);override;
  26. procedure importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);override;
  27. procedure importvariable(vs:tglobalvarsym;const name,module:string);override;
  28. procedure generatelib;override;
  29. end;
  30. tlinkermpw=class(texternallinker)
  31. private
  32. Function WriteResponseFile(isdll:boolean) : Boolean;
  33. public
  34. constructor Create;override;
  35. procedure SetDefaultInfo;override;
  36. function MakeExecutable:boolean;override;
  37. end;
  38. implementation
  39. uses
  40. cutils,cclasses,
  41. globtype,globals,systems,verbose,script,fmodule,i_macos,
  42. symconst;
  43. {*****************************************************************************
  44. TIMPORTLIBMACOS
  45. *****************************************************************************}
  46. procedure timportlibmacos.preparelib(const s : string);
  47. begin
  48. end;
  49. procedure timportlibmacos.importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);
  50. begin
  51. { insert sharedlibrary }
  52. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  53. end;
  54. procedure timportlibmacos.importvariable(vs:tglobalvarsym;const name,module:string);
  55. begin
  56. { insert sharedlibrary }
  57. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  58. { reset the mangledname and turn off the dll_var option }
  59. vs.set_mangledname(name);
  60. exclude(vs.varoptions,vo_is_dll_var);
  61. end;
  62. procedure timportlibmacos.generatelib;
  63. begin
  64. end;
  65. {*****************************************************************************
  66. TLINKERMPW
  67. *****************************************************************************}
  68. Constructor TLinkerMPW.Create;
  69. begin
  70. Inherited Create;
  71. //LibrarySearchPath.AddPath('/lib;/usr/lib;/usr/X11R6/lib',true);
  72. end;
  73. procedure TLinkerMPW.SetDefaultInfo;
  74. begin
  75. with Info do
  76. begin
  77. ExeCmd[1]:='Execute $RES'; {The link.res file contains the whole link command.}
  78. //ExeCmd[1]:='PPCLink $OPT $DYNLINK $STATIC $STRIP -tocdataref off -dead on -o $EXE -@filelist $RES';
  79. //DllCmd[1]:='PPCLink $OPT $INIT $FINI $SONAME -shared -o $EXE -@filelist $RES';
  80. end;
  81. end;
  82. Function TLinkerMPW.WriteResponseFile(isdll:boolean) : Boolean;
  83. Var
  84. linkres : TLinkRes;
  85. s,heapsizestr: string;
  86. begin
  87. WriteResponseFile:=False;
  88. { Open link.res file }
  89. linkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  90. with linkRes do
  91. begin
  92. {#182 is escape char in MPW (analog to backslash in unix). The space}
  93. {ensures there is whitespace separating items.}
  94. Add('PPCLink '#182);
  95. { Add MPW standard libraries}
  96. if apptype = app_cui then
  97. Add('"{PPCLibraries}PPCSIOW.o" '#182);
  98. {Even GUI apps must link to PPCToolLibs, because of the System unit
  99. which can be used by MPW tools as well as by GUI apps.}
  100. Add('"{PPCLibraries}PPCToolLibs.o" '#182);
  101. Add('"{SharedLibraries}InterfaceLib" '#182);
  102. Add('"{SharedLibraries}StdCLib" '#182);
  103. Add('"{SharedLibraries}MathLib" '#182);
  104. Add('"{PPCLibraries}StdCRuntime.o" '#182);
  105. Add('"{PPCLibraries}PPCCRuntime.o" '#182);
  106. {Add main objectfiles}
  107. while not ObjectFiles.Empty do
  108. begin
  109. s:=ObjectFiles.GetFirst;
  110. if s<>'' then
  111. Add(s+' '#182);
  112. end;
  113. {Add last lines of the link command}
  114. if apptype = app_tool then
  115. Add('-t "MPST" -c "MPS " '#182);
  116. if apptype = app_cui then {If SIOW, to avoid some warnings.}
  117. Add('-ignoredups __start -ignoredups .__start -ignoredups main -ignoredups .main -ignoredups qd '#182);
  118. Add('-tocdataref off -sym on -dead on -o '+ ScriptFixFileName(current_module.exefilename^));
  119. Add('Exit If "{Status}" != 0');
  120. if heapsize = 0 then
  121. heapsizestr:= HexStr(384000, 8)
  122. else
  123. heapsizestr:= HexStr(heapsize, 8);
  124. {Add a SIZE resource on the fly. It controls:
  125. * backgrounding is enabled, to facilitate debuging with Power Mac Debugger
  126. * it is signaled it is a 32 bit app. (perhaps not nessecary on PowerPC)
  127. * heapsize }
  128. if apptype <> app_tool then
  129. Add('Echo "data ''SIZE'' (-1) '#182'{ $'#182'"1080 ' + heapsizestr + ' ' + heapsizestr +
  130. #182'" '#182'};" | Rez -a -o ' + ScriptFixFileName(current_module.exefilename^));
  131. Add('Exit If "{Status}" != 0');
  132. {Add mac resources}
  133. if apptype = app_cui then
  134. begin
  135. Add('Rez -a "{RIncludes}"SIOW.r -o ' + ScriptFixFileName(current_module.exefilename^));
  136. Add('Exit If "{Status}" != 0');
  137. end;
  138. while not (current_module.ResourceFiles.Empty) do
  139. begin
  140. s := Current_module.ResourceFiles.GetFirst;
  141. if Copy(s,Length(s)-1,Length(s)) = '.r' then
  142. Add('Rez -a ' + s + ' -o ' + ScriptFixFileName(current_module.exefilename^))
  143. else
  144. Add('DeRez ' + s + ' | Rez -a -o ' + ScriptFixFileName(current_module.exefilename^));
  145. Add('Exit If "{Status}" != 0');
  146. end;
  147. end;
  148. { Write and Close response }
  149. linkres.writetodisk;
  150. linkres.Free;
  151. WriteResponseFile:=True;
  152. end;
  153. function TLinkerMPW.MakeExecutable:boolean;
  154. var
  155. binstr,
  156. cmdstr : string;
  157. success : boolean;
  158. DynLinkStr : string[60];
  159. StaticStr,
  160. StripStr : string[40];
  161. s: string;
  162. begin
  163. //TODO Only external link in MPW is possible, otherwise yell.
  164. if not(cs_link_extern in aktglobalswitches) then
  165. Message1(exec_i_linking,current_module.exefilename^);
  166. { Create some replacements }
  167. StripStr:='';
  168. (*
  169. StaticStr:='';
  170. DynLinkStr:='';
  171. if (cs_link_staticflag in aktglobalswitches) then
  172. StaticStr:='-static';
  173. if (cs_link_strip in aktglobalswitches) then
  174. StripStr:='-s';
  175. If (cs_profile in aktmoduleswitches) or
  176. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  177. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  178. *)
  179. { Prepare linking }
  180. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  181. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename^)));
  182. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  183. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  184. Replace(cmdstr,'$STATIC',StaticStr);
  185. Replace(cmdstr,'$STRIP',StripStr);
  186. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  187. WriteResponseFile(false);
  188. success:= true;
  189. if cs_link_on_target in aktglobalswitches then
  190. success:=DoExec('SetFile', ' -c ''MPS '' -t ''TEXT'' ' +
  191. ScriptFixFileName(outputexedir+Info.ResName),true,false);
  192. { Call linker }
  193. if success then
  194. success:=DoExec('Execute',CmdStr,true,false);
  195. { Remove ReponseFile }
  196. if (success) and not(cs_link_extern in aktglobalswitches) then
  197. RemoveFile(outputexedir+Info.ResName);
  198. MakeExecutable:=success; { otherwise a recursive call to link method }
  199. end;
  200. {*****************************************************************************
  201. Initialize
  202. *****************************************************************************}
  203. initialization
  204. {$ifdef m68k}
  205. RegisterTarget(system_m68k_macos_info);
  206. RegisterImport(system_m68k_macos,timportlibmacos);
  207. {$endif m68k}
  208. {$ifdef powerpc}
  209. RegisterExternalLinker(system_powerpc_macos_info,TLinkerMPW);
  210. RegisterTarget(system_powerpc_macos_info);
  211. RegisterImport(system_powerpc_macos,timportlibmacos);
  212. {$endif powerpc}
  213. end.
  214. {
  215. $Log$
  216. Revision 1.22 2005-03-25 21:55:43 jonas
  217. * removed some unused variables
  218. Revision 1.21 2005/02/14 17:13:10 peter
  219. * truncate log
  220. Revision 1.20 2005/02/08 22:30:32 olle
  221. + added SIZE resource facilities
  222. Revision 1.19 2005/01/24 17:53:12 olle
  223. + Mac style resource files can now be included in MacOS
  224. Revision 1.18 2005/01/09 16:35:41 olle
  225. + linker response file is now removed after linking
  226. }