summaryrefslogtreecommitdiff
path: root/gettext-tools/gnulib-lib/javacomp.c
blob: a318c0dd93159ae2bb6bb53527a23848f4d8e796 (plain)
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
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
/* Compile a Java program.
   Copyright (C) 2001-2003, 2006-2016 Free Software Foundation, Inc.
   Written by Bruno Haible <haible@clisp.cons.org>, 2001.

   This program 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 3 of the License, or
   (at your option) any later version.

   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <config.h>
#include <alloca.h>

/* Specification.  */
#include "javacomp.h"

#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "javaversion.h"
#include "execute.h"
#include "spawn-pipe.h"
#include "wait-process.h"
#include "classpath.h"
#include "xsetenv.h"
#include "sh-quote.h"
#include "binary-io.h"
#include "safe-read.h"
#include "xalloc.h"
#include "xmalloca.h"
#include "concat-filename.h"
#include "fwriteerror.h"
#include "clean-temp.h"
#include "error.h"
#include "xvasprintf.h"
#include "c-strstr.h"
#include "gettext.h"

#define _(str) gettext (str)


/* Survey of Java compilers.

   A = does it work without CLASSPATH being set
   C = option to set CLASSPATH, other than setting it in the environment
   O = option for optimizing
   g = option for debugging
   T = test for presence

   Program  from        A  C               O  g  T

   $JAVAC   unknown     N  n/a            -O -g  true
   gcj -C   GCC 3.2     Y  --classpath=P  -O -g  gcj --version | sed -e 's,^[^0-9]*,,' -e 1q | sed -e '/^3\.[01]/d' | grep '^[3-9]' >/dev/null
   javac    JDK 1.1.8   Y  -classpath P   -O -g  javac 2>/dev/null; test $? = 1
   javac    JDK 1.3.0   Y  -classpath P   -O -g  javac 2>/dev/null; test $? -le 2
   jikes    Jikes 1.14  N  -classpath P   -O -g  jikes 2>/dev/null; test $? = 1

   All compilers support the option "-d DIRECTORY" for the base directory
   of the classes to be written.

   The CLASSPATH is a colon separated list of pathnames. (On Windows: a
   semicolon separated list of pathnames.)

   We try the Java compilers in the following order:
     1. getenv ("JAVAC"), because the user must be able to override our
        preferences,
     2. "gcj -C", because it is a completely free compiler,
     3. "javac", because it is a standard compiler,
     4. "jikes", comes last because it has some deviating interpretation
        of the Java Language Specification and because it requires a
        CLASSPATH environment variable.

   We unset the JAVA_HOME environment variable, because a wrong setting of
   this variable can confuse the JDK's javac.
 */

/* Return the default target_version.  */
static const char *
default_target_version (void)
{
  /* Use a cache.  Assumes that the PATH environment variable doesn't change
     during the lifetime of the program.  */
  static const char *java_version_cache;
  if (java_version_cache == NULL)
    {
      /* Determine the version from the found JVM.  */
      java_version_cache = javaexec_version ();
      if (java_version_cache == NULL
          || !(java_version_cache[0] == '1' && java_version_cache[1] == '.'
               && (java_version_cache[2] >= '1' && java_version_cache[2] <= '6')
               && java_version_cache[3] == '\0'))
        java_version_cache = "1.1";
    }
  return java_version_cache;
}

/* ======================= Source version dependent ======================= */

/* Convert a source version to an index.  */
#define SOURCE_VERSION_BOUND 3 /* exclusive upper bound */
static unsigned int
source_version_index (const char *source_version)
{
  if (source_version[0] == '1' && source_version[1] == '.'
      && (source_version[2] >= '3' && source_version[2] <= '5')
      && source_version[3] == '\0')
    return source_version[2] - '3';
  error (EXIT_FAILURE, 0, _("invalid source_version argument to compile_java_class"));
  return 0;
}

/* Return a snippet of code that should compile in the given source version.  */
static const char *
get_goodcode_snippet (const char *source_version)
{
  if (strcmp (source_version, "1.3") == 0)
    return "class conftest {}\n";
  if (strcmp (source_version, "1.4") == 0)
    return "class conftest { static { assert(true); } }\n";
  if (strcmp (source_version, "1.5") == 0)
    return "class conftest<T> { T foo() { return null; } }\n";
  error (EXIT_FAILURE, 0, _("invalid source_version argument to compile_java_class"));
  return NULL;
}

/* Return a snippet of code that should fail to compile in the given source
   version, or NULL (standing for a snippet that would fail to compile with
   any compiler).  */
static const char *
get_failcode_snippet (const char *source_version)
{
  if (strcmp (source_version, "1.3") == 0)
    return "class conftestfail { static { assert(true); } }\n";
  if (strcmp (source_version, "1.4") == 0)
    return "class conftestfail<T> { T foo() { return null; } }\n";
  if (strcmp (source_version, "1.5") == 0)
    return NULL;
  error (EXIT_FAILURE, 0, _("invalid source_version argument to compile_java_class"));
  return NULL;
}

/* ======================= Target version dependent ======================= */

/* Convert a target version to an index.  */
#define TARGET_VERSION_BOUND 6 /* exclusive upper bound */
static unsigned int
target_version_index (const char *target_version)
{
  if (target_version[0] == '1' && target_version[1] == '.'
      && (target_version[2] >= '1' && target_version[2] <= '6')
      && target_version[3] == '\0')
    return target_version[2] - '1';
  error (EXIT_FAILURE, 0, _("invalid target_version argument to compile_java_class"));
  return 0;
}

/* Return the class file version number corresponding to a given target
   version.  */
static int
corresponding_classfile_version (const char *target_version)
{
  if (strcmp (target_version, "1.1") == 0)
    return 45;
  if (strcmp (target_version, "1.2") == 0)
    return 46;
  if (strcmp (target_version, "1.3") == 0)
    return 47;
  if (strcmp (target_version, "1.4") == 0)
    return 48;
  if (strcmp (target_version, "1.5") == 0)
    return 49;
  if (strcmp (target_version, "1.6") == 0)
    return 50;
  error (EXIT_FAILURE, 0, _("invalid target_version argument to compile_java_class"));
  return 0;
}

/* ======================== Compilation subroutines ======================== */

/* Try to compile a set of Java sources with $JAVAC.
   Return a failure indicator (true upon error).  */
static bool
compile_using_envjavac (const char *javac,
                        const char * const *java_sources,
                        unsigned int java_sources_count,
                        const char *directory,
                        bool optimize, bool debug,
                        bool verbose, bool null_stderr)
{
  /* Because $JAVAC may consist of a command and options, we use the
     shell.  Because $JAVAC has been set by the user, we leave all
     environment variables in place, including JAVA_HOME, and we don't
     erase the user's CLASSPATH.  */
  bool err;
  unsigned int command_length;
  char *command;
  char *argv[4];
  int exitstatus;
  unsigned int i;
  char *p;

  command_length = strlen (javac);
  if (optimize)
    command_length += 3;
  if (debug)
    command_length += 3;
  if (directory != NULL)
    command_length += 4 + shell_quote_length (directory);
  for (i = 0; i < java_sources_count; i++)
    command_length += 1 + shell_quote_length (java_sources[i]);
  command_length += 1;

  command = (char *) xmalloca (command_length);
  p = command;
  /* Don't shell_quote $JAVAC, because it may consist of a command
     and options.  */
  memcpy (p, javac, strlen (javac));
  p += strlen (javac);
  if (optimize)
    {
      memcpy (p, " -O", 3);
      p += 3;
    }
  if (debug)
    {
      memcpy (p, " -g", 3);
      p += 3;
    }
  if (directory != NULL)
    {
      memcpy (p, " -d ", 4);
      p += 4;
      p = shell_quote_copy (p, directory);
    }
  for (i = 0; i < java_sources_count; i++)
    {
      *p++ = ' ';
      p = shell_quote_copy (p, java_sources[i]);
    }
  *p++ = '\0';
  /* Ensure command_length was correctly calculated.  */
  if (p - command > command_length)
    abort ();

  if (verbose)
    printf ("%s\n", command);

  argv[0] = "/bin/sh";
  argv[1] = "-c";
  argv[2] = command;
  argv[3] = NULL;
  exitstatus = execute (javac, "/bin/sh", argv, false, false, false,
                        null_stderr, true, true, NULL);
  err = (exitstatus != 0);

  freea (command);

  return err;
}

/* Try to compile a set of Java sources with gcj.
   Return a failure indicator (true upon error).  */
static bool
compile_using_gcj (const char * const *java_sources,
                   unsigned int java_sources_count,
                   bool no_assert_option,
                   bool fsource_option, const char *source_version,
                   bool ftarget_option, const char *target_version,
                   const char *directory,
                   bool optimize, bool debug,
                   bool verbose, bool null_stderr)
{
  bool err;
  unsigned int argc;
  char **argv;
  char **argp;
  char *fsource_arg;
  char *ftarget_arg;
  int exitstatus;
  unsigned int i;

  argc =
    2 + (no_assert_option ? 1 : 0) + (fsource_option ? 1 : 0)
    + (ftarget_option ? 1 : 0) + (optimize ? 1 : 0) + (debug ? 1 : 0)
    + (directory != NULL ? 2 : 0) + java_sources_count;
  argv = (char **) xmalloca ((argc + 1) * sizeof (char *));

  argp = argv;
  *argp++ = "gcj";
  *argp++ = "-C";
  if (no_assert_option)
    *argp++ = "-fno-assert";
  if (fsource_option)
    {
      fsource_arg = (char *) xmalloca (9 + strlen (source_version) + 1);
      memcpy (fsource_arg, "-fsource=", 9);
      strcpy (fsource_arg + 9, source_version);
      *argp++ = fsource_arg;
    }
  else
    fsource_arg = NULL;
  if (ftarget_option)
    {
      ftarget_arg = (char *) xmalloca (9 + strlen (target_version) + 1);
      memcpy (ftarget_arg, "-ftarget=", 9);
      strcpy (ftarget_arg + 9, target_version);
      *argp++ = ftarget_arg;
    }
  else
    ftarget_arg = NULL;
  if (optimize)
    *argp++ = "-O";
  if (debug)
    *argp++ = "-g";
  if (directory != NULL)
    {
      *argp++ = "-d";
      *argp++ = (char *) directory;
    }
  for (i = 0; i < java_sources_count; i++)
    *argp++ = (char *) java_sources[i];
  *argp = NULL;
  /* Ensure argv length was correctly calculated.  */
  if (argp - argv != argc)
    abort ();

  if (verbose)
    {
      char *command = shell_quote_argv (argv);
      printf ("%s\n", command);
      free (command);
    }

  exitstatus = execute ("gcj", "gcj", argv, false, false, false, null_stderr,
                        true, true, NULL);
  err = (exitstatus != 0);

  if (ftarget_arg != NULL)
    freea (ftarget_arg);
  if (fsource_arg != NULL)
    freea (fsource_arg);
  freea (argv);

  return err;
}

/* Try to compile a set of Java sources with javac.
   Return a failure indicator (true upon error).  */
static bool
compile_using_javac (const char * const *java_sources,
                     unsigned int java_sources_count,
                     bool source_option, const char *source_version,
                     bool target_option, const char *target_version,
                     const char *directory,
                     bool optimize, bool debug,
                     bool verbose, bool null_stderr)
{
  bool err;
  unsigned int argc;
  char **argv;
  char **argp;
  int exitstatus;
  unsigned int i;

  argc =
    1 + (source_option ? 2 : 0) + (target_option ? 2 : 0) + (optimize ? 1 : 0)
    + (debug ? 1 : 0) + (directory != NULL ? 2 : 0) + java_sources_count;
  argv = (char **) xmalloca ((argc + 1) * sizeof (char *));

  argp = argv;
  *argp++ = "javac";
  if (source_option)
    {
      *argp++ = "-source";
      *argp++ = (char *) source_version;
    }
  if (target_option)
    {
      *argp++ = "-target";
      *argp++ = (char *) target_version;
    }
  if (optimize)
    *argp++ = "-O";
  if (debug)
    *argp++ = "-g";
  if (directory != NULL)
    {
      *argp++ = "-d";
      *argp++ = (char *) directory;
    }
  for (i = 0; i < java_sources_count; i++)
    *argp++ = (char *) java_sources[i];
  *argp = NULL;
  /* Ensure argv length was correctly calculated.  */
  if (argp - argv != argc)
    abort ();

  if (verbose)
    {
      char *command = shell_quote_argv (argv);
      printf ("%s\n", command);
      free (command);
    }

  exitstatus = execute ("javac", "javac", argv, false, false, false,
                        null_stderr, true, true, NULL);
  err = (exitstatus != 0);

  freea (argv);

  return err;
}

/* Try to compile a set of Java sources with jikes.
   Return a failure indicator (true upon error).  */
static bool
compile_using_jikes (const char * const *java_sources,
                     unsigned int java_sources_count,
                     const char *directory,
                     bool optimize, bool debug,
                     bool verbose, bool null_stderr)
{
  bool err;
  unsigned int argc;
  char **argv;
  char **argp;
  int exitstatus;
  unsigned int i;

  argc =
    1 + (optimize ? 1 : 0) + (debug ? 1 : 0) + (directory != NULL ? 2 : 0)
    + java_sources_count;
  argv = (char **) xmalloca ((argc + 1) * sizeof (char *));

  argp = argv;
  *argp++ = "jikes";
  if (optimize)
    *argp++ = "-O";
  if (debug)
    *argp++ = "-g";
  if (directory != NULL)
    {
      *argp++ = "-d";
      *argp++ = (char *) directory;
    }
  for (i = 0; i < java_sources_count; i++)
    *argp++ = (char *) java_sources[i];
  *argp = NULL;
  /* Ensure argv length was correctly calculated.  */
  if (argp - argv != argc)
    abort ();

  if (verbose)
    {
      char *command = shell_quote_argv (argv);
      printf ("%s\n", command);
      free (command);
    }

  exitstatus = execute ("jikes", "jikes", argv, false, false, false,
                        null_stderr, true, true, NULL);
  err = (exitstatus != 0);

  freea (argv);

  return err;
}

/* ====================== Usability test subroutines ====================== */

/* Write a given contents to a temporary file.
   FILE_NAME is the name of a file inside TMPDIR that is known not to exist
   yet.
   Return a failure indicator (true upon error).  */
static bool
write_temp_file (struct temp_dir *tmpdir, const char *file_name,
                 const char *contents)
{
  FILE *fp;

  register_temp_file (tmpdir, file_name);
  fp = fopen_temp (file_name, "w");
  if (fp == NULL)
    {
      error (0, errno, _("failed to create \"%s\""), file_name);
      unregister_temp_file (tmpdir, file_name);
      return true;
    }
  fputs (contents, fp);
  if (fwriteerror_temp (fp))
    {
      error (0, errno, _("error while writing \"%s\" file"), file_name);
      return true;
    }
  return false;
}

/* Return the class file version number of a class file on disk.  */
static int
get_classfile_version (const char *compiled_file_name)
{
  unsigned char header[8];
  int fd;

  /* Open the class file.  */
  fd = open (compiled_file_name, O_RDONLY | O_BINARY, 0);
  if (fd >= 0)
    {
      /* Read its first 8 bytes.  */
      if (safe_read (fd, header, 8) == 8)
        {
          /* Verify the class file signature.  */
          if (header[0] == 0xCA && header[1] == 0xFE
              && header[2] == 0xBA && header[3] == 0xBE)
            return header[7];
        }
      close (fd);
    }

  /* Could not get the class file version.  Return a very large one.  */
  return INT_MAX;
}

/* Return true if $JAVAC is a version of gcj.  */
static bool
is_envjavac_gcj (const char *javac)
{
  static bool envjavac_tested;
  static bool envjavac_gcj;

  if (!envjavac_tested)
    {
      /* Test whether $JAVAC is gcj:
         "$JAVAC --version 2>/dev/null | sed -e 1q | grep gcj > /dev/null"  */
      unsigned int command_length;
      char *command;
      char *argv[4];
      pid_t child;
      int fd[1];
      FILE *fp;
      char *line;
      size_t linesize;
      size_t linelen;
      int exitstatus;
      char *p;

      /* Setup the command "$JAVAC --version".  */
      command_length = strlen (javac) + 1 + 9 + 1;
      command = (char *) xmalloca (command_length);
      p = command;
      /* Don't shell_quote $JAVAC, because it may consist of a command
         and options.  */
      memcpy (p, javac, strlen (javac));
      p += strlen (javac);
      memcpy (p, " --version", 1 + 9 + 1);
      p += 1 + 9 + 1;
      /* Ensure command_length was correctly calculated.  */
      if (p - command > command_length)
        abort ();

      /* Call $JAVAC --version 2>/dev/null.  */
      argv[0] = "/bin/sh";
      argv[1] = "-c";
      argv[2] = command;
      argv[3] = NULL;
      child = create_pipe_in (javac, "/bin/sh", argv, DEV_NULL, true, true,
                              false, fd);
      if (child == -1)
        goto failed;

      /* Retrieve its result.  */
      fp = fdopen (fd[0], "r");
      if (fp == NULL)
        goto failed;

      line = NULL; linesize = 0;
      linelen = getline (&line, &linesize, fp);
      if (linelen == (size_t)(-1))
        {
          fclose (fp);
          goto failed;
        }
      /* It is safe to call c_strstr() instead of strstr() here; see the
         comments in c-strstr.h.  */
      envjavac_gcj = (c_strstr (line, "gcj") != NULL);

      fclose (fp);

      /* Remove zombie process from process list, and retrieve exit status.  */
      exitstatus =
        wait_subprocess (child, javac, true, true, true, false, NULL);
      if (exitstatus != 0)
        envjavac_gcj = false;

     failed:
      freea (command);

      envjavac_tested = true;
    }

  return envjavac_gcj;
}

/* Return true if $JAVAC, known to be a version of gcj, is a version >= 4.3
   of gcj.  */
static bool
is_envjavac_gcj43 (const char *javac)
{
  static bool envjavac_tested;
  static bool envjavac_gcj43;

  if (!envjavac_tested)
    {
      /* Test whether $JAVAC is gcj:
         "$JAVAC --version 2>/dev/null | sed -e 's,^[^0-9]*,,' -e 1q \
          | sed -e '/^4\.[012]/d' | grep '^[4-9]' >/dev/null"  */
      unsigned int command_length;
      char *command;
      char *argv[4];
      pid_t child;
      int fd[1];
      FILE *fp;
      char *line;
      size_t linesize;
      size_t linelen;
      int exitstatus;
      char *p;

      /* Setup the command "$JAVAC --version".  */
      command_length = strlen (javac) + 1 + 9 + 1;
      command = (char *) xmalloca (command_length);
      p = command;
      /* Don't shell_quote $JAVAC, because it may consist of a command
         and options.  */
      memcpy (p, javac, strlen (javac));
      p += strlen (javac);
      memcpy (p, " --version", 1 + 9 + 1);
      p += 1 + 9 + 1;
      /* Ensure command_length was correctly calculated.  */
      if (p - command > command_length)
        abort ();

      /* Call $JAVAC --version 2>/dev/null.  */
      argv[0] = "/bin/sh";
      argv[1] = "-c";
      argv[2] = command;
      argv[3] = NULL;
      child = create_pipe_in (javac, "/bin/sh", argv, DEV_NULL, true, true,
                              false, fd);
      if (child == -1)
        goto failed;

      /* Retrieve its result.  */
      fp = fdopen (fd[0], "r");
      if (fp == NULL)
        goto failed;

      line = NULL; linesize = 0;
      linelen = getline (&line, &linesize, fp);
      if (linelen == (size_t)(-1))
        {
          fclose (fp);
          goto failed;
        }
      p = line;
      while (*p != '\0' && !(*p >= '0' && *p <= '9'))
        p++;
      envjavac_gcj43 =
        !(*p == '4' && p[1] == '.' && p[2] >= '0' && p[2] <= '2')
        && (*p >= '4' && *p <= '9');

      fclose (fp);

      /* Remove zombie process from process list, and retrieve exit status.  */
      exitstatus =
        wait_subprocess (child, javac, true, true, true, false, NULL);
      if (exitstatus != 0)
        envjavac_gcj43 = false;

     failed:
      freea (command);

      envjavac_tested = true;
    }

  return envjavac_gcj43;
}

/* Test whether $JAVAC, known to be a version of gcj >= 4.3, can be used, and
   whether it needs a -fsource and/or -ftarget option.
   Return a failure indicator (true upon error).  */
static bool
is_envjavac_gcj43_usable (const char *javac,
                          const char *source_version,
                          const char *target_version,
                          bool *usablep,
                          bool *fsource_option_p, bool *ftarget_option_p)
{
  /* The cache depends on the source_version and target_version.  */
  struct result_t
  {
    bool tested;
    bool usable;
    bool fsource_option;
    bool ftarget_option;
  };
  static struct result_t result_cache[SOURCE_VERSION_BOUND][TARGET_VERSION_BOUND];
  struct result_t *resultp;

  resultp = &result_cache[source_version_index (source_version)]
                         [target_version_index (target_version)];
  if (!resultp->tested)
    {
      /* Try $JAVAC.  */
      struct temp_dir *tmpdir;
      char *conftest_file_name;
      char *compiled_file_name;
      const char *java_sources[1];
      struct stat statbuf;

      tmpdir = create_temp_dir ("java", NULL, false);
      if (tmpdir == NULL)
        return true;

      conftest_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
      if (write_temp_file (tmpdir, conftest_file_name,
                           get_goodcode_snippet (source_version)))
        {
          free (conftest_file_name);
          cleanup_temp_dir (tmpdir);
          return true;
        }

      compiled_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
      register_temp_file (tmpdir, compiled_file_name);

      java_sources[0] = conftest_file_name;
      if (!compile_using_envjavac (javac,
                                   java_sources, 1, tmpdir->dir_name,
                                   false, false, false, true)
          && stat (compiled_file_name, &statbuf) >= 0
          && get_classfile_version (compiled_file_name)
             <= corresponding_classfile_version (target_version))
        {
          /* $JAVAC compiled conftest.java successfully.  */
          /* Try adding -fsource option if it is useful.  */
          char *javac_source =
            xasprintf ("%s -fsource=%s", javac, source_version);

          unlink (compiled_file_name);

          java_sources[0] = conftest_file_name;
          if (!compile_using_envjavac (javac_source,
                                       java_sources, 1, tmpdir->dir_name,
                                       false, false, false, true)
              && stat (compiled_file_name, &statbuf) >= 0
              && get_classfile_version (compiled_file_name)
                 <= corresponding_classfile_version (target_version))
            {
              const char *failcode = get_failcode_snippet (source_version);

              if (failcode != NULL)
                {
                  free (compiled_file_name);
                  free (conftest_file_name);

                  conftest_file_name =
                    xconcatenated_filename (tmpdir->dir_name,
                                            "conftestfail.java",
                                            NULL);
                  if (write_temp_file (tmpdir, conftest_file_name, failcode))
                    {
                      free (conftest_file_name);
                      free (javac_source);
                      cleanup_temp_dir (tmpdir);
                      return true;
                    }

                  compiled_file_name =
                    xconcatenated_filename (tmpdir->dir_name,
                                            "conftestfail.class",
                                            NULL);
                  register_temp_file (tmpdir, compiled_file_name);

                  java_sources[0] = conftest_file_name;
                  if (!compile_using_envjavac (javac,
                                               java_sources, 1,
                                               tmpdir->dir_name,
                                               false, false, false, true)
                      && stat (compiled_file_name, &statbuf) >= 0)
                    {
                      unlink (compiled_file_name);

                      java_sources[0] = conftest_file_name;
                      if (compile_using_envjavac (javac_source,
                                                  java_sources, 1,
                                                  tmpdir->dir_name,
                                                  false, false, false, true))
                        /* $JAVAC compiled conftestfail.java successfully, and
                           "$JAVAC -fsource=$source_version" rejects it.  So
                           the -fsource option is useful.  */
                        resultp->fsource_option = true;
                    }
                }
            }

          free (javac_source);

          resultp->usable = true;
        }
      else
        {
          /* Try with -fsource and -ftarget options.  */
          char *javac_target =
            xasprintf ("%s -fsource=%s -ftarget=%s",
                       javac, source_version, target_version);

          unlink (compiled_file_name);

          java_sources[0] = conftest_file_name;
          if (!compile_using_envjavac (javac_target,
                                       java_sources, 1, tmpdir->dir_name,
                                       false, false, false, true)
              && stat (compiled_file_name, &statbuf) >= 0
              && get_classfile_version (compiled_file_name)
                 <= corresponding_classfile_version (target_version))
            {
              /* "$JAVAC -fsource $source_version -ftarget $target_version"
                 compiled conftest.java successfully.  */
              resultp->fsource_option = true;
              resultp->ftarget_option = true;
              resultp->usable = true;
            }

          free (javac_target);
        }

      free (compiled_file_name);
      free (conftest_file_name);

      resultp->tested = true;
    }

  *usablep = resultp->usable;
  *fsource_option_p = resultp->fsource_option;
  *ftarget_option_p = resultp->ftarget_option;
  return false;
}

/* Test whether $JAVAC, known to be a version of gcj < 4.3, can be used for
   compiling with target_version = 1.4 and source_version = 1.4.
   Return a failure indicator (true upon error).  */
static bool
is_envjavac_oldgcj_14_14_usable (const char *javac, bool *usablep)
{
  static bool envjavac_tested;
  static bool envjavac_usable;

  if (!envjavac_tested)
    {
      /* Try $JAVAC.  */
      struct temp_dir *tmpdir;
      char *conftest_file_name;
      char *compiled_file_name;
      const char *java_sources[1];
      struct stat statbuf;

      tmpdir = create_temp_dir ("java", NULL, false);
      if (tmpdir == NULL)
        return true;

      conftest_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
      if (write_temp_file (tmpdir, conftest_file_name,
                           get_goodcode_snippet ("1.4")))
        {
          free (conftest_file_name);
          cleanup_temp_dir (tmpdir);
          return true;
        }

      compiled_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
      register_temp_file (tmpdir, compiled_file_name);

      java_sources[0] = conftest_file_name;
      if (!compile_using_envjavac (javac, java_sources, 1, tmpdir->dir_name,
                                   false, false, false, true)
          && stat (compiled_file_name, &statbuf) >= 0)
        /* Compilation succeeded.  */
        envjavac_usable = true;

      free (compiled_file_name);
      free (conftest_file_name);

      cleanup_temp_dir (tmpdir);

      envjavac_tested = true;
    }

  *usablep = envjavac_usable;
  return false;
}

/* Test whether $JAVAC, known to be a version of gcj < 4.3, can be used for
   compiling with target_version = 1.4 and source_version = 1.3.
   Return a failure indicator (true upon error).  */
static bool
is_envjavac_oldgcj_14_13_usable (const char *javac,
                                 bool *usablep, bool *need_no_assert_option_p)
{
  static bool envjavac_tested;
  static bool envjavac_usable;
  static bool envjavac_need_no_assert_option;

  if (!envjavac_tested)
    {
      /* Try $JAVAC and "$JAVAC -fno-assert".  But add -fno-assert only if
         it makes a difference.  (It could already be part of $JAVAC.)  */
      struct temp_dir *tmpdir;
      char *conftest_file_name;
      char *compiled_file_name;
      const char *java_sources[1];
      struct stat statbuf;
      bool javac_works;
      char *javac_noassert;
      bool javac_noassert_works;

      tmpdir = create_temp_dir ("java", NULL, false);
      if (tmpdir == NULL)
        return true;

      conftest_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
      if (write_temp_file (tmpdir, conftest_file_name,
                           get_goodcode_snippet ("1.3")))
        {
          free (conftest_file_name);
          cleanup_temp_dir (tmpdir);
          return true;
        }

      compiled_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
      register_temp_file (tmpdir, compiled_file_name);

      java_sources[0] = conftest_file_name;
      if (!compile_using_envjavac (javac,
                                   java_sources, 1, tmpdir->dir_name,
                                   false, false, false, true)
          && stat (compiled_file_name, &statbuf) >= 0)
        /* Compilation succeeded.  */
        javac_works = true;
      else
        javac_works = false;

      unlink (compiled_file_name);

      javac_noassert = xasprintf ("%s -fno-assert", javac);

      java_sources[0] = conftest_file_name;
      if (!compile_using_envjavac (javac_noassert,
                                   java_sources, 1, tmpdir->dir_name,
                                   false, false, false, true)
          && stat (compiled_file_name, &statbuf) >= 0)
        /* Compilation succeeded.  */
        javac_noassert_works = true;
      else
        javac_noassert_works = false;

      free (compiled_file_name);
      free (conftest_file_name);

      if (javac_works && javac_noassert_works)
        {
          conftest_file_name =
            xconcatenated_filename (tmpdir->dir_name, "conftestfail.java",
                                    NULL);
          if (write_temp_file (tmpdir, conftest_file_name,
                               get_failcode_snippet ("1.3")))
            {
              free (conftest_file_name);
              free (javac_noassert);
              cleanup_temp_dir (tmpdir);
              return true;
            }

          compiled_file_name =
            xconcatenated_filename (tmpdir->dir_name, "conftestfail.class",
                                    NULL);
          register_temp_file (tmpdir, compiled_file_name);

          java_sources[0] = conftest_file_name;
          if (!compile_using_envjavac (javac,
                                       java_sources, 1, tmpdir->dir_name,
                                       false, false, false, true)
              && stat (compiled_file_name, &statbuf) >= 0)
            {
              /* Compilation succeeded.  */
              unlink (compiled_file_name);

              java_sources[0] = conftest_file_name;
              if (!(!compile_using_envjavac (javac_noassert,
                                             java_sources, 1, tmpdir->dir_name,
                                             false, false, false, true)
                    && stat (compiled_file_name, &statbuf) >= 0))
                /* Compilation failed.  */
                /* "$JAVAC -fno-assert" works better than $JAVAC.  */
                javac_works = true;
            }

          free (compiled_file_name);
          free (conftest_file_name);
        }

      cleanup_temp_dir (tmpdir);

      if (javac_works)
        {
          envjavac_usable = true;
          envjavac_need_no_assert_option = false;
        }
      else if (javac_noassert_works)
        {
          envjavac_usable = true;
          envjavac_need_no_assert_option = true;
        }

      envjavac_tested = true;
    }

  *usablep = envjavac_usable;
  *need_no_assert_option_p = envjavac_need_no_assert_option;
  return false;
}

/* Test whether $JAVAC, known to be not a version of gcj, can be used, and
   whether it needs a -source and/or -target option.
   Return a failure indicator (true upon error).  */
static bool
is_envjavac_nongcj_usable (const char *javac,
                           const char *source_version,
                           const char *target_version,
                           bool *usablep,
                           bool *source_option_p, bool *target_option_p)
{
  /* The cache depends on the source_version and target_version.  */
  struct result_t
  {
    bool tested;
    bool usable;
    bool source_option;
    bool target_option;
  };
  static struct result_t result_cache[SOURCE_VERSION_BOUND][TARGET_VERSION_BOUND];
  struct result_t *resultp;

  resultp = &result_cache[source_version_index (source_version)]
                         [target_version_index (target_version)];
  if (!resultp->tested)
    {
      /* Try $JAVAC.  */
      struct temp_dir *tmpdir;
      char *conftest_file_name;
      char *compiled_file_name;
      const char *java_sources[1];
      struct stat statbuf;

      tmpdir = create_temp_dir ("java", NULL, false);
      if (tmpdir == NULL)
        return true;

      conftest_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
      if (write_temp_file (tmpdir, conftest_file_name,
                           get_goodcode_snippet (source_version)))
        {
          free (conftest_file_name);
          cleanup_temp_dir (tmpdir);
          return true;
        }

      compiled_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
      register_temp_file (tmpdir, compiled_file_name);

      java_sources[0] = conftest_file_name;
      if (!compile_using_envjavac (javac,
                                   java_sources, 1, tmpdir->dir_name,
                                   false, false, false, true)
          && stat (compiled_file_name, &statbuf) >= 0
          && get_classfile_version (compiled_file_name)
             <= corresponding_classfile_version (target_version))
        {
          /* $JAVAC compiled conftest.java successfully.  */
          /* Try adding -source option if it is useful.  */
          char *javac_source =
            xasprintf ("%s -source %s", javac, source_version);

          unlink (compiled_file_name);

          java_sources[0] = conftest_file_name;
          if (!compile_using_envjavac (javac_source,
                                       java_sources, 1, tmpdir->dir_name,
                                       false, false, false, true)
              && stat (compiled_file_name, &statbuf) >= 0
              && get_classfile_version (compiled_file_name)
                 <= corresponding_classfile_version (target_version))
            {
              const char *failcode = get_failcode_snippet (source_version);

              if (failcode != NULL)
                {
                  free (compiled_file_name);
                  free (conftest_file_name);

                  conftest_file_name =
                    xconcatenated_filename (tmpdir->dir_name,
                                            "conftestfail.java",
                                            NULL);
                  if (write_temp_file (tmpdir, conftest_file_name, failcode))
                    {
                      free (conftest_file_name);
                      free (javac_source);
                      cleanup_temp_dir (tmpdir);
                      return true;
                    }

                  compiled_file_name =
                    xconcatenated_filename (tmpdir->dir_name,
                                            "conftestfail.class",
                                            NULL);
                  register_temp_file (tmpdir, compiled_file_name);

                  java_sources[0] = conftest_file_name;
                  if (!compile_using_envjavac (javac,
                                               java_sources, 1,
                                               tmpdir->dir_name,
                                               false, false, false, true)
                      && stat (compiled_file_name, &statbuf) >= 0)
                    {
                      unlink (compiled_file_name);

                      java_sources[0] = conftest_file_name;
                      if (compile_using_envjavac (javac_source,
                                                  java_sources, 1,
                                                  tmpdir->dir_name,
                                                  false, false, false, true))
                        /* $JAVAC compiled conftestfail.java successfully, and
                           "$JAVAC -source $source_version" rejects it.  So the
                           -source option is useful.  */
                        resultp->source_option = true;
                    }
                }
            }

          free (javac_source);

          resultp->usable = true;
        }
      else
        {
          /* Try with -target option alone. (Sun javac 1.3.1 has the -target
             option but no -source option.)  */
          char *javac_target =
            xasprintf ("%s -target %s", javac, target_version);

          unlink (compiled_file_name);

          java_sources[0] = conftest_file_name;
          if (!compile_using_envjavac (javac_target,
                                       java_sources, 1, tmpdir->dir_name,
                                       false, false, false, true)
              && stat (compiled_file_name, &statbuf) >= 0
              && get_classfile_version (compiled_file_name)
                 <= corresponding_classfile_version (target_version))
            {
              /* "$JAVAC -target $target_version" compiled conftest.java
                 successfully.  */
              /* Try adding -source option if it is useful.  */
              char *javac_target_source =
                xasprintf ("%s -source %s", javac_target, source_version);

              unlink (compiled_file_name);

              java_sources[0] = conftest_file_name;
              if (!compile_using_envjavac (javac_target_source,
                                           java_sources, 1, tmpdir->dir_name,
                                           false, false, false, true)
                  && stat (compiled_file_name, &statbuf) >= 0
                  && get_classfile_version (compiled_file_name)
                     <= corresponding_classfile_version (target_version))
                {
                  const char *failcode = get_failcode_snippet (source_version);

                  if (failcode != NULL)
                    {
                      free (compiled_file_name);
                      free (conftest_file_name);

                      conftest_file_name =
                        xconcatenated_filename (tmpdir->dir_name,
                                                "conftestfail.java",
                                                NULL);
                      if (write_temp_file (tmpdir, conftest_file_name,
                                           failcode))
                        {
                          free (conftest_file_name);
                          free (javac_target_source);
                          free (javac_target);
                          cleanup_temp_dir (tmpdir);
                          return true;
                        }

                      compiled_file_name =
                        xconcatenated_filename (tmpdir->dir_name,
                                                "conftestfail.class",
                                                NULL);
                      register_temp_file (tmpdir, compiled_file_name);

                      java_sources[0] = conftest_file_name;
                      if (!compile_using_envjavac (javac_target,
                                                   java_sources, 1,
                                                   tmpdir->dir_name,
                                                   false, false, false, true)
                          && stat (compiled_file_name, &statbuf) >= 0)
                        {
                          unlink (compiled_file_name);

                          java_sources[0] = conftest_file_name;
                          if (compile_using_envjavac (javac_target_source,
                                                      java_sources, 1,
                                                      tmpdir->dir_name,
                                                      false, false, false,
                                                      true))
                            /* "$JAVAC -target $target_version" compiled
                               conftestfail.java successfully, and
                               "$JAVAC -target $target_version -source $source_version"
                               rejects it.  So the -source option is useful.  */
                            resultp->source_option = true;
                        }
                    }
                }

              free (javac_target_source);

              resultp->target_option = true;
              resultp->usable = true;
            }
          else
            {
              /* Maybe this -target option requires a -source option? Try with
                 -target and -source options. (Supported by Sun javac 1.4 and
                 higher.)  */
              char *javac_target_source =
                xasprintf ("%s -source %s", javac_target, source_version);

              unlink (compiled_file_name);

              java_sources[0] = conftest_file_name;
              if (!compile_using_envjavac (javac_target_source,
                                           java_sources, 1, tmpdir->dir_name,
                                           false, false, false, true)
                  && stat (compiled_file_name, &statbuf) >= 0
                  && get_classfile_version (compiled_file_name)
                     <= corresponding_classfile_version (target_version))
                {
                  /* "$JAVAC -target $target_version -source $source_version"
                     compiled conftest.java successfully.  */
                  resultp->source_option = true;
                  resultp->target_option = true;
                  resultp->usable = true;
                }

              free (javac_target_source);
            }

          free (javac_target);
        }

      free (compiled_file_name);
      free (conftest_file_name);

      resultp->tested = true;
    }

  *usablep = resultp->usable;
  *source_option_p = resultp->source_option;
  *target_option_p = resultp->target_option;
  return false;
}

static bool
is_gcj_present (void)
{
  static bool gcj_tested;
  static bool gcj_present;

  if (!gcj_tested)
    {
      /* Test for presence of gcj:
         "gcj --version 2> /dev/null | \
          sed -e 's,^[^0-9]*,,' -e 1q | \
          sed -e '/^3\.[01]/d' | grep '^[3-9]' > /dev/null"  */
      char *argv[3];
      pid_t child;
      int fd[1];
      int exitstatus;

      argv[0] = "gcj";
      argv[1] = "--version";
      argv[2] = NULL;
      child = create_pipe_in ("gcj", "gcj", argv, DEV_NULL, true, true,
                              false, fd);
      gcj_present = false;
      if (child != -1)
        {
          /* Read the subprocess output, drop all lines except the first,
             drop all characters before the first digit, and test whether
             the remaining string starts with a digit >= 3, but not with
             "3.0" or "3.1".  */
          char c[3];
          size_t count = 0;

          while (safe_read (fd[0], &c[count], 1) > 0)
            {
              if (c[count] == '\n')
                break;
              if (count == 0)
                {
                  if (!(c[0] >= '0' && c[0] <= '9'))
                    continue;
                  gcj_present = (c[0] >= '3');
                }
              count++;
              if (count == 3)
                {
                  if (c[0] == '3' && c[1] == '.'
                      && (c[2] == '0' || c[2] == '1'))
                    gcj_present = false;
                  break;
                }
            }
          while (safe_read (fd[0], &c[0], 1) > 0)
            ;

          close (fd[0]);

          /* Remove zombie process from process list, and retrieve exit
             status.  */
          exitstatus =
            wait_subprocess (child, "gcj", false, true, true, false, NULL);
          if (exitstatus != 0)
            gcj_present = false;
        }

      if (gcj_present)
        {
          /* See if libgcj.jar is well installed.  */
          struct temp_dir *tmpdir;

          tmpdir = create_temp_dir ("java", NULL, false);
          if (tmpdir == NULL)
            gcj_present = false;
          else
            {
              char *conftest_file_name;

              conftest_file_name =
                xconcatenated_filename (tmpdir->dir_name, "conftestlib.java",
                                        NULL);
              if (write_temp_file (tmpdir, conftest_file_name,
"public class conftestlib {\n"
"  public static void main (String[] args) {\n"
"  }\n"
"}\n"))
                gcj_present = false;
              else
                {
                  char *compiled_file_name;
                  const char *java_sources[1];

                  compiled_file_name =
                    xconcatenated_filename (tmpdir->dir_name,
                                            "conftestlib.class",
                                            NULL);
                  register_temp_file (tmpdir, compiled_file_name);

                  java_sources[0] = conftest_file_name;
                  if (compile_using_gcj (java_sources, 1, false,
                                         false, NULL, false, NULL,
                                         tmpdir->dir_name,
                                         false, false, false, true))
                    gcj_present = false;

                  free (compiled_file_name);
                }
              free (conftest_file_name);
            }
          cleanup_temp_dir (tmpdir);
        }

      gcj_tested = true;
    }

  return gcj_present;
}

static bool
is_gcj_43 (void)
{
  static bool gcj_tested;
  static bool gcj_43;

  if (!gcj_tested)
    {
      /* Test for presence of gcj:
         "gcj --version 2> /dev/null | \
          sed -e 's,^[^0-9]*,,' -e 1q | \
          sed -e '/^4\.[012]/d' | grep '^[4-9]'"  */
      char *argv[3];
      pid_t child;
      int fd[1];
      int exitstatus;

      argv[0] = "gcj";
      argv[1] = "--version";
      argv[2] = NULL;
      child = create_pipe_in ("gcj", "gcj", argv, DEV_NULL, true, true,
                              false, fd);
      gcj_43 = false;
      if (child != -1)
        {
          /* Read the subprocess output, drop all lines except the first,
             drop all characters before the first digit, and test whether
             the remaining string starts with a digit >= 4, but not with
             "4.0" or "4.1" or "4.2".  */
          char c[3];
          size_t count = 0;

          while (safe_read (fd[0], &c[count], 1) > 0)
            {
              if (c[count] == '\n')
                break;
              if (count == 0)
                {
                  if (!(c[0] >= '0' && c[0] <= '9'))
                    continue;
                  gcj_43 = (c[0] >= '4');
                }
              count++;
              if (count == 3)
                {
                  if (c[0] == '4' && c[1] == '.' && c[2] >= '0' && c[2] <= '2')
                    gcj_43 = false;
                  break;
                }
            }
          while (safe_read (fd[0], &c[0], 1) > 0)
            ;

          close (fd[0]);

          /* Remove zombie process from process list, and retrieve exit
             status.  */
          exitstatus =
            wait_subprocess (child, "gcj", false, true, true, false, NULL);
          if (exitstatus != 0)
            gcj_43 = false;
        }

      gcj_tested = true;
    }

  return gcj_43;
}

/* Test whether gcj >= 4.3 can be used, and whether it needs a -fsource and/or
   -ftarget option.
   Return a failure indicator (true upon error).  */
static bool
is_gcj43_usable (const char *source_version,
                 const char *target_version,
                 bool *usablep,
                 bool *fsource_option_p, bool *ftarget_option_p)
{
  /* The cache depends on the source_version and target_version.  */
  struct result_t
  {
    bool tested;
    bool usable;
    bool fsource_option;
    bool ftarget_option;
  };
  static struct result_t result_cache[SOURCE_VERSION_BOUND][TARGET_VERSION_BOUND];
  struct result_t *resultp;

  resultp = &result_cache[source_version_index (source_version)]
                         [target_version_index (target_version)];
  if (!resultp->tested)
    {
      /* Try gcj.  */
      struct temp_dir *tmpdir;
      char *conftest_file_name;
      char *compiled_file_name;
      const char *java_sources[1];
      struct stat statbuf;

      tmpdir = create_temp_dir ("java", NULL, false);
      if (tmpdir == NULL)
        return true;

      conftest_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
      if (write_temp_file (tmpdir, conftest_file_name,
                           get_goodcode_snippet (source_version)))
        {
          free (conftest_file_name);
          cleanup_temp_dir (tmpdir);
          return true;
        }

      compiled_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
      register_temp_file (tmpdir, compiled_file_name);

      java_sources[0] = conftest_file_name;
      if (!compile_using_gcj (java_sources, 1, false, false, NULL, false, NULL,
                              tmpdir->dir_name, false, false, false, true)
          && stat (compiled_file_name, &statbuf) >= 0
          && get_classfile_version (compiled_file_name)
             <= corresponding_classfile_version (target_version))
        {
          /* gcj compiled conftest.java successfully.  */
          /* Try adding -fsource option if it is useful.  */
          unlink (compiled_file_name);

          java_sources[0] = conftest_file_name;
          if (!compile_using_gcj (java_sources, 1,
                                  false, true, source_version, false, NULL,
                                  tmpdir->dir_name, false, false, false, true)
              && stat (compiled_file_name, &statbuf) >= 0
              && get_classfile_version (compiled_file_name)
                 <= corresponding_classfile_version (target_version))
            {
              const char *failcode = get_failcode_snippet (source_version);

              if (failcode != NULL)
                {
                  free (compiled_file_name);
                  free (conftest_file_name);

                  conftest_file_name =
                    xconcatenated_filename (tmpdir->dir_name,
                                            "conftestfail.java",
                                            NULL);
                  if (write_temp_file (tmpdir, conftest_file_name, failcode))
                    {
                      free (conftest_file_name);
                      cleanup_temp_dir (tmpdir);
                      return true;
                    }

                  compiled_file_name =
                    xconcatenated_filename (tmpdir->dir_name,
                                            "conftestfail.class",
                                            NULL);
                  register_temp_file (tmpdir, compiled_file_name);

                  java_sources[0] = conftest_file_name;
                  if (!compile_using_gcj (java_sources, 1,
                                          false, false, NULL, false, NULL,
                                          tmpdir->dir_name,
                                          false, false, false, true)
                      && stat (compiled_file_name, &statbuf) >= 0)
                    {
                      unlink (compiled_file_name);

                      java_sources[0] = conftest_file_name;
                      if (compile_using_gcj (java_sources, 1,
                                             false, true, source_version,
                                             false, NULL,
                                             tmpdir->dir_name,
                                             false, false, false, true))
                        /* gcj compiled conftestfail.java successfully, and
                           "gcj -fsource=$source_version" rejects it.  So
                           the -fsource option is useful.  */
                        resultp->fsource_option = true;
                    }
                }
            }

          resultp->usable = true;
        }
      else
        {
          /* Try with -fsource and -ftarget options.  */
          unlink (compiled_file_name);

          java_sources[0] = conftest_file_name;
          if (!compile_using_gcj (java_sources, 1,
                                  false, true, source_version,
                                  true, target_version,
                                  tmpdir->dir_name,
                                  false, false, false, true)
              && stat (compiled_file_name, &statbuf) >= 0
              && get_classfile_version (compiled_file_name)
                 <= corresponding_classfile_version (target_version))
            {
              /* "gcj -fsource $source_version -ftarget $target_version"
                 compiled conftest.java successfully.  */
              resultp->fsource_option = true;
              resultp->ftarget_option = true;
              resultp->usable = true;
            }
        }

      free (compiled_file_name);
      free (conftest_file_name);

      resultp->tested = true;
    }

  *usablep = resultp->usable;
  *fsource_option_p = resultp->fsource_option;
  *ftarget_option_p = resultp->ftarget_option;
  return false;
}

/* Test whether gcj < 4.3 can be used for compiling with target_version = 1.4
   and source_version = 1.4.
   Return a failure indicator (true upon error).  */
static bool
is_oldgcj_14_14_usable (bool *usablep)
{
  static bool gcj_tested;
  static bool gcj_usable;

  if (!gcj_tested)
    {
      /* Try gcj.  */
      struct temp_dir *tmpdir;
      char *conftest_file_name;
      char *compiled_file_name;
      const char *java_sources[1];
      struct stat statbuf;

      tmpdir = create_temp_dir ("java", NULL, false);
      if (tmpdir == NULL)
        return true;

      conftest_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
      if (write_temp_file (tmpdir, conftest_file_name,
                           get_goodcode_snippet ("1.4")))
        {
          free (conftest_file_name);
          cleanup_temp_dir (tmpdir);
          return true;
        }

      compiled_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
      register_temp_file (tmpdir, compiled_file_name);

      java_sources[0] = conftest_file_name;
      if (!compile_using_gcj (java_sources, 1, false, false, NULL, false, NULL,
                              tmpdir->dir_name, false, false, false, true)
          && stat (compiled_file_name, &statbuf) >= 0)
        /* Compilation succeeded.  */
        gcj_usable = true;

      free (compiled_file_name);
      free (conftest_file_name);

      cleanup_temp_dir (tmpdir);

      gcj_tested = true;
    }

  *usablep = gcj_usable;
  return false;
}

/* Test whether gcj < 4.3 can be used for compiling with target_version = 1.4
   and source_version = 1.3.
   Return a failure indicator (true upon error).  */
static bool
is_oldgcj_14_13_usable (bool *usablep, bool *need_no_assert_option_p)
{
  static bool gcj_tested;
  static bool gcj_usable;
  static bool gcj_need_no_assert_option;

  if (!gcj_tested)
    {
      /* Try gcj and "gcj -fno-assert".  But add -fno-assert only if
         it works (not gcj < 3.3).  */
      struct temp_dir *tmpdir;
      char *conftest_file_name;
      char *compiled_file_name;
      const char *java_sources[1];
      struct stat statbuf;

      tmpdir = create_temp_dir ("java", NULL, false);
      if (tmpdir == NULL)
        return true;

      conftest_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
      if (write_temp_file (tmpdir, conftest_file_name,
                           get_goodcode_snippet ("1.3")))
        {
          free (conftest_file_name);
          cleanup_temp_dir (tmpdir);
          return true;
        }

      compiled_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
      register_temp_file (tmpdir, compiled_file_name);

      java_sources[0] = conftest_file_name;
      if (!compile_using_gcj (java_sources, 1, true, false, NULL, false, NULL,
                              tmpdir->dir_name, false, false, false, true)
          && stat (compiled_file_name, &statbuf) >= 0)
        /* Compilation succeeded.  */
        {
          gcj_usable = true;
          gcj_need_no_assert_option = true;
        }
      else
        {
          unlink (compiled_file_name);

          java_sources[0] = conftest_file_name;
          if (!compile_using_gcj (java_sources, 1, false,
                                  false, NULL, false, NULL,
                                  tmpdir->dir_name, false, false, false, true)
              && stat (compiled_file_name, &statbuf) >= 0)
            /* Compilation succeeded.  */
            {
              gcj_usable = true;
              gcj_need_no_assert_option = false;
            }
        }

      free (compiled_file_name);
      free (conftest_file_name);

      cleanup_temp_dir (tmpdir);

      gcj_tested = true;
    }

  *usablep = gcj_usable;
  *need_no_assert_option_p = gcj_need_no_assert_option;
  return false;
}

static bool
is_javac_present (void)
{
  static bool javac_tested;
  static bool javac_present;

  if (!javac_tested)
    {
      /* Test for presence of javac: "javac 2> /dev/null ; test $? -le 2"  */
      char *argv[2];
      int exitstatus;

      argv[0] = "javac";
      argv[1] = NULL;
      exitstatus = execute ("javac", "javac", argv, false, false, true, true,
                            true, false, NULL);
      javac_present = (exitstatus == 0 || exitstatus == 1 || exitstatus == 2);
      javac_tested = true;
    }

  return javac_present;
}

/* Test whether javac can be used and whether it needs a -source and/or
   -target option.
   Return a failure indicator (true upon error).  */
static bool
is_javac_usable (const char *source_version, const char *target_version,
                 bool *usablep, bool *source_option_p, bool *target_option_p)
{
  /* The cache depends on the source_version and target_version.  */
  struct result_t
  {
    bool tested;
    bool usable;
    bool source_option;
    bool target_option;
  };
  static struct result_t result_cache[SOURCE_VERSION_BOUND][TARGET_VERSION_BOUND];
  struct result_t *resultp;

  resultp = &result_cache[source_version_index (source_version)]
                         [target_version_index (target_version)];
  if (!resultp->tested)
    {
      /* Try javac.  */
      struct temp_dir *tmpdir;
      char *conftest_file_name;
      char *compiled_file_name;
      const char *java_sources[1];
      struct stat statbuf;

      tmpdir = create_temp_dir ("java", NULL, false);
      if (tmpdir == NULL)
        return true;

      conftest_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.java", NULL);
      if (write_temp_file (tmpdir, conftest_file_name,
                           get_goodcode_snippet (source_version)))
        {
          free (conftest_file_name);
          cleanup_temp_dir (tmpdir);
          return true;
        }

      compiled_file_name =
        xconcatenated_filename (tmpdir->dir_name, "conftest.class", NULL);
      register_temp_file (tmpdir, compiled_file_name);

      java_sources[0] = conftest_file_name;
      if (!compile_using_javac (java_sources, 1,
                                false, source_version,
                                false, target_version,
                                tmpdir->dir_name, false, false, false, true)
          && stat (compiled_file_name, &statbuf) >= 0
          && get_classfile_version (compiled_file_name)
             <= corresponding_classfile_version (target_version))
        {
          /* javac compiled conftest.java successfully.  */
          /* Try adding -source option if it is useful.  */
          unlink (compiled_file_name);

          java_sources[0] = conftest_file_name;
          if (!compile_using_javac (java_sources, 1,
                                    true, source_version,
                                    false, target_version,
                                    tmpdir->dir_name, false, false, false, true)
              && stat (compiled_file_name, &statbuf) >= 0
              && get_classfile_version (compiled_file_name)
                 <= corresponding_classfile_version (target_version))
            {
              const char *failcode = get_failcode_snippet (source_version);

              if (failcode != NULL)
                {
                  free (compiled_file_name);
                  free (conftest_file_name);

                  conftest_file_name =
                    xconcatenated_filename (tmpdir->dir_name,
                                            "conftestfail.java",
                                            NULL);
                  if (write_temp_file (tmpdir, conftest_file_name, failcode))
                    {
                      free (conftest_file_name);
                      cleanup_temp_dir (tmpdir);
                      return true;
                    }

                  compiled_file_name =
                    xconcatenated_filename (tmpdir->dir_name,
                                            "conftestfail.class",
                                            NULL);
                  register_temp_file (tmpdir, compiled_file_name);

                  java_sources[0] = conftest_file_name;
                  if (!compile_using_javac (java_sources, 1,
                                            false, source_version,
                                            false, target_version,
                                            tmpdir->dir_name,
                                            false, false, false, true)
                      && stat (compiled_file_name, &statbuf) >= 0)
                    {
                      unlink (compiled_file_name);

                      java_sources[0] = conftest_file_name;
                      if (compile_using_javac (java_sources, 1,
                                               true, source_version,
                                               false, target_version,
                                               tmpdir->dir_name,
                                               false, false, false, true))
                        /* javac compiled conftestfail.java successfully, and
                           "javac -source $source_version" rejects it.  So the
                           -source option is useful.  */
                        resultp->source_option = true;
                    }
                }
            }

          resultp->usable = true;
        }
      else
        {
          /* Try with -target option alone. (Sun javac 1.3.1 has the -target
             option but no -source option.)  */
          unlink (compiled_file_name);

          java_sources[0] = conftest_file_name;
          if (!compile_using_javac (java_sources, 1,
                                    false, source_version,
                                    true, target_version,
                                    tmpdir->dir_name,
                                    false, false, false, true)
              && stat (compiled_file_name, &statbuf) >= 0
              && get_classfile_version (compiled_file_name)
                 <= corresponding_classfile_version (target_version))
            {
              /* "javac -target $target_version" compiled conftest.java
                 successfully.  */
              /* Try adding -source option if it is useful.  */
              unlink (compiled_file_name);

              java_sources[0] = conftest_file_name;
              if (!compile_using_javac (java_sources, 1,
                                        true, source_version,
                                        true, target_version,
                                        tmpdir->dir_name,
                                        false, false, false, true)
                  && stat (compiled_file_name, &statbuf) >= 0
                  && get_classfile_version (compiled_file_name)
                     <= corresponding_classfile_version (target_version))
                {
                  const char *failcode = get_failcode_snippet (source_version);

                  if (failcode != NULL)
                    {
                      free (compiled_file_name);
                      free (conftest_file_name);

                      conftest_file_name =
                        xconcatenated_filename (tmpdir->dir_name,
                                                "conftestfail.java",
                                                NULL);
                      if (write_temp_file (tmpdir, conftest_file_name,
                                           failcode))
                        {
                          free (conftest_file_name);
                          cleanup_temp_dir (tmpdir);
                          return true;
                        }

                      compiled_file_name =
                        xconcatenated_filename (tmpdir->dir_name,
                                                "conftestfail.class",
                                                NULL);
                      register_temp_file (tmpdir, compiled_file_name);

                      java_sources[0] = conftest_file_name;
                      if (!compile_using_javac (java_sources, 1,
                                                false, source_version,
                                                true, target_version,
                                                tmpdir->dir_name,
                                                false, false, false, true)
                          && stat (compiled_file_name, &statbuf) >= 0)
                        {
                          unlink (compiled_file_name);

                          java_sources[0] = conftest_file_name;
                          if (compile_using_javac (java_sources, 1,
                                                   true, source_version,
                                                   true, target_version,
                                                   tmpdir->dir_name,
                                                   false, false, false, true))
                            /* "javac -target $target_version" compiled
                               conftestfail.java successfully, and
                               "javac -target $target_version -source $source_version"
                               rejects it.  So the -source option is useful.  */
                            resultp->source_option = true;
                        }
                    }
                }

              resultp->target_option = true;
              resultp->usable = true;
            }
          else
            {
              /* Maybe this -target option requires a -source option? Try with
                 -target and -source options. (Supported by Sun javac 1.4 and
                 higher.)  */
              unlink (compiled_file_name);

              java_sources[0] = conftest_file_name;
              if (!compile_using_javac (java_sources, 1,
                                        true, source_version,
                                        true, target_version,
                                        tmpdir->dir_name,
                                        false, false, false, true)
                  && stat (compiled_file_name, &statbuf) >= 0
                  && get_classfile_version (compiled_file_name)
                     <= corresponding_classfile_version (target_version))
                {
                  /* "javac -target $target_version -source $source_version"
                     compiled conftest.java successfully.  */
                  resultp->source_option = true;
                  resultp->target_option = true;
                  resultp->usable = true;
                }
            }
        }

      free (compiled_file_name);
      free (conftest_file_name);

      resultp->tested = true;
    }

  *usablep = resultp->usable;
  *source_option_p = resultp->source_option;
  *target_option_p = resultp->target_option;
  return false;
}

static bool
is_jikes_present (void)
{
  static bool jikes_tested;
  static bool jikes_present;

  if (!jikes_tested)
    {
      /* Test for presence of jikes: "jikes 2> /dev/null ; test $? = 1"  */
      char *argv[2];
      int exitstatus;

      argv[0] = "jikes";
      argv[1] = NULL;
      exitstatus = execute ("jikes", "jikes", argv, false, false, true, true,
                            true, false, NULL);
      jikes_present = (exitstatus == 0 || exitstatus == 1);
      jikes_tested = true;
    }

  return jikes_present;
}

/* ============================= Main function ============================= */

bool
compile_java_class (const char * const *java_sources,
                    unsigned int java_sources_count,
                    const char * const *classpaths,
                    unsigned int classpaths_count,
                    const char *source_version,
                    const char *target_version,
                    const char *directory,
                    bool optimize, bool debug,
                    bool use_minimal_classpath,
                    bool verbose)
{
  bool err = false;
  char *old_JAVA_HOME;

  {
    const char *javac = getenv ("JAVAC");
    if (javac != NULL && javac[0] != '\0')
      {
        bool usable = false;
        bool no_assert_option = false;
        bool source_option = false;
        bool target_option = false;
        bool fsource_option = false;
        bool ftarget_option = false;

        if (target_version == NULL)
          target_version = default_target_version ();

        if (is_envjavac_gcj (javac))
          {
            /* It's a version of gcj.  */
            if (is_envjavac_gcj43 (javac))
              {
                /* It's a version of gcj >= 4.3.  Assume the classfile versions
                   are correct.  */
                if (is_envjavac_gcj43_usable (javac,
                                              source_version, target_version,
                                              &usable,
                                              &fsource_option, &ftarget_option))
                  {
                    err = true;
                    goto done1;
                  }
              }
            else
              {
                /* It's a version of gcj < 4.3.  Ignore the version of the
                   class files that it creates.  */
                if (strcmp (target_version, "1.4") == 0
                    && strcmp (source_version, "1.4") == 0)
                  {
                    if (is_envjavac_oldgcj_14_14_usable (javac, &usable))
                      {
                        err = true;
                        goto done1;
                      }
                  }
                else if (strcmp (target_version, "1.4") == 0
                         && strcmp (source_version, "1.3") == 0)
                  {
                    if (is_envjavac_oldgcj_14_13_usable (javac,
                                                         &usable,
                                                         &no_assert_option))
                      {
                        err = true;
                        goto done1;
                      }
                  }
              }
          }
        else
          {
            /* It's not gcj.  Assume the classfile versions are correct.  */
            if (is_envjavac_nongcj_usable (javac,
                                           source_version, target_version,
                                           &usable,
                                           &source_option, &target_option))
              {
                err = true;
                goto done1;
              }
          }

        if (usable)
          {
            char *old_classpath;
            char *javac_with_options;

            /* Set CLASSPATH.  */
            old_classpath =
              set_classpath (classpaths, classpaths_count, false, verbose);

            javac_with_options =
              (no_assert_option
               ? xasprintf ("%s -fno-assert", javac)
               : xasprintf ("%s%s%s%s%s%s%s%s%s",
                            javac,
                            source_option ? " -source " : "",
                            source_option ? source_version : "",
                            target_option ? " -target " : "",
                            target_option ? target_version : "",
                            fsource_option ? " -fsource=" : "",
                            fsource_option ? source_version : "",
                            ftarget_option ? " -ftarget=" : "",
                            ftarget_option ? target_version : ""));

            err = compile_using_envjavac (javac_with_options,
                                          java_sources, java_sources_count,
                                          directory, optimize, debug, verbose,
                                          false);

            free (javac_with_options);

            /* Reset CLASSPATH.  */
            reset_classpath (old_classpath);

            goto done1;
          }
      }
  }

  /* Unset the JAVA_HOME environment variable.  */
  old_JAVA_HOME = getenv ("JAVA_HOME");
  if (old_JAVA_HOME != NULL)
    {
      old_JAVA_HOME = xstrdup (old_JAVA_HOME);
      unsetenv ("JAVA_HOME");
    }

  if (is_gcj_present ())
    {
      /* It's a version of gcj.  */
      bool usable = false;
      bool no_assert_option = false;
      bool fsource_option = false;
      bool ftarget_option = false;

      if (target_version == NULL)
        target_version = default_target_version ();

      if (is_gcj_43 ())
        {
          /* It's a version of gcj >= 4.3.  Assume the classfile versions
             are correct.  */
          if (is_gcj43_usable (source_version, target_version,
                               &usable, &fsource_option, &ftarget_option))
            {
              err = true;
              goto done1;
            }
        }
      else
        {
          /* It's a version of gcj < 4.3.  Ignore the version of the class
             files that it creates.
             Test whether it supports the desired target-version and
             source-version.  */
          if (strcmp (target_version, "1.4") == 0
              && strcmp (source_version, "1.4") == 0)
            {
              if (is_oldgcj_14_14_usable (&usable))
                {
                  err = true;
                  goto done1;
                }
            }
          else if (strcmp (target_version, "1.4") == 0
                   && strcmp (source_version, "1.3") == 0)
            {
              if (is_oldgcj_14_13_usable (&usable, &no_assert_option))
                {
                  err = true;
                  goto done1;
                }
            }
        }

      if (usable)
        {
          char *old_classpath;

          /* Set CLASSPATH.  We could also use the --CLASSPATH=... option
             of gcj.  Note that --classpath=... option is different: its
             argument should also contain gcj's libgcj.jar, but we don't
             know its location.  */
          old_classpath =
            set_classpath (classpaths, classpaths_count, use_minimal_classpath,
                           verbose);

          err = compile_using_gcj (java_sources, java_sources_count,
                                   no_assert_option,
                                   fsource_option, source_version,
                                   ftarget_option, target_version,
                                   directory, optimize, debug, verbose, false);

          /* Reset CLASSPATH.  */
          reset_classpath (old_classpath);

          goto done2;
        }
    }

  if (is_javac_present ())
    {
      bool usable = false;
      bool source_option = false;
      bool target_option = false;

      if (target_version == NULL)
        target_version = default_target_version ();

      if (is_javac_usable (source_version, target_version,
                           &usable, &source_option, &target_option))
        {
          err = true;
          goto done1;
        }

      if (usable)
        {
          char *old_classpath;

          /* Set CLASSPATH.  We don't use the "-classpath ..." option because
             in JDK 1.1.x its argument should also contain the JDK's
             classes.zip, but we don't know its location.  (In JDK 1.3.0 it
             would work.)  */
          old_classpath =
            set_classpath (classpaths, classpaths_count, use_minimal_classpath,
                           verbose);

          err = compile_using_javac (java_sources, java_sources_count,
                                     source_option, source_version,
                                     target_option, target_version,
                                     directory, optimize, debug, verbose,
                                     false);

          /* Reset CLASSPATH.  */
          reset_classpath (old_classpath);

          goto done2;
        }
    }

  if (is_jikes_present ())
    {
      /* Test whether it supports the desired target-version and
         source-version.  */
      bool usable = (strcmp (source_version, "1.3") == 0);

      if (usable)
        {
          char *old_classpath;

          /* Set CLASSPATH.  We could also use the "-classpath ..." option.
             Since jikes doesn't come with its own standard library, it
             needs a classes.zip or rt.jar or libgcj.jar in the CLASSPATH.
             To increase the chance of success, we reuse the current CLASSPATH
             if the user has set it.  */
          old_classpath =
            set_classpath (classpaths, classpaths_count, false, verbose);

          err = compile_using_jikes (java_sources, java_sources_count,
                                     directory, optimize, debug, verbose,
                                     false);

          /* Reset CLASSPATH.  */
          reset_classpath (old_classpath);

          goto done2;
        }
    }

  error (0, 0, _("Java compiler not found, try installing gcj or set $JAVAC"));
  err = true;

 done2:
  if (old_JAVA_HOME != NULL)
    {
      xsetenv ("JAVA_HOME", old_JAVA_HOME, 1);
      free (old_JAVA_HOME);
    }

 done1:
  return err;
}