fpcres.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. {
  2. FPCRes - Free Pascal Resource Converter
  3. Part of the Free Pascal distribution
  4. Copyright (C) 2008 by Giulio Bernardi
  5. See the file COPYING, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. }
  11. { Note: This program is not the old fpcres by Simon Kissel }
  12. program fpcres;
  13. {$MODE OBJFPC} {$H+}
  14. uses
  15. SysUtils, Classes, paramparser, target, msghandler, sourcehandler,
  16. closablefilestream, resource,
  17. //readers
  18. resreader, coffreader, winpeimagereader, elfreader, machoreader,
  19. externalreader, dfmreader, tlbreader,
  20. //writers
  21. reswriter, coffwriter, elfwriter, machowriter, externalwriter,
  22. //misc
  23. elfconsts, cofftypes, machotypes, externaltypes
  24. ;
  25. const
  26. halt_no_err = 0;
  27. halt_param_err = 1;
  28. halt_read_err = 2;
  29. halt_write_err = 3;
  30. progname = 'fpcres';
  31. progversion = '2.0'; //to distinguish from the old fpcres
  32. fpcversion = {$INCLUDE %FPCVERSION%};
  33. host_arch = {$INCLUDE %FPCTARGETCPU%};
  34. host_os = {$INCLUDE %FPCTARGETOS%};
  35. build_date = {$INCLUDE %DATE%};
  36. var
  37. params : TParameters = nil;
  38. resources : TResources = nil;
  39. sourcefiles : TSourceFiles = nil;
  40. procedure ShowVersion;
  41. begin
  42. writeln(progname+' - resource file converter, version '+progversion+' ['+build_date+'], FPC '+fpcversion);
  43. writeln('Host platform: '+host_os+' - '+host_arch);
  44. writeln('Copyright (c) 2008 by Giulio Bernardi.');
  45. end;
  46. procedure ShowHelp;
  47. begin
  48. ShowVersion;
  49. writeln('Syntax: '+progname+' [options] <inputfile> [<inputfile>...] [-o <outputfile>]');
  50. writeln;
  51. writeln('Options:');
  52. writeln(' --help, -h, -? Show this screen.');
  53. writeln(' --version, -V Show program version.');
  54. writeln(' --verbose, -v Be verbose.');
  55. writeln(' --input, -i <x> Ignored for compatibility.');
  56. writeln(' --output, -o <x> Set the output file name.');
  57. writeln(' -of <format> Set the output file format. Supported formats:');
  58. writeln(' res, elf, coff, mach-o, external');
  59. writeln(' --arch, -a <name> Set object file architecture. Supported architectures:');
  60. writeln(' i386, x86_64, arm (coff)');
  61. writeln(' i386, x86_64, powerpc, powerpc64, arm, armeb, m68k,');
  62. writeln(' sparc, alpha, ia64 (elf)');
  63. writeln(' i386, x86_64, powerpc, powerpc64, arm (mach-o)');
  64. writeln(' bigendian, littleendian (external)');
  65. writeln(' --subarch, -s <name> Set object file sub-architecture. Supported values:');
  66. writeln(' arm: all, v4t, v6, v5tej, xscale, v7');
  67. writeln(' other architectures: all');
  68. writeln(' @<file> Read more options from file <file>');
  69. writeln('Default output target: '+TargetToStr(currenttarget));
  70. end;
  71. const
  72. SOutputFileAlreadySet = 'Output file name already set.';
  73. SUnknownParameter = 'Unknown parameter ''%s''';
  74. SArgumentMissing = 'Argument missing for option ''%s''';
  75. SUnknownObjFormat = 'Unknown file format ''%s''';
  76. SUnknownMachine = 'Unknown architecture ''%s''';
  77. SFormatArchMismatch = 'Architecture %s is not available for %s format';
  78. SNoInputFiles = 'No input files';
  79. SNoOutputFile = 'No output file name specified';
  80. SCannotReadConfFile ='Can''t read config file ''%s''';
  81. SCantOpenFile = 'Can''t open file ''%s''';
  82. SUnknownInputFormat = 'No known file format detected for file ''%s''';
  83. SCantCreateFile = 'Can''t create file ''%s''';
  84. function GetCurrentTimeMsec : longint;
  85. var h,m,s,ms : word;
  86. begin
  87. DecodeTime(Time,h,m,s,ms);
  88. Result:=h*3600*1000 + m*60*1000 + s*1000 + ms;
  89. end;
  90. procedure CheckTarget;
  91. begin
  92. //if user explicitally set a format, use it
  93. if params.Target.objformat<>ofNone then
  94. CurrentTarget.objformat:=params.Target.objformat;
  95. //if no machine was specified, check if current is ok for this format,
  96. //otherwise pick the default one for that format
  97. if params.Target.machine=mtNone then
  98. begin
  99. if not (CurrentTarget.machine in ObjFormats[CurrentTarget.objformat].machines) then
  100. begin
  101. CurrentTarget.machine:=GetDefaultMachineForFormat(CurrentTarget.objformat);
  102. CurrentTarget.submachine:=GetDefaultSubMachineForMachine(currentTarget.machine);
  103. end
  104. end
  105. else
  106. begin
  107. CurrentTarget.machine:=params.Target.machine;
  108. CurrentTarget.submachine:=params.Target.submachine;
  109. end;
  110. if not (CurrentTarget.machine in ObjFormats[CurrentTarget.objformat].machines) then
  111. begin
  112. Messages.DoError(Format(SFormatArchMismatch,[
  113. MachineToStr(CurrentTarget.machine),ObjFormatToStr(CurrentTarget.objformat)]));
  114. halt(halt_param_err);
  115. end;
  116. Messages.DoVerbose('target set to '+TargetToStr(CurrentTarget));
  117. end;
  118. procedure CheckInputFiles;
  119. begin
  120. if params.InputFiles.Count=0 then
  121. begin
  122. Messages.DoError(SNoInputFiles);
  123. halt(halt_param_err);
  124. end;
  125. end;
  126. procedure CheckOutputFile;
  127. var tmp : string;
  128. begin
  129. if params.OutputFile<>'' then exit;
  130. if params.InputFiles.Count>1 then
  131. begin
  132. Messages.DoError(SNoOutputFile);
  133. halt(halt_param_err);
  134. end;
  135. tmp:=ChangeFileExt(ExtractFileName(params.InputFiles[0]),
  136. ObjFormats[CurrentTarget.objformat].ext);
  137. if lowercase(tmp)=lowercase(params.InputFiles[0]) then
  138. tmp:=tmp+ObjFormats[CurrentTarget.objformat].ext;
  139. params.OutputFile:=tmp;
  140. end;
  141. procedure ParseParams;
  142. var msg : string;
  143. begin
  144. Messages.DoVerbose('parsing command line parameters');
  145. msg:='';
  146. if ParamCount = 0 then
  147. begin
  148. ShowHelp;
  149. halt(halt_no_err);
  150. end;
  151. params:=TParameters.Create;
  152. try
  153. params.Parse;
  154. except
  155. on e : EOutputFileAlreadySetException do msg:=SOutputFileAlreadySet;
  156. on e : EUnknownParameterException do msg:=Format(SUnknownParameter,[e.Message]);
  157. on e : EArgumentMissingException do msg:=Format(SArgumentMissing,[e.Message]);
  158. on e : EUnknownObjFormatException do msg:=Format(SUnknownObjFormat,[e.Message]);
  159. on e : EUnknownMachineException do msg:=Format(SUnknownMachine,[e.Message]);
  160. on e : ECannotReadConfFile do msg:=Format(SCannotReadConfFile,[e.Message]);
  161. end;
  162. Messages.Verbose:=params.Verbose;
  163. if msg<>'' then
  164. begin
  165. Messages.DoError(msg);
  166. halt(halt_param_err);
  167. end;
  168. if params.Version then
  169. begin
  170. ShowVersion;
  171. halt(halt_no_err);
  172. end;
  173. if params.Help then
  174. begin
  175. ShowHelp;
  176. halt(halt_no_err);
  177. end;
  178. CheckTarget;
  179. CheckInputFiles;
  180. CheckOutputFile;
  181. Messages.DoVerbose('finished parsing command line parameters');
  182. end;
  183. procedure LoadSourceFiles;
  184. var msg : string;
  185. begin
  186. msg:='';
  187. resources:=TResources.Create;
  188. sourcefiles:=TSourceFiles.Create;
  189. sourcefiles.FileList.AddStrings(params.InputFiles);
  190. try
  191. sourcefiles.Load(resources);
  192. except
  193. on e : ECantOpenFileException do msg:=Format(SCantOpenFile,[e.Message]);
  194. on e : EUnknownInputFormatException do msg:=Format(SUnknownInputFormat,[e.Message]);
  195. on e : Exception do
  196. begin
  197. if e.Message='' then msg:=e.ClassName
  198. else msg:=e.Message;
  199. end;
  200. end;
  201. if msg<>'' then
  202. begin
  203. Messages.DoError(msg);
  204. halt(halt_read_err);
  205. end;
  206. end;
  207. function SetUpResWriter : TResResourceWriter;
  208. begin
  209. Result:=TResResourceWriter.Create;
  210. end;
  211. function SetUpElfWriter : TElfResourceWriter;
  212. begin
  213. Result:=TElfResourceWriter.Create;
  214. case CurrentTarget.machine of
  215. // mtnone :
  216. mti386 : Result.MachineType:=emti386;
  217. mtx86_64 : Result.MachineType:=emtx86_64;
  218. mtppc : Result.MachineType:=emtppc;
  219. mtppc64 : Result.MachineType:=emtppc64;
  220. mtarm : Result.MachineType:=emtarm;
  221. mtarmeb : Result.MachineType:=emtarmeb;
  222. mtm68k : Result.MachineType:=emtm68k;
  223. mtsparc : Result.MachineType:=emtsparc;
  224. mtalpha : Result.MachineType:=emtalpha;
  225. mtia64 : Result.MachineType:=emtia64;
  226. end;
  227. end;
  228. function SetUpCoffWriter : TCoffResourceWriter;
  229. begin
  230. Result:=TCoffResourceWriter.Create;
  231. case CurrentTarget.machine of
  232. // mtnone :
  233. mti386 : Result.MachineType:=cmti386;
  234. mtarm : Result.MachineType:=cmtarm;
  235. mtx86_64 : Result.MachineType:=cmtx8664;
  236. end;
  237. end;
  238. function SetUpMachOWriter : TMachOResourceWriter;
  239. const
  240. ArmSubMachine2MachOSubMachine: array[TSubMachineTypeArm] of TMachOSubMachineTypeArm =
  241. (msmarm_all,msmarm_v4t,msmarm_v6,msmarm_v5tej,msmarm_xscale,msmarm_v7);
  242. var
  243. MachOSubMachineType: TMachoSubMachineType;
  244. begin
  245. Result:=TMachOResourceWriter.Create;
  246. case CurrentTarget.machine of
  247. // mtnone :
  248. mti386 :
  249. begin
  250. Result.MachineType:=mmti386;
  251. MachOSubMachineType.f386SubType:=msm386_all;
  252. end;
  253. mtx86_64 :
  254. begin
  255. Result.MachineType:=mmtx86_64;
  256. MachOSubMachineType.fX64SubType:=msmx64_all;
  257. end;
  258. mtppc :
  259. begin
  260. Result.MachineType:=mmtpowerpc;
  261. MachOSubMachineType.fPpcSubType:=msmppc_all;
  262. end;
  263. mtppc64 :
  264. begin
  265. Result.MachineType:=mmtpowerpc64;
  266. MachOSubMachineType.fPpc64SubType:=msmppc64_all;
  267. end;
  268. mtarm :
  269. begin
  270. Result.MachineType:=mmtarm;
  271. MachOSubMachineType.fArmSubType:=ArmSubMachine2MachOSubMachine[CurrentTarget.submachine.subarm];
  272. end;
  273. end;
  274. Result.SubMachineType:=MachOSubMachineType;
  275. end;
  276. function SetUpExternalWriter : TExternalResourceWriter;
  277. begin
  278. Result:=TExternalResourceWriter.Create;
  279. case CurrentTarget.machine of
  280. // mtnone :
  281. mtBigEndian : Result.Endianess:=EXT_ENDIAN_BIG;
  282. mtLittleEndian : Result.Endianess:=EXT_ENDIAN_LITTLE;
  283. end;
  284. end;
  285. procedure WriteOutputFile;
  286. var aStream : TClosableFileStream;
  287. aWriter : TAbstractResourceWriter;
  288. msg : string;
  289. begin
  290. Messages.DoVerbose(Format('Trying to create output file %s...',[params.OutputFile]));
  291. try
  292. aStream:=TClosableFileStream.Create(params.OutputFile,fmCreate or fmShareDenyWrite);
  293. except
  294. Messages.DoError(Format(SCantCreateFile,[params.OutputFile]));
  295. halt(halt_write_err);
  296. end;
  297. try
  298. Messages.DoVerbose('Setting up resource writer...');
  299. case CurrentTarget.objformat of
  300. ofRes : aWriter:=SetUpResWriter;
  301. ofElf : aWriter:=SetUpElfWriter;
  302. ofCoff : aWriter:=SetUpCoffWriter;
  303. ofMachO : aWriter:=SetUpMachOWriter;
  304. ofExt : aWriter:=SetUpExternalWriter;
  305. end;
  306. try
  307. Messages.DoVerbose(Format('Writing output file %s...',[params.OutputFile]));
  308. try
  309. resources.WriteToStream(aStream,aWriter);
  310. except
  311. on e : Exception do
  312. begin
  313. if e.Message='' then msg:=e.ClassName
  314. else msg:=e.Message;
  315. Messages.DoError(msg);
  316. halt(halt_write_err);
  317. end;
  318. end;
  319. Messages.DoVerbose(Format('Output file %s written',[params.OutputFile]));
  320. finally
  321. aWriter.Free;
  322. end;
  323. finally
  324. aStream.Free;
  325. end;
  326. end;
  327. procedure Cleanup;
  328. begin
  329. Messages.DoVerbose('Cleaning up');
  330. if Resources<>nil then Resources.Free;
  331. if SourceFiles<>nil then SourceFiles.Free;
  332. if Params<>nil then Params.Free;
  333. end;
  334. var before, elapsed : longint;
  335. begin
  336. try
  337. before:=GetCurrentTimeMsec;
  338. ParseParams;
  339. LoadSourceFiles;
  340. WriteOutputFile;
  341. elapsed:=GetCurrentTimeMsec-before;
  342. if elapsed<0 then elapsed:=24*3600*1000 + elapsed;
  343. Messages.DoVerbose(Format('Time elapsed: %d.%d seconds',[elapsed div 1000,(elapsed mod 1000) div 10]));
  344. finally
  345. Cleanup;
  346. end;
  347. end.