dl Ada-bindings

This commit is contained in:
Per Sandberg 2012-03-08 07:45:29 +01:00
parent 22004bcf99
commit 61c7f54154
6 changed files with 72 additions and 37 deletions

View File

@ -1,23 +1,27 @@
PREFIX=$(dir $(shell which gnatls))..
compile:
gprbuild -p -P dl.gpr
gprbuild -p -P dl.gpr
all:compile install
install:
mkdir -p ${PREFIX}/include/dl
cp dl.ad? ${PREFIX}/include/dl
cp -f dl.ad? ${PREFIX}/include/dl
chmod -w ${PREFIX}/include/dl/*.ad?
mkdir -p ${PREFIX}/lib/gnat
cp libdl.gpr ${PREFIX}/lib/gnat
cp dl.gpr.inst ${PREFIX}/lib/gnat/dl.gpr
cp -f libdl.gpr ${PREFIX}/lib/gnat
cp -f dl.gpr.inst ${PREFIX}/lib/gnat/dl.gpr
chmod -w ${PREFIX}/lib/gnat/*dl.gpr
mkdir -p ${PREFIX}/lib/dl
cp lib/* ${PREFIX}/lib/dl/
cp -f lib/* ${PREFIX}/lib/dl/
clean:
rm -rf lib .obj gen
generate:
rm -rf gen
mkdir -p gen

View File

@ -1,18 +1,28 @@
with "libdl.gpr";
with "gnatcoll";
project DL is
Version := "1.0";
for Languages use ("C","Ada");
type DL_Kind_Type is ("static", "relocatable");
DL_Kind : DL_Kind_Type := external ("LIBRARY_TYPE", "static");
package Compiler is
for Driver ("C") use "";
for Default_Switches ("ada") use ("-g", "-O2", "-gnat05");
end Compiler;
for Object_Dir use ".obj";
for Object_Dir use ".obj/" & DL_Kind;
for Library_Name use "dlAda";
for Library_Dir use "lib";
for Library_Interface use ("dl");
for Library_Kind use "dynamic";
for Library_Kind use DL_Kind;
for Library_Dir use "lib/" & Project'Library_Kind;
for Library_Version use
"lib" & Project'Library_Name & ".so." & Version;
package Linker is
for Linker_Options use ("-ldl");
end Linker;
end DL;

View File

@ -1,17 +1,17 @@
with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings;
with System;
package body dl is
package body gnatcoll.dl is
package dlfcn_h is
function dlopen (arg1 : Interfaces.C.Strings.chars_ptr; arg2 : int) return System.Address; -- dlfcn.h:57:14
function dlopen (arg1 : System.Address; arg2 : Flag) return System.Address; -- dlfcn.h:57:14
pragma Import (C, dlopen, "dlopen");
function dlclose (arg1 : System.Address) return int; -- dlfcn.h:61:12
pragma Import (C, dlclose, "dlclose");
function dlsym (arg1 : System.Address; arg2 : Interfaces.C.Strings.chars_ptr) return System.Address; -- dlfcn.h:65:14
function dlsym (arg1 : System.Address; arg2 : System.Address) return System.Address; -- dlfcn.h:65:14
pragma Import (C, dlsym, "dlsym");
function dlerror return Interfaces.C.Strings.chars_ptr; -- dlfcn.h:83:14
@ -21,16 +21,26 @@ package body dl is
use dlfcn_h;
use type system.Address;
procedure open (this : in out dynamic_Library;
File_Name : string;
Flags : integer) is
procedure Open (This : in out Dynamic_Library;
File_Name : String;
Flags : Flag := RTLD_LAZY) is
L_name : aliased constant string := File_Name & ASCII.NUL;
begin
this.handle := dlopen (Strings.Null_Ptr,0);
this.handle := dlopen (L_name'Address, Flags);
if this.handle = System.Null_Address then
raise Dynamic_Library_Error with error;
end if;
end;
function Open (File_Name : String;
Flags : Flag := RTLD_LAZY) return Dynamic_Library is
begin
return ret : Dynamic_Library do
open(ret,File_Name,Flags);
end return;
end;
procedure close (this : dynamic_Library) is
ret : int;
begin
@ -41,9 +51,10 @@ package body dl is
end;
function sym (this : dynamic_Library; Symbol_name : String) return System.Address is
L_name : aliased constant string := Symbol_name & ASCII.NUL;
begin
return ret : System.Address do
ret := dlsym (this.handle, strings.Null_Ptr);
ret := dlsym (this.handle, L_name'Address);
if ret = System.Null_Address then
raise Dynamic_Library_Error with error;
end if;
@ -55,4 +66,4 @@ package body dl is
return Interfaces.C.Strings.Value (dlerror);
end;
end dl;
end gnatcoll.dl;

View File

@ -1,12 +1,14 @@
with System;
package Dl is
package gnatcoll.Dl is
pragma Preelaborate;
type Dynamic_Library is tagged private;
type Flag is private;
function "+" (L , R Flag) return Flag;
function "or" (L , R Flag) return Flag;
type Flag is mod 2**32;
pragma warnings(off, """Or"" is being renamed as a different operator");
function "+" (L , R :Flag) return Flag renames "or";
pragma warnings(on, """Or"" is being renamed as a different operator");
RTLD_LAZY : constant Flag;
-- Perform lazy binding.
@ -55,6 +57,9 @@ package Dl is
File_Name : String;
Flags : Flag := RTLD_LAZY);
function Open (File_Name : String;
Flags : Flag := RTLD_LAZY) return Dynamic_Library;
procedure Close (This : Dynamic_Library);
function Sym (This : Dynamic_Library;
@ -68,7 +73,6 @@ private
end record;
function Error return String; -- dlfcn.h:83:14
type Flag is mod 2**32;
RTLD_LAZY : constant Flag := 2#0000_0000_0000_0001#;
RTLD_NOW : constant Flag := 2#0000_0000_0000_0010#;
RTLD_GLOBAL : constant Flag := 2#0000_0001_0000_0000#;
@ -76,4 +80,5 @@ private
RTLD_NODELETE : constant Flag := 2#0001_0000_0000_0000#;
RTLD_NOLOAD : constant Flag := 2#0000_0000_0000_0100#;
RTLD_DEEPBIND : constant Flag := 2#0000_0000_0000_1000#;
end Dl;
end gnatcoll.Dl;

View File

@ -1,10 +0,0 @@
project libDL is
for languages use ("C");
for source_files use ("dlfcn.h");
for source_dirs use ("/usr/include");
for Library_name use "dl";
for externally_Built use "True";
for Library_dir use "/usr/lib";
for Library_Kind use "dynamic";
end libDL;

View File

@ -1,5 +1,9 @@
with ada.Text_IO;
with GNAT.Source_Info;
with AUnit.Assertions;
with System.Address_Image;
with Interfaces.C.Strings;
with ada.Unchecked_Conversion;
package body Dl.Test_Basics is
use AUnit;
use AUnit.Assertions;
@ -26,9 +30,20 @@ package body Dl.Test_Basics is
T : Test_Case renames Test_Case (Test);
pragma Unreferenced (T);
P : Dynamic_Library;
type zlibVersion_Access is access function return Interfaces.C.Strings.chars_ptr;
function conv is new ada.Unchecked_Conversion (system.Address, zlibVersion_Access);
zlibVersion : zlibVersion_Access;
begin
p.open(
Assert (False, "TODO Implement Test");
p.open (File_Name => "/lib/libz.so.1");
zlibVersion := conv (p.Sym ("zlibVersion"));
declare
version : constant string := Interfaces.C.Strings.Value (zlibVersion.all);
begin
test.Assert (Version (version'first + 1 ) = '.', "Dont seem to be a version numnber");
test.Assert (Version (version'first + 3 ) = '.', "Dont seem to be a version numnber");
end;
p.close;
end SampleTest;
--------------------