AR            = ar
RANLIB        = ranlib
CP            = cp
CC            = @CC@
CXX           = @CXX@
QMAKE         = @QMAKE@
LINKER        = @LINKER@
COMMON_FLAGS  = -g @DEFS@ -DPATH_PREFIX=@prefix@ -DLIB_DIR=@libdir@ -Wall -Wextra -Wpointer-arith -msse2
CFLAGS        = @CFLAGS@ @X_CFLAGS@ $(COMMON_FLAGS) -D_GNU_SOURCE
CPPFLAGS      = @CPPFLAGS@ -D_GNU_SOURCE
CXXFLAGS      = @CXXFLAGS@ $(COMMON_FLAGS) -D_GNU_SOURCE
MKDIR_P       = mkdir -p
NVCCFLAGS     = @NVCCFLAGS@ -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_35,code=compute_35
LDFLAGS      =  @LDFLAGS@
LIBS         += @LIBS@ @JACK_TRANS_LIB@ @MATHLIBS@ @COREAUDIO_LIB@ \
		-lm -pthread
INC           = -Isrc -I$(srcdir) -I$(srcdir)/src -I$(srcdir)/test @DVS_INC@ @DELTACAST_INC@ @QUICKTIME_INC@ @RTSP_INC@ @DECKLINK_INC@ @PORTAUDIO_INC@ \
                @GL_INC@ @SAGE_INC@ @JACK_TRANS_INC@ @JPEG_INC@ @COREAUDIO_INC@ @SPEEX_INC@ \
		@BLUEFISH444_INC@ @CURSES_CFLAGS@ @CUDA_INC@
DECKLINK_PATH = @DECKLINK_PATH@
EXEEXT        = @EXEEXT@
TARGET        = bin/uv$(EXEEXT)
IMPORT_C_TARGET = bin/import_control_keyboard$(EXEEXT)
SWITCHER_TARGET = bin/switcher_control_keyboard$(EXEEXT)
PERF          = bin/uv_perf
BUNDLE        = uv.app
DXT_GLSL_CFLAGS = @DXT_GLSL_CFLAGS@
NVCC	      = @NVCC@
DOCS 	      = README REPORTING-BUGS

GUI_TARGET    = @GUI_TARGET@
REFLECTOR_TARGET = bin/hd-rum-transcode$(EXEEXT)

PREFIX = @prefix@
prefix = $(PREFIX)
exec_prefix = @exec_prefix@
INSTALL = @INSTALL@
bindir = @bindir@
libdir = @libdir@
datadir = @datadir@
docdir = @docdir@/ultragrid/
datarootdir = @datarootdir@
uv_datadir = @datadir@/ultragrid/
srcdir        = @srcdir@
VPATH         = @srcdir@

# autogenerated headers
GENERATED_HEADERS              = @GENERATED_HEADERS@

ALL_INCLUDES	= $(wildcard $(srcdir)/src/*.h $(srcdir)/src/*/*.h $(srcdir)/src/*/*/*.h)

OBJS	      = @OBJS@ \
	        src/blackmagic_common.o \
	        src/bitstream.o \
		src/control_socket.o \
		src/debug.o \
		src/host.o \
		src/messaging.o \
		src/perf.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/ptime.o \
		src/rtp/net_udp.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/none.o \
		src/audio/playback/sdi.o \
		src/audio/resampler.o \
		src/audio/utils.o \
		src/audio/wav_reader.o \
		@COREAUDIO_OBJ@ \
		@JACK_TRANS_OBJ@ \
		@SPEEX_OBJ@ \
		src/capture_filter.o \
		src/capture_filter/blank.o \
		src/capture_filter/every.o \
		src/capture_filter/logo.o \
		src/capture_filter/none.o \
		src/capture_filter/scale.o \
		src/compat/drand48.o \
		src/compat/gettimeofday.o \
		src/compat/platform_semaphore.o \
		src/compat/platform_spin.o \
		src/compat/platform_time.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/ihdtv/ihdtv.o \
		src/module.o \
		src/rtsp/rtsp_utils.o \
		src/stats.o \
		src/utils/config_file.o \
		src/utils/list.o \
		src/lib_common.o \
		src/utils/synchronized_queue.o \
		src/utils/misc.o \
		src/utils/packet_counter.o \
		src/utils/resource_manager.o \
		src/utils/ring_buffer.o \
		src/utils/timed_message.o \
		src/utils/vf_split.o \
		src/utils/wait_obj.o \
		src/utils/worker.o \
		src/video.o \
		src/video_frame.o \
		src/video_codec.o \
		src/video_capture.o \
		src/video_capture/aggregate.o \
		src/video_capture/import.o \
		src/video_capture/null.o \
		src/video_capture/switcher.o \
		src/video_compress.o \
		src/video_compress/none.o \
		src/video_decompress.o \
		src/video_decompress/null.o \
		src/video_display.o \
		src/video_display/aggregate.o \
		src/video_display/null.o \
		src/video_display/pipe.o \
		src/video_display/proxy.o \
		src/video_export.o \
		src/video_rxtx.o \
		src/video_rxtx/ihdtv.o \
		src/video_rxtx/rtp.o \
		src/video_rxtx/sage.o \
		src/video_rxtx/ultragrid_rtp.o \
		src/vo_postprocess.o \
		src/vo_postprocess/deinterlace.o \
		src/vo_postprocess/3d-interlaced.o \
		src/vo_postprocess/interlace.o \
		src/vo_postprocess/double-framerate.o \
		src/vo_postprocess/split.o \
		ldgm-coding/ldgm-session-cpu.o \
		ldgm-coding/ldgm-session.o \
		ldgm-coding/tanner.o \
		ldgm-coding/matrix-gen/matrix-generator.o \
		ldgm-coding/matrix-gen/ldpc-matrix.o \

ULTRAGRID_OBJS = src/main.o \

REFLECTOR_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

IMPORT_CONTROL_KEYBOARD_OBJS = src/import_control_keyboard.o
SWITCHER_CONTROL_KEYBOARD_OBJS = src/switcher_control_keyboard.o

# -------------------------------------------------------------------------------------------------
all: src/dir-stamp $(TARGET) $(GUI_TARGET) $(IMPORT_C_TARGET) $(SWITCHER_TARGET) $(REFLECTOR_TARGET) modules ag-plugins configure-messages

src/dir-stamp:
	${MKDIR_P} src src/audio src/audio/capture src/audio/codec src/audio/playback src/capture_filter src/compat src/crypto src/hd-rum-translator src/ihdtv src/rtp src/rtsp src/utils src/video_capture src/video_compress src/video_decompress src/video_display src/video_rxtx src/vo_postprocess ag_plugin bin cuda_dxt dxt_compress ldgm-coding ldgm-coding/matrix-gen lib lib/ultragrid
	touch $@

modules: @LIB_TARGETS@

$(TARGET): $(OBJS) $(ULTRAGRID_OBJS) $(GENERATED_HEADERS)
	$(LINKER) $(LDFLAGS) $(OBJS) $(ULTRAGRID_OBJS) $(LIBS) -o $(TARGET)
	if [ -n "@DLL_LIBS@" ]; then $(INSTALL) -m 644 @DLL_LIBS@ bin; fi

$(IMPORT_C_TARGET): $(IMPORT_CONTROL_KEYBOARD_OBJS)
	$(LINKER) $(LDFLAGS) $(IMPORT_CONTROL_KEYBOARD_OBJS) @CURSES_LIBS@ @NET_LIBS@ -o $@

$(SWITCHER_TARGET): $(SWITCHER_CONTROL_KEYBOARD_OBJS)
	$(LINKER) $(LDFLAGS) $(SWITCHER_CONTROL_KEYBOARD_OBJS) @CURSES_LIBS@ @NET_LIBS@ -o $@ -pthread

$(REFLECTOR_TARGET): $(OBJS) $(GENERATED_HEADERS) $(REFLECTOR_OBJS)
	$(LINKER) $(LDFLAGS) $(OBJS) $(REFLECTOR_OBJS) $(LIBS) -o $@

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

%.o: %.cpp $(ALL_INCLUDES)
	$(CXX) $(CXXFLAGS) $(INC) -c $< -o $@

%.o: %.m $(ALL_INCLUDES)
	$(CXX) $(CFLAGS) $(INC) -c $< -o $@

%.o: %.mm $(ALL_INCLUDES)
	$(CXX) -x objective-c++ -std=c++11 $(CXXFLAGS) $(INC) -c $< -o $@

# Set suffix for CUDA files
.SUFFIXES: .cu

# Pattern rule for compiling CUDA files
%.cu.o: %.cu $(ALL_INCLUDES)
	"$(NVCC)" $(NVCCFLAGS) $(INC) -c $< -o $@
%.cu.lib: %.cu $(ALL_INCLUDES)
	"$(NVCC)" $(NVCCFLAGS) -DEXPORT_DLL_SYMBOLS $(INC) --shared $< -o $<.dll

# Important for this target is inclusion of cuda_wrapper that has patched cuda_runtime.h header (wrapper)
ldgm-coding/ldgm-session-gpu.o: ldgm-coding/ldgm-session-gpu.cpp $(ALL_INCLUDES)
	$(CXX) $(CXXFLAGS) -Isrc/cuda_wrapper -DEXPORT_DLL_SYMBOLS $(INC) -c $< -o $@


SPEEX_FLAGS=-Wno-sign-compare -Wno-unused-parameter -Wno-bad-function-cast -Wno-missing-prototypes -Wno-missing-declarations
src/audio/resample.o:
	$(CC) $(CFLAGS) $(SPEEX_FLAGS) $(INC) -DEXPORT="" -DRANDOM_PREFIX=speex -DFLOATING_POINT -DOUTSIDE_SPEEX -I. -I $(srcdir)/speex-1.2rc1/include/speex -Iinclude -fvisibility=hidden  -c $(srcdir)/speex-1.2rc1/libspeex/resample.c  -fPIC -DPIC -o $@

src/audio/preprocess.o:
	$(CC) $(CFLAGS) $(SPEEX_FLAGS) $(INC) -DEXPORT="" -DRANDOM_PREFIX=speex -DFLOATING_POINT -DOUTSIDE_SPEEX -I. -I $(srcdir)/speex-1.2rc1/include/speex -Iinclude -fvisibility=hidden  -c $(srcdir)/speex-1.2rc1/libspeex/preprocess.c  -fPIC -DPIC -o $@

src/audio/filterbank.o:
	$(CC) $(CFLAGS) $(SPEEX_FLAGS) $(INC) -DEXPORT="" -DRANDOM_PREFIX=speex -DFLOATING_POINT -DOUTSIDE_SPEEX -I. -I $(srcdir)/speex-1.2rc1/include/speex -I/include -fvisibility=hidden  -c $(srcdir)/speex-1.2rc1/libspeex/filterbank.c  -fPIC -DPIC -o $@

src/audio/fftwrap.o:
	$(CC) $(CFLAGS) $(SPEEX_FLAGS) $(INC) -DUSE_SMALLFT -DEXPORT="" -DRANDOM_PREFIX=speex -DFLOATING_POINT -DOUTSIDE_SPEEX -I. -I $(srcdir)/speex-1.2rc1/include/speex -Iinclude -fvisibility=hidden  -c $(srcdir)/speex-1.2rc1/libspeex/fftwrap.c  -fPIC -DPIC -o $@

src/audio/smallft.o:
	$(CC) $(CFLAGS) $(SPEEX_FLAGS) $(INC) -DEXPORT="" -DRANDOM_PREFIX=speex -DFLOATING_POINT -DOUTSIDE_SPEEX -I. -I $(srcdir)/speex-1.2rc1/include/speex -Iinclude -fvisibility=hidden  -c $(srcdir)/speex-1.2rc1/libspeex/smallft.c  -fPIC -DPIC -o $@

src/audio/mdf.o:
	$(CC) $(CFLAGS) $(SPEEX_FLAGS) $(INC) -DEXPORT="" -DRANDOM_PREFIX=speex -DFLOATING_POINT -DOUTSIDE_SPEEX -I. -I $(srcdir)/speex-1.2rc1/include/speex -Iinclude -fvisibility=hidden  -c $(srcdir)/speex-1.2rc1/libspeex/mdf.c  -fPIC -DPIC -o $@

src/audio/capture/coreaudio.o: src/audio/capture/coreaudio.c $(ALL_INCLUDES)
	$(CC) $(CFLAGS) -Wno-error=deprecated-declarations $(INC) -c $< -o $@

src/audio/playback/coreaudio.o: src/audio/playback/coreaudio.c $(ALL_INCLUDES)
	$(CC) $(CFLAGS) -Wno-error=deprecated-declarations $(INC) -c $< -o $@

src/video_display/quicktime.o: src/video_display/quicktime.c $(ALL_INCLUDES)
	$(CC) $(CFLAGS) -Wno-error=deprecated-declarations $(INC) -c $< -o $@

src/video_capture/DeckLinkAPIDispatch.o: $(DECKLINK_PATH)/DeckLinkAPIDispatch.cpp
	$(CXX) $(CXXFLAGS) -c $(INC) -o src/video_capture/DeckLinkAPIDispatch.o $(DECKLINK_PATH)/DeckLinkAPIDispatch.cpp

src/video_capture/DeckLinkAPI_i.o: $(DECKLINK_PATH)/DeckLinkAPI_i.c
	$(CC) $(CFLAGS) -c $(INC) -o src/video_capture/DeckLinkAPI_i.o $(DECKLINK_PATH)/DeckLinkAPI_i.c

dxt_compress/dxt_encoder.o: dxt_compress/dxt_encoder.c dxt_compress/dxt_glsl.h
	$(CC) $(CFLAGS) $(INC) $(DXT_GLSL_CFLAGS) $< -c -o $@

dxt_compress/dxt_decoder.o: dxt_compress/dxt_decoder.c dxt_compress/dxt_glsl.h
	$(CC) $(CFLAGS) $(INC) $(DXT_GLSL_CFLAGS) $< -c -o $@

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 "/**" > $@
	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 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 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  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  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  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  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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 dxt_compress/rgba_to_yuv422_vp.glsl | sed 's/\(.*\)/    \"\1\\n\"/' >> $@
	#echo ";" >> $@

gui/QT/uv-qt:
	cd gui/QT; $(QMAKE) ; make -j; cd -

# -------------------------------------------------------------------------------------------------
TEST_OBJS = test/test_bitstream.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 \
	    test/run_tests.o

test/run_tests: $(TEST_OBJS) $(OBJS)
	$(LINKER) $(CFLAGS) $(TEST_OBJS) $(OBJS) $(LIBS) -o test/run_tests

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

configure-messages:
	@printf "@POST_COMPILE_MSG@"


tests: test/run_tests
	@test/run_tests

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

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

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

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

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

# -------------------------------------------------------------------------------------------------
clean:
	-rm -f $(OBJS) $(GENERAED_HEADERS) $(ULTRAGRID_OBJS) $(TARGET) src/version.h
	-rm -f $(TEST_OBJS) test/run_tests
	-rm -f ag_plugin/uvReceiverService.zip ag_plugin/uvSenderService.zip
	-rm -rf $(BUNDLE)
	-rm -rf $(PERF) src/uv_perf.o
	-rm -rf $(REFLECTOR_TARGET) $(REFLECTOR_OBJS)
	-rm -rf @LIB_OBJS@ @LIB_TARGETS@ @LIB_GENERATED_HEADERS@ @X_OBJ@ @GL_COMMON_OBJ@ @CUDA_COMMON_OBJ@
	-rm -rf $(IMPORT_C_TARGET) $(SWITCHER_TARGET)
	if [ -f "gui/QT/Makefile" ]; then make -C gui/QT/ clean; fi

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

etags:
	etags src/*.[ch] src/*/*.[ch]

ctags:
	ctags src/*.[ch] src/*/*.[ch]

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

bundle-nolib: all
	mkdir -p $(BUNDLE)/Contents/MacOS
	cp $(TARGET) $(BUNDLE)/Contents/MacOS/
	cp -r data/MacOS-bundle-template/* $(BUNDLE)/

bundle: bundle-nolib
	@if `which dylibbundler`; then \
		dylibbundler -of -p @executable_path/../libs/ -od -b -x $(BUNDLE)/Contents/MacOS/uv -d $(BUNDLE)/Contents/libs/; \
		for n in $(BUNDLE)/Contents/MacOS/uv $(BUNDLE)/Contents/libs/*; do echo $$n; while $$( otool -L $$n | grep -q @rpath) ; \
			do NAME=$$( otool -L $$n | grep @rpath| tail -n 1|cut -f 1 -d\ ); install_name_tool -change $$NAME $${NAME/@rpath/@executable_path\/..\/libs} $$n ; done; done ; \
	else \
		echo "WARNING: Cannot find dylibbundler. We cannot put libraries into a bundle so it probably won't be portable."; \
	fi
	$(MKDIR_P) $(BUNDLE)/Contents/Frameworks
	$(CP) -r redists/VideoMasterHD.framework $(BUNDLE)/Contents/Frameworks
	$(CP) -r redists/VideoMasterHD_Audio.framework $(BUNDLE)/Contents/Frameworks
	install_name_tool -change /Library/Frameworks/VideoMasterHD_Audio.framework/Versions/A/VideoMasterHD_Audio @executable_path/../Frameworks/VideoMasterHD_Audio.framework/Versions/A/VideoMasterHD_Audio $(BUNDLE)/Contents/MacOS/uv
	install_name_tool -change /Library/Frameworks/VideoMasterHD.framework/Versions/A/VideoMasterHD @executable_path/../Frameworks/VideoMasterHD.framework/Versions/A/VideoMasterHD $(BUNDLE)/Contents/MacOS/uv
	#echo -n "UltraGrid" > $(BUNDLE)/Contents/PkgInfo

osx-gui-dmg: bundle
	cd 'gui/UltraGrid GUI'; \
		xcodebuild -alltargets -arch x86_64 -sdk @OSX_SDK@; \
		cp ../../uv.app/Contents/MacOS/uv build/Release/UltraGrid\ GUI.app/Contents/MacOS/; \
		mkdir build/Release/UltraGrid\ GUI.app/Contents/libs; \
		cp ../../uv.app/Contents/libs/* build/Release/UltraGrid\ GUI.app/Contents/libs; \
		MBYTES=`du -mc build/Release/UltraGrid\ GUI.app | tail -n1 | cut -f1`; \
		MBYTES=$$(($$MBYTES+2)); \
		[ ! -f UltraGrid.dmg ] || rm UltraGrid.dmg; \
		hdiutil create -megabytes $$MBYTES -fs MS-DOS -volname ULTRAGRID UltraGrid.dmg; \
		MOUNT_PT=`hdiutil mount UltraGrid.dmg | tail -n1 | cut -f3`; \
		mv "build/Release/UltraGrid GUI.app" "$$MOUNT_PT"; \
		hdiutil eject "$$MOUNT_PT"; \


perf: src/tv.o src/crypto/random.o
	$(CC) $(CFLAGS) -DPERF src/uv_perf.c src/crypto/random.o src/tv.o -o $(PERF)

sdl: @SDL_LIB_TARGET@
gl: @GL_LIB_TARGET@
testcard: @TESTCARD_LIB_TARGET@
testcard2: @TESTCARD2_LIB_TARGET@
decklink: @DECKLINK_PLAY_LIB_TARGET@ @DECKLINK_CAP_LIB_TARGET@ @DECKLINK_SOUND_PLAY_LIB_TARGET@
quad: @QUAD_LIB_TARGET@
deltacast: @DELTACAST_DISP_LIB_TARGET@ @DELTACAST_CAP_LIB_TARGET@
dvs: @DVS_CAP_LIB_TARGET@ @DVS_DISP_LIB_TARGET@
screen: @SCREEN_CAP_LIB_TARGET@
sage: @SAGE_LIB_TARGET@
rtdxt: @RTDXT_COMPRESS_LIB_TARGET@ @RTDXT_DECOMPRESS_LIB_TARGET@
uyvy: @UYVY_COMPRESS_LIB_TARGET@
jpeg: @JPEG_DECOMPRESS_LIB_TARGET@ @JPEG_COMPRESS_LIB_TARGET@
alsa: @ALSA_PLAY_LIB_TARGET@ @ALSA_CAP_LIB_TARGET@
portaudio: @PORTAUDIO_PLAY_LIB_TARGET@ @PORTAUDIO_CAP_LIB_TARGET@
jack: @JACK_CAP_LIB_TARGET@ @JACK_PLAY_LIB_TARGET@
scale: @SCALE_LIB_TARGET@
libavcodec: @LIBAVCODEC_DECOMPRESS_LIB_TARGET@ @LIBAVCODEC_COMPRESS_LIB_TARGET@ @LIBAVCODEC_AUDIO_CODEC_LIB_TARGET@
rtsp: @RTSP_LIB_TARGET@
rtsp_server: @RTSP_SERVER_LIB_TARGET@

@SDL_LIB_TARGET@: @SDL_OBJ@ @X_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,display_sdl.so.@video_display_abi_version@ $^ @SDL_LIBS@ -o $@

@GL_LIB_TARGET@: @GL_OBJ@ @X_OBJ@ @GL_COMMON_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,display_gl.so.@video_display_abi_version@ $^ @GL_LIB@ -o $@

@RTSP_LIB_TARGET@: @RTSP_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vidcap_rtsp.so.@video_capture_abi_version@ $^ @RTSP_LIBS@ -o $@
	
@TESTCARD_LIB_TARGET@: @TESTCARD_COMMON@ @TESTCARD_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vidcap_testcard.so.@video_capture_abi_version@ $^ @TESTCARD_LIB@ -o $@

@SWMIX_LIB_TARGET@: @GL_COMMON_OBJ@ @SWMIX_OBJ@ @X_OBJ@ src/host.o
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vidcap_swmix.so.@video_capture_abi_version@ $^ @SWMIX_LIB@ -o $@

@TESTCARD2_LIB_TARGET@: @TESTCARD_COMMON@ @TESTCARD2_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vidcap_testcard2.so.@video_capture_abi_version@ $^ @TESTCARD2_LIB@ -o $@

@DECKLINK_CAP_LIB_TARGET@: @DECKLINK_CAP_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vidcap_decklink.so.@video_capture_abi_version@ $^ @DECKLINK_LIB@ -o $@

@DECKLINK_DISP_LIB_TARGET@: src/video_capture/decklink.o @DECKLINK_DISP_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,display_decklink.so.@video_display_abi_version@ $^ @DECKLINK_LIB@ -o $@

@BLUEFISH444_CAP_LIB_TARGET@: @BLUEFISH444_CAP_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vidcap_bluefish444.so.@video_capture_abi_version@ $^ @BLUEFISH444_LIB@ -o $@

@BLUEFISH444_DISP_LIB_TARGET@: @BLUEFISH444_DISP_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,display_bluefish444.so.@video_display_abi_version@ $^ @BLUEFISH444_LIB@ -o $@

@DELTACAST_CAP_LIB_TARGET@: @DELTACAST_DISP_OBJ@ @DELTACAST_CAP_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vidcap_deltacast.so.@video_capture_abi_version@ $^ @DELTACAST_LIB@ -o $@

@DELTACAST_DISP_LIB_TARGET@: @DELTACAST_DISP_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,display_deltacast.so.@video_display_abi_version@ $^ @DELTACAST_LIB@ -o $@

@DVS_CAP_LIB_TARGET@: src/video_display/dvs.o @DVS_CAP_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vidcap_dvs.so.@video_capture_abi_version@ $^ @DVS_LIB@ -o $@

@DVS_DISP_LIB_TARGET@: @DVS_DISP_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,display_dvs.so.@video_display_abi_version@ $^ @DVS_LIB@ -o $@

@SCREEN_CAP_LIB_TARGET@: @SCREEN_CAP_OBJ@ @X_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vidcap_screen.so.@video_capture_abi_version@ $^ @SCREEN_CAP_LIB@ -o $@

@SAGE_LIB_TARGET@: @SAGE_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,display_sage.so.@video_display_abi_version@ $^ @SAGE_LIB@ -o $@

@RTDXT_COMPRESS_LIB_TARGET@: @GL_COMMON_OBJ@ @X_OBJ@ @RTDXT_COMMON_OBJ@ @RTDXT_COMPRESS_OBJ@ @RTDXT_COMMON_HEADERS@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vcompress_rtdxt.so.@video_compress_abi_version@ @GL_COMMON_OBJ@ @X_OBJ@ @RTDXT_COMMON_OBJ@ @RTDXT_COMPRESS_OBJ@ @RTDXT_LIB@ -o $@

@UYVY_COMPRESS_LIB_TARGET@: @GL_COMMON_OBJ@ @X_OBJ@ @UYVY_COMPRESS_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vcompress_uyvy.so.@video_compress_abi_version@ @GL_COMMON_OBJ@ @X_OBJ@ @UYVY_COMPRESS_OBJ@ -o $@

@LIBAVCODEC_COMPRESS_LIB_TARGET@: @LIBAVCODEC_COMPRESS_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vcompress_libavcodec.so.@video_compress_abi_version@ $^ @LIBAVCODEC_LIBS@ -o $@

@LIBAVCODEC_DECOMPRESS_LIB_TARGET@: @LIBAVCODEC_DECOMPRESS_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vdecompress_libavcodec.so.@video_decompress_abi_version@ $^ @LIBAVCODEC_LIBS@ -o $@

@LIBAVCODEC_AUDIO_CODEC_LIB_TARGET@: @LIBAVCODEC_AUDIO_CODEC_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,acodec_libavcodec.so.@audio_codec_abi_version@ $^ @LIBAVCODEC_LIBS@ -o $@

@JPEG_COMPRESS_LIB_TARGET@: @JPEG_COMPRESS_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vcompress_jpeg.so.@video_compress_abi_version@ $^ @JPEG_LIB@ -o $@

@CUDA_DXT_COMPRESS_LIB_TARGET@: @CUDA_DXT_OBJ@ @CUDA_COMMON_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vcompress_cuda_dxt.so.@video_compress_abi_version@ $^ @CUDA_DXT_LIB@ -o $@

@RTDXT_DECOMPRESS_LIB_TARGET@: @GL_COMMON_OBJ@ @X_OBJ@ @RTDXT_COMMON_OBJ@ @RTDXT_DECOMPRESS_OBJ@ @RTDXT_COMMON_HEADERS@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vdecompress_rtdxt.so.@video_decompress_abi_version@  @GL_COMMON_OBJ@ @X_OBJ@ @RTDXT_COMMON_OBJ@ @RTDXT_DECOMPRESS_OBJ@ @RTDXT_LIB@ -o $@

@JPEG_DECOMPRESS_LIB_TARGET@: @JPEG_DECOMPRESS_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vdecompress_jpeg.so.@video_decompress_abi_version@ $^ @JPEG_LIB@ -o $@

@JPEG_TO_DXT_DECOMPRESS_LIB_TARGET@: @JPEG_TO_DXT_OBJ@ @JPEG_DECOMPRESS_OBJ@ @CUDA_COMMON_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vdecompress_jpeg_to_dxt.so.@video_decompress_abi_version@ $^ @JPEG_TO_DXT_LIB@ -o $@


@ALSA_PLAY_LIB_TARGET@: @ALSA_PLAY_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,aplay_alsa.so.@audio_playback_abi_version@ $^ @ALSA_LIB@ -o $@

@ALSA_CAP_LIB_TARGET@: @ALSA_CAP_OBJ@ src/audio/playback/alsa.o
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,acap_alsa.so.@audio_capture_abi_version@ $^ @ALSA_LIB@ -o $@

@PORTAUDIO_PLAY_LIB_TARGET@: @PORTAUDIO_PLAY_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,aplay_portaudio.so.@audio_playback_abi_version@ $^ @PORTAUDIO_LIB@ -o $@

@PORTAUDIO_CAP_LIB_TARGET@: @PORTAUDIO_CAP_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,acap_portaudio.so.@audio_capture_abi_version@ $^ @PORTAUDIO_LIB@ -o $@

@JACK_CAP_LIB_TARGET@: @JACK_CAP_OBJ@ @SPEEX_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,acap_jack.so.@audio_capture_abi_version@ $^ @JACK_LIB@ -o $@

@JACK_PLAY_LIB_TARGET@: @JACK_PLAY_OBJ@ @SPEEX_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,aplay_jack.so.@audio_playback_abi_version@ $^ @JACK_LIB@ -o $@

@DECKLINK_SOUND_PLAY_LIB_TARGET@: @DECKLINK_SOUND_PLAY_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,aplay_jack.so.@audio_playback_abi_version@ $^ @DECKLINK_LIB@ -o $@

@SCALE_LIB_TARGET@: @SCALE_OBJ@ @GL_COMMON_OBJ@ @X_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vo_pp_scale.so.@vo_pp_abi_version@ $^ @SCALE_LIB@ -o $@

@V4L2_LIB_TARGET@: @V4L2_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,vidcap_v4l2.so.@video_capture_abi_version@ $^ @V4L2_LIBS@ -o $@

@RTSP_SERVER_LIB_TARGET@: @RTSP_SERVER_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,video_rxtx_h264.so.@video_rxtx_abi_version@ $^ @RTSP_SERVER_LIBS@ -o $@

@VCAPTURE_FILTER_RESIZE_LIB_TARGET@: @RESIZE_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,module_vcapfilter_resize.so $^ @RESIZE_LIBS@ -o $@

@OPENSSL_LIB_TARGET@: @OPENSSL_OBJ@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,module_openssl.so $^ @CRYPTO_LIBS@ -o $@

@LDGM_GPU_LIB_TARGET@: @LDGM_GPU_OBJS@
	$(LINKER) $(LDFLAGS) -shared -Wl,-soname,module_ldgm_gpu.so $^ @LDGM_GPU_LIBS@ -o $@

install: all
	$(INSTALL) -d -m 755 $(DESTDIR)/$(bindir)
	$(INSTALL) -m 755 bin/uv $(DESTDIR)/$(bindir)
	$(INSTALL) -m 755 $(REFLECTOR_TARGET) $(DESTDIR)/$(bindir)
	if [ -f "$(GUI_TARGET)" ]; then $(INSTALL) -m 755 $(GUI_TARGET) $(DESTDIR)/$(bindir); fi
	if [ -n "@LIB_TARGETS@" ]; then\
		$(INSTALL) -d -m 755 $(DESTDIR)/$(libdir)/ultragrid;\
		$(INSTALL) -m 644 @LIB_TARGETS@ $(DESTDIR)/$(libdir)/ultragrid/;\
	fi
	$(INSTALL) -d -m 755 $(DESTDIR)/$(uv_datadir)
	$(INSTALL) -m 755 data/ultragrid-bugreport-collect.sh $(DESTDIR)/$(uv_datadir)
	$(INSTALL) -d -m 755 $(DESTDIR)/$(docdir)
	$(INSTALL) -m 644 $(DOCS) $(DESTDIR)/$(docdir)
	$(CP) @COPYRIGHT@ $(DESTDIR)/$(docdir)/COPYRIGHT
	if [ -n "@DLL_LIBS@" ]; then  $(INSTALL) -m 644 @DLL_LIBS@ $(DESTDIR)/$(bindir); fi

uninstall:
	$(RM) $(DESTDIR)/$(bindir)/uv
	$(RM) $(DESTDIR)/$(bindir)/hd-rum-transcode
	if [ -n "@LIB_TARGETS@" ]; then for n in @LIB_TARGETS@; do $(RM) $(DESTDIR)/$(libdir)/ultragrid/`basename $$n`; done; fi
	$(RM) $(DESTDIR)/$(uv_datadir)/ultragrid-bugreport-collect.sh
	for n in $(DOCS); do $(RM) $(DESTDIR)/$(docdir)/$$n; done; fi
	$(RM) $(DESTDIR)/$(docdir)/@COPYRIGHT@
	$(RM) $(DESTDIR)/$(uv_datadir)/ultragrid-bugreport-collect.sh
	if [ -f "$(GUI_TARGET)" ]; then $(RM) $(DESTDIR)/$(bindir)/`basename $(GUI_TARGET)`; fi
