-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
93 lines (78 loc) · 2.75 KB
/
Makefile
File metadata and controls
93 lines (78 loc) · 2.75 KB
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
# Lesson 24: install Target
#
# The `install` target copies built artifacts to system directories.
# Conventions:
# PREFIX -- base directory (default: /usr/local)
# DESTDIR -- staging root for package builders (prepended to all paths)
# BINDIR -- where executables go ($(PREFIX)/bin)
# LIBDIR -- where libraries go ($(PREFIX)/lib)
# INCDIR -- where headers go ($(PREFIX)/include)
#
# The `install` command (not `cp`) is used because it:
# - Sets file permissions in one step
# - Creates directories with -d
# - Is the standard tool for this job on Unix
CXX = g++
CXXFLAGS = -std=c++20 -Wall -Wextra -Iinclude -MMD -MP
AR = ar
ARFLAGS = rcs
BUILDDIR = build
# -- Install directories -----------------------------------------
PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
LIBDIR = $(PREFIX)/lib
INCDIR = $(PREFIX)/include/greetlib
# -- Library -----------------------------------------------------
LIB_SRCS = src/greet.cpp
LIB_OBJS = $(patsubst %.cpp,$(BUILDDIR)/%.o,$(LIB_SRCS))
LIB = $(BUILDDIR)/libgreet.a
# -- Executable --------------------------------------------------
APP_SRCS = src/main.cpp
APP_OBJS = $(patsubst %.cpp,$(BUILDDIR)/%.o,$(APP_SRCS))
TARGET = $(BUILDDIR)/greeter
ALL_OBJS = $(LIB_OBJS) $(APP_OBJS)
DEPS = $(ALL_OBJS:.o=.d)
.PHONY: all clean install uninstall debug
all: $(TARGET)
$(TARGET): $(APP_OBJS) $(LIB)
@mkdir -p $(@D)
$(CXX) -o $@ $(APP_OBJS) $(LIB)
$(LIB): $(LIB_OBJS)
@mkdir -p $(@D)
$(AR) $(ARFLAGS) $@ $^
$(BUILDDIR)/%.o: %.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -c -o $@ $<
clean:
rm -rf $(BUILDDIR)
# -- Install target ----------------------------------------------
# DESTDIR is prepended for staged installs (e.g., packaging)
install: $(TARGET) $(LIB)
install -d $(DESTDIR)$(BINDIR)
install -m 755 $(TARGET) $(DESTDIR)$(BINDIR)
install -d $(DESTDIR)$(LIBDIR)
install -m 644 $(LIB) $(DESTDIR)$(LIBDIR)
install -d $(DESTDIR)$(INCDIR)
install -m 644 include/greet.hpp $(DESTDIR)$(INCDIR)
uninstall:
rm -f $(DESTDIR)$(BINDIR)/$(notdir $(TARGET))
rm -f $(DESTDIR)$(LIBDIR)/$(notdir $(LIB))
rm -f $(DESTDIR)$(INCDIR)/greet.hpp
-rmdir $(DESTDIR)$(INCDIR) 2>/dev/null || true
# -- File manipulation functions ---------------------------------
# Make provides functions to dissect paths -- useful in install rules
debug:
@echo "=== Path Functions ==="
@echo "TARGET = $(TARGET)"
@echo "\$$(notdir ...) = $(notdir $(TARGET))"
@echo "\$$(dir ...) = $(dir $(TARGET))"
@echo "\$$(basename ...) = $(basename $(TARGET))"
@echo "\$$(suffix ...) = $(suffix $(LIB))"
@echo ""
@echo "=== Install Paths ==="
@echo "PREFIX = $(PREFIX)"
@echo "DESTDIR = $(DESTDIR)"
@echo "BINDIR = $(DESTDIR)$(BINDIR)"
@echo "LIBDIR = $(DESTDIR)$(LIBDIR)"
@echo "INCDIR = $(DESTDIR)$(INCDIR)"
-include $(DEPS)