1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# returns all the elements from the src uri that are .cfg files
8def find_cfgs(d):
9    sources=src_patches(d, True)
10    sources_list=[]
11    for s in sources:
12        if s.endswith('.cfg'):
13            sources_list.append(s)
14
15    return sources_list
16
17cml1_do_configure() {
18	set -e
19	unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS
20	yes '' | oe_runmake oldconfig
21}
22
23EXPORT_FUNCTIONS do_configure
24
25inherit terminal
26
27OE_TERMINAL_EXPORTS += "HOST_EXTRACFLAGS HOSTLDFLAGS TERMINFO CROSS_CURSES_LIB CROSS_CURSES_INC"
28HOST_EXTRACFLAGS = "${BUILD_CFLAGS} ${BUILD_LDFLAGS}"
29HOSTLDFLAGS = "${BUILD_LDFLAGS}"
30CROSS_CURSES_LIB = "-lncurses -ltinfo"
31CROSS_CURSES_INC = '-DCURSES_LOC="<curses.h>"'
32TERMINFO = "${STAGING_DATADIR_NATIVE}/terminfo"
33
34KCONFIG_CONFIG_COMMAND ??= "menuconfig"
35KCONFIG_CONFIG_ENABLE_MENUCONFIG ??= "true"
36KCONFIG_CONFIG_ROOTDIR ??= "${B}"
37python do_menuconfig() {
38    import shutil
39
40    if not bb.utils.to_boolean(d.getVar("KCONFIG_CONFIG_ENABLE_MENUCONFIG")):
41        bb.fatal("do_menuconfig is disabled, please check KCONFIG_CONFIG_ENABLE_MENUCONFIG variable.")
42        return
43
44    config = os.path.join(d.getVar('KCONFIG_CONFIG_ROOTDIR'), ".config")
45    configorig = os.path.join(d.getVar('KCONFIG_CONFIG_ROOTDIR'), ".config.orig")
46
47    try:
48        mtime = os.path.getmtime(config)
49        shutil.copy(config, configorig)
50    except OSError:
51        mtime = 0
52
53    # setup native pkg-config variables (kconfig scripts call pkg-config directly, cannot generically be overriden to pkg-config-native)
54    d.setVar("PKG_CONFIG_DIR", "${STAGING_DIR_NATIVE}${libdir_native}/pkgconfig")
55    d.setVar("PKG_CONFIG_PATH", "${PKG_CONFIG_DIR}:${STAGING_DATADIR_NATIVE}/pkgconfig")
56    d.setVar("PKG_CONFIG_LIBDIR", "${PKG_CONFIG_DIR}")
57    d.setVarFlag("PKG_CONFIG_SYSROOT_DIR", "unexport", "1")
58    # ensure that environment variables are overwritten with this tasks 'd' values
59    d.appendVar("OE_TERMINAL_EXPORTS", " PKG_CONFIG_DIR PKG_CONFIG_PATH PKG_CONFIG_LIBDIR PKG_CONFIG_SYSROOT_DIR")
60
61    oe_terminal("sh -c 'make %s; if [ \\$? -ne 0 ]; then echo \"Command failed.\"; printf \"Press any key to continue... \"; read r; fi'" % d.getVar('KCONFIG_CONFIG_COMMAND'),
62                d.getVar('PN') + ' Configuration', d)
63
64    try:
65        newmtime = os.path.getmtime(config)
66    except OSError:
67        newmtime = 0
68
69    if newmtime > mtime:
70        bb.plain("Changed configuration saved at:\n %s\nRecompile will be forced" % config)
71        bb.build.write_taint('do_compile', d)
72}
73do_menuconfig[depends] += "ncurses-native:do_populate_sysroot"
74do_menuconfig[nostamp] = "1"
75do_menuconfig[dirs] = "${KCONFIG_CONFIG_ROOTDIR}"
76addtask menuconfig after do_configure
77
78python do_diffconfig() {
79    import shutil
80    import subprocess
81
82    workdir = d.getVar('WORKDIR')
83    fragment = workdir + '/fragment.cfg'
84    configorig = os.path.join(d.getVar('KCONFIG_CONFIG_ROOTDIR'), ".config.orig")
85    config = os.path.join(d.getVar('KCONFIG_CONFIG_ROOTDIR'), ".config")
86
87    try:
88        md5newconfig = bb.utils.md5_file(configorig)
89        md5config = bb.utils.md5_file(config)
90        isdiff = md5newconfig != md5config
91    except IOError as e:
92        bb.fatal("No config files found. Did you do menuconfig ?\n%s" % e)
93
94    if isdiff:
95        statement = 'diff --unchanged-line-format= --old-line-format= --new-line-format="%L" ' + configorig + ' ' + config + '>' + fragment
96        subprocess.call(statement, shell=True)
97        # No need to check the exit code as we know it's going to be
98        # non-zero, but that's what we expect.
99        shutil.copy(configorig, config)
100
101        bb.plain("Config fragment has been dumped into:\n %s" % fragment)
102    else:
103        if os.path.exists(fragment):
104            os.unlink(fragment)
105}
106
107do_diffconfig[nostamp] = "1"
108do_diffconfig[dirs] = "${KCONFIG_CONFIG_ROOTDIR}"
109addtask diffconfig
110
111do_showconfig() {
112    bbplain "Config file written to ${KCONFIG_CONFIG_ROOTDIR}/.config"
113}
114do_showconfig[nostamp] = "1"
115addtask showconfig after do_configure
116