-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.in
209 lines (180 loc) · 5.68 KB
/
Makefile.in
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
## Project settings
LIBRARY_NAME ?= @LIBRARY_NAME@
LIBRARY_VERSION_MAJOR = @VERSION_MAJOR@
LIBRARY_VERSION_MINOR = @VERSION_MINOR@
LIBRARY_VERSION_PATCH = @VERSION_PATCH@
LIBRARY_VERSION_REVISION = @VERSION_REVISION@
LIBRARY_MAINTAINER := "joseph.werle@gmail.com"
LIBRARY_WEBSITE := "https://github.com/jwerle/lib$(LIBRARY_NAME)"
LIBRARY_DATE_COMPILED := $(shell date)
## Target static library
TARGET_STATIC := lib$(LIBRARY_NAME).a
TARGET_SOLIB = lib$(LIBRARY_NAME).so.$(LIBRARY_VERSION_MAJOR)
TARGET_DYLIB = lib$(LIBRARY_NAME).$(LIBRARY_VERSION_MAJOR).$(LIBRARY_VERSION_MINOR).dylib
TARGET_SO = lib$(LIBRARY_NAME).so
## Build configuration
BUILD_DIRECTORY ?= @BUILD@
BUILD_INCLUDE ?= $(BUILD_DIRECTORY)/include
BUILD_LIB ?= $(BUILD_DIRECTORY)/lib
## Environment properties
PREFIX ?= @PREFIX@
CWD ?= $(shell pwd)
OS ?= $(shell uname)
SHELL := /bin/bash
## Project command dependencies
_MKDIR = mkdir -p
MKDIR = mkdir -p
STRIP = strip
CC ?= gcc
RM = rm -rf
CP = cp -rf
LN = ln -sf
## Source files
SRC += @SRC@
SRC += @DEPS@
## Source headers
HEADERS += $(wildcard $(CWD)/*.h)
HEADERS += $(wildcard $(CWD)/include/*.h)
HEADERS += $(wildcard $(CWD)/include/$(LIBRARY_NAME)/*.h)
HEADERS += $(wildcard $(CWD)/include/$(LIBRARY_NAME)/*/*.h)
HEADERS += $(wildcard $(CWD)/include/$(LIBRARY_NAME)/*/*/*.h)
## Source objects
OBJS := $(SRC:.c=.o)
## Libary macro info
CFLAGS += -D SLEEPFILE_NAME='"lib$(LIBRARY_NAME)"'
CFLAGS += -D SLEEPFILE_DATE_COMPILED='"$(LIBRARY_DATE_COMPILED)"'
CFLAGS += -D SLEEPFILE_VERSION_MAJOR='$(LIBRARY_VERSION_MAJOR)'
CFLAGS += -D SLEEPFILE_VERSION_MINOR='$(LIBRARY_VERSION_MINOR)'
CFLAGS += -D SLEEPFILE_VERSION_PATCH='$(LIBRARY_VERSION_PATCH)'
CFLAGS += -D SLEEPFILE_VERSION_REVISION='$(LIBRARY_VERSION_REVISION)'
CFLAGS += -D SLEEPFILE_VERSION="( \
$(LIBRARY_VERSION_MAJOR) << 24 | \
$(LIBRARY_VERSION_MINOR) << 16 | \
$(LIBRARY_VERSION_PATCH) << 8 | \
$(LIBRARY_VERSION_REVISION) \
)"
## Compiler flags
CFLAGS += @CFLAGS@
CFLAGS += -I $(BUILD_DIRECTORY)/include
## Linker flags
LDFLAGS += @LDFLAGS@
ifeq ($(OS), Darwin)
LDFLAGS += -shared -lc -Wl,-install_name,$(TARGET_SO)
else
LDFLAGS += -shared -Wl,-soname,$(TARGET_SO).$(LIBRARY_VERSION_MAJOR)
endif
## Macro to ensure build directory structure is in place
define ENSURE_BUILD_DIRECTORY_STRUCTURE
@if ! test -d $(BUILD_DIRECTORY); then \
printf " "; \
printf $(BRIEF_FORMAT) "MKDIR" \
" $(BUILD_DIRECTORY)/{include/$(LIBRARY_NAME),lib}" \
| tr '\n' ' ' && echo; \
$(_MKDIR) $(BUILD_DIRECTORY)/{include/$(LIBRARY_NAME),lib}; \
fi
endef
## Brief commands
BRIEFC += MKDIR
BRIEFC += STRIP
BRIEFC += MAKE
BRIEFC += RM
BRIEFC += LN
BRIEFC += AR
BRIEFC += CC
ifneq (1,$(NO_BRIEF))
-include mk/brief.mk
-include brief.mk
endif
## Builds everything
build: $(OBJS)
build: $(BUILD_INCLUDE)/$(LIBRARY_NAME)
build: $(BUILD_LIB)/$(TARGET_STATIC)
build: $(BUILD_LIB)/$(TARGET_SO)
build: $(BUILD_LIB)/$(TARGET_SOLIB)
ifeq ($(OS),Darwin)
build: $(BUILD_LIB)/$(TARGET_DYLIB)
endif
## Compiles object files
.c.o:
$(CC) $(CFLAGS) -c $< -o $@
## Builds static library
.PHONY: $(TARGET_STATIC)
$(TARGET_STATIC): $(BUILD_LIB)/$(TARGET_STATIC)
$(BUILD_LIB)/$(TARGET_STATIC): $(OBJS)
$(ENSURE_BUILD_DIRECTORY_STRUCTURE)
$(AR) crs $@ $^
## Copies header files
.PHONY: $(BUILD_INCLUDE)/$(LIBRARY_NAME)
$(BUILD_INCLUDE)/$(LIBRARY_NAME): $(HEADERS)
$(ENSURE_BUILD_DIRECTORY_STRUCTURE)
@echo $(HEADERS) | xargs dirname | while read dir; do \
dest=$${dir/'$(CWD)'/'$(BUILD_DIRECTORY)'}; \
mkdir -p $$dest; \
cp -rf $$dir/* $$dest; \
done
## Builds a shared object
.PHONY: $(TARGET_SO)
$(TARGET_SO): $(BUILD_LIB)/$(TARGET_SO)
$(BUILD_LIB)/$(TARGET_SO): $(OBJS)
$(ENSURE_BUILD_DIRECTORY_STRUCTURE)
$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(OBJS)
ifneq ($(OS),Darwin)
$(STRIP) --strip-unneeded $@
endif
## Links shared object to name with major version
.PHONY: $(TARGET_SOLIB)
$(TARGET_SOLIB): $(BUILD_LIB)/$(TARGET_SOLIB)
$(BUILD_LIB)/$(TARGET_SOLIB): $(BUILD_LIB)/$(TARGET_SO)
$(ENSURE_BUILD_DIRECTORY_STRUCTURE)
$(LN) $(shell basename $<) $@
$(LN) $(shell basename $<) $@.$(LIBRARY_VERSION_MINOR)
$(LN) $(shell basename $<) $@.$(LIBRARY_VERSION_MINOR).$(LIBRARY_VERSION_PATCH)
$(LN) $(shell basename $<) $@.$(LIBRARY_VERSION_MINOR).$(LIBRARY_VERSION_PATCH).$(LIBRARY_VERSION_REVISION)
## Builds dynamic libary
ifeq ($(OS),Darwin)
.PHONY: $(TARGET_DYLIB)
$(TARGET_DYLIB): $(BUILD_LIB)/$(TARGET_DYLIB)
$(BUILD_LIB)/$(TARGET_DYLIB): $(OBJS)
$(ENSURE_BUILD_DIRECTORY_STRUCTURE)
$(CC) $(LDFLAGS) -dynamiclib -undefined suppress -flat_namespace $^ -o $@
endif
## Cleans project directory
.PHONY: clean
clean: test/clean
clean: example/clean
clean: BRIEF_ARGS = $(OBJS) $(BUILD_DIRECTORY)
clean:
$(RM) $(OBJS) $(BUILD_DIRECTORY)
.PHONY: distclean
distclean: clean
$(RM) -f Makefile
## Cleans test directory
.PHONY: test/clean
test/clean: BRIEF_ARGS = clean (test)
test/clean:
$(MAKE) clean -C test
## Compiles and runs all test
.PHONY: test
test: build
$(MAKE) -C $@
.PHONY: example/clean
example/clean: BRIEF_ARGS = clean (example)
example/clean:
$(MAKE) clean -C example
## Compiles example
.PHONY: example
example: build
$(MAKE) -C $@
## Installs library into system
.PHONY: install
install: $(BUILD_DIRECTORY)/lib/$(TARGET_STATIC)
$(CP) -fr build/include/* $(PREFIX)/include/
$(CP) -f $(BUILD_DIRECTORY)/lib/* $(PREFIX)/lib
## Uninstalls library from system
.PHONY: uninstall
uninstall:
$(RM) -r $(PREFIX)/include/$(LIBRARY_NAME)
$(RM) $(PREFIX)/lib/$(TARGET_STATIC)
$(RM) $(PREFIX)/lib/$(TARGET_DYLIB)
$(RM) $(PREFIX)/lib/$(TARGET_SOLIB)
$(RM) $(PREFIX)/lib/$(TARGET_SO)