#
# Copyright (c) Citrix Systems, Inc.
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
#   1) Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
# 
#   2) Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
#

MAJOR = 2
MINOR = 0

CFLAGS = -g -Iinclude                     \
         $(shell xml2-config --cflags) \
         $(shell curl-config --cflags) \
         -W -Wall -Wmissing-prototypes -Werror -std=c99 -fPIC

LDFLAGS = -g $(shell xml2-config --libs) \
          $(shell curl-config --libs) \
	  -Wl,-rpath,$(shell pwd)

# -h for Solaris
SONAME_LDFLAG ?= -soname
# -R /usr/sfw/$(LIBDIR) -shared for Solaris
SHLIB_CFLAGS ?= -shared

# ginstall for Solaris
INSTALL      = install
INSTALL_DIR  = $(INSTALL) -d -m0755 -p
INSTALL_DATA = $(INSTALL) -m0644 -p

LIBXENAPI_HDRS = $(wildcard include/*.h)
LIBXENAPI_OBJS = $(patsubst %.c, %.o, $(wildcard src/*.c))

TEST_PROGRAMS = test/test_vm_ops test/test_event_handling \
                test/test_failures test/test_vm_async_migrate

TARBALL_DEST = libxenserver-$(MAJOR).$(MINOR)

.PHONY: all
all: $(TEST_PROGRAMS)

libxenserver.so: libxenserver.so.$(MAJOR)
	ln -sf $< $@

libxenserver.so.$(MAJOR): libxenserver.so.$(MAJOR).$(MINOR)
	ln -sf $< $@

libxenserver.so.$(MAJOR).$(MINOR): $(LIBXENAPI_OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenserver.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $^

libxenserver.a: $(LIBXENAPI_OBJS)
	$(AR) rcs libxenserver.a $^

$(TEST_PROGRAMS): test/%: test/%.o libxenserver.so
	$(CC) -o $@ $< -L . -lxenserver $(LDFLAGS)


.PHONY: install
install: all
	$(INSTALL_DIR) $(DESTDIR)/usr/include/xen/api
	$(INSTALL_DIR) $(DESTDIR)/usr/$(LIBDIR)
	$(INSTALL_PROG) libxenserver.so.$(MAJOR).$(MINOR) $(DESTDIR)/usr/$(LIBDIR)
	ln -sf libxenserver.so.$(MAJOR).$(MINOR) $(DESTDIR)/usr/$(LIBDIR)/libxenserver.so.$(MAJOR)
	ln -sf libxenserver.so.$(MAJOR) $(DESTDIR)/usr/$(LIBDIR)/libxenserver.so
	$(INSTALL_DATA) libxenserver.a $(DESTDIR)/usr/$(LIBDIR)
	for i in $(LIBXENAPI_HDRS); do \
	    $(INSTALL_DATA) $$i $(DESTDIR)/usr/include/xen/api; \
	done


.PHONY: tarball
tarball: $(TARBALL_DEST).tar.bz2

$(TARBALL_DEST).tar.bz2: all
	rm -Rf $(TARBALL_DEST)
	mkdir -p $(TARBALL_DEST)/include/xen/api
	mkdir -p $(TARBALL_DEST)/src
	mkdir -p $(TARBALL_DEST)/test
	cp COPYING $(TARBALL_DEST)
	cp Makefile.dist $(TARBALL_DEST)/Makefile
	cp Makefile.dist $(TARBALL_DEST)/Makefile.dist
	cp README $(TARBALL_DEST)
	cp include/*.h $(TARBALL_DEST)/include
	cp include/xen/api/*.h $(TARBALL_DEST)/include/xen/api
	cp src/*.c $(TARBALL_DEST)/src
	cp test/*.c $(TARBALL_DEST)/test
	fakeroot chown root:root -R $(TARBALL_DEST)
	fakeroot tar cjf $(TARBALL_DEST).tar.bz2 $(TARBALL_DEST)


.PHONY: clean
clean:
	rm -f `find -name *.o`
	rm -f libxenserver.so*
	rm -f libxenserver.a
	rm -f $(TEST_PROGRAMS)


.PHONY: uberheader
uberheader: include/xen/api/xen_all.h
include/xen/api/xen_all.h::
	echo "/* This file is autogenerated */" >$@
	echo "#ifndef XEN_API_XEN_ALL_H" >>$@
	echo "#define XEN_API_XEN_ALL_H" >>$@
	ls include/xen/api/*.h | grep -v xen_all.h | grep -v _decl.h | \
          sed 's,^include/\(.*\)$$,#include <\1>,g' >>$@
	echo "#endif" >>$@
