diff mbox

support/scripts: tool to create fragments

Message ID 1477425699-37971-1-git-send-email-matthew.weber@rockwellcollins.com
State Changes Requested
Headers show

Commit Message

Matt Weber Oct. 25, 2016, 8:01 p.m. UTC
From: Sam Voss <samuel.voss@rockwellcollins.com>

Add script to create kconfig fragment; defaults to busybox fragment if
only one config is specified (compares to package/busybox/busybox.config).

Signed-off-by: Sam Voss <samuel.voss@rockwellcollins.com>
Signed-off-by: Matt Weber <matthew.weber@rockwellcollins.com>
---
 support/scripts/gen-config-fragment.sh | 59 ++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100755 support/scripts/gen-config-fragment.sh

Comments

Thomas Petazzoni Oct. 25, 2016, 8:03 p.m. UTC | #1
Hello,

On Tue, 25 Oct 2016 15:01:39 -0500, Matt Weber wrote:
> From: Sam Voss <samuel.voss@rockwellcollins.com>
> 
> Add script to create kconfig fragment; defaults to busybox fragment if
> only one config is specified (compares to package/busybox/busybox.config).
> 
> Signed-off-by: Sam Voss <samuel.voss@rockwellcollins.com>
> Signed-off-by: Matt Weber <matthew.weber@rockwellcollins.com>

Could you show an example on how this script is intended to be used ?

Thanks,

Thomas
Matt Weber Oct. 25, 2016, 9:21 p.m. UTC | #2
Sam,

On Tue, Oct 25, 2016 at 3:01 PM, Matt Weber <
matthew.weber@rockwellcollins.com> wrote:

> From: Sam Voss <samuel.voss@rockwellcollins.com>
>
> Add script to create kconfig fragment; defaults to busybox fragment if
> only one config is specified (compares to package/busybox/busybox.config).
>
> Signed-off-by: Sam Voss <samuel.voss@rockwellcollins.com>
> Signed-off-by: Matt Weber <matthew.weber@rockwellcollins.com>
> ---
>  support/scripts/gen-config-fragment.sh | 59
> ++++++++++++++++++++++++++++++++++
>  1 file changed, 59 insertions(+)
>  create mode 100755 support/scripts/gen-config-fragment.sh
>
> diff --git a/support/scripts/gen-config-fragment.sh
> b/support/scripts/gen-config-fragment.sh
> new file mode 100755
> index 0000000..a8b5345
> --- /dev/null
> +++ b/support/scripts/gen-config-fragment.sh
> @@ -0,0 +1,59 @@
> +#!/bin/bash
> +
> +###########################################################
> #####################
> +# DESCRIPTION:
> +#     Creates a fragment by comparing two kconfigs. Default usage creates
> a
> +#       busybox fragment, however providing two config files will
> override this.
> +#
> +#     Required arguments:
> +#       $1 - config #1. This is generally the new one created
> +#
> +#     Optional arguments:
> +#       $2 - config #2: The configuration file to compare against. If none
> +#            supplied, will compare against package/busybox/busybox.config
> +###########################################################
> #####################
> +usage ()
> +{
> +use="gen-config-fragment.sh:
> +    Creates a fragment by comparing two kconfigs. Default usage creates a
> +      busybox fragment, however providing two config files will override
> this.
> +
> +    Required arguments:
> +      $1 - config #1. This is generally the new one created
> +
> +    Optional arguments:
> +      $2 - config #2: The configuration file to compare against. If none
> +           supplied, will compare against package/busybox/busybox.config"
> +        printf "$use"
> +        exit 1
> +}
> +
> +if [ -z "$1" ]; then
> +        usage
> +else
> +        FirstConfig="$1"
> +fi
> +
> +if [ -z "$2" ]; then
> +        SecondConfig="package/busybox/busybox.config"
> +else
> +        SecondConfig="$2"
> +fi
> +
> +loadedFirstConfig=()
> +loadedSecondConfig=()
> +while read line; do
> +        if [[ ${line:0:1} != '#' ]] || [[ ${line:2:3} == 'BR2' ]]; then
> +                loadedFirstConfig+=("${line// /_space_}")
>

Oops need to update for kernel config use,  you must have just tested
against buildroot.


> +        fi
> +done < $FirstConfig
> +
> +while read -r line; do
> +        if [[ ${line:0:1} != '#' ]] || [[ ${line:2:3} == 'BR2' ]]; then
> +                loadedSecondConfig+=("${line// /_space_}")
>

same here


> +        fi
> +done < $SecondConfig
> +
> +
> +D=($(comm -23 <(printf '%s\n' "${loadedFirstConfig[@]}" | sort -d)
> <(printf '%s\n' "${loadedSecondConfig[@]}" | sort -d)))
> +printf '%s\n' "${D[@]//_space_/ }" > "$FirstConfig.fragment"
> --
> 1.9.1
>
>
Samuel Voss Oct. 26, 2016, 2:06 p.m. UTC | #3
Thomas,

>
> Could you show an example on how this script is intended to be used ?
>

Assuming you have already done a `make linux-savedefconfig` and have
an older version to compare against (example files given below), it
would be used (from the
buildroot root directory) as follows:

./support/scripts/gen-config-fragment.sh
output/build/linux-4.8.1/defconfig  board/qemu/x86_64/linux-4.8.config

File 1 - output/build/linux-4.8.1/defconfig


CONFIG_SYSVIPC=y
CONFIG_MODULES=y
CONFIG_MODULE_UNLOAD=y
CONFIG_SMP=y
# CONFIG_X86_MPPARSE is not set
CONFIG_HYPERVISOR_GUEST=y
CONFIG_PARAVIRT=y
CONFIG_NET=y
CONFIG_PACKET=y
CONFIG_UNIX=y
CONFIG_INET=y
CONFIG_DEVTMPFS=y
CONFIG_DEVTMPFS_MOUNT=y
CONFIG_VIRTIO_BLK=y
CONFIG_BLK_DEV_SD=y
CONFIG_SCSI_VIRTIO=y
CONFIG_ATA=y
CONFIG_ATA_PIIX=y
CONFIG_NETDEVICES=y
CONFIG_VIRTIO_NET=y
CONFIG_NE2K_PCI=y
CONFIG_8139CP=y
CONFIG_INPUT_EVDEV=y
CONFIG_SERIAL_8250=y
CONFIG_SERIAL_8250_CONSOLE=y
CONFIG_VIRTIO_CONSOLE=y
CONFIG_HW_RANDOM_VIRTIO=m
CONFIG_DRM=y
CONFIG_DRM_QXL=y
CONFIG_DRM_BOCHS=y
CONFIG_DRM_VIRTIO_GPU=y
CONFIG_SOUND=y
CONFIG_SND=y
CONFIG_SND_HDA_INTEL=y
CONFIG_SND_HDA_GENERIC=y
CONFIG_USB=y
CONFIG_USB_XHCI_HCD=y
CONFIG_USB_EHCI_HCD=y
CONFIG_USB_UHCI_HCD=y
CONFIG_USB_STORAGE=y
CONFIG_VIRTIO_PCI=y
CONFIG_VIRTIO_BALLOON=y
CONFIG_VIRTIO_INPUT=y
CONFIG_VIRTIO_MMIO=y
CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES=y
CONFIG_EXT4_FS=y
CONFIG_TMPFS=y
CONFIG_TMPFS_POSIX_ACL=y

File 2 - Original defconfig - board/qemu/x86_64/linux-4.8.config

CONFIG_SYSVIPC=y
CONFIG_MODULES=y
CONFIG_MODULE_UNLOAD=y
CONFIG_SMP=y
CONFIG_HYPERVISOR_GUEST=y
CONFIG_PARAVIRT=y
CONFIG_NET=y
CONFIG_PACKET=y
CONFIG_UNIX=y
CONFIG_INET=y
CONFIG_VIRTIO_BLK=y
CONFIG_BLK_DEV_SD=y
CONFIG_SCSI_VIRTIO=y
CONFIG_ATA=y
CONFIG_ATA_PIIX=y
CONFIG_NETDEVICES=y
CONFIG_VIRTIO_NET=y
CONFIG_NE2K_PCI=y
CONFIG_8139CP=y
CONFIG_INPUT_EVDEV=y
CONFIG_SERIAL_8250=y
CONFIG_SERIAL_8250_CONSOLE=y
CONFIG_VIRTIO_CONSOLE=y
CONFIG_HW_RANDOM_VIRTIO=m
CONFIG_DRM=y
CONFIG_DRM_BOCHS=y
CONFIG_DRM_QXL=y
CONFIG_DRM_VIRTIO_GPU=y
CONFIG_SOUND=y
CONFIG_SND=y
CONFIG_SND_HDA_INTEL=y
CONFIG_SND_HDA_GENERIC=y
CONFIG_USB=y
CONFIG_USB_XHCI_HCD=y
CONFIG_USB_EHCI_HCD=y
CONFIG_USB_UHCI_HCD=y
CONFIG_USB_STORAGE=y
CONFIG_VIRTIO_PCI=y
CONFIG_VIRTIO_BALLOON=y
CONFIG_VIRTIO_INPUT=y
CONFIG_VIRTIO_MMIO=y
CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES=y
CONFIG_EXT4_FS=y
CONFIG_TMPFS=y
CONFIG_TMPFS_POSIX_ACL=y

Giving output file of - output/build/linux-4.8.1/defconfig.fragment

CONFIG_DEVTMPFS_MOUNT=y
CONFIG_DEVTMPFS=y
# CONFIG_X86_MPPARSE is not set

Should I add an example execution of this in the header of the script?
 Something like the following?

#
# Linux Example
# ./support/scripts/gen-config-fragment.sh <output
dir>/build/linux-x.y.z/defconfig
board/custom-board/linux-x.y.z.config
#
# Busybox Example using default base config
# ./support/scripts/gen-config-fragment.sh <output
dir>/build/busybox-x.y.z/.config
#
# Busybox Example using custom base config
# ./support/scripts/gen-config-fragment.sh <output
dir>/build/busybox-x.y.z/.config board/custom-board/busybox.config
Thomas Petazzoni Oct. 26, 2016, 2:17 p.m. UTC | #4
Hello,

On Wed, 26 Oct 2016 09:06:12 -0500, Sam Voss wrote:

> Assuming you have already done a `make linux-savedefconfig` and have
> an older version to compare against (example files given below), it
> would be used (from the
> buildroot root directory) as follows:
> 
> ./support/scripts/gen-config-fragment.sh
> output/build/linux-4.8.1/defconfig  board/qemu/x86_64/linux-4.8.config

[...]

> Giving output file of - output/build/linux-4.8.1/defconfig.fragment
> 
> CONFIG_DEVTMPFS_MOUNT=y
> CONFIG_DEVTMPFS=y
> # CONFIG_X86_MPPARSE is not set

OK, understood. So in fact, you have reimplemented the diffconfig tool
available in the Linux kernel scripts/ directory. For your two
configuration files, it shows:

thomas@skate:/tmp$ ~/projets/linux-2.6/scripts/diffconfig config1 config2 
-DEVTMPFS y
-DEVTMPFS_MOUNT y
-X86_MPPARSE n

Though I agree that your output format is better, as it can be re-used
as a fragment as-is.

However, calling it gen-config-fragment looks a bit wrong to me. What
about diffconfig, like in the Linux kernel?

> Should I add an example execution of this in the header of the script?
>  Something like the following?
> 
> #
> # Linux Example
> # ./support/scripts/gen-config-fragment.sh <output
> dir>/build/linux-x.y.z/defconfig  
> board/custom-board/linux-x.y.z.config
> #
> # Busybox Example using default base config
> # ./support/scripts/gen-config-fragment.sh <output
> dir>/build/busybox-x.y.z/.config  
> #
> # Busybox Example using custom base config
> # ./support/scripts/gen-config-fragment.sh <output
> dir>/build/busybox-x.y.z/.config board/custom-board/busybox.config  

No need to give so many examples, but just one example would be good.

In terms of implementation, should we re-use the diffconfig tool from
the kernel, simply tweaked in terms of output format?

Thanks,

Thomas
Samuel Voss Oct. 26, 2016, 3:45 p.m. UTC | #5
Thomas,

> OK, understood. So in fact, you have reimplemented the diffconfig tool
> available in the Linux kernel scripts/ directory.

Interesting, I didn't know this tool existed!

> However, calling it gen-config-fragment looks a bit wrong to me. What
> about diffconfig, like in the Linux kernel?

I agree with a name change, however might it be confused (in discussion)
with the other tool? If that is not a problem I would agree with the new name.

> No need to give so many examples, but just one example would be good.

Will update the patch to include one along with the changes Matt mentioned.

> In terms of implementation, should we re-use the diffconfig tool from
> the kernel, simply tweaked in terms of output format?

I actually don't think this would be a best course of action. As it is
written it will work
on busybox or kernel configs, however if we choose to reuse the output
of the diffconfig
tool either logic will have to be added to figure out what configs are
being generated or
another argument will have to specify it and I'm not sure if it is
worth that extra code.

Thanks,

Sam
Yann E. MORIN Oct. 26, 2016, 9:17 p.m. UTC | #6
Matt, Sam, All,

On 2016-10-25 15:01 -0500, Matt Weber spake thusly:
> From: Sam Voss <samuel.voss@rockwellcollins.com>
> 
> Add script to create kconfig fragment; defaults to busybox fragment if
> only one config is specified (compares to package/busybox/busybox.config).

As stated by Thomas, this is re-inventing diffconfig.

The only difference being the output format.

Currently diffconfig can output a diff-like, but it can also output a
"merge" style (although I'm not sure what it means, given that a merge
on your example is just empty).

I think it would be much better to update diffconfig to allow it to spit
out a fragment format. Maybe something like:

    $ diffconfig -f config.base config.modified
    CONFIG_DEVTMPFS_MOUNT=y
    CONFIG_DEVTMPFS=y
    # CONFIG_X86_MPPARSE is not set

Thoughts?

Note: either way, I'd prefer both files to be mandatory arguments, with
the original one specified first and the modified one specified last,
and that there is no default comparison against busybox.

Otherwise, some review below...

> Signed-off-by: Sam Voss <samuel.voss@rockwellcollins.com>
> Signed-off-by: Matt Weber <matthew.weber@rockwellcollins.com>
> ---
>  support/scripts/gen-config-fragment.sh | 59 ++++++++++++++++++++++++++++++++++
>  1 file changed, 59 insertions(+)
>  create mode 100755 support/scripts/gen-config-fragment.sh
> 
> diff --git a/support/scripts/gen-config-fragment.sh b/support/scripts/gen-config-fragment.sh
> new file mode 100755
> index 0000000..a8b5345
> --- /dev/null
> +++ b/support/scripts/gen-config-fragment.sh
> @@ -0,0 +1,59 @@
> +#!/bin/bash
> +
> +################################################################################
> +# DESCRIPTION:
> +#     Creates a fragment by comparing two kconfigs. Default usage creates a
> +#       busybox fragment, however providing two config files will override this.
> +#
> +#     Required arguments:
> +#       $1 - config #1. This is generally the new one created
> +#
> +#     Optional arguments:
> +#       $2 - config #2: The configuration file to compare against. If none
> +#            supplied, will compare against package/busybox/busybox.config
> +################################################################################
> +usage ()
> +{
> +use="gen-config-fragment.sh:
> +    Creates a fragment by comparing two kconfigs. Default usage creates a
> +      busybox fragment, however providing two config files will override this.
> +
> +    Required arguments:
> +      $1 - config #1. This is generally the new one created
> +
> +    Optional arguments:
> +      $2 - config #2: The configuration file to compare against. If none
> +           supplied, will compare against package/busybox/busybox.config"
> +        printf "$use"
> +        exit 1

Dont use a variable; use an here-document (leading TABS, repesented by
»»»» here, are ignored):

    cat <<-_EOF_
    »»»»gen-config-fragment.sh:
    »»»»    Creates a fragment by comparing two kconfigs.

    »»»»Arguments:
    »»»»    $1 : the unmodified original configuration file
    »»»»    $2 : the modified configuration file

    »»»»Blabla...
    _EOF_

> +}
> +
> +if [ -z "$1" ]; then
> +        usage
> +else
> +        FirstConfig="$1"
> +fi
> +
> +if [ -z "$2" ]; then
> +        SecondConfig="package/busybox/busybox.config"
> +else
> +        SecondConfig="$2"
> +fi
> +
> +loadedFirstConfig=()
> +loadedSecondConfig=()

Declare the variables:

    declare -a loadedFirstConfig loadedSecondConfig

> +while read line; do
> +        if [[ ${line:0:1} != '#' ]] || [[ ${line:2:3} == 'BR2' ]]; then

Use just plain '[ ... ]' to do tests, since they are POSIX; quote
strings:

    if [ "${line:0:3}" = "BR2_" -o "${line:2:4}" = "BR2_" ]; then

> +                loadedFirstConfig+=("${line// /_space_}")

I'm not too fond of the magic placeholder _space_. But I can't find an
easy and better solution... Maybe make it even less probable, like:
    _S_P_A_C_E_

> +        fi
> +done < $FirstConfig

Quote strings:  "${FirstConfig}"

But there is a better and faster way to fill in the array:

    loadedFirstConfig=( $( sed -r -e 's/ /_S_P_A_C_E_/g' "${FirstConfig}" ) )

But it turns out you don;t need the array in the first place, see below.

> +
> +while read -r line; do
> +        if [[ ${line:0:1} != '#' ]] || [[ ${line:2:3} == 'BR2' ]]; then
> +                loadedSecondConfig+=("${line// /_space_}")
> +        fi
> +done < $SecondConfig
> +
> +
> +D=($(comm -23 <(printf '%s\n' "${loadedFirstConfig[@]}" | sort -d) <(printf '%s\n' "${loadedSecondConfig[@]}" | sort -d)))

Line too long; split it.

> +printf '%s\n' "${D[@]//_space_/ }" > "$FirstConfig.fragment"

Just output to stdout; let the user redirect to the file of his choice.

But we can do it much more simply, in a single command. Your script
would be just (without even a requirement for bash):

    #!/bin/sh

    usage() {
        cat <<-_EOF_
        Blabla
        _EOF_
    }

    [ ${#} -eq 2 ] || { usage; exit 1; }
    [ -f "${1}" ]  || { usage; exit 1; }
    [ -f "${2}" ]  || { usage; exit 1; }

    comm -23 <( sed -r -e 's/ /_S_P_A_C_E_/g' "${FirstConfig}"  |sort -d ) \
             <( sed -r -e 's/ /_S_P_A_C_E_/g' "${SecondConfig}" |sort -d ) \
    |sed -r -e 's/_S_P_A_C_E_/ /g'

Regards,
Yann E. MORIN.
Yann E. MORIN Oct. 26, 2016, 9:19 p.m. UTC | #7
Matt, Sam, All,

On 2016-10-26 23:17 +0200, Yann E. MORIN spake thusly:
> On 2016-10-25 15:01 -0500, Matt Weber spake thusly:
> > From: Sam Voss <samuel.voss@rockwellcollins.com>
> > Add script to create kconfig fragment; defaults to busybox fragment if
> > only one config is specified (compares to package/busybox/busybox.config).
[--SNIP--]
> But we can do it much more simply, in a single command. Your script
> would be just (without even a requirement for bash):
> 
>     #!/bin/sh
> 
>     usage() {
>         cat <<-_EOF_
>         Blabla
>         _EOF_
>     }
> 
>     [ ${#} -eq 2 ] || { usage; exit 1; }
>     [ -f "${1}" ]  || { usage; exit 1; }
>     [ -f "${2}" ]  || { usage; exit 1; }
> 
>     comm -23 <( sed -r -e 's/ /_S_P_A_C_E_/g' "${FirstConfig}"  |sort -d ) \
>              <( sed -r -e 's/ /_S_P_A_C_E_/g' "${SecondConfig}" |sort -d ) \
>     |sed -r -e 's/_S_P_A_C_E_/ /g'

Of course, you would already noticed that you would have to replace
${FirstConfig} with ${1} and ${SecondConfig} with ${2}. ;-)

Regards,
Yann E. MORIN.
Thomas Petazzoni Oct. 26, 2016, 9:28 p.m. UTC | #8
Hello,

On Wed, 26 Oct 2016 23:17:08 +0200, Yann E. MORIN wrote:

> > +loadedFirstConfig=()
> > +loadedSecondConfig=()  
> 
> Declare the variables:
> 
>     declare -a loadedFirstConfig loadedSecondConfig

And don't use camel case naming for variables, please.

Thomas
Matt Weber Oct. 26, 2016, 9:38 p.m. UTC | #9
Yann,

On Wed, Oct 26, 2016 at 4:17 PM, Yann E. MORIN <yann.morin.1998@free.fr>
wrote:

> Matt, Sam, All,
>
> On 2016-10-25 15:01 -0500, Matt Weber spake thusly:
> > From: Sam Voss <samuel.voss@rockwellcollins.com>
> >
> > Add script to create kconfig fragment; defaults to busybox fragment if
> > only one config is specified (compares to package/busybox/busybox.
> config).
>
> As stated by Thomas, this is re-inventing diffconfig.
>
> The only difference being the output format.
>
> Currently diffconfig can output a diff-like, but it can also output a
> "merge" style (although I'm not sure what it means, given that a merge
> on your example is just empty).
>
> I think it would be much better to update diffconfig to allow it to spit
> out a fragment format. Maybe something like:
>
>     $ diffconfig -f config.base config.modified
>     CONFIG_DEVTMPFS_MOUNT=y
>     CONFIG_DEVTMPFS=y
>     # CONFIG_X86_MPPARSE is not set
>
> Thoughts?
>

We could propose a wrapper which lives as support/scripts/diffconfig which
checks to see if the linux source has been extracted and if it is, calls
the diffconfig tool as is.  Except for the new case of -f where it does
additional formatting.
Most of our use cases are with a complete build we're trying to split out
configs.  However if you didn't have build the script would be of no use.


> Note: either way, I'd prefer both files to be mandatory arguments, with
> the original one specified first and the modified one specified last,
> and that there is no default comparison against busybox.
>
> Otherwise, some review below...
>
> > Signed-off-by: Sam Voss <samuel.voss@rockwellcollins.com>
> > Signed-off-by: Matt Weber <matthew.weber@rockwellcollins.com>
> > ---
> >  support/scripts/gen-config-fragment.sh | 59
> ++++++++++++++++++++++++++++++++++
> >  1 file changed, 59 insertions(+)
> >  create mode 100755 support/scripts/gen-config-fragment.sh
> >
> > diff --git a/support/scripts/gen-config-fragment.sh
> b/support/scripts/gen-config-fragment.sh
> > new file mode 100755
> > index 0000000..a8b5345
> > --- /dev/null
> > +++ b/support/scripts/gen-config-fragment.sh
> > @@ -0,0 +1,59 @@
> > +#!/bin/bash
> > +
> > +###########################################################
> #####################
> > +# DESCRIPTION:
> > +#     Creates a fragment by comparing two kconfigs. Default usage
> creates a
> > +#       busybox fragment, however providing two config files will
> override this.
> > +#
> > +#     Required arguments:
> > +#       $1 - config #1. This is generally the new one created
> > +#
> > +#     Optional arguments:
> > +#       $2 - config #2: The configuration file to compare against. If
> none
> > +#            supplied, will compare against
> package/busybox/busybox.config
> > +###########################################################
> #####################
> > +usage ()
> > +{
> > +use="gen-config-fragment.sh:
> > +    Creates a fragment by comparing two kconfigs. Default usage creates
> a
> > +      busybox fragment, however providing two config files will
> override this.
> > +
> > +    Required arguments:
> > +      $1 - config #1. This is generally the new one created
> > +
> > +    Optional arguments:
> > +      $2 - config #2: The configuration file to compare against. If none
> > +           supplied, will compare against package/busybox/busybox.
> config"
> > +        printf "$use"
> > +        exit 1
>
> Dont use a variable; use an here-document (leading TABS, repesented by
> »»»» here, are ignored):
>
>     cat <<-_EOF_
>     »»»»gen-config-fragment.sh:
>     »»»»    Creates a fragment by comparing two kconfigs.
>
>     »»»»Arguments:
>     »»»»    $1 : the unmodified original configuration file
>     »»»»    $2 : the modified configuration file
>
>     »»»»Blabla...
>     _EOF_
>
> > +}
> > +
> > +if [ -z "$1" ]; then
> > +        usage
> > +else
> > +        FirstConfig="$1"
> > +fi
> > +
> > +if [ -z "$2" ]; then
> > +        SecondConfig="package/busybox/busybox.config"
> > +else
> > +        SecondConfig="$2"
> > +fi
> > +
> > +loadedFirstConfig=()
> > +loadedSecondConfig=()
>
> Declare the variables:
>
>     declare -a loadedFirstConfig loadedSecondConfig
>
> > +while read line; do
> > +        if [[ ${line:0:1} != '#' ]] || [[ ${line:2:3} == 'BR2' ]]; then
>
> Use just plain '[ ... ]' to do tests, since they are POSIX; quote
> strings:
>
>     if [ "${line:0:3}" = "BR2_" -o "${line:2:4}" = "BR2_" ]; then
>
> > +                loadedFirstConfig+=("${line// /_space_}")
>
> I'm not too fond of the magic placeholder _space_. But I can't find an
> easy and better solution... Maybe make it even less probable, like:
>     _S_P_A_C_E_
>
> > +        fi
> > +done < $FirstConfig
>
> Quote strings:  "${FirstConfig}"
>
> But there is a better and faster way to fill in the array:
>
>     loadedFirstConfig=( $( sed -r -e 's/ /_S_P_A_C_E_/g' "${FirstConfig}"
> ) )
>
> But it turns out you don;t need the array in the first place, see below.
>
> > +
> > +while read -r line; do
> > +        if [[ ${line:0:1} != '#' ]] || [[ ${line:2:3} == 'BR2' ]]; then
> > +                loadedSecondConfig+=("${line// /_space_}")
> > +        fi
> > +done < $SecondConfig
> > +
> > +
> > +D=($(comm -23 <(printf '%s\n' "${loadedFirstConfig[@]}" | sort -d)
> <(printf '%s\n' "${loadedSecondConfig[@]}" | sort -d)))
>
> Line too long; split it.
>
> > +printf '%s\n' "${D[@]//_space_/ }" > "$FirstConfig.fragment"
>
> Just output to stdout; let the user redirect to the file of his choice.
>
> But we can do it much more simply, in a single command. Your script
> would be just (without even a requirement for bash):
>
>     #!/bin/sh
>
>     usage() {
>         cat <<-_EOF_
>         Blabla
>         _EOF_
>     }
>
>     [ ${#} -eq 2 ] || { usage; exit 1; }
>     [ -f "${1}" ]  || { usage; exit 1; }
>     [ -f "${2}" ]  || { usage; exit 1; }
>
>     comm -23 <( sed -r -e 's/ /_S_P_A_C_E_/g' "${FirstConfig}"  |sort -d )
> \
>              <( sed -r -e 's/ /_S_P_A_C_E_/g' "${SecondConfig}" |sort -d )
> \
>     |sed -r -e 's/_S_P_A_C_E_/ /g'
>

Will give that a try.  Thanks !
diff mbox

Patch

diff --git a/support/scripts/gen-config-fragment.sh b/support/scripts/gen-config-fragment.sh
new file mode 100755
index 0000000..a8b5345
--- /dev/null
+++ b/support/scripts/gen-config-fragment.sh
@@ -0,0 +1,59 @@ 
+#!/bin/bash
+
+################################################################################
+# DESCRIPTION:
+#     Creates a fragment by comparing two kconfigs. Default usage creates a
+#       busybox fragment, however providing two config files will override this.
+#
+#     Required arguments:
+#       $1 - config #1. This is generally the new one created
+#
+#     Optional arguments:
+#       $2 - config #2: The configuration file to compare against. If none
+#            supplied, will compare against package/busybox/busybox.config
+################################################################################
+usage ()
+{
+use="gen-config-fragment.sh:
+    Creates a fragment by comparing two kconfigs. Default usage creates a
+      busybox fragment, however providing two config files will override this.
+
+    Required arguments:
+      $1 - config #1. This is generally the new one created
+
+    Optional arguments:
+      $2 - config #2: The configuration file to compare against. If none
+           supplied, will compare against package/busybox/busybox.config"
+        printf "$use"
+        exit 1
+}
+
+if [ -z "$1" ]; then
+        usage
+else
+        FirstConfig="$1"
+fi
+
+if [ -z "$2" ]; then
+        SecondConfig="package/busybox/busybox.config"
+else
+        SecondConfig="$2"
+fi
+
+loadedFirstConfig=()
+loadedSecondConfig=()
+while read line; do
+        if [[ ${line:0:1} != '#' ]] || [[ ${line:2:3} == 'BR2' ]]; then
+                loadedFirstConfig+=("${line// /_space_}")
+        fi
+done < $FirstConfig
+
+while read -r line; do
+        if [[ ${line:0:1} != '#' ]] || [[ ${line:2:3} == 'BR2' ]]; then
+                loadedSecondConfig+=("${line// /_space_}")
+        fi
+done < $SecondConfig
+
+
+D=($(comm -23 <(printf '%s\n' "${loadedFirstConfig[@]}" | sort -d) <(printf '%s\n' "${loadedSecondConfig[@]}" | sort -d)))
+printf '%s\n' "${D[@]//_space_/ }" > "$FirstConfig.fragment"