summaryrefslogtreecommitdiff
path: root/configure
blob: aa5f08a4fce9f0dfc80fd6ce3c5612aff4688925 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
# configure script adapter for Meson
# Based on build-api: https://github.com/cgwalters/build-api
# Copyright 2010, 2011, 2013 Colin Walters <walters@verbum.org>
# Copyright 2016 Emmanuele Bassi
# Copyright 2017 Niels De Graef
# Licensed under the new-BSD license (http://www.opensource.org/licenses/bsd-license.php)

# Build API variables:
# buildapi-variable-require-builddir

shopt -s extglob

# Configurable Options (for more info, run 'meson configure')
# ====================
DIRECTORY_OPTIONS='--@(prefix|bindir|sbindir|libexecdir|datadir|sysconfdir|libdir|mandir|includedir)'
# Keep this in sync with meson_options.txt
PROJECT_OPTIONS='--@(with-cheese|telepathy|with-manpage)'

# The configurable option values
declare -A option_values

# Little helper function for reading args from the commandline.
# it automatically handles -a b and -a=b variants, and returns 1 if
# we need to shift $3.
read_arg() {
    # $1 = arg name
    # $2 = arg value
    # $3 = arg parameter
    local rematch='^[^=]*=(.*)$'
    if [[ $2 =~ $rematch ]]; then
      option_values["$1"]="${BASH_REMATCH[1]}"
    else
      option_values["$1"]="$3"
      # There is no way to shift our callers args, so
      # return 1 to indicate they should do it instead.
      return 1
    fi
}

options2list() {
    local pattern="$1"
    local pattern="${pattern#--@(}"
    local pattern="${pattern%)}"
    IFS=\| read -a "$2" <<<"${pattern#--@(}"
}

sanitycheck() {
    # $1 = arg name
    # $1 = arg command
    # $2 = arg alternates
    local cmd=$( which $2 2>/dev/null )

    if [ -x "$cmd" ]; then
        read "$1" <<< "$cmd"
        return 0
    fi

    test -z $3 || {
        for alt in $3; do
            cmd=$( which $alt 2>/dev/null )

            if [ -x "$cmd" ]; then
                read "$1" <<< "$cmd"
                return 0
            fi
        done
    }

    echo -e "\e[1;31mERROR\e[0m: Command '$2' not found"
    exit 1
}

# First check if our build system is installed
sanitycheck MESON 'meson'
sanitycheck NINJA 'ninja' 'ninja-build'

# convert the patterns into actual lists
options2list "${DIRECTORY_OPTIONS}" 'dir_list'
options2list "${PROJECT_OPTIONS}" 'project_option_list'

# Parse the options
while (($# > 0)); do
    option="${1%%=*}"
    option_name=${option#--}

    case "${option}" in
      --prefix) read_arg "${option_name}" "$@" || shift;;
      ${DIRECTORY_OPTIONS}) read_arg "${option_name}" "$@" || shift;;
      ${PROJECT_OPTIONS}) read_arg "${option_name}" "$@" || shift;;
      # Special case for datarootdir: not understood by meson
      --datarootdir) read_arg "datadir" "$@" || shift;;
      *) echo -e "\e[1;33mINFO\e[0m: Ignoring unknown option '$1'";;
    esac
    shift
done

# Deduce directories
prefix="${option_values['prefix']:-/usr/local}"
option_values['prefix']="${prefix}"
option_values['bindir']="${option_values['bindir']:-${prefix}/bin}"
option_values['sbindir']="${option_values['sbindir']:-${prefix}/sbin}"
option_values['libexecdir']="${option_values['libexecdir']:-${prefix}/bin}"
option_values['datarootdir']="${option_values['datarootdir']:-${prefix}/share}"
option_values['datadir']="${option_values['datarootdir']}"
option_values['sysconfdir']="${option_values['sysconfdir']:-${prefix}/etc}"
option_values['libdir']="${option_values['libdir']:-${prefix}/lib}"
option_values['mandir']="${option_values['mandir']:-${prefix}/share/man}"
option_values['includedir']="${option_values['includedir']:-${prefix}/include}"

# The source directory is the location of this file
srcdir="$(dirname $0)"

# The bild directory is the current directory
builddir="$(pwd)"

# Wrapper Makefile for Ninja
cat > Makefile <<END
# Generated by configure; do not edit

all:
	CC="\$(CC)" CXX="\$(CXX)" ${NINJA}

install:
	DESTDIR="\$(DESTDIR)" ${NINJA} install
END


# Summary
column_width=16

echo "Summary:"
echo "    meson:........... ${MESON}"
echo "    ninja:........... ${NINJA}"
echo "Directories:"
for dir in "${dir_list[@]}";do
  padding_len=$(( ${column_width} - ${#dir} ))
  padding="$(printf '.%.0s' $(seq 1 ${padding_len}))"
  value=${option_values[${dir}]}
  echo "    ${dir}:${padding} ${value}"
done
echo "Project options:"
for opt in "${project_option_list[@]}";do
  padding_len=$(( $column_width - ${#opt} ))
  padding="$(printf '.%.0s' $(seq 1 $padding_len))"
  value=${option_values[$opt]:-<use_default>}
  echo "    ${opt}:${padding} ${value}"
done

# Add arguments to meson
meson_args=()
# Directories
for dir in "${dir_list[@]}";do
  value="${option_values[$dir]}"
  meson_args+=(--${dir}="${value}")
done
# Project options (only add if set)
for opt in "${project_option_list[@]}";do
  value="${option_values[$opt]}"
  [[ -n "${value}" ]] && meson_args+=(-D${opt}="${value}")
done


set -x
${MESON} "${meson_args[@]}" "${srcdir}" "${builddir}"