# Copyright (C) 2007-2022 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

ifeq ($(OS),Windows_NT) 
RM = del /Q /F
CP = copy /Y
ifdef ComSpec
SHELL := $(ComSpec)
endif
ifdef COMSPEC
SHELL := $(COMSPEC)
endif
else
RM = rm -rf
CP = cp -f
endif

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

LIB_VER=1
CFLAGS=-g -DXP_WIN -DWINVER=0x0501 -DVERSION=$(VERSION) -Ispidermonkey/js/src -Wall
CC=gcc
PYTHON ?= python

.PHONY: clean pymod install-pymod

all: pacparser.dll pactester

pacparser.o: pacparser.c pac_utils.h js.lib
	$(CC) $(CFLAGS) -c pacparser.c -o pacparser.o

js.lib:
	$(MAKE) -C spidermonkey -f Makefile.win32

pacparser.dll: pacparser.o spidermonkey/js.lib
	$(CC) -shared -o pacparser.dll \
	-Wl,--output-def,pacparser.def \
	-Wl,--out-implib,libpacparser.a \
	-Wl,--export-all-symbols \
	pacparser.o -ljs -Lspidermonkey -lws2_32

pacparser.lib: pacparser.dll pacparser.def
	lib /machine:i386 /def:pacparser.def

pactester: pactester.c pacparser.h pacparser.o
	$(CC) pactester.c pacparser.o -o pactester -ljs -Lspidermonkey -lws2_32

dist: pacparser.dll pactester pacparser.def
	if exist dist rmdir /s /q dist
	mkdir dist
	$(CP) pacparser.dll dist
	$(CP) pacparser.h dist
	$(CP) pactester.exe dist
	$(CP) pacparser.def dist
	if exist pacparser.lib $(CP) pacparser.lib dist
	$(CP) ..\README.md dist\README.txt
	$(CP) ..\COPYING dist\COPYING.txt
	$(CP) ..\INSTALL dist\INSTALL.txt
	mkdir dist\docs
	$(CP) ..\README.win32.md dist\docs
	if exist ..\docs\html xcopy ..\docs\html dist\docs

# Targets to build python module
pymod: pacparser.h pacparser.dll pacparser.o js.lib
	cd pymod && $(PYTHON) setup.py build --compiler=mingw32
	cd .. && $(PYTHON) tests/runtests.py

pymod-dist: pacparser.h pacparser.dll pacparser.o js.lib
	cd pymod && $(PYTHON) setup.py build --compiler=mingw32 && $(PYTHON) setup.py dist
	cd .. && $(PYTHON) tests/runtests.py

pymod-%: pacparser.h pacparser.dll pacparser.o js.lib
	cd pymod && py -$* setup.py  build --compiler=mingw32
	cd .. && py -$* tests\runtests.py

pymod-dist-%:
	cd pymod && py -$* setup.py dist

clean:
	$(RM) pacparser.dll *.lib pacparser.def pacparser.exp pacparser.o pactester.exe libpacparser.a
	$(MAKE) -C spidermonkey -f Makefile.win32 clean
	cd pymod && $(PYTHON) setup.py clean --all
	$(RM) dist
