aboutsummaryrefslogtreecommitdiff
path: root/configure
blob: 17b63dfb7aa5a3d6d48b59f780f6ac3e6000bc0a (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
function do_help() {
    printf "Options:
  --help (-h)   Display this message
  --prefix      Path to install MOOG
  --bindir      Path to install MOOG binaries (default: PREFIX/bin)
  --datadir     Path to install MOOG data files (default: PREFIX/share/moog)
  --with-x11    Path to X11 lib directory
  --with-sm     Path to SM lib directory\n"
}

dummy_c='#include <stdio.h>
int main(int argc, char *argv[]) { return 0; }'

function check_lib() {
    # Purpose:
    #   Link a dummy C program to a library
    #
    # Invocation:
    #   check_lib X11 /usr/X11/lib
    #   check_lib X11
    #
    # Returns:
    #   "yes" and 0 on linkage success
    #   "no" and 1 on linkage failure
    if [[ $# < 1 ]]; then
        echo "ERROR: ${FUNCNAME[0]}: Missing library name" >&2
        return 2
    fi

    local name="$(mktemp)"
    local lib="${1}"
    local libdirs=""
    for libdir in "${@:2}"; do
        libdirs+="-L$libdir "
    done

    echo "$dummy_c" > "$name".c
    if ! "$CC" -o "${name}" -l"${lib}" $libdirs "${name}".c; then
        echo "no"
        rm -f "$name"
        rm -f "$name".c
        return 1
    fi
    echo "yes"
    rm -f "$name"
    rm -f "$name".c
    return 0
}

# Determine C compiler's absolute path
CC=${CC:-gcc}
CC="$(command -v $CC 2>/dev/null)"
if [[ ! -f "$CC" ]]; then
    echo "$CC could not be found" >&2
    exit 1
fi
if [[ -L "$CC" ]]; then
    CC="$(readlink $CC)"
fi

# Determine fortran compiler's absolute path
FC=${FC:-gfortran}
FC="$(command -v $FC 2>/dev/null)"
if [[ ! -f "$FC" ]]; then
    echo "$FC could not be found" >&2
    exit 1
fi
if [[ -L "$FC" ]]; then
    FC="$(readlink $FC)"
fi

with_x11="/usr/X11/lib"
with_sm="/usr/local/sm"
fflags=(
    -Wall
    -Wextra
    -ff2c
    -fdefault-double-8
    -fdefault-real-8
)
gcc_version="$($FC -v 2>&1 | tail -n 1 | sed -E -n 's/gcc\ version\ ([0-9]+.[0-9]+.[0-9]+).*/\1/p')"
gcc_major="${gcc_version%%\.*}"

# Handle GCC 10 requirements
if (( $gcc_major >= 10 )); then
    fflags+=(--allow-argument-mismatch)
fi

# Generate new argv list, with elements split on '=' or ' '
argv=()
for x in $@; do
    if [[ "$x" =~ .*=.* ]]; then
        key="${x%=*}"
        value="${x#*=}"
        argv+=($key)
        argv+=($value)
    else
        argv+=($x)
    fi
done


# Parse arguments
i=0
nargs=${#argv[@]}
while [[ $i < $nargs ]]; do
    key="${argv[$i]}"
    value="${argv[$i+1]}"
    case "$key" in
        --help|-h)
            do_help
            exit 0
            ;;
        --prefix)
            prefix="$value"
            (( i++ ))
            ;;
        --bindir)
            bindir="$value"
            (( i++ ))
            ;;
        --datadir)
            datadir="$value"
            (( i++ ))
            ;;
        --with-x11)
            with_x11="$value"
            (( i++ ))
            ;;
        --with-sm)
            with_sm="$value"
            (( i++ ))
            ;;
    esac
    (( i++ ))
done

# Assign default paths if not modified by the user
[[ -z "${prefix}" ]] && prefix="/usr/local"
[[ -z "${bindir}" ]] && bindir="${prefix}/bin"
[[ -z "${datadir}" ]] && datadir="${prefix}/share/moog"

# Convert fortran flag array to string
fflags="${fflags[@]}"

# Populate build templates
sed "s|@PREFIX@|${prefix}|g;\
    s|@BINDIR@|${bindir}|g;\
    s|@DATADIR@|${datadir}|g;\
    s|@FFLAGS@|${fflags}|g;\
    s|@WITH_X11@|${with_x11}|g;\
    s|@WITH_SM@|${with_sm}|g;\
    s|@FC@|${FC}|g;\
    " Makefile.in > Makefile
sed "s|@MOOGPATH_DEFAULT@|${datadir}/|" Moog.f.in > Moog.f

state_tcl=$(check_lib tcl)
state_tk=$(check_lib tk)
state_x11=$(check_lib X11 $with_x11)
state_sm_plotsub=$(check_lib plotsub $with_sm)
state_sm_devices=$(check_lib devices $with_sm)
state_sm_utils=$(check_lib utils $with_sm)

# Dump information
printf "
Configured with:

GCC version ........... $gcc_version
Fortran compiler ...... $FC
Installation prefix ... $prefix
X11 library ........... $with_x11
X11 works ............. $state_x11
TCL works ............. $state_tcl
TK works .............. $state_tk
SM library ............ $with_sm
SM (plotsub) works .... $state_sm_plotsub
SM (devices) works .... $state_sm_devices
SM (utils) works ...... $state_sm_utils

Run 'make' to compile
Run 'make install' to install
"