# Copyright 2023 Kevin Ryde

# This file is part of pari-primecount.

# pari-primecount 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 3, or (at your option) any later
# version.
#
# pari-primecount 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 pari-primecount.  See the file COPYING.  If not, see
# <http://www.gnu.org/licenses/>.

VERSION = 1

GP = gp
GPFLAGS = -f --quiet
CFLAGS = -O -Wall -fPIC
LOADLIBES = -lprimecount

pari_primecount.so: pari_primecount.c
	$(CC) $(CFLAGS) -shared pari_primecount.c $(LOADLIBES) -o pari_primecount.so

test: test-library test-program
test-library: pari_primecount.so
	$(GP) $(GPFLAGS) <test.gp
test-program:
	echo 'primecount_use_program = 1; read("test.gp");' | $(GP) $(GPFLAGS)

clean:
	rm -f pari_primecount.so

#------------------------------------------------------------------------------
# Development

DISTNAME = pari-primecount
DISTVNAME = $(DISTNAME)-$(VERSION)

DISTFILES = \
  COPYING \
  Makefile \
  *.gp \
  *.c \
  devel/Makefile \
  devel/*.c \
  devel/*.gp \
  devel/*.sh \
  unused/*.gp \
  unused/*.pl \
  unused/nthprimeweb-fetch

distdir:
	$(MAKE) -$(MAKEFLAGS) $(DIST_GENERATED_FILES)
	rm -rf $(DISTVNAME)
	mkdir $(DISTVNAME)
	mkdir $(DISTVNAME)/devel
	mkdir $(DISTVNAME)/unused
	set -e; for i in $(DISTFILES); do \
	  cp -p $$i $(DISTVNAME)/$$i; \
	done
dist:
	$(MAKE) -$(MAKEFLAGS) distdir
	tar cf $(DISTVNAME).tar --sort=name $(DISTVNAME)
	gzip -9 --force $(DISTVNAME).tar
	rm -rf $(DISTVNAME)

spell:
	ispell-batch-gp.pl \
	  --localwords="PARI pari GP gp www org Ryde" \
	  --localwords="primecount prime primepi" \
	  --localwords="sopath LD usr" \
	  --localwords="eval externstr malloc" \
	  --localwords="init num str n'th stdout stderr" \
	  --localwords="vG GG INT MEM" \
	  primecount.gp

#------------------------------------------------------------------------------

.PHONY: clean dist distdir test
