File: objfw-compile

package info (click to toggle)
objfw 1.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,644 kB
  • sloc: objc: 101,491; asm: 5,083; sh: 4,092; makefile: 1,574; ansic: 709; xml: 367; pascal: 214
file content (362 lines) | stat: -rw-r--r-- 7,845 bytes parent folder | download | duplicates (2)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/bin/sh
#
#  Copyright (c) 2008-2025 Jonathan Schleifer <js@nil.im>
#
#  All rights reserved.
#
#  This program is free software: you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License version 3.0 only,
#  as published by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
#  version 3.0 for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  version 3.0 along with this program. If not, see
#  <https://www.gnu.org/licenses/>.
#

if test x"$(basename "$0")" != x"objfw-compile"; then
	OBJFW_CONFIG="$(basename "$0" | sed 's/-objfw-compile$//')-objfw-config"
else
	OBJFW_CONFIG="objfw-config"
fi

if ! which $OBJFW_CONFIG >/dev/null 2>&1; then
	echo "You need to have ObjFW and $OBJFW_CONFIG installed!"
	exit 1
fi

parse_packages() {
	packages=""

	while test x"$1" != "x"; do
		case "$1" in
		--package)
			shift
			packages="$packages --package $1"
			;;
		esac
		shift
	done
}
parse_packages "$@"

parse_static_libs() {
	static_libs="no"

	while test x"$1" != "x"; do
		case "$1" in
		--static-libs)
			static_libs="yes"
			break
			;;
		esac
		shift
	done
}
parse_static_libs "$@"

show_help() {
	cat >&2 <<__EOF__
Syntax: objfw-compile -o output [flags] source1.m source2.mm ...

    -o name         Specify the output name (not file name!)
    --arc           Use automatic reference counting
    --lib version   Compile a library (with the specified version) instead of
                    an application
    --plugin        Compile a plugin instead of an application
    --package name  Use the specified package
    --builddir dir  Place built objects into the specified directory
    --static-libs   Link ObjFW and packages statically
    -D*  -D *       Pass the specified define to the compiler
    -framework *    Pass the specified -framework argument to the linker
                    (macOS / iOS only)
    -f*             Pass the specified -f flag to the compiler
    -F* -F *        Pass the specified -F flag to the linker (macOS / iOS only)
    -g*             Pass the specified -g flag to the compiler
    -I*  -I *       Pass the specified -I flag to the compiler
    -l*  -l *       Pass the specified -l flag to the linker
    -L*  -L *       Pass the specified -L flag to the linker
    -m*             Pass the specified -m flag to the compiler
    -O*             Pass the specified -O flag to the compiler
    -pthread        Pass -pthread to the compiler and linker
    -std=*          Pass the specified -std= flag to the compiler
    -Wl,*           Pass the specified -Wl, flag to the linker
    -W*             Pass the specified -W flag to the compiler
    --help          Show this help
__EOF__
}

CPPFLAGS="$CPPFLAGS $($OBJFW_CONFIG $packages --cppflags)"
OBJC="$($OBJFW_CONFIG --objc)"
OBJCFLAGS="$OBJCFLAGS $($OBJFW_CONFIG $packages --objcflags) -Wall -g"
if test x"$static_libs" = x"yes"; then
	LIBS="$LIBS $($OBJFW_CONFIG $packages --static-libs)"
else
	LIBS="$LIBS $($OBJFW_CONFIG $packages --libs)"
fi
LDFLAGS="$LDFLAGS $($OBJFW_CONFIG $packages --ldflags --rpath)"

if test x"$1" = "x"; then
	show_help
	exit 1
fi

status_compiling() {
	printf "\033[K\033[0;33mCompiling \033[1;33m%s\033[0;33m...\033[0m\r" \
		"$1"
}
status_compiled() {
	printf "\033[K\033[0;32mSuccessfully compiled \033[1;32m%s\033[0;32m." \
		"$1"
	printf "\033[0m\n"
}
status_compile_failed() {
	printf "\033[K\033[0;31mFailed to compile \033[1;31m%s\033[0;31m!" "$1"
	printf "\033[0m\n"
	exit $2
}
status_linking() {
	printf "\033[K\033[0;33mLinking \033[1;33m%s\033[0;33m...\033[0m\r" "$1"
}
status_linked() {
	printf "\033[K\033[0;32mSuccessfully linked \033[1;32m%s\033[0;32m." \
		"$1"
	printf "\033[0m\n"
}
status_link_failed() {
	printf "\033[K\033[0;31mFailed to link \033[1;31m%s\033[0;31m!" "$1"
	printf "\033[0m\n"
	exit $2
}

srcs=""
out=""
objs=""
builddir=""
link="no"
link_stdcpp="no"
lib="no"
plugin="no"
static="no"
out_prefix=""
out_suffix=""

while test x"$1" != "x"; do
	case "$1" in
	-o|--out)
		shift
		out="$1"
		;;
	--lib)
		if test x"$plugin" = x"yes"; then
			echo "You can't use --lib and --plugin!"
			exit 1
		fi

		shift

		if ! echo "$1" | grep "^[0-9]\+\.[0-9]\+$" >/dev/null; then
			echo "$1 is not a valid library version!"
			exit 1
		fi

		export LIB_MAJOR="${1%.*}"
		export LIB_MINOR="${1#*.}"

		lib="yes"
		OBJCFLAGS="$OBJCFLAGS $($OBJFW_CONFIG --lib-cflags)"
		out_prefix="$($OBJFW_CONFIG --lib-prefix)"
		out_suffix="$($OBJFW_CONFIG --lib-suffix)"
		;;
	--package)
		# Already included into the flags.
		shift
		;;
	--plugin)
		if test x"$lib" = x"yes"; then
			echo "You can't use --lib and --plugin!"
			exit 1
		fi

		plugin="yes"
		OBJCFLAGS="$OBJCFLAGS $($OBJFW_CONFIG --plugin-cflags)"
		LDFLAGS="$LDFLAGS $($OBJFW_CONFIG --plugin-ldflags)"
		out_suffix="$($OBJFW_CONFIG --plugin-suffix)"
		;;
	--arc)
		OBJCFLAGS="$OBJCFLAGS $($OBJFW_CONFIG --arc)"
		;;
	--builddir)
		shift
		builddir="$1"
		;;
	--static-libs)
		# Already handled separately.
		;;
	-D)
		shift
		CPPFLAGS="$CPPFLAGS -D$1"
		;;
	-D*)
		CPPFLAGS="$CPPFLAGS $1"
		;;
	-framework)
		shift
		LIBS="$LIBS -framework $1"
		;;
	-f*)
		OBJCFLAGS="$OBJCFLAGS $1"
		;;
	-F)
		shift
		LIBS="$LIBS -F$1"
		;;
	-F*)
		LIBS="$LIBS $1"
		;;
	-g*)
		OBJCFLAGS="$OBJCFLAGS $1"
		;;
	-I)
		shift
		CPPFLAGS="$CPPFLAGS -I$1"
		;;
	-I*)
		CPPFLAGS="$CPPFLAGS $1"
		;;
	-l)
		shift
		LIBS="$LIBS -l$1"
		;;
	-l*)
		LIBS="$LIBS $1"
		;;
	-L)
		shift
		LIBS="$LIBS -L$1"
		;;
	-L*)
		LIBS="$LIBS $1"
		;;
	-m*)
		OBJCFLAGS="$OBJCFLAGS $1"
		;;
	-O*)
		OBJCFLAGS="$OBJCFLAGS $1"
		;;
	-pthread)
		OBJCFLAGS="$OBJCFLAGS $1"
		LDFLAGS="$LDFLAGS $1"
		;;
	-std=*)
		OBJCFLAGS="$OBJCFLAGS $1"
		;;
	-Wl,*)
		LDFLAGS="$LDFLAGS $1"
		;;
	-W*)
		OBJCFLAGS="$OBJCFLAGS $1"
		;;
	--help)
		show_help
		exit 0
		;;
	-*)
		echo "Unknown option: $1"
		exit 1
		;;
	*.m)
		srcs="$srcs $1"
		;;
	*.mm)
		srcs="$srcs $1"
		link_stdcpp="yes"
		;;
	*)
		echo "Only .m and .mm files can be compiled!" 1>&2
		exit 1
		;;
	esac

	shift
done

if test x"$out" = x""; then
	echo "No output name specified! Use -o or --out!"
	exit 1
fi

case "$builddir" in
"")
	;;
*/)
	;;
*)
	builddir="$builddir/"
	;;
esac

for i in $srcs; do
	case $i in
	*.m)
		if test x"$lib" = x"yes"; then
			obj="$builddir${i%.m}.lib.o"
		elif test x"$plugin" = x"yes"; then
			obj="$builddir${i%.m}.plugin.o"
		else
			obj="$builddir${i%.m}.o"
		fi
		;;
	*.mm)
		if test x"$lib" = x"yes"; then
			obj="$builddir${i%.mm}.lib.o"
		elif test x"$plugin" = x"yes"; then
			obj="$builddir${i%.mm}.plugin.o"
		else
			obj="$builddir${i%.mm}.o"
		fi
		;;
	esac
	objs="$objs $obj"
	build="no"

	if test ! -f "$obj" -o "$i" -nt "$obj"; then
		build="yes"
	else
		deps=$($OBJC -E -M $CPPFLAGS $OBJCFLAGS $i |
			sed -e 's/.*: //' -e 's/\\//g')
		for dep in $deps; do
			test "$dep" -nt $obj && build="yes"
		done
	fi

	if test x"$build" = x"yes"; then
		link="yes"
		status_compiling $i
		mkdir -p "$(dirname $obj)" || status_compile_failed $i $?
		$OBJC $CPPFLAGS $OBJCFLAGS -c -o $obj $i || \
			status_compile_failed $i $?
		status_compiled $i
	fi
done

test x"$lib" = x"no" -a x"$plugin" = x"no" && \
	out_suffix="$($OBJFW_CONFIG --prog-suffix)"

test x"$link_stdcpp" = x"yes" && LIBS="$LIBS -lstdc++"

if test x"$lib" = x"yes"; then
	export SHARED_LIB="$out_prefix$out$out_suffix"
	LDFLAGS="$LDFLAGS $($OBJFW_CONFIG --lib-ldflags)"
fi

if test ! -f "$out_prefix$out$out_suffix" -o x"$link" = x"yes"; then
	status_linking $out_prefix$out$out_suffix
	$OBJC -o $out_prefix$out$out_suffix $objs $LIBS $LDFLAGS || \
		status_link_failed $out $?
	status_linked $out_prefix$out$out_suffix
fi