# module/Makefile.nyacc			-*- Makefile -*-
#
# Copyright (C) 2015-2018 -  Matthew R. Wette
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.
#

GUILE=guile
GUILD=guild

default:
	@echo SITE_SCM_DIR = $(SITE_SCM_DIR)
	@echo SITE_SCM_GO_DIR = $(SITE_SCM_GO_DIR)
	@echo NYACC_SRCS = $(NYACC_SRCS)

NYACC_BASE_BINS =				\
	nyacc/bison.go				\
	nyacc/export.go				\
	nyacc/import.go				\
	nyacc/lalr.go				\
	nyacc/lex.go				\
	nyacc/parse.go				\
	nyacc/util.go				\
	nyacc/version.go			\
	nyacc/lang/sx-util.go			\
	nyacc/lang/util.go

NYACC_BASE_SRCS = $(patsubst %.go,%.scm,$(NYACC_BASE_BINS))

NYACC_C99_BINS =				\
	nyacc/lang/c99/cpp.go			\
	nyacc/lang/c99/cppmach.go		\
	nyacc/lang/c99/parser.go		\
	nyacc/lang/c99/mach.go			\
	nyacc/lang/c99/pprint.go		\
	nyacc/lang/c99/util1.go			\
	nyacc/lang/c99/munge.go			\
	nyacc/lang/c99/cxmach.go		\
	nyacc/lang/c99/cxeval.go		

NYACC_C99_SRCS = $(patsubst %.go,%.scm,$(NYACC_C99_BINS)) \
	nyacc/lang/c99/body.scm			\
	nyacc/lang/c99/mach.d/c99act.scm	\
	nyacc/lang/c99/mach.d/c99tab.scm	\
	nyacc/lang/c99/mach.d/c99xact.scm	\
	nyacc/lang/c99/mach.d/c99xtab.scm	\
	nyacc/lang/c99/mach.d/cppact.scm	\
	nyacc/lang/c99/mach.d/cpptab.scm	\
	nyacc/lang/c99/mach.d/c99cxact.scm	\
	nyacc/lang/c99/mach.d/c99cxtab.scm


NYACC_BINS = $(NYACC_BASE_BINS) $(NYACC_C99_BINS)
NYACC_SRCS = $(NYACC_BASE_SRCS) $(NYACC_C99_SRCS)

NYACC_FH_BINS =					\
	nyacc/lang/c99/ffi-help.go		\
	system/ffi-help-rt.go			\
	scripts/compile-ffi.go			

NYACC_FH_SRCS = $(patsubst %.go,%.scm,$(NYACC_FH_BINS))


.PHONY: install
install: install-srcs install-fh-srcs install-bins install-fh-bins

.PHONY: install-srcs
install-srcs:
	@mkdir -p $(DESTDIR)$(SITE_SCM_DIR)
	@echo "installing NYACC sources in $(SITE_SCM_DIR)"
	@for file in $(NYACC_SRCS); do \
	  if test ! -d $(DESTDIR)$(SITE_SCM_DIR)/`dirname $${file}`; then \
	    mkdir -p $(DESTDIR)$(SITE_SCM_DIR)/`dirname $${file}`; \
	  fi; \
	  cp $${file} $(DESTDIR)$(SITE_SCM_DIR)/$${file}; \
	done

.PHONY: install-bins
install-bins:
	@mkdir -p $(DESTDIR)$(SITE_SCM_GO_DIR)
	@echo "installing NYACC binaries in $(SITE_SCM_GO_DIR)"
	@for binfile in $(NYACC_BINS); do \
	  if test ! -d $(DESTDIR)$(SITE_SCM_DIR)/`dirname $${binfile}`; then \
	    mkdir -p $(DESTDIR)$(SITE_SCM_GO_DIR)/`dirname $${binfile}`; \
	  fi; \
	  srcfile=`echo $${binfile} | sed -e 's/\.go$$/.scm/'`; \
	  GUILE_LOAD_PATH=$(DESTDIR)$(SITE_SCM_DIR) $(GUILD) compile \
		-o $(DESTDIR)$(SITE_SCM_GO_DIR)/$${binfile} \
		$(DESTDIR)$(SITE_SCM_DIR)/$${srcfile}; \
	  sleep 1; \
	done

.PHONY: install-fh-srcs
install-fh-srcs:
	@mkdir -p $(DESTDIR)$(SITE_SCM_DIR)
	@echo "installing FFI helper sources in $(SITE_SCM_DIR)"
	@for file in $(NYACC_FH_SRCS); do \
	  if test ! -d $(DESTDIR)$(SITE_SCM_DIR)/`dirname $${file}`; then \
	    mkdir -p $(DESTDIR)$(SITE_SCM_DIR)/`dirname $${file}`; \
	  fi; \
	  cp $${file} $(DESTDIR)$(SITE_SCM_DIR)/$${file}; \
	done

.PHONY: install-fh-bins
install-fh-bins:
	@if GUILE_LOAD_PATH=$(SITE_SCM_DIR) GUILE_AUTO_COMPILE=0 \
		$(GUILE) -c '(use-modules (bytestructures guile))' \
			>/dev/null 2>&1; then \
	  echo "installing FFI helper binaries in $(SITE_SCM_GO_DIR)"; \
	  for binfile in $(NYACC_FH_BINS); do \
	    if test ! -d $(DESTDIR)$(SITE_SCM_DIR)/`dirname $${binfile}`; then \
	      mkdir -p $(DESTDIR)$(SITE_SCM_GO_DIR)/`dirname $${binfile}`; \
	    fi; \
	    srcfile=`echo $${binfile} | sed -e 's/\.go$$/.scm/'`; \
	    GUILE_LOAD_PATH=$(DESTDIR)$(SITE_SCM_DIR) $(GUILD) compile \
		-o $(DESTDIR)$(SITE_SCM_GO_DIR)/$${binfile} \
		$(DESTDIR)$(SITE_SCM_DIR)/$${srcfile}; \
	    sleep 1; \
	  done; \
	else \
	  echo "FFI helper binaries not installed: bytestructures needed"; \
	fi


# === files for making source tarballs

nyacc/lang/c99/mach.d/c99act.scm \
nyacc/lang/c99/mach.d/c99tab.scm \
nyacc/lang/c99/mach.d/c99xact.scm \
nyacc/lang/c99/mach.d/c99xtab.scm : nyacc/lang/c99/mach.scm
	GUILE_LOAD_PATH=`pwd` \
	$(GUILE) -l $< -c \
		'((@ (nyacc lang c99 mach) gen-c99-files) "nyacc/lang/c99")'

nyacc/lang/c99/mach.d/cppact.scm \
nyacc/lang/c99/mach.d/cpptab.scm : nyacc/lang/c99/cppmach.scm
	GUILE_LOAD_PATH=`pwd` \
	$(GUILE) -l $< -c \
		'((@ (nyacc lang c99 cppmach) gen-cpp-files) "nyacc/lang/c99")'

.PHONY: dist-files
dist-files:
	(cd nyacc/lang/c99; \
	 GUILE_LOAD_PATH= $(GUILE) --no-auto-compile -L ../../.. \
	    -c '(use-modules (nyacc lang c99 cppmach)) (gen-cpp-files ".")'; \
	 GUILE_LOAD_PATH= $(GUILE) --no-auto-compile -L ../../.. \
	    -c '(use-modules (nyacc lang c99 mach)) (gen-c99-files ".")'; \
	 GUILE_LOAD_PATH= $(GUILE) --no-auto-compile -L ../../.. \
	    -c '(use-modules (nyacc lang c99 cxmach)) (gen-c99cx-files ".")')

# --- last line ---
