| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- G N A T D L L -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1997-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- |
| 17 |
-- for more details. You should have received a copy of the GNU General -- |
| 18 |
-- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
| 19 |
-- http://www.gnu.org/licenses for a complete copy of the license. -- |
| 20 |
-- -- |
| 21 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 22 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 23 |
-- -- |
| 24 |
------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
-- GNATDLL is a Windows specific tool for building a DLL. |
| 27 |
-- Both relocatable and non-relocatable DLL's are supported |
| 28 |
|
| 29 |
with Gnatvsn; |
| 30 |
with MDLL.Fil; use MDLL.Fil; |
| 31 |
with MDLL.Utl; |
| 32 |
with Switch; use Switch; |
| 33 |
|
| 34 |
with Ada.Text_IO; use Ada.Text_IO; |
| 35 |
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; |
| 36 |
with Ada.Exceptions; use Ada.Exceptions; |
| 37 |
with Ada.Command_Line; use Ada.Command_Line; |
| 38 |
|
| 39 |
with GNAT.OS_Lib; use GNAT.OS_Lib; |
| 40 |
with GNAT.Command_Line; use GNAT.Command_Line; |
| 41 |
|
| 42 |
procedure Gnatdll is |
| 43 |
|
| 44 |
procedure Syntax; |
| 45 |
-- Print out usage |
| 46 |
|
| 47 |
procedure Check (Filename : String); |
| 48 |
-- Check that the file whose name is Filename exists |
| 49 |
|
| 50 |
procedure Parse_Command_Line; |
| 51 |
-- Parse the command line arguments passed to gnatdll |
| 52 |
|
| 53 |
procedure Check_Context; |
| 54 |
-- Check the context before running any commands to build the library |
| 55 |
|
| 56 |
Syntax_Error : exception; |
| 57 |
-- Raised when a syntax error is detected, in this case a usage info will |
| 58 |
-- be displayed. |
| 59 |
|
| 60 |
Context_Error : exception; |
| 61 |
-- Raised when some files (specified on the command line) are missing to |
| 62 |
-- build the DLL. |
| 63 |
|
| 64 |
Help : Boolean := False; |
| 65 |
-- Help will be set to True the usage information is to be displayed |
| 66 |
|
| 67 |
Version : constant String := Gnatvsn.Gnat_Version_String; |
| 68 |
-- Why should it be necessary to make a copy of this |
| 69 |
|
| 70 |
Default_DLL_Address : constant String := "0x11000000"; |
| 71 |
-- Default address for non relocatable DLL (Win32) |
| 72 |
|
| 73 |
Lib_Filename : Unbounded_String := Null_Unbounded_String; |
| 74 |
-- The DLL filename that will be created (.dll) |
| 75 |
|
| 76 |
Def_Filename : Unbounded_String := Null_Unbounded_String; |
| 77 |
-- The definition filename (.def) |
| 78 |
|
| 79 |
List_Filename : Unbounded_String := Null_Unbounded_String; |
| 80 |
-- The name of the file containing the objects file to put into the DLL |
| 81 |
|
| 82 |
DLL_Address : Unbounded_String := To_Unbounded_String (Default_DLL_Address); |
| 83 |
-- The DLL's base address |
| 84 |
|
| 85 |
Gen_Map_File : Boolean := False; |
| 86 |
-- Set to True if a map file is to be generated |
| 87 |
|
| 88 |
Objects_Files : Argument_List_Access := MDLL.Null_Argument_List_Access; |
| 89 |
-- List of objects to put inside the library |
| 90 |
|
| 91 |
Ali_Files : Argument_List_Access := MDLL.Null_Argument_List_Access; |
| 92 |
-- For each Ada file specified, we keep a record of the corresponding |
| 93 |
-- ALI file. This list of SLI files is used to build the binder program. |
| 94 |
|
| 95 |
Options : Argument_List_Access := MDLL.Null_Argument_List_Access; |
| 96 |
-- A list of options set in the command line |
| 97 |
|
| 98 |
Largs_Options : Argument_List_Access := MDLL.Null_Argument_List_Access; |
| 99 |
Bargs_Options : Argument_List_Access := MDLL.Null_Argument_List_Access; |
| 100 |
-- GNAT linker and binder args options |
| 101 |
|
| 102 |
type Build_Mode_State is (Import_Lib, Dynamic_Lib, Dynamic_Lib_Only, Nil); |
| 103 |
-- Import_Lib means only the .a file will be created, Dynamic_Lib means |
| 104 |
-- that both the DLL and the import library will be created. |
| 105 |
-- Dynamic_Lib_Only means that only the DLL will be created (no import |
| 106 |
-- library). |
| 107 |
|
| 108 |
Build_Mode : Build_Mode_State := Nil; |
| 109 |
-- Will be set when parsing the command line |
| 110 |
|
| 111 |
Must_Build_Relocatable : Boolean := True; |
| 112 |
-- True means build a relocatable DLL, will be set to False if a |
| 113 |
-- non-relocatable DLL must be built. |
| 114 |
|
| 115 |
------------ |
| 116 |
-- Syntax -- |
| 117 |
------------ |
| 118 |
|
| 119 |
procedure Syntax is |
| 120 |
procedure P (Str : String) renames Put_Line; |
| 121 |
begin |
| 122 |
P ("Usage : gnatdll [options] [list-of-files]"); |
| 123 |
New_Line; |
| 124 |
P ("[list-of-files] a list of Ada libraries (.ali) and/or " & |
| 125 |
"foreign object files"); |
| 126 |
New_Line; |
| 127 |
P ("[options] can be"); |
| 128 |
P (" -h Help - display this message"); |
| 129 |
P (" -v Verbose"); |
| 130 |
P (" -q Quiet"); |
| 131 |
P (" -k Remove @nn suffix from exported names"); |
| 132 |
P (" -g Generate debugging information"); |
| 133 |
P (" -Idir Specify source and object files search path"); |
| 134 |
P (" -l file File contains a list-of-files to be added to " |
| 135 |
& "the library"); |
| 136 |
P (" -e file Definition file containing exports"); |
| 137 |
P (" -d file Put objects in the dynamic " |
| 138 |
& "library <file>"); |
| 139 |
P (" -a[addr] Build non-relocatable DLL at address <addr>"); |
| 140 |
P (" if <addr> is not specified use " |
| 141 |
& Default_DLL_Address); |
| 142 |
P (" -m Generate map file"); |
| 143 |
P (" -n No-import - do not create the import library"); |
| 144 |
P (" -bargs opts opts are passed to the binder"); |
| 145 |
P (" -largs opts opts are passed to the linker"); |
| 146 |
end Syntax; |
| 147 |
|
| 148 |
----------- |
| 149 |
-- Check -- |
| 150 |
----------- |
| 151 |
|
| 152 |
procedure Check (Filename : String) is |
| 153 |
begin |
| 154 |
if not Is_Regular_File (Filename) then |
| 155 |
Raise_Exception |
| 156 |
(Context_Error'Identity, "Error: " & Filename & " not found."); |
| 157 |
end if; |
| 158 |
end Check; |
| 159 |
|
| 160 |
------------------------ |
| 161 |
-- Parse_Command_Line -- |
| 162 |
------------------------ |
| 163 |
|
| 164 |
procedure Parse_Command_Line is |
| 165 |
|
| 166 |
procedure Add_File (Filename : String); |
| 167 |
-- Add one file to the list of file to handle |
| 168 |
|
| 169 |
procedure Add_Files_From_List (List_Filename : String); |
| 170 |
-- Add the files listed in List_Filename (one by line) to the list |
| 171 |
-- of file to handle |
| 172 |
|
| 173 |
Max_Files : constant := 50_000; |
| 174 |
Max_Options : constant := 1_000; |
| 175 |
|
| 176 |
Ofiles : Argument_List (1 .. Max_Files); |
| 177 |
O : Positive := Ofiles'First; |
| 178 |
-- List of object files to put in the library. O is the next entry |
| 179 |
-- to be used. |
| 180 |
|
| 181 |
Afiles : Argument_List (1 .. Max_Files); |
| 182 |
A : Positive := Afiles'First; |
| 183 |
-- List of ALI files. A is the next entry to be used |
| 184 |
|
| 185 |
Gopts : Argument_List (1 .. Max_Options); |
| 186 |
G : Positive := Gopts'First; |
| 187 |
-- List of gcc options. G is the next entry to be used |
| 188 |
|
| 189 |
Lopts : Argument_List (1 .. Max_Options); |
| 190 |
L : Positive := Lopts'First; |
| 191 |
-- A list of -largs options (L is next entry to be used) |
| 192 |
|
| 193 |
Bopts : Argument_List (1 .. Max_Options); |
| 194 |
B : Positive := Bopts'First; |
| 195 |
-- A list of -bargs options (B is next entry to be used) |
| 196 |
|
| 197 |
Build_Import : Boolean := True; |
| 198 |
-- Set to False if option -n if specified (no-import) |
| 199 |
|
| 200 |
-------------- |
| 201 |
-- Add_File -- |
| 202 |
-------------- |
| 203 |
|
| 204 |
procedure Add_File (Filename : String) is |
| 205 |
begin |
| 206 |
if Is_Ali (Filename) then |
| 207 |
Check (Filename); |
| 208 |
|
| 209 |
-- Record it to generate the binder program when |
| 210 |
-- building dynamic library |
| 211 |
|
| 212 |
Afiles (A) := new String'(Filename); |
| 213 |
A := A + 1; |
| 214 |
|
| 215 |
elsif Is_Obj (Filename) then |
| 216 |
Check (Filename); |
| 217 |
|
| 218 |
-- Just record this object file |
| 219 |
|
| 220 |
Ofiles (O) := new String'(Filename); |
| 221 |
O := O + 1; |
| 222 |
|
| 223 |
else |
| 224 |
-- Unknown file type |
| 225 |
|
| 226 |
Raise_Exception |
| 227 |
(Syntax_Error'Identity, |
| 228 |
"don't know what to do with " & Filename & " !"); |
| 229 |
end if; |
| 230 |
end Add_File; |
| 231 |
|
| 232 |
------------------------- |
| 233 |
-- Add_Files_From_List -- |
| 234 |
------------------------- |
| 235 |
|
| 236 |
procedure Add_Files_From_List (List_Filename : String) is |
| 237 |
File : File_Type; |
| 238 |
Buffer : String (1 .. 500); |
| 239 |
Last : Natural; |
| 240 |
|
| 241 |
begin |
| 242 |
Open (File, In_File, List_Filename); |
| 243 |
|
| 244 |
while not End_Of_File (File) loop |
| 245 |
Get_Line (File, Buffer, Last); |
| 246 |
Add_File (Buffer (1 .. Last)); |
| 247 |
end loop; |
| 248 |
|
| 249 |
Close (File); |
| 250 |
|
| 251 |
exception |
| 252 |
when Name_Error => |
| 253 |
Raise_Exception |
| 254 |
(Syntax_Error'Identity, |
| 255 |
"list-of-files file " & List_Filename & " not found."); |
| 256 |
end Add_Files_From_List; |
| 257 |
|
| 258 |
-- Start of processing for Parse_Command_Line |
| 259 |
|
| 260 |
begin |
| 261 |
Initialize_Option_Scan ('-', False, "bargs largs"); |
| 262 |
|
| 263 |
-- scan gnatdll switches |
| 264 |
|
| 265 |
loop |
| 266 |
case Getopt ("g h v q k a? b: d: e: l: n m I:") is |
| 267 |
when ASCII.NUL => |
| 268 |
exit; |
| 269 |
|
| 270 |
when 'h' => |
| 271 |
Help := True; |
| 272 |
|
| 273 |
when 'g' => |
| 274 |
Gopts (G) := new String'("-g"); |
| 275 |
G := G + 1; |
| 276 |
|
| 277 |
when 'v' => |
| 278 |
|
| 279 |
-- Turn verbose mode on |
| 280 |
|
| 281 |
MDLL.Verbose := True; |
| 282 |
if MDLL.Quiet then |
| 283 |
Raise_Exception |
| 284 |
(Syntax_Error'Identity, |
| 285 |
"impossible to use -q and -v together."); |
| 286 |
end if; |
| 287 |
|
| 288 |
when 'q' => |
| 289 |
|
| 290 |
-- Turn quiet mode on |
| 291 |
|
| 292 |
MDLL.Quiet := True; |
| 293 |
if MDLL.Verbose then |
| 294 |
Raise_Exception |
| 295 |
(Syntax_Error'Identity, |
| 296 |
"impossible to use -v and -q together."); |
| 297 |
end if; |
| 298 |
|
| 299 |
when 'k' => |
| 300 |
MDLL.Kill_Suffix := True; |
| 301 |
|
| 302 |
when 'a' => |
| 303 |
if Parameter = "" then |
| 304 |
|
| 305 |
-- Default address for a relocatable dynamic library. |
| 306 |
-- address for a non relocatable dynamic library. |
| 307 |
|
| 308 |
DLL_Address := To_Unbounded_String (Default_DLL_Address); |
| 309 |
|
| 310 |
else |
| 311 |
DLL_Address := To_Unbounded_String (Parameter); |
| 312 |
end if; |
| 313 |
|
| 314 |
Must_Build_Relocatable := False; |
| 315 |
|
| 316 |
when 'e' => |
| 317 |
Def_Filename := To_Unbounded_String (Parameter); |
| 318 |
|
| 319 |
when 'd' => |
| 320 |
|
| 321 |
-- Build a non relocatable DLL |
| 322 |
|
| 323 |
Lib_Filename := To_Unbounded_String (Parameter); |
| 324 |
|
| 325 |
if Def_Filename = Null_Unbounded_String then |
| 326 |
Def_Filename := To_Unbounded_String |
| 327 |
(Ext_To (Parameter, "def")); |
| 328 |
end if; |
| 329 |
|
| 330 |
Build_Mode := Dynamic_Lib; |
| 331 |
|
| 332 |
when 'm' => |
| 333 |
Gen_Map_File := True; |
| 334 |
|
| 335 |
when 'n' => |
| 336 |
Build_Import := False; |
| 337 |
|
| 338 |
when 'l' => |
| 339 |
List_Filename := To_Unbounded_String (Parameter); |
| 340 |
|
| 341 |
when 'I' => |
| 342 |
Gopts (G) := new String'("-I" & Parameter); |
| 343 |
G := G + 1; |
| 344 |
|
| 345 |
when others => |
| 346 |
raise Invalid_Switch; |
| 347 |
end case; |
| 348 |
end loop; |
| 349 |
|
| 350 |
-- Get parameters |
| 351 |
|
| 352 |
loop |
| 353 |
declare |
| 354 |
File : constant String := Get_Argument (Do_Expansion => True); |
| 355 |
begin |
| 356 |
exit when File'Length = 0; |
| 357 |
Add_File (File); |
| 358 |
end; |
| 359 |
end loop; |
| 360 |
|
| 361 |
-- Get largs parameters |
| 362 |
|
| 363 |
Goto_Section ("largs"); |
| 364 |
|
| 365 |
loop |
| 366 |
case Getopt ("*") is |
| 367 |
when ASCII.NUL => |
| 368 |
exit; |
| 369 |
|
| 370 |
when others => |
| 371 |
Lopts (L) := new String'(Full_Switch); |
| 372 |
L := L + 1; |
| 373 |
end case; |
| 374 |
end loop; |
| 375 |
|
| 376 |
-- Get bargs parameters |
| 377 |
|
| 378 |
Goto_Section ("bargs"); |
| 379 |
|
| 380 |
loop |
| 381 |
case Getopt ("*") is |
| 382 |
when ASCII.NUL => |
| 383 |
exit; |
| 384 |
|
| 385 |
when others => |
| 386 |
Bopts (B) := new String'(Full_Switch); |
| 387 |
B := B + 1; |
| 388 |
end case; |
| 389 |
end loop; |
| 390 |
|
| 391 |
-- if list filename has been specified, parse it |
| 392 |
|
| 393 |
if List_Filename /= Null_Unbounded_String then |
| 394 |
Add_Files_From_List (To_String (List_Filename)); |
| 395 |
end if; |
| 396 |
|
| 397 |
-- Check if the set of parameters are compatible |
| 398 |
|
| 399 |
if Build_Mode = Nil and then not Help and then not MDLL.Verbose then |
| 400 |
Raise_Exception (Syntax_Error'Identity, "nothing to do."); |
| 401 |
end if; |
| 402 |
|
| 403 |
-- -n option but no file specified |
| 404 |
|
| 405 |
if not Build_Import |
| 406 |
and then A = Afiles'First |
| 407 |
and then O = Ofiles'First |
| 408 |
then |
| 409 |
Raise_Exception |
| 410 |
(Syntax_Error'Identity, |
| 411 |
"-n specified but there are no objects to build the library."); |
| 412 |
end if; |
| 413 |
|
| 414 |
-- Check if we want to build an import library (option -e and |
| 415 |
-- no file specified) |
| 416 |
|
| 417 |
if Build_Mode = Dynamic_Lib |
| 418 |
and then A = Afiles'First |
| 419 |
and then O = Ofiles'First |
| 420 |
then |
| 421 |
Build_Mode := Import_Lib; |
| 422 |
end if; |
| 423 |
|
| 424 |
-- If map file is to be generated, add linker option here |
| 425 |
|
| 426 |
if Gen_Map_File and then Build_Mode = Import_Lib then |
| 427 |
Raise_Exception |
| 428 |
(Syntax_Error'Identity, |
| 429 |
"Can't generate a map file for an import library."); |
| 430 |
end if; |
| 431 |
|
| 432 |
-- Check if only a dynamic library must be built |
| 433 |
|
| 434 |
if Build_Mode = Dynamic_Lib and then not Build_Import then |
| 435 |
Build_Mode := Dynamic_Lib_Only; |
| 436 |
end if; |
| 437 |
|
| 438 |
if O /= Ofiles'First then |
| 439 |
Objects_Files := new Argument_List'(Ofiles (1 .. O - 1)); |
| 440 |
end if; |
| 441 |
|
| 442 |
if A /= Afiles'First then |
| 443 |
Ali_Files := new Argument_List'(Afiles (1 .. A - 1)); |
| 444 |
end if; |
| 445 |
|
| 446 |
if G /= Gopts'First then |
| 447 |
Options := new Argument_List'(Gopts (1 .. G - 1)); |
| 448 |
end if; |
| 449 |
|
| 450 |
if L /= Lopts'First then |
| 451 |
Largs_Options := new Argument_List'(Lopts (1 .. L - 1)); |
| 452 |
end if; |
| 453 |
|
| 454 |
if B /= Bopts'First then |
| 455 |
Bargs_Options := new Argument_List'(Bopts (1 .. B - 1)); |
| 456 |
end if; |
| 457 |
|
| 458 |
exception |
| 459 |
when Invalid_Switch => |
| 460 |
Raise_Exception |
| 461 |
(Syntax_Error'Identity, |
| 462 |
Message => "Invalid Switch " & Full_Switch); |
| 463 |
|
| 464 |
when Invalid_Parameter => |
| 465 |
Raise_Exception |
| 466 |
(Syntax_Error'Identity, |
| 467 |
Message => "No parameter for " & Full_Switch); |
| 468 |
end Parse_Command_Line; |
| 469 |
|
| 470 |
------------------- |
| 471 |
-- Check_Context -- |
| 472 |
------------------- |
| 473 |
|
| 474 |
procedure Check_Context is |
| 475 |
begin |
| 476 |
Check (To_String (Def_Filename)); |
| 477 |
|
| 478 |
-- Check that each object file specified exists and raise exception |
| 479 |
-- Context_Error if it does not. |
| 480 |
|
| 481 |
for F in Objects_Files'Range loop |
| 482 |
Check (Objects_Files (F).all); |
| 483 |
end loop; |
| 484 |
end Check_Context; |
| 485 |
|
| 486 |
procedure Check_Version_And_Help is new Check_Version_And_Help_G (Syntax); |
| 487 |
|
| 488 |
-- Start of processing for Gnatdll |
| 489 |
|
| 490 |
begin |
| 491 |
Check_Version_And_Help ("GNATDLL", "1997"); |
| 492 |
|
| 493 |
if Ada.Command_Line.Argument_Count = 0 then |
| 494 |
Help := True; |
| 495 |
else |
| 496 |
Parse_Command_Line; |
| 497 |
end if; |
| 498 |
|
| 499 |
if MDLL.Verbose or else Help then |
| 500 |
New_Line; |
| 501 |
Put_Line ("GNATDLL " & Version & " - Dynamic Libraries Builder"); |
| 502 |
New_Line; |
| 503 |
end if; |
| 504 |
|
| 505 |
MDLL.Utl.Locate; |
| 506 |
|
| 507 |
if Help |
| 508 |
or else (MDLL.Verbose and then Ada.Command_Line.Argument_Count = 1) |
| 509 |
then |
| 510 |
Syntax; |
| 511 |
else |
| 512 |
Check_Context; |
| 513 |
|
| 514 |
case Build_Mode is |
| 515 |
when Import_Lib => |
| 516 |
MDLL.Build_Import_Library |
| 517 |
(To_String (Lib_Filename), |
| 518 |
To_String (Def_Filename)); |
| 519 |
|
| 520 |
when Dynamic_Lib => |
| 521 |
MDLL.Build_Dynamic_Library |
| 522 |
(Objects_Files.all, |
| 523 |
Ali_Files.all, |
| 524 |
Options.all, |
| 525 |
Bargs_Options.all, |
| 526 |
Largs_Options.all, |
| 527 |
To_String (Lib_Filename), |
| 528 |
To_String (Def_Filename), |
| 529 |
To_String (DLL_Address), |
| 530 |
Build_Import => True, |
| 531 |
Relocatable => Must_Build_Relocatable, |
| 532 |
Map_File => Gen_Map_File); |
| 533 |
|
| 534 |
when Dynamic_Lib_Only => |
| 535 |
MDLL.Build_Dynamic_Library |
| 536 |
(Objects_Files.all, |
| 537 |
Ali_Files.all, |
| 538 |
Options.all, |
| 539 |
Bargs_Options.all, |
| 540 |
Largs_Options.all, |
| 541 |
To_String (Lib_Filename), |
| 542 |
To_String (Def_Filename), |
| 543 |
To_String (DLL_Address), |
| 544 |
Build_Import => False, |
| 545 |
Relocatable => Must_Build_Relocatable, |
| 546 |
Map_File => Gen_Map_File); |
| 547 |
|
| 548 |
when Nil => |
| 549 |
null; |
| 550 |
end case; |
| 551 |
end if; |
| 552 |
|
| 553 |
Set_Exit_Status (Success); |
| 554 |
|
| 555 |
exception |
| 556 |
when SE : Syntax_Error => |
| 557 |
Put_Line ("Syntax error : " & Exception_Message (SE)); |
| 558 |
New_Line; |
| 559 |
Syntax; |
| 560 |
Set_Exit_Status (Failure); |
| 561 |
|
| 562 |
when E : MDLL.Tools_Error | Context_Error => |
| 563 |
Put_Line (Exception_Message (E)); |
| 564 |
Set_Exit_Status (Failure); |
| 565 |
|
| 566 |
when others => |
| 567 |
Put_Line ("gnatdll: INTERNAL ERROR. Please report"); |
| 568 |
Set_Exit_Status (Failure); |
| 569 |
end Gnatdll; |