# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 1999, 2001-2003 Silicon Graphics, Inc.  All Rights Reserved.
#
ifndef _BUILDRULES_INCLUDED_
_BUILDRULES_INCLUDED_ = 1

include $(TOPDIR)/include/builddefs

clean clobber : $(addsuffix -clean,$(SUBDIRS))
	@rm -f $(DIRT) .ltdep .dep .extradep
	@rm -fr $(DIRDIRT)
%-clean:
	@echo "Cleaning $*"
	$(Q)$(MAKE) $(MAKEOPTS) -C $* clean

ifdef HDR_SUBDIRS
.PHONY: .xfs

headers: $(addsuffix -headers, $(HDR_SUBDIRS))

%-headers: .xfs
	@echo "   [HEADERS] $*"
	$(Q)$(MAKE) $(MAKEOPTS) -C $* NODEP=1 install-headers

.xfs:
	@mkdir -p include/xfs
endif

# Never blow away subdirs
ifdef SUBDIRS
.PRECIOUS: $(SUBDIRS)
.PHONY: $(SUBDIRS)

$(SUBDIRS):
	@echo "Building $@"
	$(Q)$(MAKE) $(MAKEOPTS) -q -C $@ || $(MAKE) $(MAKEOPTS) -C $@
endif

ifndef CHECK_CMD
CHECK_CMD = @true
endif

#
# Standard targets
#

ifeq ($(CHECKSRC),2)

# Check every .c file with sparse CHECK_CMD, do not call compiler
$(LTCOMMAND) $(LTLIBRARY) : $(SUBDIRS) $(OBJECTS)
.PHONY: $(LTCOMMAND) $(LTLIBRARY)

%.lo %.o : %.c FORCE
	@echo "    [CHECK]  $<"
	$(Q)$(CHECK_CMD) $(CFLAGS) $<

FORCE:

else
# Regular build, possibly calling sparse CHECK_CMD as well

ifdef LTCOMMAND
$(LTCOMMAND) : $(SUBDIRS) $(OBJECTS) $(LTDEPENDENCIES)
	@echo "    [LD]     $@"
	$(Q)$(LTLINK) -o $@ $(LDFLAGS) $(OBJECTS) $(LDLIBS)
endif

ifdef LTLIBRARY
$(LTLIBRARY) : $(SUBDIRS) $(LTOBJECTS)
	@echo "    [LD]     $@"
	$(Q)$(LTLINK) $(LTLDFLAGS) -o $(LTLIBRARY) $(LTOBJECTS) $(LTLIBS)

%.lo: %.c
	@echo "    [CC]     $@"
	$(Q)$(LTCOMPILE) -c $<
	$(Q)$(CHECK_CMD) $(CFLAGS) $<
else

%.o: %.c
	@echo "    [CC]     $@"
	$(Q)$(CC) $(CFLAGS) -c $<
	$(Q)$(CHECK_CMD) $(CFLAGS) $<

endif
endif

ifdef POTHEAD
XGETTEXT_FLAGS=\
	--keyword=_ \
	--keyword=N_ \
	--package-name=$(PKG_NAME) \
	--package-version=$(PKG_VERSION) \
	--msgid-bugs-address=$(PKG_BUGREPORT)

$(POTHEAD): $(XGETTEXTFILES)
	@echo "    [GETTXT] $@"
	$(Q)$(XGETTEXT) $(XGETTEXT_FLAGS) -o $@ $(XGETTEXTFILES)

# Update translations
update-po: $(POTHEAD) $(wildcard $(TOPDIR)/po/*.po)
	catalogs="$(wildcard $(TOPDIR)/po/*.po)"; \
	for cat in $$catalogs; do \
	  lang=`basename $$cat .po`; \
	  mv $$lang.po $$lang.old.po; \
	  echo "$$lang:"; \
	  if $(MSGMERGE) $$lang.old.po $(POTHEAD) -o $$lang.po; then \
	    rm -f $$lang.old.po; \
	  else \
	    echo "msgmerge for $$lang failed!"; \
	    rm -f $$lang.po; \
	    mv $$lang.old.po $$lang.po; \
	  fi; \
	done

%.mo: %.po
	@echo "    [MSGFMT] $@"
	$(Q)$(MSGFMT) -c --statistics -o $@ $<
endif

endif # _BUILDRULES_INCLUDED_

$(_FORCE):

# dependency build is automatic, relies on gcc -MM to generate.
#
# This is a bit messy. It regenerates the dependencies on each build so
# that we catch files being added and removed. There are other ways of doing
# this (e.g. per-file dependency files) but that requires more in-depth changes
# to the build system. Compile time is not an issue for us, so the
# rebuild on every make invocation isn't a problem we need to care about. Just
# do it silently so it doesn't make the build unnecessarily noisy.

.PHONY : depend ltdepend

MAKEDEP := $(MAKEDEPEND) $(CFLAGS)

ltdepend: rmltdep .ltdep

rmltdep:
	$(Q)rm -f .ltdep

.ltdep: $(CFILES) $(HFILES)
	$(Q)$(MAKEDEP) $(CFILES) | $(SED) -e 's,^\([^:]*\)\.o,\1.lo,' > .ltdep

depend: rmdep .dep

rmdep:
	$(Q)rm -f .dep

.dep: $(CFILES) $(HFILES)
	$(Q)$(MAKEDEP) $(CFILES) > .dep
