comprsrc.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_nolink in current_settings.globalswitches) then
  78. begin
  79. Message(exec_e_res_not_found);
  80. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  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_nolink in current_settings.globalswitches) then
  103. begin
  104. Message1(exec_i_compilingresource,fname);
  105. Message2(exec_d_resbin_params,resbin,s);
  106. FlushOutput;
  107. {$IFDEF USE_SYSUTILS}
  108. try
  109. if ExecuteProcess(resbin,s) <> 0 then
  110. begin
  111. Message(exec_e_error_while_linking);
  112. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  113. end;
  114. except
  115. on E:EOSError do
  116. begin
  117. Message(exec_e_cant_call_linker);
  118. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  119. end
  120. end;
  121. {$ELSE USE_SYSUTILS}
  122. swapvectors;
  123. exec(resbin,s);
  124. swapvectors;
  125. if (doserror<>0) then
  126. begin
  127. Message(exec_e_cant_call_linker);
  128. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  129. end
  130. else
  131. if (dosexitcode<>0) then
  132. begin
  133. Message(exec_e_error_while_linking);
  134. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  135. end;
  136. {$ENDIF USE_SYSUTILS}
  137. end;
  138. { Update asmres when externmode is set }
  139. if cs_link_nolink in current_settings.globalswitches then
  140. AsmRes.AddLinkCommand(resbin,s,'');
  141. if ObjUsed then
  142. current_module.linkotherofiles.add(resobj,link_always);
  143. end;
  144. procedure CompileResourceFiles;
  145. var
  146. hr : presourcefile;
  147. begin
  148. { OS/2 (EMX) must be processed elsewhere (in the linking/binding stage).
  149. same with MacOS}
  150. if not (target_info.system in [system_i386_os2,
  151. system_i386_emx,system_powerpc_macos]) then
  152. While not current_module.ResourceFiles.Empty do
  153. begin
  154. if target_info.res<>res_none then
  155. begin
  156. hr:=new(presourcefile,init(current_module.ResourceFiles.getfirst));
  157. hr^.compile;
  158. dispose(hr,done);
  159. end
  160. else
  161. Message(scan_e_resourcefiles_not_supported);
  162. end;
  163. end;
  164. end.