# Makefile for drbd.o
#
# This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
#
# drbd is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# drbd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with drbd; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#

# variables set by configure
DISTRO = @DISTRO@
prefix = @prefix@
exec_prefix = @exec_prefix@
localstatedir = @localstatedir@
datarootdir = @datarootdir@
datadir = @datadir@
sbindir = @sbindir@
sysconfdir = @sysconfdir@
BASH_COMPLETION_SUFFIX = @BASH_COMPLETION_SUFFIX@
UDEV_RULE_SUFFIX = @UDEV_RULE_SUFFIX@
INITDIR = @INITDIR@
LIBDIR = @prefix@/lib/@PACKAGE_TARNAME@
CC = @CC@
CFLAGS = @CFLAGS@
LDFLAGS = @LDFLAGS@
LN_S = @LN_S@

# features enabled or disabled by configure
WITH_UTILS = @WITH_UTILS@
WITH_KM = @WITH_KM@
WITH_UDEV = @WITH_UDEV@
WITH_XEN = @WITH_XEN@
WITH_PACEMAKER = @WITH_PACEMAKER@
WITH_HEARTBEAT = @WITH_HEARTBEAT@
WITH_RGMANAGER = @WITH_RGMANAGER@
WITH_BASHCOMPLETION = @WITH_BASHCOMPLETION@

# variables meant to be overridden from the make command line
DESTDIR ?= /

CFLAGS += -Wall -I../drbd -I../drbd/compat

drbdadm-obj = drbdadm_scanner.o drbdadm_parser.o drbdadm_main.o \
	      drbdadm_adjust.o drbdtool_common.o drbdadm_usage_cnt.o \
	      drbd_buildtag.o drbdadm_minor_table.o

drbdsetup-obj = drbdsetup.o drbdtool_common.o drbd_buildtag.o \
	        drbd_strings.o

drbdmeta-obj = drbdmeta.o drbdmeta_scanner.o drbdtool_common.o drbd_buildtag.o

all: tools

ifeq ($(WITH_UTILS),yes)
tools: drbdadm drbdmeta drbdsetup
else
tools:
endif

drbd_buildtag.c: ../drbd/drbd_buildtag.c
	cp $^ $@

drbd_strings.c: ../drbd/drbd_strings.c
	cp $^ $@

drbdadm: $(drbdadm-obj)
	$(CC) $(LDFLAGS) -o $@ $^

drbdadm_scanner.c: drbdadm_scanner.fl drbdadm_parser.h
	flex -s -odrbdadm_scanner.c drbdadm_scanner.fl

drbdmeta_scanner.c: drbdmeta_scanner.fl drbdmeta_parser.h
	flex -s -odrbdmeta_scanner.c drbdmeta_scanner.fl

drbdsetup: $(drbdsetup-obj)
	$(CC) $(LDFLAGS) -o $@ $^

drbdmeta: $(drbdmeta-obj)
	$(CC) $(LDFLAGS) -o $@ $^

clean:
	rm -f drbdadm_scanner.c drbdmeta_scanner.c
	rm -f drbdsetup drbdadm drbdmeta drbdmeta_unfinished_rewrite *.o
	rm -f drbd_buildtag.c drbd_strings.c
	rm -f *~

distclean: clean

install:
ifeq ($(WITH_UTILS),yes)
	install -d $(DESTDIR)/sbin/
	install -d $(DESTDIR)$(localstatedir)/lib/drbd
	install -d $(DESTDIR)$(localstatedir)/lock
	if getent group haclient > /dev/null 2> /dev/null ; then	\
		install -g haclient -m 4750 drbdsetup $(DESTDIR)/sbin/ ;	\
		install -g haclient -m 4750 drbdmeta $(DESTDIR)/sbin/ ;	\
		install -m 755 drbdadm $(DESTDIR)/sbin/ ;		\
	else								\
		install -m 755 drbdsetup $(DESTDIR)/sbin/ ;		\
		install -m 755 drbdmeta $(DESTDIR)/sbin/ ;		\
		install -m 755 drbdadm $(DESTDIR)/sbin/ ; 		\
	fi
endif

uninstall:
	rm -f $(DESTDIR)/sbin/drbdsetup
	rm -f $(DESTDIR)/sbin/drbdadm

spell:
	for f in drbdadm_adjust.c drbdadm_main.c drbdadm_parser.c drbdadm_usage_cnt.c drbdmeta.c drbdsetup.c drbdtool_common.c; do \
	 	aspell --save-repl --dont-backup --personal=./../documentation/aspell.en.per check $$f; \
	done

###dependencies
drbdsetup.o:       drbdtool_common.h ../drbd/linux/drbd_limits.h
drbdsetup.o:       ../drbd/linux/drbd_tag_magic.h ../drbd/linux/drbd.h
drbdsetup.o:       ../drbd/linux/drbd_config.h ../drbd/linux/drbd_nl.h
drbdsetup.o:       unaligned.h
drbdtool_common.o: drbdtool_common.h
drbdadm_main.o:    drbdtool_common.h drbdadm.h
drbdadm_adjust.o:  drbdtool_common.h drbdadm.h
drbdadm_parser.o:  drbdtool_common.h drbdadm.h ../drbd/linux/drbd_limits.h
drbdadm_scanner.o:                   drbdadm.h               drbdadm_parser.h
drbdsetup.o:       drbdtool_common.h           ../drbd/linux/drbd_limits.h
drbdmeta.o:        drbdtool_common.h drbd_endian.h
drbdmeta_unfinished_rewrite.o:        drbdtool_common.h drbd_endian.h
drbdadm_usage_cnt.o: drbdadm.h drbd_endian.h
