mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-27 00:23:44 +02:00

svn+ssh://svn.freepascal.org/FPC/svn/fpc/branches/resources ........ r9694 | michael | 2008-01-09 21:31:18 +0100 (Wed, 09 Jan 2008) | 1 line * Initial check-in ........ r9695 | michael | 2008-01-09 21:35:58 +0100 (Wed, 09 Jan 2008) | 1 line * New version from Giulio Bernardi ........ r9697 | michael | 2008-01-09 21:41:54 +0100 (Wed, 09 Jan 2008) | 1 line * Patch from Giulio Bernardi with resource support ........ r9698 | michael | 2008-01-09 21:46:33 +0100 (Wed, 09 Jan 2008) | 1 line * Patch from Giulio Bernardi to add more resource testing ........ r9699 | michael | 2008-01-09 21:57:26 +0100 (Wed, 09 Jan 2008) | 1 line * New tool from Giulio Bernardi ........ r9700 | michael | 2008-01-09 21:58:23 +0100 (Wed, 09 Jan 2008) | 1 line * New tool from Giulio Bernardi ........ r9701 | michael | 2008-01-09 22:01:54 +0100 (Wed, 09 Jan 2008) | 1 line * Added fcl-res ........ r9702 | michael | 2008-01-09 22:01:58 +0100 (Wed, 09 Jan 2008) | 1 line * Added fcl-res ........ r9703 | michael | 2008-01-10 08:54:26 +0100 (Thu, 10 Jan 2008) | 1 line * Fixed double code ........ r9704 | jonas | 2008-01-10 10:59:20 +0100 (Thu, 10 Jan 2008) | 2 lines - removed duplicate code ........ r9705 | jonas | 2008-01-10 11:25:21 +0100 (Thu, 10 Jan 2008) | 2 lines + added missing fcl-res dependencies ........ r9706 | jonas | 2008-01-10 11:58:30 +0100 (Thu, 10 Jan 2008) | 2 lines + dependencies for fpintres and fpextres ........ r9707 | yury | 2008-01-10 12:47:51 +0100 (Thu, 10 Jan 2008) | 3 lines * Fixed compilation of resource, which is included in a unit located in different folder than main source. * .res files must be copied to units output folder, otherwise .res files will not be found when only compiled units path is available and compiler does not know anything about sources folder. * Improved resource related error messages. ........ r9708 | michael | 2008-01-10 12:52:13 +0100 (Thu, 10 Jan 2008) | 1 line * Removed double source after end. ........ r9709 | michael | 2008-01-10 12:52:48 +0100 (Thu, 10 Jan 2008) | 1 line * No longer needed ........ r9710 | tom_at_work | 2008-01-10 22:09:08 +0100 (Thu, 10 Jan 2008) | 1 line * properly align FPC_RESLOCATION so that linking does not fail on some architectures (e.g. ppc64) ........ r9711 | tom_at_work | 2008-01-10 23:53:12 +0100 (Thu, 10 Jan 2008) | 1 line * fix splitting of 64 bit load/stores from/to unaligned memory locations into multiple load/stores, which in some cases generated wrong code ........ r9712 | michael | 2008-01-11 11:00:08 +0100 (Fri, 11 Jan 2008) | 1 line * Fixed bug in BSS section on 64-bit platforms ........ r9720 | giulio | 2008-01-12 10:02:04 +0100 (Sat, 12 Jan 2008) | 1 line Updated fcl-res documentation: occurrences of reslib changed to fcl-res. ........ r9740 | giulio | 2008-01-13 19:36:44 +0100 (Sun, 13 Jan 2008) | 3 lines - Don't try to compile resources on systems with a non windows-like resource support. - Don't add the .or file to the list of object files if resource compiling failed. ........ r10201 | giulio | 2008-02-04 11:35:44 +0100 (Mon, 04 Feb 2008) | 5 lines * resource compiling supported on OS/2 via wrc * CompileResourceFiles and CollectResourceFiles don't do target-specific checks anymore * refactored a bit ........ r10389 | giulio | 2008-02-25 21:32:52 +0100 (Mon, 25 Feb 2008) | 2 lines Deleted test file which was committed by mistake ........ r10472 | giulio | 2008-03-10 12:22:18 +0100 (Mon, 10 Mar 2008) | 2 lines changed define FPC_HAS_RESOURCES to FPC_HAS_WINLIKERESOURCES ........ git-svn-id: trunk@10481 -
272 lines
6.9 KiB
ObjectPascal
272 lines
6.9 KiB
ObjectPascal
{
|
|
|
|
FPCResLipo - Free Pascal External Resource Thinner
|
|
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.
|
|
}
|
|
|
|
program fpcreslipo;
|
|
|
|
{$MODE OBJFPC} {$H+}
|
|
|
|
uses
|
|
SysUtils, Classes, paramparser, msghandler, sourcehandler,
|
|
resource, externalreader, externalwriter;
|
|
|
|
const
|
|
halt_no_err = 0;
|
|
halt_param_err = 1;
|
|
halt_read_err = 2;
|
|
halt_write_err = 3;
|
|
|
|
progname = 'fpcreslipo';
|
|
progversion = '1.0';
|
|
|
|
fpcversion = {$INCLUDE %FPCVERSION%};
|
|
host_arch = {$INCLUDE %FPCTARGETCPU%};
|
|
host_os = {$INCLUDE %FPCTARGETOS%};
|
|
build_date = {$INCLUDE %DATE%};
|
|
|
|
var
|
|
params : TParameters = nil;
|
|
sourcefiles : TSourceFiles = nil;
|
|
outResources : TResources = nil;
|
|
|
|
procedure ShowVersion;
|
|
begin
|
|
writeln(progname+' - external resource file thinner, 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(' --output, -o <x> Set the output file name.');
|
|
writeln(' --endian, -e <x> Set shared file endianess (big, little)');
|
|
writeln(' default is big');
|
|
writeln;
|
|
writeln('Example:');
|
|
writeln(' '+progname+' myprog.i386.fpcres myprog.powerpc.fpcres -o myprog.fpcres');
|
|
writeln;
|
|
writeln(' strips common resources from the two input files and puts them in the');
|
|
writeln(' output file');
|
|
end;
|
|
|
|
const
|
|
SOutputFileAlreadySet = 'Output file name already set.';
|
|
SUnknownParameter = 'Unknown parameter ''%s''';
|
|
SArgumentMissing = 'Argument missing for option ''%s''';
|
|
SUnknownEndianess = 'Unknown endianess ''%s''';
|
|
SNoInputFiles = 'No input files';
|
|
STooFewInputFiles = 'At least two input files must be specified';
|
|
SNoOutputFile = 'No output file name specified';
|
|
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 CheckInputFiles;
|
|
begin
|
|
if params.InputFiles.Count<2 then
|
|
begin
|
|
case params.InputFiles.Count of
|
|
0 : Messages.DoError(SNoInputFiles);
|
|
1 : Messages.DoError(STooFewInputFiles);
|
|
end;
|
|
halt(halt_param_err);
|
|
end;
|
|
end;
|
|
|
|
procedure CheckOutputFile;
|
|
begin
|
|
if params.OutputFile<>'' then exit;
|
|
Messages.DoError(SNoOutputFile);
|
|
halt(halt_param_err);
|
|
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 : EUnknownEndianessException do msg:=Format(SUnknownEndianess,[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;
|
|
|
|
CheckInputFiles;
|
|
CheckOutputFile;
|
|
|
|
Messages.DoVerbose('finished parsing command line parameters');
|
|
end;
|
|
|
|
procedure LoadSourceFiles;
|
|
var msg : string;
|
|
i : integer;
|
|
begin
|
|
msg:='';
|
|
sourcefiles:=TSourceFiles.Create;
|
|
try
|
|
for i:=0 to params.InputFiles.Count-1 do
|
|
sourcefiles.NewSourceFile(params.InputFiles[i]);
|
|
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;
|
|
|
|
procedure ProcessFiles;
|
|
begin
|
|
Messages.DoVerbose('processing input files...');
|
|
outResources:=TResources.Create;
|
|
sourcefiles.Process(outResources);
|
|
Messages.DoVerbose('input files processed.');
|
|
end;
|
|
|
|
function WriteOutputFile : boolean;
|
|
var aStream : TFileStream;
|
|
aWriter : TExternalResourceWriter;
|
|
msg : string;
|
|
begin
|
|
if outResources.Count=0 then
|
|
begin
|
|
Result:=false;
|
|
Messages.DoVerbose('Nothing to do');
|
|
exit;
|
|
end;
|
|
Result:=true;
|
|
Messages.DoVerbose(Format('Trying to create file %s...',[params.OutputFile]));
|
|
try
|
|
aStream:=TFileStream.Create(params.OutputFile,fmCreate or fmShareDenyWrite);
|
|
except
|
|
Messages.DoError(Format(SCantCreateFile,[params.OutputFile]));
|
|
halt(halt_write_err);
|
|
end;
|
|
try
|
|
aWriter:=TExternalResourceWriter.Create;
|
|
aWriter.Endianess:=params.Endianess;
|
|
try
|
|
try
|
|
outResources.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('%d resources written.',[outResources.Count]));
|
|
Messages.DoVerbose(Format('File %s written',[params.OutputFile]));
|
|
finally
|
|
aWriter.Free;
|
|
end;
|
|
finally
|
|
aStream.Free;
|
|
end;
|
|
FreeAndNil(outResources);
|
|
end;
|
|
|
|
procedure UpdateFiles;
|
|
var msg : string;
|
|
begin
|
|
try
|
|
sourcefiles.Update;
|
|
except
|
|
on e : ECantCreateFileException do msg:=Format(SCantCreateFile,[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_write_err);
|
|
end;
|
|
end;
|
|
|
|
procedure Cleanup;
|
|
begin
|
|
Messages.DoVerbose('Cleaning up');
|
|
if OutResources<>nil then OutResources.Free;
|
|
if SourceFiles<>nil then SourceFiles.Free;
|
|
if Params<>nil then Params.Free;
|
|
end;
|
|
|
|
var before, elapsed : longint;
|
|
|
|
begin
|
|
try
|
|
before:=GetCurrentTimeMsec;
|
|
ParseParams;
|
|
LoadSourceFiles;
|
|
ProcessFiles;
|
|
if WriteOutputFile then
|
|
UpdateFiles;
|
|
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.
|