RetroArch/qb/qb.params.sh
orbea 13cf3cdef2 qb: Refactor how config.h and config.mk are created.
This is a potential security issue.

The problem is that config.h and config.mk are populated with
all variables prefixed with 'HAVE_' from the user's environment.

Example:

  $ HAVE_FOO=yes ./configure
  $ grep FOO config.mk
  HAVE_FOO = 1
  $ grep FOO config.h
  #define HAVE_FOO 1

After this commit these files will only use variables set by
qb configure process and not from the user's environment. This
issue could result in hard to diagnose undefined behavior or
maybe worse?

The user should experience no change in behavior, but
developers should be more careful about setting 'HAVE_'
variables manually.

Unless the FOO variable is used by check_enabled ($2 only),
check_platform, check_lib, check_pkgconf, check_header,
check_macro or check_switch functions it should be set at
least once by the new add_opt function. The first argument
should be 'FOO' which matches the HAVE_FOO variable and the
second argument should contain 'auto', 'no' or 'yes'.

Example:

  add_opt FOO yes

When in doubt its safe to use add_opt. This will also fix a
few potential issues where configure arguments used by the
user are ignored.

When the second argument is not set the FOO variable will only
be used to populate config.h and config.mk with its current
value. This should only be done in qb/qb.libs.sh in functions
that set 'HAVE_' variables.
2019-07-28 07:59:41 -07:00

157 lines
3.9 KiB
Bash

# add_opt
# $1 = HAVE_$1
# $2 = value ['auto', 'no' or 'yes', checked only if non-empty]
add_opt()
{ setval="$(eval "printf %s \"\$USER_$1\"")"
[ "${2:-}" ] && ! match "$setval" no yes && eval "HAVE_$1=\"$2\""
for opt in $(printf %s "$CONFIG_OPTS"); do
case "$opt" in
"$1") return 0 ;;
esac
done
CONFIG_OPTS="${CONFIG_OPTS} $1"
}
# print_help_option
# $1 = option
# $@ = description
print_help_option()
{
_opt="$1"
shift 1
printf ' %-26s %s\n' "$_opt" "$@"
}
print_help()
{ cat << EOF
====================
Quickbuild script
====================
Package: $PACKAGE_NAME
General environment variables:
CC: C compiler
CFLAGS: C compiler flags
CXX: C++ compiler
CXXFLAGS: C++ compiler flags
LDFLAGS: Linker flags
General options:
EOF
print_help_option "--prefix=PATH" "Install path prefix"
print_help_option "--sysconfdir=PATH" "System wide config file prefix"
print_help_option "--bindir=PATH" "Binary install directory"
print_help_option "--datarootdir=PATH" "Read-only data install directory"
print_help_option "--docdir=PATH" "Documentation install directory"
print_help_option "--mandir=PATH" "Manpage install directory"
print_help_option "--global-config-dir=PATH" "System wide config file prefix (Deprecated)"
print_help_option "--build=BUILD" "The build system (no-op)"
print_help_option "--host=HOST" "Cross-compile with HOST-gcc instead of gcc"
print_help_option "--help" "Show this help"
printf %s\\n '' 'Custom options:'
while read -r VAR COMMENT; do
TMPVAR="${VAR%=*}"
COMMENT="${COMMENT#*#}"
VAL="${VAR#*=}"
VAR="$(printf %s "${TMPVAR#HAVE_}" | tr '[:upper:]' '[:lower:]')"
case "$VAR" in
'c89_'*|'cxx_'*) continue;;
*)
case "$VAL" in
'yes'*)
print_help_option "--disable-$VAR" "Disable $COMMENT";;
'no'*)
print_help_option "--enable-$VAR" "Enable $COMMENT";;
'auto'*)
print_help_option "--enable-$VAR" "Enable $COMMENT"
print_help_option "--disable-$VAR" "Disable $COMMENT";;
*)
print_help_option "--with-$VAR" "Config $COMMENT";;
esac
esac
done < 'qb/config.params.sh'
}
opt_exists() # $opt is returned if exists in OPTS
{ opt="$(printf %s "$1" | tr '[:lower:]' '[:upper:]')"
err="$2"
eval "set -- $OPTS"
for OPT do [ "$opt" = "$OPT" ] && return; done
die 1 "Unknown option $err"
}
parse_input() # Parse stuff :V
{ BUILD=''
OPTS=''
CONFIG_OPTS=''
config_opts='./configure'
while read -r VAR _; do
TMPVAR="${VAR%=*}"
NEWVAR="${TMPVAR##HAVE_}"
OPTS="${OPTS} $NEWVAR"
case "$NEWVAR" in
C89_*|CXX_*) : ;;
*) CONFIG_OPTS="${CONFIG_OPTS} $NEWVAR" ;;
esac
eval "USER_$NEWVAR=auto"
done < 'qb/config.params.sh'
#OPTS contains all available options in config.params.sh - used to speedup
#things in opt_exists()
while [ $# -gt 0 ]; do
config_opts="${config_opts} $1"
case "$1" in
--prefix=*) PREFIX=${1##--prefix=};;
--global-config-dir=*|--sysconfdir=*) GLOBAL_CONFIG_DIR="${1#*=}";;
--bindir=*) BIN_DIR="${1#*=}";;
--build=*) BUILD="${1#*=}";;
--datarootdir=*) SHARE_DIR="${1#*=}";;
--docdir=*) DOC_DIR="${1#*=}";;
--host=*) CROSS_COMPILE=${1##--host=}-;;
--mandir=*) MAN_DIR="${1#*=}";;
--enable-*)
opt_exists "${1##--enable-}" "$1"
eval "HAVE_$opt=yes"
eval "USER_$opt=yes"
;;
--disable-*)
opt_exists "${1##--disable-}" "$1"
eval "HAVE_$opt=no"
eval "USER_$opt=no"
eval "HAVE_NO_$opt=yes"
;;
--with-*)
arg="${1##--with-}"
val="${arg##*=}"
opt_exists "${arg%%=*}" "$1"
eval "$opt=\"$val\""
;;
-h|--help) print_help; exit 0;;
--) break ;;
'') : ;;
*) die 1 "Unknown option $1";;
esac
shift
done
cat > config.log << EOF
Command line invocation:
\$ ${config_opts}
## ----------- ##
## Core Tests. ##
## ----------- ##
EOF
}
. qb/config.params.sh
parse_input "$@"