summaryrefslogtreecommitdiff
path: root/SConstruct
blob: ccaca7da8ac1f4c73f492afd803c8b0671bc727a (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
### SCons build recipe for the GPSD project

# Important targets:
#
# build     - build the software (default)
# dist      - make distribution tarball
# install   - install programs, libraries, and manual pages
# uninstall - undo an install
#
# check     - run regression and unit tests.
# audit     - run code-auditing tools
# testbuild - test-build the code from a tarball
# release   - ship a release
#
# Setting the DESTDIR environment variable will prefix the install destinations
# without changing the --prefix prefix.

# Unfinished items:
# * Out-of-directory builds: see http://www.scons.org/wiki/UsingBuildDir

# Release identification begins here
gpsd_version = "3.5~dev"

# library version
libgps_version_current  = 20
libgps_version_revision = 0
libgps_version_age      = 0

# Release identification ends here

# Hosting information (mainly used for templating web pages) begins here
# Each variable foo has a corresponding @FOO@ expanded in .in files.
# There are no project-dependent URLs or references to the hosting site
# anywhere else in the distribution; preserve this property!
sitename   = "Savannah"
sitesearch = "catb.org"
website    = "http://catb.org/gpsd" 
mainpage   = "https://savannah.nongnu.org/projects/gpsd/"
webupload  = "login.ibiblio.org:/public/html/catb/gpsd"
scpupload  = "dl.sv.nongnu.org:/releases/gpsd/"
mailman    = "http://lists.nongnu.org/mailman/listinfo/"
admin      = "https://savannah.nongnu.org/project/admin/?group=gpsd"
download   = "http://download-mirror.savannah.gnu.org/releases/gpsd/"
bugtracker = "https://savannah.nongnu.org/bugs/?group=gpsd"
browserepo = "http://git.savannah.gnu.org/cgit/gpsd.git"
clonerepo  = "https://savannah.nongnu.org/git/?group=gpsd"
gitrepo    = "git://git.savannah.nongnu.org/gpsd.git"
webform    = "https://www.mainframe.cx/cgi-bin/gps_report.cgi"
formserver = "www@mainframe.cx"
devmail    = "gpsd-dev@lists.nongnu.org"
# Hosting information ends here

EnsureSConsVersion(2,0,1)

import copy, os, sys, glob, re, platform, time
from distutils import sysconfig
from distutils.util import get_platform
import SCons

# replacement for functions from the commands module, which is deprecated.
from subprocess import PIPE, STDOUT, Popen
def _getstatusoutput(cmd, input=None, cwd=None, env=None):
    pipe = Popen(cmd, shell=True, cwd=cwd, env=env, stdout=PIPE, stderr=STDOUT)
    (output, errout) = pipe.communicate(input=input)
    status = pipe.returncode
    return (status, output)
def _getoutput(cmd, input=None, cwd=None, env=None):
    return _getstatusoutput(cmd, input, cwd, env)[1]


#
# Build-control options
#

# Start by reading configuration variables from the cache
opts = Variables('.scons-option-cache')

systemd = os.path.exists("/usr/share/systemd/system")

# Set distribution-specific defaults here
imloads = True
if sys.platform.startswith('linux'):
    (distro, version, cutename) = platform.linux_distribution()
    if distro == 'Fedora':
        if int(version) >= 13:
            # See https://fedoraproject.org/wiki/Features/ChangeInImplicitDSOLinking
            imloads = False
    elif os.path.exists("/etc/arch-release"):
        imloads = False

# Does our platform has a working memory-barrier instruction?
# The shared-memory export won't be reliable without it.
mfence = (platform.machine() in ('x86_64',))
    
boolopts = (
    # GPS protocols
    ("nmea",          True,  "NMEA support"),
    ("ashtech",       True,  "Ashtech support"),
    ("earthmate",     True,  "DeLorme EarthMate Zodiac support"),
    ("evermore",      True,  "EverMore binary support"),
    ("fv18",          True,  "San Jose Navigation FV-18 support"),
    ("garmin",        True,  "Garmin kernel driver support"),
    ("garmintxt",     True,  "Garmin Simple Text support"),
    ("geostar",       True,  "Geostar Protocol support"),
    ("itrax",         True,  "iTrax hardware support"),
    ("mtk3301",       True,  "MTK-3301 support"),
    ("navcom",        True,  "Navcom support"),
    ("oncore",        True,  "Motorola OnCore chipset support"),
    ("sirf",          True,  "SiRF chipset support"),
    ("superstar2",    True,  "Novatel SuperStarII chipset support"),
    ("tnt",           True,  "True North Technologies support"),
    ("tripmate",      True,  "DeLorme TripMate support"),
    ("tsip",          True,  "Trimble TSIP support"),
    ("ubx",           True,  "UBX Protocol support"),
    ("fury",          True,  "Jackson Labs Fury and Firefly support"),
    # Non-GPS protocols
    ("aivdm",         True,  "AIVDM support"),
    ("gpsclock",      True,  "GPSClock support"),
    ("ntrip",         True,  "NTRIP support"),
    ("oceanserver",   True,  "OceanServer support"),
    ("rtcm104v2",     True,  "rtcm104v2 support"),
    ("rtcm104v3",     True,  "rtcm104v3 support"),
    # Time service
    ("ntpshm",        True,  "NTP time hinting support"),
    ("pps",           True,  "PPS time syncing support"),
    # Export methods
    ("socket_export", True,  "data export over sockets"),
    ("dbus_export",   False, "enable DBUS export support"),
    ("shm_export",    mfence,"export via shared memory"),
    # Communication
    ('usb',           True,  "libusb support for USB devices"),
    ("bluez",         True,  "BlueZ support for Bluetooth devices"),
    ("ipv6",          True,  "build IPv6 support"),
    ("netfeed",       True,  "build support for handling TCP/IP data sources"),
    ("passthrough",   True,  "build support for passing through JSON"),
    # Other daemon options
    ("force_global",  False, "force daemon to listen on all addressses"),
    ("timing",        False, "latency timing support"),
    ("control_socket",True,  "control socket for hotplug notifications"),
    ("systemd",       systemd, "systemd socket activation"),
    # Client-side options
    ("clientdebug",   True,  "client debugging support"),
    ("oldstyle",      True,  "oldstyle (pre-JSON) protocol support"),
    ("libgpsmm",      True,  "build C++ bindings"),
    ("libQgpsmm",     True,  "build QT bindings"),
    ("reconfigure",   True,  "allow gpsd to change device settings"),
    ("controlsend",   True,  "allow gpsctl/gpsmon to change device settings"),
    ("cheapfloats",   True,  "float ops are cheap, compute error estimates"),
    ("squelch",       False, "squelch gpsd_report/gpsd_hexdump to save cpu"),
    ("ncurses",       True,  "build with ncurses"),
    # Build control
    ("shared",        True,  "build shared libraries, not static"),
    ("implicit_link", imloads,"implicit linkage is supported in shared libs"),
    ("python",        True,  "build Python support and modules."),
    ("debug",         False, "include debug information in build"),
    ("profiling",     False, "build with profiling enabled"),
    ("strip",         True,  "build with stripping of binaries enabled"),
    )
for (name, default, help) in boolopts:
    opts.Add(BoolVariable(name, help, default))

nonboolopts = (
    ("gpsd_user",           "nobody",      "privilege revocation user",),
    ("gpsd_group",          "(undefined)", "privilege revocation group"),
    ("prefix",              "/usr/local",  "installation directory prefix"),
    ("limited_max_clients", 0,             "maximum allowed clients"),
    ("limited_max_devices", 0,             "maximum allowed devices"),
    ("fixed_port_speed",    0,             "fixed serial port speed"),
    ("fixed_stop_bits",     0,             "fixed serial port stop bits"),
    ("pps_pin",             "DCD",         "pin to expect PPS pulses on"),
    ("target",              "",            "cross-development target"),
    ("sysroot",             "",            "cross-development system root"),
    )
for (name, default, help) in nonboolopts:
    opts.Add(name, help, default)

pathopts = (
    ("sysconfdir",          "etc",           "system configuration directory"),
    ("bindir",              "bin",           "application binaries directory"),
    ("includedir",          "include",       "header file directory"),
    ("libdir",              "lib",           "system libraries"),
    ("sbindir",             "sbin",          "system binaries directory"),
    ("mandir",              "share/man",     "manual pages directory"),
    ("docdir",              "share/doc",     "documents directory"),
    ("pkgconfig",           "$libdir/pkgconfig", "pkgconfig file directory"),
    )
for (name, default, help) in pathopts:
    opts.Add(PathVariable(name, help, default, PathVariable.PathAccept))


#
# Environment creation
#
import_env = (
    "DISPLAY",         # Required for dia to run under scons
    "GROUPS",          # Required by gpg
    "HOME",            # Required by gpg
    "LOGNAME",         # LOGNAME is required for the flocktest production.
    'PATH',            # Required for ccache and Coverity scan-build
    'PKG_CONFIG_PATH', # Set .pc file directory in a crossbuild
    'STAGING_PREFIX',  # Required by the OpenWRT build.
    )
envs = {}
for var in import_env:
    if var in os.environ:
        envs[var] = os.environ[var]

env = Environment(tools=["default", "tar", "textfile"], options=opts, ENV=envs)
opts.Save('.scons-option-cache', env)
env.SConsignFile(".sconsign.dblite")

for (name, default, help) in pathopts:
    env[name] = env.subst(env[name])

env['VERSION'] = gpsd_version
env['PYTHON'] = sys.executable

# Set defaults from environment.  Note that scons doesn't cope well
# with multi-word CPPFLAGS/LDFLAGS/SHLINKFLAGS values; you'll have to
# explicitly quote them or (better yet) use the "=" form of GNU option
# settings.
env['STRIP'] = "strip"
env['PKG_CONFIG'] = "pkg-config"
env['CHRPATH'] = 'chrpath'
for i in ["AR", "ARFLAGS", "CCFLAGS", "CFLAGS", "CC", "CXX", "CXXFLAGS", "STRIP", "PKG_CONFIG", "CHRPATH", "LD", "TAR"]:
    if os.environ.has_key(i):
        j = i
        if i == "LD":
            i = "SHLINK"
        if i == "CFLAGS" or i == "CCFLAGS":
            env.Replace(**{j: Split(os.getenv(i))})
        else:
            env.Replace(**{j: os.getenv(i)})
for flags in ["LDFLAGS", "LINKFLAGS", "SHLINKFLAGS", "CPPFLAGS"]:
    if os.environ.has_key(flags):
        env.MergeFlags({flags : [os.getenv(flags)]})


# Placeholder so we can kluge together something like VPATH builds.
# $SRCDIR replaces occurrences for $(srcdir) in the autotools build.
env['SRCDIR'] = '.'

def announce(msg):
    if not env.GetOption("silent"):
        print msg

# GCC isn't always named gcc, alas.
if env['CC'] == 'gcc' or (sys.platform.startswith('freebsd') and env['CC'] == 'cc'):
    # Enable all GCC warnings except uninitialized and
    # missing-field-initializers, which we can't help triggering because
    # of the way some of the JSON-parsing code is generated.
    # Also not including -Wcast-qual and -Wimplicit-function-declaration,
    # because we can't seem to keep scons from passing it to g++.
    env.Append(CFLAGS=Split('''-Wextra -Wall -Wno-uninitialized
                            -Wno-missing-field-initializers -Wcast-align
                            -Wmissing-declarations -Wmissing-prototypes
                            -Wstrict-prototypes -Wpointer-arith -Wreturn-type
                            -D_GNU_SOURCE'''))

# DESTDIR environment variable means user wants to prefix the installation root.
DESTDIR = os.environ.get('DESTDIR', '')

def installdir(dir, add_destdir=True):
    # use os.path.join to handle absolute paths properly.
    wrapped = os.path.join(env['prefix'], env[dir])
    if add_destdir:
        wrapped = os.path.normpath(DESTDIR + os.path.sep + wrapped)
    wrapped.replace("/usr/etc", "/etc")
    return wrapped

# Honor the specified installation prefix in link paths.
env.Prepend(LIBPATH=[installdir('libdir')])
if env["shared"]:
    env.Prepend(RPATH=[installdir('libdir')])

# Give deheader a way to set compiler flags
if 'MORECFLAGS' in os.environ:
    env.Append(CFLAGS=Split(os.environ['MORECFLAGS']))

# Don't change CCFLAGS if already set by environment.
if not 'CCFLAGS' in os.environ:
    # Should we build with profiling?
    if env['profiling']:
        env.Append(CCFLAGS=['-pg'])
        env.Append(LDFLAGS=['-pg'])
    # Should we build with debug symbols?
    if env['debug']:
        env.Append(CCFLAGS=['-g'])
        env.Append(CCFLAGS=['-O0'])
    else:
        env.Append(CCFLAGS=['-O2'])

# Get a slight speedup by not doing automatic RCS and SCCS fetches.
env.SourceCode('.', None)

## Cross-development

devenv = (("ADDR2LINE", "addr2line"),
          ("AR","ar"),
          ("AS","as"),
          ("CHRPATH", "chrpath"),
          ("CXX","c++"),
          ("CXXFILT","c++filt"),
          ("CPP","cpp"),
          ("GXX","g++"),
          ("CC","gcc"),
          ("GCCBUG","gccbug"),
          ("GCOV","gcov"),
          ("GPROF","gprof"),
          ("LD", "ld"),
          ("NM", "nm"),
          ("OBJCOPY","objcopy"),
          ("OBJDUMP","objdump"),
          ("RANLIB", "ranlib"),
          ("READELF","readelf"),
          ("SIZE", "size"),
          ("STRINGS", "strings"),
          ("STRIP", "strip"))

if env['target']:
    for (name, toolname) in devenv:
        env[name] = env['target'] + '-' + toolname

if env['sysroot']:
    env.MergeFlags({"CFLAGS": ["--sysroot=%s" % env['sysroot']]})
    env.MergeFlags({"LINKFLAGS": ["--sysroot=%s" % env['sysroot']]})

## Build help

Help("""Arguments may be a mixture of switches and targets in any order.
Switches apply to the entire build regardless of where they are in the order.
Important switches include:

    prefix=/usr     probably what you want for production tools

Options are cached in a file named .scons-option-cache and persist to later
invocations.  The file is editable.  Delete it to start fresh.  Current option
values can be listed with 'scons -h'.

""" + opts.GenerateHelpText(env, sort=cmp))

if "help" in ARGLIST:
    Return()

## Configuration

def CheckPKG(context, name):
    context.Message( 'Checking for %s... ' % name )
    ret = context.TryAction('%s --exists \'%s\'' % (env['PKG_CONFIG'], name))[0]
    context.Result( ret )
    return ret

def CheckExecutable(context, testprogram, check_for):
    context.Message( 'Checking for %s... ' %(check_for,))
    ret = context.TryAction(testprogram)[0]
    context.Result( ret )
    return ret

# Stylesheet URLs for making HTML and man pages from DocBook XML.
docbook_url_stem = 'http://docbook.sourceforge.net/release/xsl/current/'
docbook_man_uri = docbook_url_stem + 'manpages/docbook.xsl'
docbook_html_uri = docbook_url_stem + 'html/docbook.xsl'

def CheckXsltproc(context):
    context.Message('Checking that xsltproc can make man pages... ')
    with open("xmltest.xml", "w") as ofp:
        ofp.write('''
       <refentry id="foo.1">
      <refmeta>
        <refentrytitle>foo</refentrytitle>
        <manvolnum>1</manvolnum>
        <refmiscinfo class='date'>9 Aug 2004</refmiscinfo>
      </refmeta>
      <refnamediv id='name'>
        <refname>foo</refname>
        <refpurpose>check man page generation from docbook source</refpurpose>
      </refnamediv>
    </refentry>
''')
    probe = "xsltproc --nonet --noout '%s' xmltest.xml" % (docbook_man_uri,)
    ret = context.TryAction(probe)[0]
    os.remove("xmltest.xml")
    if os.path.exists("foo.1"):
        os.remove("foo.1")
    context.Result( ret )
    return ret

config = Configure(env, custom_tests = { 'CheckPKG' : CheckPKG,
                                         'CheckExecutable' : CheckExecutable,
                                         'CheckXsltproc' : CheckXsltproc})

env.Prepend(LIBPATH=[os.path.realpath(os.curdir)])
if config.CheckExecutable('$CHRPATH -v', 'chrpath'):
    # Tell generated binaries to look in the current directory for
    # shared libraries so we can run tests without hassle. Should be
    # handled sanely by scons on all systems.  Not good to use '.' or
    # a relative path here; it's a security risk.  At install time we
    # use chrpath to edit this out of RPATH.
    if env["shared"]:
        env.Prepend(RPATH=[os.path.realpath(os.curdir)])
else:
    print "chrpath is not available, forcing static linking."
    env["shared"] = False

confdefs = ["/* gpsd_config.h.  Generated by scons, do not hand-hack.  */\n"]

confdefs.append('#define VERSION "%s"\n' % gpsd_version)

confdefs.append('#define GPSD_URL "%s"\n' % website)

cxx = config.CheckCXX()

# define a helper function for pkg-config - we need to pass
# --static for static linking, too.
if env["shared"]:
    pkg_config = lambda pkg: ['!%s --cflags --libs %s' %(env['PKG_CONFIG'], pkg, )]
else:
    pkg_config = lambda pkg: ['!%s --cflags --libs --static %s' %(env['PKG_CONFIG'], pkg, )]

# The actual distinction here is whether the platform has ncurses in the
# base system or not. If it does, pkg-config is not likely to tell us
# anything useful. FreeBSD does, Linux doesn't. Most likely other BSDs
# are like FreeBSD.
ncurseslibs= []
if env['ncurses']:
    if config.CheckPKG('ncurses'):
        ncurseslibs = pkg_config('ncurses')
    elif config.CheckExecutable('ncurses5-config --version', 'ncurses5-config'):
        ncurseslibs = ['!ncurses5-config --libs --cflags']
    elif sys.platform.startswith('freebsd'):
        ncurseslibs= [ '-lncurses' ]
    elif sys.platform.startswith('openbsd'):
        ncurseslibs= [ '-lcurses' ]

if env['usb']:
    # In FreeBSD except version 7, USB libraries are in the base system
    if config.CheckPKG('libusb-1.0'):
        confdefs.append("#define HAVE_LIBUSB 1\n")
        try:
            usblibs = pkg_config('libusb-1.0')
        except OSError:
            announce("pkg_config is confused about the state of libusb-1.0.")
            usblibs = []
    elif sys.platform.startswith("freebsd"):
        confdefs.append("#define HAVE_LIBUSB 1\n")
        usblibs = [ "-lusb"]
    else:
        confdefs.append("/* #undef HAVE_LIBUSB */\n")
        usblibs = []
else:
    confdefs.append("/* #undef HAVE_LIBUSB */\n")
    usblibs = []

if config.CheckLib('librt'):
    confdefs.append("#define HAVE_LIBRT 1\n")
    # System library - no special flags
    rtlibs = ["-lrt"]
else:
    confdefs.append("/* #undef HAVE_LIBRT */\n")
    rtlibs = []

if env['dbus_export'] and config.CheckPKG('dbus-1'):
    confdefs.append("#define HAVE_DBUS 1\n")
    dbus_libs = pkg_config('dbus-1')
else:
    confdefs.append("/* #undef HAVE_DBUS */\n")
    dbus_libs = []

if env['bluez'] and config.CheckPKG('bluez'):
    confdefs.append("#define HAVE_BLUEZ 1\n")
    bluezlibs = pkg_config('bluez')
else:
    confdefs.append("/* #undef HAVE_BLUEZ */\n")
    bluezlibs = []

if config.CheckHeader("sys/timepps.h"):
    confdefs.append("#define HAVE_SYS_TIMEPPS_H 1\n")
    announce("You have kernel PPS available.")
else:
    confdefs.append("/* #undef HAVE_SYS_TIMEPPS_H */\n")
    announce("You do not have kernel PPS available.")

# check function after libraries, because some function require library
# for example clock_gettime() require librt on Linux
for f in ("daemon", "strlcpy", "strlcat", "clock_gettime"):
    if config.CheckFunc(f):
        confdefs.append("#define HAVE_%s 1\n" % f.upper())
    else:
        confdefs.append("/* #undef HAVE_%s */\n" % f.upper())

# Map options to libraries required to support them that might be absent.
optionrequires = {
    "bluez": ["libbluetooth"],
    "dbus_export" : ["libdbus-1"],
    }

keys = map(lambda x: (x[0],x[2]), boolopts) + map(lambda x: (x[0],x[2]), nonboolopts) + map(lambda x: (x[0],x[2]), pathopts)
keys.sort()
for (key,help) in keys:
    value = env[key]
    if value and key in optionrequires:
        for required in optionrequires[key]:
            if not config.CheckLib(required):
                announce("%s not found, %s cannot be enabled." % (required,key))
                value = False
                break

    confdefs.append("/* %s */" % help)
    if type(value) == type(True):
        if value:
            confdefs.append("#define %s_ENABLE 1\n" % key.upper())
        else:
            confdefs.append("/* #undef %s_ENABLE */\n" % key.upper())
    elif value in (0, "", "(undefined)"):
        confdefs.append("/* #undef %s */\n" % key.upper())
    else:
        if value.isdigit():
            confdefs.append("#define %s %s\n" % (key.upper(), value))
        else:
            confdefs.append("#define %s \"%s\"\n" % (key.upper(), value))


confdefs.append('''
/* will not handle pre-Intel Apples that can run big-endian */
#if defined __BIG_ENDIAN__
#define WORDS_BIGENDIAN 1
#else
#undef WORDS_BIGENDIAN
#endif

/* Some libcs do not have strlcat/strlcpy. Local copies are provided */
#ifndef HAVE_STRLCAT
# ifdef __cplusplus
extern "C" {
# endif
size_t strlcat(/*@out@*/char *dst, /*@in@*/const char *src, size_t size);
# ifdef __cplusplus
}
# endif
#endif
#ifndef HAVE_STRLCPY
# ifdef __cplusplus
extern "C" {
# endif
size_t strlcpy(/*@out@*/char *dst, /*@in@*/const char *src, size_t size);
# ifdef __cplusplus
}
# endif
#endif

#define GPSD_CONFIG_H
''')


manbuilder = mangenerator = htmlbuilder = None
if config.CheckXsltproc():
    mangenerator = 'xsltproc'
    build = "xsltproc --nonet %s $SOURCE >$TARGET"
    htmlbuilder = build % docbook_html_uri
    manbuilder = build % docbook_man_uri
elif WhereIs("xmlto"):
    mangenerator = 'xmlto'
    htmlbuilder = "xmlto html-nochunks $SOURCE; mv `basename $TARGET` $TARGET"
    manbuilder = "xmlto man $SOURCE; mv `basename $TARGET` $TARGET"
else:
    announce("Neither xsltproc nor xmlto found, documentation cannot be built.")
if manbuilder:
    env['BUILDERS']["Man"] = Builder(action=manbuilder)
    env['BUILDERS']["HTML"] = Builder(action=htmlbuilder,
                                      src_suffix=".xml", suffix=".html")

qt_network = env['libQgpsmm'] and config.CheckPKG('QtNetwork')

env = config.Finish()

# Be explicit about what we're doing.
changelatch = False 
for (name, default, help) in boolopts + nonboolopts + pathopts:
    if env[name] != env.subst(default):
        if not changelatch:
            announce("Altered configuration variables:")
            changelatch = True
        announce("%s = %s (default %s): %s" % (name, env[name], env.subst(default), help))
if not changelatch:
    announce("All configuration flags are defaulted.")

# Gentoo systems can have a problem with the Python path
if os.path.exists("/etc/gentoo-release"):
    announce("This is a Gentoo system.")
    announce("Adjust your PYTHONPATH to see library directories under /usr/local/lib")

# Should we build the Qt binding?
if cxx and qt_network:
    qt_env = env.Clone()
    qt_env.MergeFlags('-DUSE_QT')
    if qt_network:
        try:
            qt_env.MergeFlags(pkg_config('QtNetwork'))
        except OSError:
            announce("pkg_config is confused about the state of QtNetwork.")
            qt_env = None
else:
    qt_env = None

## Two shared libraries provide most of the code for the C programs

libgps_version_soname = libgps_version_current - libgps_version_age
libgps_version = "%d.%d.%d" %(libgps_version_soname, libgps_version_age, libgps_version_revision)

libgps_sources = [
    "ais_json.c",
    "daemon.c",
    "gpsutils.c",
    "gpsdclient.c",
    "gps_maskdump.c",
    "hex.c",
    "json.c",
    "libgps_core.c",
    "libgps_dbus.c",
    "libgps_json.c",
    "libgps_shm.c",
    "libgps_sock.c",
    "netlib.c",
    "rtcm2_json.c",
    "shared_json.c",
    "strl.c",
]

if cxx and env['libgpsmm']:
    libgps_sources.append("libgpsmm.cpp")

libgpsd_sources = [
    "bits.c",
    "bsd_base64.c",
    "crc24q.c",
    "gpsd_json.c",
    "geoid.c",
    "isgps.c",
    "libgpsd_core.c",
    "net_dgpsip.c",
    "net_gnss_dispatch.c",
    "net_ntrip.c",
    "packet.c",
    "pseudonmea.c",
    "serial.c",
    #"srecord.c",
    "subframe.c",
    "timebase.c",
    "drivers.c",
    "driver_aivdm.c",
    "driver_evermore.c",
    "driver_garmin.c",
    "driver_garmin_txt.c",
    "driver_geostar.c",
    "driver_italk.c",
    "driver_navcom.c",
    "driver_nmea.c",
    "driver_oncore.c",
    "driver_rtcm2.c",
    "driver_rtcm3.c",
    "driver_sirf.c",
    "driver_superstar2.c",
    "driver_tsip.c",
    "driver_ubx.c",
    "driver_zodiac.c",
]

# Cope with scons's failure to set SONAME in its builtins.
# Inspired by Richard Levitte's (slightly buggy) code at
# http://markmail.org/message/spttz3o4xrsftofr

def VersionedSharedLibrary(env, libname, libgps_version, lib_objs=[], parse_flags=[]):
    platform = env.subst('$PLATFORM')
    shlib_pre_action = None
    shlib_suffix = env.subst('$SHLIBSUFFIX')
    shlib_post_action = None
    shlink_flags = SCons.Util.CLVar(env.subst('$SHLINKFLAGS'))

    if platform == 'posix':
        ilib_suffix = shlib_suffix + '.' + libgps_version
        (major, age, revision) = libgps_version.split(".")
        soname = "lib" + libname + shlib_suffix + "." + major
        shlink_flags += [ '-Wl,-Bsymbolic', '-Wl,-soname=%s' % soname ]
    elif platform == 'cygwin':
        ilib_suffix = shlib_suffix
        shlink_flags += [ '-Wl,-Bsymbolic',
                          '-Wl,--out-implib,${TARGET.base}.a' ]
    elif platform == 'darwin':
        ilib_suffix = '.' + libgps_version + shlib_suffix
        shlink_flags += [ '-current_version', '%s' % libgps_version,
                          '-compatibility_version', '%s' % libgps_version,
                          '-undefined', 'dynamic_lookup' ]

    ilib = env.SharedLibrary(libname,lib_objs,
                            SHLIBSUFFIX=ilib_suffix,
                            SHLINKFLAGS=shlink_flags, parse_flags=parse_flags)

    if platform == 'darwin':
        if libgps_version.count(".") != 2:
            # We need a library name in libfoo.x.y.z.dylib form to proceed
            raise ValueError
        lib = 'lib' + libname + '.' + libgps_version + '.dylib'
        lib_no_ver = 'lib' + libname + '.dylib'
        # Link libfoo.x.y.z.dylib to libfoo.dylib
        env.AddPostAction(ilib, 'rm -f %s; ln -s %s %s' % (
            lib_no_ver, lib, lib_no_ver))
        env.Clean(lib, lib_no_ver)
    elif platform == 'posix':
        if libgps_version.count(".") != 2:
            # We need a library name in libfoo.so.x.y.z form to proceed
            raise ValueError
        lib = "lib" + libname + ".so." + libgps_version
        suffix_re = '%s\\.[0-9\\.]*$' % re.escape(shlib_suffix)
        # For libfoo.so.x.y.z, links libfoo.so libfoo.so.x.y libfoo.so.x
        major_name = shlib_suffix + "." + lib.split(".")[2]
        minor_name = major_name + "." + lib.split(".")[3]
        for linksuffix in [shlib_suffix, major_name, minor_name]:
            linkname = re.sub(suffix_re, linksuffix, lib)
            env.AddPostAction(ilib, 'rm -f %s; ln -s %s %s' % (
                linkname, lib, linkname))
            env.Clean(lib, linkname)

    return ilib

def VersionedSharedLibraryInstall(env, destination, libs):
    platform = env.subst('$PLATFORM')
    shlib_suffix = env.subst('$SHLIBSUFFIX')
    ilibs = env.Install(destination, libs)
    if platform == 'posix':
        suffix_re = '%s\\.[0-9\\.]*$' % re.escape(shlib_suffix)
        for lib in map(str, libs):
            if lib.count(".") != 4:
                # We need a library name in libfoo.so.x.y.z form to proceed
                raise ValueError
            # For libfoo.so.x.y.z, links libfoo.so libfoo.so.x.y libfoo.so.x
            major_name = shlib_suffix + "." + lib.split(".")[2]
            minor_name = major_name + "." + lib.split(".")[3]
            for linksuffix in [shlib_suffix, major_name, minor_name]:
                linkname = re.sub(suffix_re, linksuffix, lib)
                env.AddPostAction(ilibs, 'cd %s; rm -f %s; ln -s %s %s' % (destination, linkname, lib, linkname))
            env.Clean(lib, linkname)
    return ilibs

if not env["shared"]:
    def Library(env, target, sources, version, parse_flags=[]):
        return env.StaticLibrary(target, sources, parse_flags=parse_flags)
    LibraryInstall = lambda env, libdir, sources: env.Install(libdir, sources)
else:
    def Library(env, target, sources, version, parse_flags=[]):
        return VersionedSharedLibrary(env=env,
                                     libname=target,
                                     libgps_version=version,
                                     lib_objs=sources,
                                     parse_flags=parse_flags)
    LibraryInstall = lambda env, libdir, sources: \
                     VersionedSharedLibraryInstall(env, libdir, sources)

# Klugery to handle sonames ends

compiled_gpslib = Library(env=env,
                          target="gps",
                          sources=libgps_sources,
                          version=libgps_version,
                          parse_flags= ["-lm"] + dbus_libs)
env.Clean(compiled_gpslib, "gps_maskdump.c")

compiled_gpsdlib = Library(env=env,
                           target="gpsd",
                           sources=libgpsd_sources,
                           version=libgps_version,
                           parse_flags=usblibs + rtlibs + bluezlibs)

libraries = [compiled_gpslib, compiled_gpsdlib]

if qt_env:
    qtobjects = []
    qt_flags = qt_env['CFLAGS']
    for c_only in ('-Wmissing-prototypes', '-Wstrict-prototypes'):
        if c_only in qt_flags:
            qt_flags.remove(c_only)
    # Qt binding object files have to be renamed as they're built to avoid
    # name clashes with the plain non-Qt object files. This prevents the
    # infamous "Two environments with different actions were specified
    # for the same target" error.
    for src in libgps_sources:
        if src in ("gpsutils.c", "libgps_sock.c"):
            compile_with = qt_env['CXX']
            compile_flags = qt_flags
        else:
            compile_with = qt_env['CC']
            compile_flags = qt_env['CFLAGS']
        qtobjects.append(qt_env.SharedObject(src.split(".")[0] + '-qt', src,
                                             CC=compile_with,
                                             CFLAGS=compile_flags,
                                             parse_flags=dbus_libs))
    compiled_qgpsmmlib = Library(qt_env, "Qgpsmm", qtobjects, libgps_version)
    libraries.append(compiled_qgpsmmlib)

# The libraries have dependencies on system libraries

gpslibs = ["-lgps"]
gpsdlibs = ["-lgpsd"] + usblibs + bluezlibs + gpslibs

# Source groups

gpsd_sources = ['gpsd.c','ntpshm.c','shmexport.c','dbusexport.c']

if env['systemd']:
    gpsd_sources.append("sd_socket.c")

gpsmon_sources = [
    'gpsmon.c',
    'monitor_italk.c',
    'monitor_nmea.c',
    'monitor_oncore.c',
    'monitor_sirf.c',
    'monitor_superstar2.c',
    'monitor_tnt.c',
    'monitor_ubx.c',
    'monitor_garmin.c',
    ]

## Production programs

# FIXME: What we really want is for libm to be linked when libgps is.
# VersionedSharedLibrary accomplishes this for its case, but we don't
# know how to force it when linking staticly.
#
# It turns out there are two cases where we need to force this.  Some
# distributions don't do implicit linking by design.  See the test
# code for implicit_link.
#
if not env['shared'] or not env["implicit_link"]:
    env.MergeFlags("-lm")

gpsd_env = env.Clone()
gpsd_env.MergeFlags("-pthread")

gpsd = gpsd_env.Program('gpsd', gpsd_sources,
                        parse_flags = gpsdlibs + dbus_libs)
env.Depends(gpsd, [compiled_gpsdlib, compiled_gpslib])

gpsdecode = env.Program('gpsdecode', ['gpsdecode.c'], parse_flags=gpsdlibs)
env.Depends(gpsdecode, [compiled_gpsdlib, compiled_gpslib])

gpsctl = env.Program('gpsctl', ['gpsctl.c'], parse_flags=gpsdlibs)
env.Depends(gpsctl, [compiled_gpsdlib, compiled_gpslib])

gpsdctl = env.Program('gpsdctl', ['gpsdctl.c'], parse_flags=gpslibs)
env.Depends(gpsdctl, compiled_gpslib)

gpsmon = env.Program('gpsmon', gpsmon_sources,
                     parse_flags=gpsdlibs + ncurseslibs)
env.Depends(gpsmon, [compiled_gpsdlib, compiled_gpslib])

gpspipe = env.Program('gpspipe', ['gpspipe.c'], parse_flags=gpslibs)
env.Depends(gpspipe, compiled_gpslib)

gpxlogger = env.Program('gpxlogger', ['gpxlogger.c'], parse_flags=gpslibs)
env.Depends(gpxlogger, compiled_gpslib)

lcdgps = env.Program('lcdgps', ['lcdgps.c'], parse_flags=gpslibs)
env.Depends(lcdgps, compiled_gpslib)

cgps = env.Program('cgps', ['cgps.c'], parse_flags=gpslibs + ncurseslibs)
env.Depends(cgps, compiled_gpslib)

binaries = [gpsd, gpsdecode, gpsctl, gpsdctl, gpspipe, gpxlogger, lcdgps]
if ncurseslibs:
    binaries += [cgps, gpsmon]

clockwatcher = env.Program('clockwatcher', ['clockwatcher.c'], parse_flags=gpslibs)
env.Depends(clockwatcher, compiled_gpslib)

# Test programs
test_float = env.Program('test_float', ['test_float.c'])
test_geoid = env.Program('test_geoid', ['test_geoid.c'], parse_flags=gpsdlibs)
env.Depends(test_geoid, compiled_gpsdlib)
test_json = env.Program('test_json', ['test_json.c'], parse_flags=gpslibs)
env.Depends(test_json, compiled_gpslib)
test_mkgmtime = env.Program('test_mkgmtime', ['test_mkgmtime.c'], parse_flags=gpslibs)
env.Depends(test_mkgmtime, compiled_gpslib)
test_trig = env.Program('test_trig', ['test_trig.c'], parse_flags=["-lm"])
test_packet = env.Program('test_packet', ['test_packet.c'], parse_flags=gpsdlibs)
env.Depends(test_packet, [compiled_gpsdlib, compiled_gpslib])
test_bits = env.Program('test_bits', ['test_bits.c'], parse_flags=gpsdlibs)
env.Depends(test_bits, [compiled_gpsdlib, compiled_gpslib])
test_gpsmm = env.Program('test_gpsmm', ['test_gpsmm.cpp'], parse_flags=gpslibs)
env.Depends(test_gpsmm, compiled_gpslib)
test_libgps = env.Program('test_libgps', ['test_libgps.c'], parse_flags=gpslibs)
env.Depends(test_libgps, compiled_gpslib)
testprogs = [test_float, test_trig, test_bits, test_packet,
             test_mkgmtime, test_geoid, test_libgps]
if env['socket_export']:
    testprogs.append(test_json)
if cxx and env["libgpsmm"]:
    testprogs.append(test_gpsmm)

# Python programs
if not env['python']:
    python_built_extensions = []
else:
    python_progs = ["gpscat", "gpsfake", "gpsprof", "xgps", "xgpsspeed", "gegps"]
    python_modules = Glob('gps/*.py') 

    # Build Python binding
    #
    python_extensions = {
        "gps" + os.sep + "packet" : ["gpspacket.c", "packet.c", "isgps.c",
                                        "driver_rtcm2.c", "strl.c", "hex.c", "crc24q.c"],
        "gps" + os.sep + "clienthelpers" : ["gpsclient.c", "geoid.c", "gpsdclient.c", "strl.c"]
    }

    python_env = env.Clone()
    vars = sysconfig.get_config_vars('CC', 'CXX', 'OPT', 'BASECFLAGS', 'CCSHARED', 'LDSHARED', 'SO', 'INCLUDEPY')
    for i in range(len(vars)):
        if vars[i] is None:
            vars[i] = ""
    (cc, cxx, opt, basecflags, ccshared, ldshared, so_ext, includepy) = vars
    # in case CC/CXX was set to the scan-build wrapper,
    # ensure that we build the python modules with scan-build, too
    if env['CC'] is None or env['CC'].find('scan-build') < 0:
        python_env['CC'] = cc
    else:
        python_env['CC'] = ' '.join([env['CC']] + cc.split()[1:])
    if env['CXX'] is None or env['CXX'].find('scan-build') < 0:
        python_env['CXX'] = cxx
    else:
        python_env['CXX'] = ' '.join([env['CXX']] + cxx.split()[1:])

    python_env['SHLINKFLAGS'] = []
    python_env['SHLINK'] = ldshared
    python_env['SHLIBPREFIX']=""
    python_env['SHLIBSUFFIX']=so_ext
    python_env['CPPPATH'] =[includepy]
    python_env['CPPFLAGS']=basecflags + " " + opt
    python_objects={}
    python_compiled_libs = {}
    for ext, sources in python_extensions.iteritems():
        python_objects[ext] = []
        for src in sources:
            python_objects[ext].append(
                python_env.NoCache(
                    python_env.SharedObject(
                        src.split(".")[0] + '-py_' + '_'.join(['%s' %(x) for x in sys.version_info]) + so_ext, src
                    )
                )
            )
        python_compiled_libs[ext] = python_env.SharedLibrary(ext, python_objects[ext])
    python_built_extensions = python_compiled_libs.values()

    python_egg_info_source = """Metadata-Version: 1.0
Name: gps
Version: %s
Summary: Python libraries for the gpsd service daemon
Home-page: %s
Author: the GPSD project
Author-email: %s
License: BSD
Description: The gpsd service daemon can monitor one or more GPS devices connected to a host computer, making all data on the location and movements of the sensors available to be queried on TCP port 2947.
Platform: UNKNOWN
""" %(gpsd_version, website, devmail)
    python_egg_info = python_env.Textfile(target="gps-%s.egg-info" %(gpsd_version, ), source=python_egg_info_source)

env.Command(target = "packet_names.h", source="packet_states.h", action="""
    rm -f $TARGET &&\
    sed -e '/^ *\([A-Z][A-Z0-9_]*\),/s//   \"\\1\",/' <$SOURCE >$TARGET &&\
    chmod a-w $TARGET""")

# build timebase.h
def timebase_h(target, source, env):
    from leapsecond import make_leapsecond_include
    with open(target[0].abspath, 'w') as f:
        f.write(make_leapsecond_include(source[0].abspath))
env.Command(target="timebase.h", source="leapseconds.cache",
            action=timebase_h)

env.Textfile(target="gpsd_config.h", source=confdefs)
env.Textfile(target="gpsd.h", source=[File("gpsd.h-head"), File("gpsd_config.h"), File("gpsd.h-tail")])

env.Command(target="gps_maskdump.c", source=["maskaudit.py", "gps.h", "gpsd.h"], action='''
    rm -f $TARGET &&\
        $PYTHON $SOURCE -c $SRCDIR >$TARGET &&\
        chmod a-w $TARGET''')

env.Command(target="ais_json.i", source="jsongen.py", action='''\
    rm -f $TARGET &&\
    $PYTHON $SOURCE --ais --target=parser >$TARGET &&\
    chmod a-w $TARGET''')

# generate revision.h
if 'dev' in gpsd_version:
    (st, rev) = _getstatusoutput('git describe')
    if st != 0:
        from datetime import datetime
        rev = datetime.now().isoformat()[:-4]
else:
    rev = gpsd_version
revision='#define REVISION "%s"\n' %(rev.strip(),)
env.Textfile(target="revision.h", source=[revision])

# generate pps_pin.h
pps_pin = env['pps_pin']
ppsh = '/* generated by scons from the pps_pin option - do not hand-hack */\n'
ppsh += '#define PPS_LINE_NAME "%s"\n' % pps_pin
tioc_map = {"DCD": "CAR"}
ppsh += '#define PPS_LINE_TIOC TIOCM_%s\n' % tioc_map.get(pps_pin, pps_pin)
ppsh += "/* end */\n"
env.NoClean(env.Textfile(target="pps_pin.h", source=[ppsh]))

generated_sources = ['packet_names.h', 'timebase.h', 'gpsd.h', "ais_json.i",
                     'gps_maskdump.c', 'revision.h', 'gpsd.php', 'pps_pin.h']

# leapseconds.cache is a local cache for information on leapseconds issued
# by the U.S. Naval observatory. It gets kept in the repository so we can
# build without Internet access.
from leapsecond import save_leapseconds
leapseconds_cache_rebuild = lambda target, source, env: save_leapseconds(target[0].abspath)
if 'dev' in gpsd_version or not os.path.exists('leapseconds.cache'):
    leapseconds_cache = env.Command(target="leapseconds.cache",
                                source="leapsecond.py",
                                action=leapseconds_cache_rebuild)
    env.Clean(leapseconds_cache, "leapsecond.pyc")
    env.NoClean(leapseconds_cache)
    env.Precious(leapseconds_cache)

# Instantiate some file templates.  We'd like to use the Substfile builtin
# but it doesn't seem to work in scons 1.20
def substituter(target, source, env):
    substmap = (
        ('@VERSION@',    gpsd_version),
        ('@prefix@',     env['prefix']),
        ('@libdir@',     env['libdir']),
        ('@PYTHON@',     sys.executable),
        ('@DATE@',       time.asctime()),
        ('@MASTER',      'DO NOT HAND_HACK! THIS FILE IS GENERATED@'),
        ('@SITENAME@',   sitename),
        ('@SITESEARCH@', sitesearch),
        ('@WEBSITE@',    website),
        ('@MAINPAGE@',   mainpage),
        ('@WEBUPLOAD@',  webupload),
        ('@SCPUPLOAD@',  scpupload),
        ('@MAILMAN@',    mailman),
        ('@ADMIN@',      admin),
        ('@DOWNLOAD@',   download),
        ('@BUGTRACKER@', bugtracker),
        ('@BROWSEREPO@', browserepo),
        ('@CLONEREPO@',  clonerepo),
        ('@GITREPO@',    gitrepo),
        ('@WEBFORM@',    webform),
        ('@FORMSERVER@', formserver),
        ('@DEVMAIL@',    devmail),
        ('@LIBGPSVERSION@', libgps_version),
        )
    with open(str(source[0])) as sfp:
        content = sfp.read()
    for (s, t) in substmap:
        content = content.replace(s, t)
    m = re.search("@[A-Z]+@", content)
    if m and m.group(0) not in map(lambda x: x[0], substmap):
        print >>sys.stderr, "Unknown subst token %s in %s." % (m.group(0), sfp.name)
    with open(str(target[0]), "w") as tfp:
        tfp.write(content)

templated = glob.glob("*.in") + glob.glob("*/*.in") + glob.glob("*/*/*.in")

for fn in templated:
    builder = env.Command(source=fn, target=fn[:-3], action=substituter)
    env.AddPostAction(builder, 'chmod -w $TARGET')
    if fn.endswith(".py.in"):
        env.AddPostAction(builder, 'chmod +x $TARGET')

# Documentation

base_manpages = {
    "gpsd.8" : "gpsd.xml",
    "gpsd_json.5" : "gpsd_json.xml",
    "gps.1" : "gps.xml",
    "cgps.1" : "gps.xml",
    "lcdgps.1" : "gps.xml",
    "libgps.3" : "libgps.xml",
    "libgpsmm.3" : "libgpsmm.xml",
    "libQgpsmm.3" : "libgpsmm.xml",
    "libgpsd.3" : "libgpsd.xml",
    "gpsmon.1": "gpsmon.xml",
    "gpsctl.1" : "gpsctl.xml",
    "gpsdctl.8" : "gpsdctl.xml",
    "gpspipe.1" : "gpspipe.xml",
    "gpsdecode.1" : "gpsdecode.xml",
    "srec.5" : "srec.xml",
    }
python_manpages = {
    "gpsprof.1" : "gpsprof.xml",
    "gpsfake.1" : "gpsfake.xml",
    "gpscat.1" : "gpscat.xml",
    "xgpsspeed.1" : "gps.xml",
    "xgps.1" : "gps.xml",
    "gegps.1" : "gps.xml",
    }

manpage_targets = []
if manbuilder:
    for (man, xml) in base_manpages.items() + python_manpages.items():
        manpage_targets.append(env.Man(source=xml, target=man))

## Where it all comes together

build = env.Alias('build', [libraries, binaries, python_built_extensions, "gpsd.php", manpage_targets])
env.Clean(build,
          map(glob.glob,("*.[oa]", "*.os", "*.os.*", "*.pyc", "gps/*.pyc")) + \
          generated_sources + \
          map(lambda f: f[:-3], templated) + \
          [".sconf_temp"])
env.Default(*build)

if qt_env:
    build_qt = qt_env.Alias('build', [compiled_qgpsmmlib])
    qt_env.Default(*build_qt)

if env['python']:
    build_python = python_env.Alias('build', [python_built_extensions])
    python_env.Default(*build_python)

## Installation and deinstallation

# Not here because too distro-specific: udev rules, desktop files, init scripts

# It's deliberate that we don't install gpsd.h. It's full of internals that
# third-party client programs should not see.
headerinstall = [ env.Install(installdir('includedir'), x) for x in ("libgpsmm.h", "gps.h")]

binaryinstall = []
binaryinstall.append(env.Install(installdir('sbindir'), [gpsd, gpsdctl]))
binaryinstall.append(env.Install(installdir('bindir'),  [gpsdecode, gpsctl, gpspipe, gpxlogger, lcdgps]))
if ncurseslibs:
    binaryinstall.append(env.Install(installdir('bindir'), [cgps, gpsmon]))
binaryinstall.append(LibraryInstall(env, installdir('libdir'), compiled_gpslib))
binaryinstall.append(LibraryInstall(env, installdir('libdir'), compiled_gpsdlib))
if qt_env:
    binaryinstall.append(LibraryInstall(qt_env, installdir('libdir'), compiled_qgpsmmlib))

# We don't use installdir here in order to avoid having DESTDIR affect the rpath
if env["shared"]:
    env.AddPostAction(binaryinstall, '$CHRPATH -r "%s" "$TARGET"' \
                      % (installdir('libdir', False), ))

if not env['debug'] and not env['profiling'] and env['strip']:
    env.AddPostAction(binaryinstall, '$STRIP $TARGET')

if not env['python']:
    python_install = []
else:
    python_lib_dir = sysconfig.get_python_lib(plat_specific=1)
    python_module_dir = python_lib_dir + os.sep + 'gps'
    python_extensions_install = python_env.Install( DESTDIR + python_module_dir,
                                                    python_built_extensions)
    if not env['debug'] or env['profiling']:
        python_env.AddPostAction(python_extensions_install, '$STRIP $TARGET')

    python_modules_install = python_env.Install( DESTDIR + python_module_dir,
                                                python_modules)

    python_progs_install = python_env.Install(installdir('bindir'), python_progs)

    python_egg_info_install = python_env.Install(DESTDIR + python_lib_dir,
                                                 python_egg_info)
    python_install = [  python_extensions_install,
                        python_modules_install,
                        python_progs_install,
                        python_egg_info_install]

pc_install = [ env.Install(installdir('pkgconfig'), x) for x in ("libgps.pc", "libgpsd.pc") ]
if qt_env:
    pc_install.append(qt_env.Install(installdir('pkgconfig'), 'Qgpsmm.pc'))
    pc_install.append(qt_env.Install(installdir('libdir'), 'libQgpsmm.prl'))


maninstall = []
if manbuilder:
    for manpage in base_manpages.keys() + python_manpages.keys():
        section = manpage.split(".")[1]
        dest = os.path.join(installdir('mandir'), "man"+section, manpage)
        maninstall.append(env.InstallAs(source=manpage, target=dest))
install = env.Alias('install', binaryinstall + maninstall + python_install + pc_install + headerinstall)

def Uninstall(nodes):
    deletes = []
    for node in nodes:
        if node.__class__ == install[0].__class__:
            deletes.append(Uninstall(node.sources))
        else:
            deletes.append(Delete(str(node)))
    return deletes
uninstall = env.Command('uninstall', '', Flatten(Uninstall(Alias("install"))) or "")
env.AlwaysBuild(uninstall)
env.Precious(uninstall)

# Utility productions

def Utility(target, source, action):
    target = env.Command(target=target, source=source, action=action)
    env.AlwaysBuild(target)
    env.Precious(target)
    return target

# Report splint warnings
# Note: test_bits.c is unsplintable because of the PRI64 macros.
env['SPLINTOPTS'] = "-I/usr/include/libusb-1.0 +quiet"

def Splint(target,sources, description, params):
    return Utility(target,sources+generated_sources,[
            '@echo "Running splint on %s..."'%description,
            '-splint $SPLINTOPTS %s %s'%(" ".join(params)," ".join(sources)),
            ])

splint_table = [
    ('splint-daemon',gpsd_sources,'daemon', ['-exportlocal', '-redef']),
    ('splint-libgpsd',libgpsd_sources,'libgpsd', ['-exportlocal', '-redef']),
    ('splint-libgps',libgps_sources,'user-side libraries', ['-exportlocal',
                                                            '-fileextensions',
                                                            '-redef']),
    ('splint-cgps',['cgps.c'],'cgps', ['-exportlocal']),
    ('splint-gpsctl',['gpsctl.c'],'gpsctl', ['']),
    ('splint-gpsdctl',['gpsdctl.c'],'gpsdctl', ['']),
    ('splint-gpsmon',gpsmon_sources,'gpsmon', ['-exportlocal']),
    ('splint-gpspipe',['gpspipe.c'],'gpspipe', ['']),
    ('splint-gpsdecode',['gpsdecode.c'],'gpsdecode', ['']),
    ('splint-gpxlogger',['gpxlogger.c'],'gpxlogger', ['']),
    ('splint-test_packet',['test_packet.c'],'test_packet test harness', ['']),
    ('splint-test_mkgmtime',['test_mkgmtime.c'],'test_mkgmtime test harness', ['']),
    ('splint-test_geoid',['test_geoid.c'],'test_geoid test harness', ['']),
    ('splint-test_json',['test_json.c'],'test_json test harness', ['']),
    ]

for (target,sources,description,params) in splint_table:
    env.Alias('splint',Splint(target,sources,description,params))

Utility("cppcheck", ["gpsd.h", "packet_names.h"],
        "cppcheck --template gcc --all --force $SRCDIR")

# Sanity-check Python code. TODO: add xgps for the complete set.
Utility("pychecker", ["jsongen.py", "maskaudit.py"],
        ['''for f in  gpsprof gpscat gpsfake gegps; do ln -s $$f $$f.py; done; \
        pychecker --no-classattr --no-callinit jsongen.py leapsecond.py maskaudit.py gpsprof.py gpscat.py gpsfake.py gegps.py gps/*.py;
        for f in gpsprof gpscat gpsfake gegps; do rm $$f.py $$f.pyc; done'''])
Utility("pylint", ["jsongen.py", "maskaudit.py"],
        ['''pylint --output-format=parseable --reports=n --include-ids=y --disable=F0001,C0103,C0111,C0301,C0302,C0322,C0324,C0323,C0321,R0201,R0902,R0903,R0904,R0911,R0912,R0913,R0914,R0915,W0201,W0401,W0141,W0142,W0603,W0614,W0621,E1101,E1102 jsongen.py leapsecond.py maskaudit.py gpsprof.py gpscat.py gpsfake.py gegps.py gps/*.py xgps'''])

# Check the documentation for bogons, too
Utility("xmllint", glob.glob("*.xml"),
    "for xml in $SOURCES; do xmllint --nonet --noout --valid $$xml; done")

# Use deheader to remove headers not required.  If the statistics line
# ends with other than '0 removed' there's work to be done.
Utility("deheader", generated_sources, [
    'deheader -x cpp -x contrib -x gpspacket.c -x gpsclient.c -x monitor_proto.c -i gpsd_config.h -i gpsd.h -m "MORECFLAGS=\'-Werror -Wfatal-errors -DDEBUG -DPPS_ENABLE\' scons -Q"',
        ])

# Perform all sanity checks.
audit = env.Alias('audit',
                  ['splint',
                   'cppcheck',
                   'xmllint',
                   'valgrind-audit',
                   ])

#
# Regression tests begin here
#
# Note that the *-makeregress targets re-create the *.log.chk source
# files from the *.log source files.

# Check that all Python modules compile properly 
if env['python']:
    def check_compile(target, source, env):
        for pyfile in source:
            'cp %s tmp.py'%(pyfile)
            '%s -tt -m py_compile tmp.py' %(sys.executable, )
            'rm -f tmp.py tmp.pyc'
    python_compilation_regress = Utility('python-compilation-regress',
            Glob('*.py') + python_modules + python_progs + ['SConstruct'], check_compile)
else:
    python_compilation_regress = None

# Regression-test the daemon
gps_regress = Utility("gps-regress", [gpsd, python_built_extensions],
        '$SRCDIR/regress-driver test/daemon/*.log')

# Test that super-raw mode works. Compare each logfile against itself
# dumped through the daemon running in R=2 mode.  (This test is not
# included in the normal regressions.)
Utility("raw-regress", [gpsd, python_built_extensions],
    '$SRCDIR/regress-driver test/daemon/*.log')

# Build the regression tests for the daemon.
Utility('gps-makeregress', [gpsd, python_built_extensions],
    '$SRCDIR/regress-driver -b test/daemon/*.log')

# To build an individual test for a load named foo.log, put it in
# test/daemon and do this:
#    regress-driver -b test/daemon/foo.log

# Regression-test the RTCM decoder.
rtcm_regress = Utility('rtcm-regress', [gpsdecode], [
    '@echo "Testing RTCM decoding..."',
    'for f in $SRCDIR/test/*.rtcm2; do '
        'echo "Testing $${f}..."; '
        'TMPFILE=`mktemp -t gpsd-test-XXXXXXXXXXXXXX.chk`; '
        '$SRCDIR/gpsdecode -j <$${f} >$${TMPFILE}; '
        'diff -ub $${f}.chk $${TMPFILE}; '
        'rm -f $${TMPFILE}; '
    'done;',
    '@echo "Testing idempotency of JSON dump/decode for RTCM2"',
    'TMPFILE=`mktemp -t gpsd-test-XXXXXXXXXXXXXX.chk`; '
    '$SRCDIR/gpsdecode -e -j <test/synthetic-rtcm2.json >$${TMPFILE}; '
        'grep -v "^#" test/synthetic-rtcm2.json | diff -ub - $${TMPFILE}; '
        'rm -f $${TMPFILE}; ',
        ])

# Rebuild the RTCM regression tests.
Utility('rtcm-makeregress', [gpsdecode], [
    'for f in $SRCDIR/test/*.rtcm2; do '
        '$SRCDIR/gpsdecode -j < ${f} > ${f}.chk; '
    'done'
        ])

# Regression-test the AIVDM decoder.
aivdm_regress = Utility('aivdm-regress', [gpsdecode], [
    '@echo "Testing AIVDM decoding..."',
    'for f in $SRCDIR/test/*.aivdm; do '
        'echo "Testing $${f}..."; '
        'TMPFILE=`mktemp -t gpsd-test-XXXXXXXXXXXXXX.chk`; '
        '$SRCDIR/gpsdecode -u -c <$${f} >$${TMPFILE}; '
        'diff -ub $${f}.chk $${TMPFILE}; '
        'rm -f $${TMPFILE}; '
    'done;',
    '@echo "Testing idempotency of JSON dump/decode for AIS"',
    'TMPFILE=`mktemp -t gpsd-test-XXXXXXXXXXXXXX.chk`; '
    '$SRCDIR/gpsdecode -e -j <$SRCDIR/test/synthetic-ais.json >$${TMPFILE}; '
        'grep -v "^#" $SRCDIR/test/synthetic-ais.json | diff -ub - $${TMPFILE}; '
        'rm -f $${TMPFILE}; ',
        ])

# Rebuild the AIVDM regression tests.
Utility('aivdm-makeregress', [gpsdecode], [
    'for f in $SRCDIR/test/*.aivdm; do '
        '$SRCDIR/gpsdecode -u -c <$${f} > $${f}.chk; '
    'done',
        ])

# Regression-test the packet getter.
packet_regress = Utility('packet-regress', [test_packet], [
    '@echo "Testing detection of invalid packets..."',
    '$SRCDIR/test_packet | diff -u $SRCDIR/test/packet.test.chk -',
    ])

# Rebuild the packet-getter regression test
Utility('packet-makeregress', [test_packet], [
    '$SRCDIR/test_packet >$SRCDIR/test/packet.test.chk',
    ])

# Rebuild the geoid test
Utility('geoid-makeregress', [test_geoid], [
    '$SRCDIR/test_geoid 37.371192 122.014965 >$SRCDIR/test/geoid.test.chk'])

# Regression-test the geoid tester.
geoid_regress = Utility('geoid-regress', [test_geoid], [
    '@echo "Testing the geoid model..."',
    '$SRCDIR/test_geoid 37.371192 122.014965 | diff -u $SRCDIR/test/geoid.test.chk -',
    ])

# Regression-test the Maidenhead Locator
maidenhead_locator_regress = Utility('maidenhead-locator-regress', [], [
    '@echo "Testing the Maidenhead Locator conversion..."',
    '$SRCDIR/test_maidenhead.py >/dev/null',
    ])

# Regression-test the calendar functions
time_regress = Utility('time-regress', [test_mkgmtime], [
    '$SRCDIR/test_mkgmtime'
    ])

# Regression test the unpacking code in libgps
unpack_regress = Utility('unpack-regress', [test_libgps], [
    '@echo "Testing the client-library sentence decoder..."',
    '$SRCDIR/regress-driver -c $SRCDIR/test/clientlib/*.log',
    ])

# Build the regression test for the sentence unpacker
Utility('unpack-makeregress', [test_libgps], [
    '@echo "Rebuilding the client sentence-unpacker tests..."',
    '$SRCDIR/regress-driver -c -b $SRCDIR/test/clientlib/*.log'
    ])

# Unit-test the JSON parsing
json_regress = Utility('json-regress', [test_json], [
    '$SRCDIR/test_json'
    ])

# Unit-test the bitfield extractor - not in normal tests
bits_regress = Utility('bits-regress', [test_bits], [
    '$SRCDIR/test_bits'
    ])

# Run a valgrind audit on the daemon  - not in normal tests
valgrind_audit = Utility('valgrind-audit', ['valgrind-audit.py'], 'valgrind-audit.py')

# Run test builds on remote machines
flocktest = Utility("flocktest", [], "cd devtools; flocktest " + gitrepo)

# Run all normal regression tests
check = env.Alias('check', [
    python_compilation_regress,
    gps_regress,
    rtcm_regress,
    aivdm_regress,
    packet_regress,
    geoid_regress,
    maidenhead_locator_regress,
    time_regress,
    unpack_regress,
    json_regress,
    ])

env.Alias('testregress', check)

# The website directory
#
# None of these productions are fired by default.
# The content they handle is the GPSD website, not included in release tarballs.

webpages = Split('''www/installation.html
    www/gpscat.html www/gpsctl.html www/gpsdecode.html 
    www/gpsd.html www/gpsd_json.html www/gpsfake.html www/gpsmon.html 
    www/gpspipe.html www/gpsprof.html www/gps.html 
    www/libgpsd.html www/libgpsmm.html www/libgps.html
    www/srec.html
    www/AIVDM.html www/NMEA.html
    www/protocol-evolution.html www/protocol-transition.html
    www/client-howto.html www/writing-a-driver.html
    www/hardware.html
    www/performance/performance.html
    www/internals.html
    ''') + map(lambda f: f[:-3], glob.glob("www/*.in"))

www = env.Alias('www', webpages)

# Paste 'scons --quiet validation-list' to a batch validator such as
# http://htmlhelp.com/tools/validator/batch.html.en 
def validation_list(target, source, env):
    for page in glob.glob("www/*.html"):
        if not '-head' in page:
            with open(page) as fp:
                if "Valid HTML" in fp.read():
                    print os.path.join(website, os.path.basename(page))
Utility("validation-list", [www], validation_list)

# How to update the website
upload_web = Utility("upload_web", [www],
                     ['rsync --exclude="*.in" -avz www/ ' + webupload])

# When the URL declarations change, so must the generated web pages
for fn in glob.glob("www/*.in"):
    env.Depends(fn[:-3], "SConstruct")

# asciidoc documents
if env.WhereIs('asciidoc'):
    env.Command("www/installation.html",
                "INSTALL",
                ["asciidoc -o www/installation.html INSTALL"])
    for stem in ['AIVDM', 'NMEA',
                 'protocol-evolution', 'protocol-transition',
                 'client-howto']:
        env.Command('www/%s.html' % stem, 'www/%s.txt' % stem,    
                    ['asciidoc -a toc -o www/%s.html www/%s.txt' % (stem, stem)])

if htmlbuilder:
    # Manual pages
    for xml in glob.glob("*.xml"):
        env.HTML('www/%s.html' % xml[:-4], xml)
    
    # DocBook documents
    for stem in ['writing-a-driver', 'performance/performance']:
        env.HTML('www/%s.html' % stem, 'www/%s.xml' % stem)

    # The internals manual.
    # Doesn't capture dependencies on the subpages
    env.HTML('www/internals.html', '$SRCDIR/doc/explanation.xml')

# The hardware page
env.Command('www/hardware.html', ['gpscap.py',
                                  'www/hardware-head.html',
                                  'gpscap.ini',
                                  'www/hardware-tail.html'],
            ['(cat www/hardware-head.html; $PYTHON gpscap.py; cat www/hardware-tail.html) >www/hardware.html'])

# The diagram editor dia is required in order to edit the diagram masters
Utility("www/cycle.png", ["www/cycle.dia"], ["dia -e www/cycle.png www/cycle.dia"])

# Experimenting with pydoc.  Not yet fired by any other productions.

if env['python']:
    env.Alias('pydoc', "www/pydoc/index.html")

    # We need to run epydoc with the Python version we built the modules for.
    # So we define our own epydoc instead of using /usr/bin/epydoc
    EPYDOC = "python -c 'from epydoc.cli import cli; cli()'"
    env.Command('www/pydoc/index.html', python_progs + glob.glob("*.py")  + glob.glob("gps/*.py"), [
        'mkdir -p www/pydoc',
        EPYDOC + " -v --html --graph all -n GPSD $SOURCES -o www/pydoc",
            ])

# Productions for setting up and performing udev tests.
#
# Requires root. Do "udev-install", then "tail -f /var/log/syslog" in
# another window, then run 'scons udev-test', then plug and unplug the
# GPS ad libitum.  All is well when you get fix reports each time a GPS
# is plugged in.

Utility('udev-install', '', [
    'mkdir -p ' + DESTDIR + '/lib/udev/rules.d',
    'cp $SRCDIR/gpsd.rules ' + DESTDIR + '/lib/udev/rules.d/25-gpsd.rules',
    'cp $SRCDIR/gpsd.hotplug ' + DESTDIR + '/lib/udev/',
    'chmod a+x ' + DESTDIR + '/lib/udev/gpsd.hotplug',
        ])

Utility('udev-uninstall', '', [
    'rm -f /lib/udev/gpsd.hotplug',
    'rm -f /lib/udev/rules.d/25-gpsd.rules',
        ])

Utility('udev-test', '', [
    '$SRCDIR/gpsd -N -n -F /var/run/gpsd.sock -D 5',
        ])

# Release machinery begins here
#
# We need to be in the actual project repo (i.e. not doing a -Y build)
# for these productions to work.

if os.path.exists("gpsd.c") and os.path.exists(".gitignore"):
    distfiles = _getoutput(r"git ls-files | grep -v '^www/'").split()
    if ".gitignore" in distfiles:
        distfiles.remove(".gitignore")
    distfiles += generated_sources
    distfiles += base_manpages.keys() + python_manpages.keys()
    distfiles.remove("gpsd.h")
    if "packaging/rpm/gpsd.spec" not in distfiles:
        distfiles.append("packaging/rpm/gpsd.spec")

    # How to build a tarball.
    tarball = env.Command('tarball', distfiles, [
        '@tar --transform "s:^:gpsd-${VERSION}/:" -czf gpsd-${VERSION}.tar.gz $SOURCES',
        '@ls -l gpsd-${VERSION}.tar.gz',
        ])
    env.Clean(tarball, ["gpsd-${VERSION}.tar.gz", "packaging/rpm/gpsd.spec"])

    # Make RPM from the specfile in packaging
    Utility('dist-rpm', tarball, 'rpmbuild -ta $SOURCE')

    # Make sure build-from-tarball works.
    testbuild = Utility('testbuild', [tarball], [
        'tar -xzvf gpsd-${VERSION}.tar.gz',
        'cd gpsd-${VERSION}; scons',
        'rm -fr gpsd-${VERSION}',
        ])

    releasecheck = env.Alias('releasecheck', [
        testbuild,
        check,
        audit,
        flocktest,
        ])

    # This is how to ship a release to the hosting site.
    # The chmod copes with the fact that scp will give a
    # replacement the permissions of the *original*...
    upload_release = Utility('upload-release', [tarball], [
            'gpg -b gpsd-${VERSION}.tar.gz',
            'chmod ug=rw,o=r gpsd-${VERSION}.tar.gz gpsd-${VERSION}.tar.gz.sig',
            'scp gpsd-${VERSION}.tar.gz gpsd-${VERSION}.tar.gz.sig ' + scpupload,
            ])

    # How to tag a release
    tag_release = Utility('tag-release', [], [
        'git tag -s -m "Tagged for external release ${VERSION}" release-${VERSION}'
        ])
    upload_tags = Utility('upload-tags', [], ['git push --tags'])

    # Local release preparation. This production will require Internet access,
    # but it doesn't do any uploads or public repo mods.
    #
    # Note that tag_release has to fire early, otherwise the value of REVISION
    # won't be right when revision.h is generated for the tarball. 
    releaseprep = env.Alias("releaseprep",
                            [Utility("distclean", [], ["rm -f revision.h"]),
                             tag_release,
                             tarball])
    # Undo local release preparation
    Utility("undoprep", [], ['rm -f gpsd-${VERSION}.tar.gz;',
                             'git tag -d release-${VERSION};'])

    # All a buildup to this.
    env.Alias("release", [releaseprep,
                          upload_release,
                          upload_tags,
                          upload_web])

# The following sets edit modes for GNU EMACS
# Local Variables:
# mode:python
# End: