aboutsummaryrefslogtreecommitdiffstats
path: root/base/new-c-proj
blob: af0b472e8beb2b77e495d73fc7372b4901bfec64 (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
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
#!/bin/sh

set -e

if ! test $# = 1; then
	printf 'usage %s: proj-name\n' "$0" >&2
	exit 1
fi

if printf '%s\n' "$1" | grep / >/dev/null 2>/dev/null; then
	printf 'project name cannot contain a slash\n' "$0" >&2
	exit 1
fi

mkdir -- "$1"
cd -- "$1"
git init .

cat > LICENSE <<EOF
ISC License

© $(date +%Y) Mattias Andrée <maandree@kth.se>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
EOF

cat > .gitignore <<EOF
*\#*
*~
*.o
*.a
*.lo
*.su
*.so
*.so.*
*.dll
*.dylib
*.gch
*.gcov
*.gcno
*.gcda
EOF

printf '_*\n' >> .git/info/exclude

if printf '%s\n' "$1" | grep '^lib' >/dev/null 2>/dev/null; then
	UPPERCASE="$(printf '%s\n' "$1" | tr '[a-z-]' '[A-Z_]')"
	LIBNAME="$(printf '%s\n' "$1" | sed 's/^lib//')"

	printf '%s\n' >> "$1".h \
		'/* See LICENSE file for copyright and license details. */' \
		"#ifndef ${UPPERCASE}_H" \
		"#define ${UPPERCASE}_H" \
		'' \
		'#endif'

	mkdir -p -- mk

	cat <<-EOF | sed 's/x\t/\t/g' > Makefile
		.POSIX:

		CONFIGFILE = config.mk
		include \$(CONFIGFILE)

		OS = linux
		# Linux:   linux
		# Mac OS:  macos
		# Windows: windows
		include mk/\$(OS).mk


		LIB_MAJOR = 1
		LIB_MINOR = 0
		LIB_VERSION = \$(LIB_MAJOR).\$(LIB_MINOR)
		LIB_NAME = \${LIBNAME}


		OBJ =

		HDR =\\
		x	$1.h

		LOBJ = \$(OBJ:.o=.lo)


		all: $1.a $1.\$(LIBEXT)
		\$(OBJ): \$(HDR)
		\$(LOBJ): \$(HDR)

		.c.o:
		x	\$(CC) -c -o \$@ \$< \$(CFLAGS) \$(CPPFLAGS)

		.c.lo:
		x	\$(CC) -fPIC -c -o \$@ \$< \$(CFLAGS) \$(CPPFLAGS)

		$1.a: \$(OBJ)
		x	@rm -f -- \$@
		x	\$(AR) rc \$@ \$(OBJ)

		$1.\$(LIBEXT): \$(LOBJ)
		x	\$(CC) \$(LIBFLAGS) -o $@ \$(LOBJ) \$(LDFLAGS)

		install: $1.a $1.\$(LIBEXT)
		x	mkdir -p -- "\$(DESTDIR)\$(PREFIX)/lib"
		x	mkdir -p -- "\$(DESTDIR)\$(PREFIX)/include"
		x	cp -- $1.a "\$(DESTDIR)\$(PREFIX)/lib/"
		x	cp -- $1.\$(LIBEXT) "\$(DESTDIR)\$(PREFIX)/lib/$1.\$(LIBMINOREXT)"
		x	ln -sf -- $1.\$(LIBMINOREXT) "\$(DESTDIR)\$(PREFIX)/lib/$1.\$(LIBMAJOREXT)"
		x	ln -sf -- $1.\$(LIBMAJOREXT) "\$(DESTDIR)\$(PREFIX)/lib/$1.\$(LIBEXT)"
		x	cp -- $1.h "\$(DESTDIR)\$(PREFIX)/include/"

		uninstall:
		x	-rm -f -- "\$(DESTDIR)\$(PREFIX)/lib/$1.a"
		x	-rm -f -- "\$(DESTDIR)\$(PREFIX)/lib/$1.\$(LIBMAJOREXT)"
		x	-rm -f -- "\$(DESTDIR)\$(PREFIX)/lib/$1.\$(LIBMINOREXT)"
		x	-rm -f -- "\$(DESTDIR)\$(PREFIX)/lib/$1.\$(LIBEXT)"
		x	-rm -f -- "\$(DESTDIR)\$(PREFIX)/include/$1.h"

		clean:
		x	-rm -f -- *.o *.a *.lo *.su *.so *.so.* *.dll *.dylib
		x	-rm -f -- *.gch *.gcov *.gcno *.gcda *.\$(LIBEXT)

		.SUFFIXES:
		.SUFFIXES: .lo .o .c

		.PHONY: all install uninstall clean
	EOF

	cat <<-EOF | sed 's/x\t/\t/g' > mk/linux.mk
		LIBEXT      = so
		LIBFLAGS    = -shared -Wl,-soname,lib\$(LIB_NAME).\$(LIBEXT).\$(LIB_MAJOR)
		LIBMAJOREXT = \$(LIBEXT).\$(LIB_MAJOR)
		LIBMINOREXT = \$(LIBEXT).\$(LIB_VERSION)
	EOF

	cat <<-EOF | sed 's/x\t/\t/g' > mk/macos.mk
		LIBEXT      = dylib
		LIBFLAGS    = -dynamiclib
		LIBMAJOREXT = \$(LIB_MAJOR).\$(LIBEXT)
		LIBMINOREXT = \$(LIB_VERSION).\$(LIBEXT)
	EOF

	cat <<-EOF | sed 's/x\t/\t/g' > mk/windows.mk
		LIBEXT      = dll
		LIBFLAGS    = -mdll
		LIBMAJOREXT = \$(LIB_MAJOR).\$(LIBEXT)
		LIBMINOREXT = \$(LIB_VERSION).\$(LIBEXT)
	EOF
else
	printf '%s\n' "/$1"  >> .gitignore

	printf '%s\n' '/* See LICENSE file for copyright and license details. */'  >> "$1".c

	cat <<-EOF | sed 's/x\t/\t/g' > Makefile
		.POSIX:

		CONFIGFILE = config.mk
		include \$(CONFIGFILE)

		OBJ =\\
		x	$1.o

		HDR =

		all: $1
		\$(OBJ): \$(HDR)

		.c.o:
		x	\$(CC) -c -o \$@ \$< \$(CFLAGS) \$(CPPFLAGS)

		$1: \$(OBJ)
		x	\$(CC) -o \$@ \$(OBJ) \$(LDFLAGS)

		install: $1
		x	mkdir -p -- "\$(DESTDIR)\$(PREFIX)/bin"
		x	mkdir -p -- "\$(DESTDIR)\$(MANPREFIX)/man1/"
		x	cp -- $1 "\$(DESTDIR)\$(PREFIX)/bin/"
		x	cp -- $1.1 "\$(DESTDIR)\$(MANPREFIX)/man1/"

		uninstall:
		x	-rm -f -- "\$(DESTDIR)\$(PREFIX)/bin/$1"
		x	-rm -f -- "\$(DESTDIR)\$(MANPREFIX)/man1/$1.1"

		clean:
		x	-rm -f -- *.o *.a *.lo *.su *.so *.so.* *.gch *.gcov *.gcno *.gcda
		x	-rm -f -- $1

		.SUFFIXES:
		.SUFFIXES: .o .c

		.PHONY: all install uninstall clean
	EOF
fi

cat > config.mk <<-EOF
	PREFIX    = /usr
	MANPREFIX = \$(PREFIX)/share/man

	CC = cc

	CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE
	CFLAGS   = -std=c99 -Wall -g
	LDFLAGS  =
EOF