1# SPDX-License-Identifier: GPL-2.0
2include ../../scripts/Makefile.include
3include ../../scripts/utilities.mak		# QUIET_CLEAN
4
5ifeq ($(srctree),)
6srctree := $(patsubst %/,%,$(dir $(CURDIR)))
7srctree := $(patsubst %/,%,$(dir $(srctree)))
8srctree := $(patsubst %/,%,$(dir $(srctree)))
9#$(info Determined 'srctree' to be $(srctree))
10endif
11
12CC ?= $(CROSS_COMPILE)gcc
13LD ?= $(CROSS_COMPILE)ld
14AR ?= $(CROSS_COMPILE)ar
15
16RM = rm -f
17
18MAKEFLAGS += --no-print-directory
19
20INSTALL = install
21
22# Use DESTDIR for installing into a different root directory.
23# This is useful for building a package. The program will be
24# installed in this directory as if it was the root directory.
25# Then the build tool can move it later.
26DESTDIR ?=
27DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
28
29LIBFILE = $(OUTPUT)libsubcmd.a
30
31CFLAGS := -ggdb3 -Wall -Wextra -std=gnu99 -fPIC
32
33ifeq ($(DEBUG),0)
34  ifeq ($(feature-fortify-source), 1)
35    CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2
36  endif
37endif
38
39ifeq ($(DEBUG),1)
40  CFLAGS += -O0
41else ifeq ($(CC_NO_CLANG), 0)
42  CFLAGS += -O3
43else
44  CFLAGS += -O6
45endif
46
47# Treat warnings as errors unless directed not to
48ifneq ($(WERROR),0)
49  CFLAGS += -Werror
50endif
51
52CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
53
54CFLAGS += -I$(srctree)/tools/include/
55
56CFLAGS += $(EXTRA_WARNINGS) $(EXTRA_CFLAGS)
57
58SUBCMD_IN := $(OUTPUT)libsubcmd-in.o
59
60ifeq ($(LP64), 1)
61  libdir_relative = lib64
62else
63  libdir_relative = lib
64endif
65
66prefix ?=
67libdir = $(prefix)/$(libdir_relative)
68
69# Shell quotes
70libdir_SQ = $(subst ','\'',$(libdir))
71
72all:
73
74export srctree OUTPUT CC LD CFLAGS V
75include $(srctree)/tools/build/Makefile.include
76
77all: fixdep $(LIBFILE)
78
79$(SUBCMD_IN): FORCE
80	@$(MAKE) $(build)=libsubcmd
81
82$(LIBFILE): $(SUBCMD_IN)
83	$(QUIET_AR)$(RM) $@ && $(AR) rcs $@ $(SUBCMD_IN)
84
85define do_install_mkdir
86	if [ ! -d '$(DESTDIR_SQ)$1' ]; then             \
87		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
88	fi
89endef
90
91define do_install
92	if [ ! -d '$2' ]; then             \
93		$(INSTALL) -d -m 755 '$2'; \
94	fi;                                             \
95	$(INSTALL) $1 $(if $3,-m $3,) '$2'
96endef
97
98install_lib: $(LIBFILE)
99	$(call QUIET_INSTALL, $(LIBFILE)) \
100		$(call do_install_mkdir,$(libdir_SQ)); \
101		cp -fpR $(LIBFILE) $(DESTDIR)$(libdir_SQ)
102
103HDRS := exec-cmd.h help.h pager.h parse-options.h run-command.h
104INSTALL_HDRS_PFX := $(DESTDIR)$(prefix)/include/subcmd
105INSTALL_HDRS := $(addprefix $(INSTALL_HDRS_PFX)/, $(HDRS))
106
107$(INSTALL_HDRS): $(INSTALL_HDRS_PFX)/%.h: %.h
108	$(call QUIET_INSTALL, $@) \
109		$(call do_install,$<,$(INSTALL_HDRS_PFX)/,644)
110
111install_headers: $(INSTALL_HDRS)
112	$(call QUIET_INSTALL, libsubcmd_headers)
113
114install: install_lib install_headers
115
116clean:
117	$(call QUIET_CLEAN, libsubcmd) $(RM) $(LIBFILE); \
118	find $(or $(OUTPUT),.) -name \*.o -or -name \*.o.cmd -or -name \*.o.d | xargs $(RM)
119
120FORCE:
121
122.PHONY: clean FORCE
123