aboutsummaryrefslogtreecommitdiffstats
path: root/classes/image_populate_mfgtool.bbclass
blob: d30b47a3b3958acb23bba30775a6ceab4b75c11b (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
# Allow generation of mfgtool bundle
#
# The class provides the infrastructure for MFGTOOL generation and is tied to images. To generate
# the bundle, the task populate_mfgtool must be called. For example:
#
# ,----[ Running populate_mfgtool for core-image-minimal image ]
# | $: bitbake core-image-minimal -c populate_mfgtool
# `----
#
# The class behavior is controlled through the MFGTOOLCONFIG (analogous to PACKAGECONFIG)
# variable. The MFGTOOLCONFIG variable itself specifies a space-separated list of the script to
# enable. Following the script, you can determine the behavior of each script by providing up to two
# order-dependent arguments, which are separated by commas. You can omit any argument you like but
# must retain the separating commas. The order is important and specifies the following:
#
#   1. Extra dependencies that should be added to the do_populate_mfgtool task, if the script is
#      enabled.
#   2. Extra binaries that should be added to the bundle, if the script is enabled.
#
# For example:
#
# ,----[ Defining foo.uuu.in and bar.uuu script ]
# | MFGTOOLCONFIG = "foo.uuu.in bar.uuu"
# | MFGTOOLCONFIG[foo.uuu.in] = "dep-foo1:do_deploy dep-foo2:do_deploy,file-foo1"
# | MFGTOOLCONFIG[bar.uuu] = "dep-bar1:do_deploy,file-bar1 file-bar2"
# `----
#
# The dep-foo1:do_deploy, dep-foo2:do_deploy, and dep-bar1:do_deploy are added to
# do_populate_mfgtool dependencies. In addition, file-foo1, file-bar1, and file-bar2 are copied to
# the bundle, only if the script is enabled.
#
# During the mfgtool bundle generation, the uuu.in files are processed and some variables
# replaced. The variables are:
#
#   - MACHINE
#   - UBOOT_BINARY
#   - SPL_BINARY
#   - IMAGE_BASENAME
#
# Copyright 2022-2023 (C) O.S. Systems Software LTDA.
#
# SPDX-License-Identifier: MIT

MFGTOOL_FILESPATH ??= " \
    ${@base_set_filespath(["%s/mfgtool" % p for p in "${BBPATH}".split(":")] \
                             + ["${FILE_DIRNAME}/${BP}/mfgtool", \
                                "${FILE_DIRNAME}/${BPN}/mfgtool", \
                                "${FILE_DIRNAME}/files/mfgtool"] \
                          , d)} \
"

MFGTOOLDIR = "${WORKDIR}/mfgtool-${PN}"
do_populate_mfgtool[dirs] += "${MFGTOOLDIR}"
do_populate_mfgtool[cleandirs] = "${MFGTOOLDIR}"

addtask populate_mfgtool after do_image_complete do_unpack before do_deploy
do_populate_mfgtool[nostamp] = "1"
do_populate_mfgtool[dirs] += "${DEPLOY_DIR_IMAGE} ${WORKDIR}"
do_populate_mfgtool[recrdeptask] += "do_deploy"
do_populate_mfgtool[depends] += "uuu-bin:do_populate_sysroot"

python () {
    depends = []
    deploy_files = ""
    scripts = (d.getVar('MFGTOOLCONFIG') or "").split()
    scripts_and_flags = d.getVarFlags('MFGTOOLCONFIG') or {}
    for flag, flagval in sorted(scripts_and_flags.items()):
        items = flagval.split(",")
        num = len(items)
        if num > 2:
            bb.error("%s: MFGTOOLCONFIG[%s] Only \"depends,deploy files\" can be specified!" % (d.getVar("PN"), flag))

        if flag in scripts:
            if num >= 2 and items[1]:
                deploy_files += ' ' + items[1]
            if num >= 1 and items[0]:
                depends.append(items[0])

    d.appendVarFlag('do_populate_mfgtool', 'depends', ' ' + ' '.join(depends))
    d.setVar('_SCRIPT_DEPLOY_FILES', ' '.join(sorted(list(set(deploy_files.split())))))
}

python do_populate_mfgtool() {
    # For MFGTOOLCONFIG items we use BitBake's fetcher module allowing a consistent behavior.
    scripts = (d.getVar('MFGTOOLCONFIG') or "").split()
    src_uri = ["file://%s" % f for f in scripts]
    if not src_uri:
        bb.fatal("MFGTOOLCONFIG is empty so populate_mfgtool cannot be run.")
        return
    bb.debug(1, "following scripts are used: %s" % ', '.join(scripts))

    localdata = bb.data.createCopy(d)
    filespath = (d.getVar('MFGTOOL_FILESPATH') or "")
    localdata.setVar('FILESPATH', filespath)

    try:
        fetcher = bb.fetch2.Fetch(src_uri, localdata)
        fetcher.unpack(localdata.getVar('WORKDIR'))
    except bb.fetch2.BBFetchException as e:
        bb.fatal("BitBake Fetcher Error: " + repr(e))

    # Generate MFGTOOL bundle.
    bb.build.exec_func('generate_mfgtool_bundle', d)
}

generate_mfgtool_bundle() {
    bbnote "Processing uuu files ..."
    for src in $(ls -1 ${WORKDIR}/*.uuu.in); do
        dest=$(echo $src | sed 's,.in$,,g')
        bbnote " - $src -> $dest"
        sed -e 's/@@MACHINE@@/${MACHINE}/g' \
            -e "s,@@UBOOT_BINARY@@,${UBOOT_BINARY},g" \
            -e "s,@@SPL_BINARY@@,${SPL_BINARY},g" \
            -e "s,@@IMAGE_BASENAME@@,${IMAGE_BASENAME},g" \
            $src > $dest
    done

    bbnote "Deploying uuu files ..."
    for src in $(ls -1 ${WORKDIR}/*.uuu); do
        dest=$(basename $src)
        bbnote " - $src -> ${MFGTOOLDIR}/${PN}-${MACHINE}/$dest"
        install -D -m 0644 $src ${MFGTOOLDIR}/${PN}-${MACHINE}/$dest
    done

    bbnote "Copying uuu binaries..."
    cp -v -s ${STAGING_LIBDIR}/uuu/* ${MFGTOOLDIR}/${PN}-${MACHINE}/

    bbnote "Copying MFGTOOL extra deploy files..."
    for f in ${_SCRIPT_DEPLOY_FILES}; do
        mkdir -p ${MFGTOOLDIR}/${PN}-${MACHINE}/binaries
        cp -v -s ${DEPLOY_DIR_IMAGE}/$f ${MFGTOOLDIR}/${PN}-${MACHINE}/binaries/
    done

    tar -czf ${DEPLOY_DIR_IMAGE}/mfgtool-bundle-${PN}-${MACHINE}.tar.gz \
        --dereference -C ${MFGTOOLDIR} ${PN}-${MACHINE}

    ln -fs mfgtool-bundle-${PN}-${MACHINE}.tar.gz \
          ${DEPLOY_DIR_IMAGE}/mfgtool-bundle-${PN}.tar.gz
}