-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathMakefile
185 lines (146 loc) · 7.09 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
SRC_DIR = ..
EXAMPLE_DIR = examples
TEST_DIR = tests
BUILD_DIR = build
GEN_DIR = $(BUILD_DIR)/gen
################################################################################
LIBNAME = luaradio
LIB_SRCS = luaradio.c
LUA_SRCS = $(shell find $(SRC_DIR)/radio -type f -name "*.lua")
EXAMPLE_SRCS = $(EXAMPLE_DIR)/fm-radio.c $(EXAMPLE_DIR)/rds-timesync.c
TEST_SRCS = $(TEST_DIR)/test_api.c
################################################################################
ifneq ($(shell uname -s),Darwin)
SHARED_LIB = $(BUILD_DIR)/lib$(LIBNAME).so
else
SHARED_LIB = $(BUILD_DIR)/lib$(LIBNAME).dylib
endif
STATIC_LIB = $(BUILD_DIR)/lib$(LIBNAME).a
LUA_C_SRCS := $(patsubst $(SRC_DIR)/%.lua, $(GEN_DIR)/%.c, $(LUA_SRCS))
LIB_OBJS := $(patsubst $(GEN_DIR)/%.c, $(BUILD_DIR)/%.o, $(LUA_C_SRCS)) $(patsubst %.c, $(BUILD_DIR)/%.o, $(LIB_SRCS))
EXAMPLE_BINS = $(patsubst $(EXAMPLE_DIR)/%.c, $(BUILD_DIR)/$(EXAMPLE_DIR)/%, $(EXAMPLE_SRCS))
TEST_BINS = $(patsubst $(TEST_DIR)/%.c, $(BUILD_DIR)/$(TEST_DIR)/%, $(TEST_SRCS))
################################################################################
ifneq ($(shell pkg-config luajit 2>/dev/null; echo $$?), 0)
$(error "LuaJIT library not found with pkg-config")
endif
ifneq ($(shell which luajit 2>/dev/null),)
LUAJIT ?= luajit
else
LUAJIT ?= luajit-$(shell pkg-config --modversion luajit)
endif
ifneq ($(shell $(LUAJIT) -v 1>/dev/null 2>/dev/null; echo $$?), 0)
$(error "LuaJIT executable not found")
endif
INSTALL_LMOD = $(shell pkg-config luajit --variable INSTALL_LMOD)
INSTALL_CMOD = $(shell pkg-config luajit --variable INSTALL_CMOD)
PREFIX ?= /usr/local
################################################################################
OPT = -O3
DEBUG =
VERSION_MAJOR := $(shell cd ../; $(LUAJIT) -e "package.path = './?/init.lua;' .. package.path; print(require('radio').version_info.major)" 2>/dev/null)
VERSION_MINOR := $(shell cd ../; $(LUAJIT) -e "package.path = './?/init.lua;' .. package.path; print(require('radio').version_info.minor)" 2>/dev/null)
VERSION_PATCH := $(shell cd ../; $(LUAJIT) -e "package.path = './?/init.lua;' .. package.path; print(require('radio').version_info.patch)" 2>/dev/null)
VERSION_COMMIT := $(shell git describe --abbrev --always --tags --dirty 2>/dev/null || echo "")
override CFLAGS += -std=c99 -D_DEFAULT_SOURCE
override CFLAGS += $(shell pkg-config --cflags luajit)
override CFLAGS += -fPIC
override CFLAGS += $(OPT) $(DEBUG) $(INCLUDES)
override CFLAGS += -DVERSION_MAJOR=$(VERSION_MAJOR) -DVERSION_MINOR=$(VERSION_MINOR) -DVERSION_PATCH=$(VERSION_PATCH)
override CFLAGS += -DVERSION_COMMIT=\"$(VERSION_COMMIT)\"
override CFLAGS += -Wall -Wextra -Wcast-align -Wcast-qual -Wimplicit
override CFLAGS += -Wpointer-arith -Wswitch -Wredundant-decls -Wreturn-type
override CFLAGS += -Wshadow -Wunused -Wno-unused-parameter
LIBFLAGS = $(shell pkg-config --libs luajit)
ifneq ($(shell uname -s),Darwin)
LDFLAGS = -Wl,-E
EXTRA_LDFLAGS =
else
LDFLAGS =
EXTRA_LDFLAGS = -pagezero_size 10000 -image_base 100000000
endif
################################################################################
.PHONY: all
all: lib tests examples
.PHONY: lib
lib: $(SHARED_LIB) $(STATIC_LIB)
.PHONY: examples
examples: $(SHARED_LIB) $(EXAMPLE_BINS)
.PHONY: tests
tests: $(SHARED_LIB) $(TEST_BINS)
.PHONY: runtests
runtests: $(TEST_BINS)
for f in $(TEST_BINS); do \
LD_LIBRARY_PATH=$(BUILD_DIR) DYLD_LIBRARY_PATH=$(BUILD_DIR) ./$$f; \
done
install-lmod:
install -d $(DESTDIR)$(INSTALL_LMOD)
@for d in $(sort $(dir $(patsubst $(SRC_DIR)/%.lua, %.lua, $(LUA_SRCS)))); do \
echo install -d $(DESTDIR)$(INSTALL_LMOD)/$$d; \
install -d $(DESTDIR)$(INSTALL_LMOD)/$$d; \
done
@for f in $(patsubst $(SRC_DIR)/%.lua, %.lua, $(LUA_SRCS)); do \
echo install -m644 $(SRC_DIR)/$$f $(DESTDIR)$(INSTALL_LMOD)/$$f; \
install -m644 $(SRC_DIR)/$$f $(DESTDIR)$(INSTALL_LMOD)/$$f; \
done
install -d $(DESTDIR)$(PREFIX)/bin
install -m755 ../luaradio $(DESTDIR)$(PREFIX)/bin/luaradio
install -d $(DESTDIR)$(PREFIX)/share/licenses/luaradio
install -m644 ../LICENSE $(DESTDIR)$(PREFIX)/share/licenses/luaradio/LICENSE
.PHONY: install install-data install-exec
install: install-exec install-data
install-data:
install -d $(DESTDIR)$(PREFIX)/include
install -m644 luaradio.h $(DESTDIR)$(PREFIX)/include/luaradio.h
install -d $(DESTDIR)$(PREFIX)/share/licenses/luaradio
install -m644 ../LICENSE $(DESTDIR)$(PREFIX)/share/licenses/luaradio/LICENSE
install-exec: $(STATIC_LIB) $(SHARED_LIB)
install -d $(DESTDIR)$(PREFIX)/lib
install -d $(DESTDIR)$(INSTALL_CMOD)
ifneq ($(shell uname -s),Darwin)
install -m755 $(STATIC_LIB) $(DESTDIR)$(PREFIX)/lib/lib$(LIBNAME).a
install -m755 $(SHARED_LIB) $(DESTDIR)$(PREFIX)/lib/lib$(LIBNAME).so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)
ln -sf lib$(LIBNAME).so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH) $(DESTDIR)$(PREFIX)/lib/lib$(LIBNAME).so.$(VERSION_MAJOR)
ln -sf lib$(LIBNAME).so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH) $(DESTDIR)$(PREFIX)/lib/lib$(LIBNAME).so
ln -sf $(PREFIX)/lib/lib$(LIBNAME).so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH) $(DESTDIR)$(INSTALL_CMOD)/radio.so
else
# Mac OS X install
install -m755 $(STATIC_LIB) $(DESTDIR)$(PREFIX)/lib/lib$(LIBNAME).a
install -m755 $(SHARED_LIB) $(DESTDIR)$(PREFIX)/lib/lib$(LIBNAME).$(VERSION_MAJOR).dylib
ln -sf lib$(LIBNAME).$(VERSION_MAJOR).dylib $(DESTDIR)$(PREFIX)/lib/lib$(LIBNAME).dylib
ln -sf $(PREFIX)/lib/lib$(LIBNAME).$(VERSION_MAJOR).dylib $(DESTDIR)$(INSTALL_CMOD)/radio.so
endif
install -d $(DESTDIR)$(PREFIX)/bin
install -m755 ../luaradio $(DESTDIR)$(PREFIX)/bin/luaradio
.PHONY: clean
clean:
rm -rf $(BUILD_DIR)
################################################################################
$(BUILD_DIR)/$(TEST_DIR)/%: $(TEST_DIR)/%.c $(SHARED_LIB)
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -I./ $< -o $@ -L$(BUILD_DIR) -l$(LIBNAME) $(LIBFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS)
$(BUILD_DIR)/$(EXAMPLE_DIR)/%: $(EXAMPLE_DIR)/%.c $(SHARED_LIB)
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -I./ $< -o $@ -L$(BUILD_DIR) -l$(LIBNAME) $(LIBFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS)
$(STATIC_LIB): $(LIB_OBJS)
ar rcs $@ $^
ifneq ($(shell uname -s),Darwin)
$(SHARED_LIB): $(LIB_OBJS)
$(CC) $(CFLAGS) -shared -Wl,-soname,lib$(LIBNAME).so.$(VERSION_MAJOR) -o $@.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH) $^ $(LDFLAGS)
ln -sf lib$(LIBNAME).so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH) $@.$(VERSION_MAJOR)
ln -sf lib$(LIBNAME).so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH) $@
else
# Mac OS X shared library build
$(SHARED_LIB): $(LIB_OBJS)
$(CC) $(CFLAGS) -dynamiclib -Wl,-undefined -Wl,dynamic_lookup -install_name "lib$(LIBNAME).$(VERSION_MAJOR).dylib" -current_version $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH) -compatibility_version $(VERSION_MAJOR).$(VERSION_MINOR) -o $(subst .dylib,.$(VERSION_MAJOR).dylib,$@) $^ $(LDFLAGS)
ln -sf lib$(LIBNAME).$(VERSION_MAJOR).dylib $@
endif
$(BUILD_DIR)/%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/%.o: $(GEN_DIR)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@
.SECONDARY: $(LUA_C_SRCS)
$(GEN_DIR)/%.c: $(SRC_DIR)/%.lua
@mkdir -p $(dir $@)
$(LUAJIT) -bn $(subst .init, , $(subst /,., $(patsubst $(SRC_DIR)/%.lua, %, $<))) $< $@