t_macos.pas 8.1 KB

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