#
# Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
# Copyright (C) 2004-2018 Red Hat, Inc. All rights reserved.
#
# This file is part of LVM2.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = @top_builddir@

all: device-mapper

SOURCES2 = dmsetup.c

CFLOW_SOURCES = $(addprefix $(srcdir)/, $(SOURCES2))
CFLOW_TARGET := $(TARGETS_DM)
-include $(top_builddir)/libdm/libdevmapper.cflow

ifeq ("@SHARED_LINK@", "yes")
  TARGETS_DM += dmsetup
  install_device-mapper: install_dmsetup_dynamic
endif

ifeq ("@STATIC_LINK@", "yes")
  TARGETS_DM += dmsetup.static
  install_device-mapper: install_dmsetup_static
endif

# dmfilemapd support
ifeq ("@BUILD_DMFILEMAPD@", "yes")
  SOURCES2 += dmfilemapd.c

  ifeq ("@SHARED_LINK@", "yes")
     TARGETS_DM += dmfilemapd
     install_dmfilemapd: install_dmfilemapd_dynamic
  endif

  ifeq ("@STATIC_LINK@", "yes")
    TARGETS_DM += dmfilemapd.static
    install_dmfilemapd: install_dmfilemapd_static
  endif
endif

CLEAN_TARGETS = $(LDDEPS) .exported_symbols_generated \
	dmfilemapd dmfilemapd.static \
	dmsetup dmsetup.static

include $(top_builddir)/libdm/make.tmpl

CFLAGS_dmsetup.o += $(UDEV_CFLAGS) $(EXTRA_EXEC_CFLAGS)
CFLAGS_dmfilemapd.o += $(EXTRA_EXEC_CFLAGS)
LIBDM_LIBS = -L$(interfacebuilddir) -ldevmapper
LIBDM_SHARED = $(interfacebuilddir)/libdevmapper.so
LIBDM_STATIC = $(interfacebuilddir)/libdevmapper.a

dmsetup: dmsetup.o $(LIBDM_SHARED)
	$(SHOW) "    [CC] $@"
	$(Q) $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_EXEC_LDFLAGS) $(ELDFLAGS) \
	      -o $@ $< $(LIBDM_LIBS) $(LIBS)

dmsetup.static: dmsetup.o $(LIBDM_STATIC)
	$(SHOW) "    [CC] $@"
	$(Q) $(CC) $(CFLAGS) $(LDFLAGS) $(STATIC_LDFLAGS) -static \
	      -o $@ $< $(LIBDM_LIBS) $(LIBS) $(STATIC_LIBS)

install_dmsetup_dynamic: dmsetup
	$(SHOW) "    [INSTALL] $<"
	$(Q) $(INSTALL_PROGRAM) -D $< $(sbindir)/$(<F)
	$(Q) $(LN_S) -f $(<F) $(sbindir)/dmstats

install_dmsetup_static: dmsetup.static
	$(SHOW) "    [INSTALL] $<"
	$(Q) $(INSTALL_PROGRAM) -D $< $(staticdir)/$(<F)
	$(Q) $(LN_S) -f $(<F) $(staticdir)/dmstats.static


dmfilemapd: dmfilemapd.o $(LIBDM_SHARED)
	$(SHOW) "    [CC] $@"
	$(Q) $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_EXEC_LDFLAGS) $(ELDFLAGS) \
	      -o $@ $< $(LIBDM_LIBS) $(LIBS)

dmfilemapd.static: dmfilemapd.o $(LIBDM_STATIC)
	$(SHOW) "    [CC] $@"
	$(Q) $(CC) $(CFLAGS) $(LDFLAGS) $(STATIC_LDFLAGS) -static \
	      -o $@ $< $(LIBDM_LIBS) $(LIBS) $(STATIC_LIBS)

install_dmfilemapd_dynamic: dmfilemapd
	$(SHOW) "    [INSTALL] $<"
	$(Q) $(INSTALL_PROGRAM) -D $< $(sbindir)/$(<F)

install_dmfilemapd_static: dmfilemapd.static
	$(SHOW) "    [INSTALL] $<"
	$(Q) $(INSTALL_PROGRAM) -D $< $(staticdir)/$(<F)


.PHONY: install_dmsetup_dynamic install_dmsetup_static
.PHONY: install_dmfilemapd install_dmfilemapd_static

install: install_device-mapper install_dmfilemapd

device-mapper: $(TARGETS_DM)
