# -*- makefile -*-

AM_CPPFLAGS = -DPKGDATADIR=\"$(pkgdatadir)\"
AM_CPPFLAGS += -DPKGLIBDIR=\"$(pkglibdir)\"
pkglocalstatedir = $(localstatedir)/$(PACKAGE)
AM_CPPFLAGS += -DPKGLOCALSTATEDIR=\"$(pkglocalstatedir)\"
pkgsysconfdir = $(sysconfdir)/$(PACKAGE)
AM_CPPFLAGS += -DPKGSYSCONFDIR=\"$(pkgsysconfdir)\"
AM_CPPFLAGS += -include $(top_builddir)/config.h

# Include paths.
AM_CPPFLAGS += -I $(top_srcdir)/src/include
AM_CPPFLAGS += $(OPENFLOW_CPPFLAGS) 
AM_CPPFLAGS += $(BOOST_CPPFLAGS)

# GNU C library definitions.
AM_CPPFLAGS += -D_GNU_SOURCE=1
if NDEBUG
AM_CPPFLAGS += -g -DNDEBUG
endif
if PROFILING
AM_CPPFLAGS += -DPROFILING
endif
AM_CPPFLAGS += -D_REENTRANT

# GNU C++ library definitions.
AM_CPPFLAGS += -D__STDC_LIMIT_MACROS=1
AM_CPPFLAGS += -D__STDC_FORMAT_MACROS=1
AM_CPPFLAGS += -D__STDC_CONSTANT_MACROS=1
if !NDEBUG
AM_CPPFLAGS += -D_GLIBCXX_CONCEPT_CHECKS=1
AM_CPPFLAGS += -D_GLIBCXX_DEBUG=1
AM_CPPFLAGS += -D_GLIBCXX_DEBUG_PEDANTIC=1
endif

# Always enable frame pointers.  The benefits for debugging outweigh
# the costs.  (On x86-64 frame pointers are disabled by default, so
# we need this option.)
COMMON_FLAGS = -fno-omit-frame-pointer -fno-strict-aliasing
COMMON_FLAGS += -Wall -Wno-sign-compare -Winit-self -Wformat-nonliteral -Wformat-security

AM_CXXFLAGS = $(COMMON_FLAGS)

AM_CFLAGS = $(COMMON_FLAGS)
AM_CFLAGS += -Wstrict-prototypes
AM_CFLAGS += -Wmissing-prototypes

LDADD =

AM_LDFLAGS = -R$(pkglibdir)
if !NDEBUG
AM_LDFLAGS += -export-dynamic
endif

# set python runtimefiles to be installed in the same directory as pkg
pkglib_SCRIPTS = $(NOX_RUNTIMEFILES) $(NOX_PYBUILDFILES)
BUILT_SOURCES  = $(NOX_PYBUILDFILES)

# Runtime-files build and clean rules
nox-runtimefiles-build:
	@dlist="$(NOX_RUNTIMEFILES)";for f in $$dlist; do \
		if test -f ${srcdir}/$$f && test ! -f ${builddir}/$$f; then \
			echo "Soft-linking file: $(builddir)/$$f";\
			ln -sf $(srcdir)/$$f $(builddir)/$$f;\
		fi;\
	done;

nox-runtimefiles-clean:
	@dlist="$(NOX_RUNTIMEFILES)";for f in $$dlist; do \
		if test ! -f $(builddir)/$$f; then \
			echo "Removing soft-link: $(builddir)/$$f";\
			rm -f $(builddir)/$$f;\
		fi;\
	done;
	rm -f *.pyc

# Mako template build and clean rules
mako-template-build:
	@if test -d $(srcdir)/templates; then\
		if test ! -d $(builddir)/templates; then\
			echo "Soft-linking mako templates in: $(builddir)/templates";\
			ln -snf $(srcdir)/templates $(builddir)/templates;\
		fi;\
		if test ! -d $(builddir)/mako_modules; then\
			echo "Creating mako modules directory: $(builddir)/mako_modules";\
			mkdir -p $(builddir)/mako_modules;\
		fi;\
	else\
		echo "The target mako-template-build is a dependency in the";\
		echo "Makefile but there is no 'templates' subdirectory.";\
		false;\
	fi;

mako-template-clean:
	rm -rf $(builddir)/mako_modules
	@if test -L $(builddir)/templates; then\
		echo "Removing soft-link: $(builddir)/templates";\
		rm -f $(builddir)/templates;\
	fi

WWW_BUILD_SUBDIR=$(top_builddir)/src/nox/webapps/webserver/www

# Base www build rule.
#
# NOTE: this is for including things into the base level of the
# webroot directory tree.  It is not intended for constructing general
# subtrees within that directory hierarchy.  It should be used
# infrequently.  The static-www-build rule, which includes things in a
# component-specific subdirectory is much more appropriate in most
# circumstances.
base-www-build:
	mkdir -p "$(WWW_BUILD_SUBDIR)/.files"
	if test -z "$(BASE_WWW_FILES)"; then \
		if test -d "base_www"; then \
			fn=`find base_www -printf "%P\n"`; \
		else \
			fn=""; \
		fi; \
	else \
	    fn="$(BASE_WWW_FILES)"; \
	fi; \
	for f in $$fn; do \
		echo "Soft-linking from base_www: $$f";\
		ln -snf ../../$(top_srcdir)/src/$(MODULE_BASE_PATH)/$(MODULE)/base_www/$$f $(WWW_BUILD_SUBDIR)/.files;\
	done;

base-www-clean:     # Intentionally doesn't do anything

# Static www build and clean rules
static-www-build:
	@if test -d $(srcdir)/static_www; then\
		if test ! -d $(WWW_BUILD_SUBDIR)/$(MODULE_BASE_PATH); then\
			echo "Creating module www directory: $(WWW_BUILD_SUBDIR)/$(MODULE_BASE_PATH)";\
			mkdir -p $(WWW_BUILD_SUBDIR)/.files/$(MODULE_BASE_PATH);\
		fi;\
		echo "Soft-linking 'static_www' directory: $(WWW_BUILD_SUBDIR)/$(MODULE_BASE_PATH)/$(MODULE)";\
		ln -snf ../$(MODULE_BASE_ESCAPE)/../$(top_srcdir)/src/$(MODULE_BASE_PATH)/$(MODULE)/static_www $(WWW_BUILD_SUBDIR)/.files/$(MODULE_BASE_PATH)/$(MODULE);\
	else\
		echo "The target static-www-build is a dependency in the";\
		echo "Makefile but there is no 'static_www' subdirectory here.";\
		false;\
	fi;

static-www-clean:     # Intentionally doesn't do anything

# NOX rules for setting up links in remote build tree 
nox-all-local:
	@dlist="$(NOX_RUNTIMEFILES)";for f in $$dlist; do \
	  if test -f $(srcdir)/$$f && test ! -f $$f; then \
		ln -sf $(srcdir)/$$f $(builddir)/$$f;\
	  fi;\
	done;
	@dlist="$(NOX_PYLIBFILES)";for f in $$dlist; do \
	  if test ! -f $$f; then \
		ln -sf $(builddir)/.libs/$$f $(builddir)/$$f;\
	  fi;\
	done;

# NOX clean rules 
nox-clean-local:
	@find . -maxdepth 1 -name "*.py" -type l -exec rm -rf {} \;
	@rm -f *_wrap.cc *.pyc *.la *.so *.o
	@rm -rf __nox_tests
	@dlist="$(NOX_PYBUILDFILES)";for f in $$dlist; do \
      if test -f $(builddir)/$$f; then \
        rm -f $(builddir)/$$f;\
      fi;\
    done;

# NOX install rules
pkglibdir = $(subst src,$(bindir),$(subdir))
nox_installsubdir = $(DESTDIR)$(subst src,$(bindir),$(subdir))
nox_installwwwdir = $(DESTDIR)$(bindir)/nox/webapps/webserver/www

nox-install-local:
	@if test -d $(srcdir)/templates; then\
		cp -rf -L $(srcdir)/templates $(nox_installsubdir)/templates;\
		if test ! -d $(nox_installsubdir)/mako_modules; then\
			mkdir -p $(builddir)/mako_modules;\
		fi;\
	fi;

test:
	@$(PYTHON) $(top_builddir)/src/nox/coreapps/testharness/testharness.py $(TEST_FLAGS) -b $(top_builddir) ./
