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
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
|
-- GHDL driver - commands invoking gcc.
-- Copyright (C) 2002, 2003, 2004, 2005 Tristan Gingold
--
-- GHDL is free software; you can redistribute it and/or modify it under
-- the terms of the GNU General Public License as published by the Free
-- Software Foundation; either version 2, or (at your option) any later
-- version.
--
-- GHDL is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY 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
-- along with GCC; see the file COPYING. If not, write to the Free
-- Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-- 02111-1307, USA.
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Characters.Latin_1;
with GNAT.OS_Lib; use GNAT.OS_Lib;
with Tables;
with GNAT.Dynamic_Tables;
with Libraries;
with Name_Table; use Name_Table;
with Std_Package;
with Types; use Types;
with Iirs; use Iirs;
with Files_Map;
with Configuration;
with Default_Paths;
with Interfaces.C_Streams;
with System;
with Ghdlmain; use Ghdlmain;
with Ghdllocal; use Ghdllocal;
with Errorout;
with Version;
with Options;
package body Ghdldrv is
-- Name of the tools used.
Compiler_Cmd : String_Access := null;
Post_Processor_Cmd : String_Access := null;
Assembler_Cmd : String_Access := null;
Linker_Cmd : String_Access := null;
-- Path of the tools.
Compiler_Path : String_Access;
Post_Processor_Path : String_Access;
Assembler_Path : String_Access;
Linker_Path : String_Access;
-- Set by the '-o' option: the output filename. If the option is not
-- present, then null.
Output_File : String_Access;
-- "-o" string.
Dash_o : constant String_Access := new String'("-o");
-- "-c" string.
Dash_c : constant String_Access := new String'("-c");
-- "-quiet" option.
Dash_Quiet : constant String_Access := new String'("-quiet");
-- "-fpic" option.
Dash_Fpic : constant String_Access := new String'("-fpic");
-- If set, do not assmble
Flag_Asm : Boolean;
-- If true, executed commands are displayed.
Flag_Disp_Commands : Boolean;
-- Flag not quiet
Flag_Not_Quiet : Boolean;
-- True if failure expected.
Flag_Expect_Failure : Boolean;
-- Elaboration mode.
type Elab_Mode_Type is
(-- Static elaboration (or pre-elaboration).
Elab_Static,
-- Dynamic elaboration: design is elaborated just before being run.
Elab_Dynamic);
-- Default elaboration mode is dynamic.
Elab_Mode : Elab_Mode_Type := Elab_Dynamic;
-- Argument table for the tools.
-- Each table low bound is 1 so that the length of a table is equal to
-- the last bound.
package Argument_Table_Pkg is new GNAT.Dynamic_Tables
(Table_Component_Type => String_Access,
Table_Index_Type => Integer,
Table_Low_Bound => 1,
Table_Initial => 4,
Table_Increment => 100);
use Argument_Table_Pkg;
-- Arguments for tools.
Compiler_Args : Argument_Table_Pkg.Instance;
Postproc_Args : Argument_Table_Pkg.Instance;
Assembler_Args : Argument_Table_Pkg.Instance;
Linker_Args : Argument_Table_Pkg.Instance;
-- Display the program spawned in Flag_Disp_Commands is TRUE.
-- Return the exit status.
function My_Spawn_Status (Program_Name : String; Args : Argument_List)
return Integer is
begin
if Flag_Disp_Commands then
Put (Program_Name);
for I in Args'Range loop
Put (' ');
Put (Args (I).all);
end loop;
New_Line;
end if;
return Spawn (Program_Name, Args);
end My_Spawn_Status;
-- Display the program spawned in Flag_Disp_Commands is TRUE.
-- Raise COMPILE_ERROR in case of failure.
procedure My_Spawn (Program_Name : String; Args : Argument_List)
is
Status : Integer;
begin
Status := My_Spawn_Status (Program_Name, Args);
if Status = 0 then
return;
elsif Status = 1 then
Error ("compilation error");
raise Compile_Error;
elsif Status > 127 then
Error ("executable killed by a signal");
raise Exec_Error;
else
Error ("exec error");
raise Exec_Error;
end if;
end My_Spawn;
-- Compile FILE with additional argument OPTIONSS.
procedure Do_Compile
(Options : Argument_List; File : String; In_Work : Boolean)
is
Obj_File : String_Access;
Asm_File : String_Access;
Post_File : String_Access;
Success : Boolean;
begin
-- Create post file.
if Flag_Postprocess then
Post_File := Append_Suffix (File, Post_Suffix, In_Work);
end if;
-- Create asm file.
case Backend is
when Backend_Gcc =>
Asm_File := Append_Suffix (File, Asm_Suffix, In_Work);
when Backend_Llvm
| Backend_Mcode =>
null;
end case;
-- Create obj file (may not be used, but the condition isn't simple).
Obj_File := Append_Suffix (File, Get_Object_Suffix.all, In_Work);
-- Compile.
declare
P : Natural;
Nbr_Args : constant Natural :=
Last (Compiler_Args) + Options'Length + 5;
Args : Argument_List (1 .. Nbr_Args);
begin
P := 0;
for I in First .. Last (Compiler_Args) loop
P := P + 1;
Args (P) := Compiler_Args.Table (I);
end loop;
for I in Options'Range loop
P := P + 1;
Args (P) := Options (I);
end loop;
-- Add -quiet for gcc, add -c for llvm
if not Flag_Postprocess then
case Backend is
when Backend_Gcc =>
if not Flag_Not_Quiet then
P := P + 1;
Args (P) := Dash_Quiet;
end if;
when Backend_Llvm =>
P := P + 1;
Args (P) := Dash_c;
when Backend_Mcode =>
null;
end case;
end if;
-- Add -fpic for gcc/llvm.
if not Flag_Postprocess
and then Default_Paths.Default_Pie
then
case Backend is
when Backend_Gcc
| Backend_Llvm =>
P := P + 1;
Args (P) := Dash_Fpic;
when Backend_Mcode =>
null;
end case;
end if;
-- Object file (or assembly file).
Args (P + 1) := Dash_o;
if Flag_Postprocess then
Args (P + 2) := Post_File;
else
case Backend is
when Backend_Gcc =>
Args (P + 2) := Asm_File;
when Backend_Mcode
| Backend_Llvm =>
Args (P + 2) := Obj_File;
end case;
end if;
Args (P + 3) := new String'(File);
My_Spawn (Compiler_Path.all, Args (1 .. P + 3));
Free (Args (P + 3));
exception
when Compile_Error =>
-- Delete temporary file in case of error.
Delete_File (Args (P + 2).all, Success);
-- FIXME: delete object file too ?
raise;
end;
-- Post-process.
if Flag_Postprocess then
declare
P : Natural;
Nbr_Args : constant Natural := Last (Postproc_Args) + 5;
Args : Argument_List (1 .. Nbr_Args);
begin
P := 0;
for I in First .. Last (Postproc_Args) loop
P := P + 1;
Args (P) := Postproc_Args.Table (I);
end loop;
case Backend is
when Backend_Gcc =>
if not Flag_Not_Quiet then
P := P + 1;
Args (P) := Dash_Quiet;
end if;
when Backend_Llvm =>
null;
when Backend_Mcode =>
null;
end case;
Args (P + 1) := Dash_o;
case Backend is
when Backend_Gcc =>
Args (P + 2) := Asm_File;
when Backend_Llvm
| Backend_Mcode =>
Args (P + 2) := Obj_File;
end case;
Args (P + 3) := Post_File;
My_Spawn (Post_Processor_Path.all, Args (1 .. P + 3));
end;
Free (Post_File);
end if;
-- Assemble.
case Backend is
when Backend_Gcc =>
if Flag_Expect_Failure then
Delete_File (Asm_File.all, Success);
elsif not Flag_Asm then
declare
P : Natural;
Nbr_Args : constant Natural := Last (Assembler_Args) + 4;
Args : Argument_List (1 .. Nbr_Args);
Success : Boolean;
begin
P := 0;
for I in First .. Last (Assembler_Args) loop
P := P + 1;
Args (P) := Assembler_Args.Table (I);
end loop;
Args (P + 1) := Dash_o;
Args (P + 2) := Obj_File;
Args (P + 3) := Asm_File;
My_Spawn (Assembler_Path.all, Args (1 .. P + 3));
Delete_File (Asm_File.all, Success);
end;
end if;
when Backend_Mcode
| Backend_Llvm =>
null;
end case;
Free (Asm_File);
Free (Obj_File);
end Do_Compile;
package Filelist is new Tables
(Table_Component_Type => String_Access,
Table_Index_Type => Natural,
Table_Low_Bound => 1,
Table_Initial => 16);
Link_Obj_Suffix : String_Access;
-- Read a list of files from file FILENAME.
-- Lines starting with a '#' are ignored (comments)
-- Lines starting with a '>' are directory lines
-- If first character of a line is a '@', it is replaced with
-- the lib_prefix_path.
-- If TO_OBJ is true, then each file is converted to an object file name
-- (suffix is replaced by the object file extension).
procedure Add_File_List (Filename : String; To_Obj : Boolean)
is
use Interfaces.C_Streams;
use System;
use Ada.Characters.Latin_1;
-- Replace the first '@' with the machine path.
function Substitute (Str : String) return String
is
begin
for I in Str'Range loop
if Str (I) = '@' then
return Str (Str'First .. I - 1)
& Get_Machine_Path_Prefix
& Str (I + 1 .. Str'Last);
end if;
end loop;
return Str;
end Substitute;
Dir : String (1 .. max_path_len);
Dir_Len : Natural;
Line : String (1 .. max_path_len);
Stream : Interfaces.C_Streams.FILEs;
Mode : constant String := "rt" & Ghdllocal.Nul;
L : Natural;
File : String_Access;
begin
Line (1 .. Filename'Length) := Filename;
Line (Filename'Length + 1) := Ghdllocal.Nul;
Stream := fopen (Line'Address, Mode'Address);
if Stream = NULL_Stream then
Error ("cannot open " & Filename);
raise Compile_Error;
end if;
Dir_Len := 0;
loop
exit when fgets (Line'Address, Line'Length, Stream) = NULL_Stream;
if Line (1) /= '#' then
-- Compute string length.
L := 0;
while Line (L + 1) /= Ghdllocal.Nul loop
L := L + 1;
end loop;
-- Remove trailing NL.
while L > 0 and then (Line (L) = LF or Line (L) = CR) loop
L := L - 1;
end loop;
if Line (1) = '>' then
Dir_Len := L - 1;
Dir (1 .. Dir_Len) := Line (2 .. L);
else
if To_Obj then
File := new String'(Dir (1 .. Dir_Len)
& Get_Base_Name (Line (1 .. L))
& Link_Obj_Suffix.all);
else
File := new String'(Substitute (Line (1 .. L)));
end if;
Filelist.Increment_Last;
Filelist.Table (Filelist.Last) := File;
Dir_Len := 0;
end if;
end if;
end loop;
if fclose (Stream) /= 0 then
Error ("cannot close " & Filename);
end if;
end Add_File_List;
function Get_Object_Filename (File : Iir_Design_File) return String
is
Dir : Name_Id;
Name : Name_Id;
begin
Dir := Get_Library_Directory (Get_Library (File));
Name := Get_Design_File_Filename (File);
return Image (Dir) & Get_Base_Name (Image (Name))
& Get_Object_Suffix.all;
end Get_Object_Filename;
procedure Add_Argument (Inst : in out Instance; Arg : String_Access)
is
begin
Increment_Last (Inst);
Inst.Table (Last (Inst)) := Arg;
end Add_Argument;
-- Convert option "-Wx,OPTIONS" to arguments for tool X.
procedure Add_Arguments (Inst : in out Instance; Opt : String) is
begin
Add_Argument (Inst, new String'(Opt (Opt'First + 4 .. Opt'Last)));
end Add_Arguments;
procedure Tool_Not_Found (Name : String) is
begin
Error ("installation problem: " & Name & " not found");
raise Option_Error;
end Tool_Not_Found;
-- Set the compiler command according to the configuration (and switches).
procedure Set_Tools_Name is
begin
-- Set tools name.
if Compiler_Cmd = null then
if Flag_Postprocess then
Compiler_Cmd := new String'(Default_Paths.Compiler_Debug);
else
case Backend is
when Backend_Gcc =>
Compiler_Cmd := new String'(Default_Paths.Compiler_Gcc);
when Backend_Mcode =>
Compiler_Cmd := new String'(Default_Paths.Compiler_Mcode);
when Backend_Llvm =>
Compiler_Cmd := new String'(Default_Paths.Compiler_Llvm);
end case;
end if;
end if;
if Post_Processor_Cmd = null then
Post_Processor_Cmd := new String'(Default_Paths.Post_Processor);
end if;
if Assembler_Cmd = null then
Assembler_Cmd := new String'("as");
end if;
if Linker_Cmd = null then
Linker_Cmd := new String'("gcc");
end if;
end Set_Tools_Name;
function Locate_Exec_Tool (Toolname : String) return String_Access is
begin
if Is_Absolute_Path (Toolname) then
if Is_Executable_File (Toolname) then
return new String'(Toolname);
end if;
else
-- Try from install prefix. This is used at least with gcc when
-- ghdl1 is installed in a libexec subdirectory, and also during
-- development.
if Exec_Prefix /= null then
declare
Path : constant String :=
Exec_Prefix.all & Directory_Separator & Toolname;
begin
if Is_Executable_File (Path) then
return new String'(Path);
end if;
end;
end if;
-- Try from install prefix / bin. This is used at least for
-- ghdl1-llvm.
if Exec_Prefix /= null then
declare
Path : constant String :=
Exec_Prefix.all & Directory_Separator
& "bin" & Directory_Separator & Toolname;
begin
if Is_Executable_File (Path) then
return new String'(Path);
end if;
end;
end if;
-- Try configured prefix.
declare
Path : constant String :=
Default_Paths.Install_Prefix & Directory_Separator & Toolname;
begin
if Is_Executable_File (Path) then
return new String'(Path);
end if;
end;
end if;
-- Search the basename on path.
declare
Pos : constant Natural := Get_Basename_Pos (Toolname);
begin
return Locate_Exec_On_Path (Toolname (Pos + 1 .. Toolname'Last));
end;
end Locate_Exec_Tool;
procedure Locate_Tools is
begin
-- Compiler.
Compiler_Path := Locate_Exec_Tool (Compiler_Cmd.all);
if Compiler_Path = null then
Tool_Not_Found (Compiler_Cmd.all);
end if;
-- Postprocessor.
if Flag_Postprocess then
Post_Processor_Path := Locate_Exec_Tool (Post_Processor_Cmd.all);
if Post_Processor_Path = null then
Tool_Not_Found (Post_Processor_Cmd.all);
end if;
end if;
-- Assembler.
case Backend is
when Backend_Gcc =>
Assembler_Path := Locate_Exec_On_Path (Assembler_Cmd.all);
if Assembler_Path = null and not Flag_Asm then
Tool_Not_Found (Assembler_Cmd.all);
end if;
when Backend_Llvm
| Backend_Mcode =>
null;
end case;
-- Linker.
Linker_Path := Locate_Exec_On_Path (Linker_Cmd.all);
if Linker_Path = null then
Tool_Not_Found (Linker_Cmd.all);
end if;
end Locate_Tools;
procedure Setup_Compiler (Load : Boolean)
is
use Libraries;
begin
Set_Tools_Name;
Setup_Libraries (Load);
Locate_Tools;
for I in 2 .. Get_Nbr_Paths loop
Add_Argument (Compiler_Args,
new String'("-P" & Image (Get_Path (I))));
end loop;
end Setup_Compiler;
type Command_Comp is abstract new Command_Lib with null record;
-- Setup GHDL.
procedure Init (Cmd : in out Command_Comp);
-- Handle:
-- all ghdl flags.
-- some GCC flags.
procedure Decode_Option (Cmd : in out Command_Comp;
Option : String;
Arg : String;
Res : out Option_Res);
procedure Disp_Long_Help (Cmd : Command_Comp);
procedure Init (Cmd : in out Command_Comp)
is
begin
-- Init options.
Flag_Not_Quiet := False;
Flag_Disp_Commands := False;
Flag_Asm := False;
Flag_Expect_Failure := False;
Output_File := null;
-- Initialize argument tables.
Init (Compiler_Args);
Init (Postproc_Args);
Init (Assembler_Args);
Init (Linker_Args);
Init (Command_Lib (Cmd));
end Init;
procedure Decode_Option (Cmd : in out Command_Comp;
Option : String;
Arg : String;
Res : out Option_Res)
is
Opt : constant String (1 .. Option'Length) := Option;
Str : String_Access;
begin
Res := Option_Bad;
if Opt = "-v" and then Flag_Verbose = False then
-- Note: this is also decoded for command_lib, but we set
-- Flag_Disp_Commands too.
Flag_Verbose := True;
--Flags.Verbose := True;
Flag_Disp_Commands := True;
Res := Option_Ok;
elsif Opt'Length > 8 and then Opt (1 .. 8) = "--GHDL1=" then
Compiler_Cmd := new String'(Opt (9 .. Opt'Last));
Res := Option_Ok;
elsif Opt'Length > 5 and then Opt (1 .. 5) = "--AS=" then
Assembler_Cmd := new String'(Opt (6 .. Opt'Last));
Res := Option_Ok;
elsif Opt'Length > 7 and then Opt (1 .. 7) = "--LINK=" then
Linker_Cmd := new String'(Opt (8 .. Opt'Last));
Res := Option_Ok;
elsif Opt = "-S" then
Flag_Asm := True;
Res := Option_Ok;
elsif Opt = "--post" then
Flag_Postprocess := True;
Res := Option_Ok;
elsif Opt = "-o" then
if Arg'Length = 0 then
Res := Option_Arg_Req;
else
Output_File := new String'(Arg);
Res := Option_Arg;
end if;
elsif Opt = "-m32" then
Add_Argument (Compiler_Args, new String'("-m32"));
Add_Argument (Assembler_Args, new String'("--32"));
Add_Argument (Linker_Args, new String'("-m32"));
Decode_Option (Command_Lib (Cmd), Opt, Arg, Res);
elsif Opt'Length > 4
and then Opt (2) = 'W' and then Opt (4) = ','
then
if Opt (3) = 'c' then
Add_Arguments (Compiler_Args, Opt);
elsif Opt (3) = 'a' then
Add_Arguments (Assembler_Args, Opt);
elsif Opt (3) = 'p' then
Add_Arguments (Postproc_Args, Opt);
elsif Opt (3) = 'l' then
Add_Arguments (Linker_Args, Opt);
else
Error ("unknown tool name in '-W" & Opt (3) & ",' option");
raise Option_Error;
end if;
Res := Option_Ok;
elsif Opt'Length >= 2 and then Opt (2) = 'g' then
-- Debugging option.
Str := new String'(Opt);
Add_Argument (Compiler_Args, Str);
Add_Argument (Linker_Args, Str);
Res := Option_Ok;
elsif Opt = "-Q" then
Flag_Not_Quiet := True;
Res := Option_Ok;
elsif Opt = "--expect-failure" then
Add_Argument (Compiler_Args, new String'(Opt));
Flag_Expect_Failure := True;
Res := Option_Ok;
elsif Opt = "-C" then
-- Translate -C into --mb-comments, as gcc already has a definition
-- for -C. Done before Flags.Parse_Option.
Add_Argument (Compiler_Args, new String'("--mb-comments"));
Res := Option_Ok;
elsif Opt = "--pre-elab" then
Elab_Mode := Elab_Static;
Res := Option_Ok;
elsif Opt = "--dyn-elab" then
Elab_Mode := Elab_Dynamic;
Res := Option_Ok;
elsif Options.Parse_Option (Opt) then
if Opt'Length > 2 and then Opt (1 .. 2) = "-P" then
-- Discard -Pxxx switches, as they are already added to
-- compiler_args.
null;
else
if Backend = Backend_Gcc then
-- Prefix options for gcc so that lang.opt does need to be
-- updated when a new option is added.
Str := new String'("--ghdl" & Opt);
else
Str := new String'(Opt);
end if;
Add_Argument (Compiler_Args, Str);
end if;
Res := Option_Ok;
elsif Opt'Length > 18
and then Opt (1 .. 18) = "--time-resolution="
then
Error ("option --time-resolution not supported by back-end");
raise Option_Error;
elsif Opt'Length >= 2
and then (Opt (2) = 'O' or Opt (2) = 'f')
then
-- Optimization option.
-- This is put after Flags.Parse_Option, since it may catch -fxxx
-- options.
Add_Argument (Compiler_Args, new String'(Opt));
Res := Option_Ok;
else
Decode_Option (Command_Lib (Cmd), Opt, Arg, Res);
end if;
end Decode_Option;
procedure Disp_Long_Help (Cmd : Command_Comp) is
begin
Disp_Long_Help (Command_Lib (Cmd));
Put_Line (" -v Be verbose");
Put_Line (" --GHDL1=PATH Set the path of the ghdl1 compiler");
Put_Line (" --AS=as Use as for the assembler");
Put_Line (" --LINK=gcc Use gcc for the linker driver");
Put_Line (" -S Do not assemble");
Put_Line (" -o FILE Set the name of the output file");
-- Put_Line (" -m32 Generate 32bit code on 64bit machines");
Put_Line (" -WX,OPTION Pass OPTION to X, where X is one of");
Put_Line (" c: compiler, a: assembler, l: linker");
Put_Line (" -g[XX] Pass debugging option to the compiler");
Put_Line (" -O[XX]/-f[XX] Pass optimization option to the compiler");
Put_Line (" -Q Do not add -quiet option to compiler");
Put_Line (" --expect-failure Expect analysis/elaboration failure");
end Disp_Long_Help;
-- Command dispconfig.
type Command_Dispconfig is new Command_Comp with null record;
function Decode_Command (Cmd : Command_Dispconfig; Name : String)
return Boolean;
function Get_Short_Help (Cmd : Command_Dispconfig) return String;
procedure Perform_Action (Cmd : Command_Dispconfig;
Args : Argument_List);
function Decode_Command (Cmd : Command_Dispconfig; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "--dispconfig" or else Name = "--disp-config";
end Decode_Command;
function Get_Short_Help (Cmd : Command_Dispconfig) return String
is
pragma Unreferenced (Cmd);
begin
return "--disp-config Disp tools path";
end Get_Short_Help;
procedure Perform_Action (Cmd : Command_Dispconfig;
Args : Argument_List)
is
use Libraries;
pragma Unreferenced (Cmd);
begin
if Args'Length /= 0 then
Error ("--disp-config does not accept any argument");
raise Option_Error;
end if;
Set_Tools_Name;
Put_Line ("Paths at configuration:");
Put ("compiler command: ");
Put_Line (Compiler_Cmd.all);
if Flag_Postprocess then
Put ("post-processor command: ");
Put_Line (Post_Processor_Cmd.all);
end if;
case Backend is
when Backend_Gcc =>
Put ("assembler command: ");
Put_Line (Assembler_Cmd.all);
when Backend_Llvm
| Backend_Mcode =>
null;
end case;
Put ("linker command: ");
Put_Line (Linker_Cmd.all);
Put_Line ("default lib prefix: " & Default_Paths.Lib_Prefix);
New_Line;
Disp_Config_Prefixes;
Locate_Tools;
Put ("compiler path: ");
Put_Line (Compiler_Path.all);
if Flag_Postprocess then
Put ("post-processor path: ");
Put_Line (Post_Processor_Path.all);
end if;
case Backend is
when Backend_Gcc =>
Put ("assembler path: ");
Put_Line (Assembler_Path.all);
when Backend_Llvm
| Backend_Mcode =>
null;
end case;
Put ("linker path: ");
Put_Line (Linker_Path.all);
New_Line;
Put_Line ("default library paths:");
for I in 2 .. Get_Nbr_Paths loop
Put (' ');
Put_Line (Image (Get_Path (I)));
end loop;
end Perform_Action;
-- Command Bootstrap-standard
type Command_Bootstrap is new Command_Comp with null record;
function Decode_Command (Cmd : Command_Bootstrap; Name : String)
return Boolean;
function Get_Short_Help (Cmd : Command_Bootstrap) return String;
procedure Perform_Action (Cmd : Command_Bootstrap;
Args : Argument_List);
function Decode_Command (Cmd : Command_Bootstrap; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "--bootstrap-standard";
end Decode_Command;
function Get_Short_Help (Cmd : Command_Bootstrap) return String
is
pragma Unreferenced (Cmd);
begin
return "--bootstrap-standard (Internal) compile std.standard";
end Get_Short_Help;
procedure Perform_Action (Cmd : Command_Bootstrap;
Args : Argument_List)
is
pragma Unreferenced (Cmd);
Opt : Argument_List (1 .. 1);
begin
if Args'Length /= 0 then
Error ("no file allowed for --bootstrap-standard");
raise Option_Error;
end if;
Setup_Compiler (False);
Opt (1) := new String'("--compile-standard");
Do_Compile (Opt, "std_standard.vhdl", True);
end Perform_Action;
-- Command Analyze.
type Command_Analyze is new Command_Comp with null record;
function Decode_Command (Cmd : Command_Analyze; Name : String)
return Boolean;
function Get_Short_Help (Cmd : Command_Analyze) return String;
procedure Perform_Action (Cmd : Command_Analyze;
Args : Argument_List);
function Decode_Command (Cmd : Command_Analyze; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "-a";
end Decode_Command;
function Get_Short_Help (Cmd : Command_Analyze) return String
is
pragma Unreferenced (Cmd);
begin
return "-a [OPTS] FILEs Analyze FILEs";
end Get_Short_Help;
procedure Perform_Action (Cmd : Command_Analyze;
Args : Argument_List)
is
pragma Unreferenced (Cmd);
Nil_Opt : Argument_List (2 .. 1);
begin
if Args'Length = 0 then
Error ("no file to analyze");
raise Option_Error;
end if;
Setup_Compiler (False);
for I in Args'Range loop
Do_Compile (Nil_Opt, Args (I).all, True);
end loop;
end Perform_Action;
-- Elaboration.
Base_Name : String_Access;
Elab_Name : String_Access;
Filelist_Name : String_Access;
Unit_Name : String_Access;
procedure Set_Elab_Units (Cmd_Name : String;
Args : Argument_List;
Run_Arg : out Natural)
is
begin
Extract_Elab_Unit (Cmd_Name, Args, Run_Arg);
if Sec_Name = null then
Base_Name := Prim_Name;
Unit_Name := Prim_Name;
else
Base_Name := new String'(Prim_Name.all & '-' & Sec_Name.all);
Unit_Name := new String'(Prim_Name.all & '(' & Sec_Name.all & ')');
end if;
Filelist_Name := null;
-- Choose a default name for the executable.
if Output_File = null then
Output_File := new String'(Base_Name.all);
end if;
-- Set a name for the elaboration files. Use the basename of the
-- output file, so that parallel builds with different output files
-- are allowed.
declare
Dir_Pos : constant Natural := Get_Basename_Pos (Output_File.all);
begin
Elab_Name := new String'
(Output_File (Output_File'First .. Dir_Pos)
& Elab_Prefix & Output_File (Dir_Pos + 1 .. Output_File'Last));
end;
end Set_Elab_Units;
procedure Set_Elab_Units (Cmd_Name : String; Args : Argument_List)
is
Next_Arg : Natural;
begin
Set_Elab_Units (Cmd_Name, Args, Next_Arg);
if Next_Arg <= Args'Last then
Error ("too many unit names for command '" & Cmd_Name & "'");
raise Option_Error;
end if;
end Set_Elab_Units;
procedure Bind
is
Comp_List : Argument_List (1 .. 4);
Elab_Cmd : String_Access;
begin
Filelist_Name := new String'(Elab_Name.all & List_Suffix);
case Elab_Mode is
when Elab_Static =>
Elab_Cmd := new String'("--pre-elab");
when Elab_Dynamic =>
Elab_Cmd := new String'("--elab");
end case;
Comp_List (1) := Elab_Cmd;
Comp_List (2) := Unit_Name;
Comp_List (3) := new String'("-l");
Comp_List (4) := Filelist_Name;
Do_Compile (Comp_List, Elab_Name.all, False);
Free (Comp_List (3));
Free (Comp_List (1));
end Bind;
procedure Bind_Anaelab (Files : Argument_List)
is
Comp_List : Argument_List (1 .. Files'Length + 2);
Index : Natural;
begin
Comp_List (1) := new String'("--anaelab");
Comp_List (2) := Unit_Name;
Index := 3;
for I in Files'Range loop
Comp_List (Index) := new String'("--ghdl-source=" & Files (I).all);
Index := Index + 1;
end loop;
Do_Compile (Comp_List, Elab_Name.all, False);
Free (Comp_List (1));
for I in 3 .. Comp_List'Last loop
Free (Comp_List (I));
end loop;
end Bind_Anaelab;
procedure Link (Add_Std : Boolean; Disp_Only : Boolean)
is
Last_File : Natural;
begin
Link_Obj_Suffix := Get_Object_Suffix;
-- read files list
if Filelist_Name /= null then
Add_File_List (Filelist_Name.all, True);
end if;
Last_File := Filelist.Last;
Add_File_List (Get_Machine_Path_Prefix & Directory_Separator
& "grt" & List_Suffix, False);
-- call the linker
declare
P : Natural;
Nbr_Args : constant Natural := Last (Linker_Args) + Filelist.Last + 4;
Args : Argument_List (1 .. Nbr_Args);
Obj_File : String_Access;
Std_File : String_Access;
begin
Obj_File := Append_Suffix (Elab_Name.all, Link_Obj_Suffix.all, False);
P := 0;
Args (P + 1) := Dash_o;
Args (P + 2) := Output_File;
Args (P + 3) := Obj_File;
P := P + 3;
if Add_Std then
Std_File := new
String'(Get_Machine_Path_Prefix & Directory_Separator
& "std" & Directory_Separator
& Get_Version_Path & Directory_Separator
& "std_standard" & Link_Obj_Suffix.all);
P := P + 1;
Args (P) := Std_File;
else
Std_File := null;
end if;
-- Object files of the design.
for I in Filelist.First .. Last_File loop
P := P + 1;
Args (P) := Filelist.Table (I);
end loop;
-- User added options.
for I in First .. Last (Linker_Args) loop
P := P + 1;
Args (P) := Linker_Args.Table (I);
end loop;
-- GRT files (should be the last one, since it contains an
-- optional main).
for I in Last_File + 1 .. Filelist.Last loop
P := P + 1;
Args (P) := Filelist.Table (I);
end loop;
if Disp_Only then
for I in 3 .. P loop
Put_Line (Args (I).all);
end loop;
else
My_Spawn (Linker_Path.all, Args (1 .. P));
end if;
Free (Obj_File);
Free (Std_File);
end;
for I in Filelist.First .. Filelist.Last loop
Free (Filelist.Table (I));
end loop;
end Link;
-- Command Elab.
type Command_Elab is new Command_Comp with null record;
function Decode_Command (Cmd : Command_Elab; Name : String)
return Boolean;
function Get_Short_Help (Cmd : Command_Elab) return String;
procedure Perform_Action (Cmd : Command_Elab;
Args : Argument_List);
function Decode_Command (Cmd : Command_Elab; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "-e";
end Decode_Command;
function Get_Short_Help (Cmd : Command_Elab) return String
is
pragma Unreferenced (Cmd);
begin
return "-e [OPTS] UNIT [ARCH] Elaborate UNIT";
end Get_Short_Help;
procedure Perform_Action (Cmd : Command_Elab; Args : Argument_List)
is
pragma Unreferenced (Cmd);
Success : Boolean;
pragma Unreferenced (Success);
begin
Set_Elab_Units ("-e", Args);
Setup_Compiler (False);
Bind;
if not Flag_Expect_Failure then
Link (Add_Std => True, Disp_Only => False);
end if;
Delete_File (Filelist_Name.all, Success);
end Perform_Action;
-- Command Run.
type Command_Run is new Command_Comp with null record;
function Decode_Command (Cmd : Command_Run; Name : String)
return Boolean;
function Get_Short_Help (Cmd : Command_Run) return String;
procedure Perform_Action (Cmd : Command_Run;
Args : Argument_List);
function Decode_Command (Cmd : Command_Run; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "-r";
end Decode_Command;
function Get_Short_Help (Cmd : Command_Run) return String
is
pragma Unreferenced (Cmd);
begin
return "-r UNIT [ARCH] [OPTS] Run UNIT";
end Get_Short_Help;
procedure Run_Design (Exec : String_Access; Args : Argument_List)
is
Status : Integer;
begin
if Is_Absolute_Path (Exec.all) then
Status := My_Spawn_Status (Exec.all, Args);
else
Status := My_Spawn_Status
('.' & Directory_Separator & Exec.all, Args);
end if;
Set_Exit_Status (Exit_Status (Status));
end Run_Design;
procedure Perform_Action (Cmd : Command_Run; Args : Argument_List)
is
pragma Unreferenced (Cmd);
Suffix : constant String_Access := Get_Executable_Suffix;
Opt_Arg : Natural;
begin
Extract_Elab_Unit ("-r", Args, Opt_Arg);
if Sec_Name = null then
Base_Name := new String'
(Prim_Name.all & Suffix.all);
else
Base_Name := new String'
(Prim_Name.all & '-' & Sec_Name.all & Suffix.all);
end if;
if not Is_Regular_File (Base_Name.all & Nul) then
Error ("file '" & Base_Name.all & "' does not exist");
Error ("Please elaborate your design.");
raise Exec_Error;
end if;
Run_Design (Base_Name, Args (Opt_Arg .. Args'Last));
end Perform_Action;
-- Command Elab_Run.
type Command_Elab_Run is new Command_Comp with null record;
function Decode_Command (Cmd : Command_Elab_Run; Name : String)
return Boolean;
function Get_Short_Help (Cmd : Command_Elab_Run) return String;
procedure Perform_Action (Cmd : Command_Elab_Run;
Args : Argument_List);
function Decode_Command (Cmd : Command_Elab_Run; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "--elab-run";
end Decode_Command;
function Get_Short_Help (Cmd : Command_Elab_Run) return String
is
pragma Unreferenced (Cmd);
begin
return "--elab-run [OPTS] UNIT [ARCH] [OPTS] Elaborate and run UNIT";
end Get_Short_Help;
procedure Perform_Action (Cmd : Command_Elab_Run;
Args : Argument_List)
is
pragma Unreferenced (Cmd);
Success : Boolean;
Run_Arg : Natural;
begin
Set_Elab_Units ("--elab-run", Args, Run_Arg);
Setup_Compiler (False);
Bind;
if Flag_Expect_Failure then
Delete_File (Filelist_Name.all, Success);
else
Link (Add_Std => True, Disp_Only => False);
Delete_File (Filelist_Name.all, Success);
Run_Design (Output_File, Args (Run_Arg .. Args'Last));
end if;
end Perform_Action;
-- Command Bind.
type Command_Bind is new Command_Comp with null record;
function Decode_Command (Cmd : Command_Bind; Name : String)
return Boolean;
function Get_Short_Help (Cmd : Command_Bind) return String;
procedure Perform_Action (Cmd : Command_Bind;
Args : Argument_List);
function Decode_Command (Cmd : Command_Bind; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "--bind";
end Decode_Command;
function Get_Short_Help (Cmd : Command_Bind) return String
is
pragma Unreferenced (Cmd);
begin
return "--bind [OPTS] UNIT [ARCH] Bind UNIT";
end Get_Short_Help;
procedure Perform_Action (Cmd : Command_Bind; Args : Argument_List)
is
pragma Unreferenced (Cmd);
begin
Set_Elab_Units ("--bind", Args);
Setup_Compiler (False);
Bind;
end Perform_Action;
-- Command Link.
type Command_Link is new Command_Comp with null record;
function Decode_Command (Cmd : Command_Link; Name : String)
return Boolean;
function Get_Short_Help (Cmd : Command_Link) return String;
procedure Perform_Action (Cmd : Command_Link; Args : Argument_List);
function Decode_Command (Cmd : Command_Link; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "--link";
end Decode_Command;
function Get_Short_Help (Cmd : Command_Link) return String
is
pragma Unreferenced (Cmd);
begin
return "--link [OPTS] UNIT [ARCH] Link UNIT";
end Get_Short_Help;
procedure Perform_Action (Cmd : Command_Link; Args : Argument_List)
is
pragma Unreferenced (Cmd);
begin
Set_Elab_Units ("--link", Args);
Setup_Compiler (False);
Filelist_Name := new String'(Elab_Name.all & List_Suffix);
Link (Add_Std => True, Disp_Only => False);
end Perform_Action;
-- Command List_Link.
type Command_List_Link is new Command_Comp with null record;
function Decode_Command (Cmd : Command_List_Link; Name : String)
return Boolean;
function Get_Short_Help (Cmd : Command_List_Link) return String;
procedure Perform_Action (Cmd : Command_List_Link;
Args : Argument_List);
function Decode_Command (Cmd : Command_List_Link; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "--list-link";
end Decode_Command;
function Get_Short_Help (Cmd : Command_List_Link) return String
is
pragma Unreferenced (Cmd);
begin
return "--list-link [OPTS] UNIT [ARCH] List objects file to link UNIT";
end Get_Short_Help;
procedure Perform_Action (Cmd : Command_List_Link;
Args : Argument_List)
is
pragma Unreferenced (Cmd);
begin
Set_Elab_Units ("--list-link", Args);
Setup_Compiler (False);
Filelist_Name := new String'(Elab_Name.all & List_Suffix);
Link (Add_Std => True, Disp_Only => True);
end Perform_Action;
-- Command analyze and elaborate
type Command_Anaelab is new Command_Comp with null record;
function Decode_Command (Cmd : Command_Anaelab; Name : String)
return Boolean;
function Get_Short_Help (Cmd : Command_Anaelab) return String;
procedure Decode_Option (Cmd : in out Command_Anaelab;
Option : String;
Arg : String;
Res : out Option_Res);
procedure Perform_Action (Cmd : Command_Anaelab;
Args : Argument_List);
function Decode_Command (Cmd : Command_Anaelab; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "-c";
end Decode_Command;
function Get_Short_Help (Cmd : Command_Anaelab) return String
is
pragma Unreferenced (Cmd);
begin
return "-c [OPTS] FILEs -e UNIT [ARCH] "
& "Generate whole code to elab UNIT from FILEs";
end Get_Short_Help;
procedure Decode_Option (Cmd : in out Command_Anaelab;
Option : String;
Arg : String;
Res : out Option_Res)
is
begin
if Option = "-e" then
Res := Option_End;
return;
else
Decode_Option (Command_Comp (Cmd), Option, Arg, Res);
end if;
end Decode_Option;
procedure Perform_Action (Cmd : Command_Anaelab;
Args : Argument_List)
is
pragma Unreferenced (Cmd);
Elab_Index : Integer;
Error : Boolean;
begin
Elab_Index := -1;
for I in Args'Range loop
if Args (I).all = "-e" then
Elab_Index := I;
exit;
end if;
end loop;
if Elab_Index < 0 then
Analyze_Files (Args, True, Error);
if Error then
raise Errorout.Compilation_Error;
end if;
else
Set_Elab_Units ("-c", Args (Elab_Index + 1 .. Args'Last));
Setup_Compiler (False);
Bind_Anaelab (Args (Args'First .. Elab_Index - 1));
Link (Add_Std => False, Disp_Only => False);
end if;
end Perform_Action;
-- Command Make.
type Command_Make is new Command_Comp with record
-- Bind only; don't link
Flag_Bind_Only : Boolean;
-- Disp dependences during make.
Flag_Depend_Unit : Boolean;
-- Force recompilation of units in work library.
Flag_Force : Boolean;
end record;
function Decode_Command (Cmd : Command_Make; Name : String)
return Boolean;
procedure Init (Cmd : in out Command_Make);
procedure Decode_Option (Cmd : in out Command_Make;
Option : String;
Arg : String;
Res : out Option_Res);
function Get_Short_Help (Cmd : Command_Make) return String;
procedure Disp_Long_Help (Cmd : Command_Make);
procedure Perform_Action (Cmd : Command_Make;
Args : Argument_List);
function Decode_Command (Cmd : Command_Make; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "-m";
end Decode_Command;
function Get_Short_Help (Cmd : Command_Make) return String
is
pragma Unreferenced (Cmd);
begin
return "-m [OPTS] UNIT [ARCH] Make UNIT";
end Get_Short_Help;
procedure Disp_Long_Help (Cmd : Command_Make)
is
begin
Disp_Long_Help (Command_Comp (Cmd));
Put_Line (" -b Bind only; don't link");
Put_Line (" -f Force recompilation of work units");
Put_Line (" -Mu Disp unit dependences (human format)");
end Disp_Long_Help;
procedure Init (Cmd : in out Command_Make) is
begin
Init (Command_Comp (Cmd));
Cmd.Flag_Bind_Only := False;
Cmd.Flag_Depend_Unit := False;
Cmd.Flag_Force := False;
end Init;
procedure Decode_Option (Cmd : in out Command_Make;
Option : String;
Arg : String;
Res : out Option_Res) is
begin
if Option = "-b" then
Cmd.Flag_Bind_Only := True;
Res := Option_Ok;
elsif Option = "-Mu" then
Cmd.Flag_Depend_Unit := True;
Res := Option_Ok;
elsif Option = "-f" then
Cmd.Flag_Force := True;
Res := Option_Ok;
else
Decode_Option (Command_Comp (Cmd), Option, Arg, Res);
end if;
end Decode_Option;
Last_Stamp : OS_Time;
Last_Stamp_File : Iir;
function Missing_Object_File (Design_File : Iir_Design_File) return Boolean
is
Name : constant Name_Id := Get_Design_File_Filename (Design_File);
Obj_Pathname : constant String := Get_Object_Filename (Design_File);
Stamp : OS_Time;
begin
Stamp := File_Time_Stamp (Obj_Pathname);
-- If the object file does not exist, recompile the file.
if Stamp = Invalid_Time then
if Flag_Verbose then
Put_Line ("no object file for " & Image (Name));
end if;
return True;
end if;
-- Keep the time stamp of the most recently analyzed unit.
if Last_Stamp = Invalid_Time or else Stamp > Last_Stamp then
Last_Stamp := Stamp;
Last_Stamp_File := Design_File;
end if;
return False;
end Missing_Object_File;
procedure Perform_Action (Cmd : Command_Make; Args : Argument_List)
is
use Configuration;
File : Iir_Design_File;
Unit : Iir;
Lib : Iir_Library_Declaration;
In_Work : Boolean;
Files_List : Iir_List;
Files_It : List_Iterator;
-- Set when a design file has been compiled.
Has_Compiled : Boolean;
Need_Analyze : Boolean;
Need_Elaboration : Boolean;
Stamp : OS_Time;
File_Id : Name_Id;
Nil_Args : Argument_List (2 .. 1);
Success : Boolean;
begin
Set_Elab_Units ("-m", Args);
Setup_Compiler (True);
-- Create list of files.
Files_List := Build_Dependence (Prim_Name, Sec_Name);
if Errorout.Nbr_Errors /= 0 then
raise Errorout.Compilation_Error;
end if;
if Cmd.Flag_Depend_Unit then
Put_Line ("Units analysis order:");
for I in Design_Units.First .. Design_Units.Last loop
Unit := Design_Units.Table (I);
Put (" ");
Disp_Library_Unit (Get_Library_Unit (Unit));
New_Line;
-- Put (" file: ");
-- File := Get_Design_File (Unit);
-- Put_Line (Image (Get_Design_File_Filename (File)));
end loop;
end if;
if Cmd.Flag_Depend_Unit then
Put_Line ("File analysis order:");
Files_It := List_Iterate (Files_List);
while Is_Valid (Files_It) loop
Put (" ");
File := Get_Element (Files_It);
Put (Image (Get_Design_File_Filename (File)));
if Flag_Verbose then
Put_Line (":");
declare
Dep_List : constant Iir_List :=
Get_File_Dependence_List (File);
Dep_It : List_Iterator;
Dep_File : Iir;
begin
Dep_It := List_Iterate_Safe (Dep_List);
while Is_Valid (Dep_It) loop
Put (" ");
Dep_File := Get_Element (Dep_It);
Put_Line (Image (Get_Design_File_Filename (Dep_File)));
Next (Dep_It);
end loop;
end;
else
New_Line;
end if;
Next (Files_It);
end loop;
end if;
Has_Compiled := False;
Last_Stamp := Invalid_Time;
Files_It := List_Iterate (Files_List);
while Is_Valid (Files_It) loop
File := Get_Element (Files_It);
if File = Std_Package.Std_Standard_File then
Need_Analyze := False;
elsif Missing_Object_File (File)
or else Source_File_Modified (File)
or else Is_File_Outdated (File)
then
Need_Analyze := True;
else
Need_Analyze := False;
end if;
Lib := Get_Library (File);
In_Work := Lib = Libraries.Work_Library;
if Need_Analyze or else (Cmd.Flag_Force and In_Work) then
File_Id := Get_Design_File_Filename (File);
if not Flag_Verbose then
Put ("analyze ");
Put (Image (File_Id));
--Disp_Library_Unit (Get_Library_Unit (Unit));
New_Line;
end if;
if In_Work then
Do_Compile (Nil_Args, Image (File_Id), True);
else
declare
use Libraries;
Lib_Args : Argument_List (1 .. 2);
Prev_Workdir : Name_Id;
begin
Prev_Workdir := Work_Directory;
-- Must be set, since used to build the object filename.
Work_Directory := Get_Library_Directory (Lib);
-- Always overwrite --work and --workdir.
Lib_Args (1) := new String'
("--work=" & Image (Get_Identifier (Lib)));
if Work_Directory = Libraries.Local_Directory then
Lib_Args (2) := new String'("--workdir=.");
else
Lib_Args (2) := new String'
("--workdir=" & Image (Work_Directory));
end if;
Do_Compile (Lib_Args, Image (File_Id), True);
Work_Directory := Prev_Workdir;
Free (Lib_Args (1));
Free (Lib_Args (2));
end;
end if;
Has_Compiled := True;
-- Set the analysis time stamp since the file has just been
-- analyzed.
Set_Analysis_Time_Stamp (File, Files_Map.Get_Os_Time_Stamp);
end if;
Next (Files_It);
end loop;
Need_Elaboration := False;
-- Elaboration.
-- if libgrt is more recent than the executable (FIXME).
if Has_Compiled then
if Flag_Verbose then
Put_Line ("link due to a file compilation");
end if;
Need_Elaboration := True;
else
Stamp := File_Time_Stamp (Output_File.all);
if Stamp = Invalid_Time then
if Flag_Verbose then
Put_Line ("link due to no binary file");
end if;
Need_Elaboration := True;
else
if Last_Stamp > Stamp then
-- if a file is more recent than the executable.
if Flag_Verbose then
Put ("link due to outdated binary file: ");
Put (Image (Get_Design_File_Filename (Last_Stamp_File)));
New_Line;
end if;
Need_Elaboration := True;
end if;
end if;
end if;
if Need_Elaboration then
if not Flag_Verbose then
Put ("elaborate ");
Put (Prim_Name.all);
--Disp_Library_Unit (Get_Library_Unit (Unit));
New_Line;
end if;
Bind;
if not Cmd.Flag_Bind_Only then
Link (Add_Std => True, Disp_Only => False);
Delete_File (Filelist_Name.all, Success);
end if;
end if;
exception
when Errorout.Compilation_Error =>
if Flag_Expect_Failure then
return;
else
raise;
end if;
end Perform_Action;
-- helper for --gen-makefile and --gen-depends
procedure Gen_Makefile (Args : Argument_List; Only_Depends : Boolean);
-- Command Gen_Makefile.
type Command_Gen_Makefile is new Command_Comp with null record;
function Decode_Command (Cmd : Command_Gen_Makefile; Name : String)
return Boolean;
function Get_Short_Help (Cmd : Command_Gen_Makefile) return String;
procedure Perform_Action (Cmd : Command_Gen_Makefile;
Args : Argument_List);
function Decode_Command (Cmd : Command_Gen_Makefile; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "--gen-makefile";
end Decode_Command;
function Get_Short_Help (Cmd : Command_Gen_Makefile) return String
is
pragma Unreferenced (Cmd);
begin
return "--gen-makefile [OPTS] UNIT [ARCH] Generate a Makefile for UNIT";
end Get_Short_Help;
function Is_Makeable_File (File : Iir_Design_File) return Boolean is
begin
if File = Std_Package.Std_Standard_File then
return False;
end if;
return True;
end Is_Makeable_File;
procedure Perform_Action (Cmd : Command_Gen_Makefile;
Args : Argument_List)
is
pragma Unreferenced (Cmd);
begin
Gen_Makefile (Args, False);
end Perform_Action;
-- Command Gen_Depends.
type Command_Gen_Depends is new Command_Comp with null record;
function Decode_Command (Cmd : Command_Gen_Depends; Name : String)
return Boolean;
function Get_Short_Help (Cmd : Command_Gen_Depends) return String;
procedure Perform_Action (Cmd : Command_Gen_Depends;
Args : Argument_List);
function Decode_Command (Cmd : Command_Gen_Depends; Name : String)
return Boolean
is
pragma Unreferenced (Cmd);
begin
return Name = "--gen-depends";
end Decode_Command;
function Get_Short_Help (Cmd : Command_Gen_Depends) return String
is
pragma Unreferenced (Cmd);
begin
return "--gen-depends [OPTS] UNIT [ARCH]"
& " Generate dependencies of UNIT";
end Get_Short_Help;
procedure Perform_Action (Cmd : Command_Gen_Depends;
Args : Argument_List)
is
pragma Unreferenced (Cmd);
begin
Gen_Makefile (Args, True);
end Perform_Action;
-- generate a makefile on stdout
-- for --gen-depends (Only_Depends) rules and phony targets are omittted
procedure Gen_Makefile (Args : Argument_List; Only_Depends : Boolean)
is
HT : constant Character := Ada.Characters.Latin_1.HT;
Files_List : Iir_List;
Files_It : List_Iterator;
File : Iir_Design_File;
Lib : Iir_Library_Declaration;
Dir_Id : Name_Id;
Dep_List : Iir_List;
Dep_It : List_Iterator;
Dep_File : Iir;
begin
if Only_Depends then
Set_Elab_Units ("--gen-depends", Args);
else
Set_Elab_Units ("--gen-makefile", Args);
end if;
Setup_Libraries (True);
Files_List := Build_Dependence (Prim_Name, Sec_Name);
Put_Line ("# Makefile automatically generated by ghdl");
Put ("# Version: GHDL ");
Put (Version.Ghdl_Ver);
Put (' ');
Put (Version.Ghdl_Release);
Put (" - ");
if Version_String /= null then
Put (Version_String.all);
end if;
New_Line;
Put_Line ("# Command used to generate this makefile:");
Put ("# ");
Put (Command_Name);
for I in 1 .. Argument_Count loop
Put (' ');
Put (Argument (I));
end loop;
New_Line;
New_Line;
-- Omit variables.
if not Only_Depends then
Put ("GHDL=");
Put_Line (Command_Name);
-- Extract options for command line.
Put ("GHDLFLAGS=");
for I in 2 .. Argument_Count loop
declare
Arg : constant String := Argument (I);
begin
if Arg (1) = '-' then
if (Arg'Length > 10 and then Arg (1 .. 10) = "--workdir=")
or else (Arg'Length > 7 and then Arg (1 .. 7) = "--ieee=")
or else (Arg'Length > 6 and then Arg (1 .. 6) = "--std=")
or else (Arg'Length > 7 and then Arg (1 .. 7) = "--work=")
or else (Arg'Length > 2 and then Arg (1 .. 2) = "-P")
then
Put (" ");
Put (Arg);
end if;
end if;
end;
end loop;
New_Line;
New_Line;
Put_Line ("# Default target");
Put ("all: ");
Put_Line (Base_Name.all);
New_Line;
end if;
Put_Line ("# Elaboration target");
Put (Base_Name.all);
Put (":");
Files_It := List_Iterate (Files_List);
while Is_Valid (Files_It) loop
File := Get_Element (Files_It);
if Is_Makeable_File (File) then
Put (" ");
Put (Get_Object_Filename (File));
end if;
Next (Files_It);
end loop;
New_Line;
-- Omit rule.
if not Only_Depends then
Put_Line (HT & "$(GHDL) -e $(GHDLFLAGS) $@");
end if;
New_Line;
-- Omit phony target.
if not Only_Depends then
Put_Line ("# Run target");
Put_Line ("run: " & Base_Name.all);
Put_Line (HT & "$(GHDL) -r " & Base_Name.all & " $(GHDLRUNFLAGS)");
New_Line;
end if;
Put_Line ("# Targets to analyze files");
Files_It := List_Iterate (Files_List);
while Is_Valid (Files_It) loop
File := Get_Element (Files_It);
Dir_Id := Get_Design_File_Directory (File);
if not Is_Makeable_File (File) then
-- Builtin file.
null;
else
Put (Get_Object_Filename (File));
Put (": ");
if Dir_Id /= Files_Map.Get_Home_Directory then
Put (Image (Dir_Id));
Put (Image (Get_Design_File_Filename (File)));
New_Line;
-- Omit dummy rule.
if not Only_Depends then
Put_Line
(HT & "@echo ""This file was not locally built ($<)""");
Put_Line (HT & "exit 1");
end if;
else
Put (Image (Get_Design_File_Filename (File)));
New_Line;
-- Omit rule.
if not Only_Depends then
Put (HT & "$(GHDL) -a $(GHDLFLAGS)");
Lib := Get_Library (File);
if Lib /= Libraries.Work_Library then
-- Overwrite some options.
Put (" --work=");
Put (Image (Get_Identifier (Lib)));
Dir_Id := Get_Library_Directory (Lib);
Put (" --workdir=");
if Dir_Id = Libraries.Local_Directory then
Put (".");
else
Put (Image (Dir_Id));
end if;
end if;
Put_Line (" $<");
end if;
end if;
end if;
Next (Files_It);
end loop;
New_Line;
Put_Line ("# Files dependences");
Files_It := List_Iterate (Files_List);
while Is_Valid (Files_It) loop
File := Get_Element (Files_It);
if Is_Makeable_File (File) then
Put (Get_Object_Filename (File));
Put (": ");
Dep_List := Get_File_Dependence_List (File);
Dep_It := List_Iterate_Safe (Dep_List);
while Is_Valid (Dep_It) loop
Dep_File := Get_Element (Dep_It);
if Dep_File /= File and then Is_Makeable_File (Dep_File)
then
Put (" ");
Put (Get_Object_Filename (Dep_File));
end if;
Next (Dep_It);
end loop;
New_Line;
end if;
Next (Files_It);
end loop;
end Gen_Makefile;
procedure Register_Commands is
begin
Register_Command (new Command_Analyze);
Register_Command (new Command_Elab);
Register_Command (new Command_Run);
Register_Command (new Command_Elab_Run);
Register_Command (new Command_Bind);
Register_Command (new Command_Link);
Register_Command (new Command_List_Link);
Register_Command (new Command_Anaelab);
Register_Command (new Command_Make);
Register_Command (new Command_Gen_Makefile);
Register_Command (new Command_Gen_Depends);
Register_Command (new Command_Dispconfig);
Register_Command (new Command_Bootstrap);
end Register_Commands;
end Ghdldrv;
|