comprsrc.pas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Handles the resource files handling
  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 comprsrc;
  18. {$i fpcdefs.inc}
  19. interface
  20. type
  21. presourcefile=^tresourcefile;
  22. tresourcefile=object
  23. private
  24. fname : string;
  25. public
  26. constructor Init(const fn:string);
  27. destructor Done;
  28. procedure Compile;virtual;
  29. end;
  30. procedure CompileResourceFiles;
  31. implementation
  32. uses
  33. {$IFDEF USE_SYSUTILS}
  34. SysUtils,
  35. {$ELSE USE_SYSUTILS}
  36. dos,
  37. {$ENDIF USE_SYSUTILS}
  38. Systems,cutils,Globtype,Globals,Verbose,Fmodule,
  39. Script;
  40. {****************************************************************************
  41. TRESOURCEFILE
  42. ****************************************************************************}
  43. constructor tresourcefile.init(const fn:string);
  44. begin
  45. fname:=fn;
  46. end;
  47. destructor tresourcefile.done;
  48. begin
  49. end;
  50. procedure tresourcefile.compile;
  51. var
  52. respath,
  53. srcfilepath : dirstr;
  54. n : namestr;
  55. {$IFDEF USE_SYSUTILS}
  56. {$ELSE USE_SYSUTILS}
  57. e : extstr;
  58. {$ENDIF USE_SYSUTILS}
  59. s,
  60. resobj,
  61. resbin : string;
  62. resfound,
  63. objused : boolean;
  64. begin
  65. resbin:='';
  66. resfound:=false;
  67. if utilsdirectory<>'' then
  68. resfound:=FindFile(utilsprefix+target_res.resbin+source_info.exeext,utilsdirectory,resbin);
  69. if not resfound then
  70. resfound:=FindExe(utilsprefix+target_res.resbin,resbin);
  71. { get also the path to be searched for the windres.h }
  72. {$IFDEF USE_SYSUTILS}
  73. respath := SplitPath(resbin);
  74. {$ELSE USE_SYSUTILS}
  75. fsplit(resbin,respath,n,e);
  76. {$ENDIF USE_SYSUTILS}
  77. if (not resfound) and not(cs_link_extern in aktglobalswitches) then
  78. begin
  79. Message(exec_e_res_not_found);
  80. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  81. end;
  82. {$IFDEF USE_SYSUTILS}
  83. srcfilepath := SplitPath(current_module.mainsource^);
  84. {$ELSE USE_SYSUTILS}
  85. fsplit(current_module.mainsource^,srcfilepath,n,e);
  86. {$ENDIF USE_SYSUTILS}
  87. if not path_absolute(fname) then
  88. fname:=srcfilepath+fname;
  89. resobj:=ForceExtension(fname,target_info.resobjext);
  90. s:=target_res.rescmd;
  91. ObjUsed:=(pos('$OBJ',s)>0);
  92. Replace(s,'$OBJ',maybequoted(resobj));
  93. Replace(s,'$RES',maybequoted(fname));
  94. { windres doesn't like empty include paths }
  95. if respath='' then
  96. respath:='.';
  97. Replace(s,'$INC',maybequoted(respath));
  98. if (target_info.system = system_i386_win32) and
  99. (srcfilepath<>'') then
  100. s:=s+' --include '+maybequoted(srcfilepath);
  101. { Execute the command }
  102. if not (cs_link_extern in aktglobalswitches) then
  103. begin
  104. Message1(exec_i_compilingresource,fname);
  105. Message2(exec_d_resbin_params,resbin,s);
  106. {$IFDEF USE_SYSUTILS}
  107. try
  108. if ExecuteProcess(resbin,s) <> 0 then
  109. begin
  110. Message(exec_e_error_while_linking);
  111. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  112. end;
  113. except
  114. on E:EOSError do
  115. begin
  116. Message(exec_e_cant_call_linker);
  117. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  118. end
  119. end;
  120. {$ELSE USE_SYSUTILS}
  121. swapvectors;
  122. exec(resbin,s);
  123. swapvectors;
  124. if (doserror<>0) then
  125. begin
  126. Message(exec_e_cant_call_linker);
  127. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  128. end
  129. else
  130. if (dosexitcode<>0) then
  131. begin
  132. Message(exec_e_error_while_linking);
  133. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  134. end;
  135. {$ENDIF USE_SYSUTILS}
  136. end;
  137. { Update asmres when externmode is set }
  138. if cs_link_extern in aktglobalswitches then
  139. AsmRes.AddLinkCommand(resbin,s,'');
  140. if ObjUsed then
  141. current_module.linkotherofiles.add(resobj,link_allways);
  142. end;
  143. procedure CompileResourceFiles;
  144. var
  145. hr : presourcefile;
  146. begin
  147. { OS/2 (EMX) must be processed elsewhere (in the linking/binding stage).
  148. same with MacOS}
  149. if not (target_info.system in [system_i386_os2,
  150. system_i386_emx,system_powerpc_macos]) then
  151. While not current_module.ResourceFiles.Empty do
  152. begin
  153. if target_info.res<>res_none then
  154. begin
  155. hr:=new(presourcefile,init(current_module.ResourceFiles.getfirst));
  156. hr^.compile;
  157. dispose(hr,done);
  158. end
  159. else
  160. Message(scan_e_resourcefiles_not_supported);
  161. end;
  162. end;
  163. end.