# MPlayer Makefile
#
# copyright (c) 2008 Diego Biurrun
# Rewritten entirely from a set of Makefiles written by Arpi and many others.
#
# This file is part of MPlayer.
#
# MPlayer 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 2 of the License, or
# (at your option) any later version.
#
# MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

include old_build/config.mak

###### variable declarations #######

SOURCES_AUDIO_INPUT-$(ALSA)     += stream/ai_alsa1x.c
SOURCES_AUDIO_INPUT-$(OSS_AUDIO)+= stream/ai_oss.c
SOURCES_AUDIO_INPUT-$(SNDIO)    += stream/ai_sndio.c
SOURCES-$(AUDIO_INPUT)          += $(SOURCES_AUDIO_INPUT-yes)
SOURCES-$(CDDA)                 += stream/stream_cdda.c
SOURCES-$(DVBIN)                += stream/dvb_tune.c \
                                   stream/stream_dvb.c
SOURCES-$(DVDREAD)              += stream/stream_dvd.c \
                                   stream/stream_dvd_common.c
SOURCES-$(DVDNAV)               += stream/stream_dvdnav.c \
                                   stream/stream_dvd_common.c

SOURCES-$(RUBBERBAND)           += audio/filter/af_rubberband.c
SOURCES-$(LIBASS)               += sub/ass_mp.c sub/sd_ass.c \
                                   demux/demux_libass.c

SOURCES-$(LIBBLURAY)            += stream/stream_bluray.c

SOURCES-$(LIBSMBCLIENT)         += stream/stream_smb.c

SOURCES-$(PVR)                  += stream/stream_pvr.c

SOURCES-$(TV)                   += stream/stream_tv.c stream/tv.c \
                                   stream/frequencies.c stream/tvi_dummy.c \
                                   demux/demux_tv.c

SOURCES-$(TV_V4L2)              += stream/tvi_v4l2.c stream/audio_in.c
SOURCES-$(DUMMY_OSD)            += sub/osd_dummy.c
SOURCES-$(LIBASS_OSD)           += sub/osd_libass.c

SOURCES-$(ALSA)                 += audio/out/ao_alsa.c
SOURCES-$(CACA)                 += video/out/vo_caca.c
SOURCES-$(SDL2)                 += audio/out/ao_sdl.c video/out/vo_sdl.c
SOURCES-$(GL)                   += video/out/gl_common.c video/out/gl_osd.c \
                                   video/out/vo_opengl.c video/out/gl_lcms.c \
                                   video/out/gl_video.c video/out/dither.c \
                                   video/out/gl_hwdec.c video/out/gl_utils.c \
                                   video/out/vo_opengl_cb.c

SOURCES-$(ENCODING)             += video/out/vo_lavc.c audio/out/ao_lavc.c \
                                   common/encode_lavc.c

SOURCES-$(GL_X11)               += video/out/x11_common.c video/out/gl_x11.c
SOURCES-$(EGL_X11)              += video/out/x11_common.c video/out/gl_x11egl.c
SOURCES-$(GL_WAYLAND)           += video/out/wayland_common.c \
                                   video/out/gl_wayland.c

SOURCES-$(JACK)                 += audio/out/ao_jack.c
SOURCES-$(OPENAL)               += audio/out/ao_openal.c
SOURCES-$(OSS_AUDIO)            += audio/out/ao_oss.c
SOURCES-$(PULSE)                += audio/out/ao_pulse.c
SOURCES-$(RSOUND)               += audio/out/ao_rsound.c
SOURCES-$(SNDIO)                += audio/out/ao_sndio.c
SOURCES-$(VDPAU)                += video/vdpau.c video/vdpau_mixer.c \
                                   video/out/vo_vdpau.c video/decode/vdpau.c \
                                   video/filter/vf_vdpaupp.c \
                                   video/filter/vf_vdpaurb.c
SOURCES-$(VDPAU_GL_X11)         += video/out/gl_hwdec_vdpau.c
SOURCES-$(VAAPI)                += video/out/vo_vaapi.c \
                                   video/decode/vaapi.c \
                                   video/vaapi.c
SOURCES-$(VAAPI_VPP)            += video/filter/vf_vavpp.c
SOURCES-$(VAAPI_GLX)            += video/out/gl_hwdec_vaglx.c

SOURCES-$(XV)                   += video/out/x11_common.c video/out/vo_xv.c
SOURCES-$(WAYLAND)              += video/out/vo_wayland.c \
                                   video/out/wayland_common.c \
                                   video/out/wayland/buffer.c \
                                   video/out/wayland/memfile.c

SOURCES-$(LIBAVFILTER)          += video/filter/vf_lavfi.c \
                                   video/filter/vf_delogo.c \
                                   video/filter/vf_gradfun.c \
                                   video/filter/vf_hqdn3d.c \
                                   video/filter/vf_noise.c \
                                   video/filter/vf_pullup.c \
                                   video/filter/vf_rotate.c \
                                   video/filter/vf_unsharp.c \
                                   video/filter/vf_yadif.c \
                                   audio/filter/af_lavfi.c

SOURCES-$(LUA)                  += player/lua.c
SOURCES-$(VAPOURSYNTH_CORE)     += video/filter/vf_vapoursynth.c
SOURCES-$(LIBARCHIVE)           += demux/demux_libarchive.c \
                                   stream/stream_libarchive.c
SOURCES-$(DLOPEN)               += video/filter/vf_dlopen.c

SOURCES = audio/audio.c \
          audio/audio_buffer.c \
          audio/chmap.c \
          audio/chmap_sel.c \
          audio/fmt-conversion.c \
          audio/format.c \
          audio/mixer.c \
          audio/decode/ad_lavc.c \
          audio/decode/ad_spdif.c      \
          audio/decode/dec_audio.c \
          audio/filter/af.c \
          audio/filter/af_channels.c \
          audio/filter/af_delay.c \
          audio/filter/af_equalizer.c \
          audio/filter/af_format.c \
          audio/filter/af_lavcac3enc.c \
          audio/filter/af_lavrresample.c \
          audio/filter/af_pan.c \
          audio/filter/af_scaletempo.c \
          audio/filter/af_drc.c \
          audio/filter/af_volume.c \
          audio/filter/tools.c \
          audio/out/ao.c \
          audio/out/ao_null.c \
          audio/out/ao_pcm.c \
          audio/out/pull.c \
          audio/out/push.c \
          common/av_common.c \
          common/av_log.c \
          common/codecs.c \
          common/common.c \
          common/msg.c \
          common/playlist.c \
          common/tags.c \
          common/version.c \
          demux/codec_tags.c \
          demux/cue.c \
          demux/demux.c \
          demux/demux_edl.c \
          demux/demux_cue.c \
          demux/demux_disc.c \
          demux/demux_lavf.c \
          demux/demux_mf.c \
          demux/demux_mkv.c \
          demux/demux_mkv_timeline.c \
          demux/demux_playlist.c \
          demux/demux_rar.c \
          demux/demux_raw.c \
          demux/demux_subreader.c \
          demux/ebml.c \
          demux/packet.c \
          demux/timeline.c \
          input/cmd_list.c \
          input/cmd_parse.c \
          input/event.c \
          input/input.c \
          input/ipc.c \
          input/keycodes.c \
          misc/bstr.c \
          misc/charset_conv.c \
          misc/dispatch.c \
          misc/json.c \
          misc/rendezvous.c \
          misc/ring.c \
          options/m_config.c \
          options/m_option.c \
          options/m_property.c \
          options/options.c \
          options/parse_commandline.c \
          options/parse_configfile.c \
          options/path.c \
          osdep/io.c \
          osdep/path-unix.c \
          osdep/semaphore_osx.c \
          osdep/subprocess.c \
          osdep/subprocess-posix.c \
          osdep/terminal-unix.c \
          osdep/timer.c \
          osdep/timer-linux.c \
          osdep/threads.c \
          player/audio.c \
          player/client.c \
          player/configfiles.c \
          player/command.c \
          player/loadfile.c \
          player/main.c \
          player/misc.c \
          player/osd.c \
          player/playloop.c \
          player/screenshot.c \
          player/scripting.c \
          player/sub.c \
          player/video.c \
          stream/cache.c \
          stream/cache_file.c \
          stream/cookies.c \
          stream/rar.c \
          stream/stream.c \
          stream/stream_avdevice.c \
          stream/stream_edl.c \
          stream/stream_file.c \
          stream/stream_lavf.c \
          stream/stream_memory.c \
          stream/stream_mf.c \
          stream/stream_null.c \
          stream/stream_rar.c \
          sub/dec_sub.c \
          sub/draw_bmp.c \
          sub/find_subfiles.c \
          sub/img_convert.c \
          sub/osd.c \
          sub/sd_lavc.c \
          sub/sd_lavc_conv.c \
          sub/sd_lavf_srt.c \
          sub/sd_microdvd.c \
          sub/sd_movtext.c \
          sub/sd_srt.c \
          ta/ta.c \
          ta/ta_utils.c \
          ta/ta_talloc.c \
          video/csputils.c \
          video/fmt-conversion.c \
          video/image_writer.c \
          video/img_format.c \
          video/mp_image.c \
          video/mp_image_pool.c \
          video/sws_utils.c \
          video/decode/dec_video.c \
          video/decode/vd_lavc.c \
          video/filter/vf.c \
          video/filter/vf_buffer.c \
          video/filter/vf_crop.c \
          video/filter/vf_dsize.c \
          video/filter/vf_eq.c \
          video/filter/vf_expand.c \
          video/filter/vf_flip.c \
          video/filter/vf_format.c \
          video/filter/vf_mirror.c \
          video/filter/vf_noformat.c \
          video/filter/vf_scale.c \
          video/filter/vf_stereo3d.c \
          video/filter/vf_sub.c \
          video/out/bitmap_packer.c \
          video/out/aspect.c \
          video/out/filter_kernels.c \
          video/out/vo.c \
          video/out/vo_null.c \
          video/out/vo_image.c \
          video/out/win_state.c \
          $(SOURCES-yes)

OBJECTS         += $(addsuffix .o, $(basename $(SOURCES)))
OBJECTS         += $(OBJECTS-yes)

DEP_FILES = $(patsubst %.S,%.d,$(patsubst %.cpp,%.d,$(patsubst %.c,%.d,$(SOURCES:.m=.d) $(SOURCES:.m=.d))))

ALL_TARGETS     += mpv

INSTALL_BIN     += install-mpv
INSTALL_BIN_STRIP += install-mpv-strip
INSTALL_MAN      =

ifeq ($(BUILD_MAN),yes)
    INSTALL_MAN += install-mpv-man
    ALL_TARGETS += DOCS/man/mpv.1
endif

DIRS =  . \
        audio \
        audio/decode \
        audio/filter \
        audio/out \
        common \
        compat \
        input \
        player/timeline \
        demux \
        misc \
        options \
        osdep \
        osdep/ar \
        player \
        stream \
        sub \
        ta \
        video \
        video/decode \
        video/filter \
        video/out


ADDSUFFIXES     = $(foreach suf,$(1),$(addsuffix $(suf),$(2)))
ADD_ALL_DIRS    = $(call ADDSUFFIXES,$(1),$(DIRS))

###### brief build output #######

ifndef V
$(eval override CC = @printf "CC\t$$@\n"; $(CC))
$(eval override RM = @$(RM))
endif

###### generic rules #######

all: $(ALL_TARGETS)

%.1: %.rst
	$(RST2MAN) $< $@

%.o: %.c
	$(CC) $(DEPFLAGS) $(CFLAGS) -c -o $@ $<

mpv: $(OBJECTS) osdep/main-fn-unix.o
	$(CC) -o $@ $^ $(EXTRALIBS)

input/input.c: input/input_conf.h
input/input_conf.h: TOOLS/file2string.pl etc/input.conf
	./$^ >$@

MKVLIB_DEPS = TOOLS/lib/Parse/Matroska.pm \
              TOOLS/lib/Parse/Matroska/Definitions.pm \
              TOOLS/lib/Parse/Matroska/Element.pm \
              TOOLS/lib/Parse/Matroska/Reader.pm \
              TOOLS/lib/Parse/Matroska/Utils.pm \

demux/ebml.c demux/demux_mkv.c: demux/ebml_types.h
demux/ebml_types.h: TOOLS/matroska.pl $(MKVLIB_DEPS)
	./$< --generate-header > $@

demux/ebml.c: demux/ebml_defs.c
demux/ebml_defs.c: TOOLS/matroska.pl $(MKVLIB_DEPS)
	./$< --generate-definitions > $@

video/out/x11_common.c: video/out/x11_icon.inc
video/out/x11_icon.inc: TOOLS/file2string.pl video/out/x11_icon.bin
	./$^ >$@

sub/osd_libass.c: sub/osd_font.h
sub/osd_font.h: TOOLS/file2string.pl sub/osd_font.otf
	./$^ >$@

player/lua/%.inc: TOOLS/file2string.pl player/lua/%.lua
	./$^ >$@

player/lua.c: player/lua/defaults.inc \
              player/lua/assdraw.inc \
              player/lua/osc.inc \
              player/lua/ytdl_hook.inc \
              player/lua/options.inc

etc/_mpv: TOOLS/zsh.pl ./mpv
	./$< > $@

# ./configure must be rerun if it changed
config.mak: configure
	@echo "############################################################"
	@echo "####### Please run ./configure again - it's changed! #######"
	@echo "############################################################"

old_build/version.h .version: version.sh
	./version.sh --versionh=old_build/version.h

# Force version.sh to run to potentially regenerate version.h
-include .version

%: %.c
	$(CC) $(CFLAGS) -o $@ $^


###### dependency declarations / specific CFLAGS ######

common/version.c: old_build/version.h

DOCS/man/mpv.1: DOCS/man/af.rst \
                   DOCS/man/ao.rst \
                   DOCS/man/changes.rst \
                   DOCS/man/encode.rst \
                   DOCS/man/input.rst \
                   DOCS/man/options.rst \
                   DOCS/man/vf.rst \
                   DOCS/man/vo.rst

###### installation / clean / generic rules #######

install:               $(INSTALL_BIN)       install-data $(INSTALL_MAN)
install-no-man:        $(INSTALL_BIN)       install-data
install-strip:         $(INSTALL_BIN_STRIP) install-data $(INSTALL_MAN)
install-strip-no-man:  $(INSTALL_BIN_STRIP) install-data

install-dirs:
	if test ! -d $(BINDIR) ; then $(INSTALL) -d $(BINDIR) ; fi

install-%: % install-dirs
	$(INSTALL) -m 755 $< $(BINDIR)

install-%-strip: % install-dirs
	$(INSTALL) -m 755 -s $< $(BINDIR)

install-mpv-man:  install-mpv-man-en

install-mpv-man-en: DOCS/man/mpv.1
	if test ! -d $(MANDIR)/man1 ; then $(INSTALL) -d $(MANDIR)/man1 ; fi
	$(INSTALL) -m 644 DOCS/man/mpv.1 $(MANDIR)/man1/

ICONSIZES = 16x16 32x32 64x64

define ICON_INSTALL_RULE
install-mpv-icon-$(size): etc/mpv-icon-8bit-$(size).png
	$(INSTALL) -d $(prefix)/share/icons/hicolor/$(size)/apps
	$(INSTALL) -m 644 etc/mpv-icon-8bit-$(size).png $(prefix)/share/icons/hicolor/$(size)/apps/mpv.png
endef

$(foreach size,$(ICONSIZES),$(eval $(ICON_INSTALL_RULE)))

install-mpv-icons: $(foreach size,$(ICONSIZES),install-mpv-icon-$(size))

install-mpv-desktop: etc/mpv.desktop
	$(INSTALL) -d $(prefix)/share/applications
	$(INSTALL) -m 644 etc/mpv.desktop $(prefix)/share/applications/

install-mpv-config: etc/encoding-profiles.conf
	$(INSTALL) -d $(CONFDIR)
	$(INSTALL) -m 644 etc/encoding-profiles.conf $(CONFDIR)

install-mpv-zsh: etc/_mpv
	$(INSTALL) -d $(prefix)/share/zsh/vendor-completions
	$(INSTALL) -m 644 etc/_mpv $(prefix)/share/zsh/vendor-completions/

install-data: install-mpv-icons install-mpv-desktop install-mpv-config install-mpv-zsh

uninstall:
	$(RM) $(BINDIR)/mpv
	$(RM) $(MANDIR)/man1/mpv.1 $(MANDIR)/man1/mpv.1
	$(RM) $(prefix)/share/applications/mpv.desktop
	$(RM) $(prefix)/share/zsh/vendor-completions/_mpv
	$(RM) $(foreach size,$(ICONSIZES),$(prefix)/share/icons/hicolor/$(size)/apps/mpv.png)

clean:
	-$(RM) $(call ADD_ALL_DIRS,/*.o /*.d /*.a /*.ho /*~)
	-$(RM) $(call ADD_ALL_DIRS,/*.o /*.a /*.ho /*~)
	-$(RM) mpv
	-$(RM) DOCS/man/*/mpv.1
	-$(RM) old_build/version.h version.h
	-$(RM) input/input_conf.h
	-$(RM) video/out/vdpau_template.c
	-$(RM) demux/ebml_types.h demux/ebml_defs.c
	-$(RM) video/out/x11_icon.inc
	-$(RM) sub/osd_font.h
	-$(RM) player/lua/defaults.inc
	-$(RM) player/lua/assdraw.inc
	-$(RM) player/lua/osc.inc
	-$(RM) player/lua/ytdl_hook.inc
	-$(RM) player/lua/options.inc

distclean: clean
	-$(RM) config.log old_build/config.h old_build/config.mak Makefile
	-rmdir old_build/

-include $(DEP_FILES)

.PHONY: all *install* *clean .version

# Disable suffix rules.  Most of the builtin rules are suffix rules,
# so this saves some time on slow systems.
.SUFFIXES:

# If a command returns failure but changed its target file, delete the
# (presumably malformed) file. Otherwise the file would be considered to
# be up to date if make is restarted.

.DELETE_ON_ERROR: