Start of man-pages-NEXT: Move Changes to Changes.old
[man-pages.git] / lib / build-src.mk
blobfbb71503a55dcfdd684c71b1c988cb90f330ac40
1 ########################################################################
2 # Copyright (C) 2021, 2022 Alejandro Colomar <alx.manpages@gmail.com>
3 # SPDX-License-Identifier: GPL-2.0 OR LGPL-2.0
4 ########################################################################
7 ifndef MAKEFILE_BUILD_SRC_INCLUDED
8 MAKEFILE_BUILD_SRC_INCLUDED := 1
11 include $(srcdir)/lib/build.mk
12 include $(srcdir)/lib/cmd.mk
13 include $(srcdir)/lib/lint.mk
14 include $(srcdir)/lib/src.mk
17 PKG-CONFIG_LIBS := libbsd-overlay
20 DEFAULT_CPPFLAGS := $(shell $(PKG-CONFIG) --cflags $(PKG-CONFIG_LIBS))
21 EXTRA_CPPFLAGS :=
22 CPPFLAGS := $(DEFAULT_CPPFLAGS) $(EXTRA_CPPFLAGS)
24 DEFAULT_CFLAGS := -std=gnu17
25 DEFAULT_CFLAGS += -Wall
26 DEFAULT_CFLAGS += -Wextra
27 DEFAULT_CFLAGS += -Wstrict-prototypes
28 DEFAULT_CFLAGS += -Werror
29 DEFAULT_CFLAGS += -Wno-error=unused-parameter
30 DEFAULT_CFLAGS += -Wno-error=sign-compare
31 DEFAULT_CFLAGS += -Wno-error=format
32 DEFAULT_CFLAGS += -Wno-error=uninitialized
33 EXTRA_CFLAGS :=
34 CFLAGS := $(DEFAULT_CFLAGS) $(EXTRA_CFLAGS)
36 DEFAULT_LDFLAGS := -Wl,--as-needed
37 DEFAULT_LDFLAGS += -Wl,--no-allow-shlib-undefined
38 DEFAULT_LDFLAGS += -Wl,--no-copy-dt-needed-entries
39 DEFAULT_LDFLAGS += -Wl,--no-undefined
40 DEFAULT_LDFLAGS += $(shell $(PKG-CONFIG) --libs-only-L $(PKG-CONFIG_LIBS))
41 DEFAULT_LDFLAGS += $(shell $(PKG-CONFIG) --libs-only-other $(PKG-CONFIG_LIBS))
42 EXTRA_LDFLAGS :=
43 LDFLAGS := $(DEFAULT_LDFLAGS) $(EXTRA_LDFLAGS)
45 DEFAULT_LDLIBS := -lc
46 DEFAULT_LDLIBS += $(shell $(PKG-CONFIG) --libs-only-l $(PKG-CONFIG_LIBS))
47 EXTRA_LDLIBS :=
48 LDLIBS := $(DEFAULT_LDLIBS) $(EXTRA_LDLIBS)
51 CC := cc
52 LD := $(CC) $(CFLAGS)
53 MAN := man
56 _SRCPAGEDIRS := $(patsubst $(MANDIR)/%,$(_SRCDIR)/%.d,$(LINTMAN))
58 _UNITS_src_src := $(patsubst $(MANDIR)/%,$(_SRCDIR)/%,$(shell \
59 $(FIND) $(MANDIR)/man*/ -type f \
60 | $(GREP) '$(MANEXT)$$' \
61 | $(XARGS) $(GREP) -l '^\.TH ' \
62 | while read m; do \
63 <$$m \
64 $(SED) -n "s,^\... SRC BEGIN (\(.*.[ch]\))$$,$$m.d/\1,p"; \
65 done \
66 | $(SORT)))
67 _UNITS_src_h := $(filter %.h,$(_UNITS_src_src))
68 _UNITS_src_c := $(filter %.c,$(_UNITS_src_src))
69 _UNITS_src_o := $(patsubst %.c,%.o,$(_UNITS_src_c))
70 _UNITS_src_bin := $(patsubst %.c,%,$(_UNITS_src_c))
73 $(_SRCPAGEDIRS): $(_SRCDIR)/%.d: $(MANDIR)/% | $$(@D)/.
74 $(info MKDIR $@)
75 $(MKDIR) $@
76 touch $@
78 $(_UNITS_src_src): $$(patsubst $(_SRCDIR)/%.d,$(MANDIR)/%,$$(@D)) | $$(@D)
79 $(_UNITS_src_c): $$(filter $$(@D)/%.h,$(_UNITS_src_h))
80 $(_UNITS_src_src):
81 $(info SED $@)
82 <$< \
83 $(SED) -n \
84 -e '/^\.TH/,/^\.SH/{/^\.SH/!p}' \
85 -e '/^\.SH EXAMPLES/p' \
86 -e "/^\... SRC BEGIN ($(@F))$$/,/^\... SRC END$$/p" \
87 | $(MAN) -P cat -l - \
88 | $(SED) '/^[^ ]/d' \
89 | $(SED) 's/^ //' \
90 >$@
92 $(_UNITS_src_o): $(_SRCDIR)/%.o: $(_SRCDIR)/%.c
93 $(info CC $@)
94 $(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<
96 $(_UNITS_src_bin): $(_SRCDIR)/%: $(_SRCDIR)/%.o
97 $(info LD $@)
98 $(LD) $(LDFLAGS) -o $@ $< $(LDLIBS)
101 .PHONY: build-src-c
102 build-src-c: $(_UNITS_src_c) | builddirs-src
105 .PHONY: build-src-cc
106 build-src-cc: $(_UNITS_src_o)
109 .PHONY: build-src-ld
110 build-src-ld: $(_UNITS_src_bin)
113 .PHONY: builddirs-src
114 builddirs-src: $(_SRCDIRS)
117 .PHONY: build-src
118 build-src: build-src-ld
121 endif # MAKEFILE_BUILD_SRC_INCLUDED