##
# MegaZeux Editor Makefile fragment
##

.PHONY: editor_clean editor_target_clean

editor_src = src/editor
editor_obj = src/editor/.build

editor_flags := ${core_flags}

editor_ldflags += ${ZLIB_LDFLAGS}

ifeq (${PLATFORM},mingw)
editor_spec := -DEDITOR_LIBSPEC="__declspec(dllexport)"
endif

${editor_obj}/%.o: ${editor_src}/%.c
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${core_cflags} ${editor_flags} ${editor_spec} -c $< -o $@

editor_objs := \
  ${editor_obj}/block.o         \
  ${editor_obj}/board.o         \
  ${editor_obj}/buffer.o        \
  ${editor_obj}/char_ed.o       \
  ${editor_obj}/configure.o     \
  ${editor_obj}/debug.o         \
  ${editor_obj}/edit.o          \
  ${editor_obj}/edit_di.o       \
  ${editor_obj}/edit_menu.o     \
  ${editor_obj}/fill.o          \
  ${editor_obj}/graphics.o      \
  ${editor_obj}/macro.o         \
  ${editor_obj}/pal_ed.o        \
  ${editor_obj}/param.o         \
  ${editor_obj}/robo_debug.o    \
  ${editor_obj}/robo_ed.o       \
  ${editor_obj}/robot.o         \
  ${editor_obj}/select.o        \
  ${editor_obj}/sfx_edit.o      \
  ${editor_obj}/undo.o          \
  ${editor_obj}/window.o        \
  ${editor_obj}/world.o

# Win32 clipboard handler (takes precedence over SDL 2)
ifeq (${PLATFORM},mingw)
editor_objs += ${editor_obj}/clipboard_win32.o

# Use the SDL2 clipboard handler
else ifeq (${BUILD_LIBSDL2},1)
editor_ldflags += ${SDL_LDFLAGS}
editor_objs += ${editor_obj}/clipboard_sdl2.o

# Mac OS X clipboard handler via Carbon
else ifeq (${PLATFORM},darwin)
editor_ldflags += -framework Cocoa
editor_objs += ${editor_obj}/clipboard_carbon.o

# X11 clipboard handler (requires SDL 1.2)
else ifneq (${X11DIR},)
editor_flags += ${X11_CFLAGS}
editor_ldflags += ${X11_LDFLAGS} ${SDL_LDFLAGS}
editor_objs += ${editor_obj}/clipboard_x11.o

# No clipboard handler
else
editor_objs += ${editor_obj}/clipboard_null.o
endif

-include ${editor_objs:.o=.d}

${editor_objs}: $(filter-out $(wildcard ${editor_obj}), ${editor_obj})

ifeq (${BUILD_MODULAR},1)

editor_target := ${DSOPRE}editor${DSOPOST}

${editor_target}: ${editor_objs} ${core_target}
	$(if ${V},,@echo "  LINK    " $@)
	${CC} ${DSOLDFLAGS} -o $@ ${editor_objs} ${editor_ldflags} \
	  ${LDFLAGS} ${DSOSONAME}$@ ${DSORPATH} $(LINK_DYNAMIC_IF_MIXED) -L. -lcore

editor_target_clean:
	$(if ${V},,@echo "  RM      " ${editor_target} ${editor_target}.debug)
	${RM} ${editor_target} ${editor_target}.debug

else

editor_target := ${editor_objs}

editor_target_clean:

endif

editor_clean: editor_target_clean
	$(if ${V},,@echo "  RM      " ${editor_obj})
	${RM} -r ${editor_obj}
