-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMakefile.lib
89 lines (83 loc) · 2.62 KB
/
Makefile.lib
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
## library name, soname and version
TSTNAME := ternary_st
LIBNAME := libternary_st
SONMVER := 1
VER := $(SONMVER).0
## compiler
CC := gcc
CCLD := $(CC)
## output/object/include/source directories
BINDIR := bin
OBJDIR := obj
INCLUDE := include
SRCDIR := src
LIBDIR := lib
CNFDIR := conf
CNFILE := ternary_st.conf
ETCDIR := /etc/ld.so.conf.d
## compiler and linker flags
CFLAGS := -Wall -Wextra -pedantic -Werror -finline-functions -std=c11 -Wshadow
CFLAGS += -I$(INCLUDE)
CFLAGS += -fPIC
ifeq ($(debug),-DDEBUG)
CFLAGS += -g
else
CFLAGS += -Ofast
endif
LDFLAGS := -shared -Wl,-soname,$(LIBNAME).so.$(SONMVER)
## libraries
LIBS :=
## source/include/object variables
# SOURCES := $(wildcard $(SRCDIR)/tst*.c)
SOURCES := $(SRCDIR)/$(TSTNAME).c
INCLUDES := $(wildcard $(INCLUDE)/*.h)
OBJECTS := $(OBJDIR)/$(LIBNAME).o
## install
INSTDIR := lib64
UNAME_M := $(shell uname -m)
# if machine type is i386, i486, etc.. use lib for install
ifeq (i,$(findstring i,$(UNAME_M)))
INSTDIR := lib
endif
## PREFIX is environment variable, but if it is not set, then set default value
ifeq ($(PREFIX),)
PREFIX := /usr/local
endif
.PHONY: clean
$(LIBNAME): $(OBJECTS)
@mkdir -p $(@D)/$(LIBDIR)
$(CCLD) -o $(LIBDIR)/$(LIBNAME).so.$(VER) $(OBJECTS) $(LDFLAGS) $(LIBS)
# create symlinks in lib directory
ln -sf $(LIBNAME).so.$(VER) $(LIBDIR)/$(LIBNAME).so
ln -sf $(LIBNAME).so.$(VER) $(LIBDIR)/$(LIBNAME).so.$(SONMVER)
# create config directory and ld.so.conf.d config
mkdir -p $(@D)/$(CNFDIR) && echo $(PREFIX)/$(INSTDIR) > $(CNFDIR)/$(CNFILE)
## strip only if -DDEBUG not set
ifneq ($(debug),-DDEBUG)
strip -s $(LIBDIR)/*
endif
## create object dir/compile objects
$(OBJECTS): $(INCLUDES)
@mkdir -p $(OBJDIR)
$(CC) $(CFLAGS) $(DEFS) -c -o $(OBJECTS) $(SOURCES)
## install library
install:
install -d $(DESTDIR)$(PREFIX)/$(INSTDIR)/
install -p $(LIBDIR)/$(LIBNAME).so.$(VER) $(DESTDIR)$(PREFIX)/$(INSTDIR)/
# post_install - create install directory and symlinks
$(POST_INSTALL)
# create INSTDIR if it doesn't exits, if lib exists, soflink lib64
if [ ! -d $(PREFIX)/$(INSTDIR) -a ! -h $(PREFIX)/$(INSTDIR) ]; then \
if [ ! -d $(PREFIX)/lib ]; then mkdir -p $(PREFIX)/lib; fi; \
ln -sf $(PREFIX)/lib $(PREFIX)/$(INSTDIR); \
fi
# creae the symlinks
ln -sf $(LIBNAME).so.$(VER) $(DESTDIR)$(PREFIX)/$(INSTDIR)/$(LIBNAME).so
ln -sf $(LIBNAME).so.$(VER) $(DESTDIR)$(PREFIX)/$(INSTDIR)/$(LIBNAME).so.$(SONMVER)
# install conf file, $(CNFILE), in /etc/ld.so.conf.d/
install -p -m 0644 $(CNFDIR)/$(CNFILE) $(DESTDIR)$(ETCDIR)/
# update linker shared-object lib cache
ldconfig
## clean source directory
clean:
rm -rf $(LIBDIR) $(OBJDIR) $(CNFDIR) 2>/dev/null