A2X           = @A2X@
AR            = ar
RANLIB        = ranlib
BIN_DEPS      = @BIN_DEPS@
CP            = cp
VERBOSE      ?= 0
COND_SILENCE_0 = @
COND_SILENCE_1 =
COND_SILENCE  = $(COND_SILENCE_$(VERBOSE))
REAL_CC       = @CC@
CC_0          = @echo "Compiling $<..."; $(REAL_CC)
CC_1          = $(REAL_CC)
CC            = $(CC_$(VERBOSE))
REAL_CXX      = @CXX@
CXX_0         = @echo "Compiling $<..."; $(REAL_CXX)
CXX_1         = $(REAL_CXX)
CXX           = $(CXX_$(VERBOSE))
QMAKE         = @QMAKE@
QMAKE_FLAGS  ?=
REAL_LINKER   = @LINKER@
LINKER_0      = @echo "Linking $@..."; $(REAL_LINKER)
LINKER_1      = $(REAL_LINKER)
LINKER        = $(LINKER_$(VERBOSE))
COMMON_FLAGS  = @COMMON_FLAGS@ -g @DEFS@ -DPATH_PREFIX='"@prefix@"' -DLIB_DIR='"@libdir@"' -Wall -Wextra -Wpointer-arith -DGF_BITS=16
CFLAGS_ORIG   := $(CFLAGS)
CFLAGS        = @CFLAGS@ $(COMMON_FLAGS) -Werror=implicit-function-declaration
CPPFLAGS      = @CPPFLAGS@ $(COMMON_FLAGS)
CXXFLAGS_ORIG := $(CXXFLAGS)
CXXFLAGS      = @CXXFLAGS@ $(COMMON_FLAGS)
MKDIR_P       = $(COND_SILENCE)mkdir -p
CUDA_FLAGS    = @CUDA_FLAGS@ @CUDA_COMPUTE_ARGS@ @DEFS@ -g
LDFLAGS      =  @LDFLAGS@
LIBS         += @LIBS@ @CUDA_LIB@ @MATHLIBS@ -lm -pthread
INC           = -Isrc -I$(srcdir)/src -I$(srcdir)/test \
                @INC@
CUDA_INC      = @CUDA_INC@ -Isrc -I$(srcdir)/src
OFAST         = @OFAST@
DECKLINK_PATH = @DECKLINK_PATH@
DYLIBBUNDLER  = @DYLIBBUNDLER@
DYLIBBUNDLER_FLAGS += @DYLIBBUNDLER_FLAGS@
EXEEXT        = @EXEEXT@
TARGET       ?= bin/uv$(EXEEXT)
BUNDLE        = uv.app
GUI_BUNDLE_DEP = gui/QT/uv-qt.app
GUI_BUNDLE    = uv-qt.app
REAL_CUDA_COMPILER = "@CUDA_COMPILER@"
CUDA_COMPILER_0 = @echo "Compiling $<..."; $(REAL_CUDA_COMPILER)
CUDA_COMPILER_1 = $(REAL_CUDA_COMPILER)
CUDA_COMPILER   = $(CUDA_COMPILER_$(VERBOSE))
MACOS_LEGACY  = @MACOS_LEGACY@

GUI_EXE      ?= @GUI_EXE@
GUI_TARGET    = @GUI_TARGET@
QT_PATH       = $(shell [ -n "$(QMAKE)" ] && python3 -c "import os; print(\
os.path.realpath(os.path.dirname(os.path.realpath(\"$$(command -v \
$(QMAKE))\")) + \"/..\"))")# realpath cmd is present in macOS 13
REFLECTOR_TARGET ?= @REFLECTOR_TARGET@
# append $(EXEEXT) if $(REFLECTOR_TARGET) not empty
REFLECTOR_TARGET != sh -c 'RT=$(REFLECTOR_TARGET); echo $${RT:+$${RT}$(EXEEXT)}'
TEST_TARGET  = bin/run_tests$(EXEEXT)

PACKAGE_TARNAME ?= @PACKAGE_TARNAME@
PREFIX = @prefix@
prefix = $(PREFIX)
exec_prefix = @exec_prefix@
INSTALL = @INSTALL@
bindir = @bindir@
libdir = @libdir@
datadir = @datadir@
datarootdir = @datarootdir@
docdir = @docdir@
mandir = @mandir@
man1dir = @mandir@/man1
srcdir        = @srcdir@
VPATH         = @srcdir@

DOCS 	      = $(srcdir)/CONTRIBUTING.md $(srcdir)/README.md $(wildcard $(srcdir)/doc/*)

# autogenerated headers
GENERATED_HEADERS              = @GENERATED_HEADERS@

COMMON_OBJS     = \
		src/control_socket.o \
		src/debug.o \
		src/host.o \
		src/keyboard_control.o \
		src/messaging.o \
		src/playback.o \
		src/ntp.o \
		src/pdb.o \
		src/tv.o \
		src/transmit.o \
		src/tfrc.o \
		src/rtp/fec.o \
		src/rtp/ldgm.o \
		src/rtp/pbuf.o \
		src/rtp/audio_decoders.o \
		src/rtp/net_udp.o \
		src/rtp/rs.o \
		src/rtp/rtp.o \
		src/rtp/rtpenc_h264.o \
		src/rtp/rtp_callback.o \
		src/rtp/video_decoders.o \
		src/audio/audio.o \
		src/audio/audio_capture.o \
		src/audio/audio_playback.o \
		src/audio/capture/none.o \
		src/audio/capture/sdi.o \
		src/audio/capture/testcard.o \
		src/audio/codec.o \
		src/audio/codec/dummy_pcm.o \
		src/audio/export.o \
		src/audio/playback/dummy.o \
		src/audio/playback/none.o \
		src/audio/playback/sdi.o \
		src/audio/resampler.o \
		src/audio/types.o \
		src/audio/utils.o \
		src/audio/wav_reader.o \
		src/audio/wav_writer.o \
		src/audio/audio_filter.o \
		src/audio/filter_chain.o \
		src/capture_filter.o \
		src/color_space.o \
		src/compat/alarm.o \
		src/compat/aligned_malloc.o \
		src/compat/dlfunc.o \
		src/compat/platform_pipe.o \
		src/compat/platform_semaphore.o \
		src/compat/usleep.o \
		src/crypto/crc_32.o \
		src/crypto/crypt_aes.o \
		src/crypto/crypt_aes_impl.o \
		src/crypto/crypt_des.o \
		src/crypto/md5.o \
		src/crypto/random.o \
		src/export.o \
		src/ihdtv/ihdtv.o \
		src/lib_common.o \
		src/module.o \
		src/pixfmt_conv.o \
		src/ug_runtime_error.o \
		tools/ipc_frame_ug.o \
		tools/ipc_frame_unix.o \
		tools/ipc_frame.o \
		src/utils/audio_buffer.o \
		src/utils/color_out.o \
		src/utils/config_file.o \
		src/utils/dictionary.o \
		src/utils/fs.o \
		src/utils/jpeg_reader.o \
		src/utils/list.o \
		src/utils/math.o \
		src/utils/misc.o \
		src/utils/nat.o \
		src/utils/net.o \
		src/utils/packet_counter.o \
		src/utils/pam.o \
		src/utils/parallel_conv.o \
		src/utils/profile_timer.o \
		src/utils/random.o \
		src/utils/resource_manager.o \
		src/utils/ring_buffer.o \
		src/utils/sdp.o \
		src/utils/string.o \
		src/utils/string_view_utils.o \
		src/utils/synchronized_queue.o \
		src/utils/text.o \
		src/utils/thread.o \
		src/utils/time.o \
		src/utils/vf_split.o \
		src/utils/video_frame_pool.o \
		src/utils/video_pattern_generator.o \
		src/utils/wait_obj.o \
		src/utils/windows.o \
		src/utils/worker.o \
		src/utils/y4m.o \
		src/video.o \
		src/video_frame.o \
		src/video_codec.o \
		src/video_capture.o \
		src/video_capture_params.o \
		src/video_capture/null.o \
		src/video_capture/testcard.o \
		src/video_capture/testcard_common.o \
		src/video_compress.o \
		src/video_compress/none.o \
		src/video_decompress.o \
		src/video_display.o \
		src/video_display/dummy.o \
		src/video_display/null.o \
		src/video_export.o \
		src/video_rxtx.o \
		src/video_rxtx/rtp.o \
		src/video_rxtx/ultragrid_rtp.o \
		src/vo_postprocess.o \
		ldgm/src/ldgm-session-cpu.o \
		ldgm/src/ldgm-session.o \
		ldgm/src/tanner.o \
		ldgm/matrix-gen/matrix-generator.o \
		ldgm/matrix-gen/ldpc-matrix.o \

ULTRAGRID_LIBS = @ULTRAGRID_LIBS@
ULTRAGRID_OBJS = $(COMMON_OBJS) @ULTRAGRID_OBJS@ src/main.o \

REFLECTOR_OBJS = @REFLECTOR_OBJS@ $(COMMON_OBJS) \
		src/hd-rum-translator/hd-rum-decompress.o \
		src/hd-rum-translator/hd-rum-recompress.o \
		src/hd-rum-translator/hd-rum-translator.o \

TEST_OBJS = $(COMMON_OBJS) \
	    @TEST_OBJS@ \
	    test/codec_conversions_test.o \
	    test/ff_codec_conversions_test.o \
	    test/get_framerate_test.o \
	    test/gpujpeg_test.o \
	    test/libavcodec_test.o \
	    test/misc_test.o \
	    test/test_aes.o \
	    test/test_des.o \
	    test/test_md5.o \
	    test/test_random.o \
	    test/test_video_display.o \
	    test/test_video_capture.o \
	    test/test_tv.o \
	    test/test_net_udp.o \
	    test/test_rtp.o \
	    src/utils/sdp_parser.o \
	    test/test_sdp_parser.o \
	    test/run_tests.o

DEP_FILES_2 = $(REFLECTOR_OBJS) $(TEST_OBJS) $(ULTRAGRID_OBJS)
# replace .o and also .lib (Windows) with .P
DEP_FILES_1 = $(DEP_FILES_2:.o=.P)
DEP_FILES = $(DEP_FILES_1:.lib=.P)
# -------------------------------------------------------------------------------------------------
.PHONY: doc

all: $(TARGET) $(GUI_TARGET) $(REFLECTOR_TARGET) @MANPAGES@ @MODULES@ configure-messages

src/dir-stamp:
	$(MKDIR_P) $$(dirname $@)
	touch $@

$(TARGET): src/dir-stamp $(ULTRAGRID_OBJS) $(GENERATED_HEADERS) $(BIN_DEPS)
	$(MKDIR_P) $$(dirname $@)
	$(LINKER) $(LDFLAGS) @UV_LDFLAGS@ $(ULTRAGRID_OBJS) $(LIBS) $(ULTRAGRID_LIBS)\
	  -o $(TARGET)
	@if [ -n '@DLL_LIBS@' ]; then $(INSTALL) -m 644 @DLL_LIBS@ bin; fi
	@if [ "$$(uname -s)" = Darwin ]; then dsymutil $(TARGET); fi

$(REFLECTOR_TARGET): src/dir-stamp $(REFLECTOR_OBJS) $(GENERATED_HEADERS) bin/hd-rum-av.sh
	$(MKDIR_P) $$(dirname $@)
	$(LINKER) $(LDFLAGS) $(REFLECTOR_OBJS) $(LIBS) -o $@
	@if [ "$$(uname -s)" = Darwin ]; then dsymutil $(REFLECTOR_TARGET); fi

bin/hd-rum-av.sh: $(srcdir)/data/template/bin/hd-rum-av.sh
	$(MKDIR_P) $$(dirname $@)
	$(CP) $(srcdir)/data/template/bin/hd-rum-av.sh $@

-include $(DEP_FILES)

POSTPROCESS_DEPS = \
	@cp ${@:.o=.d} ${@:.o=.P}; \
		sed -e 's/\#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
		-e '/^$$/ d' -e 's/$$/ :/' < ${@:.o=.d} >> ${@:.o=.P}; \
		rm -f ${@:.o=.d}

.SUFFIXES: .o .c .cpp .cu .m .mm

.c.o :
	$(MKDIR_P) $$(dirname $@)
	$(CC) $(CFLAGS) $(INC) -MD -c $< -o $@
	$(POSTPROCESS_DEPS)

.cpp.o :
	$(MKDIR_P) $$(dirname $@)
	$(CXX) $(CXXFLAGS) $(INC) -MD -c $< -o $@
	$(POSTPROCESS_DEPS)

.m.o:
	$(MKDIR_P) $$(dirname $@)
	$(CC) $(CFLAGS) $(INC) -MD -c $< -o $@
	$(POSTPROCESS_DEPS)

.mm.o:
	$(MKDIR_P) $$(dirname $@)
	$(CXX) -x objective-c++ -std=c++11 $(CXXFLAGS) $(INC) -MD -c $< -o $@
	$(POSTPROCESS_DEPS)

# Pattern rule for compiling CUDA files
.cu.o:
	$(MKDIR_P) $$(dirname $@)
	$(CUDA_COMPILER) $(CUDA_FLAGS) $(CUDA_INC) -c $< -o $@
	@$(REAL_CUDA_COMPILER) $(CUDA_FLAGS) $(CUDA_INC) -M $< > $*.d
	$(POSTPROCESS_DEPS)

# rules specialized for the targets below; using C[XX]_NO_SUFF because $<
# unavailable in non-suffix rules except GNUmake. $+ (list of prerequisities)
# is defined by POSIX Issue 8 but not yet available in BSD's Make so using $@ as
# a workaround.
CC_NO_SUFF_0 = @N=$$(echo $+ | awk '{print $$1}'); echo "Compiling $${N:-$@}..."; $(REAL_CC)
CC_NO_SUFF_1 = $(REAL_CC)
CC_NO_SUFF   = $(CC_NO_SUFF_$(VERBOSE))
CXX_NO_SUFF_0 = @N=$$(echo $+ | awk '{print $$1}'); echo "Compiling $${N:-$@}..."; $(REAL_CXX)
CXX_NO_SUFF_1 = $(REAL_CXX)
CXX_NO_SUFF   = $(CXX_NO_SUFF_$(VERBOSE))

src/libavcodec/from_lavc_vid_conv.o: src/libavcodec/from_lavc_vid_conv.c
	$(MKDIR_P) $$(dirname $@)
	$(CC_NO_SUFF) $(CFLAGS) $(OFAST) $(INC) -MD -c $(srcdir)/src/libavcodec/from_lavc_vid_conv.c -o $@
	$(POSTPROCESS_DEPS)

src/libavcodec/to_lavc_vid_conv.o: src/libavcodec/to_lavc_vid_conv.c
	$(MKDIR_P) $$(dirname $@)
	$(CC_NO_SUFF) $(CFLAGS) $(OFAST) $(INC) -MD -c $(srcdir)/src/libavcodec/to_lavc_vid_conv.c -o $@
	$(POSTPROCESS_DEPS)

src/pixfmt_conv.o: src/pixfmt_conv.c
	$(MKDIR_P) $$(dirname $@)
	$(CC_NO_SUFF) $(CFLAGS) $(OFAST) $(INC) -MD -c $(srcdir)/src/pixfmt_conv.c -o $@
	$(POSTPROCESS_DEPS)

# Important for this target is inclusion of cuda_wrapper that has patched cuda_runtime.h header (wrapper)
ldgm/src/ldgm-session-gpu.o: ldgm/src/ldgm-session-gpu.cpp
	$(MKDIR_P) $$(dirname $@)
	$(CXX_NO_SUFF) $(CXXFLAGS) -Isrc/cuda_wrapper -DEXPORT_DLL_SYMBOLS $(INC) -MD\
	 -c $(srcdir)/ldgm/src/ldgm-session-gpu.cpp -o $@
	$(POSTPROCESS_DEPS)

src/zfec.o: @ZFEC_PREFIX@/fec.c
	$(MKDIR_P) $$(dirname $@)
	$(CC_NO_SUFF) $(CFLAGS) -w $(INC) -MD -c @ZFEC_PREFIX@/fec.c -o $@
	$(POSTPROCESS_DEPS)

src/video_capture/DeckLinkAPIDispatch.o: $(DECKLINK_PATH)/DeckLinkAPIDispatch.cpp
	$(MKDIR_P) $$(dirname $@)
	$(CXX_NO_SUFF) $(CXXFLAGS) -MD -c $(INC) -o\
	 src/video_capture/DeckLinkAPIDispatch.o\
	 $(DECKLINK_PATH)/DeckLinkAPIDispatch.cpp
	$(POSTPROCESS_DEPS)

src/video_capture/DeckLinkAPI_i.o: $(DECKLINK_PATH)/DeckLinkAPI_i.c
	$(MKDIR_P) $$(dirname $@)
	$(CC_NO_SUFF) $(CFLAGS) -MD -c $(INC) -o\
	  src/video_capture/DeckLinkAPI_i.o $(DECKLINK_PATH)/DeckLinkAPI_i.c
	$(POSTPROCESS_DEPS)

dxt_compress/dxt_encoder.o: dxt_compress/dxt_encoder.c dxt_compress/dxt_glsl.h
	$(MKDIR_P) $$(dirname $@)
	$(CC_NO_SUFF) $(CFLAGS) $(INC) $(srcdir)/dxt_compress/dxt_encoder.c -MD -c -o $@
	$(POSTPROCESS_DEPS)

dxt_compress/dxt_decoder.o: dxt_compress/dxt_decoder.c dxt_compress/dxt_glsl.h
	$(MKDIR_P) $$(dirname $@)
	$(CC_NO_SUFF) $(CFLAGS) $(INC) $(srcdir)/dxt_compress/dxt_decoder.c -MD -c -o $@
	$(POSTPROCESS_DEPS)

dxt_compress/dxt_glsl.h:dxt_compress/compress_vp.glsl \
		dxt_compress/compress_dxt1_fp.glsl dxt_compress/compress_dxt5ycocg_fp.glsl \
		dxt_compress/display_fp.glsl dxt_compress/display_dxt5ycocg_fp.glsl \
		dxt_compress/yuv422_to_yuv444.glsl dxt_compress/display_dxt1_yuv_fp.glsl \
		dxt_compress/rgba_to_yuv422.glsl
	@echo "Generating $@..."
	$(MKDIR_P) $$(dirname $@)
	@echo "/**" > $@
	@echo " * GLSL source codes for DXT compressions" >> $@
	@echo " *" >> $@
	@echo " * @author Martin Srom" >> $@
	@echo " */" >> $@
	# Write vp_compress
	@echo "static const char vp_compress[] = " >> $@
	@printf "\"#version 140\\\n\"\n" >> $@
	@printf "\"#define legacy 0\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/compress_vp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write vp_compress_legacy
	@echo "static const char vp_compress_legacy[] = " >> $@
	@printf "\"#define legacy 1\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/compress_vp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write fp_compress_dxt1
	@echo "static const char fp_compress_dxt1[] = " >> $@
	@printf "\"#version 140\\\n\"\n" >> $@
	@printf "\"#define legacy 0\\\n\"\n" >> $@
	@printf "\"#define FORMAT_YUV 0\\\n\"\n" >> $@
	@cat  $(srcdir)/dxt_compress/compress_dxt1_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write fp_compress_dxt1 yuv
	@echo "static const char fp_compress_dxt1_yuv[] = " >> $@
	@printf "\"#version 140\\\n\"\n" >> $@
	@printf "\"#define legacy 0\\\n\"\n" >> $@
	@printf "\"#define FORMAT_YUV 1\\\n\"\n" >> $@
	@cat  $(srcdir)/dxt_compress/compress_dxt1_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write fp_compress_dxt1_legacy
	@echo "static const char fp_compress_dxt1_legacy[] = " >> $@
	@printf "\"#define legacy 1\\\n\"\n" >> $@
	@printf "\"#define FORMAT_YUV 0\\\n\"\n" >> $@
	@cat  $(srcdir)/dxt_compress/compress_dxt1_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write fp_compress_dxt1_legacy
	@echo "static const char fp_compress_dxt1_yuv_legacy[] = " >> $@
	@printf "\"#define legacy 1\\\n\"\n" >> $@
	@printf "\"#define FORMAT_YUV 1\\\n\"\n" >> $@
	@cat  $(srcdir)/dxt_compress/compress_dxt1_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write fp_compress_dxt5ycocg
	@echo "static const char fp_compress_dxt5ycocg[] = " >> $@
	@printf "\"#version 140\\\n\"\n" >> $@
	@printf "\"#define legacy 0\\\n\"\n" >> $@
	@printf "\"#define FORMAT_YUV 0\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/compress_dxt5ycocg_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write fp_compress_dxt5ycocg yuv
	@echo "static const char fp_compress_dxt5ycocg_yuv[] = " >> $@
	@printf "\"#version 140\\\n\"\n" >> $@
	@printf "\"#define legacy 0\\\n\"\n" >> $@
	@printf "\"#define FORMAT_YUV 1\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/compress_dxt5ycocg_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write fp_compress_dxt5ycocg_legacy
	@echo "static const char fp_compress_dxt5ycocg_legacy[] = " >> $@
	@printf "\"#define legacy 1\\\n\"\n" >> $@
	@printf "\"#define FORMAT_YUV 0\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/compress_dxt5ycocg_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write fp_compress_dxt5ycocg_legacy yuv
	@echo "static const char fp_compress_dxt5ycocg_yuv_legacy[] = " >> $@
	@printf "\"#define legacy 1\\\n\"\n" >> $@
	@printf "\"#define FORMAT_YUV 1\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/compress_dxt5ycocg_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write fp_display_dxt1
	@echo "static const char fp_display[] = " >> $@
	@printf "\"#version 140\\\n\"\n" >> $@
	@printf "\"#define legacy 0\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/display_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write fp_display_dxt1 legacy
	@echo "static const char fp_display_legacy[] = " >> $@
	@printf "\"#define legacy 1\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/display_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write fp_display_dxt5ycocg
	@echo "static const char fp_display_dxt5ycocg[] = " >> $@
	@printf "\"#version 140\\\n\"\n" >> $@
	@printf "\"#define legacy 0\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/display_dxt5ycocg_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# Write fp_display_dxt5ycocg (legacy)
	@echo "static const char fp_display_dxt5ycocg_legacy[] = " >> $@
	@printf "\"#define legacy 1\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/display_dxt5ycocg_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# yuv 422 to yuv 444 shader
	@echo "static const char fp_yuv422_to_yuv_444[] = " >> $@
	@printf "\"#version 140\\\n\"\n" >> $@
	@printf "\"#define legacy 0\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/yuv422_to_yuv444.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# yuv 422 to yuv 444 shader (legacy)
	@echo "static const char fp_yuv422_to_yuv_444_legacy[] = " >> $@
	@printf "\"#define legacy 1\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/yuv422_to_yuv444.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# display YUV dxt1
	@echo "static const char fp_display_dxt1_yuv[] = " >> $@
	@printf "\"#version 140\\\n\"\n" >> $@
	@printf "\"#define legacy 0\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/display_dxt1_yuv_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# display YUV dxt1 (legacy)
	@echo "static const char fp_display_dxt1_yuv_legacy[] = " >> $@
	@printf "\"#define legacy 1\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/display_dxt1_yuv_fp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# rgba to yuv 422 shader
	@echo "static const char fp_display_rgba_to_yuv422[] = " >> $@
	@printf "\"#version 140\\\n\"\n" >> $@
	@printf "\"#define legacy 0\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/rgba_to_yuv422.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	# rgba to yuv 422 shader legacy
	@echo "static const char fp_display_rgba_to_yuv422_legacy[] = " >> $@
	@printf "\"#define legacy 1\\\n\"\n" >> $@
	@cat $(srcdir)/dxt_compress/rgba_to_yuv422.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@echo ";" >> $@
	@# rgba to yuv 422 vertex
	@#echo "static const char vp_display_rgba_to_yuv422[] = " >> $@
	@#cat $(srcdir)/dxt_compress/rgba_to_yuv422_vp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	@#echo ";" >> $@

gui/QT/Makefile: gui/QT/uv-qt.pro
	@if test -z "$(QMAKE)"; then echo "Reconfigure with '--enable-qt'"; exit 1; fi
	CFLAGS="$(CFLAGS_ORIG)" CXXFLAGS="$(CXXFLAGS_ORIG)" $(QMAKE) -makefile $(srcdir)/gui/QT/uv-qt.pro "DESTDIR+=./" -o $@ $(QMAKE_FLAGS)


.PHONY: $(GUI_TARGET)
$(GUI_TARGET): gui/QT/Makefile
	$(COND_SILENCE)cd gui/QT && $(MAKE) --no-print-directory

# -------------------------------------------------------------------------------------------------
$(TEST_TARGET): $(TEST_OBJS) @TEST_OBJS@
	$(MKDIR_P) $$(dirname $@)
	$(LINKER) $(LDFLAGS) $(TEST_OBJS) @TEST_LIBS@ -o $@
	@if [ -n '@DLL_LIBS@' ]; then $(INSTALL) -m 644 @DLL_LIBS@ bin; fi

suggest-tests:
	@echo ""
	@echo "*** Now type \"make tests\" to run the test suite"
	@echo ""

configure-messages:
	@printf "@POST_COMPILE_MSG@"

tests: $(TEST_TARGET)
	@export DYLD_LIBRARY_PATH=$(MY_DYLD_LIBRARY_PATH); $(TEST_TARGET)

check: tests

distcheck:
	$(TARGET)
	$(TARGET) --capabilities
	$(TARGET) --list-modules
	$(REFLECTOR_TARGET) -h
	[ -z "$(GUI_EXE)" ] || $(GUI_EXE) -h

# -------------------------------------------------------------------------------------------------
ag-plugins: data/ag_plugin/uvReceiverService.zip data/ag_plugin/uvSenderService.zip

AG_PLUGIN_TX_SCRIPTS = $(srcdir)/data/ag_plugin/uvSenderService.py \
                       $(srcdir)/data/ag_plugin/uvSenderService.svc \
                       $(srcdir)/data/ag_plugin/uvSenderService.manifest

data/ag_plugin/uvSenderService.zip: $(AG_PLUGIN_TX_SCRIPTS) $(TARGET)

	@echo "Creating AccessGrid plugin: uvSenderService.zip"
	@rm  -f data/ag_plugin/uvSenderService.zip
	@zip -j data/ag_plugin/uvSenderService.zip $(AG_PLUGIN_TX_SCRIPTS) $(TARGET)

AG_PLUGIN_RX_SCRIPTS = $(srcdir)/data/ag_plugin/uvReceiverService.py \
                       $(srcdir)/data/ag_plugin/uvReceiverService.svc \
                       $(srcdir)/data/ag_plugin/uvReceiverService.manifest

data/ag_plugin/uvReceiverService.zip: $(AG_PLUGIN_RX_SCRIPTS) $(TARGET)
	$(MKDIR_P) $$(dirname $@)
	@echo "Creating AccessGrid plugin: uvReceiverService.zip"
	@rm  -f data/ag_plugin/uvReceiverService.zip
	@zip -j data/ag_plugin/uvReceiverService.zip $(AG_PLUGIN_RX_SCRIPTS) $(TARGET)

# -------------------------------------------------------------------------------------------------
clean:
	@echo "Making clean..."
	$(COND_SILENCE)-rm -f $(OBJS) $(GENERATED_HEADERS) $(ULTRAGRID_OBJS) $(TARGET) src/version.h
	$(COND_SILENCE)-rm -f $(TEST_OBJS) bin/run_tests
	$(COND_SILENCE)-rm -f data/ag_plugin/uvReceiverService.zip data/ag_plugin/uvSenderService.zip
	$(COND_SILENCE)-rm -rf $(BUNDLE) $(GUI_BUNDLE) $(GUI_BUNDLE_DEP)
	$(COND_SILENCE)-rm -rf $(REFLECTOR_TARGET) bin/hd-rum-av.sh $(REFLECTOR_OBJS)
	$(COND_SILENCE)-rm -rf @TOREMOVE@ @MODULES@ @LIB_GENERATED_HEADERS@
	$(COND_SILENCE)-rm -rf $(DEP_FILES)
	$(COND_SILENCE)-rm -rf bin/shaders
	$(COND_SILENCE)-rm -f bin/uv.pdb
	$(COND_SILENCE)if [ -f "gui/QT/Makefile" ]; then make -C gui/QT/ distclean; fi

distclean: clean
	-rm -f Makefile config.status config.cache config.log src/config.h cscope.out tags

ctags:
	ctags $$(find $(srcdir)/src $(srcdir)/test \( -name '*.c' -o -name '*.cpp' -o -name '*.cu' -o -name '*.h' -o -name '*.hpp' -o -name '*.m' -o -name '*.mm' \))

etags:
	etags $$(find $(srcdir)/src $(srcdir)/test \( -name '*.c' -o -name '*.cpp' -o -name '*.cu' -o -name '*.h' -o -name '*.hpp' -o -name '*.m' -o -name '*.mm' \))

universal-ctags:
	ctags-universal $$(find $(srcdir)/src $(srcdir)/test \( -name '*.c' -o -name '*.cpp' -o -name '*.cu' -o -name '*.h' -o -name '*.hpp' -o -name '*.m' -o -name '*.mm' \))

cscope:
	cscope -bv $$(find $(srcdir)/src $(srcdir)/test \( -name '*.c' -o -name '*.cpp' -o -name '*.cu' -o -name '*.h' -o -name '*.hpp' -o -name '*.m' -o -name '*.mm' \))

doc:
	cd $(srcdir) && doxygen Doxyfile

uv.1: $(TARGET)
	$(MKDIR_P) $$(dirname $@)
	@A2X=$(A2X) UV_PATH=bin $(srcdir)/data/make_man.sh uv

hd-rum-transcode.1: $(REFLECTOR_TARGET)
	$(MKDIR_P) $$(dirname $@)
	@A2X=$(A2X) UV_PATH=bin $(srcdir)/data/make_man.sh hd-rum-transcode

man: uv.1 hd-rum-transcode.1

release:
	cvs tag release-`cat VERSION | sed "s/\./-/g"`

hd-rum-multi/hd-rum:
	make -C hd-rum-multi

$(BUNDLE): $(TARGET) $(REFLECTOR_TARGET) @MANPAGES@ hd-rum-multi/hd-rum
	rm -rf $(BUNDLE)
	$(MKDIR_P) $(BUNDLE)/Contents/MacOS $(BUNDLE)/Contents/libs
	$(CP) $(REFLECTOR_TARGET) $(TARGET) $(BUNDLE)/Contents/MacOS/
	$(CP) -r bin/*.dSYM $(BUNDLE)/Contents/MacOS/
	$(CP) -r data/template/macOS-bundle/* $(BUNDLE)/
	$(CP) hd-rum-multi/hd-rum $(BUNDLE)/Contents/MacOS/
	$(CP) $(srcdir)/data/template/bin/* $(BUNDLE)/Contents/MacOS
	$(CP) -r $(srcdir)/share/ $(BUNDLE)/Contents/share/
	$(srcdir)/data/scripts/macos_bundle_libs.sh "$(DYLIBBUNDLER)" \
		"$(DYLIBBUNDLER_FLAGS)" "$(BUNDLE)"
	if [ -d Frameworks ]; then $(CP) -R Frameworks $(BUNDLE)/Contents/; fi
	if [ -n '@MANPAGES@' ]; then mkdir -p $(BUNDLE)/Contents/man/man1; \
		$(CP) @MANPAGES@ $(BUNDLE)/Contents/man/man1/; fi
	#echo -n "UltraGrid" > $(BUNDLE)/Contents/PkgInfo

# ships UltraGrid itself to GUI bundle which is copied from
# gui/QT/uv-qt.app to uv-qt.app (referred as $(GUI_BUNDLE))
$(GUI_BUNDLE): $(BUNDLE) $(GUI_BUNDLE_DEP)
	rm -rf $(GUI_BUNDLE)
	$(MKDIR_P) $(GUI_BUNDLE)/Contents/libs
	ln -s libs $(GUI_BUNDLE)/Contents/Frameworks
	$(CP) -R $(GUI_BUNDLE_DEP)/* $(GUI_BUNDLE)/
	# add Qt frameworks
	command -v macdeployqt && macdeployqt $(GUI_BUNDLE) -verbose=2
	$(CP) -nR $(BUNDLE)/* $(GUI_BUNDLE)/ || true
	rm -rf $(GUI_BUNDLE)/Contents/MacOS/*.dSYM
	if [ $(MACOS_LEGACY) = no ]; then \
		for n in $(GUI_BUNDLE)/Contents/MacOS/*; do \
			if expr $$n : '.*-real$$' >/dev/null || expr $$n : '.*\.sh$$' >/dev/null || \
			[ ! -f $$n ]; then continue; fi; \
			mv -f $$n $$n-real; dsymutil $$n-real; \
			$(srcdir)/data/scripts/gen-macos-wrapper.sh > $$n; chmod 755 $$n; \
		done; \
	fi

	# TODO TOREMOVE: currently Qt6 (Homebrew) macdeployqt doesn't deploy QtDbus,
	# possibly because @rpath is used in QtDBus ref in QtGui
	if [ -d $(QT_PATH)/lib/QtDBus.framework ] && \
		[ ! -d $(GUI_BUNDLE)/Contents/Frameworks/QtDBus.framework ] && \
		`otool -L $(GUI_BUNDLE)/Contents/Frameworks/QtGui.framework/Versions/A/QtGui \
			| grep -q '@rpath/QtDBus'`; \
	then \
		$(CP) -R $(QT_PATH)/lib/QtDBus.framework $(GUI_BUNDLE)/Contents/Frameworks/; \
		chmod -R +w $(GUI_BUNDLE)/Contents/Frameworks/QtDBus.framework; \
		install_name_tool -change \
			'@loader_path/../../../../../../../opt/dbus/lib/libdbus-1.3.dylib' \
			/usr/local/opt/dbus/lib/libdbus-1.3.dylib \
			$(GUI_BUNDLE)/Contents/Frameworks/QtDBus.framework/QtDBus; \
		while :; do echo quit; done \
			| dylibbundler -of -cd -b -d $(GUI_BUNDLE)/Contents/libs \
				-x $(GUI_BUNDLE)/Contents/Frameworks/QtDBus.framework/QtDBus;\
	fi

	defaults write `pwd`/$(GUI_BUNDLE)/Contents/Info.plist NSCameraUsageDescription \
		'Allow camera for video capture'
	defaults write `pwd`/$(GUI_BUNDLE)/Contents/Info.plist NSMicrophoneUsageDescription \
		'Allow microphone for audio capture'
	defaults write `pwd`/$(GUI_BUNDLE)/Contents/Info.plist CFBundleIconFile icon.icns
	plutil -convert xml1 $(GUI_BUNDLE)/Contents/Info.plist

# aliases
bundle: $(BUNDLE)
gui-bundle: $(GUI_BUNDLE)

## @todo Add correct dependencies (using $(GUI_BUNDLE)/Contents/libs causes
## uv-qt rebuild which breaks fixed links by macdeployqt)
osx-gui-dmg:
	[ ! -f UltraGrid.dmg ] || rm UltraGrid.dmg
	if [ $(MACOS_LEGACY) = yes ]; then \
		LEGACY_README="-srcdir $(srcdir)/data/template/macOS-legacy/README.html"; \
	fi; \
	hdiutil create -fs HFS+ -volname ULTRAGRID -srcdir $(GUI_BUNDLE) \
		$$LEGACY_README -format UDZO -imagekey zlib-level=9 -o UltraGrid.dmg

@TARGETS@

install: all
	$(INSTALL) -d -m 755 $(DESTDIR)$(bindir)
	$(INSTALL) -m 755 $(TARGET) $(REFLECTOR_TARGET) $(DESTDIR)$(bindir)
	$(INSTALL) -m 755 $(srcdir)/data/template/bin/* $(DESTDIR)$(bindir)
	if [ -f "$(GUI_TARGET)" ]; then \
		$(INSTALL) -m 755 $(GUI_TARGET) $(DESTDIR)$(bindir);\
		$(INSTALL) -d -m 755 $(DESTDIR)$(datadir)/applications;\
		$(INSTALL) -m 644 $(srcdir)/data/uv-qt.desktop $(DESTDIR)$(datadir)/applications;\
		$(INSTALL) -d -m 755 $(DESTDIR)$(datadir)/metainfo;\
		$(INSTALL) -m 644 $(srcdir)/data/cz.cesnet.ultragrid.appdata.xml $(DESTDIR)$(datadir)/metainfo;\
		$(INSTALL) -d -m 755 $(DESTDIR)$(datadir)/pixmaps;\
		$(INSTALL) -m 644 $(srcdir)/data/ultragrid.png $(DESTDIR)$(datadir)/pixmaps;\
	fi
	if [ -n "@MODULES@" ]; then\
		$(INSTALL) -d -m 755 $(DESTDIR)$(libdir)/ultragrid;\
		$(INSTALL) -m 755 @MODULES@ $(DESTDIR)$(libdir)/ultragrid;\
	fi
	$(INSTALL) -d -m 755 $(DESTDIR)$(docdir)
	$(CP) $(DOCS) $(DESTDIR)$(docdir)
	$(INSTALL) -m 644 $(srcdir)/CONTRIBUTING.md $(srcdir)/COPYRIGHT $(srcdir)/INSTALL $(srcdir)/NEWS $(srcdir)/README.md $(DESTDIR)$(docdir)
	$(INSTALL) -d -m 755 $(DESTDIR)$(man1dir)
	if [ -n '@MANPAGES@' ]; then $(INSTALL) -m 644 @MANPAGES@ $(DESTDIR)$(man1dir); fi
	if [ -n '@DLL_LIBS@' ]; then  $(INSTALL) -m 644 @DLL_LIBS@ $(DESTDIR)$(bindir); fi
	$(INSTALL) -d -m 755 "$(DESTDIR)$(datadir)/ultragrid"
	$(INSTALL) -m 644 "$(srcdir)"/share/ultragrid/TimGM6mb_but_fixed__piano_.sf3 "$(DESTDIR)$(datadir)/ultragrid"
	if [ -n "@VULKAN@" ]; then\
		$(INSTALL) -D -m 644 "$(srcdir)/share/ultragrid/vulkan_shaders/"* -t "$(DESTDIR)$(datadir)/ultragrid/vulkan_shaders"; \
	fi
	if [ -f bin/uv.pdb ]; then\
		$(INSTALL) -m 644 bin/uv.pdb $(DESTDIR)$(bindir); \
	fi

uninstall:
	$(RM) $(DESTDIR)$(bindir)/uv
	$(RM) $(DESTDIR)$(bindir)/hd-rum-transcode
	for n in $(srcdir)/data/template/bin/*; do $(RM) $(DESTDIR)$(bindir)/`basename $$n`; done;
	if [ -n "@MODULES@" ]; then for n in @MODULES@; do $(RM) $(DESTDIR)$(libdir)/ultragrid/`basename $$n`; done; fi
	for n in $(DOCS); do $(RM) $(DESTDIR)$(docdir)/`basename $$n`; done;
	$(RM) $(DESTDIR)$(docdir)/CONTRIBUTING.md $(DESTDIR)$(docdir)/COPYRIGHT $(DESTDIR)$(docdir)/INSTALL $(DESTDIR)$(docdir)/NEWS $(DESTDIR)$(docdir)/README.md
	rmdir $(DESTDIR)$(docdir)
	$(RM) $(DESTDIR)$(man1dir)/uv.1 $(DESTDIR)$(man1dir)/hd-rum-transcode.1
	if [ -f "$(GUI_TARGET)" ]; then \
		$(RM) $(DESTDIR)$(bindir)/`basename $(GUI_TARGET)`;\
		$(RM) $(DESTDIR)$(datadir)/applications/uv-qt.desktop;\
		$(RM) $(DESTDIR)$(datadir)/metainfo/cz.cesnet.ultragrid.appdata.xml;\
		$(RM) $(DESTDIR)$(datadir)/pixmaps/ultragrid.png;\
	fi
	$(RM) $(DESTDIR)$(datarootdir)/ultragrid/TimGM6mb_but_fixed__piano_.sf3
	if [ -n "@VULKAN@" ]; then\
		$(RM) "$(DESTDIR)$(datadir)/ultragrid/vulkan_shaders/"*;\
		rmdir $(DESTDIR)$(datadir)/ultragrid/vulkan_shaders;\
		rmdir $(DESTDIR)$(datadir)/ultragrid;\
	fi
	$(RM) $(DESTDIR)$(bindir)/uv.pdb

# vim: set noexpandtab
