aboutsummaryrefslogtreecommitdiff
path: root/configure
blob: 3e5d58f3d857437e21bb215bae50640dd383ecf2 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/sh
# vim:ft=sh:ts=4:sw=4:noexpandtab

# Configured variables #####################################################
# Described in following format:
# name:initial value:type:if exported to makefile:if exported to C header
# Where type can be just a string (in C in "") or plain. None implies plain.
CNFS="GEML_VERSION:0.1:string:F:T
GEML_CONTACT:cynerd@email.cz:string:F:T
PREFIX:/usr/local:string:T:F
EPREFIX:/usr/local:string:T:F
DEBUG:no::T:F
CPREFIX:::T:F
CC:cc::T:F
CFLAGS:::T:F
LFLAGS:::T:F
DOT:dot::T:F
MKDOCS:mkdocs::T:F"
############################################################################
# TODO add options to fine tune installation directories

print_help() {
	echo "Usage: ./configure [OPTION]..."
	echo "GEML configuration script. Execute this script to configure project"
	echo "and prepare it for building."
	echo
	echo "  --help, -h              Give this help list"
	echo "  --debug, -d             Enable development features."
	echo "  --release, -r           Disable development features for project."
	echo "  --dep-check             Force depencency check for this machine."
	echo "  --prefix=PREFIX         Set PREFIX as installation prefix for"
	echo "                          architecture independent files."
	echo "                          In default /usr/local"
	echo "  --exec-prefix=EPREFIX   Set EPREFIX as installation prefix for"
	echo "                          architecture dependent files."
	echo "                          In default PREFIX"
	echo
	echo "Environment variables:"
    echo "  CPREFIX                 Compilation tools prefix."
	echo "  CC                      C compiler command."
	echo "  CFLAGS                  C compiler flags."
	echo "  LDFLAGS                 C Linker flags."
	echo "  DOT                     Directed graphs drawing program."
	echo "  MKDOCS                  Project documentation generator."
	echo
	echo "GEML version $GEML_VERSION"
	echo "Report bugs to <$GEML_CONTACT>."
}

CONFIG_FILE=.config
CONFIGURED_FILE=.configured # TODO

# Backup variables from environment
ENVVARS="CPREFIX CC CFLAGS LDFLAGS DOT MKDOCS"
for E in $ENVVARS; do
    eval "[ -n \"\${$E+y}\" ]" && eval "BACKUP_$E=\$$E"
done

# TODO support for values with escaped colons
# Load default configuration if variable not set from environment
eval `echo "$CNFS" | sed -ne 's/^\([^:]*\):\([^:]*\).*$/\1=\2/p'`

# Load existing configuration
if [ -f "$CONFIG_FILE" ]; then
	. ./"$CONFIG_FILE"
fi

# Requested operation.
# c - Configure, default configuration behavior
# m - Prints output for Makefile
# h - Prints C header file (machine validation is skipped)
OP=c

# Parse arguments
while [ "$#" -gt 0 ]; do
	case $1 in
		-h|--help)
			print_help
			exit 0
			;;
		--prefix)
			PREFIX=$2
			shift
			;;
		--prefix=*)
			PREFIX=${1#--prefix=}
			;;
		-d|--debug)
			DEBUG=yes
			;;
		-r|--release)
			DEBUG=no
			;;
		--op-makefile)
			OP=m
			;;
		--op-h)
			OP=h
			;;
		*)
			echo Unknown option $1 1>&2
			exit 2
			;;
	esac
	shift
done

# Recover from enviroment variables
for E in $ENVVARS; do
    eval "[ -n \"\${BACKUP_$E+y}\" ]" && eval "$E=\$BACKUP_$E"
done

# Basically save configuration to file
configure() {
	echo "# GEML configuration file" > "$CONFIG_FILE"
	echo "# Please do not edit this file directly." >> "$CONFIG_FILE"
	echo "$CNFS" | while read L; do
		NAME=`echo "$L" | grep -o -E '^[^:]*'`
		eval "VALUE=\$$NAME"
		echo "$NAME=$VALUE" >> "$CONFIG_FILE"
	done
	echo "Configuration written to \"$CONFIG_FILE\""
}

# Generate Makefile and configure them if they doesn't exists in PWD
doext() {
	if [ -f "Makefile" ] || [ -f configure ]; then
		return
	fi
	GEMLDIR=`dirname "$0"`

	echo "# This is external Makefile for GEML." > Makefile
	echo >> Makefile
	echo "GEML_PATH = $GEMLDIR" >> Makefile
	echo >> Makefile
	echo "MAKEARGS := -C \"\$(GEML_PATH)\"" >> Makefile
	echo "MAKEARGS += O=\"\$(shell pwd)\"" >> Makefile
	echo >> Makefile
	echo "MAKEFLAGS += --no-print-directory" >> Makefile
	echo >> Makefile
	echo "Q ?= @" >> Makefile
	echo ".PHONY: all \$(MAKECMDGOALS)" >> Makefile
	echo "all \$(MAKECMDGOALS):" >> Makefile
	echo "	\$(Q)\$(MAKE) \$(MAKEARGS) \$@" >> Makefile

	echo Created local Makefile

	echo "# This is external configure script for GEML." > configure
	echo "GEML_PATH=$GEMLDIR" >> configure
	echo "cd \$(dirname \$0)" >> configure
	echo "\$GEML_PATH/configure \$@" >> configure
	chmod +x configure

	echo Created local configure script
}

makefile() {
	echo "$CNFS" | while read L; do
		if [ `echo $L | awk -F ':' '{ print $4 }'` != "T" ]; then
			continue
		fi
		NAME=`echo "$L" | grep -o -E '^[^:]*'`
		eval "VALUE=\$$NAME"
		echo "$NAME := $VALUE"
	done
}

cheader() {
	echo "$CNFS" | while read L; do
		if [ `echo $L | awk -F ':' '{ print $5 }'` != "T" ]; then
			continue
		fi
		NAME=`echo "$L" | grep -o -E '^[^:]*'`
		eval "VALUE=\$$NAME"
		if [ "`echo $L | awk -F ':' '{ print $3 }'`" = "string" ]; then
			echo "#define $NAME \"$VALUE\""
		else
			echo "#define $NAME $VALUE"
		fi
	done
}

validate() {
	echo -n
	# TODO check dependencies and programs
}

case $OP in
	c)
		validate
		doext
		configure
		;;
	m)
		validate
		makefile
		;;
	h)
		cheader
		;;
esac