t_macos.pas 8.2 KB

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