##############################################################################
# Copyright (c) 2000-2021 Ericsson Telecom AB
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# which accompanies this distribution, and is available at
# https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
#
# Contributors:
#   Balasko, Jeno
#   Baranyi, Botond
#   Forstner, Matyas
#   Kovacs, Ferenc
#   Pandi, Krisztian
#   Raduly, Csaba
#   Szabo, Janos Zoltan – initial implementation
#
##############################################################################
# General stuff (to be included at the end of makefiles). The
# following variables are used: DEPFILES, SUBDIRS...

tags: $(SOURCES)
ifdef SUBDIRS
	@for i in $(SUBDIRS) ; do \
	  (cd $$i && $(MAKE) tags) || exit 1; \
	done
endif
	etags --members *.hh *.h *.c *.cc

dep:
ifdef SUBDIRS
	@for i in $(SUBDIRS) ; do \
	  (cd $$i && $(MAKE) dep) || exit 1; \
	done
endif
ifdef DEPFILES
	$(MAKE) $(DEPFILES)
endif

clean:
ifdef SUBDIRS
	@for i in $(SUBDIRS) ; do \
	  (cd $$i && $(MAKE) clean) || exit 1; \
	done
endif
	$(RM) $(TARGETS) $(OBJECTS) $(TOBECLEANED)

distclean:
ifdef SUBDIRS
	@for i in $(SUBDIRS) ; do \
	  (cd $$i && $(MAKE) distclean) || exit 1; \
	done
endif
	$(RM) $(TARGETS) $(OBJECTS) $(TOBECLEANED) \
	  $(GENERATED_HEADERS) $(GENERATED_SOURCES) \
	  $(GENERATED_OTHERS) \
	  $(DEPFILES) TAGS *.gcno *.gcda

# This target allows us to "make ../clean"
../% $(foreach dir, $(SUBDIRS), $(dir)/%):
	cd $(dir $@) && $(MAKE) $(notdir $@)

# General rules to compile C(++) files.
#
# These macros implement "silent" rules during building.
# Define the V make variable or environment variable to a nonzero value to get
# the exact call to the compiler: e.g.     make V=1
#
# Define the VD variable to get the exact (verbose) action while
# generating dependencies.
#
NULL  :=
SPACE := ${NULL} ${NULL}

DEF_V := 0
DEF_VD:= 0

V_CC_0  = @echo  "  (CC)	" $<;$(SPACE)
V_CXX_0 = @echo  "  (C++)	" $<;$(SPACE)
V_DEP_0 = @echo  "  (dep)	" $<;$(SPACE)

V_CC_  = $(V_CC_$(DEF_V))
V_CXX_ = $(V_CXX_$(DEF_V))
V_DEP_ = $(V_DEP_$(DEF_VD))

V_CC  = $(V_CC_$(V))
V_CXX = $(V_CXX_$(V))
V_DEP = $(V_DEP_$(VD))

%.o: %.c
	$(V_CC)$(CC) -c $(CPPFLAGS) $(CCFLAGS) $< -o $@

# Special rule for building profmerge files
%.profmerge.o: %.cc
	$(V_CXX)$(CXX) -c -DPROF_MERGE $(CPPFLAGS) $(CXXFLAGS) $< -o $@

%.o: %.cc
	$(V_CXX)$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@

# Preprocess C/C++ files
%.i: %.c
	$(CC) -E $(CPPFLAGS) $(CCFLAGS) \
		$< > $@

%.ii: %.cc
	$(CXX) -E $(CPPFLAGS) $(CXXFLAGS) \
		$< > $@

# General rules to create the dependency file.

%.d: %.c
	$(V_DEP)set -e; $(CC) $(CCDEPFLAG) $(CPPFLAGS) $< \
	| sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' > $@; \
	[ -s $@ ] || rm -f $@

%.d: %.cc
	$(V_DEP)set -e; $(CXX) $(CXXDEPFLAG) $(CPPFLAGS) $< \
	| sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' > $@; \
	[ -s $@ ] || rm -f $@

ifdef DEPFILES
  ifndef MAKECMDGOALS
    DEPFILES_NEEDED := yes
  else
    DEPFILES_NEEDED := $(filter-out clean distclean, $(MAKECMDGOALS))
  endif
  ifdef DEPFILES_NEEDED
    DEPFILES_INCLUDE := $(filter-out $(MAKECMDGOALS), $(DEPFILES))
    ifdef DEPFILES_INCLUDE
      ifeq (,$(findstring n,$(MAKEFLAGS)))
        # -n was *not* given to make
        -include $(DEPFILES_INCLUDE)
      endif
    endif
  endif
endif

# Building PDFs from man pages (for MinGW)
%.pdf: %.1
	man2pdf ./$< $@
# The "./" prefix is important, it tells man that the input is a filename,
# so it shouldn't search through MANPATH.

# List of fake targets:
.PHONY: all install tags dep clean distclean

# Disable all built-in suffix rules of make
.SUFFIXES:

# Do not delete generated headers while building .d files
# (.PRECIOUS would also keep them if make is killed)
.SECONDARY: $(GENERATED_HEADERS)

ifdef SRCDIR

REQUIRED_MAKE_VERSION = 3.81
# 3.80 is known not to work; 3.82 does work
REAL_MAKE_VERSION = $(firstword $(MAKE_VERSION))
EARLIER_MAKE_VERSION = $(firstword $(sort $(REAL_MAKE_VERSION) $(REQUIRED_MAKE_VERSION)))
ifeq "$(REQUIRED_MAKE_VERSION)" "$(EARLIER_MAKE_VERSION)"

# Declare a search path for every source.
# "vpath %.cc $(ABS_SRC)" would lump in generated .cc files,
# potentially picking up generated files laying around in the source dir
# instead of generating them in the build dir.
$(foreach src, $(STATIC_SOURCES) $(ORIGINATORS), $(eval vpath $(src) $(ABS_SRC)))

else

# alas, make 3.80 can't cope with the "foreach/eval vpath" above
#$(warning no OOBE with make $(MAKE_VERSION))

endif

endif
