# Makefile for MFPLOT.

include ../../Makefile.config

ifeq '$(DARWIN)' '"yes"'
  SHARED_EXT=dylib
else
  SHARED_EXT=so
endif

# MFPLOT subdirectories
DRVDIR     = ./src/drivers
SYSDIR     = ./src/sys
XFT_EC_DIR = ./src/xft_EC

ifeq '$(CFG)' 'debug'

  LIBS_TO_BUILD = ../../lib/libmfplot.a

else # 'release'

  LIBS_TO_BUILD = ../../lib/libmfplot.$(SHARED_EXT)

endif

#
# C compiler and compilation flags
#
XINCL = -I$(X11_INCL)
XFT_INC = -I$(XFT_EC_DIR) -I$(FREETYPE2_INCL)

#
# Loader library-flags
#
LIBS = -L$(X11_DIR) -lX11

#
# Routine lists.
#
MODULES = obj/mod_grplot.o obj/mod_pgplot.o

DRIVERS = obj/null_driver.o \
          obj/eps_driver.o obj/pdf_driver.o obj/post_pdfdriv.o
ifeq '$(NO_X11)' '"no"'
  DRIVERS += obj/x11_driver.o \
             obj/xftcolor.o obj/xftdpy.o obj/xftfont.o obj/xftinit.o \
             obj/xftrender.o obj/xftxlfd.o obj/xftcore.o obj/xftdraw.o \
             obj/xftfreetype.o obj/xftlist.o obj/xftstr.o obj/xftdbg.o \
             obj/xftextent.o obj/xftglyphs.o obj/xftname.o obj/xftswap.o
endif

SYSTEM_ROUTINES = obj/grisnan.o obj/grdate.o obj/gruser.o \
                  obj/gr_real_user.o obj/get_terminal_x11_ID.o \
                  obj/get_mod_keys.o
SYSTEM_ROUTINES += obj/deflate_stream_to_hex.o \
                   obj/deflate_stream_to_a85.o

CALL_FROM_C_ROUTINES =
ifeq '$(NO_X11)' '"no"'
  CALL_FROM_C_ROUTINES += obj/call_routines_from_x11_driver.o
endif

all: $(LIBS_TO_BUILD)

#--------------------------- dependencies ------------------------------

obj/mod_grplot.o : src/gr/grarea.f90 \
                   src/gr/grbpic.f90 \
                   src/gr/grclos.f90 \
                   src/gr/grcurs.f90 \
                   src/gr/gr_dev_to_wld.f90 \
                   src/gr/grdtyp.f90 \
                   src/gr/grepic.f90 \
                   src/gr/gresc.f90 \
                   src/gr/grexec.F90 \
                   src/gr/grfa.f90 \
                   src/gr/grimg0.f90 \
                   src/gr/grimg1.f90 \
                   src/gr/grimg2.f90 \
                   src/gr/grinit.f90 \
                   src/gr/gritoc.f90 \
                   src/gr/grlin0.f90 \
                   src/gr/grlin2.f90 \
                   src/gr/grlin3.f90 \
                   src/gr/grlina.f90 \
                   src/gr/grlinr.f90 \
                   src/gr/grmker.f90 \
                   src/gr/grmova.f90 \
                   src/gr/grmovr.f90 \
                   src/gr/grmsg.f90 \
                   src/gr/gropen.f90 \
                   src/gr/grpage.f90 \
                   src/gr/grpars.f90 \
                   src/gr/grpocl.f90 \
                   src/gr/grqcap.f90 \
                   src/gr/grqci.f90 \
                   src/gr/grqcol.f90 \
                   src/gr/grqcr.f90 \
                   src/gr/grqdev.f90 \
                   src/gr/grqdt.f90 \
                   src/gr/grqls.f90 \
                   src/gr/grqlw.f90 \
                   src/gr/grqpos.f90 \
                   src/gr/grqtyp.f90 \
                   src/gr/grrec0.f90 \
                   src/gr/grrect.f90 \
                   src/gr/grsci.f90 \
                   src/gr/grscr.f90 \
                   src/gr/grsize.f90 \
                   src/gr/grslct.f90 \
                   src/gr/grsls.f90 \
                   src/gr/grslw.f90 \
                   src/gr/grsms.f90 \
                   src/gr/grterm.f90 \
                   src/gr/grtoup.f90 \
                   src/gr/gr_wld_to_dev.f90 \
                   src/gr/grvct0.f90 \
                   src/gr/grwarn.f90 \
                   src/gr/grxhls.f90 \
                   src/gr/grxrgb.f90 \
                   src/sys/grflun.f90 \
                   src/sys/grgenv.f90 \
                   src/sys/grglun.f90 \
                   src/zoom/grclick_ec.f90 \
                   src/zoom/grcurs_ec.f90 \
                   src/zoom/grnormcurs_ec.f90 \
                   src/pgaux/change_viewport.f90 \
                   src/pgaux/embed_EnglishBT_PFA.f90 \
                   src/pgaux/grclos_ec.f90 \
                   src/pgaux/grcurs_dyn.f90 \
                   src/pgaux/gropen_ec.f90 \
                   src/pgaux/grpocl_ec.f90 \
                   src/pgaux/gr_read_AFM_files.f90 \
                   src/pgaux/gr_set_cursor_shape.f90 \
                   src/pgaux/ps_font_char_width.f90 \
                   src/pgaux/raise_mfplot_win.f90 \
                   src/pgaux/restore_viewport.f90 \
                   src/pgaux/set_focus_mfplot_win.f90 \
                   src/pgaux/set_muesli_install_dir.F90 \
                   src/pgaux/set_mfplot_background_color.f90

obj/mod_pgplot.o :  src/pg/pgarro.f90 \
                    src/pg/pgband.f90 \
                    src/pg/pgbbuf.f90 \
                    src/pg/pgbin.f90 \
                    src/pg/pgbox1.f90 \
                    src/pg/pgbox.f90 \
                    src/pg/pgcirc.f90 \
                    src/pg/pgcl.f90 \
                    src/pg/pgclos.f90 \
                    src/pg/pgcn01.f90 \
                    src/pg/pgcnsc.f90 \
                    src/pg/pgconb.f90 \
                    src/pg/pgconf.f90 \
                    src/pg/pgconl.f90 \
                    src/pg/pgconx.f90 \
                    src/pg/pgctab.f90 \
                    src/pg/pgebuf.f90 \
                    src/pg/pgenv.f90 \
                    src/pg/pgerr1.f90 \
                    src/pg/pgerrb.f90 \
                    src/pg/pgerrx.f90 \
                    src/pg/pgerry.f90 \
                    src/pg/pghtch.f90 \
                    src/pg/pgimag.f90 \
                    src/pg/pginit.f90 \
                    src/pg/pglab.f90 \
                    src/pg/pglen.f90 \
                    src/pg/pgline.f90 \
                    src/pg/pgline2.f90 \
                    src/pg/pgmtxt.f90 \
                    src/pg/pgnpl.f90 \
                    src/pg/pgnumb.f90 \
                    src/pg/pgolin.f90 \
                    src/pg/pgopen.f90 \
                    src/pg/pgpage.f90 \
                    src/pg/pgpoly.f90 \
                    src/pg/pgpt.f90 \
                    src/pg/pgptxt.f90 \
                    src/pg/pgqah.f90 \
                    src/pg/pgqch.f90 \
                    src/pg/pgqcir.f90 \
                    src/pg/pgqcs.f90 \
                    src/pg/pgqfs.f90 \
                    src/pg/pgqhs.f90 \
                    src/pg/pgqinf.f90 \
                    src/pg/pgqitf.f90 \
                    src/pg/pgqtbg.f90 \
                    src/pg/pgqtxt.f90 \
                    src/pg/pgqvp.f90 \
                    src/pg/pgqvsz.f90 \
                    src/pg/pgqwin.f90 \
                    src/pg/pgrnd.f90 \
                    src/pg/pgrnge.f90 \
                    src/pg/pgsah.f90 \
                    src/pg/pgsch.f90 \
                    src/pg/pgscir.f90 \
                    src/pg/pgscrn.f90 \
                    src/pg/pgsfs.f90 \
                    src/pg/pgshls.f90 \
                    src/pg/pgshs.f90 \
                    src/pg/pgsitf.f90 \
                    src/pg/pgslct.f90 \
                    src/pg/pgstbg.f90 \
                    src/pg/pgsvp.f90 \
                    src/pg/pgswin.f90 \
                    src/pg/pgtbox.f90 \
                    src/pg/pgtbx1.f90 \
                    src/pg/pgtbx2.f90 \
                    src/pg/pgtbx3.f90 \
                    src/pg/pgtbx4.f90 \
                    src/pg/pgtbx5.f90 \
                    src/pg/pgtbx6.f90 \
                    src/pg/pgtbx7.f90 \
                    src/pg/pgtikl.f90 \
                    src/pg/pgvect.f90 \
                    src/pg/pgvsiz.f90 \
                    src/pg/pgvw.f90 \
                    src/pg/pgwedg.f90 \
                    src/pg/pgwnad.f90 \
                    src/zoom/click_ec.f90 \
                    src/zoom/cursdev_ec.f90 \
                    src/pgaux/grtext_xft.f90 \
                    src/pgaux/grtext_ps_font.f90 \
                    src/pgaux/grparse_ps_font.f90 \
                    src/pgaux/grlen_ps_font.f90 \
                    src/pgaux/grlen_xft.f90 \
                    src/pgaux/grparse_pdf_font.f90 \
                    src/pgaux/grtext_pdf_font.f90 \
                    src/pgaux/pgarro_head_only.f90 \
                    src/pgaux/pgband_dyn.f90 \
                    src/pgaux/pgbar_ec.f90 \
                    src/pgaux/pgbar_groups_ec.f90 \
                    src/pgaux/pgbar_stacks_ec.f90 \
                    src/pgaux/pgbezier.f90 \
                    src/pgaux/pgclos_ec.f90 \
                    src/pgaux/pgcont_ec.f90 \
                    src/pgaux/pgcnsc_ec.f90 \
                    src/pgaux/pgcn01_ec.f90 \
                    src/pgaux/pgcontxy_ec.f90 \
                    src/pgaux/pgcnscxy_ec.f90 \
                    src/pgaux/pgcn01xy_ec.f90 \
                    src/pgaux/pggrid_ec.f90 \
                    src/pgaux/pghist_ec.f90 \
                    src/pgaux/pghist_ec_pre.f90 \
                    src/pgaux/pginput.f90 \
                    src/pgaux/pgline_errorbar.f90 \
                    src/pgaux/pgopen_ec.f90 \
                    src/pgaux/pgpt_errorbar.f90 \
                    src/pgaux/pgscir_PS_ec.f90 \
                    src/pgaux/pgslices.f90 \
                    src/pgaux/pg_spline_to_bezier.f90 \
                    src/pgaux/pg_store_contour.F90 \
                    src/pgaux/pgtricons_ec.f90 \
                    src/pgaux/pgtricnsc_ec.f90 \
                    src/pgaux/pre_pgbox_log.f90 \
                    src/pgaux/utf8_to_latin_1.f90

obj/x11_driver.o : \
        $(DRVDIR)/xwdriv_include/xw_add_events.inc \
        $(DRVDIR)/xwdriv_include/xw_animate_dashed_contour.inc \
        $(DRVDIR)/xwdriv_include/xw_animate_dashed_multispot.inc \
        $(DRVDIR)/xwdriv_include/xw_bad_device.inc \
        $(DRVDIR)/xwdriv_include/xw_bbox_of_pixmap.inc \
        $(DRVDIR)/xwdriv_include/xw_bitmap_from_pixmap.inc \
        $(DRVDIR)/xwdriv_include/xw_bound_cursor.inc \
        $(DRVDIR)/xwdriv_include/xw_checks_pixmap_aux.inc \
        $(DRVDIR)/xwdriv_include/xw_check_window_event.inc \
        $(DRVDIR)/xwdriv_include/xw_clear.inc \
        $(DRVDIR)/xwdriv_include/xw_clear_pixmap_aux.inc \
        $(DRVDIR)/xwdriv_include/xw_clip_at_viewport.inc \
        $(DRVDIR)/xwdriv_include/xw_current_pos.inc \
        $(DRVDIR)/xwdriv_include/xw_cursor_line.inc \
        $(DRVDIR)/xwdriv_include/xw_del_Band.inc \
        $(DRVDIR)/xwdriv_include/xw_del_XWdev.inc \
        $(DRVDIR)/xwdriv_include/xw_direct_draw_string.inc \
        $(DRVDIR)/xwdriv_include/xw_draw_arrow.inc \
        $(DRVDIR)/xwdriv_include/xw_draw_cursor.inc \
        $(DRVDIR)/xwdriv_include/xw_draw_marker.inc \
        $(DRVDIR)/xwdriv_include/xw_draw_string.inc \
        $(DRVDIR)/xwdriv_include/xw_end_cursor.inc \
        $(DRVDIR)/xwdriv_include/xw_erase_cursor.inc \
        $(DRVDIR)/xwdriv_include/xw_erase_labelling_area.inc \
        $(DRVDIR)/xwdriv_include/xw_erase_output_area.inc \
        $(DRVDIR)/xwdriv_include/xw_error.inc \
        $(DRVDIR)/xwdriv_include/xw_expose.inc \
        $(DRVDIR)/xwdriv_include/xw_find_exe.inc \
        $(DRVDIR)/xwdriv_include/xw_flush.inc \
        $(DRVDIR)/xwdriv_include/xw_get_click.inc \
        $(DRVDIR)/xwdriv_include/xw_get_image.inc \
        $(DRVDIR)/xwdriv_include/xw_get_image_line_RGB.inc \
        $(DRVDIR)/xwdriv_include/xw_get_pixmap.inc \
        $(DRVDIR)/xwdriv_include/xw_get_string_bbox.inc \
        $(DRVDIR)/xwdriv_include/xw_get_visual.inc \
        $(DRVDIR)/xwdriv_include/xw_get_window.inc \
        $(DRVDIR)/xwdriv_include/xw_handle_error.inc \
        $(DRVDIR)/xwdriv_include/xw_image_line.inc \
        $(DRVDIR)/xwdriv_include/xw_image_line_RGB.inc \
        $(DRVDIR)/xwdriv_include/xw_init_colors.inc \
        $(DRVDIR)/xwdriv_include/xw_insert_device.inc \
        $(DRVDIR)/xwdriv_include/xw_is_inside_quad.inc \
        $(DRVDIR)/xwdriv_include/xw_limit_pcoords.inc \
        $(DRVDIR)/xwdriv_include/xw_locate_cursor.inc \
        $(DRVDIR)/xwdriv_include/xw_mark_modified.inc \
        $(DRVDIR)/xwdriv_include/xw_move_arrow.inc \
        $(DRVDIR)/xwdriv_include/xw_move_arrow_in_pixmap.inc \
        $(DRVDIR)/xwdriv_include/xw_move_grobj.inc \
        $(DRVDIR)/xwdriv_include/xw_move_rect_in_pixmap.inc \
        $(DRVDIR)/xwdriv_include/xw_new_Band.inc \
        $(DRVDIR)/xwdriv_include/xw_new_cursors.inc \
        $(DRVDIR)/xwdriv_include/xw_new_geom.inc \
        $(DRVDIR)/xwdriv_include/xw_new_XWdev.inc \
        $(DRVDIR)/xwdriv_include/xw_next_event.inc \
        $(DRVDIR)/xwdriv_include/xw_next_page.inc \
        $(DRVDIR)/xwdriv_include/xw_nint.inc \
        $(DRVDIR)/xwdriv_include/xw_ok.inc \
        $(DRVDIR)/xwdriv_include/xw_raise_win.inc \
        $(DRVDIR)/xwdriv_include/xw_read_cursor_dyn.inc \
        $(DRVDIR)/xwdriv_include/xw_read_cursor_dyn_multispot.inc \
        $(DRVDIR)/xwdriv_include/xw_read_cursor_dyn_quad.inc \
        $(DRVDIR)/xwdriv_include/xw_read_cursor.inc \
        $(DRVDIR)/xwdriv_include/xw_read_cursor_no_wait.inc \
        $(DRVDIR)/xwdriv_include/xw_rem_events.inc \
        $(DRVDIR)/xwdriv_include/xw_remove_device.inc \
        $(DRVDIR)/xwdriv_include/xw_rgb_to_xcolor.inc \
        $(DRVDIR)/xwdriv_include/xw_scroll_rect.inc \
        $(DRVDIR)/xwdriv_include/xw_select_device.inc \
        $(DRVDIR)/xwdriv_include/xw_set_ci.inc \
        $(DRVDIR)/xwdriv_include/xw_set_cursor.inc \
        $(DRVDIR)/xwdriv_include/xw_set_focus_win.inc \
        $(DRVDIR)/xwdriv_include/xw_set_fontconfig_path.inc \
        $(DRVDIR)/xwdriv_include/xw_set_rgb.inc \
        $(DRVDIR)/xwdriv_include/xw_shift_cursor.inc \
        $(DRVDIR)/xwdriv_include/xw_show_pixmap.inc \
        $(DRVDIR)/xwdriv_include/xw_sync_error.inc \
        $(DRVDIR)/xwdriv_include/xw_update_colors.inc \
        $(DRVDIR)/xwdriv_include/xw_visual_info.inc \
        $(DRVDIR)/xwdriv_include/xw_win_resize.inc \
        $(DRVDIR)/xwdriv_include/xw_xcolor_to_rgb.inc \
        $(DRVDIR)/xwdriv_include/xw_XPoint_to_xy.inc \
        $(DRVDIR)/xwdriv_include/xw_xy_to_XPoint.inc

#------------------------------- rules ---------------------------------

# in case of parallel build (make -j N), we have to add this rules:
../../obj/mod_mfarray.o :
	cd ../..; $(MAKE) obj/mod_mfarray.o

.SUFFIXES :
.SUFFIXES : .c .o

obj/mod_grplot.o : mod_grplot.F90 \
                   inc_dev/mod_mfarray.mod \
                   inc_dev/mod_mfdebug.mod
ifeq '$(F90CACHE)' '"yes"'
	$(F90C) -c $(FPP_FLAGS) $(F90_FLAGS) -Iinc_dev $< -o $@ $(MOD_TO)inc_dev \
     $(patsubst %,--f90cache-depmod %,$(filter %.mod, $^))
else
	$(F90C) -c $(FPP_FLAGS) $(F90_FLAGS) -Iinc_dev $< -o $@ $(MOD_TO)inc_dev
endif

obj/mod_pgplot.o : mod_pgplot.F90 \
                   inc_dev/mod_grplot.mod
ifeq '$(F90CACHE)' '"yes"'
	$(F90C) -c $(FPP_FLAGS) $(F90_FLAGS) -Iinc_dev $< -o $@ $(MOD_TO)inc_dev \
     $(patsubst %,--f90cache-depmod %,$(filter %.mod, $^))
else
	$(F90C) -c $(FPP_FLAGS) $(F90_FLAGS) -Iinc_dev $< -o $@ $(MOD_TO)inc_dev
endif

obj/call_routines_from_x11_driver.o : call_routines_from_x11_driver.f90 \
                                      inc_dev/mod_pgplot.mod
ifeq '$(F90CACHE)' '"yes"'
	$(F90C) -c $(F90_FLAGS) -Iinc_dev $< -o $@ \
     $(patsubst %,--f90cache-depmod %,$(filter %.mod, $^))
else
	$(F90C) -c $(F90_FLAGS) -Iinc_dev $< -o $@
endif

obj/null_driver.o : $(DRVDIR)/null_driver.f90 \
                    inc_dev/mod_pgplot.mod
ifeq '$(F90CACHE)' '"yes"'
	$(F90C) -c $(F90_FLAGS) -Iinc_dev $< -o $@ \
     $(patsubst %,--f90cache-depmod %,$(filter %.mod, $^))
else
	$(F90C) -c $(F90_FLAGS) -Iinc_dev $< -o $@
endif

obj/eps_driver.o : $(DRVDIR)/eps_driver.F90 \
                   inc_dev/mod_mfarray.mod \
                   inc_dev/mod_pgplot.mod
ifeq '$(F90CACHE)' '"yes"'
	$(F90C) -c $(FPP_FLAGS) $(F90_FLAGS) -Iinc_dev $< -o $@ \
     $(patsubst %,--f90cache-depmod %,$(filter %.mod, $^))
else
	$(F90C) -c $(FPP_FLAGS) $(F90_FLAGS) -Iinc_dev $< -o $@
endif

obj/pdf_driver.o : $(DRVDIR)/pdf_driver.F90 \
                   inc_dev/mod_mfarray.mod \
                   inc_dev/mod_pgplot.mod
ifeq '$(F90CACHE)' '"yes"'
	$(F90C) -c $(FPP_FLAGS) $(F90_FLAGS) -Iinc_dev $< -o $@ \
     $(patsubst %,--f90cache-depmod %,$(filter %.mod, $^))
else
	$(F90C) -c $(FPP_FLAGS) $(F90_FLAGS) -Iinc_dev $< -o $@
endif

obj/post_pdfdriv.o : $(DRVDIR)/post_pdfdriv.f90
	$(F90C) -c $(F90_FLAGS) $< -o $@

# no optimization for grisnan
obj/grisnan.o : $(SYSDIR)/grisnan.f90 \
                inc_dev/mod_mfaux.mod
ifeq '$(F90CACHE)' '"yes"'
	$(F90C) -c -O0 $(PIC_FLAGS) -Iinc_dev $< -o $@ \
     $(patsubst %,--f90cache-depmod %,$(filter %.mod, $^))
else
	$(F90C) -c -O0 $(PIC_FLAGS) -Iinc_dev $< -o $@
endif

obj/xft%.o : $(XFT_EC_DIR)/xft%.c
	$(CC) -c $(CC_FLAGS) $(WRAPPER_FLAGS) $(XINCL) $(XFT_INC) $< -o $@

obj/%.o : $(SYSDIR)/%.c
	$(CC) $(CC_FLAGS) $(WRAPPER_FLAGS) -c $< -o $@

obj/deflate_stream_to_hex.o : $(SYSDIR)/deflate_stream_to_hex.c
	$(CC) -c -I$(ZLIB_INCL) $(CC_FLAGS) $(WRAPPER_FLAGS) $< -o $@

obj/deflate_stream_to_a85.o : $(SYSDIR)/deflate_stream_to_a85.c
	$(CC) -c -I$(ZLIB_INCL) $(CC_FLAGS) $(WRAPPER_FLAGS) $< -o $@

obj/x11_driver.o : $(DRVDIR)/x11_driver.c \
                   $(DRVDIR)/x11_driver.h
	$(CC) -D_INTEL_IFC -c $(CC_FLAGS) $(WRAPPER_FLAGS) $(XINCL) $(XFT_INC) $< -o $@

../../lib/libmfplot.a : $(MODULES) $(DRIVERS) $(SYSTEM_ROUTINES) \
                        $(CALL_FROM_C_ROUTINES)
	ar cr $@ $^

INSTALL_DIR := $(INSTALL_DIR)/muesli

Z_LIB= -L$(ZLIB_DIR) -lz

ifeq '$(NO_X11)' '"no"'
  X11_LIB= -L$(X11_DIR) -lfreetype -lfontconfig -lXrender -lX11
else
  X11_LIB=
endif
ifeq '$(DARWIN)' '"no"'
../../lib/libmfplot.$(SHARED_EXT) : $(MODULES) $(DRIVERS) $(SYSTEM_ROUTINES) \
                                    $(CALL_FROM_C_ROUTINES)
	$(C_COMP) -shared -o $@ $^ $(Z_LIB) $(X11_LIB) -lifcore
else
../../lib/libmfplot.$(SHARED_EXT) : $(MODULES) $(DRIVERS) $(SYSTEM_ROUTINES) \
                                    $(CALL_FROM_C_ROUTINES)
	$(C_COMP) -install_name "$(INSTALL_DIR)/libmfplot.$(SHARED_EXT)" \
	      -dynamiclib -o $@ $^ ../../lib/libfml.$(SHARED_EXT) \
              $(Z_LIB) $(X11_LIB) -lgfortran
endif

# special rule to avoid recompiling (recursive-make)
inc_dev/%.mod : obj/%.o
	@if [ ! -f $@ ]; then \
      rm $< ; \
      $(MAKE) $< ; \
    fi

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

clean :
	rm -f obj/* gmon.out

distclean : clean
	rm -f fontconfig/fonts.conf
