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