#-*- Makefile -*-
#
# ${R_HOME}/src/library/grDevices/src/Makefile.win

pkg = grDevices

srcdir = .
top_srcdir = ../../../..

top_builddir = ../../../..
subdir = src/library/$(pkg)/src
R_HOME = $(top_builddir)

SOURCES_C = axis_scales.c chull.c devices.c init.c stubs.c colors.c \
   clippath.c patterns.c mask.c group.c \
   devPicTeX.c devPS.c devWindows.c winbitmap.c
SOURCES_F =
DEPENDS = $(SOURCES_C:.c=.d)
OBJECTS = $(SOURCES_C:.c=.o) $(SOURCES_F:.f=.o) ../../../gnuwin32/dllversion.o

SHLIB = $(pkg).dll
RESFLAGS = -I $(R_HOME)/include

## need config.h
PKG_CPPFLAGS = -I$(R_HOME)/src/include -DHAVE_CONFIG_H
## devPS needs zlib.h and run.h
devPS-CPPFLAGS = -I"$(EXT_LIBS)/include" -I$(R_HOME)/src/gnuwin32

## devWindows needs opt.h, console.h, rui.h and graphapp headers
## remove slash to avoid Msys2 path conversion
arch_DEFS = -DR_ARCH='"$(subst /,,$(R_ARCH))"'
devWindows-CPPFLAGS = -I$(R_HOME)/src/gnuwin32 -I$(R_HOME)/src/extra $(arch_DEFS)

winbitmap-CPPFLAGS = -DHAVE_PNG -DHAVE_JPEG -DHAVE_TIFF -I"$(EXT_LIBS)/include"

PKG_LIBS = -lRgraphapp

ifndef USE_PKG_CONFIG
  ## webp has libsharpyuv.a as a separate library, but we support also older
  ## versions which included the code in libwebp.a
  LIBSHARPYUV = $(or $(and $(wildcard $(EXT_LIBS)/lib/libsharpyuv.a),-lsharpyuv),)

  ## support tiff built with lerc and without
  LIBLERC = $(or $(and $(wildcard $(EXT_LIBS)/lib/libLerc.a),-lLerc -lstdc++),)

  ## support tiff built with deflate and without
  LIBDEFLATE = $(or $(and $(wildcard $(EXT_LIBS)/lib/libdeflate.a),-ldeflate),)

  PKG_LIBS += -L"$(EXT_LIBS)"/lib$(R_ARCH) -lpng \
             -ltiff $(LIBLERC) $(LIBDEFLATE) \
             -ljpeg -lzstd -lz -lwebp $(LIBSHARPYUV) -llzma
else
  PKG_LIBS += $(shell $(PKG_CONFIG) --libs libpng libjpeg libtiff-4 zlib)
  winbitmap-CPPFLAGS += $(shell $(PKG_CONFIG) --cflags libpng libjpeg libtiff-4)
  devPS-CPPFLAGS += $(shell $(PKG_CONFIG) --cflags zlib)
endif

all: makeMakedeps shlib

shlib: $(SHLIB)
	@mkdir -p $(top_builddir)/library/$(pkg)/libs$(R_ARCH)
	@cp $(SHLIB) $(top_builddir)/library/$(pkg)/libs$(R_ARCH)

include $(R_HOME)/etc${R_ARCH}/Makeconf
include $(top_srcdir)/share/make/winshlib.mk

mostlyclean distclean: clean
clean:
	@-rm -f Makedeps *.d *.o $(SHLIB)


makeMakedeps: $(DEPENDS)
	@$(RM) Makedeps
	-@cat $(DEPENDS) > Makedeps


-include Makedeps
