t_macos.pas 8.5 KB

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