aboutsummaryrefslogtreecommitdiff
path: root/Makefile.common
blob: 70235402b28f89e30ffe2973ba3345dce9015fab (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
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
# next line has to be edited manually
PD_DIR = $(ROOT_DIR)/../pd/src

OS_NAME = $(shell uname -s)
ifneq ($(OS_NAME),Linux)
ifneq ($(OS_NAME),Darwin)
# FIXME
endif
endif

TILDE = ~

-include Makefile.dirs
include Makefile.sources
include Makefile.objects

CC = gcc

default: define_build all

SHARED_DIR = $(ROOT_DIR)/shared
OBJ_DIR = .
OUT_DIR = $(ROOT_DIR)/bin

BASE_DIR = $(shell basename `pwd`)
BASE_NAME = $(shell basename `pwd` | awk -F - '{print $$1}')

# CX: control external's main file
# AX: audio (tilde) external's main file
# LX: symlink external's main file
#     (if symlink external is tilde-only, use OTHER_SOURCES and AX_CLASSES)

CX_NAMES = $(notdir $(CX_SOURCES))
AX_NAMES = $(notdir $(AX_SOURCES))
LX_NAMES = $(notdir $(LX_SOURCES))
TYPES_NAMES = $(notdir $($1_SOURCES))

CX_DIR = $(dir $(firstword $(CX_SOURCES)))
AX_DIR = $(dir $(firstword $(AX_SOURCES)))
TYPES_DIR = $(dir $(firstword $($1_SOURCES)))

TYPES_EXTERNS = \
	$(patsubst %.c,%$($1_TILDE).$(X_SUFFIX),$(call TYPES_NAMES,$1))

SOURCES = $(CX_SOURCES) $(AX_SOURCES) $(LX_SOURCES) $(OTHER_SOURCES) \
	$(foreach type,$(TYPES),$($(type)_SOURCES))

INCLUDES = -I. -I$(PD_DIR) -I$(SHARED_DIR)

ifeq ($(OS_NAME),Linux)
X_SUFFIX = pd_linux
DEFINES = -DUNIX
OPT_CFLAGS = -O6 -funroll-loops -fomit-frame-pointer
LFLAGS = -export_dynamic  -shared
endif

ifeq ($(OS_NAME),Darwin)
X_SUFFIX = pd_darwin
DEFINES = -DUNIX -DMACOSX
OPT_CFLAGS = -O2 -funroll-loops -fomit-frame-pointer
# -private_bundle is a guess, LATER check this
LFLAGS = -bundle -undefined suppress -flat_namespace
endif

WARN_CFLAGS = -Wall -W -Wstrict-prototypes -Werror \
	-Wno-unused -Wno-parentheses -Wno-switch
DBG_CFLAGS = 
CFLAGS = $(WARN_CFLAGS) $(OPT_CFLAGS) $(DEFINES) $(INCLUDES)

EXTERNS = $(foreach fn,$(CX_NAMES:.c=.$(X_SUFFIX)),$(OUT_DIR)/$(fn)) \
	$(foreach fn,$(AX_NAMES:.c=~.$(X_SUFFIX)),$(OUT_DIR)/$(fn)) \
	$(foreach fn,$(LX_SOURCES:.c=.$(X_SUFFIX)),$(OUT_DIR)/$(fn)) \
	$(foreach fn,$(CX_CLASSES:=.$(X_SUFFIX)),$(OUT_DIR)/$(fn)) \
	$(foreach fn,$(AX_CLASSES:=~.$(X_SUFFIX)),$(OUT_DIR)/$(fn)) \
	$(foreach type,$(TYPES),\
		$(foreach fn,$(call TYPES_EXTERNS,$(type)),$(OUT_DIR)/$(fn)))

TYPES_RULE = $(foreach fn,$(call TYPES_EXTERNS,$1),$(OUT_DIR)/$(fn)): \
		$(OUT_DIR)/%$($1_TILDE).$(X_SUFFIX) \
		: $(call TYPES_DIR,$1)%.o $($1_PRIVATEOBJECTS) \
			$(foreach obj,$($1_OBJECTS),$(SHARED_DIR)/$(obj)) \
	; $(CC) -o $$@ $(CFLAGS) $(LFLAGS) $$+

# LATER find a better way...
$(if $(word 1,$(TYPES)),$(call TYPES_RULE,$(word 1,$(TYPES))))
$(if $(word 2,$(TYPES)),$(call TYPES_RULE,$(word 2,$(TYPES))))
$(if $(word 3,$(TYPES)),$(call TYPES_RULE,$(word 3,$(TYPES))))
$(if $(word 4,$(TYPES)),$(call TYPES_RULE,$(word 4,$(TYPES))))
$(if $(word 5,$(TYPES)),$(call TYPES_RULE,$(word 5,$(TYPES))))
$(if $(word 6,$(TYPES)),$(call TYPES_RULE,$(word 6,$(TYPES))))
$(if $(word 7,$(TYPES)),$(call TYPES_RULE,$(word 7,$(TYPES))))
$(if $(word 8,$(TYPES)),$(call TYPES_RULE,$(word 8,$(TYPES))))
$(if $(word 9,$(TYPES)),$(call TYPES_RULE,$(word 9,$(TYPES))))
$(if $(word 10,$(TYPES)),$(call TYPES_RULE,$(word 10,$(TYPES))))
$(if $(word 11,$(TYPES)),$(call TYPES_RULE,$(word 11,$(TYPES))))
$(if $(word 12,$(TYPES)),$(call TYPES_RULE,$(word 12,$(TYPES))))
$(if $(word 13,$(TYPES)),$(call TYPES_RULE,$(word 13,$(TYPES))))
$(if $(word 14,$(TYPES)),$(call TYPES_RULE,$(word 14,$(TYPES))))
$(if $(word 15,$(TYPES)),$(call TYPES_RULE,$(word 15,$(TYPES))))
$(if $(word 16,$(TYPES)),$(call TYPES_RULE,$(word 16,$(TYPES))))
$(if $(word 17,$(TYPES)),$(call TYPES_RULE,$(word 17,$(TYPES))))
$(if $(word 18,$(TYPES)),$(call TYPES_RULE,$(word 18,$(TYPES))))
$(if $(word 19,$(TYPES)),$(call TYPES_RULE,$(word 19,$(TYPES))))
$(if $(word 20,$(TYPES)),$(call TYPES_RULE,$(word 20,$(TYPES))))
$(if $(word 21,$(TYPES)),$(call TYPES_RULE,$(word 21,$(TYPES))))
$(if $(word 22,$(TYPES)),$(call TYPES_RULE,$(word 22,$(TYPES))))
$(if $(word 23,$(TYPES)),$(call TYPES_RULE,$(word 23,$(TYPES))))
$(if $(word 24,$(TYPES)),$(call TYPES_RULE,$(word 24,$(TYPES))))

OBJECTS = $(foreach fn,$(OTHER_SOURCES:.c=.o),$(OBJ_DIR)/$(fn)) \
	$(foreach fn,$(LX_SOURCES:.c=.o),$(OBJ_DIR)/$(fn)) \
	$(foreach fn,$(SHARED_OBJECTS),$(SHARED_DIR)/$(fn))

# lame...
.PRECIOUS: %.o

# rules for symlink externals must precede generic extern rules
$(OUT_DIR)/$(BASE_NAME)_%~.$(X_SUFFIX): $(OUT_DIR)/$(BASE_NAME).$(X_SUFFIX)
	-ln -sf $(BASE_NAME).$(X_SUFFIX) $@
$(OUT_DIR)/$(BASE_NAME)_%.$(X_SUFFIX): $(OUT_DIR)/$(BASE_NAME).$(X_SUFFIX)
	-ln -sf $(BASE_NAME).$(X_SUFFIX) $@

# library stub
# if not equal BASE_NAME, it has to be declared explicitly in BASE_DIR/Makefile
$(OUT_DIR)/$(LX_STUB)_%~.$(X_SUFFIX): $(OUT_DIR)/$(BASE_NAME).$(X_SUFFIX)
	-ln -sf $(BASE_NAME).$(X_SUFFIX) $@
$(OUT_DIR)/$(LX_STUB)_%.$(X_SUFFIX): $(OUT_DIR)/$(BASE_NAME).$(X_SUFFIX)
	-ln -sf $(BASE_NAME).$(X_SUFFIX) $@
$(OUT_DIR)/$(LX_STUB).$(X_SUFFIX): $(OUT_DIR)/$(BASE_NAME).$(X_SUFFIX)
	-ln -sf $(BASE_NAME).$(X_SUFFIX) $@

# generic rule for audio externals
$(OUT_DIR)/%~.$(X_SUFFIX): $(AX_DIR)%.c $(OBJECTS)
	$(CC) -o $@ $(CFLAGS) $(LFLAGS) $(OBJECTS) $<

# generic rule for control externals
$(OUT_DIR)/%.$(X_SUFFIX): $(CX_DIR)%.c $(OBJECTS)
	$(CC) -o $@ $(CFLAGS) $(LFLAGS) $(OBJECTS) $<

SUBDIRS = @for i in $(MIXED_DIRS) and_in_case_it_is_null ; \
		do ( if [ -d $$i ] ; then \
			cd $$i; $(MAKE) $@ ; fi ) ; done

SUBDIRS_DEFAULT = @for i in $(MIXED_DIRS) and_in_case_it_is_null ; \
		do ( if [ -d $$i ] ; then \
			cd $$i; $(MAKE) ; fi ) ; done

define_build:
	@if [[ -f build_counter && \
		`date -r build_counter +%j` != `date +%j` ]] ; then \
		mv build_counter build_counter~ ; \
		echo -n 'increment build counter (y/n)? [n]' ; read doit ; \
		if [ $$doit ] && [ $$doit == 'y' ] ; then \
			cat build_counter~ | \
	awk '{if (index($$2,"BUILD")) print $$1, $$2, $$3+1; else print}' \
			> build_counter ; \
			cat build_counter | \
	awk 'BEGIN {printf toupper("$(BASE_NAME)_VERSION = ")} \
	    {gsub("\"","",$$3); printf $$3; if (NR==1) printf "-"} \
	    END {printf "\r\n" }' > $(ROOT_DIR)/ViCious/$(BASE_DIR)/version ; \
		else cat build_counter~ > build_counter ; fi \
	fi

all$(BASE_NAME)s.c: Makefile.sources
	$(ROOT_DIR)/dumpsetups > $@

all: $(EXTERNS)
	$(SUBDIRS_DEFAULT)

clean: emptydeps
# remove all objects and externs that are contained in current directory
	-rm -f *.o *.$(X_SUFFIX)
	$(SUBDIRS)

cleanall: clean
# remove default target externs
	-rm -f $(EXTERNS)

OLDROOT_DIR = $(shell ls -d -1 ../../Mixed-* | tail -1)

diff:
	@echo -n > diff.out ; \
	for f in {*.h,*.c} ; do \
		if [ -f $$f ] ; then \
			diff -u $(OLDROOT_DIR)/$(BASE_DIR)/$$f $$f >> diff.out ; \
		fi \
	done

VERSION = $(shell awk \
	'{if (index($$2,"VERSION")) print substr($$3,2,length($$3)-2)}' \
	build_counter)
RELEASE = $(shell awk \
	'{if (index($$2,"RELEASE")) print substr($$3,2,length($$3)-2)}' \
	build_counter)
BUILD = $(shell awk '{if (index($$2,"BUILD")) print $$3}' build_counter)

RELEASE_STUB = release/$(BASE_NAME)-$(VERSION)-$(RELEASE)$(BUILD)
SRC_TAR = $(RELEASE_STUB)-src.tar
BIN_TAR = $(RELEASE_STUB)-bin.tar
LIB_TAR = $(RELEASE_STUB)-lib.tar
DOC_TAR = $(RELEASE_STUB)-doc.tar
ALL_TAR = $(RELEASE_STUB)-all.tar
REL_BINDIR = bin

REL_LIBNAMES = $(foreach fn,$(RELEASE_LIBS:=.$(X_SUFFIX)),$(REL_BINDIR)/$(fn))

REL_EXTERNS = $(foreach fn,$(CX_NAMES:.c=.$(X_SUFFIX)),$(REL_BINDIR)/$(fn)) \
	$(foreach fn,$(AX_NAMES:.c=~.$(X_SUFFIX)),$(REL_BINDIR)/$(fn)) \
	$(REL_LIBNAMES) $(foreach fn,$(RELEASE_APPS),$(REL_BINDIR)/$(fn)) \
	$(foreach type,$(TYPES),\
		$(foreach fn,$(call TYPES_EXTERNS,$(type)),$(REL_BINDIR)/$(fn)))

REL_ROOTSRC = LICENSE.txt Makefile.common dumpsetups

srelease: emptydeps define_build
	cd $(ROOT_DIR)/shared; $(MAKE) emptydeps
	cd $(ROOT_DIR); tar -X $(BASE_DIR)/$(BASE_NAME)-all.exclude -cf \
		$(SRC_TAR) $(BASE_DIR)/* $(REL_ROOTSRC)
	cd $(ROOT_DIR); tar -T $(BASE_DIR)/$(BASE_NAME)-shared.include -rf \
		$(SRC_TAR)
	cd $(ROOT_DIR); gzip -f $(SRC_TAR)

stestrelease: emptydeps define_build
	cd $(ROOT_DIR)/shared; $(MAKE) emptydeps
	cd $(ROOT_DIR); tar -X $(BASE_DIR)/$(BASE_NAME)-all.exclude -cf \
		$(SRC_TAR) $(BASE_DIR)/* $(REL_ROOTSRC)
	cd $(ROOT_DIR); tar -T $(BASE_DIR)/$(BASE_NAME)-shared.include -rf \
		$(SRC_TAR)
	cd $(ROOT_DIR); tar -X $(BASE_DIR)/$(BASE_NAME)-test.exclude -rf \
		$(SRC_TAR) test/$(BASE_NAME)/*
	cd $(ROOT_DIR); tar -X $(BASE_DIR)/$(BASE_NAME)-vicious.exclude -rf \
		$(SRC_TAR) ViCious/$(BASE_DIR)/*
	cd $(ROOT_DIR); gzip -f $(SRC_TAR)

brelease: define_build
	cd $(ROOT_DIR); tar zcf \
		$(BIN_TAR).gz $(REL_EXTERNS)

rellibs:
	cd $(ROOT_DIR); tar zcf \
		$(LIB_TAR).gz $(REL_LIBNAMES)

drelease: define_build
	cd $(ROOT_DIR); tar -X $(BASE_DIR)/$(BASE_NAME)-doc.exclude -zcf \
		$(DOC_TAR).gz doc/examples/$(BASE_NAME)/*

release: srelease brelease drelease
	cd $(ROOT_DIR); tar -X $(BASE_DIR)/$(BASE_NAME)-all.exclude -cf \
		$(ALL_TAR) $(BASE_DIR)/* $(REL_ROOTSRC) $(REL_EXTERNS)
	cd $(ROOT_DIR); tar -T $(BASE_DIR)/$(BASE_NAME)-shared.include -rf \
		$(ALL_TAR)
	cd $(ROOT_DIR); tar -X $(BASE_DIR)/$(BASE_NAME)-doc.exclude -rf \
		$(ALL_TAR) doc/examples/$(BASE_NAME)/*
	cd $(ROOT_DIR); gzip -f $(ALL_TAR)

testrelease: stestrelease brelease
	cd $(ROOT_DIR); tar -X $(BASE_DIR)/$(BASE_NAME)-all.exclude -cf \
		$(ALL_TAR) $(BASE_DIR)/* $(REL_ROOTSRC) $(REL_EXTERNS)
	cd $(ROOT_DIR); tar -T $(BASE_DIR)/$(BASE_NAME)-shared.include -rf \
		$(ALL_TAR)
	cd $(ROOT_DIR); tar -X $(BASE_DIR)/$(BASE_NAME)-test.exclude -rf \
		$(ALL_TAR) test/$(BASE_NAME)/*
	cd $(ROOT_DIR); tar -X $(BASE_DIR)/$(BASE_NAME)-vicious.exclude -rf \
		$(ALL_TAR) ViCious/$(BASE_DIR)/*
	cd $(ROOT_DIR); gzip -f $(ALL_TAR)

snap: emptyalldeps testrelease rellibs

depend:
	$(CC) $(CFLAGS) -M $(SOURCES) > Makefile.deps
	$(SUBDIRS)

emptydeps:
	-rm -f Makefile.deps
	touch Makefile.deps
	chmod 666 Makefile.deps

emptyalldeps: emptydeps
	$(SUBDIRS)

Makefile.deps:
	touch Makefile.deps
	chmod 666 Makefile.deps

-include Makefile.deps