pkg-config?

NIIBE Yutaka gniibe at fsij.org
Fri Jul 20 09:22:19 CEST 2018


Hello, again,

This week, I struggle with shell programming.  (I realized that what I
use was mostly bash features.)

NIIBE Yutaka <gniibe at fsij.org> wrote:
> I'd like to adopt pkg-config methodology to GnuPG.  I mean,
> providing PACKAGE.pc.

I understand that adding requirement of pkg-config to the GnuPG build is
not good.

I think that it is good to provide *.pc file, for the users of
libraries, even if the GnuPG build process doesn't use pkg-config.

To be more concrete, how about using a shell script like the one
at the end of this mail, with config files which is shared
between the script and pkg-config users?


========================>8=>8=>8================== gpg-error.pc
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${prefix}/lib
host=x86_64-pc-linux-gnu

Name: gpg-error
Description: GPG Runtime
Version: 1.32-betaXXX
Cflags: -I${includedir}
Libs: -L${libdir} -lgpg-error
URL: https://www.gnupg.org/software/libgpg-error/index.html
========================>8=>8=>8==================

========================>8=>8=>8================== gpg-error-mt.pc
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${prefix}/lib
host=x86_64-pc-linux-gnu

Name: gpg-error
Description: GPG Runtime
Version: 1.32-betaXXX
Cflags: -I${includedir}
Libs: -L${libdir} -lgpg-error -pthread
URL: https://www.gnupg.org/software/libgpg-error/index.html
========================>8=>8=>8==================

Here is an example for gpg-error-config replacement:
========================>8=>8=>8==================
#! /bin/sh
#
# gpg-something-config, using a config file in pkg-config style, so
# that we can share such a config file between pkg-config
# 

#
# get_var: get the variable value of NAME
#
# Variables are recorded in the shell variables named "VAR_<NAME>"
get_var () {
    local name=$1

    eval echo \$VAR_$name
}

#
# get_attr: get the attribute value of KEY
#
# Attributes are recorded in the shell variables named "ATTR_<KEY>"
get_attr () {
    local name=$1

    eval echo \$ATTR_$name
}

# Remove ${varname} part in the beginning of a string.
remove_var_expr () {
    local varname=$1
    shift

    eval echo \"\${@#\\\$\\\{$varname\\\}}\"
}

# Given a string, substitute variables.
substitute_vars () {
    local string="$1"
    local line
    local varname
    local result

    while [ -n "$string" ]; do
	case "$string" in
	    \$\$*)
		result="$result\$"
		string="${string#\$\$}"
		;;
	    \${*}*)
		varname="${string#\$\{}"
		varname="${varname%%\}*}"
		result="$result$(get_var ${varname})"
		string=$(remove_var_expr ${varname} ${string})
		;;
	    *)
		result="${result}$(printf %c "$string")"
		string="${string#$(printf %c "$string")}"
		;;
	esac
    done

    echo "$result"
}

# Read a config file
read_config_file () {
    local line
    local varname
    local value
    local key

    while read line; do
	case "$line" in
	    *=*)
		varname="${line%%=*}"
		value="${line#*=}"
		VAR_list="$VAR_list VAR_$varname"
		read VAR_$varname <<EOF1
$(substitute_vars "$value")
EOF1
		;;
	    *:\ *)
		key="${line%%:\ *}"
		value="${line#*:\ }"
		ATTR_list="$ATTR_list ATTR_$key"
		read ATTR_$key <<EOF2
$(substitute_vars "$value")
EOF2
		;;
	esac
    done
}

usage () {
    cat <<EOF
Usage: $1 [OPTIONS]
Options:
        [--mt]       (must be the first option)
	[--prefix]
	[--exec-prefix]
	[--version]
	[--libs]
	[--cflags]
EOF
    exit $2
}

if [ "$1" != "--mt" ]; then
    CONFIG_FILE="gpg-error.pc"
else
    CONFIG_FILE="gpg-error-mt.pc"
    shift
fi

read_config_file < "$CONFIG_FILE"

while test $# -gt 0; do
    case $1 in
        --prefix)
	    output="$output $(get_var prefix)"
	    ;;
        --exec-prefix)
	    output="$output $(get_var exec_prefix)"
	    ;;
        --version)
            echo "$(get_attr Version)"
	    exit 0
	    ;;
        --cflags)
	    output="$output $(get_attr Cflags)"
	    ;;
	--libs)
	    output="$output $(get_attr Libs)"
	    ;;
        --host)
            echo "$(get_var host)"
            exit 0
            ;;
	*)
            usage $0 1 1>&2
	    ;;
    esac
    shift
done

# eval unset $VAR_list VAR_list
# eval unset $ATTR_list ATTR_list

echo $output
========================>8=>8=>8==================
--



More information about the Gnupg-devel mailing list