ADD_SUBDIRECTORY(3rdparty)

CONFIGURE_FILE("lmmsconfig.h.in"        "${CMAKE_BINARY_DIR}/lmmsconfig.h")
CONFIGURE_FILE("lmmsversion.h.in"       "${CMAKE_BINARY_DIR}/lmmsversion.h")

SET(LMMS_SRCS "")
SET(LMMS_UIS "")

SET(CMAKE_AUTOMOC ON)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

# Enable C++11
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")

IF(LMMS_BUILD_APPLE)
	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
ENDIF()

ADD_SUBDIRECTORY(core)
ADD_SUBDIRECTORY(gui)
ADD_SUBDIRECTORY(tracks)

IF(QT5)
	QT5_WRAP_UI(LMMS_UI_OUT ${LMMS_UIS})
ELSE()
	QT4_WRAP_UI(LMMS_UI_OUT ${LMMS_UIS})
ENDIF()
INCLUDE_DIRECTORIES(
	"${CMAKE_CURRENT_BINARY_DIR}"
	"${CMAKE_BINARY_DIR}"
	"${CMAKE_BINARY_DIR}/include"
	"${CMAKE_SOURCE_DIR}"
	"${CMAKE_SOURCE_DIR}/include"
)

IF(WIN32)
	SET(WINRC "${CMAKE_BINARY_DIR}/lmmsrc.obj")
	ADD_CUSTOM_COMMAND(OUTPUT "${WINRC}"
				COMMAND "${WINDRES}"
					"-I\"${CMAKE_SOURCE_DIR}\""
					"-o\"${CMAKE_BINARY_DIR}/lmmsrc.obj\""
					"-i\"${CMAKE_BINARY_DIR}/lmms.rc\""
				DEPENDS "${CMAKE_BINARY_DIR}/lmms.rc")
ENDIF()

SET(lmms_EMBEDDED_RESOURCES "${CMAKE_SOURCE_DIR}/doc/AUTHORS" "${CMAKE_SOURCE_DIR}/LICENSE.txt" "${CONTRIBUTORS}")
SET(LMMS_ER_H "${CMAKE_CURRENT_BINARY_DIR}/embedded_resources.h")
ADD_CUSTOM_COMMAND(OUTPUT "${LMMS_ER_H}" COMMAND "${BIN2RES}" ARGS ${lmms_EMBEDDED_RESOURCES} > "${LMMS_ER_H}" DEPENDS bin2res)

# Paths relative to lmms executable
FILE(RELATIVE_PATH LIB_DIR_RELATIVE "/${BIN_DIR}" "/${LIB_DIR}")
FILE(RELATIVE_PATH PLUGIN_DIR_RELATIVE "/${BIN_DIR}" "/${PLUGIN_DIR}")
ADD_DEFINITIONS(-D'LIB_DIR="${LIB_DIR_RELATIVE}/"' -D'PLUGIN_DIR="${PLUGIN_DIR_RELATIVE}/"' ${PULSEAUDIO_DEFINITIONS} ${PORTAUDIO_DEFINITIONS})
INCLUDE_DIRECTORIES(
	${JACK_INCLUDE_DIRS}
	${SAMPLERATE_INCLUDE_DIRS}
	${SNDFILE_INCLUDE_DIRS}
	${SNDIO_INCLUDE_DIRS}
)

IF(NOT ("${SDL_INCLUDE_DIR}" STREQUAL ""))
	INCLUDE_DIRECTORIES("${SDL_INCLUDE_DIR}")
ENDIF()

IF(NOT ("${PORTAUDIO_INCLUDE_DIR}" STREQUAL ""))
	INCLUDE_DIRECTORIES("${PORTAUDIO_INCLUDE_DIR}")
ENDIF()

IF(NOT ("${PULSEAUDIO_INCLUDE_DIR}" STREQUAL ""))
	INCLUDE_DIRECTORIES("${PULSEAUDIO_INCLUDE_DIR}")
ENDIF()

IF(NOT ("${OGGVORBIS_INCLUDE_DIR}" STREQUAL ""))
	INCLUDE_DIRECTORIES("${OGGVORBIS_INCLUDE_DIR}")
ENDIF()

IF(NOT ("${LAME_INCLUDE_DIRS}" STREQUAL ""))
	INCLUDE_DIRECTORIES("${LAME_INCLUDE_DIRS}")
ENDIF()

# Use libraries in non-standard directories (e.g., another version of Qt)
IF(LMMS_BUILD_LINUX)
	LINK_LIBRARIES(-Wl,--enable-new-dtags)
	SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
ENDIF()
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# ADD_LIBRARY's OBJECT is only supported in CMake >=2.8.8
IF(CMAKE_VERSION VERSION_GREATER "2.8.7")

	ADD_LIBRARY(lmmsobjs OBJECT
		${LMMS_SRCS}
		${LMMS_INCLUDES}
		${LMMS_UI_OUT}
		${LMMS_ER_H}
	)
	ADD_EXECUTABLE(lmms
		core/main.cpp
		$<TARGET_OBJECTS:lmmsobjs>
		"${WINRC}"
	)

    SET(iwyu include-what-you-use -Xiwyu --mapping_file=/usr/share/include-what-you-use/qt5_4.imp)
    #SET_PROPERTY(TARGET lmmsobjs PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu})
ELSE()
	ADD_EXECUTABLE(lmms
		core/main.cpp
		${LMMS_SRCS}
		${LMMS_INCLUDES}
		${LMMS_UI_OUT}
		${LMMS_ER_H}
		"${WINRC}"
	)
ENDIF()

SET_DIRECTORY_PROPERTIES(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${LMMS_ER_H} ${LMMS_UI_OUT} lmmsconfig.h lmms.1.gz")

IF(LMMS_BUILD_WIN32)
	SET(EXTRA_LIBRARIES "-lwinmm")
ENDIF()

IF(LMMS_BUILD_APPLE)
	SET(EXTRA_LIBRARIES "-framework CoreMIDI -framework CoreFoundation")
ENDIF()

if(LMMS_HAVE_OSS AND LMMS_BUILD_OPENBSD)
    SET(EXTRA_LIBRARIES "-lossaudio")
endif()

IF(LMMS_BUILD_HAIKU)
	SET(EXTRA_LIBRARIES "-lnetwork")
ENDIF()

SET(LMMS_REQUIRED_LIBS ${LMMS_REQUIRED_LIBS}
	${CMAKE_THREAD_LIBS_INIT}
	${QT_LIBRARIES}
	${ASOUND_LIBRARY}
	${SDL_LIBRARY}
	${PORTAUDIO_LIBRARIES}
	${SOUNDIO_LIBRARY}
	${SNDIO_LIBRARIES}
	${PULSEAUDIO_LIBRARIES}
	${JACK_LIBRARIES}
	${OGGVORBIS_LIBRARIES}
	${LAME_LIBRARIES}
	${SAMPLERATE_LIBRARIES}
	${SNDFILE_LIBRARIES}
	${EXTRA_LIBRARIES}
	rpmalloc
)

# Expose required libs for tests binary
SET(LMMS_REQUIRED_LIBS ${LMMS_REQUIRED_LIBS} PARENT_SCOPE)

TARGET_LINK_LIBRARIES(lmms
	${LMMS_REQUIRED_LIBS}
)

FOREACH(LIB ${LMMS_REQUIRED_LIBS})
	IF(TARGET ${LIB})
	    GET_TARGET_PROPERTY(INCLUDE_DIRS ${LIB} INTERFACE_INCLUDE_DIRECTORIES)
	    if (INCLUDE_DIRS)
	        TARGET_INCLUDE_DIRECTORIES(lmmsobjs PRIVATE ${INCLUDE_DIRS})
	    ENDIF()
    ENDIF()
ENDFOREACH()


# Required libs for debug msys builds
IF(LMMS_BUILD_MSYS AND CMAKE_BUILD_TYPE STREQUAL "Debug")
	TARGET_LINK_LIBRARIES(lmms QtCore4 QtGui4 QtXml4)
ENDIF()

# Install
IF(LMMS_BUILD_WIN32)
	SET_TARGET_PROPERTIES(lmms PROPERTIES
		LINK_FLAGS "${LINK_FLAGS} -mwindows"
		ENABLE_EXPORTS ON
	)
	IF(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
		ADD_CUSTOM_COMMAND(TARGET lmms POST_BUILD COMMAND "${STRIP}" "$<TARGET_FILE:lmms>")
	ENDIF()

	INSTALL(TARGETS lmms RUNTIME DESTINATION "${BIN_DIR}")

	IF(QT5)
		INSTALL(FILES
					"${MINGW_PREFIX}/bin/Qt5Core.dll"
					"${MINGW_PREFIX}/bin/Qt5Gui.dll"
					"${MINGW_PREFIX}/bin/Qt5Widgets.dll"
					"${MINGW_PREFIX}/bin/Qt5Xml.dll"
				DESTINATION .)
		INSTALL(FILES
					"${MINGW_PREFIX}/lib/qt5/plugins/platforms/qwindows.dll"
				DESTINATION ./platforms)
	ELSE()
		INSTALL(FILES
					"${MINGW_PREFIX}/bin/QtCore4.dll"
					"${MINGW_PREFIX}/bin/QtGui4.dll"
					"${MINGW_PREFIX}/bin/QtSvg4.dll"
					"${MINGW_PREFIX}/bin/QtXml4.dll"
				DESTINATION .)
	ENDIF()

	INSTALL(FILES
					"${MINGW_PREFIX}/bin/libsamplerate-0.dll"
					"${MINGW_PREFIX}/bin/libsndfile-1.dll"
					"${MINGW_PREFIX}/bin/libvorbis-0.dll"
					"${MINGW_PREFIX}/bin/libvorbisenc-2.dll"
					"${MINGW_PREFIX}/bin/libvorbisfile-3.dll"
					"${MINGW_PREFIX}/bin/libjpeg-9.dll"
					"${MINGW_PREFIX}/bin/libogg-0.dll"
					"${MINGW_PREFIX}/bin/libmp3lame-0.dll"
					"${MINGW_PREFIX}/bin/libfftw3f-3.dll"
					"${MINGW_PREFIX}/bin/libFLAC-8.dll"
					"${MINGW_PREFIX}/bin/libpng16-16.dll"
					"${MINGW_PREFIX}/bin/SDL.dll"
					"${MINGW_PREFIX}/bin/libglib-2.0-0.dll"
					"${MINGW_PREFIX}/bin/libgthread-2.0-0.dll"
					"${MINGW_PREFIX}/bin/zlib1.dll"
					"${MINGW_PREFIX}/${CMAKE_SYSTEM_PROCESSOR}-w64-mingw32/bin/libwinpthread-1.dll"
				DESTINATION .)
	IF(LMMS_BUILD_MSYS)
		INSTALL(FILES
					"${MINGW_PREFIX}/bin/libgcc_s_seh-1.dll"
					"${MINGW_PREFIX}/bin/libstdc++-6.dll"
				DESTINATION .)
	ENDIF()
	IF(FLTK_FOUND)
		INSTALL(FILES
					"${MINGW_PREFIX}/bin/libfltk.dll"
				DESTINATION .)
	ENDIF()
	IF(FLUIDSYNTH_FOUND)
		INSTALL(FILES
					"${MINGW_PREFIX}/bin/libfluidsynth.dll"
				DESTINATION .)
	ENDIF()
	IF(GIG_FOUND)
		INSTALL(FILES
					"${MINGW_PREFIX}/bin/libgig-6.dll"
				DESTINATION .)
	ENDIF()
	IF(PORTAUDIO_FOUND)
		INSTALL(FILES
					"${MINGW_PREFIX}/bin/libportaudio-2.dll"
				DESTINATION .)
	ENDIF()
	IF(SOUNDIO_FOUND)
		INSTALL(FILES
					"${MINGW_PREFIX}/lib/libsoundio.dll"
				DESTINATION .)
	ENDIF()

ELSE(LMMS_BUILD_WIN32)
	IF(NOT LMMS_BUILD_APPLE)
		SET_TARGET_PROPERTIES(lmms PROPERTIES LINK_FLAGS "${LINK_FLAGS} -Wl,-E")
	ENDIF(NOT LMMS_BUILD_APPLE)

	INSTALL(TARGETS lmms RUNTIME DESTINATION "${BIN_DIR}")
	INSTALL(FILES "${CMAKE_BINARY_DIR}/lmms.1.gz" DESTINATION "${CMAKE_INSTALL_PREFIX}/share/man/man1/" PERMISSIONS OWNER_READ GROUP_READ WORLD_READ)

ENDIF(LMMS_BUILD_WIN32)
