with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Exceptions; use Ada.Exceptions;
with Ada.Command_Line; use Ada.Command_Line;
with GNAT.OS_Lib; use GNAT.OS_Lib;
with GNAT.Command_Line; use GNAT.Command_Line;
with Gnatvsn;
with MDLL.Fil; use MDLL.Fil;
with MDLL.Utl; use MDLL.Utl;
procedure Gnatdll is
use type GNAT.OS_Lib.Argument_List;
procedure Syntax;
procedure Check (Filename : String);
procedure Parse_Command_Line;
procedure Check_Context;
Syntax_Error : exception;
Context_Error : exception;
Help : Boolean := False;
Version : constant String := Gnatvsn.Gnat_Version_String;
Default_DLL_Address : constant String := "0x11000000";
Lib_Filename : Unbounded_String := Null_Unbounded_String;
Def_Filename : Unbounded_String := Null_Unbounded_String;
List_Filename : Unbounded_String := Null_Unbounded_String;
DLL_Address : Unbounded_String := To_Unbounded_String (Default_DLL_Address);
Gen_Map_File : Boolean := False;
Objects_Files : Argument_List_Access := MDLL.Null_Argument_List_Access;
Ali_Files : Argument_List_Access := MDLL.Null_Argument_List_Access;
Options : Argument_List_Access := MDLL.Null_Argument_List_Access;
Largs_Options : Argument_List_Access := MDLL.Null_Argument_List_Access;
Bargs_Options : Argument_List_Access := MDLL.Null_Argument_List_Access;
type Build_Mode_State is (Import_Lib, Dynamic_Lib, Dynamic_Lib_Only, Nil);
Build_Mode : Build_Mode_State := Nil;
Must_Build_Relocatable : Boolean := True;
procedure Syntax is
procedure P (Str : String) renames Put_Line;
begin
P ("Usage : gnatdll [options] [list-of-files]");
New_Line;
P ("[list-of-files] a list of Ada libraries (.ali) and/or " &
"foreign object files");
New_Line;
P ("[options] can be");
P (" -h Help - display this message");
P (" -v Verbose");
P (" -q Quiet");
P (" -k Remove @nn suffix from exported names");
P (" -g Generate debugging information");
P (" -Idir Specify source and object files search path");
P (" -l file File contains a list-of-files to be added to "
& "the library");
P (" -e file Definition file containing exports");
P (" -d file Put objects in the relocatable dynamic "
& "library <file>");
P (" -b addr Set base address for the relocatable DLL");
P (" default address is " & Default_DLL_Address);
P (" -a[addr] Build non-relocatable DLL at address <addr>");
P (" if <addr> is not specified use "
& Default_DLL_Address);
P (" -m Generate map file");
P (" -n No-import - do not create the import library");
P (" -bargs opts opts are passed to the binder");
P (" -largs opts opts are passed to the linker");
end Syntax;
procedure Check (Filename : in String) is
begin
if not Is_Regular_File (Filename) then
Raise_Exception
(Context_Error'Identity, "Error: " & Filename & " not found.");
end if;
end Check;
procedure Parse_Command_Line is
use GNAT.Command_Line;
procedure Add_File (Filename : in String);
procedure Add_Files_From_List (List_Filename : in String);
Max_Files : constant := 5_000;
Max_Options : constant := 100;
Ofiles : Argument_List (1 .. Max_Files);
O : Positive := Ofiles'First;
Afiles : Argument_List (1 .. Max_Files);
A : Positive := Afiles'First;
Gopts : Argument_List (1 .. Max_Options);
G : Positive := Gopts'First;
Lopts : Argument_List (1 .. Max_Options);
L : Positive := Lopts'First;
Bopts : Argument_List (1 .. Max_Options);
B : Positive := Bopts'First;
Build_Import : Boolean := True;
procedure Add_File (Filename : in String) is
begin
if Is_Ali (Filename) then
Check (Filename);
Afiles (A) := new String'(Filename);
A := A + 1;
elsif Is_Obj (Filename) then
Check (Filename);
Ofiles (O) := new String'(Filename);
O := O + 1;
else
Raise_Exception
(Syntax_Error'Identity,
"don't know what to do with " & Filename & " !");
end if;
end Add_File;
procedure Add_Files_From_List (List_Filename : in String) is
File : File_Type;
Buffer : String (1 .. 500);
Last : Natural;
begin
Open (File, In_File, List_Filename);
while not End_Of_File (File) loop
Get_Line (File, Buffer, Last);
Add_File (Buffer (1 .. Last));
end loop;
Close (File);
end Add_Files_From_List;
begin
Initialize_Option_Scan ('-', False, "bargs largs");
loop
case Getopt ("g h v q k a? b: d: e: l: n m I:") is
when ASCII.Nul =>
exit;
when 'h' =>
Help := True;
when 'g' =>
Gopts (G) := new String'("-g");
G := G + 1;
when 'v' =>
MDLL.Verbose := True;
if MDLL.Quiet then
Raise_Exception
(Syntax_Error'Identity,
"impossible to use -q and -v together.");
end if;
when 'q' =>
MDLL.Quiet := True;
if MDLL.Verbose then
Raise_Exception
(Syntax_Error'Identity,
"impossible to use -v and -q together.");
end if;
when 'k' =>
MDLL.Kill_Suffix := True;
when 'a' =>
if Parameter = "" then
DLL_Address := To_Unbounded_String (Default_DLL_Address);
else
DLL_Address := To_Unbounded_String (Parameter);
end if;
Must_Build_Relocatable := False;
when 'b' =>
DLL_Address := To_Unbounded_String (Parameter);
Must_Build_Relocatable := True;
when 'e' =>
Def_Filename := To_Unbounded_String (Parameter);
when 'd' =>
Lib_Filename := To_Unbounded_String (Parameter);
if Def_Filename = Null_Unbounded_String then
Def_Filename := To_Unbounded_String
(Ext_To (Parameter, "def"));
end if;
Build_Mode := Dynamic_Lib;
when 'm' =>
Gen_Map_File := True;
when 'n' =>
Build_Import := False;
when 'l' =>
List_Filename := To_Unbounded_String (Parameter);
when 'I' =>
Gopts (G) := new String'("-I" & Parameter);
G := G + 1;
when others =>
raise Invalid_Switch;
end case;
end loop;
loop
declare
File : constant String := Get_Argument (Do_Expansion => True);
begin
exit when File'Length = 0;
Add_File (File);
end;
end loop;
Goto_Section ("largs");
loop
case Getopt ("*") is
when ASCII.Nul =>
exit;
when others =>
Lopts (L) := new String'(Full_Switch);
L := L + 1;
end case;
end loop;
Goto_Section ("bargs");
loop
case Getopt ("*") is
when ASCII.Nul =>
exit;
when others =>
Bopts (B) := new String'(Full_Switch);
B := B + 1;
end case;
end loop;
if List_Filename /= Null_Unbounded_String then
Add_Files_From_List (To_String (List_Filename));
end if;
if Build_Mode = Nil and then not Help and then not MDLL.Verbose then
Raise_Exception (Syntax_Error'Identity, "nothing to do.");
end if;
if not Build_Import
and then A = Afiles'First
and then O = Ofiles'First
then
Raise_Exception
(Syntax_Error'Identity,
"-n specified but there are no objects to build the library.");
end if;
if Build_Mode = Dynamic_Lib
and then A = Afiles'First
and then O = Ofiles'First
then
Build_Mode := Import_Lib;
end if;
if Gen_Map_File and then Build_Mode = Import_Lib then
Raise_Exception
(Syntax_Error'Identity,
"Can't generate a map file for an import library.");
end if;
if Build_Mode = Dynamic_Lib and then not Build_Import then
Build_Mode := Dynamic_Lib_Only;
end if;
if O /= Ofiles'First then
Objects_Files := new Argument_List'(Ofiles (1 .. O - 1));
end if;
if A /= Afiles'First then
Ali_Files := new Argument_List'(Afiles (1 .. A - 1));
end if;
if G /= Gopts'First then
Options := new Argument_List'(Gopts (1 .. G - 1));
end if;
if L /= Lopts'First then
Largs_Options := new Argument_List'(Lopts (1 .. L - 1));
end if;
if B /= Bopts'First then
Bargs_Options := new Argument_List'(Bopts (1 .. B - 1));
end if;
exception
when Invalid_Switch =>
Raise_Exception
(Syntax_Error'Identity,
Message => "Invalid Switch " & Full_Switch);
when Invalid_Parameter =>
Raise_Exception
(Syntax_Error'Identity,
Message => "No parameter for " & Full_Switch);
end Parse_Command_Line;
procedure Check_Context is
begin
Check (To_String (Def_Filename));
for F in Objects_Files'Range loop
Check (Objects_Files (F).all);
end loop;
end Check_Context;
begin
if Ada.Command_Line.Argument_Count = 0 then
Help := True;
else
Parse_Command_Line;
end if;
if MDLL.Verbose or else Help then
New_Line;
Put_Line ("GNATDLL " & Version & " - Dynamic Libraries Builder");
New_Line;
end if;
MDLL.Utl.Locate;
if Help
or else (MDLL.Verbose and then Ada.Command_Line.Argument_Count = 1)
then
Syntax;
else
Check_Context;
case Build_Mode is
when Import_Lib =>
MDLL.Build_Import_Library
(To_String (Lib_Filename),
To_String (Def_Filename));
when Dynamic_Lib =>
MDLL.Build_Dynamic_Library
(Objects_Files.all,
Ali_Files.all,
Options.all,
Bargs_Options.all,
Largs_Options.all,
To_String (Lib_Filename),
To_String (Def_Filename),
To_String (DLL_Address),
Build_Import => True,
Relocatable => Must_Build_Relocatable,
Map_File => Gen_Map_File);
when Dynamic_Lib_Only =>
MDLL.Build_Dynamic_Library
(Objects_Files.all,
Ali_Files.all,
Options.all,
Bargs_Options.all,
Largs_Options.all,
To_String (Lib_Filename),
To_String (Def_Filename),
To_String (DLL_Address),
Build_Import => False,
Relocatable => Must_Build_Relocatable,
Map_File => Gen_Map_File);
when Nil =>
null;
end case;
end if;
Set_Exit_Status (Success);
exception
when SE : Syntax_Error =>
Put_Line ("Syntax error : " & Exception_Message (SE));
New_Line;
Syntax;
Set_Exit_Status (Failure);
when E : MDLL.Tools_Error | Context_Error =>
Put_Line (Exception_Message (E));
Set_Exit_Status (Failure);
when others =>
Put_Line ("gnatdll: INTERNAL ERROR. Please report");
Set_Exit_Status (Failure);
end Gnatdll;