comprsrc.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. {
  2. Copyright (c) 1998-2008 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. uses
  21. Systems, cstreams, cscript;
  22. type
  23. tresoutput = (roRES, roOBJ);
  24. tresourcefile = class(TAbstractResourceFile)
  25. private
  26. fname : ansistring;
  27. protected
  28. function SetupCompilerArguments(output: tresoutput; const OutName :
  29. ansistring; respath: ansistring; out ObjUsed : boolean) : ansistring; virtual;
  30. public
  31. constructor Create(const fn : ansistring);override;
  32. function Compile(output: tresoutput; const OutName: ansistring) : boolean; virtual;
  33. procedure PostProcessResourcefile(const s : ansistring);virtual;
  34. function IsCompiled(const fn : ansistring) : boolean;virtual;
  35. procedure Collect(const fn : ansistring);virtual;
  36. procedure EndCollect; virtual;
  37. end;
  38. TWinLikeResourceFile = class(tresourcefile)
  39. private
  40. fResScript : TScript;
  41. fScriptName : ansistring;
  42. fCollectCount : integer;
  43. protected
  44. function SetupCompilerArguments(output: tresoutput; const OutName :
  45. ansistring; respath: ansistring; out ObjUsed : boolean) : ansistring; override;
  46. public
  47. constructor Create(const fn : ansistring);override;
  48. destructor Destroy; override;
  49. function Compile(output: tresoutput; const OutName: ansistring) : boolean; override;
  50. function IsCompiled(const fn : ansistring) : boolean;override;
  51. procedure Collect(const fn : ansistring);override;
  52. procedure EndCollect; override;
  53. end;
  54. TJVMRawResourceFile = class(TWinLikeResourceFile)
  55. private
  56. protected
  57. public
  58. function Compile(output: tresoutput; const OutName: ansistring) : boolean; override;
  59. function IsCompiled(const fn : ansistring) : boolean;override;
  60. end;
  61. procedure CompileResourceFiles;
  62. procedure CollectResourceFiles;
  63. Var
  64. ResCompiler : String;
  65. RCCompiler : String;
  66. RCForceFPCRes : Boolean;
  67. implementation
  68. uses
  69. SysUtils,
  70. cutils,cfileutl,cclasses,
  71. Globtype,Globals,Verbose,Fmodule, comphook,cpuinfo,rescmn;
  72. {****************************************************************************
  73. TRESOURCEFILE
  74. ****************************************************************************}
  75. constructor tresourcefile.create(const fn : ansistring);
  76. begin
  77. fname:=fn;
  78. end;
  79. procedure tresourcefile.PostProcessResourcefile(const s : ansistring);
  80. begin
  81. end;
  82. function tresourcefile.IsCompiled(const fn: ansistring): boolean;
  83. begin
  84. Result:=CompareText(ExtractFileExt(fn), target_info.resobjext) = 0;
  85. end;
  86. procedure tresourcefile.Collect(const fn: ansistring);
  87. begin
  88. if fn='' then
  89. exit;
  90. fname:=fn;
  91. Compile(roOBJ, ChangeFileExt(fn, target_info.resobjext));
  92. end;
  93. procedure tresourcefile.EndCollect;
  94. begin
  95. end;
  96. function tresourcefile.SetupCompilerArguments(output: tresoutput; const OutName
  97. : ansistring; respath: ansistring; out ObjUsed : boolean) : ansistring;
  98. var
  99. s : TCmdStr;
  100. begin
  101. if output=roRES then
  102. begin
  103. if RCForceFPCRes then
  104. s:=FPCResRCArgs
  105. else
  106. s:=target_res.rccmd;
  107. Replace(s,'$RES',maybequoted(OutName));
  108. Replace(s,'$RC',maybequoted(fname));
  109. ObjUsed:=False;
  110. end
  111. else
  112. begin
  113. s:=target_res.rescmd;
  114. ObjUsed:=(pos('$OBJ',s)>0);
  115. Replace(s,'$OBJ',maybequoted(OutName));
  116. Replace(s,'$RES',maybequoted(fname));
  117. end;
  118. Result:=s;
  119. end;
  120. function tresourcefile.compile(output: tresoutput; const OutName: ansistring)
  121. : boolean;
  122. Function SelectBin(Const Bin1,Bin2 : String) : String;
  123. begin
  124. If (Bin1<>'') then
  125. SelectBin:=Bin1
  126. else
  127. SelectBin:=Bin2;
  128. end;
  129. var
  130. respath,
  131. s,
  132. bin,
  133. resbin : TCmdStr;
  134. resfound,
  135. objused : boolean;
  136. begin
  137. Result:=true;
  138. if output=roRES then
  139. if RCForceFPCRes then
  140. Bin:=SelectBin(RCCompiler,FPCResUtil)
  141. else
  142. Bin:=SelectBin(RCCompiler,target_res.rcbin)
  143. else
  144. Bin:=SelectBin(ResCompiler,target_res.resbin);
  145. if bin='' then
  146. begin
  147. Result:=false;
  148. exit;
  149. end;
  150. resfound:=false;
  151. if utilsdirectory<>'' then
  152. resfound:=FindFile(utilsprefix+bin+source_info.exeext,utilsdirectory,false,resbin);
  153. if not resfound then
  154. begin
  155. resfound:=FindExe(utilsprefix+bin,false,resbin);
  156. if not resfound and (utilsprefix<>'') and ( (output=roRES) or (Pos('$ARCH', target_res.rescmd)<>0) ) then
  157. { Search for resource compiler without utilsprefix, if RC->RES compiler is called }
  158. { or RES->OBJ compiler supports different architectures. }
  159. resfound:=FindExe(bin,false,resbin);
  160. end;
  161. { get also the path to be searched for the windres.h }
  162. respath:=ExtractFilePath(resbin);
  163. if (not resfound) and not(cs_link_nolink in current_settings.globalswitches) then
  164. begin
  165. Message1(exec_e_res_not_found, utilsprefix+bin+source_info.exeext);
  166. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  167. Result:=false;
  168. end;
  169. s:=SetupCompilerArguments(output,OutName,respath,objused);
  170. { Execute the command }
  171. { Always try to compile resources. but don't complain if cs_link_nolink }
  172. if resfound then
  173. begin
  174. Message1(exec_i_compilingresource,fname);
  175. Message2(exec_d_resbin_params,resbin,s);
  176. FlushOutput;
  177. try
  178. if RequotedExecuteProcess(resbin,s) <> 0 then
  179. begin
  180. if not (cs_link_nolink in current_settings.globalswitches) then
  181. Message(exec_e_error_while_compiling_resources);
  182. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  183. Result:=false;
  184. end;
  185. except
  186. on E:EOSError do
  187. begin
  188. if not (cs_link_nolink in current_settings.globalswitches) then
  189. Message1(exec_e_cant_call_resource_compiler, resbin);
  190. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  191. Result:=false;
  192. end
  193. end;
  194. end;
  195. { Update asmres when externmode is set and resource compiling failed }
  196. if (not Result) and (cs_link_nolink in current_settings.globalswitches) then
  197. AsmRes.AddLinkCommand(resbin,s,OutName);
  198. if Result and (output=roOBJ) and ObjUsed then
  199. current_module.linkunitofiles.add(OutName,link_always);
  200. end;
  201. constructor TWinLikeResourceFile.Create(const fn : ansistring);
  202. begin
  203. inherited Create(fn);
  204. fResScript:=nil;
  205. fCollectCount:=0;
  206. if (tf_use_8_3 in target_info.flags) then
  207. fScriptName:=ChangeFileExt(fn,'.rls')
  208. else
  209. fScriptName:=ChangeFileExt(fn,'.reslst');
  210. end;
  211. destructor TWinLikeResourceFile.Destroy;
  212. begin
  213. if fResScript<>nil then
  214. fResScript.Free;
  215. inherited;
  216. end;
  217. function TWinLikeResourceFile.SetupCompilerArguments(output: tresoutput; const
  218. OutName : ansistring; respath : ansistring; out ObjUsed : boolean) : ansistring;
  219. var
  220. srcfilepath,
  221. preprocessorbin,
  222. s : TCmdStr;
  223. arch,
  224. subarch: ansistring;
  225. function WindresFileName(filename: TCmdStr): TCmdStr;
  226. // to be on the safe side, for files that are passed to the preprocessor,
  227. // only give short file names with forward slashes to windres
  228. var
  229. i: longint;
  230. begin
  231. Result := GetShortName(filename);
  232. for I:=1 to Length(Result) do
  233. if Result[I] in AllowDirectorySeparators then
  234. Result[i]:='/';
  235. end;
  236. begin
  237. srcfilepath:=ExtractFilePath(current_module.mainsource);
  238. if output=roRES then
  239. begin
  240. if RCForceFPCRes then
  241. s:=FPCResRCArgs
  242. else
  243. s:=target_res.rccmd;
  244. if (target_res.rcbin = 'windres') and not RCForceFPCRes then
  245. Replace(s,'$RC',WindresFileName(fname))
  246. else
  247. Replace(s,'$RC',maybequoted(fname));
  248. Replace(s,'$RES',maybequoted(OutName));
  249. ObjUsed:=False;
  250. end
  251. else
  252. begin
  253. s:=target_res.rescmd;
  254. if (res_external_file in target_res.resflags) then
  255. ObjUsed:=false
  256. else
  257. ObjUsed:=(pos('$OBJ',s)>0);
  258. Replace(s,'$OBJ',maybequoted(OutName));
  259. subarch:='all';
  260. arch:=cpu2str[target_cpu];
  261. if (target_info.cpu=systems.cpu_arm) then
  262. begin
  263. //Differentiate between arm and armeb
  264. if (target_info.endian=endian_big) then
  265. arch:=arch+'eb';
  266. end;
  267. if target_info.cpu=systems.cpu_powerpc64 then
  268. begin
  269. { differentiate between ppc64 and ppc64le }
  270. if target_info.endian=endian_little then
  271. arch:=arch+'le';
  272. end;
  273. Replace(s,'$ARCH',arch);
  274. if target_info.system=system_arm_ios then
  275. subarch:=lower(cputypestr[current_settings.cputype]);
  276. Replace(s,'$SUBARCH',subarch);
  277. case target_info.endian of
  278. endian_little : Replace(s,'$ENDIAN','littleendian');
  279. endian_big : Replace(s,'$ENDIAN','bigendian');
  280. end;
  281. //call resource compiler with debug switch
  282. if (status.verbosity and V_Debug)<>0 then
  283. Replace(s,'$DBG','-v')
  284. else
  285. Replace(s,'$DBG','');
  286. if fCollectCount=0 then
  287. s:=s+' '+maybequoted(fname)
  288. else
  289. s:=s+' '+maybequoted('@'+fScriptName);
  290. end;
  291. { windres doesn't like empty include paths }
  292. if respath='' then
  293. respath:='.';
  294. Replace(s,'$INC',maybequoted(respath));
  295. if (output=roRes) and (target_res.rcbin='windres') and not RCForceFPCRes then
  296. begin
  297. { try to find a preprocessor }
  298. preprocessorbin := respath+'cpp'+source_info.exeext;
  299. if FileExists(preprocessorbin,true) then
  300. s:='--preprocessor='+preprocessorbin+' '+s;
  301. if (srcfilepath<>'') then
  302. s:='--include '+WindresFileName(srcfilepath)+' '+s;
  303. end;
  304. Result:=s;
  305. end;
  306. function TWinLikeResourceFile.compile(output: tresoutput;
  307. const OutName: ansistring) : boolean;
  308. begin
  309. Result:=inherited compile(output,OutName);
  310. //delete fpc-res.lst file if things went well
  311. if Result and (output=roOBJ) then
  312. DeleteFile(fScriptName);
  313. end;
  314. function TWinLikeResourceFile.IsCompiled(const fn: ansistring): boolean;
  315. const
  316. ResSignature : array [1..32] of byte =
  317. ($00,$00,$00,$00,$20,$00,$00,$00,$FF,$FF,$00,$00,$FF,$FF,$00,$00,
  318. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00);
  319. knownexts : array[1..4] of string[4] = ('.lfm', '.dfm', '.xfm', '.tlb');
  320. var
  321. f : file;
  322. oldfmode : byte;
  323. buf: array[1..32] of byte;
  324. i: longint;
  325. ext : shortstring;
  326. begin
  327. ext:=lower(ExtractFileExt(fn));
  328. Result:=CompareText(ext, target_info.resext) = 0;
  329. if not Result then
  330. for i:=1 to high(knownexts) do
  331. begin
  332. Result:=CompareText(ext, knownexts[i]) = 0;
  333. if Result then break;
  334. end;
  335. if Result or not FileExists(fn, False) then exit;
  336. oldfmode:=Filemode;
  337. Filemode:=0;
  338. assign(f,fn);
  339. reset(f,1);
  340. BlockRead(f, buf, SizeOf(buf), i);
  341. close(f);
  342. Filemode:=oldfmode;
  343. if i<>SizeOf(buf) then
  344. exit;
  345. for i:=1 to 32 do
  346. if buf[i]<>ResSignature[i] then
  347. exit;
  348. Result:=True;
  349. end;
  350. procedure TWinLikeResourceFile.Collect(const fn: ansistring);
  351. begin
  352. if fResScript=nil then
  353. fResScript:=TScript.Create(fScriptName);
  354. fResScript.Add(maybequoted_for_script(fn,script_fpcres));
  355. inc(fCollectCount);
  356. end;
  357. procedure TWinLikeResourceFile.EndCollect;
  358. begin
  359. if fResScript<>nil then
  360. begin
  361. fResScript.WriteToDisk;
  362. FreeAndNil(fResScript);
  363. Compile(roOBJ,ChangeFileExt(fname,target_info.resobjext));
  364. end;
  365. end;
  366. {****************************************************************************
  367. TJVMRawResourceFile
  368. ****************************************************************************}
  369. function TJVMRawResourceFile.Compile(output: tresoutput; const OutName: ansistring): boolean;
  370. begin
  371. if output<>roOBJ then
  372. internalerror(2011081703);
  373. result:=inherited;
  374. end;
  375. function TJVMRawResourceFile.IsCompiled(const fn: ansistring): boolean;
  376. begin
  377. internalerror(2011081704);
  378. result:=true;
  379. end;
  380. function CopyResFile(inf,outf : TCmdStr) : boolean;
  381. var
  382. src,dst : TCCustomFileStream;
  383. begin
  384. { Copy .res file to units output dir. }
  385. Result:=false;
  386. src:=CFileStreamClass.Create(inf,fmOpenRead or fmShareDenyNone);
  387. if CStreamError<>0 then
  388. begin
  389. Message1(exec_e_cant_open_resource_file, src.FileName);
  390. Include(current_settings.globalswitches, cs_link_nolink);
  391. exit;
  392. end;
  393. dst:=CFileStreamClass.Create(current_module.outputpath+outf,fmCreate);
  394. if CStreamError<>0 then
  395. begin
  396. Message1(exec_e_cant_write_resource_file, dst.FileName);
  397. Include(current_settings.globalswitches, cs_link_nolink);
  398. exit;
  399. end;
  400. dst.CopyFrom(src,src.Size);
  401. dst.Free;
  402. src.Free;
  403. Result:=true;
  404. end;
  405. procedure CompileResourceFiles;
  406. var
  407. resourcefile : tresourcefile;
  408. res: TCmdStrListItem;
  409. p,s : TCmdStr;
  410. outfmt : tresoutput;
  411. begin
  412. { Don't do anything for systems supporting resources without using resource
  413. file classes (e.g. Mac OS). They process resources elsewhere. }
  414. if ((target_info.res<>res_none) and (target_res.resourcefileclass=nil)) or
  415. (res_no_compile in target_res.resflags) then
  416. exit;
  417. p:=ExtractFilePath(ExpandFileName(current_module.mainsource));
  418. res:=TCmdStrListItem(current_module.ResourceFiles.First);
  419. while res<>nil do
  420. begin
  421. if target_info.res=res_none then
  422. Message(scan_e_resourcefiles_not_supported);
  423. s:=res.FPStr;
  424. if not path_absolute(s) then
  425. s:=p+s;
  426. if not FileExists(s, True) then
  427. begin
  428. Message1(exec_e_cant_open_resource_file, s);
  429. Include(current_settings.globalswitches, cs_link_nolink);
  430. exit;
  431. end;
  432. resourcefile:=TResourceFile(resinfos[target_info.res]^.resourcefileclass.create(s));
  433. if resourcefile.IsCompiled(s) then
  434. begin
  435. resourcefile.free;
  436. if AnsiCompareFileName(IncludeTrailingPathDelimiter(ExpandFileName(current_module.outputpath)), p) <> 0 then
  437. begin
  438. { Copy .res file to units output dir. Otherwise .res file will not be found
  439. when only compiled units path is available }
  440. res.FPStr:=ExtractFileName(res.FPStr); //store file name only in PPU.
  441. if not CopyResFile(s,res.FPStr) then exit;
  442. end;
  443. end
  444. else
  445. begin
  446. res.FPStr:=ExtractFileName(res.FPStr);
  447. if (target_res.rcbin='') and (RCCompiler='') then
  448. begin
  449. { if target does not have .rc to .res compiler, create obj }
  450. outfmt:=roOBJ;
  451. res.FPStr:=ChangeFileExt(res.FPStr,target_info.resobjext);
  452. end
  453. else
  454. begin
  455. outfmt:=roRES;
  456. res.FPStr:=ChangeFileExt(res.FPStr,target_info.resext);
  457. end;
  458. resourcefile.compile(outfmt, current_module.outputpath+res.FPStr);
  459. resourcefile.free;
  460. end;
  461. res:=TCmdStrListItem(res.Next);
  462. end;
  463. end;
  464. procedure CollectResourceFiles;
  465. var
  466. resourcefile : tresourcefile;
  467. procedure ProcessModule(u : tmodule);
  468. var
  469. res : TCmdStrListItem;
  470. s : TCmdStr;
  471. begin
  472. res:=TCmdStrListItem(u.ResourceFiles.First);
  473. while assigned(res) do
  474. begin
  475. if path_absolute(res.FPStr) then
  476. s:=res.FPStr
  477. else
  478. begin
  479. s:=u.path+res.FPStr;
  480. if not FileExists(s,True) then
  481. s:=u.outputpath+res.FPStr;
  482. end;
  483. resourcefile.Collect(s);
  484. res:=TCmdStrListItem(res.Next);
  485. end;
  486. end;
  487. var
  488. hp : tused_unit;
  489. s : TCmdStr;
  490. begin
  491. if (target_info.res=res_none) or ((target_res.resbin='')
  492. and (ResCompiler='')) then
  493. exit;
  494. // if cs_link_nolink in current_settings.globalswitches then
  495. // exit;
  496. s:=ChangeFileExt(current_module.ppufilename,target_info.resobjext);
  497. if (res_arch_in_file_name in target_res.resflags) then
  498. s:=ChangeFileExt(s,'.'+cpu2str[target_cpu]+target_info.resobjext);
  499. resourcefile:=TResourceFile(resinfos[target_info.res]^.resourcefileclass.create(s));
  500. hp:=tused_unit(usedunits.first);
  501. while assigned(hp) do
  502. begin
  503. ProcessModule(hp.u);
  504. hp:=tused_unit(hp.next);
  505. end;
  506. ProcessModule(current_module);
  507. { Finish collection }
  508. resourcefile.EndCollect;
  509. resourcefile.free;
  510. end;
  511. procedure initglobals;
  512. begin
  513. ResCompiler:='';
  514. RCCompiler:='';
  515. RCForceFPCRes:=false;
  516. end;
  517. initialization
  518. register_initdone_proc(@initglobals,nil);
  519. end.