Skip to content

Commit

Permalink
Scanner a bit working on Termux.
Browse files Browse the repository at this point in the history
  • Loading branch information
LongDirtyAnimAlf committed Nov 6, 2024
1 parent 5669676 commit 04989b8
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 16 deletions.
20 changes: 20 additions & 0 deletions fpcuptools/libraryscanner/scannercmd.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@
</Linking>
</CompilerOptions>
</Item>
<Item Name="AndroidArm64">
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="bin\$NameOnly($(ProjFile))-$(TargetCPU)-$(TargetOS)"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<CodeGeneration>
<TargetCPU Value="aarch64"/>
<TargetOS Value="android"/>
<Optimizations>
<OptimizationLevel Value="0"/>
</Optimizations>
</CodeGeneration>
</CompilerOptions>
</Item>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
Expand Down
2 changes: 1 addition & 1 deletion fpcuptools/libraryscanner/scannercmd.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ procedure TLibraryScanner.DoRun;
with TScannerCore.Create do
begin
try
GetAndSaveLibs(Location);
GetAndSaveLibs;
finally
Free;
end;
Expand Down
125 changes: 111 additions & 14 deletions fpcuptools/libraryscanner/scannercore.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

{$mode ObjFPC}{$H+}

{$ifdef Android}
{$define Termux}
{$endif}

interface

uses
termio,streamio,
Classes, SysUtils;

type
Expand All @@ -21,7 +26,7 @@ TScannerCore = class
function StoreLibrary(const aLib:string):boolean;
procedure CheckAndAddLibrary(const aLib:string);
public
procedure GetAndSaveLibs(const Location:string);
procedure GetAndSaveLibs(const Loc:string='');
constructor Create;
destructor Destroy; override;
published
Expand All @@ -37,7 +42,8 @@ TScannerCore = class
implementation

uses
StrUtils,process,FileUtil,LazFileUtils,LookupStringList;
Linux,BaseUnix,
Unix,StrUtils,process,FileUtil,LazFileUtils,LookupStringList;

const
{$ifdef CPUX86}
Expand Down Expand Up @@ -70,9 +76,15 @@ implementation
{$endif CPUARMHF}
{$endif CPUARM}
{$ifdef CPUAARCH64}
{$ifdef Android}
'/usr/aarch64-linux-android/lib',
'/usr/opt/ndk-multilib/aarch64-linux-android/lib',
'/system/lib64',
{$else}
'/usr/lib/aarch64-linux-gnu',
'/usr/local/lib/aarch64-linux-gnu',
'/lib/aarch64-linux-gnu',
{$endif}
{$endif CPUAARCH64}
{$ifdef CPULOONGARCH}
'/usr/lib/loongarch-linux-gnu',
Expand Down Expand Up @@ -166,9 +178,15 @@ implementation
{$endif CPUARMHF}
{$endif CPUARM}
{$ifdef CPUAARCH64}
{$ifdef Android}
DYNLINKV1='ld-android.so';
DYNLINKV2='ld-android.so.1';
DYNLINKV3='ld-android.so.2';
{$else}
DYNLINKV1='ld-linux-aarch64.so.1';
DYNLINKV2='ld-linux-aarch64.so.2';
DYNLINKV3='ld-linux-aarch64.so.3';
{$endif}
{$endif CPUAARCH64}
{$ifdef CPULOONGARCH}
DYNLINKV1='ld-linux-loongarch-lp64d.so.1';
Expand All @@ -188,6 +206,10 @@ implementation
{$endif}
{$endif}

{$ifdef Termux}
TERMUXPATH = '/data/data/com.termux/files';
{$endif}

const FPCLIBS : array [0..43] of string = (
'crtbegin.o',
'crtbeginS.o',
Expand Down Expand Up @@ -305,8 +327,9 @@ implementation
'libmysqlclient.so.12'
);

const LAZLIBS : array [0..30] of string = (
const LAZLIBS : array [0..31] of string = (
'libgdk-1.2.so.0',
'libgdk-3.so.0',
'libglib-1.2.so.0',
'libgmodule-1.2.so.0',
'libgtk-1.2.so.0',
Expand Down Expand Up @@ -411,14 +434,17 @@ function GetSourceCPUOS:string;
function GetStartupObjects:string;
const
LINKFILE='crtbegin.o';
SEARCHDIRS : array [0..6] of string = (
SEARCHDIRS : array [0..9] of string = (
'/usr/local/lib/',
'/usr/lib/',
'/usr/local/lib/gcc/',
'/usr/lib/gcc/',
'/usr/lib/gcc-lib/',
'/lib/gcc/',
'/lib/'
'/lib/',
TERMUXPATH+'/usr/lib', // for termux
TERMUXPATH+'/usr/lib/clang', // for termux
'/system/lib64/' // for termux
);

var
Expand Down Expand Up @@ -663,6 +689,10 @@ function GetStartupObjects:string;
result:='/usr/lib/gcc';
{$endif}

{$ifdef Termux}
result:=TERMUXPATH+'/usr/lib/clang';
{$endif}

if DirectoryExists(result) then
begin
LinkFiles := TStringList.Create;
Expand Down Expand Up @@ -850,13 +880,19 @@ procedure TScannerCore.CheckAndAddLibrary(const aLib:string);
const
MAGICNEEDED = 'NEEDED';
MAGICSHARED = 'Shared library:';
{$ifdef Termux}
DATAFILE = 'elfdynamic.dat';
{$endif}
var
SearchResultList:TStringList;
SearchResult:string;
FileName:string;
i: integer;
sd,sr,s:string;
sIn:TStringStream;
T:TextFile;
begin
sIn:=TStringStream.Create;
SearchResultList:=TStringList.Create;
try
{$ifdef Windows}
Expand All @@ -882,6 +918,10 @@ procedure TScannerCore.CheckAndAddLibrary(const aLib:string);
{$ifdef Windows}
FileName:=StringReplace(FileName,'dummy',FLibraryLocation,[]);
{$endif}
{$ifdef Termux}
if NOT DirectoryExists(sd) then
FileName:=TERMUXPATH+FileName;
{$endif}
// Do we have a wildcard ?
if (Pos('*',aLib)>0) then
begin
Expand All @@ -897,6 +937,18 @@ procedure TScannerCore.CheckAndAddLibrary(const aLib:string);
FileName:='';
break;
end;
{$ifdef Termux}
if NOT FileExists(FileName) then
begin
i:=Pos('.so',FileName);
if i>0 then
begin
Delete(FileName,i,MaxInt);
FileName:=FileName+'.so';
end;
end;
{$endif}

if FileExists(FileName) then
begin
StoreLibrary('['+ExtractFileName(FileName)+']');
Expand All @@ -914,14 +966,37 @@ procedure TScannerCore.CheckAndAddLibrary(const aLib:string);
end;
while FileIsSymlink(FileName) do FileName:=GetPhysicalFilename(FileName,pfeException);
SearchResult:='';

(*
AssignFile(T,'yolo');
//Streamio.AssignStream(T,sIn);
//TextRec(T).Mode:=fmOutput;
Append(T);
POpen(T,'readelf -d -W '+FileName,'R');
//Flush(T);
//writeln('Result: ',sIn.DataString);
//SearchResult:=sIn.DataString;
CloseFile(T);
*)

{$ifdef Termux}
DeleteFile(DATAFILE);
sIn.Clear;
fpSystem('readelf -d -W '+FileName +' > ' + DATAFILE);
sIn.LoadFromFile(DATAFILE);
SearchResult:=sIn.DataString;
{$else}
RunCommand(FReadelfBinary,['-d','-W',FileName],SearchResult,[poUsePipes, poStderrToOutPut]{$IF DEFINED(FPC_FULLVERSION) AND (FPC_FULLVERSION >= 30200)},swoHide{$ENDIF});
{$endif}

SearchResultList.Text:=SearchResult;
if (SearchResultList.Count=0) then continue;
for sr in SearchResultList do
begin
s:=sr;
if (Pos(MAGICNEEDED,s)>0) then
begin
//writeln('Looking for dependency: ',s);
i:=Pos(MAGICSHARED,s);
if (i<>-1) then
begin
Expand Down Expand Up @@ -955,10 +1030,13 @@ procedure TScannerCore.CheckAndAddLibrary(const aLib:string);
{$endif}
finally
SearchResultList.Free;
sIn.Free;
end;
end;

procedure TScannerCore.GetAndSaveLibs(const Location:string);
procedure TScannerCore.GetAndSaveLibs(const Loc:string);
var
LibsLocation:string;
begin
FLibraryList.Clear;
FLibraryNotFoundList.Clear;
Expand Down Expand Up @@ -1039,6 +1117,10 @@ procedure TScannerCore.GetAndSaveLibs(const Location:string);
{$ifdef Windows}
SearchLibPath:=StringReplace(SearchLibPath,'dummy',FLibraryLocation,[]);
{$endif}
{$ifdef Termux}
if NOT DirectoryExists(SearchDir) then
SearchLibPath:=TERMUXPATH+SearchLibPath;
{$endif}
if FileExists(SearchLibPath) then
begin
FLibraryLocationList.Append(SearchLibPath);
Expand Down Expand Up @@ -1068,13 +1150,23 @@ procedure TScannerCore.GetAndSaveLibs(const Location:string);


// Process the results from the scan by getting the real files
LibsLocation:=Loc;

if LibsLocation='' then
begin
RunCommand('pwd',[],LibsLocation,[poUsePipes, poStderrToOutPut]{$IF DEFINED(FPC_FULLVERSION) AND (FPC_FULLVERSION >= 30200)},swoHide{$ENDIF});
end;

ForceDirectories(Location+'libs');
LibsLocation:='/data/data/com.termux/files';
LibsLocation:=IncludeTrailingPathDelimiter(LibsLocation);
writeln('Saving files into:',LibsLocation);

ForceDirectories(LibsLocation+'libs');

aList:=TStringList.Create;
try
aList.Add('These libraries were sourced from: '+GetDistro+' version '+GetDistro('VERSION'));
aList.SaveToFile(Location+'libs'+DirectorySeparator+'actual_library_version_fpcup.txt');
aList.SaveToFile(LibsLocation+'libs'+DirectorySeparator+'actual_library_version_fpcup.txt');
finally
aList.Free;
end;
Expand All @@ -1084,7 +1176,7 @@ procedure TScannerCore.GetAndSaveLibs(const Location:string);
begin
FileName:=FLibraryLocationList.Strings[Index];
TargetFile:=ExtractFileName(FileName);
CopyFile(FileName,Location+'libs'+DirectorySeparator+TargetFile,[]);
CopyFile(FileName,LibsLocation+'libs'+DirectorySeparator+TargetFile,[]);
end;

// if there are any linklibs not found, create them now
Expand All @@ -1097,23 +1189,23 @@ procedure TScannerCore.GetAndSaveLibs(const Location:string);
begin
if (Pos(LinkFile,TargetFile)=1) then
begin
CopyFile(FileName,Location+'libs'+DirectorySeparator+LinkFile,[]);
CopyFile(FileName,LibsLocation+'libs'+DirectorySeparator+LinkFile,[]);
break;
end;
end;
for LinkFile in FPCEXTRALIBS do
begin
if (Pos(LinkFile,TargetFile)=1) then
begin
CopyFile(FileName,Location+'libs'+DirectorySeparator+LinkFile,[]);
CopyFile(FileName,LibsLocation+'libs'+DirectorySeparator+LinkFile,[]);
break;
end;
end;
for LinkFile in LAZLINKLIBS do
begin
if (Pos(LinkFile,TargetFile)=1) then
begin
CopyFile(FileName,Location+'libs'+DirectorySeparator+LinkFile,[]);
CopyFile(FileName,LibsLocation+'libs'+DirectorySeparator+LinkFile,[]);
break;
end;
end;
Expand All @@ -1124,7 +1216,7 @@ procedure TScannerCore.GetAndSaveLibs(const Location:string);
begin
if (Pos(LinkFile,TargetFile)=1) then
begin
CopyFile(FileName,Location+'libs'+DirectorySeparator+LinkFile,[]);
CopyFile(FileName,LibsLocation+'libs'+DirectorySeparator+LinkFile,[]);
break;
end;
end;
Expand All @@ -1135,13 +1227,18 @@ procedure TScannerCore.GetAndSaveLibs(const Location:string);
end;

constructor TScannerCore.Create;
var
Output:string;
begin
inherited Create;
FLibraryList:=TStringList.Create;
FLibraryNotFoundList:=TStringList.Create;
FLibraryLocationList:=TStringList.Create;
chkQT:=True;
FReadelfBinary:='readelf';
FReadelfBinary:='/usr/bin/readelf';
{$ifdef Termux}
FReadelfBinary:=TERMUXPATH+'/usr/libexec/binutils/readelf';
{$endif}
{$ifdef Windows}
FLibraryLocation:='c:\fpcupdeluxe';
{$endif}
Expand Down
2 changes: 1 addition & 1 deletion public/gitrevision.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[master]darwin_amd64_crossbins_all-2907(0be23ea)
[master]darwin_amd64_crossbins_all-2908(5669676)

0 comments on commit 04989b8

Please sign in to comment.