aboutsummaryrefslogtreecommitdiffstats
path: root/tools/kgit-config
blob: f17d208a50d6c4f8934f15535a57d9c4290fa710 (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
#!/usr/bin/env python3

# SPDX-License-Identifier: GPL-2.0-only
#
# kernel fragment manipulation utility
#
# Copyright (C) 2021 Bruce Ashfield
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys
import os
import glob
from collections import OrderedDict
from pathlib import Path
import textwrap

import contextlib
import shutil
import tempfile

import argparse
import re
import subprocess
import pathlib

@contextlib.contextmanager
def cd(newdir, cleanup=lambda: True):
    prevdir = os.getcwd()
    os.chdir(os.path.expanduser(newdir))
    try:
        yield
    finally:
        os.chdir(prevdir)
        cleanup()

@contextlib.contextmanager
def tempdir():
    dirpath = tempfile.mkdtemp()
    def cleanup():
        # todo: make this contingent on a flag
        if not save_temps:
            shutil.rmtree(dirpath)
        pass
    with cd(dirpath, cleanup):
        yield dirpath


def split_option( config_option_str ):
    option = config_option_str.rstrip( '\n' )
    opt = None
    val = None
    try:
        m = re.match( r"(CONFIG_[^= ]+)=([^ ]+.*)", option)
        opt = m.group(1)
        val = m.group(2)
    except:
        if re.search( "^#\s*CONFIG_", option ):
            # print( "option is a is not set!!! %s" % option )
            m = re.match(r"# (CONFIG_[^ ]+) is not set", option )
            if m:
                opt = m.group(1)
                val = "n"
            else:
                # this is an invalid option
                opt = "invalid option format"
                val = option

        # a fully commented line, is not an option and val ..
        elif re.search(r"^#.*$", option ):
            opt = None
            val = None

        elif re.search( r".*= *", option):
            # space after equals
            opt = "invalid option format"
            val = option
        elif re.search( r" *=", option):
            # space before equals
            opt = "invalid option format"
            val = option

    return opt,val

def config_queue_read( config_queue_file ):
    if verbose:
        print( "[INFO]: reading config.queue from: %s" % config_queue_file )

    p = Path(config_queue_file )
    frag_dir = p.parent

    # frag dict is a dictionary indexed by fragment path + name, that points
    # to a dictionary of config options -> values
    frag_dict = OrderedDict()

    # option dict is a diectionary indexed by the option name. it points to
    # a diectionary of fragment name and the value the fragment set it to.
    option_dict = OrderedDict()

    issues_dict = OrderedDict()
    issues_dict["duplicated_option"] = OrderedDict()
    issues_dict["redefined_option"] = OrderedDict()
    issues_dict["malformated_option"] = OrderedDict()


    non_hw_class_dict = OrderedDict()
    hw_class_dict = OrderedDict()

    try:
        p.resolve(True)
    except:
        return frag_dict,option_dict,issues_dict,hw_class_dict,non_hw_class_dict

    # There are two passes through the queue.
    #
    # First pass:
    #             - the fragments and how they are included for classification.
    #               "kconf include" ..
    #             - broad classification instructions (.kcf files)
    #
    # Second pass:
    #             - special files and their classification, this allows us to
    #               override / reclassify something that was "kconf included"
    #               a certain way.

    # first pass start
    with open(config_queue_file) as fp:
        for cnt, line in enumerate(fp):

            fragment = line.split( '#' )
            frag_name = fragment[0].rstrip()
            frag_type = fragment[1].rstrip().lstrip()

            # print("Line {}: {}".format(cnt, line))
            # print("{} Fragment: {}/{}".format(frag_type,frag_dir,frag_name))

            frag_dict[frag_name] = OrderedDict()

            frag_path = Path( str(frag_dir) + "/" + frag_name.rstrip() )
            frag_full_path = frag_path.resolve()

            frag_dirname = frag_path.parent.resolve()
            non_hardware_classification = Path( str(frag_dirname) + "/non-hardware.kcf" ).resolve()
            hardware_classification = Path( str(frag_dirname) + "/hardware.kcf" ).resolve()

            if non_hardware_classification.exists() and use_classifiers:
                if verbose:
                    print( "[DBG]: classification found: %s" % non_hardware_classification )
                with open( str(non_hardware_classification) ) as classification_file:
                    for cline in classification_file:
                        kconfig_start = Path( ksrc + "/" + cline.rstrip() )
                        if kconfig_start.exists():
                            with open( str(kconfig_start) ) as kfile:
                                for kline in kfile:
                                    m = re.match( r"^(menu).*config (\w*)", kline )
                                    if m:
                                        non_hw_class_dict[m.group(2)] = classification_file.name

                                        # being in both h/w and non-hardware leads us to results where we
                                        # can't easily silence a warning. So if we've added it to h/w we need
                                        # remove it from non-hardware. If we do it in both the hardware and
                                        # non hardware cases, the end result is that the last classification
                                        # wins, which is what we want.
                                        try:
                                            del hw_class_dict[m.group(2)]
                                        except:
                                            pass

                                    m = re.match( r"^config (\w*)", kline )
                                    if m:
                                        non_hw_class_dict[m.group(1)] = classification_file.name

                                        # being in both h/w and non-hardware leads us to results where we
                                        # can't easily silence a warning. So if we've added it to h/w we need
                                        # remove it from non-hardware. If we do it in both the hardware and
                                        # non hardware cases, the end result is that the last classification
                                        # wins, which is what we want.
                                        try:
                                            del hw_class_dict[m.group(1)]
                                        except:
                                            pass

                        # this is jut too slow, but keeping it for reference.
                        #     try:
                        #         ck = kconfiglib.Kconfig( cline.rstrip(), warn=False )
                        #     except:
                        #         ck = None
                        #     if ck:
                        #         #print( "=================== %s" % cline.rstrip() )
                        #         for c in ck.unique_defined_syms:
                        #             #print( "c: %s" % c.name )
                        #             non_hw_class_dict[c.name] = classification_file
                        #         #print( ck.unique_defined_syms )

                        else:
                            if verbose:
                                print( "[WARNING]: %s does not exist, but was a classifier" % kconfig_start )


            if hardware_classification.exists() and use_classifiers:
                if verbose:
                    print( "[DBG]: hardware classification found: %s" % hardware_classification )
                with open( str(hardware_classification) ) as classification_file:
                    for cline in classification_file:
                        kconfig_start = Path( ksrc + "/" + cline.rstrip() )
                        if kconfig_start.exists():
                            with open( str(kconfig_start) ) as kfile:
                                for kline in kfile:
                                    m = re.match( r"^(menu).*config (\w*)", kline )
                                    if m:
                                        hw_class_dict[m.group(2)] = classification_file.name

                                        # being in both h/w and non-hardware leads us to results where we
                                        # can't easily silence a warning. So if we've added it to h/w we need
                                        # remove it from non-hardware. If we do it in both the hardware and
                                        # non hardware cases, the end result is that the last classification
                                        # wins, which is what we want.
                                        try:
                                            del non_hw_class_dict[m.group(2)]
                                        except:
                                            pass

                                    m = re.match( r"^config (\w*)", kline )
                                    if m:
                                        hw_class_dict[m.group(1).rstrip()] = classification_file.name

                                        # being in both h/w and non-hardware leads us to results where we
                                        # can't easily silence a warning. So if we've added it to h/w we need
                                        # remove it from non-hardware. If we do it in both the hardware and
                                        # non hardware cases, the end result is that the last classification
                                        # wins, which is what we want.
                                        try:
                                            del non_hw_class_dict[m.group(1)]
                                        except:
                                            pass

                        else:
                            if verbose:
                                print( "[WARNING]: %s does not exist, but was a classifier" % kconfig_start )

            with open( str(frag_path) ) as config_frag:
                for cline in config_frag:
                    c,value = split_option( cline )
                    o_noprefix = ""
                    if c:
                        o_noprefix = re.sub( "^CONFIG_", "", c )
                    if c == "invalid option format":
                        if frag_name in issues_dict["malformated_option"]:
                            issues_dict["malformated_option"][frag_name].append( value )
                        else:
                            issues_dict["malformated_option"][frag_name] = [ value ]
                    elif c:
                        if frag_type == "hardware":
                            if use_classifiers:
                                # print( "            hw classification for: %s,%s" % (o_noprefix,str(frag_path)))
                                hw_class_dict[o_noprefix] = str(frag_path)
                                # we can't be both hardware and software, so we should be deleting
                                # one or the other if in both
                                try:
                                    del non_hw_class_dict[o_noprefix.rstrip()]
                                except:
                                    pass
                        elif frag_type == "non-hardware":
                            if use_classifiers:
                                # we can't be both hardware and software, so we should be deleting
                                # one or the other if in both
                                non_hw_class_dict[o_noprefix] = str(frag_path)
                                try:
                                    # print( "deleting %s from hw_class, since it is also software" % o_noprefix.rstrip() )
                                    del hw_class_dict[o_noprefix.rstrip()]
                                except:
                                    pass

                        if not c in frag_dict[frag_name]:
                            frag_dict[frag_name][c] = value
                        else:
                            if frag_name in issues_dict["duplicated_option"]:
                                issues_dict["duplicated_option"][frag_name].append( c )
                            else:
                                issues_dict["duplicated_option"][frag_name] = [ c ]

                        if not c in option_dict:
                            option_dict[c] = OrderedDict()

                        option_dict[c][frag_name] = value


    # 2nd pass start
    with open(config_queue_file) as fp:
        for cnt, line in enumerate(fp):

            fragment = line.split( '#' )
            frag_name = fragment[0].rstrip()
            frag_type = fragment[1].rstrip().lstrip()

            # print("Line {}: {}".format(cnt, line))
            # print("{} Fragment: {}/{}".format(frag_type,frag_dir,frag_name))

            frag_path = Path( str(frag_dir) + "/" + frag_name.rstrip() )
            frag_full_path = frag_path.resolve()

            frag_dirname = frag_path.parent.resolve()

            hardware_cfg = Path( str(frag_dirname) + "/hardware.cfg" ).resolve()
            non_hardware_cfg = Path( str(frag_dirname) + "/non-hardware.cfg" ).resolve()

            if hardware_cfg.exists() and use_classifiers:
                with open( str(hardware_cfg) ) as classification_file:
                    for cline in classification_file:
                        m = re.match( r"(CONFIG_[^= ]+)", cline )
                        if m:
                            o_noprefix = re.sub( "^CONFIG_", "", m.group(1) )
                            hw_class_dict[o_noprefix.rstrip()] = str(hardware_cfg.resolve())
                            # being in both h/w and non-hardware leads us to results where we
                            # can't easily silence a warning. So if we've added it to h/w we need
                            # remove it from non-hardware. If we do it in both the hardware and
                            # non hardware cases, the end result is that the last classification
                            # wins, which is what we want.
                            try:
                                del non_hw_class_dict[o_noprefix.rstrip()]
                            except:
                                pass

            if non_hardware_cfg.exists() and use_classifiers:
                with open( str(non_hardware_cfg) ) as classification_file:
                    for cline in classification_file:
                        m = re.match( r"(CONFIG_[^= ]+)", cline )
                        if m:
                            o_noprefix = re.sub( "^CONFIG_", "", m.group(1) )
                            non_hw_class_dict[o_noprefix.rstrip()] = str(non_hardware_cfg.resolve())
                            # being in both h/w and non-hardware leads us to results where we
                            # can't easily silence a warning. So if we've added it to h/w we need
                            # remove it from non-hardware. If we do it in both the hardware and
                            # non hardware cases, the end result is that the last classification
                            # wins, which is what we want.
                            try:
                                del hw_class_dict[o_noprefix.rstrip()]
                            except Exception as e:
                                pass



    for o in option_dict:
        fragments_defining_option = option_dict[o]
        if len( fragments_defining_option ) > 1:
            issues_dict["redefined_option"][o] = OrderedDict()
            for k in fragments_defining_option:
                issues_dict["redefined_option"][o][k] = fragments_defining_option[k]
                # val = fragments_defining_option[k]
                # print( "       - {}: {} ({})".format(o,k,val) )

    # this needs to just be captured in a class, so we can return one thing,
    # versus the growing list
    return frag_dict,option_dict,issues_dict,hw_class_dict,non_hw_class_dict

def create_bsp_template( name, hardware_dict, policy_dict, outdir ):
    if verbose:
        print( "[INFO]: creating bsp template for: %s" % name )

    if not os.path.exists( "{}/kernel-cache/bsp/{}".format( outdir,name ) ):
        os.makedirs( "{}/kernel-cache/bsp/{}".format( outdir,name ) )

    bsp_file = "{}/kernel-cache/bsp/{}/{}.scc".format(outdir,name,name)
    hw_cfg_file = "{}/kernel-cache/bsp/{}/{}-hw.cfg".format(outdir,name,name)
    policy_cfg_file = "{}/kernel-cache/bsp/{}/{}-policy.cfg".format(outdir,name,name)

    with open( bsp_file, "w" ) as w:
        w.write("""\
# SPDX-License-Identifier: MIT
define KMACHINE {}
define KTYPE standard

include ktypes/base/base.scc
kconf hardware {}-hw.cfg
# kconf non-hardware {}-policy.cfg

""".format(name,name,name) )
        
    with open( hw_cfg_file, "w" ) as w:
        for i,v in hardware_dict.items():
            if i:
                w.write( "CONFIG_%s=%s\n" % (i,v))

    with open( policy_cfg_file, "w" ) as w:
        for i,v in policy_dict.items():
            if i:
                w.write( "CONFIG_%s=%s\n" % (i,v))

    print( "BSP template created: %s" % bsp_file )
    print( "   Hardware options: %s" % hw_cfg_file )
    print( "   Policy options: %s" % policy_cfg_file )


def create_option_classifiers( ksrc, outdir ):
    if verbose:
        print( "[INFO]: creating option classifiers in %s" % outdir )
    try:
        env = os.environ.copy()
        if verbose:
            print( "[NOTE]: running: kgit-create-buckets -v hardware nonhardware" )
        analysis = subprocess.check_output([ 'kgit-create-buckets',
                                             '-v', '-v',
                                             'hardware',
                                             'nonhardware'],
                                           stderr=subprocess.STDOUT,
                                           cwd=ksrc, env=env ).decode('utf-8')
    except subprocess.CalledProcessError as e:
        print( "[ERROR]: creation failed: %s" % e.output.decode('utf-8'))

    if not os.path.exists( "{}/kernel-cache/ktypes/base".format( outdir ) ):
        os.makedirs( "{}/kernel-cache/ktypes/base".format( outdir) )

    shutil.copy(  "/tmp/hw_bucket.txt.sorted", "{}/kernel-cache/ktypes/base/hardware.kcf".format(outdir) )
    shutil.copy(  "/tmp/non_hw_bucket.txt.sorted", "{}/kernel-cache/ktypes/base/non-hardware.kcf".format(outdir) )

    with open( "{}/kernel-cache/ktypes/base/base.scc".format(outdir), "w" ) as w:
        w.write("""\
kconf hardware basehw.cfg
kconf non-hardware basenonhw.cfg
""" )

    with open( "{}/kernel-cache/ktypes/base/basehw.cfg".format(outdir), "w" ) as w:
        w.write("""\
# placeholder for hardware options
""" )

    with open( "{}/kernel-cache/ktypes/base/basenonhw.cfg".format(outdir), "w" ) as w:
        w.write("""\
# placeholder for non-hardware options
""" )

    if verbose:
        print( "[INFO]: option classifiers creation done" )

    return "kernel-cache"


pathname = os.path.dirname(sys.argv[0])

global verbose
verbose = ''
use_classifiers = True
ksrc = ''

parser = argparse.ArgumentParser(
                 description="kernel fragment generation/manipulation/query",
                 formatter_class=argparse.RawDescriptionHelpFormatter,
                 epilog=textwrap.dedent('''\

         Overview:

            kgit-config can be used to perform initial processing on a defconfig
            to determine what options are "hardware" and are hence suitable to be
            placed in a BSP, while leaving non-hardware/policy options to the
            fragments contained within a kernel-cache repository.

            It can also query a kernel-cache directory for details about which
            fragments provide an option, and where they are included (which
            is helpful when completing initial BSP fragments.

         Examples:

            # process a defconfig and output hardware options suitable for a BSP
            % kgit-config create --ksrc ~/poky-kernel/linux-yocto.git/ --defconfig ~/poky-kernel/linux-yocto.git/arch/x86/configs/x86_64_defconfig --kmeta ~/poky-kernel/kernel-cache

            # query a set of fragments for details on options
            % kgit-config query --kmeta ~/poky-kernel/kernel-cache CONFIG_DEBUG_FS

            [INFO]: looking for ['CONFIG_DEBUG_FS']
            [INFO]: cfg files that have options matching regex: CONFIG_DEBUG_FS
            [INFO]: 7 cfg files have option: CONFIG_DEBUG_FS
              cfg file: cfg/fs/debugfs.cfg
                 included by: /home/bruce/poky-kernel/kernel-cache/cfg/fs/debugfs.scc
              cfg file: cfg/debug/irq/debug-generic-irq-debugfs.cfg
                 included by: /home/bruce/poky-kernel/kernel-cache/cfg/debug/irq/debug-generic-irq-debugfs.scc
              cfg file: cfg/debug/irq/debug-irq-domain.cfg
                 included by: /home/bruce/poky-kernel/kernel-cache/cfg/debug/irq/debug-irq-domain.scc
              cfg file: cfg/debug/printk/debug-dynamic-debug.cfg
                 included by: /home/bruce/poky-kernel/kernel-cache/cfg/debug/printk/debug-dynamic-debug.scc
              cfg file: features/systemtap/systemtap.cfg
                 included by: /home/bruce/poky-kernel/kernel-cache/features/systemtap/systemtap.scc
              cfg file: bsp/beaglebone/beaglebone-non_hardware.cfg
                 included by: /home/bruce/poky-kernel/kernel-cache/bsp/beaglebone/beaglebone.scc
              cfg file: bsp/intel-x86/intel-x86-acpi.cfg
                 included by: /home/bruce/poky-kernel/kernel-cache/bsp/intel-x86/intel-x86.scc

         '''))

parser.add_argument("-v", action='store_true', dest="verbose",
                    help="verbose")
parser.add_argument("-s", "--ksrc",
                    help="path to the kernel source")
parser.add_argument("--create", action='store_true',
                     help="Create a minimal BSP hardware config")
parser.add_argument("--query", action='store_true',
                     help="run a query against kernel meta data" )
parser.add_argument("-k", "--kmeta",
                     help="path to the kernel meta data")
parser.add_argument("-d", "--defconfig",
                     help="path to the kernel defconfig")
parser.add_argument("-e", "--entrypoint",
                    help="fragment to use as an entrypoint for classification (default is ktypes/standard/standard.scc")
parser.add_argument( "--savetemps", action='store_true',
                     help="don't delete temporary working directory" )
parser.add_argument("-o", "--outdir",
                    help="path to the output directory (defaults to '.')")
parser.add_argument( "--bsp",
                     help="create a BSP file with the provided name" )

# parser.add_argument("--strict", action='store_true',
#                     help="When checking or processing, strictly apply ARCH and other config settings (i.e. no global checking will be done)" )


parser.add_argument('args', help="<command> (create or query)", nargs=argparse.REMAINDER)

in_args = sys.argv

if len(in_args) == 1:
    parser.print_help()
    sys.exit(1)

create_flag = False
query_flag = False
save_temps = False
if in_args:
    # we'll pass this to parse_known_args below, and when the default
    # of sys.argv is passed argv[0] is dropped. So we drop it now to
    # make sure things are parsed properly
    p_args = in_args[1:]
    if re.search( "^create$", in_args[1] ):
        # drop the command, so the argparser will pick up our dashed
        # items.
        p_args = in_args[2:]
        create_flag = True
    if re.search( "^query$", in_args[1] ):
        # drop the command, so the argparser will pick up our dashed
        # items.
        p_args = in_args[2:]
        query_flag = True

args, unknown_args = parser.parse_known_args( p_args )

if create_flag:
    args.create = True

if query_flag:
    args.query = True

if args.ksrc:
    ksrc=args.ksrc

outdir="."
if args.outdir:
    outdir=args.outdir
outdir = pathlib.Path(outdir).resolve()

if args.savetemps:
    save_temps = True

if args.verbose:
    verbose = True

if args.create:
    if not ksrc:
        print( "[ERROR]: Kernel source directory not provided" )
        sys.exit(1)

    # todo: convert to absolute and make sure it exists
    if not args.kmeta:
        if verbose:
            print( "[INFO]: no meta-data provided, creating option framework based on kernel tree: %s" % ksrc )
        kmeta = create_option_classifiers( ksrc, outdir )
        kmeta = pathlib.Path(kmeta).resolve()

        # creation will have made this entry point
        args.entrypoint = "ktypes/base/base.scc"
        args.kmeta = kmeta

    # todo: convert to absolute and make sure it exists
    if args.defconfig:
        defconfig_path = pathlib.Path(args.defconfig).exists()
    else:
        print( "[ERROR]: kernel defconfig required for minimal BSP creation" )
        sys.exit(1)

    if verbose:
        print( "[INFO]: Creating minimal h/w BSP config" )

    # make a temp directory to hold our work
    with tempdir() as dirpath:
        if save_temps and verbose:
            print( "[INFO]: --savetemps was passed, will not remove: %s" % dirpath )

        try:
            entry_point = "ktypes/standard/standard.scc"
            if args.entrypoint:
                entry_point =  args.entrypoint

            if verbose:
                print( "[NOTE]: running: " +
                       'scc' +
                       ' --force' +
                       ' -I{}'.format( args.kmeta ) +
                       ' -o {}:cfg'.format( dirpath ) +
                       ' {}/{}'.format(args.kmeta,entry_point) )

            env = os.environ.copy()
            analysis = subprocess.check_output([ 'scc',
                                                 '--force',
                                                 '-I', '{}'.format( args.kmeta ),
                                                 '-o', '{}:cfg'.format( dirpath ),
                                                 '{}/{}'.format(args.kmeta,entry_point)],
                                               cwd=dirpath, env=env ).decode('utf-8')
        except subprocess.CalledProcessError as e:
            print( "[ERROR]: creation failed: %s" % e.output.decode('utf-8'))
            sys.exit(1)

        frag_dict,option_dict,issues_dict,hw_dict,non_hw_dict = \
                               config_queue_read( dirpath + "/config.queue" )

        defconfig_hw_options = {}
        defconfig_nonhw_options = {}
        # loop through the defconfig, and remove all the non hardware options ?
        with open( str(args.defconfig) ) as defconfig:
            for cline in defconfig:
                c,value = split_option( cline )
                o_noprefix = ""
                if c:
                    o_noprefix = re.sub( "^CONFIG_", "", c )
                if c == "invalid option format":
                    # print( "invalid option: %s" % cline )
                    continue

                #if verbose:
                #    print( "defconfig: %s --> %s" % (c,value))

                try:
                    if non_hw_dict[o_noprefix]:
                        #print( "config %s is non-hardware, dropping" % (c))
                        defconfig_nonhw_options[o_noprefix] = value
                        pass
                except:
                    # it's hardware
                    #print( "     hardware option %s" % o_noprefix)
                    defconfig_hw_options[o_noprefix] = value


        if args.bsp:
            create_bsp_template( args.bsp, defconfig_hw_options, defconfig_nonhw_options, outdir )
            sys.exit(0)

        print( "[INFO]: BSP h/w configuration values:" )
        for i,v in defconfig_hw_options.items():
            if i:
                print( "CONFIG_%s=%s" % (i,v))


        # look at the non-hardware options, and see if a fragment references
        # that option, we can suggest it as a feature
        print( "\n[INFO]: BSP policy options and fragments that provide them:" )
        for i,v in defconfig_nonhw_options.items():
            try:
                fragments_defining_option = option_dict["CONFIG_" + i]
                print( "CONFIG_%s=%s (%s)" % (i,v,list(fragments_defining_option.keys()) ) )
            except:
                print( "CONFIG_%s=%s (no fragment)" % (i,v ) )


    sys.exit(0)

if args.query:
    # todo: convert to absolute and make sure it exists
    if not args.kmeta:
        print( "[ERROR]: meta data required for config query" )
        sys.exit(1)

    if args.args[0] == "query":
        command = args.args[1:]
    else:
        command = args.args

    # make a temp directory to hold our work
    with tempdir() as dirpath:
        if save_temps:
            print( "[INFO]: --savetemps was passed, will not remove: %s" % dirpath )

        possible_scc_files = []
        possible_cfg_files = []
        # list all the features
        for root, dirs, files in os.walk(args.kmeta):
            for file in files:
                if(file.endswith(".scc")):
                    possible_scc_files.append(os.path.join(root,file))
                if(file.endswith(".cfg")):
                    possible_cfg_files.append(os.path.join(root,file))

        if not command:
            feature_scc_files = {}
            for p in possible_scc_files:
                feature_scc = False
                with open(p) as f:
                    for cnt,l in enumerate(f):
                        if 'KFEATURE_DESCRIPTION' in l:
                            d = l.split()[2:]
                            d = ' '.join([str(elem) for elem in d])
                            full_path = p
                            p = re.sub( args.kmeta + '/', "", full_path )
                            feature_scc_files[p] = { 'path' : full_path,
                                                     'description' : d }
                            feature_scc = True

                if not feature_scc:
                    pass

            print( "[INFO]: available feature scc files (%s)" % args.kmeta)
            for p in feature_scc_files:
                print( "    %s: %s" % (p,feature_scc_files[p]['description']) )
        else:
            print( "[INFO]: looking for %s"  % command )
            oregex = ' '.join([str(elem) for elem in command])
            matching_cfg_files = {}
            for p in possible_cfg_files:
                matching_cfg = False
                with open(p) as f:
                    for cnt,l in enumerate(f):
                        if re.search( oregex, l ):
                            full_path = p
                            pp = re.sub( args.kmeta + '/', "", full_path )
                            matching_cfg_files[pp] = { 'path' : full_path,
                                                       'option' : re.sub( '\n', '', l) }
                            matching_cfg = True

            if matching_cfg_files:
                print( "[INFO]: cfg files that have options matching regex: %s" % oregex )
                sccs_that_include_matching_cfg = {}
                for p in matching_cfg_files:
                    pregex = os.path.basename(p)
                    match_found = False
                    for sp in possible_scc_files:
                        with open(sp) as f:
                            for cnt,l in enumerate(f):
                                if re.search( "[ /]" + pregex + "$", l ):
                                    full_path = sp
                                    pp = re.sub( args.kmeta + '/', "", full_path )
                                    try:
                                        sccs_that_include_matching_cfg[p].append( full_path )
                                    except:
                                        sccs_that_include_matching_cfg[p] = [ full_path ]

                                    match_found = True
                    if not match_found:
                        sccs_that_include_matching_cfg[p] = [ "Standalone" ]

                if matching_cfg_files:
                    print( "[INFO]: %s cfg files have option: %s" % (len(matching_cfg_files),oregex))

                for p in matching_cfg_files:
                    print( "  cfg file: %s" % p )
                    try:
                        istring = ' '.join([str(elem) for elem in list(sccs_that_include_matching_cfg[p])])
                        print( "     included by: %s" % istring)
                        #for m in sccs_that_include_matching_cfg[p]:
                        #    print( "        %s" % m )
                    except:
                        pass

            else:
                print( "no matching cfg files with option %s found" % oregex )