#   Copyright 2011, 2012, 2013 David Malcolm <dmalcolm@redhat.com>
#   Copyright 2011, 2012, 2013 Red Hat, Inc.
#
#   This 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.
#
#   This program 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 this program.  If not, see
#   <http://www.gnu.org/licenses/>.

.PHONY: all clean check-api

HANDWRITTEN_C_FILES = \
  gcc-callgraph.c \
  gcc-cfg.c \
  gcc-constant.c \
  gcc-declaration.c \
  gcc-diagnostics.c \
  gcc-function.c \
  gcc-gimple.c \
  gcc-location.c \
  gcc-option.c \
  gcc-rtl.c \
  gcc-tree.c \
  gcc-type.c \
  gcc-variable.c

GENERATED_C_FILES = \
  autogenerated-casts.c

OBJECT_FILES_FROM_HANDWRITTEN_C_FILES:= $(patsubst %.c,%.o,$(HANDWRITTEN_C_FILES))
OBJECT_FILES_FROM_GENERATED_C_FILES:= $(patsubst %.c,%.o,$(GENERATED_C_FILES))

OBJECT_FILES:= $(OBJECT_FILES_FROM_HANDWRITTEN_C_FILES) $(OBJECT_FILES_FROM_GENERATED_C_FILES)

SOURCE_XML:= \
  callgraph.xml \
  cfg.xml \
  constant.xml \
  declaration.xml \
  function.xml \
  gimple.xml \
  location.xml \
  option.xml \
  rtl.xml \
  tree.xml \
  type.xml \
  variable.xml

GENERATED_HEADERS:= \
  gcc-public-types.h \
  gcc-semiprivate-types.h \
  gcc-callgraph.h \
  gcc-cfg.h \
  gcc-constant.h \
  gcc-declaration.h \
  gcc-function.h \
  gcc-gimple.h \
  gcc-location.h \
  gcc-option.h \
  gcc-rtl.h \
  gcc-tree.h \
  gcc-type.h \
  gcc-variable.h

GCCPLUGINS_DIR:= $(shell $(CC) --print-file-name=plugin)

LIBGCC_C_API_SO	:= libgcc-c-api.so

CPPFLAGS+= -I$(GCCPLUGINS_DIR)/include -I$(GCCPLUGINS_DIR)/include/c-family -I.
# Allow user to pick optimization, choose whether warnings are fatal,
# and choose debugging information level.
CFLAGS?=-O2 -Werror -g
# Force these settings
CFLAGS+= -fPIC -fno-strict-aliasing -Wall

all: $(LIBGCC_C_API_SO)

$(LIBGCC_C_API_SO): $(OBJECT_FILES)
	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared $^ -o $@ $(LIBS)

# This is the standard .c->.o recipe, but it needs to be stated
# explicitly to support the case that $(srcdir) is not blank.
$(OBJECT_FILES): %.o: $(srcdir)%.c $(GENERATED_HEADERS) ../autogenerated-EXTRA_CFLAGS.txt
	$(COMPILE.c) $(shell cat ../autogenerated-EXTRA_CFLAGS.txt) $(OUTPUT_OPTION) $<

autogenerated-casts.c: $(SOURCE_XML) xmltypes.py generate-casts-c.py
	$(PYTHON) generate-casts-c.py $@

clean:
	$(RM) *.so *.o $(GENERATED_HEADERS)

install: $(LIBGCC_C_API_SO)
	mkdir -p $(DESTDIR)$(GCCPLUGINS_DIR)
	cp libgcc-c-api.so $(DESTDIR)$(GCCPLUGINS_DIR)

# The python interpreter to use:
PYTHON=python

$(GENERATED_HEADERS): $(SOURCE_XML) xml-to-h.py xmltypes.py
	$(PYTHON) xml-to-h.py

check-api:
	xmllint --noout --relaxng api.rng *.xml
