t_macos.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 generatelib;override;
  25. end;
  26. tlinkermpw=class(texternallinker)
  27. private
  28. Function WriteResponseFile(isdll:boolean) : Boolean;
  29. public
  30. constructor Create;override;
  31. procedure SetDefaultInfo;override;
  32. function MakeExecutable:boolean;override;
  33. end;
  34. implementation
  35. uses
  36. cutils,cclasses,
  37. globtype,globals,systems,verbose,script,fmodule,i_macos,
  38. symconst;
  39. {*****************************************************************************
  40. TIMPORTLIBMACOS
  41. *****************************************************************************}
  42. procedure timportlibmacos.generatelib;
  43. var
  44. i : longint;
  45. ImportLibrary : TImportLibrary;
  46. begin
  47. for i:=0 to current_module.ImportLibraryList.Count-1 do
  48. begin
  49. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  50. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  51. end;
  52. end;
  53. {*****************************************************************************
  54. TLINKERMPW
  55. *****************************************************************************}
  56. Constructor TLinkerMPW.Create;
  57. begin
  58. Inherited Create;
  59. //LibrarySearchPath.AddPath('/lib;/usr/lib;/usr/X11R6/lib',true);
  60. end;
  61. procedure TLinkerMPW.SetDefaultInfo;
  62. begin
  63. with Info do
  64. begin
  65. ExeCmd[1]:='Execute $RES'; {The link.res file contains the whole link command.}
  66. //ExeCmd[1]:='PPCLink $OPT $DYNLINK $STATIC $STRIP -tocdataref off -dead on -o $EXE -@filelist $RES';
  67. //DllCmd[1]:='PPCLink $OPT $INIT $FINI $SONAME -shared -o $EXE -@filelist $RES';
  68. end;
  69. end;
  70. Function TLinkerMPW.WriteResponseFile(isdll:boolean) : Boolean;
  71. Var
  72. linkres : TLinkRes;
  73. s,heapsizestr: string;
  74. begin
  75. WriteResponseFile:=False;
  76. { Open link.res file }
  77. linkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  78. with linkRes do
  79. begin
  80. {#182 is escape char in MPW (analog to backslash in unix). The space}
  81. {ensures there is whitespace separating items.}
  82. Add('PPCLink '#182);
  83. { Add MPW standard libraries}
  84. if apptype = app_cui then
  85. Add('"{PPCLibraries}PPCSIOW.o" '#182);
  86. {Even GUI apps must link to PPCToolLibs, because of the System unit
  87. which can be used by MPW tools as well as by GUI apps.}
  88. Add('"{PPCLibraries}PPCToolLibs.o" '#182);
  89. Add('"{SharedLibraries}InterfaceLib" '#182);
  90. Add('"{SharedLibraries}StdCLib" '#182);
  91. Add('"{SharedLibraries}MathLib" '#182);
  92. Add('"{PPCLibraries}StdCRuntime.o" '#182);
  93. Add('"{PPCLibraries}PPCCRuntime.o" '#182);
  94. {Add main objectfiles}
  95. while not ObjectFiles.Empty do
  96. begin
  97. s:=ObjectFiles.GetFirst;
  98. if s<>'' then
  99. Add(s+' '#182);
  100. end;
  101. {Add last lines of the link command}
  102. if apptype = app_tool then
  103. Add('-t "MPST" -c "MPS " '#182);
  104. if apptype = app_cui then {If SIOW, to avoid some warnings.}
  105. Add('-ignoredups __start -ignoredups .__start -ignoredups main -ignoredups .main -ignoredups qd '#182);
  106. Add('-tocdataref off -sym on -dead on -o '+ ScriptFixFileName(current_module.exefilename^));
  107. Add('Exit If "{Status}" != 0');
  108. if heapsize = 0 then
  109. heapsizestr:= HexStr(384000, 8)
  110. else
  111. heapsizestr:= HexStr(heapsize, 8);
  112. {Add a SIZE resource on the fly. It controls:
  113. * backgrounding is enabled, to facilitate debuging with Power Mac Debugger
  114. * it is signaled it is a 32 bit app. (perhaps not nessecary on PowerPC)
  115. * heapsize }
  116. if apptype <> app_tool then
  117. begin
  118. Add('Echo "data ''SIZE'' (-1) '#182'{ $'#182'"1080 ' + heapsizestr + ' ' + heapsizestr +
  119. #182'" '#182'};" | Rez -a -o ' + ScriptFixFileName(current_module.exefilename^));
  120. Add('Exit If "{Status}" != 0');
  121. end;
  122. {Add mac resources}
  123. if apptype = app_cui then
  124. begin
  125. Add('Rez -a "{RIncludes}"SIOW.r -o ' + ScriptFixFileName(current_module.exefilename^));
  126. Add('Exit If "{Status}" != 0');
  127. end;
  128. while not (current_module.ResourceFiles.Empty) do
  129. begin
  130. s := Current_module.ResourceFiles.GetFirst;
  131. if Copy(s,Length(s)-1,Length(s)) = '.r' then
  132. Add('Rez -a ' + s + ' -o ' + ScriptFixFileName(current_module.exefilename^))
  133. else
  134. Add('DeRez ' + s + ' | Rez -a -o ' + ScriptFixFileName(current_module.exefilename^));
  135. Add('Exit If "{Status}" != 0');
  136. end;
  137. end;
  138. { Write and Close response }
  139. linkres.writetodisk;
  140. linkres.Free;
  141. WriteResponseFile:=True;
  142. end;
  143. function TLinkerMPW.MakeExecutable:boolean;
  144. var
  145. binstr : string;
  146. cmdstr : TCmdStr;
  147. success : boolean;
  148. DynLinkStr : string[60];
  149. StaticStr,
  150. StripStr : string[40];
  151. s: string;
  152. begin
  153. //TODO Only external link in MPW is possible, otherwise yell.
  154. if not(cs_link_nolink in aktglobalswitches) then
  155. Message1(exec_i_linking,current_module.exefilename^);
  156. { Create some replacements }
  157. StripStr:='';
  158. StaticStr:='';
  159. DynLinkStr:='';
  160. (*
  161. if (cs_link_staticflag in aktglobalswitches) then
  162. StaticStr:='-static';
  163. if (cs_link_strip in aktglobalswitches) then
  164. StripStr:='-s';
  165. If (cs_profile in aktmoduleswitches) or
  166. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  167. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  168. *)
  169. { Prepare linking }
  170. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  171. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename^)));
  172. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  173. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  174. Replace(cmdstr,'$STATIC',StaticStr);
  175. Replace(cmdstr,'$STRIP',StripStr);
  176. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  177. WriteResponseFile(false);
  178. success:= true;
  179. if cs_link_on_target in aktglobalswitches then
  180. success:=DoExec('SetFile', ' -c ''MPS '' -t ''TEXT'' ' +
  181. ScriptFixFileName(outputexedir+Info.ResName),true,false);
  182. { Call linker }
  183. if success then
  184. success:=DoExec('Execute',CmdStr,true,false);
  185. { Remove ReponseFile }
  186. if (success) and not(cs_link_nolink in aktglobalswitches) then
  187. RemoveFile(outputexedir+Info.ResName);
  188. MakeExecutable:=success; { otherwise a recursive call to link method }
  189. end;
  190. {*****************************************************************************
  191. Initialize
  192. *****************************************************************************}
  193. initialization
  194. {$ifdef m68k}
  195. RegisterTarget(system_m68k_macos_info);
  196. RegisterImport(system_m68k_macos,timportlibmacos);
  197. {$endif m68k}
  198. {$ifdef powerpc}
  199. RegisterExternalLinker(system_powerpc_macos_info,TLinkerMPW);
  200. RegisterTarget(system_powerpc_macos_info);
  201. RegisterImport(system_powerpc_macos,timportlibmacos);
  202. {$endif powerpc}
  203. end.