-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
475 lines (427 loc) · 18.5 KB
/
Makefile
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#===============================================================================
# Override-able variables:
#
# CC -- Compiler
# AR -- Archiver (generate the static lib)
# STRIP -- Strip (discard symbols)
# OS -- Target operating system (in {Linux, Win, emscript})
# ARCH -- Target architecture (in {i686, x86_64})
# CFLAGS -- Base compiler flags (to which more flags are appended)
# PREFIX -- Base directory where the lib will be installed
# LIBPATH -- Directory where the shared library will be installed
# HEADERPATH -- Directory where the headers will be installed
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Cross-compiling instructions:
#
# Cross-compiling from Linux to Windows may be done by setting the following
# variables: 'CC', 'AR', 'STRIP', 'OS', 'ARCH'. Setting 'OS' to 'Win' avoids
# some overriding done when compiling natively on Windows through MinGW.
#
# The destination for the 'install' command may be chosen either by setting the
# 'PREFIX' var or by manually setting 'LIBPATH' and 'HEADERPATH'. The later may
# be interesting if you want to install both i686 and x86_64 libraries (since
# this Makefile don't follow any default recomendation).
#===============================================================================
#===============================================================================
# Define every object required by compilation
#===============================================================================
OBJS = $(OBJDIR)/synth.o \
$(OBJDIR)/synth_audio.o \
$(OBJDIR)/synth_lexer.o \
$(OBJDIR)/synth_note.o \
$(OBJDIR)/synth_parser.o \
$(OBJDIR)/synth_prng.o \
$(OBJDIR)/synth_renderer.o \
$(OBJDIR)/synth_track.o \
$(OBJDIR)/synth_volume.o
#===============================================================================
#==============================================================================
# Select which compiler to use (either gcc or emcc)
#==============================================================================
ifneq (, $(findstring emscript, $(MAKECMDGOALS)))
CC := emcc
endif
CC ?= gcc
AR ?= ar
STRIP ?= strip
# Set DEBUG as the default mode
ifneq ($(RELEASE), yes)
RELEASE := no
DEBUG := yes
endif
#==============================================================================
#==============================================================================
# Clear the suffixes' default rule, since there's an explicit one
#==============================================================================
.SUFFIXES:
#==============================================================================
#==============================================================================
# Define all targets that doesn't match its generated file
#==============================================================================
.PHONY: emscript fast fast_all install install_shared install_shared_win \
install_shared_x install_static install_static_win install_static_x \
uninstall uninstall_win uninstall_x clean emscript_clean distclean
#==============================================================================
#==============================================================================
# Define compilation target
#==============================================================================
TARGET := libCSynth
LIBNAME := lCSynth
MAJOR_VERSION := 1
MINOR_VERSION := 0
REV_VERSION := 2
# If the DEBUG flag was set, generate another binary (so it doesn't collide
# with the release one)
ifeq ($(DEBUG), yes)
TARGET := $(TARGET)_dbg
endif
#==============================================================================
#===============================================================================
# Set OS flag
#===============================================================================
UNAME := $(shell uname)
OS ?= $(UNAME)
ifneq (, $(findstring Windows_NT, $(UNAME)))
OS := Win
UNAME := Win
endif
ifneq (, $(findstring MINGW, $(UNAME)))
OS := Win
UNAME := Win
endif
ifneq (, $(findstring MSYS, $(UNAME)))
OS := Win
UNAME := Win
endif
ifeq ($(CC), emcc)
OS := emscript
endif
#===============================================================================
#===============================================================================
# Define CFLAGS (compiler flags)
#===============================================================================
# Add all warnings and default include path
CFLAGS := $(CFLAGS) -Wall -I"./include" -I"./src/include"
# Add architecture flag
ARCH ?= $(shell uname -m)
ifeq ($(OS), emscript)
CFLAGS := $(CFLAGS) -I"$(EMSCRIPTEN)/system/include/" -m32
else
ifeq ($(ARCH), x86_64)
CFLAGS := $(CFLAGS) -m64
else
CFLAGS := $(CFLAGS) -m32
endif
endif
# Add debug flags
ifeq ($(OS), emscript)
CFLAGS := $(CFLAGS) -O2
else
ifneq ($(RELEASE), yes)
CFLAGS := $(CFLAGS) -g -O0
else
CFLAGS := $(CFLAGS) -O3
endif
endif
# Set flags required by OS
ifeq ($(UNAME), Win)
CFLAGS := $(CFLAGS) -I"/d/windows/mingw/include"
endif
ifneq ($(OS), Win)
CFLAGS := $(CFLAGS) -fPIC
endif
# Set the current compiler
ifeq ($(OS), emscript)
CFLAGS := $(CFLAGS) -DEMCC
endif
#===============================================================================
#===============================================================================
# Define LDFLAGS (linker flags)
#===============================================================================
SDL_LDFLAGS := -lSDL2
ifeq ($(OS), Win)
LDFLAGS := $(LDFLAGS) -lmingw32
SDL_LDFLAGS := -lSDL2main -lSDL2
else
LDFLAGS := $(LDFLAGS) -lm
endif
#===============================================================================
#===============================================================================
# Define where source files can be found and where objects and binary are output
#===============================================================================
VPATH := src:tst
ifeq ($(RELEASE), yes)
OBJDIR := obj/release/$(OS)
BINDIR := bin/release/$(OS)
else
OBJDIR := obj/debug/$(OS)
BINDIR := bin/debug/$(OS)
endif
TESTDIR := tst
PREFIX ?= /usr
LIBPATH ?= $(PREFIX)/lib/c_synth
HEADERPATH ?= $(PREFIX)/include/c_synth
#===============================================================================
#==============================================================================
# Automatically look up for tests and compile them
#==============================================================================
TEST_SRC := $(wildcard $(TESTDIR)/tst_*.c)
TEST_OBJS := $(TEST_SRC:$(TESTDIR)/%.c=$(OBJDIR)/%.o)
TEST_BIN := $(addprefix $(BINDIR)/, $(TEST_SRC:$(TESTDIR)/%.c=%$(BIN_EXT)))
#==============================================================================
#==============================================================================
# Make sure the test's object files aren't automatically deleted
#==============================================================================
.SECONDARY: $(TEST_OBJS)
#==============================================================================
#==============================================================================
# Make the objects list constant (and the icon, if any)
#==============================================================================
OBJS := $(OBJS)
#==============================================================================
#==============================================================================
# Set shared library's extension
#==============================================================================
ifeq ($(OS), Win)
SO ?= dll
MJV ?= $(SO)
MNV ?= $(SO)
else
SO ?= so
MJV ?= $(SO).$(MAJOR_VERSION)
MNV ?= $(SO).$(MAJOR_VERSION).$(MINOR_VERSION).$(REV_VERSION)
endif
#==============================================================================
#==============================================================================
# Ensure debug build isn't stripped
#==============================================================================
ifneq ($(RELEASE), yes)
STRIP := touch
endif
#==============================================================================
#==============================================================================
# Get the number of cores for fun stuff
#==============================================================================
ifeq ($(UNAME), Win)
CORES := 1
else
CORES := $$(($(shell nproc) * 2))
endif
#==============================================================================
#===============================================================================
# Define default compilation rule
#===============================================================================
all: static shared tests
#===============================================================================
#==============================================================================
# Rule for building a object file for emscript
#==============================================================================
emscript: $(BINDIR)/$(TARGET).bc
#===============================================================================
#==============================================================================
# Rule for cleaning emscript build... It's required to modify the CC
#==============================================================================
emscript_clean: clean
#===============================================================================
#==============================================================================
# Build a emscript (LLVM) binary, to be used when compiling for HTML5
#==============================================================================
$(BINDIR)/$(TARGET).bc: MKDIRS $(OBJS)
$(CC) -o $@ $(CFLAGS) $(OBJS)
#==============================================================================
#==============================================================================
# Rule for building the static lib
#==============================================================================
static: MKDIRS $(BINDIR)/$(TARGET).a
#==============================================================================
#==============================================================================
# Rule for building the shared libs
#==============================================================================
shared: MKDIRS $(BINDIR)/$(TARGET).$(SO)
#==============================================================================
#==============================================================================
# Rule for building tests
#==============================================================================
tests: MKDIRS shared $(TEST_BIN)
#==============================================================================
#==============================================================================
# Rule for installing the library
#==============================================================================
ifeq ($(UNAME), Win)
install: install_shared_win install_static_win
install_shared: install_shared_win
install_static: install_static_win
else
install: install_shared_x install_static_x
install_shared: install_shared_x
install_static: install_static_x
endif
install_shared_win: shared
# Create destiny directories
mkdir -p /c/c_synth/lib/
mkdir -p /c/c_synth/include/c_synth
# Copy the headers
cp -rf ./include/c_synth/* /c/c_synth/include/c_synth
# Copy the lib
cp -rf $(BINDIR)/$(TARGET).dll /c/c_synth/lib/
install_static_win: static
# Create destiny directories
mkdir -p /c/c_synth/lib/
mkdir -p /c/c_synth/include/c_synth
# Copy the headers
cp -rf ./include/c_synth/* /c/c_synth/include/c_synth
# Copy the lib
cp -rf $(BINDIR)/$(TARGET).a /c/c_synth/lib/
install_shared_x: shared
# Create destiny directories
mkdir -p $(LIBPATH)
mkdir -p $(HEADERPATH)
# Copy the headers
cp -rf ./include/c_synth/* $(HEADERPATH)
# Copy every shared lib (normal, optmized and debug)
cp -f $(BINDIR)/$(TARGET)*.$(MNV) $(LIBPATH)
# -P = don't follow sym-link
cp -fP $(BINDIR)/$(TARGET)*.$(MJV) $(LIBPATH)
cp -fP $(BINDIR)/$(TARGET)*.$(SO) $(LIBPATH)
# Make the lib be automatically found
echo "$(LIBPATH)" > /etc/ld.so.conf.d/c_synth.conf
ldconfig
install_static_x: static
# Create destiny directories
mkdir -p $(LIBPATH)
mkdir -p $(HEADERPATH)
# Copy the headers
cp -rf ./include/c_synth/* $(HEADERPATH)
# Copy the static lib
cp -f $(BINDIR)/$(TARGET)*.a $(LIBPATH)
#==============================================================================
#==============================================================================
# Rule for uninstalling the library
#==============================================================================
ifeq ($(UNAME), Win)
uninstall: uninstall_win
else
uninstall: uninstall_x
endif
uninstall_win:
# Remove the libraries (account for different versions)
rm -f /c/c_synth/lib/$(TARGET)_dbg.*
rm -f /c/c_synth/lib/$(TARGET).*
# Remove the headers
rm -rf /c/c_synth/include/*
# Remove its directories
rmdir /c/c_synth/lib/
rmdir /c/c_synth/include/
rmdir /c/c_synth/
uninstall_x:
# Remove the libraries (account for different versions)
rm -f $(LIBPATH)/$(TARGET)_dbg.*
rm -f $(LIBPATH)/$(TARGET).*
# Remove the headers
rm -rf $(HEADERPATH)/*
# Remove its directories
rmdir $(LIBPATH)
rmdir $(HEADERPATH)
# Remove the lib from the default path
rm /etc/ld.so.conf.d/c_synth.conf
# Update the paths
ldconfig
#==============================================================================
#==============================================================================
# Rule for actually building the static library
#==============================================================================
$(BINDIR)/$(TARGET).a: $(OBJS)
rm -f $(BINDIR)/$(TARGET).a
$(AR) -cvq $(BINDIR)/$(TARGET).a $(OBJS)
#==============================================================================
#==============================================================================
# Rule for actually building the shared library
#==============================================================================
# Windows DLL
$(BINDIR)/$(TARGET).dll: $(OBJS)
rm -f $@
$(CC) -shared -Wl,-soname,$(TARGET).dll -Wl,-export-all-symbols $(CFLAGS) \
-o $@ $(OBJS) $(LDFLAGS)
$(STRIP) $@
# Linux
$(BINDIR)/$(TARGET).so: $(BINDIR)/$(TARGET).$(MJV)
rm -f $(BINDIR)/$(TARGET).$(SO)
cd $(BINDIR); ln -f -s $(TARGET).$(MJV) $(TARGET).$(SO)
ifneq ($(SO), $(MJV))
$(BINDIR)/$(TARGET).$(MJV): $(BINDIR)/$(TARGET).$(MNV)
rm -f $(BINDIR)/$(TARGET).$(MJV)
cd $(BINDIR); ln -f -s $(TARGET).$(MNV) $(TARGET).$(MJV)
endif
ifneq ($(SO), $(MNV))
$(BINDIR)/$(TARGET).$(MNV): $(OBJS)
$(CC) -shared -Wl,-soname,$(TARGET).$(MJV) -Wl,-export-dynamic \
$(CFLAGS) -o $(BINDIR)/$(TARGET).$(MNV) $(OBJS) $(LDFLAGS)
$(STRIP) $@
endif
# Mac OS X
$(BINDIR)/$(TARGET).dylib: $(OBJS)
$(CC) -dynamiclib $(CFLAGS) -o $(BINDIR)/$(TARGET).dylib $(OBJS)
$(STRIP) $@
#==============================================================================
#==============================================================================
# Rule for compiling test binaries that uses SDL2 as its backend (those are
# prefixed by 'tst_' and suffixed by 'SDL2')
#==============================================================================
$(BINDIR)/tst_%SDL2$(BIN_EXT): $(OBJDIR)/tst_%SDL2.o
$(CC) $(CFLAGS) -o $@ $< -L$(BINDIR) $(LDFLAGS) -$(LIBNAME)_dbg $(SDL_LDFLAGS)
#==============================================================================
#==============================================================================
# Rule for compiling a test binary (it's prefixed by 'tst_')
#==============================================================================
$(BINDIR)/tst_%$(BIN_EXT): $(OBJDIR)/tst_%.o
$(CC) $(CFLAGS) -o $@ $< -L$(BINDIR) $(LDFLAGS) -$(LIBNAME)_dbg
#==============================================================================
#==============================================================================
# Rule for compiling any .c in its object
#==============================================================================
$(OBJDIR)/%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $<
#==============================================================================
#==============================================================================
# Rule for creating every directory
#==============================================================================
MKDIRS: | $(OBJDIR)
#==============================================================================
#==============================================================================
# Build everything as fast as possible (and using as many cores/threads as
# possible)
#==============================================================================
fast:
make -j $(CORES) static shared
#==============================================================================
#==============================================================================
# Build everything as fast as possible (and using as many cores/threads as
# possible)
#==============================================================================
fast_all:
make -j $(CORES) static shared && make -j $(CORES)
#==============================================================================
#==============================================================================
# Rule for actually creating every directory
#==============================================================================
$(OBJDIR):
mkdir -p $(OBJDIR)
mkdir -p $(BINDIR)
#==============================================================================
#==============================================================================
# Removes all built objects (use emscript_clean to clear the emscript stuff)
#==============================================================================
clean:
rm -f $(OBJS) $(BINDIR)/$(TARGET).a $(BINDIR)/*
rm -rf $(OBJDIR) $(BINDIR)
#==============================================================================
#==============================================================================
# Remove all built objects and target directories
#==============================================================================
distclean:
make emscript_clean DEBUG=yes
make emscript_clean RELEASE=yes
make clean DEBUG=yes
make clean RELEASE=yes
#==============================================================================