summaryrefslogtreecommitdiff
path: root/project/config/cfgdefs.sh
blob: 2ff76312fe09516d1911165879e980d0fb696774 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# in projects where [ $mb_use_custom_cfgdefs = yes ],
# cfgdefs.sh is invoked from within ./configure via
# . $mb_project_dir/project/cfgdefs.sh

# a successful return from cfgdefs.sh will be followed
# by a second invocation of the config_copy() function,
# reflecting any changes to common config variables
# made by cfgdefs.sh.

# finally, cfgdefs.sh may update the contents of the
# config-time generated cfgdefs.mk.


# sofort's config test framework
. "$mb_project_dir/sofort/cfgtest/cfgtest.sh"


for arg ; do
	case "$arg" in
		*)
			error_msg ${arg#}: "unsupported config argument."
			exit 2
	esac
done


cfgdefs_bootstrap()
{
	cfgdefs_bootstrap_done=${cfgdefs_bootstrap_done:-}

	if [ _$cfgdefs_bootstrap_done = _yes ]; then
		return 0
	fi

	cfgdefs_objs=

	cfgdefs_utilsrc='main.c'

	cfgdefs_sources="arch.c buf.c compat.c cond.c dir.c for.c hash.c job.c"
	cfgdefs_sources="$cfgdefs_sources lst.c make.c make_malloc.c meta.c metachar.c parse.c"
	cfgdefs_sources="$cfgdefs_sources str.c suff.c targ.c trace.c util.c var.c"

	cfgdefs_cflags=
	cfgdefs_cflags="$cfgdefs_cflags -I${mb_source_dir}/src/internal"
	cfgdefs_cflags="$cfgdefs_cflags -D_PATH_DEFSYSPATH=\"${mb_source_dir}/mk\""
	cfgdefs_cflags="$cfgdefs_cflags $cfgdefs_added_defs"
	cfgdefs_cflags="$cfgdefs_cflags -D_XOPEN_SOURCE=700"

	cfgtest_native_section

	if cfgtest_compiler_switch -std=c99; then
		cfgdefs_cflags="$cfgdefs_cflags -std=c99"
	fi

	cfgdefs_make="$mb_pwd/bootstrap/make"

	mkdir -p "$mb_pwd/bootstrap"

	output_section_break
	output_script_status 'bootstrapping' './bootstrap/make'

	for src in $cfgdefs_utilsrc; do
		obj=${src%%.c}.o
		cfgdefs_objs="$cfgdefs_objs $mb_pwd/bootstrap/${obj}"

		output_step_prolog 'compiling' "./bootstrap/${obj}"

		printf '\n$ %s %s\n'  "$mb_native_cc $mb_native_cflags" '\' >&3
		printf '\n$ %s\n'     "$cfgdefs_cflags" '\'                 >&3
		printf '\t-c %s %s\n' "$mb_source_dir/src/${src}" '\'    >&3
		printf '\t-o %s %s\n\n\n' "$mb_pwd/bootstrap/${obj}"     >&3

		"$mb_native_cc" $mb_native_cflags $cfgdefs_cflags \
			-c "$mb_source_dir/src/${src}"             \
			-o "$mb_pwd/bootstrap/${obj}"               \
			2>&3

		output_step_epilog
	done

	for src in $cfgdefs_sources; do
		obj=${src%%.c}.o
		cfgdefs_objs="$cfgdefs_objs $mb_pwd/bootstrap/${obj}"

		output_step_prolog 'compiling' "./bootstrap/${obj}"

		printf '\n$ %s %s\n'  "$mb_native_cc $mb_native_cflags" '\' >&3
		printf '\n$ %s\n'     "$cfgdefs_cflags" '\'                 >&3
		printf '\t-c %s %s\n' "$mb_source_dir/src/logic/${src}" '\' >&3
		printf '\t-o %s %s\n\n\n' "$mb_pwd/bootstrap/${obj}"        >&3

		"$mb_native_cc" $mb_native_cflags $cfgdefs_cflags \
			-c "$mb_source_dir/src/logic/${src}"       \
			-o "$mb_pwd/bootstrap/${obj}"               \
			2>&3

		output_step_epilog
	done

	output_step_prolog 'linking' './bootstrap/make'

	printf '\n$ %s %s\n' "$mb_native_cc $cfgdefs_cflags" '\' >&3
	printf '\t %s %s\n' "$mb_native_ldflags" '\'             >&3
	printf '\t %s %s\n' "$cfgdefs_objs" '\'                  >&3
	printf '\t-o %s %s\n\n\n' "$cfgdefs_make"                >&3

	"$mb_native_cc" $mb_native_cflags $cfgdefs_cflags \
		$mb_native_ldflags $cfgdefs_objs           \
		-o "$cfgdefs_make"                          \
		2>&3

	output_step_epilog

	mb_make="$cfgdefs_make"

	cfgdefs_bootstrap_done='yes'

	output_section_break
}


cfgdefs_output_custom_defs()
{
	cat "$mb_project_dir/project/config/cfgdefs.in" > cfgdefs.mk
}


cfgdefs_perform_common_tests()
{
	# bootstrap
	cfgdefs_added_defs=

	# interfaces
	mb_cfgtest_headers='stdio.h'
	cfgtest_interface_presence 'vsnprintf'

	if [ $? = '0' ]; then
		cfgdefs_added_defs="$cfgdefs_added_defs -DHAVE_VSNPRINTF"
	fi

	mb_cfgtest_headers='string.h'
	cfgtest_interface_presence 'strerror'

	if [ $? = '0' ]; then
		cfgdefs_added_defs="$cfgdefs_added_defs -DHAVE_STRERROR"
	fi

	mb_cfgtest_headers='sys/types.h unistd.h'
	cfgtest_interface_presence 'setpgid'

	if [ $? = '0' ]; then
		cfgdefs_added_defs="$cfgdefs_added_defs -DHAVE_SETPGID"
	fi

	mb_cfgtest_headers='sys/time.h sys/resource.h'
	cfgtest_interface_presence 'setrlimit'

	if [ $? = '0' ]; then
		cfgdefs_added_defs="$cfgdefs_added_defs -DHAVE_SETRLIMIT"
	fi

	mb_cfgtest_headers='stdlib.h'
	cfgtest_interface_presence 'setenv'

	if [ $? = '0' ]; then
		cfgdefs_added_defs="$cfgdefs_added_defs -DHAVE_SETENV"
	fi

	cfgtest_newline
}


cfgdefs_perform_native_tests()
{
	# init
	cfgtest_newline
	cfgtest_native_section

	# common tests
	cfgdefs_perform_common_tests

	# pretty cfgdefs.mk
	cfgtest_newline
}

cfgdefs_perform_target_tests()
{
	# init
	cfgtest_newline
	cfgtest_host_section

	# common tests
	cfgdefs_perform_common_tests

	# pretty cfgdefs.mk
	cfgtest_newline
}

# cfgdefs.in --> cfgdefs.mk
cfgdefs_output_custom_defs

# strict: some tests might fail
set +e

# bootstrapping cflags
cfgdefs_perform_native_tests

# strict: restore mode
set -e

# bootstrap
cfgdefs_bootstrap

# strict: some tests might fail
set +e

# target-specific tests
cfgdefs_perform_target_tests

# strict: restore mode
set -e

# all done
return 0