t_macos.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. SysUtils,
  37. cutils,cfileutl,cclasses,
  38. globtype,globals,systems,verbose,script,fmodule,i_macos,
  39. ogbase,
  40. symconst;
  41. {*****************************************************************************
  42. TIMPORTLIBMACOS
  43. *****************************************************************************}
  44. procedure timportlibmacos.generatelib;
  45. var
  46. i : longint;
  47. ImportLibrary : TImportLibrary;
  48. begin
  49. for i:=0 to current_module.ImportLibraryList.Count-1 do
  50. begin
  51. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  52. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  53. end;
  54. end;
  55. {*****************************************************************************
  56. TLINKERMPW
  57. *****************************************************************************}
  58. Constructor TLinkerMPW.Create;
  59. begin
  60. Inherited Create;
  61. //LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib',true);
  62. end;
  63. procedure TLinkerMPW.SetDefaultInfo;
  64. begin
  65. with Info do
  66. begin
  67. ExeCmd[1]:='Execute $RES'; {The link.res file contains the whole link command.}
  68. //ExeCmd[1]:='PPCLink $OPT $DYNLINK $STATIC $STRIP -tocdataref off -dead on -o $EXE -@filelist $RES';
  69. //DllCmd[1]:='PPCLink $OPT $INIT $FINI $SONAME -shared -o $EXE -@filelist $RES';
  70. end;
  71. end;
  72. Function TLinkerMPW.WriteResponseFile(isdll:boolean) : Boolean;
  73. Var
  74. linkres : TLinkRes;
  75. s,heapsizestr: string;
  76. begin
  77. WriteResponseFile:=False;
  78. { Open link.res file }
  79. linkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  80. with linkRes do
  81. begin
  82. {#182 is escape char in MPW (analog to backslash in unix). The space}
  83. {ensures there is whitespace separating items.}
  84. Add('PPCLink '#182);
  85. { Add MPW standard libraries}
  86. if apptype = app_cui then
  87. Add('"{PPCLibraries}PPCSIOW.o" '#182);
  88. {Even GUI apps must link to PPCToolLibs, because of the System unit
  89. which can be used by MPW tools as well as by GUI apps.}
  90. Add('"{PPCLibraries}PPCToolLibs.o" '#182);
  91. Add('"{SharedLibraries}InterfaceLib" '#182);
  92. Add('"{SharedLibraries}StdCLib" '#182);
  93. Add('"{SharedLibraries}MathLib" '#182);
  94. Add('"{PPCLibraries}StdCRuntime.o" '#182);
  95. Add('"{PPCLibraries}PPCCRuntime.o" '#182);
  96. {Add main objectfiles}
  97. while not ObjectFiles.Empty do
  98. begin
  99. s:=ObjectFiles.GetFirst;
  100. if s<>'' then
  101. Add(s+' '#182);
  102. end;
  103. {Add last lines of the link command}
  104. if apptype = app_tool then
  105. Add('-t "MPST" -c "MPS " '#182);
  106. if apptype = app_cui then {If SIOW, to avoid some warnings.}
  107. Add('-ignoredups __start -ignoredups .__start -ignoredups main -ignoredups .main -ignoredups qd '#182);
  108. Add('-tocdataref off -sym on -dead on -o '+ ScriptFixFileName(current_module.exefilename));
  109. Add('Exit If "{Status}" != 0');
  110. if heapsize = 0 then
  111. heapsizestr:= HexStr(384000, 8)
  112. else
  113. heapsizestr:= HexStr(heapsize, 8);
  114. {Add a SIZE resource on the fly. It controls:
  115. * backgrounding is enabled, to facilitate debuging with Power Mac Debugger
  116. * it is signaled it is a 32 bit app. (perhaps not nessecary on PowerPC)
  117. * heapsize }
  118. if apptype <> app_tool then
  119. begin
  120. Add('Echo "data ''SIZE'' (-1) '#182'{ $'#182'"1080 ' + heapsizestr + ' ' + heapsizestr +
  121. #182'" '#182'};" | Rez -a -o ' + ScriptFixFileName(current_module.exefilename));
  122. Add('Exit If "{Status}" != 0');
  123. end;
  124. {Add mac resources}
  125. if apptype = app_cui then
  126. begin
  127. Add('Rez -a "{RIncludes}"SIOW.r -o ' + ScriptFixFileName(current_module.exefilename));
  128. Add('Exit If "{Status}" != 0');
  129. end;
  130. while not (current_module.ResourceFiles.Empty) do
  131. begin
  132. s := Current_module.ResourceFiles.GetFirst;
  133. if Copy(s,Length(s)-1,Length(s)) = '.r' then
  134. Add('Rez -a ' + s + ' -o ' + ScriptFixFileName(current_module.exefilename))
  135. else
  136. Add('DeRez ' + s + ' | Rez -a -o ' + ScriptFixFileName(current_module.exefilename));
  137. Add('Exit If "{Status}" != 0');
  138. end;
  139. end;
  140. { Write and Close response }
  141. linkres.writetodisk;
  142. linkres.Free;
  143. WriteResponseFile:=True;
  144. end;
  145. function TLinkerMPW.MakeExecutable:boolean;
  146. var
  147. binstr,
  148. cmdstr : TCmdStr;
  149. success : boolean;
  150. DynLinkStr : string[60];
  151. StaticStr,
  152. StripStr : string[40];
  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. RegisterLinker(ld_mpw,TLinkerMPW);
  201. RegisterTarget(system_powerpc_macos_info);
  202. RegisterImport(system_powerpc_macos,timportlibmacos);
  203. {$endif powerpc}
  204. end.