t_macos.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. { used for crosscompiling, depends on Retro68 GNU binutils }
  35. TLinkerMacOS = class(texternallinker)
  36. private
  37. function WriteResponseFile(isdll: boolean): boolean;
  38. procedure SetMacOS68kInfo;
  39. function MakeMacOSExe: boolean;
  40. public
  41. constructor Create; override;
  42. procedure SetDefaultInfo; override;
  43. {procedure InitSysInitUnitName; override;}
  44. function MakeExecutable: boolean; override;
  45. end;
  46. implementation
  47. uses
  48. SysUtils,
  49. cutils,cfileutl,cclasses,aasmbase,
  50. globtype,globals,systems,verbose,cscript,fmodule,i_macos,
  51. ogbase,
  52. symconst;
  53. {*****************************************************************************
  54. TIMPORTLIBMACOS
  55. *****************************************************************************}
  56. procedure timportlibmacos.generatelib;
  57. var
  58. i : longint;
  59. ImportLibrary : TImportLibrary;
  60. begin
  61. for i:=0 to current_module.ImportLibraryList.Count-1 do
  62. begin
  63. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  64. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  65. end;
  66. end;
  67. {*****************************************************************************
  68. TLINKERMPW
  69. *****************************************************************************}
  70. Constructor TLinkerMPW.Create;
  71. begin
  72. Inherited Create;
  73. //LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib;=/usr/lib;=/usr/X11R6/lib',true);
  74. end;
  75. procedure TLinkerMPW.SetDefaultInfo;
  76. begin
  77. with Info do
  78. begin
  79. ExeCmd[1]:='Execute $RES'; {The link.res file contains the whole link command.}
  80. //ExeCmd[1]:='PPCLink $OPT $DYNLINK $STATIC $STRIP -tocdataref off -dead on -o $EXE -@filelist $RES';
  81. //DllCmd[1]:='PPCLink $OPT $INIT $FINI $SONAME -shared -o $EXE -@filelist $RES';
  82. end;
  83. end;
  84. Function TLinkerMPW.WriteResponseFile(isdll:boolean) : Boolean;
  85. Var
  86. linkres : TLinkRes;
  87. s,heapsizestr: string;
  88. begin
  89. WriteResponseFile:=False;
  90. { Open link.res file }
  91. linkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  92. with linkRes do
  93. begin
  94. {#182 is escape char in MPW (analog to backslash in unix). The space}
  95. {ensures there is whitespace separating items.}
  96. Add('PPCLink '#182);
  97. { Add MPW standard libraries}
  98. if apptype = app_cui then
  99. Add('"{PPCLibraries}PPCSIOW.o" '#182);
  100. {Even GUI apps must link to PPCToolLibs, because of the System unit
  101. which can be used by MPW tools as well as by GUI apps.}
  102. Add('"{PPCLibraries}PPCToolLibs.o" '#182);
  103. Add('"{SharedLibraries}InterfaceLib" '#182);
  104. Add('"{SharedLibraries}StdCLib" '#182);
  105. Add('"{SharedLibraries}MathLib" '#182);
  106. Add('"{PPCLibraries}StdCRuntime.o" '#182);
  107. Add('"{PPCLibraries}PPCCRuntime.o" '#182);
  108. {Add main objectfiles}
  109. while not ObjectFiles.Empty do
  110. begin
  111. s:=ObjectFiles.GetFirst;
  112. if s<>'' then
  113. Add(s+' '#182);
  114. end;
  115. {Add last lines of the link command}
  116. if apptype = app_tool then
  117. Add('-t "MPST" -c "MPS " '#182);
  118. if apptype = app_cui then {If SIOW, to avoid some warnings.}
  119. Add('-ignoredups __start -ignoredups .__start -ignoredups main -ignoredups .main -ignoredups qd '#182);
  120. Add('-tocdataref off -sym on -dead on -o '+ ScriptFixFileName(current_module.exefilename));
  121. Add('Exit If "{Status}" != 0');
  122. if heapsize = 0 then
  123. heapsizestr:= HexStr(384000, 8)
  124. else
  125. heapsizestr:= HexStr(heapsize, 8);
  126. {Add a SIZE resource on the fly. It controls:
  127. * backgrounding is enabled, to facilitate debuging with Power Mac Debugger
  128. * it is signaled it is a 32 bit app. (perhaps not nessecary on PowerPC)
  129. * heapsize }
  130. if apptype <> app_tool then
  131. begin
  132. Add('Echo "data ''SIZE'' (-1) '#182'{ $'#182'"1080 ' + heapsizestr + ' ' + heapsizestr +
  133. #182'" '#182'};" | Rez -a -o ' + ScriptFixFileName(current_module.exefilename));
  134. Add('Exit If "{Status}" != 0');
  135. end;
  136. {Add mac resources}
  137. if apptype = app_cui then
  138. begin
  139. Add('Rez -a "{RIncludes}"SIOW.r -o ' + ScriptFixFileName(current_module.exefilename));
  140. Add('Exit If "{Status}" != 0');
  141. end;
  142. while not (current_module.ResourceFiles.Empty) do
  143. begin
  144. s := Current_module.ResourceFiles.GetFirst;
  145. if Copy(s,Length(s)-1,Length(s)) = '.r' then
  146. Add('Rez -a ' + s + ' -o ' + ScriptFixFileName(current_module.exefilename))
  147. else
  148. Add('DeRez ' + s + ' | Rez -a -o ' + ScriptFixFileName(current_module.exefilename));
  149. Add('Exit If "{Status}" != 0');
  150. end;
  151. end;
  152. { Write and Close response }
  153. linkres.writetodisk;
  154. linkres.Free;
  155. WriteResponseFile:=True;
  156. end;
  157. function TLinkerMPW.MakeExecutable:boolean;
  158. var
  159. binstr,
  160. cmdstr : TCmdStr;
  161. success : boolean;
  162. DynLinkStr : string[60];
  163. StaticStr,
  164. StripStr : string[40];
  165. begin
  166. //TODO Only external link in MPW is possible, otherwise yell.
  167. if not(cs_link_nolink in current_settings.globalswitches) then
  168. Message1(exec_i_linking,current_module.exefilename);
  169. { Create some replacements }
  170. StripStr:='';
  171. StaticStr:='';
  172. DynLinkStr:='';
  173. (*
  174. if (cs_link_staticflag in current_settings.globalswitches) then
  175. StaticStr:='-static';
  176. if (cs_link_strip in current_settings.globalswitches) then
  177. StripStr:='-s';
  178. If (cs_profile in current_settings.moduleswitches) or
  179. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  180. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  181. *)
  182. { Prepare linking }
  183. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  184. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename)));
  185. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  186. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  187. Replace(cmdstr,'$STATIC',StaticStr);
  188. Replace(cmdstr,'$STRIP',StripStr);
  189. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  190. WriteResponseFile(false);
  191. success:= true;
  192. if cs_link_on_target in current_settings.globalswitches then
  193. success:=DoExec('SetFile', ' -c ''MPS '' -t ''TEXT'' ' +
  194. ScriptFixFileName(outputexedir+Info.ResName),true,false);
  195. { Call linker }
  196. if success then
  197. success:=DoExec('Execute',CmdStr,true,false);
  198. { Remove ReponseFile }
  199. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  200. DeleteFile(outputexedir+Info.ResName);
  201. MakeExecutable:=success; { otherwise a recursive call to link method }
  202. end;
  203. {*****************************************************************************
  204. TLINKERMACOS
  205. *****************************************************************************}
  206. constructor TLinkerMacOS.Create;
  207. begin
  208. Inherited Create;
  209. { allow duplicated libs (PM) }
  210. SharedLibFiles.doubles:=true;
  211. StaticLibFiles.doubles:=true;
  212. end;
  213. procedure TLinkerMacOS.SetMacOS68kInfo;
  214. begin
  215. with Info do
  216. begin
  217. ExeCmd[1]:='ld $DYNLINK $GCSECTIONS $OPT -d -n -o $EXE $RES';
  218. end
  219. end;
  220. procedure TLinkerMacOS.SetDefaultInfo;
  221. begin
  222. case (target_info.system) of
  223. system_m68k_macosclassic: SetMacOS68kInfo;
  224. end;
  225. end;
  226. {procedure TLinkerMacOS.InitSysInitUnitName;
  227. begin
  228. sysinitunit:='si_prc';
  229. end;}
  230. function TLinkerMacOS.WriteResponseFile(isdll: boolean): boolean;
  231. var
  232. linkres : TLinkRes;
  233. i : longint;
  234. HPath : TCmdStrListItem;
  235. s : string;
  236. linklibc : boolean;
  237. begin
  238. WriteResponseFile:=False;
  239. { Open link.res file }
  240. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  241. { Write path to search libraries }
  242. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  243. while assigned(HPath) do
  244. begin
  245. s:=HPath.Str;
  246. if (cs_link_on_target in current_settings.globalswitches) then
  247. s:=ScriptFixFileName(s);
  248. LinkRes.Add('-L'+s);
  249. HPath:=TCmdStrListItem(HPath.Next);
  250. end;
  251. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  252. while assigned(HPath) do
  253. begin
  254. s:=HPath.Str;
  255. if s<>'' then
  256. LinkRes.Add('SEARCH_DIR("'+s+'")');
  257. HPath:=TCmdStrListItem(HPath.Next);
  258. end;
  259. LinkRes.Add('INPUT (');
  260. { add objectfiles, start with prt0 always }
  261. if not (target_info.system in systems_internal_sysinit) then
  262. begin
  263. s:=FindObjectFile('prt0','',false);
  264. LinkRes.AddFileName(maybequoted(s));
  265. end;
  266. while not ObjectFiles.Empty do
  267. begin
  268. s:=ObjectFiles.GetFirst;
  269. if s<>'' then
  270. begin
  271. LinkRes.AddFileName(maybequoted(s));
  272. end;
  273. end;
  274. LinkRes.Add(')');
  275. { Write staticlibraries }
  276. if not StaticLibFiles.Empty then
  277. begin
  278. LinkRes.Add('GROUP(');
  279. while not StaticLibFiles.Empty do
  280. begin
  281. S:=StaticLibFiles.GetFirst;
  282. LinkRes.AddFileName(maybequoted(s));
  283. end;
  284. LinkRes.Add(')');
  285. end;
  286. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  287. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  288. linklibc:=false;
  289. while not SharedLibFiles.Empty do
  290. begin
  291. S:=SharedLibFiles.GetFirst;
  292. if s<>'c' then
  293. begin
  294. i:=Pos(target_info.sharedlibext,S);
  295. if i>0 then
  296. Delete(S,i,255);
  297. LinkRes.Add('-l'+s);
  298. end
  299. else
  300. begin
  301. LinkRes.Add('-l'+s);
  302. linklibc:=true;
  303. end;
  304. end;
  305. { be sure that libc&libgcc is the last lib }
  306. if linklibc then
  307. begin
  308. LinkRes.Add('-lc');
  309. LinkRes.Add('-lgcc');
  310. end;
  311. { Write and Close response }
  312. linkres.writetodisk;
  313. linkres.free;
  314. WriteResponseFile:=True;
  315. end;
  316. function TLinkerMacOS.MakeMacOSExe: boolean;
  317. var
  318. BinStr,
  319. CmdStr : TCmdStr;
  320. StripStr: string[40];
  321. DynLinkStr : string;
  322. GCSectionsStr : string;
  323. ExeName: string;
  324. begin
  325. StripStr:='';
  326. GCSectionsStr:='';
  327. DynLinkStr:='';
  328. if (cs_link_strip in current_settings.globalswitches) then
  329. StripStr:='-s';
  330. if rlinkpath<>'' then
  331. DynLinkStr:='--rpath-link '+rlinkpath;
  332. if create_smartlink_sections then
  333. GCSectionsStr:='--gc-sections ';
  334. ExeName:=current_module.exefilename;
  335. { Call linker }
  336. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  337. binstr:=FindUtil(utilsprefix+BinStr);
  338. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  339. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ExeName)));
  340. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  341. Replace(cmdstr,'$STRIP',StripStr);
  342. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  343. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  344. MakeMacOSExe:=DoExec(BinStr,CmdStr,true,false);
  345. end;
  346. function TLinkerMacOS.MakeExecutable:boolean;
  347. var
  348. success : boolean;
  349. begin
  350. if not(cs_link_nolink in current_settings.globalswitches) then
  351. Message1(exec_i_linking,current_module.exefilename);
  352. { Write used files and libraries }
  353. WriteResponseFile(false);
  354. success:=MakeMacOSExe;
  355. { Remove ReponseFile }
  356. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  357. DeleteFile(outputexedir+Info.ResName);
  358. MakeExecutable:=success; { otherwise a recursive call to link method }
  359. end;
  360. {*****************************************************************************
  361. Initialize
  362. *****************************************************************************}
  363. initialization
  364. {$ifdef m68k}
  365. {$ifndef macos}
  366. RegisterLinker(ld_mpw,TLinkerMacOS);
  367. {$endif}
  368. RegisterTarget(system_m68k_macosclassic_info);
  369. RegisterImport(system_m68k_macosclassic,timportlibmacos);
  370. {$endif m68k}
  371. {$ifdef powerpc}
  372. {$ifndef macos}
  373. RegisterLinker(ld_mpw,TLinkerMacOS);
  374. {$else}
  375. RegisterLinker(ld_mpw,TLinkerMPW);
  376. {$endif}
  377. RegisterTarget(system_powerpc_macosclassic_info);
  378. RegisterImport(system_powerpc_macosclassic,timportlibmacos);
  379. {$endif powerpc}
  380. end.