# Copyright (C) 2016, 2017, 2018, 2019, 2020  Stefan Vargyas
# 
# This file is part of Json-Type.
# 
# Json-Type 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 of the License, or
# (at your option) any later version.
# 
# Json-Type 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 Json-Type.  If not, see <http://www.gnu.org/licenses/>.

.PHONY:: litex

PROGRAM := json.so

GCC_STD := gnu99
CFLAGS := -Wall -Wextra \
          -std=${GCC_STD} -g -I. -I.. \
          -fPIC -fvisibility=hidden \
          -DPROGRAM=${PROGRAM}
LDFLAGS := -fdata-sections \
           -ffunction-sections \
           -Wl,--gc-sections \
           -Wl,--entry=json_main \
           -Wl,--export-dynamic \
           -shared
MKFLAGS := --no-print-directory
LIBS := -ldl

EXCP := debug-impl.c \
        json-litex.c \
        json-litex-module.c \
        json-type-module.c \
        test-gen-expr.c \
        test-filter.c \
        test-common.c \
        test-litex.c \
        test-type.c \
        test-trie.c \
        test-expr.c \
        test-gen.c
SRCS := $(filter-out ${EXCP}, $(wildcard *.c))
OBJS := $(patsubst %.c,%.o, ${SRCS})
BIN  := ${PROGRAM}

JSON_DEBUG = $(shell \
	sed -nr '/^\s*\#\s*define\s+JSON_DEBUG\s*$$/{s//yes/;p;q}' json.h)

ifeq (${JSON_DEBUG},yes)
CFLAGS += -DJSON_DEBUG
endif

include common.mk

# dependency rules

ifeq (.depend-test-filter, $(wildcard .depend-test-filter))
include .depend-test-filter
endif

ifeq (.depend-test-trie, $(wildcard .depend-test-trie))
include .depend-test-trie
endif

ifeq (.depend-test-gen, $(wildcard .depend-test-gen))
include .depend-test-gen
endif

ifeq (.depend-bit-set, $(wildcard .depend-bit-set))
include .depend-bit-set
endif

all:: test-filter.so test-trie test-gen

# main targets

test-trie test-gen bit-set:
ifdef COVERAGE
	@${MAKE} ${MKFLAGS} -f $@.mk COVERAGE=
else
	@${MAKE} ${MKFLAGS} -f $@.mk
endif

test-filter.so:
ifdef COVERAGE
	@${MAKE} ${MKFLAGS} -f test-filter.mk COVERAGE=
else
	@${MAKE} ${MKFLAGS} -f test-filter.mk
endif

litex:
ifdef COVERAGE
	@${MAKE} ${MKFLAGS} -f json-litex.mk COVERAGE=
	@${MAKE} ${MKFLAGS} -f test-expr.mk COVERAGE=
else
	@${MAKE} ${MKFLAGS} -f json-litex.mk
	@${MAKE} ${MKFLAGS} -f test-expr.mk
endif

depend clean allclean::
	@${MAKE} ${MKFLAGS} -f json-litex.mk $@
	@${MAKE} ${MKFLAGS} -f test-filter.mk $@
	@${MAKE} ${MKFLAGS} -f test-gen-expr.mk $@
	@${MAKE} ${MKFLAGS} -f test-expr.mk $@
	@${MAKE} ${MKFLAGS} -f test-trie.mk $@
	@${MAKE} ${MKFLAGS} -f test-gen.mk $@
	@${MAKE} ${MKFLAGS} -f bit-set.mk $@


