summaryrefslogtreecommitdiff
path: root/acprep
blob: 6ec372f8871477fe9d2c796ed4dfbebadb6fcb49 (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
#!/usr/bin/env python3

# acprep, version 3.1
#
# This script simply sets up the compiler and linker flags for all the various
# build permutations I use for testing and profiling.

import inspect
import logging
import logging.handlers
import optparse
import os
import re
import shutil
import sys
import locale

try:
    import hashlib
except:
    import md5

from os.path import *
from stat import *
from subprocess import Popen, PIPE, call

LEVELS = {'DEBUG':    logging.DEBUG,
          'INFO':     logging.INFO,
          'WARNING':  logging.WARNING,
          'ERROR':    logging.ERROR,
          'CRITICAL': logging.CRITICAL}

def which(program):
    def is_exe(fpath):
        return os.path.exists(fpath) and os.access(fpath, os.X_OK)

    def ext_candidates(fpath):
        yield fpath
        for ext in os.environ.get("PATHEXT", "").split(os.pathsep):
            yield fpath + ext

    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, program)
            for candidate in ext_candidates(exe_file):
                if is_exe(candidate):
                    return candidate

    return None

class BoostInfo(object):
    def dependencies(self, system):
        if system in ['darwin-homebrew']:
            return [ 'boost' ]

        if system in ['darwin-macports']:
            return [ 'boost-jam', 'boost', '+python37' ]

        if system in ['centos']:
            return [ 'boost-devel' ]

        elif system in ['ubuntu-jammy', 'ubuntu-focal', 'ubuntu-bionic', 'ubuntu-xenial',
                        'ubuntu-eoan', 'ubuntu-trusty', 'ubuntu-cosmic']:
            return [ 'libboost-dev',
                     'libboost-date-time-dev',
                     'libboost-filesystem-dev',
                     'libboost-iostreams-dev',
                     'libboost-python-dev',
                     'libboost-regex-dev',
                     'libboost-system-dev',
                     'libboost-test-dev',
                     'tzdata' ]

        elif system in [ 'ubuntu-saucy', 'ubuntu-precise']:
            return [ 'autopoint',
                     'libboost-dev',
                     'libboost-test-dev',
                     'libboost-regex-dev',
                     'libboost-date-time-dev',
                     'libboost-filesystem-dev',
                     'libboost-iostreams-dev',
                     'libboost-python-dev' ]

        elif system in ['ubuntu-lucid']:
            return [ 'bjam', 'autopoint',
                     'libboost-dev',
                     'libboost-regex-dev',
                     'libboost-date-time-dev',
                     'libboost-filesystem-dev',
                     'libboost-iostreams-dev',
                     'libboost-python-dev' ]


class CommandLineApp(object):
    "Base class for building command line applications."

    force_exit    = True           # If true, always ends run() with sys.exit()
    log_handler   = None
    boost_major   = "1_52"

    def __init__(self):
        "Initialize CommandLineApp."
        # Create the logger
        self.log = logging.getLogger(os.path.basename(sys.argv[0]))
        ch = logging.StreamHandler()
        formatter = logging.Formatter("%(name)s: %(levelname)s: %(message)s")
        ch.setFormatter(formatter)
        self.log.addHandler(ch)
        self.log_handler = ch

        # Setup the options parser
        usage = 'usage: %prog [OPTIONS...] [ARGS...]'
        op = self.option_parser = optparse.OptionParser(usage = usage,
                                                        conflict_handler = 'resolve')
        op.add_option('', '--debug',
                      action='store_true', dest='debug',
                      default=False, help='show debug messages and pass exceptions')
        op.add_option('-v', '--verbose',
                      action='store_true', dest='verbose',
                      default=False, help='show informational messages')
        op.add_option('-q', '--quiet',
                      action='store_true', dest='quiet',
                      default=False, help='do not show log messages on console')
        op.add_option('', '--log', metavar='FILE',
                      type='string', action='store', dest='logfile',
                      default=False, help='append logging data to FILE')
        op.add_option('', '--loglevel', metavar='LEVEL',
                      type='string', action='store', dest='loglevel',
                      default=False, help='set log level: DEBUG, INFO, WARNING, ERROR, CRITICAL')

        self.options = op.get_default_values()

    def main(self, *args):
        """Main body of your application.

        This is the main portion of the app, and is run after all of the
        arguments are processed.  Override this method to implement the primary
        processing section of your application."""
        pass

    def handleInterrupt(self):
        """Called when the program is interrupted via Control-C or SIGINT.
        Returns exit code."""
        self.log.error('Canceled by user.')
        return 1

    def handleMainException(self):
        "Invoked when there is an error in the main() method."
        if not self.options.debug:
            self.log.exception('Caught exception')
        return 1

    ## INTERNALS (Subclasses should not need to override these methods)

    def run(self):
        """Entry point.

        Process options and execute callback functions as needed.  This method
        should not need to be overridden, if the main() method is defined."""
        # Process the options supported and given
        self.options, main_args = self.option_parser.parse_args(values=self.options)

        if self.options.logfile:
            fh = logging.handlers.RotatingFileHandler(self.options.logfile,
                                                      maxBytes = (1024 * 1024),
                                                      backupCount = 5)
            formatter = logging.Formatter("%(asctime)s - %(levelname)s: %(message)s")
            fh.setFormatter(formatter)
            self.log.addHandler(fh)

        if self.options.quiet:
            self.log.removeHandler(self.log_handler)
            ch = logging.handlers.SysLogHandler()
            formatter = logging.Formatter("%(name)s: %(levelname)s: %(message)s")
            ch.setFormatter(formatter)
            self.log.addHandler(ch)
            self.log_handler = ch

        if self.options.loglevel:
            self.log.setLevel(LEVELS[self.options.loglevel])
        elif self.options.debug:
            self.log.setLevel(logging.DEBUG)
        elif self.options.verbose:
            self.log.setLevel(logging.INFO)

        exit_code = 0
        try:
            # We could just call main() and catch a TypeError, but that would
            # not let us differentiate between application errors and a case
            # where the user has not passed us enough arguments.  So, we check
            # the argument count ourself.
            argspec = inspect.getfullargspec(self.main)
            expected_arg_count = len(argspec[0]) - 1

            if len(main_args) >= expected_arg_count:
                exit_code = self.main(*main_args)
            else:
                self.log.debug('Incorrect argument count (expected %d, got %d)' %
                               (expected_arg_count, len(main_args)))
                self.option_parser.print_help()
                exit_code = 1

        except KeyboardInterrupt:
            exit_code = self.handleInterrupt()

        except SystemExit as msg:
            exit_code = msg.args[0]

        except Exception:
            exit_code = self.handleMainException()
            if self.options.debug:
                raise

        if self.force_exit:
            sys.exit(exit_code)
        return exit_code


class PrepareBuild(CommandLineApp):
    #########################################################################
    # Initialization routines                                               #
    #########################################################################

    def initialize(self):
        self.log.debug('Initializing all state variables')

        self.should_clean    = False
        self.configured      = False
        self.current_ver     = None
        #self.current_flavor  = 'default'
        self.current_flavor  = 'debug'
        self.products_dir    = None
        self.configure_args  = []
        self.CXXFLAGS        = []
        self.LDFLAGS         = []

        self.envvars = {
            'CXX':      '',
            'CXXFLAGS': '',
            'LDFLAGS':  '',
        }

        for varname in self.envvars.keys():
            if varname in os.environ:
                self.envvars[varname] = os.environ[varname]

                if varname.endswith('FLAGS'):
                    self.__dict__[varname] = str.split(os.environ[varname])
                    self.envvars[varname]  = ''

        # If ~/Products/ or build/ exists, use them instead of the source tree
        # for building
        products = self.default_products_directory()
        if (exists(products) and isdir(products)) or \
           (exists('build') and isdir('build')):
            self.options.build_dir = None

    def __init__(self):
        CommandLineApp.__init__(self)
        self.log.setLevel(logging.INFO)

        self.source_dir = os.getcwd()

        op = self.option_parser

        op.add_option('', '--help', action="callback",
                      callback=self.option_help,
                      help='Show this help text')
        op.add_option('-j', '--jobs', metavar='N',
                      type='int', action='store', dest='jobs',
                      default=1, help='Allow N make jobs at once')

        op.add_option('', '--boost', metavar='BOOST_ROOT',
                      action="store", dest="boost_root",
                      help='Set Boost library root (ex: "--boost=/usr/local")')
        op.add_option('', '--boost-suffix', metavar='BOOST_SUFFIX',
                      action="store", dest="boost_suffix",
                      help='Set Boost library suffix (ex: "--boost-suffix=-mt")')
        op.add_option('', '--boost-include', metavar='BOOST_INCLUDE',
                      action="store", dest="boost_include",
                      help='Set Boost include path (ex: "--boost-include=DIR")')

        op.add_option('', '--compiler', metavar='COMPILER',
                      action="store", dest="compiler",
                      help='Use the Clang C++ compiler')
        op.add_option('', '--cxx', metavar='COMPILER',
                      action="store", dest="compiler",
                      help='Use the Clang C++ compiler')

        op.add_option('-N', '--ninja', action='store_true', dest='use_ninja',
                      default=False,
                      help='Use ninja to build, rather than make')
        op.add_option('', '--no-git', action='store_true', dest='no_git',
                      default=False,
                      help='Do not call out to Git; useful for offline builds')
        op.add_option('', '--doxygen', action='store_true',
                      dest='enable_doxygen', default=False,
                      help='Enable use of Doxygen to build ref manual ("make docs")')
        op.add_option('', '--python', action='store_true', dest='python',
                      default=False,
                      help='Enable Python support')
        op.add_option('', '--no-python', action='store_false', dest='python',
                      help='Disable python support (default)')
        op.add_option('', '--prefix', metavar='DIR', action="store",
                      dest="prefix_dir", help='Use custom installation prefix')
        op.add_option('', '--products', metavar='DIR', action="store",
                      dest="option_products",
                      help='Collect all build products in this directory')
        op.add_option('', '--output', metavar='DIR', action="store",
                      default=self.source_dir,
                      dest="build_dir", help='Build in the specified directory')
        op.add_option('', '--local', action="callback",
                      callback=self.option_local,
                      help='Build directly within the source tree (default)')

        self.options = op.get_default_values()

        self.initialize()

    def main(self, *args):
        if args and args[0] in ['default', 'debug', 'opt', 'gcov', 'gprof']:
            self.current_flavor = args[0]
            args = args[1:]

        if args:
            cmd = args[0]
            if 'phase_' + cmd not in PrepareBuild.__dict__:
                self.log.error("Unknown build phase: " + cmd + "\n")
                sys.exit(1)
            else:
                args = args[1:]
        else:
            cmd = 'config'

        self.log.info('Invoking primary phase: ' + cmd)
        PrepareBuild.__dict__['phase_' + cmd](self, *args)

    #########################################################################
    # General utility code                                                  #
    #########################################################################

    def execute(self, *args):
        try:
            self.log.debug('Executing command: ' + ' '.join(args))

            retcode = call(args, shell=False)
            if retcode < 0:
                self.log.error("Child was terminated by signal", -retcode)
                sys.exit(1)
            elif retcode != 0:
                self.log.error("Execution failed: " + ' '.join(args))
                sys.exit(1)
        except OSError as e:
            self.log.error("Execution failed: " + e)
            sys.exit(1)

    def isnewer(self, file1, file2):
        "Check if file1 is newer than file2."
        if not exists(file2):
            return True
        return os.stat(file1)[ST_MTIME] > os.stat(file2)[ST_MTIME]

    #########################################################################
    # Determine information about the surroundings                          #
    #########################################################################

    def prefix_directory(self):
        if self.options.prefix_dir:
            return self.options.prefix_dir
        else:
            return None

    def default_products_directory(self):
        return join(os.environ['HOME'], "Products")

    def products_directory(self):
        if not self.products_dir:
            products = self.default_products_directory()

            if not exists(products) or not isdir(products):
                products = join(self.source_dir, 'build')

            products = join(products, basename(self.source_dir))

            self.products_dir = products

        return self.products_dir

    def build_directory(self):
        if not self.options.build_dir:
            self.options.build_dir = join(self.products_directory(),
                                          self.current_flavor)
        return self.options.build_dir

    def ensure(self, dirname):
        if not exists(dirname):
            self.log.info('Making directory: ' + dirname)
            os.makedirs(dirname)
        elif not isdir(dirname):
            self.log.error('Directory is not a directory: ' + dirname)
            sys.exit(1)
        return dirname

    def git_working_tree(self):
        return exists('.git') and isdir('.git') and not self.options.no_git

    def current_version(self):
        if not self.current_ver:
            major, minor, patch, date = None, None, None, None

            version_m4 = open('CMakeLists.txt', 'r')
            for line in version_m4.readlines():
                match = re.match('^set\(Ledger_VERSION_MAJOR ([0-9]+)\)', line)
                if match:
                    major = match.group(1)
                match = re.match('^set\(Ledger_VERSION_MINOR ([0-9]+)\)', line)
                if match:
                    minor = match.group(1)
                match = re.match('^set\(Ledger_VERSION_PATCH ([0-9]+)\)', line)
                if match:
                    patch = match.group(1)
                match = re.match('^set\(Ledger_VERSION_DATE ([0-9]+)\)', line)
                if match:
                    date = match.group(1)
                    break
            self.current_ver = "%s.%s.%s%s" % (major, minor, patch,
                                               "-%s" % date if date else "")
            version_m4.close()
        return self.current_ver

    def phase_products(self, *args):
        self.log.info('Executing phase: products')
        print(self.products_directory())

    def phase_info(self, *args):
        self.log.info('Executing phase: info')

        environ, conf_args = self.configure_environment()

        self.log.info("Current version          => " + self.current_version())
        self.log.info("Current flavor           => " + self.current_flavor)
        self.log.info("Source directory         => " + self.source_dir)
        if self.prefix_directory():
            self.log.info("Installation prefix      => " + self.prefix_directory())
        self.log.info("Products directory       => " + self.products_directory())
        self.log.info("Build directory          => " + self.build_directory())

        self.log.debug('CMake environment =>')

        keys = environ.keys()
        for key in sorted(keys):
            if key in ['PATH', 'CXX'] or key.endswith('FLAGS'):
                self.log.debug('  %s=%s' % (key, environ[key]))

        self.log.debug('CMake arguments   =>')

        for arg in conf_args + list(args):
            self.log.debug('  %s' % arg)

    def phase_sloc(self, *args):
        self.log.info('Executing phase: sloc')
        self.execute('sloccount', 'src', 'python', 'test')

    #########################################################################
    # Update local files with the latest information                        #
    #########################################################################

    def phase_pull(self, *args):
        self.log.info('Executing phase: pull')
        if self.git_working_tree():
            self.execute('git', 'pull')

    #########################################################################
    # Automatic installation of build dependencies                          #
    #########################################################################

    def phase_dependencies(self, *args):
        self.log.info('Executing phase: dependencies')

        self.log.info("Installing Ledger's build dependencies ...")

        system = os.uname().sysname

        if system == 'Darwin':
            if exists('/opt/local/bin/port'):
                self.log.info('Looks like you are using MacPorts on macOS')
                packages = [
                    'sudo', 'port', 'install', '-f',
                    'automake', 'autoconf', 'libtool',
                    'python37',
                    'libiconv',
                    'zlib',
                    'gmp', 'mpfr',
                    'ncurses',
                    'gettext',
                    'libedit',
                    'texlive-xetex', 'doxygen', 'graphviz', 'texinfo',
                    'lcov', 'sloccount'
                ] + BoostInfo().dependencies('darwin-macports')
                self.log.info('Executing: ' + ' '.join(packages))
                self.execute(*packages)
            elif exists('/usr/local/bin/brew') or exists('/opt/local/bin/brew'):
                self.log.info('Looks like you are using Homebrew on macOS')
                packages = [
                    'brew', 'install',
                    'cmake', 'ninja',
                    'mpfr', 'gmp',
                ] + BoostInfo().dependencies('darwin-homebrew')
                self.log.info('Executing: ' + ' '.join(packages))
                self.execute(*packages)
            elif exists('/sw/bin/fink'):
                self.log.info('Looks like you are using Fink on macOS')
                self.log.error("I don't know the package names for Fink yet!")
                sys.exit(1)

        elif system == 'Linux':
            if exists('/etc/issue'):
                issue = open('/etc/issue')
                issue_name = issue.readline()
                if issue_name.startswith('Ubuntu'):
                    info = dict([line.strip().split('=', 1)
                                 for line in open('/etc/lsb-release')])
                    release = info['DISTRIB_CODENAME']
                    self.log.info('Looks like you are using APT on Ubuntu ' + release)
                    packages = [
                        'sudo', 'apt-get', 'install',
                        'build-essential',
                    ]
                    if release == 'jammy':
                        packages.extend([
                            'doxygen',
                            'cmake',
                            'ninja-build',
                            'zlib1g-dev',
                            'libbz2-dev',
                            'python-dev',
                            'libgmp3-dev',
                            'libmpfr-dev',
                            'gettext',
                            'libedit-dev',
                            'texinfo',
                            'lcov',
                            'libutfcpp-dev',
                            'sloccount'
                        ])
                    elif release == 'focal':
                        packages.extend([
                            'doxygen',
                            'cmake',
                            'ninja-build',
                            'zlib1g-dev',
                            'libbz2-dev',
                            'python-dev',
                            'libgmp3-dev',
                            'libmpfr-dev',
                            'gettext',
                            'libedit-dev',
                            'texinfo',
                            'lcov',
                            'libutfcpp-dev',
                            'sloccount'
                        ])
                    elif release == 'bionic':
                        packages.extend([
                            'doxygen',
                            'cmake',
                            'ninja-build',
                            'zlib1g-dev',
                            'libbz2-dev',
                            'python-dev',
                            'libgmp3-dev',
                            'libmpfr-dev',
                            'gettext',
                            'libedit-dev',
                            'texinfo',
                            'lcov',
                            'libutfcpp-dev',
                            'sloccount'
                        ])
                    elif release == 'trusty':
                        packages.extend([
                            'doxygen',
                            'cmake',
                            'ninja-build',
                            'zlib1g-dev',
                            'libbz2-dev',
                            'python-dev',
                            'libgmp3-dev',
                            'libmpfr-dev',
                            'gettext',
                            'libedit-dev',
                            'texinfo',
                            'lcov',
                            'libutfcpp-dev',
                            'sloccount'
                        ])
                    elif release == 'eoan':
                        packages.extend([
                            'doxygen',
                            'cmake',
                            'ninja-build',
                            'zlib1g-dev',
                            'libbz2-dev',
                            'python-dev',
                            'libgmp3-dev',
                            'libmpfr-dev',
                            'gettext',
                            'libedit-dev',
                            'texinfo',
                            'lcov',
                            'libutfcpp-dev',
                            'sloccount'
                        ])
                    elif release == 'xenial':
                        packages.extend([
                            'doxygen',
                            'cmake',
                            'ninja-build',
                            'zlib1g-dev',
                            'libbz2-dev',
                            'python-dev',
                            'libgmp3-dev',
                            'libmpfr-dev',
                            'gettext',
                            'libedit-dev',
                            'texinfo',
                            'lcov',
                            'libutfcpp-dev',
                            'sloccount'
                        ])
                    elif release == 'saucy':
                        packages.extend([
                            'doxygen',
                            'cmake',
                            'ninja-build',
                            'zlib1g-dev',
                            'libbz2-dev',
                            'python-dev',
                            'libgmp-dev',
                            'libmpfr-dev',
                            'gettext',
                            'libedit-dev',
                            'texinfo',
                            'lcov',
                            'sloccount'
                        ])
                    elif release == 'precise':
                        packages.extend([
                            'libtool',
                            'cmake',
                            'zlib1g-dev',
                            'libbz2-dev',
                            'python-dev',
                            'libgmp-dev',
                            'libmpfr-dev',
                            'gettext',
                            'libedit-dev',
                            'texinfo',
                            'lcov',
                            'libutfcpp-dev',
                            'sloccount'
                        ])
                    else:
                        self.log.info('I do not recognize your version of Ubuntu!')
                        packages = None
                    if packages:
                        packages.extend(
                            BoostInfo().dependencies('ubuntu-' + release))
                        self.log.info('Executing: ' + ' '.join(packages))
                        self.execute(*packages)
                elif issue_name.startswith('Arch Linux'):
                    self.log.info('Looks like you are using Pacman on Arch Linux')
                    packages = [
                        'sudo', 'pacman', '-S', '--noconfirm',
                        'base-devel',
                        'boost',
                        'doxygen',
                        'gmp',
                        'graphviz',
                        'lcov',
                        'libedit',
                        'mpfr',
                        'sloccount',
                        'utf8cpp'
                    ]
                    self.log.info('Executing: ' + ' '.join(packages))
                    self.execute(*packages)

            if exists('/etc/debian_version'):
                release = open('/etc/debian_version').readline()
                if release.startswith('trixie'):
                    self.log.info(f'Looks like you are using APT on Debian {release}')
                    packages = [
                        'sudo', 'apt', 'install', '-y', '--no-install-recommends',
                        'clang',
                        'cmake',
                        'ninja',
                        'libboost-dev',
                        'libmpfr-dev',
                        'libgmp-dev',
                        'libicu-dev',
                        'libedit-dev',
                        'libutfcpp-dev',
                        'gettext',
                        'texinfo',
                        'doxygen',
                        'graphviz',
                        'lcov',
                        'sloccount',
                    ]
                    self.log.info('Executing: ' + ' '.join(packages))
                    self.execute(*packages)
                else:
                    self.log.info(f"I do not recognize your version '{release}' of Debian!")

            if exists('/etc/redhat-release'):
                release = open('/etc/redhat-release').readline()
                if release.startswith('CentOS'):
                    self.log.info('Looks like you are using YUM on CentOS')
                    packages = [
                        'sudo', 'yum', 'install',
                        'gcc',
                        'gcc-c++',
                        'compat-gcc-*',
                        'make',
                        'libtool',
                        'autoconf',
                        'automake',
                        'zlib-devel',
                        'bzip2-devel',
                        'python-devel',
                        'gmp-devel',
                        'gettext-devel',
                        #'mpfr-devel'
                        'libedit-devel',
                        #'texlive-full',
                        #'doxygen',
                        #'graphviz',
                        'texinfo',
                        #'lcov',
                        #'sloccount'
                    ]
                    self.log.info('Executing: ' + ' '.join(packages))
                    self.execute(*packages)
                elif release.startswith('Fedora release 20'):
                    self.log.info('Looks like you are using YUM on Fedora 20')
                    packages = [
                        'sudo', 'yum', 'install',
                        'boost-devel',
                        'bzip2-devel',
                        'cmake',
                        'doxygen',
                        'gcc',
                        'gcc-c++',
                        'gettext',
                        'gettext-devel',
                        'gmp-devel',
                        'lcov',
                        'libedit-devel',
                        'mpfr-devel',
                        'ninja-build',
                        'python-devel',
                        'sloccount',
                        'texinfo',
                        'zlib-devel'
                    ]
                    self.log.info('Executing: ' + ' '.join(packages))
                    self.execute(*packages)

        elif system.startswith('CYGWIN'):
            self.log.info('Looks like you are using Cygwin')
            self.log.info('Please install the dependencies manually.')

    #########################################################################
    # Determine the system's basic configuration                            #
    #########################################################################

    def setup_for_johnw(self):
        self.configure_args.append('-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON')

        if not self.options.compiler:
            self.configure_args.append('-DCMAKE_CXX_COMPILER:PATH=/usr/local/bin/clang++')

            if self.current_flavor == 'opt':
                self.configure_args.append('-DCMAKE_CXX_FLAGS_RELEASE:STRING=-O3')
                self.configure_args.append('-DCMAKE_EXE_LINKER_FLAGS:STRING=-O3')
                self.configure_args.append('-DCMAKE_SHARED_LINKER_FLAGS:STRING=-O3')
                self.configure_args.append('-DCMAKE_MODULE_LINKER_FLAGS:STRING=-O3')
            #else:
            #    self.CXXFLAGS.append('-g -O1 -faddress-sanitizer')
            #    self.LDFLAGS.append('-g -O1 -faddress-sanitizer')

            self.configure_args.append(self.source_dir)

        else:
            self.configure_args.append('-DCMAKE_CXX_COMPILER:PATH=' + self.options.compiler)
            self.configure_args.append('-DCMAKE_INCLUDE_PATH:STRING=/usr/local/include')
            self.configure_args.append('-DCMAKE_LIBRARY_PATH:STRING=/usr/local/lib')
            self.configure_args.append('-DBOOST_ROOT=/usr/local')
            self.configure_args.append(self.source_dir)

    def setup_for_system(self):
        system = os.uname().sysname
        self.log.info('System type is => ' + system)

        if self.options.enable_doxygen:
            self.configure_args.append('-DUSE_DOXYGEN=1')
        if self.options.python:
            self.configure_args.append('-DUSE_PYTHON=1')

        if system.startswith('CYGWIN'):
            self.configure_args.append('-G')
            self.configure_args.append('Unix Makefiles')
        elif system.startswith('MINGW'):
            self.configure_args.append('-G')
            self.configure_args.append('MSYS Makefiles')
        elif self.options.use_ninja:
            self.configure_args.append('-GNinja')

        if exists('/Users/johnw/Projects/ledger/plan/TODO'):
            self.setup_for_johnw()

    def setup_flavor(self):
        self.setup_for_system()

        if 'setup_flavor_' + self.current_flavor not in PrepareBuild.__dict__:
            self.log.error('Unknown build flavor "%s"' % self.current_flavor)
            sys.exit(1)

        self.log.info('Setting up build flavor => ' + self.current_flavor)
        PrepareBuild.__dict__['setup_flavor_' + self.current_flavor](self)

    def escape_string(self, data):
        return re.sub('(["\\\\])', '\\\\\\1', data)

    def finalize_config(self):
        self.setup_flavor()

        for var in ('CXXFLAGS', 'LDFLAGS'):
            value = self.__dict__[var]
            if value:
                first = not self.envvars[var]
                for member in value:
                    #escaped = self.escape_string(member)
                    #if member != escaped:
                    #    member = escaped
                    if first:
                        first = False
                    else:
                        self.envvars[var] += ' '
                    self.envvars[var] += member
                self.log.debug('Final value of %s: %s' %
                               (var, self.envvars[var]))

            elif var in self.envvars:
                del self.envvars[var]

    #########################################################################
    # Options that can modify any build flavor                              #
    #########################################################################

    def option_local(self, option=None, opt_str=None, value=None, parser=None):
        self.log.debug('Saw option --local')
        self.options.build_dir = self.source_dir

    def option_help(self, option=None, opt_str=None, value=None, parser=None):
        self.phase_help()

    #########################################################################
    # The various build flavors                                             #
    #########################################################################

    def setup_flavor_default(self):
        pass

    def setup_flavor_debug(self):
        self.configure_args.append('-DBUILD_DEBUG=1')

    def setup_flavor_opt(self):
        self.configure_args.append('-DBUILD_DEBUG=0')
        self.configure_args.append('-DNO_ASSERTS=1')

    def setup_flavor_gcov(self):
        # NO_ASSERTS is set so that branch coverage ignores the never-taken
        # else branch inside assert statements.
        self.configure_args.append('-DBUILD_DEBUG=1')
        self.configure_args.append('-DNO_ASSERTS=1')
        self.configure_args.append('-DCLANG_GCOV=1')

        self.CXXFLAGS.append('-fprofile-arcs')
        self.CXXFLAGS.append('-ftest-coverage')
        self.LDFLAGS.append('-fprofile-arcs')
        self.LDFLAGS.append('-ftest-coverage')

        if not self.options.compiler or self.options.compiler == "clang-3.1":
            self.LDFLAGS.append('-lgcov')

    def setup_flavor_gprof(self):
        self.configure_args.append('-DBUILD_DEBUG=1')

        self.CXXFLAGS.append('-pg')
        self.LDFLAGS.append('-pg')

    #########################################################################
    # Configure build tree using CMake                                      #
    #########################################################################

    def configure_environment(self):
        self.finalize_config()

        environ = dict(os.environ)
        for key, value in self.envvars.items():
            if value:
                environ[key] = value

        if self.build_directory() == self.source_dir:
            conf_args = ['cmake']
        else:
            conf_args = ['cmake', self.source_dir]

        if not which('cmake'):
            self.log.error("Cannot find CMake, please check your PATH")
            sys.exit(1)

        for var in ('CXX', 'CXXFLAGS', 'LDFLAGS'):
            if self.envvars.get(var) and (var.endswith('FLAGS')
                                          or exists(self.envvars[var])):
                if var == 'CXX':
                    conf_args.append('-DCMAKE_CXX_COMPILER=%s' %
                                     self.envvars[var])
                elif var == 'CXXFLAGS':
                    conf_args.append('-DCMAKE_CXX_FLAGS=%s' %
                                     self.envvars[var])
                elif var == 'LDFLAGS':
                    conf_args.append('-DCMAKE_EXE_LINKER_FLAGS=%s' %
                                     self.envvars[var])

        if self.options.boost_root:
            conf_args.append('-DBOOST_ROOT=%s' %
                             self.options.boost_root)
            conf_args.append('-DBoost_NO_SYSTEM_PATHS=TRUE')
        if self.options.boost_suffix:
            conf_args.append('-DBoost_COMPILER=%s' %
                             self.options.boost_suffix)
        if self.options.boost_include:
            conf_args.append('-DBOOST_INCLUDEDIR=%s' %
                             self.options.boost_include)

        if self.prefix_directory():
            conf_args.append('-DCMAKE_INSTALL_PREFIX=%s' % self.prefix_directory())

        return (environ, conf_args + self.configure_args)

    def phase_configure(self, *args):
        self.log.info('Executing phase: configure')

        self.configured = True

        environ, conf_args = self.configure_environment()
        for arg in args:
            if arg: conf_args.append(arg)

        build_dir = self.ensure(self.build_directory())
        try:
            os.chdir(build_dir)

            need_to_config = not isfile('rules.ninja' if self.options.use_ninja else 'Makefile')
            if need_to_config:
                self.log.debug('Source => ' + self.source_dir)
                self.log.debug('Build  => ' + build_dir)
                self.log.debug('configure env  => ' + str(environ))
                self.log.debug('configure args => ' + str(conf_args))

                configure = Popen(conf_args, shell=False, env=environ)
                retcode = configure.wait()
                if retcode < 0:
                    self.log.error("Child was terminated by signal", -retcode)
                    sys.exit(1)
                elif retcode != 0:
                    self.log.error("Execution failed: " + ' '.join(conf_args))
                    sys.exit(1)
            else:
                self.log.debug('configure does not need to be run')

        finally:
            os.chdir(self.source_dir)

    def phase_config(self, *args):
        self.log.info('Executing phase: config')
        self.phase_configure(*args)
        if self.should_clean:
            self.phase_clean()

    #########################################################################
    # Builds products from the sources                                      #
    #########################################################################

    def phase_make(self, *args):
        self.log.info('Executing phase: make')

        config_args = []
        make_args   = []

        for arg in args:
            if arg.startswith('--') or arg.startswith('-D'):
                config_args.append(arg)
            else:
                make_args.append(arg)

        if self.options.jobs > 1 and self.current_flavor != 'gcov':
            make_args.append('-j%d' % self.options.jobs)

        if self.options.verbose:
            make_args.append('-v' if self.options.use_ninja else 'VERBOSE=1')

        self.log.debug('Configure arguments => ' + str(config_args))
        self.log.debug('Makefile arguments  => ' + str(make_args))

        if not self.configured:
            self.phase_config(*config_args)

        build_dir = self.ensure(self.build_directory())
        try:
            self.log.debug('Changing directory to ' + build_dir)
            os.chdir(build_dir)

            self.execute(*(['ninja' if self.options.use_ninja else 'make'] +
                           make_args))
        finally:
            os.chdir(self.source_dir)

    def phase_check(self, *args):
        self.log.info('Executing phase: check')
        build_dir = self.ensure(self.build_directory())
        try:
            self.log.debug('Changing directory to ' + build_dir)
            os.chdir(build_dir)

            make_args = list(args)
            if self.options.jobs > 1:
                make_args.append('-j%d' % self.options.jobs)

            self.execute(*(['ctest'] + list(make_args)))
        finally:
            os.chdir(self.source_dir)

    def phase_update(self, *args):
        self.log.info('Executing phase: update')
        self.phase_pull()
        self.phase_make(*args)

    #########################################################################
    # Build directory cleaning phases                                       #
    #########################################################################

    def phase_clean(self, *args):
        self.log.info('Executing phase: clean')
        self.phase_make('clean')

    def phase_gitclean(self, *args):
        self.log.info('Executing phase: gitclean')
        if self.git_working_tree():
            self.execute('git', 'clean', '-dfx')

    #########################################################################
    # Other build phases                                                    #
    #########################################################################

    def configure_flavor(self, flavor, reset=True):
        self.initialize()                  # reset everything
        self.current_flavor = flavor
        self.options.build_dir      = None # use the build/ tree
        self.options.prefix_dir     = None

        if reset and exists(self.build_directory()) and \
           isdir(self.build_directory()):
            self.log.info('=== Wiping build directory %s ===' %
                          self.build_directory())
            try:
                shutil.rmtree(self.build_directory())
            except:
                self.execute('chmod', '-R', 'u+w', self.build_directory())
                self.execute('rm', '-fr', self.build_directory())

    def phase_rsync(self, *args):
        self.log.info('Executing phase: rsync')

        proof_dir = 'ledger-proof'

        if self.options.python:
            proof_dir += "-python"
        if self.options.compiler:
            proof_dir += "-" + basename(self.options.compiler)

        source_copy_dir = join(self.ensure(self.products_directory()), proof_dir)

        self.execute('rsync', '-a', '--delete', '--exclude=/dist/',
                     '--exclude=.git/', '--exclude=b/',
                     '--exclude=/lib/boost-release/',
                     '--exclude=/archive/', '--exclude=/build/',
                     '%s/' % self.source_dir, '%s/' % source_copy_dir)

        self.source_dir = source_copy_dir

    def phase_proof(self, *args):
        self.log.info('Executing phase: proof')

        self.log.info('=== Copying source tree ===')
        self.phase_rsync()

        self.phase_makeall(reset=True, *args)

        self.configure_flavor('opt', reset=False)
        self.log.info('=== Testing opt ===')
        # jww (2012-05-20): Can't use fullcheck yet
        #self.phase_make('fullcheck')
        self.phase_make('test')

        self.configure_flavor('gcov', reset=False)
        self.log.info('=== Testing gcov ===')
        #self.phase_make('check')
        self.phase_make('test')

        self.configure_flavor('default', reset=False)
        self.log.info('=== Testing default ===')
        #self.phase_make('fullcheck')
        self.phase_make('test')
        # jww (2012-05-20): docs are not working yet
        #self.phase_make('docs')

        self.configure_flavor('debug', reset=False)
        self.log.info('=== Testing debug ===')
        #self.phase_make('fullcheck')
        self.phase_make('test')

    def phase_makeall(self, reset=False, *args):
        self.log.info('Executing phase: makeall')

        self.configure_flavor('opt', reset)
        self.log.info('=== Building opt ===')
        self.phase_make(*args)

        self.configure_flavor('gcov', reset)
        self.log.info('=== Building gcov ===')
        self.phase_make(*args)

        self.configure_flavor('default', reset)
        self.log.info('=== Building default ===')
        self.phase_make(*args)

        self.configure_flavor('debug', reset)
        self.log.info('=== Building debug ===')
        self.phase_make(*args)

    #########################################################################
    # Help                                                                  #
    #########################################################################

    def phase_help(self, *args):
        self.option_parser.print_help()

        print("""
Of the optional ARGS, the first is an optional build FLAVOR, with the default
being 'debug':

  default           Regular autoconf settings
  debug             Debugging and --verify support (default)
  opt               Full optimizations
  gcov              Coverage analysis
  gprof             Code profiling (for macOS, just use: 'shark -i ledger ...')

Next is the optional build PHASE, with 'config' being the default:

  clean             Runs 'make clean' in the build directory
  config            Configure the environment for building
  dependencies      Automatically install all necessary build dependencies
  gitclean          Runs 'git clean -dfx', which *really* cleans things
  help              Displays this help text
  info              Show information about the build environment
  make              Do a make in the build directory
  proof             Proves Ledger by building and testing every flavor
  pull              Pulls the latest, and updates local config if need be
  update            Does it all, updates your environment and re-make's

There are many other build phases, though most are not of interest to the
typical user:

  configure         Runs just cmake
  do_all            Runs makeall followed by proof
  gettext           Initialize gettext support
  makeall           Build every flavor there is
  products          Report the products directory path
  rsync             Rsync a copy of the source tree into Products
  sloc              Report total Source Lines Of Code
  version           Output current HEAD version to version.m4

NOTE: If you wish to pass options to CMake or make, add "--" followed by
your options.  Those starting with "-D" or "--" will be passed on to CMake,
positional arguments and other options will be passed to make.
For the 'config' and 'configure' phase everything will be passed to CMake.

Here are some real-world examples:

  ./acprep
  ./acprep --python
  ./acprep opt make
  ./acprep make doc -- -DBUILD_WEB_DOCS=1""")
        sys.exit(0)

PrepareBuild().run()