1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288 | ------------------------------------------------------------------------------
-- --
-- GPR TECHNOLOGY --
-- --
-- Copyright (C) 2006-2023, AdaCore --
-- --
-- This is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 3, or (at your option) any later ver- --
-- sion. This software is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details. You should have received a copy of the GNU --
-- General Public License distributed with GNAT; see file COPYING. If not, --
-- see <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------
-- gprbind is the executable called by gprbuild to bind Ada sources. It is
-- the driver for gnatbind. It gets its input from gprbuild through the
-- binding exchange file and gives back its results through the same file.
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Containers.Indefinite_Vectors;
with Ada.Directories;
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Directory_Operations; use GNAT.Directory_Operations;
with GNAT.OS_Lib; use GNAT.OS_Lib;
with Gprexch; use Gprexch;
with GPR.Script; use GPR, GPR.Script;
with GPR.ALI; use GPR.ALI;
with GPR.Names; use GPR.Names;
with GPR.Osint; use GPR.Osint;
with GPR.Tempdir;
with GPR.Util; use GPR.Util;
procedure Gprbind is
Executable_Suffix : constant String_Access := Get_Executable_Suffix;
-- The suffix of executables on this platforms
GNATBIND : String_Access := new String'("gnatbind");
-- The file name of the gnatbind executable. May be modified by an option
-- in the Minimum_Binder_Options.
Gnatbind_Prefix_Equal : constant String := "gnatbind_prefix=";
-- Start of the option to specify a prefix for the gnatbind executable
Gnatbind_Path_Equal : constant String := "--gnatbind_path=";
-- Start of the option to specify the absolute path of gnatbind
Ada_Binder_Equal : constant String := "ada_binder=";
-- Start of the option to specify the full name of the Ada binder
-- executable. Introduced for GNAAMP, where it is gnaambind.
Quiet_Output : Boolean := False;
Verbose_Low_Mode : Boolean := False;
Verbose_Higher_Mode : Boolean := False;
Dash_O_Specified : Boolean := False;
Dash_O_File_Specified : Boolean := False;
There_Are_Stand_Alone_Libraries : Boolean := False;
-- Set to True if the corresponding label is in the exchange file
No_Main_Option : constant String := "-n";
Dash_o : constant String := "-o";
Dash_x : constant String := "-x";
Dash_Fequal : constant String := "-F=";
Dash_OO : constant String := "-O";
-- Minimum switches to be used to compile the binder generated file
Dash_c : constant String := "-c";
Dash_gnatA : constant String := "-gnatA";
Dash_gnatWb : constant String := "-gnatWb";
Dash_gnatiw : constant String := "-gnatiw";
Dash_gnatws : constant String := "-gnatws";
IO_File : File_Type;
-- The file to get the inputs and to put the results of the binding
Line : String (1 .. 1_000);
Last : Natural;
Exchange_File_Name : String_Access;
Ada_Compiler_Path : String_Access;
FULL_GNATBIND : String_Access;
Gnatbind_Path : String_Access;
Gnatbind_Path_Specified : Boolean := False;
Compiler_Options : String_Vectors.Vector;
Compiler_Trailing_Options : String_Vectors.Vector;
Gnatbind_Options : String_Vectors.Vector;
Main_ALI : String_Access := null;
Main_Base_Name : String_Access := null;
Binder_Generated_File : String_Access := null;
BG_File : File_Type;
Mapping_File : String_Access := null;
Success : Boolean := False;
Return_Code : Integer;
Adalib_Dir : String_Access;
Prefix_Path : String_Access;
Lib_Path : String_Access;
Static_Libs : Boolean := True;
Current_Section : Binding_Section := No_Binding_Section;
All_Binding_Options : Boolean;
Get_Option : Boolean;
Xlinker_Seen : Boolean;
Stack_Equal_Seen : Boolean;
GNAT_Version : String_Access := new String'("000");
-- The version of GNAT, coming from the Toolchain_Version for Ada
GNAT_Version_First_2 : String (1 .. 2);
GNAT_Version_Set : Boolean := False;
-- True when the toolchain version is in the input exchange file
Delete_Temp_Files : Boolean := True;
FD_Objects : File_Descriptor;
Objects_Path : Path_Name_Type;
Objects_File : File_Type;
Ada_Object_Suffix : String_Access := Get_Object_Suffix;
Display_Line : String_Access := new String (1 .. 1_000);
Display_Last : Natural := 0;
-- A String buffer to store temporarily the displayed gnatbind command
-- invoked by gprbind.
procedure Add_To_Display_Line (S : String);
-- Add an argument to the Display_Line
procedure Output_Lib_Path_Or_Line (Lib_Name : String);
-- Output to IO_File full library pathname to the Other_Arguments if found
-- in Prefix_Path, Output Line (1 .. Last) otherwise.
Binding_Options_Table : String_Vectors.Vector;
Binding_Option_Dash_V_Specified : Boolean := False;
-- Set to True if -v is specified in the binding options
GNAT_6_Or_Higher : Boolean := False;
-- Set to True when GNAT version is neither 3.xx nor 5.xx
GNAT_6_4_Or_Higher : Boolean := False;
-- Set to True when GNAT_6_Or_Higher is True and if GNAT version is 6.xy
-- with x >= 4.
ALI_Files_Table : String_Vectors.Vector;
type Path_And_Stamp (Path_Len, Stamp_Len : Natural) is record
Path : String (1 .. Path_Len);
Stamp : String (1 .. Stamp_Len);
end record;
package PS_Vectors is new Ada.Containers.Indefinite_Vectors
(Positive, Path_And_Stamp);
Project_Paths : PS_Vectors.Vector;
type Bound_File;
type Bound_File_Access is access Bound_File;
type Bound_File is record
Name : String_Access;
Next : Bound_File_Access;
end record;
Bound_Files : Bound_File_Access;
-------------------------
-- Add_To_Display_Line --
-------------------------
procedure Add_To_Display_Line (S : String) is
begin
while Display_Last + 1 + S'Length > Display_Line'Last loop
declare
New_Buffer : constant String_Access :=
new String (1 .. 2 * Display_Line'Length);
begin
New_Buffer (1 .. Display_Last) :=
Display_Line (1 .. Display_Last);
Free (Display_Line);
Display_Line := New_Buffer;
end;
end loop;
if Display_Last > 0 then
Display_Last := Display_Last + 1;
Display_Line (Display_Last) := ' ';
end if;
Display_Line (Display_Last + 1 .. Display_Last + S'Length) := S;
Display_Last := Display_Last + S'Length;
end Add_To_Display_Line;
-----------------------------
-- Output_Lib_Path_Or_Line --
-----------------------------
procedure Output_Lib_Path_Or_Line (Lib_Name : String) is
begin
Lib_Path := Locate_Regular_File (Lib_Name, Prefix_Path.all);
if Lib_Path /= null then
Put_Line (IO_File, Lib_Path.all);
Free (Lib_Path);
else
Put_Line (IO_File, Line (1 .. Last));
end if;
end Output_Lib_Path_Or_Line;
begin
Set_Program_Name ("gprbind");
-- As the section header has alreading been displayed when gprlib was
-- invoked, indicate that it should not be displayed again.
GPR.Set (Section => GPR.Bind);
if Argument_Count /= 1 then
Fail_Program (null, "incorrect invocation");
end if;
Exchange_File_Name := new String'(Argument (1));
-- DEBUG: save a copy of the exchange file
declare
Gprbind_Debug : constant String := Getenv ("GPRBIND_DEBUG").all;
begin
if Gprbind_Debug = "TRUE" then
Copy_File
(Exchange_File_Name.all,
Exchange_File_Name.all & "__saved",
Success,
Mode => Overwrite,
Preserve => Time_Stamps);
end if;
end;
-- Open the binding exchange file
begin
Open (IO_File, In_File, Exchange_File_Name.all);
exception
when others =>
Fail_Program (null, "could not read " & Exchange_File_Name.all);
end;
-- Get the information from the binding exchange file
while not End_Of_File (IO_File) loop
Get_Line (IO_File, Line, Last);
if Last > 0 then
if Line (1) = '[' then
Current_Section := Get_Binding_Section (Line (1 .. Last));
case Current_Section is
when No_Binding_Section =>
Fail_Program
(null, "unknown section: " & Line (1 .. Last));
when Quiet =>
Quiet_Output := True;
Verbose_Low_Mode := False;
Verbose_Higher_Mode := False;
when Verbose_Low =>
Quiet_Output := False;
Verbose_Low_Mode := True;
Verbose_Higher_Mode := False;
when Verbose_Higher =>
Quiet_Output := False;
Verbose_Low_Mode := True;
Verbose_Higher_Mode := True;
when Shared_Libs =>
Static_Libs := False;
when Gprexch.There_Are_Stand_Alone_Libraries =>
There_Are_Stand_Alone_Libraries := True;
when others =>
null;
end case;
else
case Current_Section is
when No_Binding_Section =>
Fail_Program
(null, "no section specified: " & Line (1 .. Last));
when Quiet =>
Fail_Program (null, "quiet section should be empty");
when Verbose_Low | Verbose_Higher =>
Fail_Program (null, "verbose section should be empty");
when Shared_Libs =>
Fail_Program
(null, "shared libs section should be empty");
when Gprexch.There_Are_Stand_Alone_Libraries =>
Fail_Program
(null, "stand-alone libraries section should be empty");
when Gprexch.Main_Base_Name =>
if Main_Base_Name /= null then
Fail_Program
(null, "main base name specified multiple times");
end if;
Main_Base_Name := new String'(Line (1 .. Last));
when Gprexch.Mapping_File =>
Mapping_File := new String'(Line (1 .. Last));
when Compiler_Path =>
if Ada_Compiler_Path /= null then
Fail_Program
(null, "compiler path specified multiple times");
end if;
Ada_Compiler_Path := new String'(Line (1 .. Last));
when Compiler_Leading_Switches =>
Compiler_Options.Append (Line (1 .. Last));
when Compiler_Trailing_Switches =>
Compiler_Trailing_Options.Append (Line (1 .. Last));
when Main_Dependency_File =>
if Main_ALI /= null then
Fail_Program
(null, "main ALI file specified multiple times");
end if;
Main_ALI := new String'(Line (1 .. Last));
when Dependency_Files =>
ALI_Files_Table.Append (Line (1 .. Last));
when Binding_Options =>
-- Check if a gnatbind absolute is specified
if Last > Gnatbind_Path_Equal'Length
and then Line (1 .. Gnatbind_Path_Equal'Length) =
Gnatbind_Path_Equal
then
Gnatbind_Path := new String'
(Line (Gnatbind_Path_Equal'Length + 1 .. Last));
Gnatbind_Path_Specified := True;
-- Check if a gnatbind prefix is specified
elsif Starts_With (Line (1 .. Last), Gnatbind_Prefix_Equal)
then
-- Ignore an empty prefix
if Last > Gnatbind_Prefix_Equal'Length then
-- There is always a '-' between <prefix> and
-- "gnatbind". Add one if not already in <prefix>.
if Line (Last) /= '-' then
Last := Last + 1;
Line (Last) := '-';
end if;
GNATBIND := new String'
(Line (Gnatbind_Prefix_Equal'Length + 1 .. Last) &
"gnatbind");
end if;
elsif Last > Ada_Binder_Equal'Length
and then Line (1 .. Ada_Binder_Equal'Length) =
Ada_Binder_Equal
then
GNATBIND := new String'
(Line (Ada_Binder_Equal'Length + 1 .. Last));
-- When -O is used, instead of -O=file, -v is ignored to
-- avoid polluting the output. Record occurence of -v and
-- check the GNAT version later.
elsif Line (1 .. Last) = "-v" then
Binding_Option_Dash_V_Specified := True;
-- Ignore -C, as the generated sources are always in Ada
elsif Line (1 .. Last) /= "-C" then
Binding_Options_Table.Append (Line (1 .. Last));
end if;
when Project_Files =>
if End_Of_File (IO_File) then
Fail_Program
(null, "no time stamp for " & Line (1 .. Last));
else
declare
Path : constant String := Line (1 .. Last);
begin
Get_Line (IO_File, Line, Last);
Project_Paths.Append
(Path_And_Stamp'
(Path_Len => Path'Length,
Stamp_Len => Last,
Path => Path,
Stamp => Line (1 .. Last)));
end;
end if;
when Gprexch.Toolchain_Version =>
if End_Of_File (IO_File) then
Fail_Program
(null,
"no toolchain version for language "
& Line (1 .. Last));
elsif Line (1 .. Last) = "ada" then
Get_Line (IO_File, Line, Last);
if Last > 5 and then Line (1 .. 5) = GNAT_And_Space then
GNAT_Version := new String'(Line (6 .. Last));
GNAT_Version_Set := True;
GNAT_Version_First_2 :=
(if Last = 6 then Line (6) & ' ' else Line (6 .. 7));
end if;
else
Skip_Line (IO_File);
end if;
when Gprexch.Delete_Temp_Files =>
begin
Delete_Temp_Files := Boolean'Value (Line (1 .. Last));
exception
when Constraint_Error =>
null;
end;
when Gprexch.Object_File_Suffix =>
if End_Of_File (IO_File) then
Fail_Program
(null,
"no object file suffix for language "
& Line (1 .. Last));
elsif Line (1 .. Last) = "ada" then
Get_Line (IO_File, Line, Last);
Ada_Object_Suffix := new String'(Line (1 .. Last));
else
Skip_Line (IO_File);
end if;
when Script_Path =>
Build_Script_Name := new String'(Line (1 .. Last));
when Nothing_To_Bind |
Generated_Object_File |
Generated_Source_Files |
Bound_Object_Files |
Resulting_Options |
Run_Path_Option =>
null;
end case;
end if;
end if;
end loop;
if Main_Base_Name = null then
Fail_Program (null, "no main base name specified");
else
Binder_Generated_File :=
new String'("b__" & Main_Base_Name.all & ".adb");
end if;
Close (IO_File);
-- Modify binding option -A=<file> if <file> is not an absolute path
if not Project_Paths.Is_Empty then
declare
Project_Dir : constant String :=
Ada.Directories.Containing_Directory
(Project_Paths.First_Element.Path);
begin
for J in 1 .. Binding_Options_Table.Last_Index loop
if Binding_Options_Table.Element (J)'Length >= 4 and then
Binding_Options_Table (J) (1 .. 3) = "-A="
then
declare
Value : constant String := Binding_Options_Table.Element (J);
File : constant String := Value (4 .. Value'Last);
begin
if not Is_Absolute_Path (File) then
declare
New_File : constant String :=
Normalize_Pathname
(File, Project_Dir,
Resolve_Links => False);
begin
Binding_Options_Table.Replace_Element
(J, "-A=" & New_File);
end;
end if;
end;
end if;
end loop;
end;
end if;
-- Check if GNAT version is 6.4 or higher
if GNAT_Version_Set
and then GNAT_Version.all /= "000"
and then GNAT_Version_First_2 not in "3." | "5."
then
GNAT_6_Or_Higher := True;
if GNAT_Version_First_2 /= "6." or else GNAT_Version.all >= "6.4" then
GNAT_6_4_Or_Higher := True;
end if;
end if;
-- Check if binding option -v was specified and issue it only if the GNAT
-- version is 6.4 or higher, otherwise the output of gnatbind -O will be
-- polluted.
if Binding_Option_Dash_V_Specified and then GNAT_6_4_Or_Higher then
Binding_Options_Table.Append ("-v");
end if;
if not Static_Libs then
Gnatbind_Options.Append (Dash_Shared);
end if;
-- Specify the name of the generated file to gnatbind
Gnatbind_Options.Append (Dash_o);
Gnatbind_Options.Append (Binder_Generated_File.all);
if Ada_Compiler_Path = null then
Fail_Program (null, "no Ada compiler path specified");
elsif not Is_Regular_File (Ada_Compiler_Path.all) then
Fail_Program (null, "could not find the Ada compiler");
end if;
if Main_ALI /= null then
Gnatbind_Options.Append (Main_ALI.all);
end if;
-- If there are Stand-Alone Libraries, invoke gnatbind with -F (generate
-- checks of elaboration flags) to avoid multiple elaborations.
if There_Are_Stand_Alone_Libraries
and then GNAT_Version_Set
and then GNAT_Version_First_2 /= "3."
then
Gnatbind_Options.Append ("-F");
end if;
Gnatbind_Options.Append_Vector (ALI_Files_Table);
for Option of Binding_Options_Table loop
Gnatbind_Options.Append (Option);
if Option = Dash_OO then
Dash_O_Specified := True;
elsif Starts_With (Option, Dash_OO & '=') then
Dash_O_Specified := True;
Dash_O_File_Specified := True;
Objects_Path := Get_Path_Name_Id (Option (4 .. Option'Last));
end if;
end loop;
-- Add -x at the end, so that if -s is specified in the binding options,
-- gnatbind does not try to look for sources, as the binder mapping file
-- specified by -F- is not for sources, but for ALI files.
Gnatbind_Options.Append (Dash_x);
if Is_Absolute_Path (GNATBIND.all) then
FULL_GNATBIND := GNATBIND;
else
FULL_GNATBIND :=
new String'
(Dir_Name (Ada_Compiler_Path.all) &
Directory_Separator &
GNATBIND.all);
end if;
if Gnatbind_Path_Specified then
FULL_GNATBIND := Gnatbind_Path;
end if;
Gnatbind_Path := Locate_Exec_On_Path (FULL_GNATBIND.all);
-- If gnatbind is not found and its full path was not specified, check for
-- gnatbind on the path.
if Gnatbind_Path = null and then not Gnatbind_Path_Specified then
Gnatbind_Path := Locate_Exec_On_Path (GNATBIND.all);
end if;
if Gnatbind_Path = null then
-- Make sure Namelen has a non negative value
Name_Len := 0;
declare
Path_Of_Gnatbind : String_Access := GNATBIND;
begin
if Gnatbind_Path_Specified then
Path_Of_Gnatbind := FULL_GNATBIND;
end if;
Finish_Program
(null,
Osint.E_Fatal,
"could not locate " & Path_Of_Gnatbind.all);
end;
else
-- Normalize the path, so that gnaampbind does not complain about not
-- being in a "bin" directory. But don't resolve symbolic links,
-- because in GNAT 5.01a1 and previous releases, gnatbind was a symbolic
-- link for .gnat_wrapper.
Gnatbind_Path :=
new String'
(Normalize_Pathname (Gnatbind_Path.all, Resolve_Links => False));
end if;
if Main_ALI = null then
Gnatbind_Options.Append (No_Main_Option);
end if;
-- Add the switch -F=<mapping file> if the mapping file was specified
-- and the version of GNAT is recent enough.
if Mapping_File /= null
and then GNAT_Version_Set
and then GNAT_Version_First_2 /= "3."
then
Gnatbind_Options.Append (Dash_Fequal & Mapping_File.all);
end if;
-- Create temporary file to get the list of objects
if not Dash_O_File_Specified then
Tempdir.Create_Temp_File (FD_Objects, Objects_Path);
Record_Temp_File (null, Objects_Path);
end if;
if GNAT_6_4_Or_Higher then
if not Dash_O_File_Specified then
Gnatbind_Options.Append
(Dash_OO & "=" & Get_Name_String (Objects_Path));
Close (FD_Objects);
end if;
elsif not Dash_O_Specified then
Gnatbind_Options.Append (Dash_OO);
end if;
if not Quiet_Output then
if Verbose_Low_Mode then
Display_Last := 0;
Add_To_Display_Line (Gnatbind_Path.all);
for Option of Gnatbind_Options loop
Add_To_Display_Line (Option);
end loop;
Put_Line (Display_Line (1 .. Display_Last));
else
if Main_ALI /= null then
Display
(Section => GPR.Bind,
Command => "Ada",
Argument => Base_Name (Main_ALI.all));
elsif not ALI_Files_Table.Is_Empty then
Display
(Section => GPR.Bind,
Command => "Ada",
Argument => Base_Name (ALI_Files_Table.First_Element)
& " " & No_Main_Option);
end if;
end if;
end if;
declare
Size : Natural := 0;
Args_List : String_List_Access;
begin
for Option of Gnatbind_Options loop
Size := Size + Option'Length + 1;
end loop;
-- Invoke gnatbind with the arguments if the size is not too large or
-- if the version of GNAT is not recent enough.
Script_Write (Gnatbind_Path.all, Gnatbind_Options);
if not GNAT_6_Or_Higher or else Size <= Maximum_Size then
Args_List := new String_List'(To_Argument_List (Gnatbind_Options));
if not GNAT_6_4_Or_Higher then
Spawn
(Gnatbind_Path.all,
Args_List.all,
FD_Objects,
Return_Code,
Err_To_Out => False);
Success := Return_Code = 0;
else
Return_Code := Spawn (Gnatbind_Path.all, Args_List.all);
end if;
Free (Args_List);
else
-- Otherwise create a temporary response file
declare
FD : File_Descriptor;
Path : Path_Name_Type;
Args : Argument_List (1 .. 1);
EOL : constant String (1 .. 1) := (1 => ASCII.LF);
Status : Integer;
Quotes_Needed : Boolean;
Last_Char : Natural;
Ch : Character;
begin
Tempdir.Create_Temp_File (FD, Path);
Record_Temp_File (null, Path);
Args (1) := new String'("@" & Get_Name_String (Path));
for Option of Gnatbind_Options loop
-- Check if the argument should be quoted
Quotes_Needed := False;
Last_Char := Option'Length;
for J in Option'Range loop
Ch := Option (J);
if Ch = ' ' or else Ch = ASCII.HT or else Ch = '"' then
Quotes_Needed := True;
exit;
end if;
end loop;
if Quotes_Needed then
-- Quote the argument, doubling '"'
declare
Arg : String (1 .. Option'Length * 2 + 2);
begin
Arg (1) := '"';
Last_Char := 1;
for J in Option'Range loop
Ch := Option (J);
Last_Char := Last_Char + 1;
Arg (Last_Char) := Ch;
if Ch = '"' then
Last_Char := Last_Char + 1;
Arg (Last_Char) := '"';
end if;
end loop;
Last_Char := Last_Char + 1;
Arg (Last_Char) := '"';
Status := Write (FD, Arg'Address, Last_Char);
end;
else
Status := Write
(FD,
Option (Option'First)'Address,
Last_Char);
end if;
if Status /= Last_Char then
Fail_Program (null, "disk full");
end if;
Status := Write (FD, EOL (1)'Address, 1);
if Status /= 1 then
Fail_Program (null, "disk full");
end if;
end loop;
Close (FD);
-- And invoke gnatbind with this response file
if not GNAT_6_4_Or_Higher then
Spawn
(Gnatbind_Path.all,
Args,
FD_Objects,
Return_Code,
Err_To_Out => False);
else
Return_Code := Spawn (Gnatbind_Path.all, Args);
end if;
end;
end if;
end;
if not GNAT_6_4_Or_Higher and then not Dash_O_File_Specified then
Close (FD_Objects);
end if;
if Return_Code /= 0 then
Fail_Program (null, "invocation of gnatbind failed");
end if;
Compiler_Options.Append (Dash_c);
Compiler_Options.Append (Dash_gnatA);
Compiler_Options.Append (Dash_gnatWb);
Compiler_Options.Append (Dash_gnatiw);
Compiler_Options.Append (Dash_gnatws);
-- Read the ALI file of the first ALI file. Fetch the back end switches
-- from this ALI file and use these switches to compile the binder
-- generated file.
if Main_ALI /= null or else not ALI_Files_Table.Is_Empty then
Initialize_ALI;
declare
F : constant File_Name_Type :=
Get_File_Name_Id
(if Main_ALI = null then ALI_Files_Table.First_Element
else Main_ALI.all);
T : Text_Buffer_Ptr;
A : ALI_Id;
begin
-- Load the ALI file
T := Osint.Read_Library_Info (F, True);
-- Read it. Note that we ignore errors, since we only want very
-- limited information from the ali file, and likely a slightly
-- wrong version will be just fine, though in normal operation
-- we don't expect this to happen.
A := Scan_ALI
(F,
T,
Ignore_ED => False,
Err => False,
Read_Lines => "A");
if A /= No_ALI_Id then
for
Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
Units.Table (ALIs.Table (A).First_Unit).Last_Arg
loop
-- Do not compile with the front end switches
declare
Arg : String_Access renames Args.Table (Index);
Argv : constant String (1 .. Arg'Length) := Arg.all;
begin
if (Argv'Last <= 2 or else Argv (1 .. 2) /= "-I")
and then
(Argv'Last <= 5 or else Argv (1 .. 5) /= "-gnat")
and then
(Argv'Last <= 6 or else Argv (1 .. 6) /= "--RTS=")
then
Compiler_Options.Append (Arg.all);
end if;
end;
end loop;
end if;
end;
end if;
Compiler_Options.Append (Binder_Generated_File.all);
declare
Object : constant String :=
"b__" & Main_Base_Name.all & Ada_Object_Suffix.all;
begin
Compiler_Options.Append (Dash_o);
Compiler_Options.Append (Object);
-- Add the trailing options, if any
Compiler_Options.Append_Vector (Compiler_Trailing_Options);
if Verbose_Low_Mode then
Set_Name_Buffer (Ada_Compiler_Path.all);
-- Remove the executable suffix, if present
if Executable_Suffix'Length > 0
and then
Name_Len > Executable_Suffix'Length
and then
Name_Buffer
(Name_Len - Executable_Suffix'Length + 1 .. Name_Len) =
Executable_Suffix.all
then
Name_Len := Name_Len - Executable_Suffix'Length;
end if;
Display_Last := 0;
Add_To_Display_Line (Name_Buffer (1 .. Name_Len));
for Option of Compiler_Options loop
Add_To_Display_Line (Option);
end loop;
Put_Line (Display_Line (1 .. Display_Last));
end if;
Spawn_And_Script_Write
(Ada_Compiler_Path.all,
Compiler_Options,
Success);
if not Success then
Fail_Program (null, "compilation of binder generated file failed");
end if;
Create (IO_File, Out_File, Exchange_File_Name.all);
-- First, the generated object file
Put_Line (IO_File, Binding_Label (Generated_Object_File));
Put_Line (IO_File, Object);
-- Repeat the project paths with their time stamps
Put_Line (IO_File, Binding_Label (Project_Files));
for PS of Project_Paths loop
Put_Line (IO_File, PS.Path);
Put_Line (IO_File, PS.Stamp);
end loop;
-- Get the bound object files from the Object file
Open (Objects_File, In_File, Get_Name_String (Objects_Path));
Put_Line (IO_File, Binding_Label (Bound_Object_Files));
while not End_Of_File (Objects_File) loop
Get_Line (Objects_File, Line, Last);
-- Only put in the exchange file the path of the object files.
-- Output anything else on standard output.
if Is_Regular_File (Line (1 .. Last)) then
Put_Line (IO_File, Line (1 .. Last));
Bound_Files := new Bound_File'
(Name => new String'(Line (1 .. Last)), Next => Bound_Files);
if Dash_O_Specified and then not Dash_O_File_Specified then
Put_Line (Line (1 .. Last));
end if;
elsif not Dash_O_File_Specified then
Put_Line (Line (1 .. Last));
end if;
end loop;
Close (Objects_File);
-- For the benefit of gprclean, the generated files other than the
-- generated object file.
Put_Line (IO_File, Binding_Label (Generated_Source_Files));
Put_Line (IO_File, "b__" & Main_Base_Name.all & ".ads");
Put_Line (IO_File, Binder_Generated_File.all);
Put_Line (IO_File, "b__" & Main_Base_Name.all & ".ali");
-- Get the options from the binder generated file
Open (BG_File, In_File, Binder_Generated_File.all);
while not End_Of_File (BG_File) loop
Get_Line (BG_File, Line, Last);
exit when Line (1 .. Last) = Begin_Info;
end loop;
if not End_Of_File (BG_File) then
Put_Line (IO_File, Binding_Label (Resulting_Options));
All_Binding_Options := False;
Xlinker_Seen := False;
Stack_Equal_Seen := False;
loop
Get_Line (BG_File, Line, Last);
exit when Line (1 .. Last) = End_Info;
Line (1 .. Last - 8) := Line (9 .. Last);
Last := Last - 8;
if Line (1) = '-' then
-- After the first switch, we take all options, because some
-- of the options specified in pragma Linker_Options may not
-- start with '-'.
All_Binding_Options := True;
end if;
Get_Option :=
All_Binding_Options
or else
Base_Name (Line (1 .. Last)) in "g-trasym.o" | "g-trasym.obj";
-- g-trasym is a special case as it is not included in libgnat
-- Avoid duplication of object file
if Get_Option then
declare
BF : Bound_File_Access := Bound_Files;
begin
while BF /= null loop
if BF.Name.all = Line (1 .. Last) then
Get_Option := False;
exit;
else
BF := BF.Next;
end if;
end loop;
end;
end if;
if Get_Option then
if Line (1 .. Last) = "-Xlinker" then
Xlinker_Seen := True;
elsif Xlinker_Seen then
Xlinker_Seen := False;
-- Make sure that only the first switch --stack= is put in
-- the exchange file.
if Last > 8 and then Line (1 .. 8) = "--stack=" then
if not Stack_Equal_Seen then
Stack_Equal_Seen := True;
Put_Line (IO_File, "-Xlinker");
Put_Line (IO_File, Line (1 .. Last));
end if;
else
Put_Line (IO_File, "-Xlinker");
Put_Line (IO_File, Line (1 .. Last));
end if;
elsif Last > 12 and then Line (1 .. 12) = "-Wl,--stack=" then
if not Stack_Equal_Seen then
Stack_Equal_Seen := True;
Put_Line (IO_File, Line (1 .. Last));
end if;
elsif Last >= 3 and then Line (1 .. 2) = "-L" then
-- Set Adalib_Dir only if libgnat is found inside.
if Is_Regular_File
(Line (3 .. Last) & Directory_Separator & "libgnat.a")
then
Adalib_Dir := new String'(Line (3 .. Last));
if Verbose_Higher_Mode then
Put_Line ("Adalib_Dir = """ & Adalib_Dir.all & '"');
end if;
-- Build the Prefix_Path, where to look for some
-- archives: libaddr2line.a, libbfd.a, libgnatmon.a,
-- libgnalasup.a and libiberty.a. It contains three
-- directories: $(adalib)/.., $(adalib)/../.. and the
-- subdirectory "lib" ancestor of $(adalib).
declare
Dir_Last : Positive;
Prev_Dir_Last : Positive;
First : Positive;
Prev_Dir_First : Positive;
Nmb : Natural;
begin
Set_Name_Buffer (Line (3 .. Last));
while Name_Buffer (Name_Len) = Directory_Separator
or else Name_Buffer (Name_Len) = '/'
loop
Name_Len := Name_Len - 1;
end loop;
while Name_Buffer (Name_Len) /= Directory_Separator
and then Name_Buffer (Name_Len) /= '/'
loop
Name_Len := Name_Len - 1;
end loop;
while Name_Buffer (Name_Len) = Directory_Separator
or else Name_Buffer (Name_Len) = '/'
loop
Name_Len := Name_Len - 1;
end loop;
Dir_Last := Name_Len;
Nmb := 0;
Dir_Loop : loop
Prev_Dir_Last := Dir_Last;
First := Dir_Last - 1;
while First > 3
and then
Name_Buffer (First) /= Directory_Separator
and then
Name_Buffer (First) /= '/'
loop
First := First - 1;
end loop;
Prev_Dir_First := First + 1;
exit Dir_Loop when First <= 3;
Dir_Last := First - 1;
while Name_Buffer (Dir_Last) = Directory_Separator
or else Name_Buffer (Dir_Last) = '/'
loop
Dir_Last := Dir_Last - 1;
end loop;
Nmb := Nmb + 1;
if Nmb <= 1 then
Add_Char_To_Name_Buffer (Path_Separator);
Add_Str_To_Name_Buffer
(Name_Buffer (1 .. Dir_Last));
elsif Name_Buffer (Prev_Dir_First .. Prev_Dir_Last)
= "lib"
then
Add_Char_To_Name_Buffer (Path_Separator);
Add_Str_To_Name_Buffer
(Name_Buffer (1 .. Prev_Dir_Last));
exit Dir_Loop;
end if;
end loop Dir_Loop;
Prefix_Path :=
new String'(Name_Buffer (1 .. Name_Len));
if Verbose_Higher_Mode then
Put_Line
("Prefix_Path = """ & Prefix_Path.all & '"');
end if;
end;
end if;
Put_Line (IO_File, Line (1 .. Last));
elsif Line (1 .. Last) in Static_Libgcc | Shared_Libgcc then
Put_Line (IO_File, Line (1 .. Last));
-- For a number of archives, we need to indicate the full
-- path of the archive, if we find it, to be sure that the
-- correct archive is used by the linker.
elsif Line (1 .. Last) = Dash_Lgnat then
if Adalib_Dir = null then
if Verbose_Higher_Mode then
Put_Line ("No Adalib_Dir");
end if;
Put_Line (IO_File, Dash_Lgnat);
elsif Static_Libs then
Put_Line (IO_File, Adalib_Dir.all & "libgnat.a");
else
Put_Line (IO_File, Dash_Lgnat);
end if;
elsif Line (1 .. Last) = Dash_Lgnarl
and then Static_Libs
and then Adalib_Dir /= null
then
Put_Line (IO_File, Adalib_Dir.all & "libgnarl.a");
elsif Line (1 .. Last) = "-laddr2line"
and then Prefix_Path /= null
then
Output_Lib_Path_Or_Line ("libaddr2line.a");
elsif Line (1 .. Last) = "-lbfd"
and then Prefix_Path /= null
then
Output_Lib_Path_Or_Line ("libbfd.a");
elsif Line (1 .. Last) = "-lgnalasup"
and then Prefix_Path /= null
then
Output_Lib_Path_Or_Line ("libgnalasup.a");
elsif Line (1 .. Last) = "-lgnatmon"
and then Prefix_Path /= null
then
Output_Lib_Path_Or_Line ("libgnatmon.a");
elsif Line (1 .. Last) = "-liberty"
and then Prefix_Path /= null
then
Output_Lib_Path_Or_Line ("libiberty.a");
else
Put_Line (IO_File, Line (1 .. Last));
end if;
end if;
end loop;
end if;
Close (BG_File);
if not Static_Libs
and then Adalib_Dir /= null
then
Put_Line (IO_File, Binding_Label (Run_Path_Option));
Put_Line (IO_File, Adalib_Dir.all);
Name_Len := Adalib_Dir'Length;
Name_Buffer (1 .. Name_Len) := Adalib_Dir.all;
for J in reverse 2 .. Name_Len - 4 loop
if Name_Buffer (J) = Directory_Separator and then
Name_Buffer (J + 4) = Directory_Separator and then
Name_Buffer (J + 1 .. J + 3) = "lib"
then
Name_Len := J + 3;
Put_Line (IO_File, Name_Buffer (1 .. Name_Len));
exit;
end if;
end loop;
end if;
Close (IO_File);
end;
if Delete_Temp_Files then
Delete_All_Temp_Files (null);
end if;
end Gprbind;
|