t_win16.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i8086) Win16 target
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit t_win16;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. SysUtils,
  24. cutils,cfileutl,cclasses,
  25. globtype,globals,systems,verbose,script,
  26. import,fmodule,i_win16,
  27. link,aasmbase,cpuinfo,
  28. omfbase,ogbase,ogomf,owomflib;
  29. type
  30. { TImportLibWin16 }
  31. TImportLibWin16=class(timportlib)
  32. public
  33. procedure generatelib;override;
  34. end;
  35. { the (Open) Watcom linker }
  36. TExternalLinkerWin16WLink=class(texternallinker)
  37. private
  38. Function WriteResponseFile(isdll:boolean) : Boolean;
  39. public
  40. constructor Create;override;
  41. procedure SetDefaultInfo;override;
  42. function MakeExecutable:boolean;override;
  43. end;
  44. var
  45. importlist: array of string;
  46. {****************************************************************************
  47. TImportLibWin16
  48. ****************************************************************************}
  49. procedure TImportLibWin16.generatelib;
  50. var
  51. i: Integer;
  52. j: Integer;
  53. ImportLibrary: TImportLibrary;
  54. ImportSymbol: TImportSymbol;
  55. begin
  56. for i:=0 to current_module.ImportLibraryList.Count-1 do
  57. begin
  58. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  59. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  60. begin
  61. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  62. SetLength(importlist, Length(importlist)+1);
  63. WriteStr(importlist[high(importlist)],'import ',ImportSymbol.Name,' ',ImportLibrary.Name);
  64. end;
  65. end;
  66. end;
  67. {****************************************************************************
  68. TExternalLinkerWin16WLink
  69. ****************************************************************************}
  70. function TExternalLinkerWin16WLink.WriteResponseFile(isdll: boolean): Boolean;
  71. Var
  72. linkres : TLinkRes;
  73. s : string;
  74. i: Integer;
  75. begin
  76. WriteResponseFile:=False;
  77. { Open link.res file }
  78. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  79. { Add all options to link.res instead of passing them via command line:
  80. DOS command line is limited to 126 characters! }
  81. LinkRes.Add('option quiet');
  82. if target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
  83. LinkRes.Add('debug dwarf');
  84. { add objectfiles, start with prt0 always }
  85. case current_settings.x86memorymodel of
  86. mm_tiny: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0t','',false)));
  87. mm_small: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0s','',false)));
  88. mm_medium: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0m','',false)));
  89. mm_compact: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0c','',false)));
  90. mm_large: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0l','',false)));
  91. mm_huge: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0h','',false)));
  92. end;
  93. while not ObjectFiles.Empty do
  94. begin
  95. s:=ObjectFiles.GetFirst;
  96. if s<>'' then
  97. LinkRes.Add('file ' + maybequoted(s));
  98. end;
  99. while not StaticLibFiles.Empty do
  100. begin
  101. s:=StaticLibFiles.GetFirst;
  102. if s<>'' then
  103. LinkRes.Add('library '+MaybeQuoted(s));
  104. end;
  105. LinkRes.Add('format windows');
  106. if (cs_link_map in current_settings.globalswitches) then
  107. LinkRes.Add('option map='+maybequoted(ChangeFileExt(current_module.exefilename,'.map')));
  108. LinkRes.Add('name ' + maybequoted(current_module.exefilename));
  109. { LinkRes.Add('import InitTask KERNEL');
  110. LinkRes.Add('import WaitEvent KERNEL');
  111. LinkRes.Add('import InitApp USER');}
  112. for i:=low(importlist) to high(importlist) do
  113. LinkRes.Add(importlist[i]);
  114. { Write and Close response }
  115. linkres.writetodisk;
  116. LinkRes.Free;
  117. WriteResponseFile:=True;
  118. end;
  119. constructor TExternalLinkerWin16WLink.Create;
  120. begin
  121. Inherited Create;
  122. { allow duplicated libs (PM) }
  123. SharedLibFiles.doubles:=true;
  124. StaticLibFiles.doubles:=true;
  125. end;
  126. procedure TExternalLinkerWin16WLink.SetDefaultInfo;
  127. begin
  128. with Info do
  129. begin
  130. ExeCmd[1]:='wlink $OPT $RES';
  131. end;
  132. end;
  133. function TExternalLinkerWin16WLink.MakeExecutable: boolean;
  134. var
  135. binstr,
  136. cmdstr : TCmdStr;
  137. success : boolean;
  138. begin
  139. if not(cs_link_nolink in current_settings.globalswitches) then
  140. Message1(exec_i_linking,current_module.exefilename);
  141. { Write used files and libraries and our own tlink script }
  142. WriteResponsefile(false);
  143. { Call linker }
  144. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  145. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  146. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  147. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  148. { Remove ReponseFile }
  149. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  150. DeleteFile(outputexedir+Info.ResName);
  151. MakeExecutable:=success; { otherwise a recursive call to link method }
  152. end;
  153. {*****************************************************************************
  154. Initialize
  155. *****************************************************************************}
  156. initialization
  157. RegisterLinker(ld_win16,TExternalLinkerWin16WLink);
  158. RegisterImport(system_i8086_win16,TImportLibWin16);
  159. RegisterTarget(system_i8086_win16_info);
  160. end.