#!/usr/bin/env python

# todo: make z3 the default, have separate flag for file analysis that just does it all

# Kmax
# Copyright (C) 2012-2019 Paul Gazzillo
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

if __name__ == '__main__':
    import argparse    
    from kmax.vcommon import getLogLevel , getLogger
    import kmax.settings
    import kmax.about

    aparser = argparse.ArgumentParser("find interactions from Kbuild Makefiles")
    ag = aparser.add_argument
    ag('makefile',
       nargs="*",
       type=str,
       help="""the name of a Linux Makefiles or subdirs""")
    
    ag("--log_level", "-log_level",
       help="set logger info",
       type=int, 
       choices=range(5),
       default = 3)    
    
    ag('-z',
       '--output-smtlib2',
       action="store_true",
       default=True,
       help="""Pickle a dictionary mapping names to z3's smtlib2 format.  Defaults to on.""")
    
    ag('-a',
       '--output-all-unit-types',
       action="store_true",
       help="""Output all kinds of units found, including compilation units, library units, hostprogs, unconfigurable units, extra targets, clean files, C file targets, and composites.  This is used to help with completeness checking.""")
    
    ag('-u',
       '--output-unit-pc-format',
       action="store_true",
       help="""Output the legacy unit_pc text format.""")
    
    ag('-t',
       '--table',
       action="store_true",
       help="""show symbol table entries""")
    
    ag('--unselectable',
       type=str,
       help="""the name of a file containing a list of unselectable configuration options.  these options will be treated as always disabled.""")

    ag('-D',
       '--define',
       action='append',
       help="""\
    define a makefile variable""")

    ag('-B',
       '--boolean-configs',
       action="store_true",
       default=True,
       help="""\
    Treat all configuration variables as Boolean variables""")

    ag('-T',
       '--tristate-configs',
       action="store_true",
       help="""\
    Treat all Boolean configuration variables as tri-state variables""")

    ag('--version',
       action="version",
       version="%s %s" % (kmax.about.__title__, kmax.about.__version__),
       help="""Print the version number.""")
    # ag('--case-study',
    #    type=str,
    #    help="""avail options: busybox/linux""")

    args = aparser.parse_args()
        
    if args.log_level != kmax.settings.logger_level and 0 <= args.log_level <= 4:
        kmax.settings.logger_level = args.log_level

    kmax.settings.logger_level = getLogLevel(kmax.settings.logger_level)
    mlog = getLogger(__name__, kmax.settings.logger_level)    
    if __debug__:
        mlog.warn("DEBUG MODE ON. Can be slow! (Use python -O ... for optimization)")

    kmax.settings.do_table = args.table
    if not args.tristate_configs:
        kmax.settings.do_boolean_configs = args.boolean_configs
    kmax.settings.defines = args.define
    kmax.settings.output_smtlib2 = args.output_smtlib2
    kmax.settings.output_all_unit_types = args.output_all_unit_types

    if args.output_unit_pc_format:
        kmax.settings.output_unit_pc_format = True
        kmax.settings.output_smtlib2 = False
        kmax.settings.output_all_unit_types = False
    
    if args.unselectable:
        with open(args.unselectable, "rb") as f:
            kmax.settings.unselectable = [ x.strip() for x in f.readlines() ]

    inp = args.makefile
    mlog.info("processing {}\n".format(inp))

    from kmax.alg import Run
    myrun = Run()
    myrun.run(inp)
    print(myrun.results)
