HELPER_MK_FILE := ../MakefileHelpers/common.mk
include ${HELPER_MK_FILE}

DEBUG_TARGET := bin/Debug/libSFMBackend.a
RELEASE_TARGET := bin/Release/libSFMBackend.a

DEBUG_OBJ_DIR := obj/Debug/
RELEASE_OBJ_DIR := obj/Release/

DEBUG_DEP_DIR := dep/Debug/
RELEASE_DEP_DIR := dep/Release/

CUDA_OUTPUT_DIR := kernels/Release/
CUDA_DEP_DIR := cudep/


CFLAGS := ${CUDA_CFLAGS} ${IMAGE_MAGICK_CFLAGS} ${COMMON_FLAGS} ${BOOST_CFLAGS}

DEBUG_CFLAGS := ${CFLAGS} ${DEBUG_CFLAG_ADDITIONS}
RELEASE_CFLAGS := ${CFLAGS} ${RELEASE_CFLAG_ADDITIONS}


SRCS := \
	source/config/BundleAdjustmentConfig.cpp \
	source/config/FeatureExtractionConfig.cpp \
	source/config/FeatureMatchingConfig.cpp \
	source/config/SfMConfig.cpp \
	source/cudaInterface/CudaSift.cpp \
	source/cudaInterface/HomographyImageResampler.cpp \
	source/cudaInterface/ImageMipchainBuilder.cpp \
	source/cudaInterface/InitialPatchGatherer.cpp \
	source/cudaInterface/InitialPatchOptimizer.cpp \
	source/cudaInterface/NewTrackObservationTester.cpp \
	source/cudaInterface/PackedPatchToPatchCacheTransfer.cpp \
	source/cudaInterface/PairwisePatchAligner.cpp \
	source/cudaInterface/PatchAtlas.cpp \
	source/cudaInterface/PatchCache.cpp \
	source/cudaInterface/SourceImageToPatchAtlasTransfer.cpp \
	source/cudaInterface/trackAlignmentOptimizer.cpp \
	source/cudaUtilities/BaseMemoryChunk.cpp \
	source/cudaUtilities/CudaCodeModule.cpp \
	source/cudaUtilities/CudaDevice.cpp \
	source/cudaUtilities/CudaDeviceContext.cpp \
	source/cudaUtilities/CudaDeviceMemory.cpp \
	source/cudaUtilities/CudaDriver.cpp \
	source/cudaUtilities/CudaKernel.cpp \
	source/cudaUtilities/cudaProfilingScope.cpp \
	source/cudaUtilities/CudaStream.cpp \
	source/cudaUtilities/CudaSurfaceReference.cpp \
	source/cudaUtilities/CudaTextureMemory.cpp \
	source/cudaUtilities/CudaTextureReference.cpp \
	source/SFM/BifocalRANSACFilter.cpp \
	source/SFM/Camera.cpp \
	source/SFM/DuplicateTrackRemoval.cpp \
	source/SFM/FeaturePointAlignmentEstimator.cpp \
	source/SFM/Frame.cpp \
	source/SFM/InternalCameraCalibration.cpp \
	source/SFM/ObservationGrid.cpp \
	source/SFM/TrackMatchingDescriptor.cpp \
	source/SFM/SFM.cpp \
	source/SFM/SiftDescriptorDB.cpp \
	source/SFM/Track.cpp \
	source/SFM/TrackObservation.cpp \
	source/SFM/ViewMatrixEstimator.cpp \
	source/SFM/BundleAdjustment/BundleAdjustment.cpp \
	source/SFM/BundleAdjustment/PFBundleAdjustment.cpp \
	source/SFM/BundleAdjustment/PFJacobi.cpp \
	source/SFM/BundleAdjustment/SingleTrackBundleAdjustment.cpp \
	source/tools/AlignedAllocator.cpp \
	source/tools/BlockSparseMatrix.cpp \
	source/tools/ChunkedSOA.cpp \
	source/tools/CPUStopWatch.cpp \
	source/tools/LinAlg.cpp \
	source/tools/PCVToolbox.cpp \
	source/tools/RasterImage.cpp \
	source/tools/SpectatorCamera.cpp \
	source/tools/SSEMath.cpp \
	source/tools/StackAllocator.cpp \
	source/tools/SvgComposer.cpp \
	source/tools/TaskScheduler.cpp

CUDA_SRCS := \
	source/cudaKernels/CudaSift.cu \
	source/cudaKernels/ImageResampling.cu

CUDA_FATBINS    := $(addprefix ${CUDA_OUTPUT_DIR}, $(notdir ${CUDA_SRCS:.cu=.fatbin} ))
CUDA_DEPS    := $(addprefix ${CUDA_DEP_DIR}, ${CUDA_SRCS:.cu=.dep} )
CUDA_XDEPS   := $(wildcard ${CUDA_DEPS}) 

ifneq (${CUDA_XDEPS},) 
include ${CUDA_XDEPS} 
endif 


include ../MakefileHelpers/buildLibrary.mk

.PHONY: kernels

kernels:: ${CUDA_FATBINS}

${CUDA_FATBINS}: ${CUDA_OUTPUT_DIR}%.fatbin: source/cudaKernels/%.cu ${CUDA_DEP_DIR}source/cudaKernels/%.dep 
	@echo -e ${bash_yellow}Compiling $<${bash_nocolor}
	@mkdir -p $(dir $@)
	@${NVCC} ${CUDA_NVCC_CFLAGS} -fatbin $< -o $@ 

${CUDA_DEPS}: ${CUDA_DEP_DIR}%.dep: %.cu Makefile ${HELPER_MK_FILE}
	@echo -e ${bash_yellow}Building deps for $<${bash_nocolor}
	@mkdir -p $(dir $@)
	@${NVCC} --generate-dependencies ${CUDA_NVCC_CFLAGS} $< > $@ 

clean::
	rm -f ${CUDA_FATBINS} ${CUDA_DEPS}
