summaryrefslogtreecommitdiff
path: root/support/scripts/br2-external
blob: b2bff644b63a25d68f3a40edf1f4b086f6bc1052 (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
#!/bin/bash
set -e

# The location of the br2-external tree, once validated.
declare BR2_EXT

main() {
    local OPT OPTARG
    local br2_ext ofile ofmt

    while getopts :hkmo: OPT; do
        case "${OPT}" in
        h)  help; exit 0;;
        o)  ofile="${OPTARG}";;
        k)  ofmt="kconfig";;
        m)  ofmt="mk";;
        :)  error "option '%s' expects a mandatory argument\n" "${OPTARG}";;
        \?) error "unknown option '%s'\n" "${OPTARG}";;
        esac
    done
    # Forget options; keep only positional args
    shift $((OPTIND-1))

    # Accept 0 or 1 br2-external tree.
    if [ ${#} -gt 1 ]; then
        error "only zero or one br2-external tree allowed.\n"
    fi

    br2_ext="${1}"

    case "${ofmt}" in
    mk|kconfig)
        ;;
    *)  error "no output format specified (-m/-k)\n";;
    esac
    if [ -z "${ofile}" ]; then
        error "no output file specified (-o)\n"
    fi

    exec >"${ofile}"

    do_validate "${br2_ext}"

    do_${ofmt}
}

# Validates the br2-external tree passed as argument. Makes it cannonical
# and store it in global variable BR2_EXT.
#
# Note: since this script is always first called from Makefile context
# to generate the Makefile fragment before it is called to generate the
# Kconfig snippet, we're sure that any error in do_validate will be
# interpreted in Makefile context. Going up to generating the Kconfig
# snippet means that there were no error.
#
do_validate() {
    local br2_ext="${1}"

    # No br2-external tree is valid
    if [ -z "${br2_ext}" ]; then
        return
    fi

    if [ ! -d "${br2_ext}" ]; then
        error "'%s': no such file or directory\n" "${br2_ext}"
    fi
    if [ ! -r "${br2_ext}" -o ! -x "${br2_ext}" ]; then
        error "'%s': permission denied\n" "${br2_ext}"
    fi

    BR2_EXT="$(cd "${br2_ext}"; pwd -P )"
}

# Generate the .mk snippet that defines makefile variables
# for the br2-external tree
do_mk() {
    local BR2_EXT="${1}"

    printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
    printf '\n'

    printf 'BR2_EXTERNAL ?= %s\n' "${BR2_EXT}"
    printf 'BR2_EXTERNAL_MK =\n'
    printf '\n'

    if [ -z "${BR2_EXT}" ]; then
        printf '# No br2-external tree defined.\n'
        return
    fi

    printf 'BR2_EXTERNAL_MK = %s/external.mk\n' "${BR2_EXT}"
}

# Generate the kconfig snippet for the br2-external tree.
do_kconfig() {
    printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
    printf '\n'

    if [ -z "${BR2_EXT}" ]; then
        printf '# No br2-external tree defined.\n'
        return
    fi

    printf 'config BR2_EXTERNAL\n'
    printf '\tstring\n'
    printf '\tdefault "%s"\n' "${BR2_EXT}"
    printf '\n'
    printf 'menu "User-provided options"\n'
    printf '\n'
    printf 'source "%s/Config.in"\n' "${BR2_EXT}"
    printf '\n'
    printf "endmenu # User-provided options\n"
}

help() {
    cat <<-_EOF_
	Usage:
	    ${my_name} <-m|-k> -o FILE PATH

	With -m, ${my_name} generates the makefile fragment that defines
	variables related to the br2-external tree passed as positional
	argument.

	With -k, ${my_name} generates the kconfig snippet to include the
	configuration options specified in the br2-external tree passed
	as positional argument.

	Using -k and -m together is not possible. The last one wins.

	Options:
	    -m  Generate the makefile fragment.

	    -k  Generate the kconfig snippet.

	    -o FILE
	        FILE in which to generate the kconfig snippet or makefile
	        fragment.

	Returns:
	    0   If no error
	    !0  If any error
	_EOF_
}

error() { local fmt="${1}"; shift; printf "BR2_EXTERNAL_ERROR = ${fmt}" "${@}"; exit 1; }

my_name="${0##*/}"
main "${@}"