-
Notifications
You must be signed in to change notification settings - Fork 2
/
configure.sh
executable file
·225 lines (184 loc) · 5.44 KB
/
configure.sh
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env sh
# To be run from the root directory of the project.
#########################
# GLOBALS AND CONSTANTS #
#########################
#######################
# CONFIG FILE HELPERS #
#######################
# Generates a config file by appending to WRITE_TO, then copying to COPY_TO.
# Prints pretty messages along the way.
WRITE_TO="/tmp/GeneratedConfig.h"
COPY_TO="stdlib/Native/Configs/GeneratedConfig.h"
rm "$WRITE_TO" 2>/dev/null
touch "$WRITE_TO"
COLS=$(expr $(stty size | cut -d' ' -f2) - 23)
[ $COLS -gt 90 ] && COLS=57
IN_CMT="/* _DAI_STDLIB_GENERATEDCONFIG */"
msg() { printf "${GREEN}%-20s:${NORMAL} ${YELLOW}%${COLS}s${NORMAL}\n" "$1" "$2"; }
warn() { printf "${YELLOW}%-20s: %${COLS}s${NORMAL}\n" "$1" "$2"; exit 1; }
append() { printf "%s\n" "$1" >> "$WRITE_TO"; }
set_colors() {
ncolors=$(tput colors)
# TODO: Use escape sequences as they're more portable than tput
if [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
HAS_COLORS=1
NORMAL=$(tput sgr0)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
MAGENTA=$(tput setaf 5)
fi
}
test_compatibility() {
cat <<-_end_of_text
${MAGENTA}
#######################
# Compatibility Tests #
#######################
${NORMAL}
_end_of_text
# TODO delete temp files
# assertions
# TODO use mktemp if it's POSIX
# or use path prefix properly
cc config/assertions.c -D_DAI_RUNNING_CONFIGURE_SCRIPT -o assertions.cfg 1>/dev/null && msg "ASSERTIONS" "PASSED" ||
warn "Daisho is not supported on this system for the reason specified in the error message above"
rm assertions.cfg 2>/dev/null
# endianness
echo -n I | od -to2 | awk 'FNR==0{ print substr($2,6,1)}' && msg "ENDIANNESS" "PASSED" ||
warn "Daisho is not supported on Big-Endian or Unknown-Endianness machines."
# locale
cc config/locale.c -D_DAI_RUNNING_CONFIGURE_SCRIPT -o locale.cfg 1>/dev/null 2>&1
./locale.cfg && msg "UTF8 LOCALE" "PASSED" ||
warn "Daisho is not supported on systems that do not support the \"C.UTF-8\" locale."
rm locale.cfg 2>/dev/null
# strerror_r
cc config/strerror_r.c -D_DAI_RUNNING_CONFIGURE_SCRIPT -o strerror_r.cfg 1>/dev/null 2>&1
./strerror_r.cfg && msg "STRERROR_R" "PASSED" ||
warn "Daisho is not supported on systems without an XSI-compliant implementation of strerror_r."
rm strerror_r.cfg 2>/dev/null
}
#################################
# #
# CONFIGURATION VARIABLES #
# #
#################################
config_variables() {
cat <<-_end_of_text
${MAGENTA}
###########################
# Configuration Variables #
###########################
${NORMAL}
_end_of_text
cat <<-_end_of_header >>"$WRITE_TO"
/***************************/
/* Configuration Variables */
/***************************/
_end_of_header
# page size
PAGESIZE=$(getconf PAGE_SIZE)
msg "PAGE SIZE" "$PAGESIZE"
append "#define _DAI_PAGESIZE $PAGESIZE"
#################
# THREAD NUMBER #
#################
THREADS=$(grep -c processor /proc/cpuinfo)
msg "THREADS" "$THREADS"
append "#define _DAI_IDEAL_NUM_THREADS $THREADS"
###############
# STDLIB PATH #
###############
LIBPATH=$(realpath stdlib/)
msg "LIBPATH" "$LIBPATH"
append "#define _DAIC_LIB_INCLUDE_PATH \"$LIBPATH\""
}
gitrev() {
LONGREV=$(git rev-parse HEAD)
SHORTREV=$(git rev-parse --short HEAD)
VERSION_MAJOR="0"
VERSION_MINOR="0"
VERSION_SUBMINOR="1"
cat <<-_end_of_text
${MAGENTA}
###########
# Version #
###########
${NORMAL}
_end_of_text
cat <<-_end_of_header >>"$WRITE_TO"
/***********/
/* Version */
/***********/
_end_of_header
msg "Version" "0.0.1"
append "#define _DAI_VERSION \"$VERSION_MAJOR.$VERSION_MINOR.$VERSION_SUBMINOR\""
append "#define _DAI_VERSION_MAJOR $VERSION_MINOR"
append "#define _DAI_VERSION_MINOR $VERSION_MAJOR"
append "#define _DAI_VERSION_SUBMINOR $VERSION_SUBMINOR"
msg "Revision" "$LONGREV"
append "#define _DAI_SHORT_REV \"$SHORTREV\""
append "#define _DAI_LONG_REV \"$LONGREV\""
}
############################
# #
# SUPPORTED FEATURES #
# #
############################
supported_features() {
cat <<-_end_of_text
${MAGENTA}
######################
# Supported Features #
######################
${NORMAL}
_end_of_text
cat <<-_end_of_header >>"$WRITE_TO"
/**********************/
/* Supported Features */
/**********************/
_end_of_header
###############
# ANSI COLORS #
###############
msg "ANSI COLORS" "$HAS_COLORS"
append "#define _DAI_HAS_ANSI_COLORS $HAS_COLORS"
##############
# BACKTRACES #
##############
cc config/backtraces.c -D_DAI_RUNNING_CONFIGURE_SCRIPT -o backtraces.cfg 2>/dev/null
./backtraces.cfg 2>/dev/null
ret=$(expr $? = 0)
msg "BACKTRACES" "$ret"
append "#define _DAI_HAS_BACKTRACES $ret"
rm backtraces.cfg 2>/dev/null
####################
# LABELS AS VALUES #
####################
cc config/label_values.c -D_DAI_RUNNING_CONFIGURE_SCRIPT -o label_values.cfg 2>/dev/null
ret=$(expr $? = 0)
msg "LABEL VALUES" "$ret"
append "#define _DAI_HAS_LABEL_VALUES $ret"
rm label_values.cfg 2>/dev/null
append ""
}
write_config() {
test_compatibility
cat <<-_end_of_guard >"$WRITE_TO"
#pragma once
#ifndef _DAI_STDLIB_GENERATEDCONFIG
#define _DAI_STDLIB_GENERATEDCONFIG
_end_of_guard
gitrev
config_variables
supported_features
append "#endif $IN_CMT"
}
set_colors
write_config
cp "$WRITE_TO" "$COPY_TO"
cat <<_end_of_status
${MAGENTA}
Wrote config file to:
${NORMAL}${COPY_TO}
_end_of_status