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++14
SET(CMAKE_CXX_STANDARD 14)

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

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

QT5_WRAP_UI(LMMS_UI_OUT ${LMMS_UIS})
INCLUDE_DIRECTORIES(
	"${CMAKE_CURRENT_BINARY_DIR}"
	"${CMAKE_BINARY_DIR}"
	"${CMAKE_BINARY_DIR}/include"
	"${CMAKE_SOURCE_DIR}"
	"${CMAKE_SOURCE_DIR}/include"
	"${RINGBUFFER_DIR}/include"
)

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

INCLUDE(GenQrc)
ADD_GEN_QRC(LMMS_RCC_OUT lmms.qrc
	"${CMAKE_SOURCE_DIR}/doc/AUTHORS"
	"${CMAKE_SOURCE_DIR}/LICENSE.txt"
	"${CONTRIBUTORS}"
)

# 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(-DLIB_DIR="${LIB_DIR_RELATIVE}" -DPLUGIN_DIR="${PLUGIN_DIR_RELATIVE}" ${PULSEAUDIO_DEFINITIONS} ${PORTAUDIO_DEFINITIONS})
INCLUDE_DIRECTORIES(
	${JACK_INCLUDE_DIRS}
	${SAMPLERATE_INCLUDE_DIRS}
	${SNDFILE_INCLUDE_DIRS}
	${SNDIO_INCLUDE_DIRS}
	${FFTW3F_INCLUDE_DIRS}
)

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

IF(LMMS_HAVE_WEAKJACK)
	LIST(APPEND LMMS_SRCS "${WEAKJACK_INCLUDE_DIRS}/weak_libjack.c")
	LIST(APPEND LMMS_INCLUDES "${WEAKJACK_INCLUDE_DIRS}/weak_libjack.h")
	INCLUDE_DIRECTORIES("${WEAKJACK_INCLUDE_DIRS}")
	ADD_DEFINITIONS(-DUSE_WEAK_JACK=1 -DNO_JACK_METADATA=1)
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()

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

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

IF(NOT ("${SUIL_INCLUDE_DIRS}" STREQUAL ""))
	INCLUDE_DIRECTORIES(${SUIL_INCLUDE_DIRS})
ENDIF()
LIST(APPEND LMMS_SRCS "${RINGBUFFER_DIR}/src/lib/ringbuffer.cpp")

# 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(lmmsobjs OBJECT
	${LMMS_SRCS}
	${LMMS_INCLUDES}
	${LMMS_UI_OUT}
	${LMMS_RCC_OUT}
)

GENERATE_EXPORT_HEADER(lmmsobjs
	BASE_NAME lmms
)

ADD_EXECUTABLE(lmms
	core/main.cpp
	$<TARGET_OBJECTS:lmmsobjs>
	"${WINRC}"
)
TARGET_INCLUDE_DIRECTORIES(lmms
	PUBLIC ${CMAKE_CURRENT_BINARY_DIR}
)

# CMake doesn't define target_EXPORTS for OBJECT libraries.
# See the documentation of DEFINE_SYMBOL for details.
# Also add LMMS_STATIC_DEFINE for targets linking against it.
TARGET_COMPILE_DEFINITIONS(lmmsobjs
	PRIVATE -Dlmmsobjs_EXPORTS
	INTERFACE -DLMMS_STATIC_DEFINE
)
TARGET_COMPILE_DEFINITIONS(lmms
	PRIVATE $<TARGET_PROPERTY:lmmsobjs,INTERFACE_COMPILE_DEFINITIONS>
)

# Set Visual Studio startup project to lmms
# https://stackoverflow.com/a/37994396/8166701
IF(NOT CMAKE_VERSION VERSION_LESS 3.6)
	SET_PROPERTY(DIRECTORY ${CMAKE_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT lmms)
ENDIF()

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

IF(LMMS_BUILD_WIN32)
	SET(EXTRA_LIBRARIES "winmm")
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}
	${SDL2_LIBRARY}
	${PORTAUDIO_LIBRARIES}
	${SOUNDIO_LIBRARY}
	${SNDIO_LIBRARIES}
	${PULSEAUDIO_LIBRARIES}
	${JACK_LIBRARIES}
	${OGGVORBIS_LIBRARIES}
	${LAME_LIBRARIES}
	${LV2_LIBRARIES}
	${SUIL_LIBRARIES}
	${LILV_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()

IF(LMMS_BUILD_WIN32)
	SET_TARGET_PROPERTIES(lmms PROPERTIES
		ENABLE_EXPORTS ON
	)
	IF(LMMS_BUILD_MSYS)
		# ENABLE_EXPORTS property has no effect in some MSYS2 configurations.
		# Add the linker flag manually to create liblmms.dll.a import library
		SET_PROPERTY(TARGET lmms
			APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--out-implib,liblmms.dll.a"
		)
	ENDIF()
ELSE()
	IF(NOT LMMS_BUILD_APPLE)
		SET_TARGET_PROPERTIES(lmms PROPERTIES LINK_FLAGS "${LINK_FLAGS} -Wl,-E")
	ENDIF(NOT LMMS_BUILD_APPLE)

	if(CMAKE_INSTALL_MANDIR)
		SET(INSTALL_MANDIR ${CMAKE_INSTALL_MANDIR})
	ELSE(CMAKE_INSTALL_MANDIR)
		SET(INSTALL_MANDIR ${CMAKE_INSTALL_PREFIX}/share/man)
	ENDIF(CMAKE_INSTALL_MANDIR)
	INSTALL(FILES "${CMAKE_BINARY_DIR}/lmms.1.gz"
			DESTINATION "${INSTALL_MANDIR}/man1/"
			PERMISSIONS OWNER_READ GROUP_READ WORLD_READ)
ENDIF()

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