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