# Copyright (C) 2007 Manu Garg.
# Author: Manu Garg <manugarg@gmail.com>
#
# Makefile for pacparser. Please read README file included with this package
# for more information about pacparser.
#
# pacparser is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.

# pacparser 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

# This file is not part of the source code repository. It's generated by the
# packaging script.
-include version.mk

VERSION ?= $(shell git describe --always --tags --candidate=100)

OS_ARCH := $(subst /,_,$(shell uname -s | sed /\ /s//_/))

LIBRARY_NAME = libpacparser
LIB_VER = 1

# This Makefile should at least work on Linux and Mac OS X. It should work on
# most other types of Unix systems too, but I have not put any conscious effort
# for that.
#
# Set variables according to Linux.
SO_SUFFIX = so
LIBRARY = $(LIBRARY_NAME).$(SO_SUFFIX).$(LIB_VER)
MKSHLIB = $(CC) -shared
LIB_OPTS = -Wl,-soname=$(LIBRARY) -Wl,-exclude-libs=libjs.a
SHFLAGS = -fPIC
SMCFLAGS = -DHAVE_VA_COPY -DVA_COPY=__va_copy

ifeq ($(OS_ARCH),Darwin)
  PREFIX ?= /usr/local
  MAC_MAJOR_VERSION := $(shell sw_vers -productVersion | cut -d. -f1)
  MAC_GT_OS11 := $(shell [ $(MAC_MAJOR_VERSION) -le 10 ] && echo false)
  SO_SUFFIX = dylib
  LIBRARY = $(LIBRARY_NAME).$(LIB_VER).$(SO_SUFFIX)
  MKSHLIB = $(CC) -dynamiclib -framework System
  LIB_OPTS = -install_name $(PREFIX)/lib/$(notdir $@)
  SHFLAGS =
  ifeq ($(MAC_GT_OS11),false)
    MAC_MINOR_VERSION := $(shell sw_vers -productVersion | cut -d. -f2)
    MAC_GT_10_5 := $(shell [ $(MAC_MINOR_VERSION) -le 5 ] && echo false)
    ifeq ($(MAC_GT_10_5),false)
      SMCFLAGS =
    endif
  endif
endif

PREFIX ?= /usr
MAINT_CFLAGS := -g -DXP_UNIX -Wall -DVERSION=$(VERSION)

ifndef PYTHON
  PYTHON = python
endif

# Spidermonkey library.
MAINT_CFLAGS += -Ispidermonkey/js/src

LIBRARY_LINK = $(LIBRARY_NAME).$(SO_SUFFIX)
PREFIX := $(DESTDIR)$(PREFIX)
LIB_PREFIX = $(PREFIX)/lib
INC_PREFIX = $(PREFIX)/include
BIN_PREFIX = $(PREFIX)/bin
DOC_PREFIX = $(PREFIX)/share/doc/pacparser
MAN_PREFIX = $(PREFIX)/share/man

.PHONY: clean pymod install-pymod
all: testpactester

jsapi_buildstamp: spidermonkey/js/src
	cd spidermonkey && SMCFLAGS="$(SHFLAGS) $(SMCFLAGS)" $(MAKE) jsapi
	touch jsapi_buildstamp

spidermonkey/libjs.a: spidermonkey/js/src
	cd spidermonkey && SMCFLAGS="$(SHFLAGS) $(SMCFLAGS)" $(MAKE) jslib

pacparser.o: pacparser.c pac_utils.h pacparser.h jsapi_buildstamp
	$(CC) $(MAINT_CFLAGS) $(CFLAGS) $(SHFLAGS) -c pacparser.c -o pacparser.o
	touch pymod/pacparser_o_buildstamp

$(LIBRARY): pacparser.o spidermonkey/libjs.a
	$(MKSHLIB) $(MAINT_CFLAGS) $(CFLAGS) $(LDFLAGS) $(LIB_OPTS) -o $(LIBRARY) pacparser.o spidermonkey/libjs.a -lm

$(LIBRARY_LINK): $(LIBRARY)
	ln -sf $(LIBRARY) $(LIBRARY_LINK)

pactester: pactester.c pacparser.h pacparser.o spidermonkey/libjs.a
	$(CC) $(MAINT_CFLAGS) $(CFLAGS) $(LDFLAGS) pactester.c pacparser.o spidermonkey/libjs.a -o pactester -lm -L. -I.

testpactester: pactester $(LIBRARY_LINK)
	echo "Running tests for pactester."
	NO_INTERNET=$(NO_INTERNET) ../tests/runtests.sh

docs:
	../tools/generatedocs.sh

install: all
	install -d $(LIB_PREFIX) $(INC_PREFIX) $(BIN_PREFIX)
	install -m 644 $(LIBRARY) $(LIB_PREFIX)/$(LIBRARY)
	ln -sf $(LIBRARY) $(LIB_PREFIX)/$(LIBRARY_LINK)
	install -m 755 pactester $(BIN_PREFIX)/pactester
	install -m 644 pacparser.h $(INC_PREFIX)/pacparser.h
	# install pactester manpages
	install -d $(MAN_PREFIX)/man1/
	(test -d ../docs && install -m 644 ../docs/man/man1/*.1 $(MAN_PREFIX)/man1/) || true
	# install pacparser manpages
	install -d $(MAN_PREFIX)/man3/
	(test -d ../docs && install -m 644 ../docs/man/man3/*.3 $(MAN_PREFIX)/man3/) || true
	# install html docs
	install -d $(DOC_PREFIX)/html/
	(test -d ../docs/html && install -m 644 ../docs/html/* $(DOC_PREFIX)/html/) || true
	# install examples
	install -d $(DOC_PREFIX)/examples/
	(test -d ../examples && install -m 644 ../examples/* $(DOC_PREFIX)/examples/) || true

# Targets to build python module
pymod: pacparser.o pacparser.h spidermonkey/libjs.a
	cd pymod && ARCHFLAGS="" $(PYTHON) setup.py build
	$(PYTHON) ../tests/runtests.py

pymod-dist: pacparser.o pacparser.h spidermonkey/libjs.a
	cd pymod && ARCHFLAGS="" $(PYTHON) setup.py build
	cd pymod && ARCHFLAGS="" $(PYTHON) setup.py dist
	$(PYTHON) ../tests/runtests.py

install-pymod: pymod
	cd pymod && ARCHFLAGS="" $(PYTHON) setup.py install --root="$(DESTDIR)/" $(EXTRA_ARGS)

clean:
	rm -f $(LIBRARY_LINK) $(LIBRARY) pacparser.o pactester pymod/pacparser_o_buildstamp jsapi_buildstamp
	rm -rf dist
	cd pymod && $(PYTHON) setup.py clean --all
	cd spidermonkey && $(MAKE) clean
