#**************************************************************************
#    Lightspark, a free flash player implementation
#
#    Copyright (C) 2010-2013  Alessandro Pignotti <a.pignotti@sssup.it>
#    Copyright (C) 2010  Giacomo Spigler <g.spigler@sssup.it>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#**************************************************************************


CMAKE_MINIMUM_REQUIRED(VERSION 2.6...3.19)

# Name & Version
PROJECT(LIGHTSPARK)
SET(MAJOR_VERSION "0")
SET(MINOR_VERSION "8")
SET(PATCH_VERSION "7")
SET(SUBPATCH_VERSION "0")

IF(EXISTS "${CMAKE_SOURCE_DIR}/.git/")
	SET(GIT_VERSION "-git")

	#Find out if we are exactly at a tag. Then the version
	#is enough. Only consider the official release tags like
	#'lightspark-0.5.3'
	EXECUTE_PROCESS(COMMAND git describe --match "lightspark-*" --tags
			WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
			OUTPUT_VARIABLE GIT_DESC
			ERROR_QUIET
			OUTPUT_STRIP_TRAILING_WHITESPACE)
	#Check if that command failed (maybe git is not installed at all)
	IF(GIT_DESC)
		EXECUTE_PROCESS(COMMAND git describe --match "lightspark-*" --tags --abbrev=0
				WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
				OUTPUT_VARIABLE GIT_DESC_ABBREV
				ERROR_QUIET
				OUTPUT_STRIP_TRAILING_WHITESPACE)

		IF(${GIT_DESC} STREQUAL ${GIT_DESC_ABBREV})
			#We are exactly at a tag
			SET(GIT_VERSION "")
		ELSE()
			#Append to GIT_VERSION
			#GIT_DESC_ABBREV is of the form lightspark-0.5.3 (the name of the previous tag)
			#GIT_DESC is of the form lightspark-0.5.3-69-gf49ec56 (tag-numberOfCommits-currentHash)
			#We want GIT_REVISION to be 69-gf49ec56
			#which denotes the number of commits since the last tag (=release)
			#and the current commit's hash
			STRING(REPLACE "${GIT_DESC_ABBREV}-" "" GIT_REVISION "${GIT_DESC}")
			SET(GIT_VERSION "-${GIT_REVISION}")
		ENDIF()
	ENDIF()
ELSE(EXISTS "${CMAKE_SOURCE_DIR}/.git/")
	SET(GIT_VERSION "")
ENDIF(EXISTS "${CMAKE_SOURCE_DIR}/.git/")

IF(SUBPATCH_VERSION EQUAL "0")
	SET(FULLVERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}${GIT_VERSION}")
ELSE(SUBPATCH_VERSION EQUAL "0")
	SET(FULLVERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}.${SUBPATCH_VERSION}${GIT_VERSION}")
ENDIF(SUBPATCH_VERSION EQUAL "0")

SET(SHORTVERSION "${MINOR_VERSION}${PATCH_VERSION}${SUBPATCH_VERSION}")

CONFIGURE_FILE("${CMAKE_SOURCE_DIR}/src/version.h.in" "${CMAKE_CURRENT_BINARY_DIR}/version.h" @ONLY)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

# GCC 4.6+ is required, GCC_VERSION macro taken from GCC manual
INCLUDE(CheckCSourceCompiles)
IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
   CHECK_C_SOURCE_COMPILES("
   #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
   #if GCC_VERSION < 40600
   #error
   #endif
   int main() { return 0; }" GCC_IS_4_6)
   IF(NOT GCC_IS_4_6)
      MESSAGE(FATAL_ERROR "GCC 4.6+ is required.")
   ENDIF(NOT GCC_IS_4_6)
ENDIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")

# Find put the path of the gnash executable, if available
FIND_PROGRAM(GNASH_EXE_PATH NAMES gnash)

IF(GNASH_EXE_PATH)
	MESSAGE(STATUS "Found gnash path: ${GNASH_EXE_PATH}")
ELSE(GNASH_EXE_PATH)
# Set a default path
	MESSAGE(STATUS "Gnash not found")
	SET(GNASH_EXE_PATH "/usr/bin/gnash")
ENDIF(GNASH_EXE_PATH)

# Find out CPU architecture ...
#
# Setting the value of LIB_SUFFIX if not specified (We could build 32bit version on 64bit)
# However, some distro don't implement FHS the same way
# Suse, Redhat put 64bit libs in /lib64; Debian use /lib for 64bit, but specifies /lib32 for 32bit libs
# See FHS 2.3 for Lib directories under each architecture

IF(CMAKE_SIZEOF_VOID_P STREQUAL "8")
    ADD_DEFINITIONS(-DLIGHTSPARK_64)
endif(CMAKE_SIZEOF_VOID_P STREQUAL "8")
# Some directory shortcuts
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/conf)
INCLUDE(Pack)
# If we're gcc, then use nasm to get fastpath.  If MSVC, just use inline asm to get around
# CMake issues
IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
   INCLUDE(CMakeASM-NASMCompiler)
ENDIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
IF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^i[3-6]86$|^x86$")
	SET(i386 1)
	SET(LIB_SUFFIX "" CACHE STRING "Choose the suffix of the lib folder (if any) : None 32")
	# nasm for assembly optimizations
	IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
	   ENABLE_LANGUAGE(ASM-NASM)
	ENDIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
ELSEIF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "unknown" AND ${CMAKE_SYSTEM} MATCHES "GNU-0.3")
	# GNU Hurd is i386
	SET(i386 1)
	SET(LIB_SUFFIX "" CACHE STRING "Choose the suffix of the lib folder (if any) : None 32")
	# nasm for assembly optimizations
	IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
	   ENABLE_LANGUAGE(ASM-NASM)
	ENDIF ()
ELSEIF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^x86_64$|^amd64$")
	SET(x86_64 1)
	SET(LIB_SUFFIX "" CACHE STRING "Choose the suffix of the lib folder (if any) : None 64")
	# nasm for assembly optimizations
	IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
	  ENABLE_LANGUAGE(ASM-NASM)
	ENDIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
ELSEIF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "ppc")
	SET(ppc 1)
	SET(LIB_SUFFIX "" CACHE STRING "Choose the suffix of the lib folder (if any) : None ppc")
	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__STDC_CONSTANT_MACROS -fPIC")
	IF(NOT(DISABLE_ALTIVEC))
		SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maltivec")
	ENDIF()
ELSE(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^i[3-6]86$|^x86$")
	# All non-x86 non-ppc architectures.
	SET(LIB_SUFFIX "" CACHE STRING "Choose the suffix of the lib folder (if any) : None")
ENDIF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^i[3-6]86$|^x86$")

IF(CMAKE_SYSTEM_NAME STREQUAL CMAKE_HOST_SYSTEM_NAME)
  SET(CROSSCOMPILING FALSE)
ELSE()
  SET(CROSSCOMPILING TRUE)
ENDIF()

#On mingw-cross-env, all depedencies are static libs
IF(CROSSCOMPILING AND MINGW)
  SET(STATICDEPS TRUE)
ELSE()
  SET(STATICDEPS FALSE)
ENDIF()

# Using relative folders, cmake happens by itself CMAKE_INSTALL_PREFIX
IF(UNIX)
   SET(ETCDIR "/etc") #Using absolute folder
   SET(BINDIR "bin")
   SET(DATADIR "share")
   # Absolute path, because this path is embedded in the binary
   SET(LSDATADIR "${CMAKE_INSTALL_PREFIX}/${DATADIR}/lightspark")
   SET(LIBDIR "lib${LIB_SUFFIX}")
   #We need a absolute path here because we use it for rpath
   #later on
   SET(PRIVATELIBDIR "${CMAKE_INSTALL_PREFIX}/${LIBDIR}/lightspark")
   SET(CMAKE_INSTALL_RPATH "${PRIVATELIBDIR}")
ELSE()
  SET(ETCDIR ".")
  SET(BINDIR ".")
  SET(DATADIR ".")
  SET(LSDATADIR ".")
  SET(LIBDIR ".")
  SET(PRIVATELIBDIR ".")
ENDIF(UNIX)

SET(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Install prefix, default is /usr/local (UNIX) and C:\\Program Files (Windows)")
SET(COMPILE_LIGHTSPARK TRUE CACHE BOOL "Compile Lightspark?")
SET(COMPILE_TIGHTSPARK FALSE CACHE BOOL "Compile Tightspark?")
IF(EMSCRIPTEN)
SET(COMPILE_NPAPI_PLUGIN FALSE)
SET(COMPILE_PPAPI_PLUGIN FALSE)
SET(ENABLE_GLES2 TRUE)
ELSE()
SET(COMPILE_NPAPI_PLUGIN TRUE CACHE BOOL "Compile the npapi browser plugin?")
SET(COMPILE_PPAPI_PLUGIN TRUE CACHE BOOL "Compile the ppapi browser plugin?")
SET(ENABLE_GLES2 FALSE CACHE BOOL "Build with OpenGLES 2.0 support instead of OpenGL")
ENDIF(EMSCRIPTEN)
SET(ENABLE_CURL TRUE CACHE BOOL "Enable CURL? (Required for Downloader functionality)")
SET(ENABLE_LIBAVCODEC TRUE CACHE BOOL "Enable libavcodec and dependent functionality?")
SET(ENABLE_RTMP TRUE CACHE BOOL "Enable librtmp and dependent functionality?")
SET(ENABLE_LLVM FALSE CACHE BOOL "Enable support for llvm based jit execution (currently broken)")
SET(ENABLE_PROFILING FALSE CACHE BOOL "Enable profiling support? (Causes performance issues)")
SET(ENABLE_MEMORY_USAGE_PROFILING FALSE CACHE BOOL "Enable profiling of memory usage? (Causes performance issues)")
SET(PLUGIN_DIRECTORY "${LIBDIR}/mozilla/plugins" CACHE STRING "Directory to install Firefox plugin to")
SET(PPAPI_PLUGIN_DIRECTORY "${LIBDIR}/PepperFlash" CACHE STRING "Directory to install PPAPI plugin to")
SET(MANUAL_DIRECTORY "share/man" CACHE STRING "Directory to install manual to (UNIX only)")
SET(ENABLE_SSE2 TRUE CACHE BOOL "Enable use of SSE2 asm instructions (x86/x86_64 only)")

IF(ENABLE_DEBIAN_ALTERNATIVES OR WIN32)
  SET(PLUGIN_DIRECTORY ${PRIVATELIBDIR})
ENDIF()

IF(NOT CMAKE_BUILD_TYPE)
	SET(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
		"Choose the type of build, options are: Debug Release RelWithDebInfo Profile" FORCE)
ENDIF(NOT CMAKE_BUILD_TYPE)

# The library is searched in its private library directory
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_INSTALL_RPATH "${PRIVATELIBDIR}")

# Libraries we need
INCLUDE(FindPkgConfig REQUIRED)

IF (ENABLE_LLVM)
	ADD_DEFINITIONS(-DLLVM_ENABLED)
	INCLUDE(FindLLVM REQUIRED)
	IF(${LLVM_STRING_VERSION} VERSION_LESS 2.8)
		MESSAGE(FATAL_ERROR "LLVM >=2.8 is required!")
	ENDIF(${LLVM_STRING_VERSION} VERSION_LESS 2.8)
	IF(${LLVM_STRING_VERSION} VERSION_EQUAL 2.9)
		MESSAGE(FATAL_ERROR "LLVM !=2.9 is required!")
	ENDIF(${LLVM_STRING_VERSION} VERSION_EQUAL 2.9)
	IF(${LLVM_STRING_VERSION} VERSION_EQUAL 2.8)
		ADD_DEFINITIONS(-DLLVM_28)
	ENDIF(${LLVM_STRING_VERSION} VERSION_EQUAL 2.8)
	IF(${LLVM_STRING_VERSION} VERSION_EQUAL 3.0)
		ADD_DEFINITIONS(-DLLVM_30)
	ENDIF(${LLVM_STRING_VERSION} VERSION_EQUAL 3.0)
	IF(${LLVM_STRING_VERSION} VERSION_GREATER 3.0)
		ADD_DEFINITIONS(-DLLVM_31)
	ENDIF(${LLVM_STRING_VERSION} VERSION_GREATER 3.0)
	IF(${LLVM_STRING_VERSION} VERSION_GREATER 3.3)
		ADD_DEFINITIONS(-DLLVM_34)
	ENDIF(${LLVM_STRING_VERSION} VERSION_GREATER 3.3)
	IF(${LLVM_STRING_VERSION} VERSION_GREATER 3.4)
		ADD_DEFINITIONS(-DLLVM_35)
	ENDIF(${LLVM_STRING_VERSION} VERSION_GREATER 3.4)
	IF(NOT (${LLVM_STRING_VERSION} VERSION_LESS 3.6))
		ADD_DEFINITIONS(-DLLVM_36)
	ENDIF(NOT (${LLVM_STRING_VERSION} VERSION_LESS 3.6))
	IF(NOT ${LLVM_STRING_VERSION} VERSION_LESS 3.7)
		ADD_DEFINITIONS(-DLLVM_37)
	ENDIF(NOT ${LLVM_STRING_VERSION} VERSION_LESS 3.7)
	IF(NOT ${LLVM_STRING_VERSION} VERSION_LESS 3.8)
		ADD_DEFINITIONS(-DLLVM_38)
	ENDIF(NOT ${LLVM_STRING_VERSION} VERSION_LESS 3.8)
	IF(NOT ${LLVM_STRING_VERSION} VERSION_LESS 5.0)
		ADD_DEFINITIONS(-DLLVM_50)
	ENDIF(NOT ${LLVM_STRING_VERSION} VERSION_LESS 5.0)
	IF(NOT ${LLVM_STRING_VERSION} VERSION_LESS 7.0)
		ADD_DEFINITIONS(-DLLVM_70)
	ENDIF(NOT ${LLVM_STRING_VERSION} VERSION_LESS 7.0)
ENDIF(ENABLE_LLVM)

IF(EMSCRIPTEN)
	SET(STATICDEPS TRUE)
	set(USE_FLAGS "-s USE_SDL=2 -s USE_FREETYPE=1 -s USE_ZLIB=1 -s USE_LIBJPEG=1 -s USE_LIBPNG=1")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${USE_FLAGS}")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${USE_FLAGS}")
	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${USE_FLAGS}")
	set(CMAKE_EXECUTABLE_SUFFIX .html)
ELSE()
	INCLUDE(FindZLIB REQUIRED)
	INCLUDE(FindFreetype REQUIRED)
	INCLUDE(FindJPEG REQUIRED)
	INCLUDE(FindPNG REQUIRED)
ENDIF(EMSCRIPTEN)
IF(NOT(ENABLE_GLES2))
	SET(OpenGL_GL_PREFERENCE LEGACY)
	INCLUDE(FindOpenGL REQUIRED)
	INCLUDE(FindGLEW REQUIRED)
	ADD_DEFINITIONS(-DNANOVG_GL2_IMPLEMENTATION)
ENDIF(NOT(ENABLE_GLES2))

pkg_check_modules(LZMA REQUIRED liblzma)
INCLUDE_DIRECTORIES(${LZMA_INCLUDE_DIRS})
SET(OPTIONAL_LIBRARIES ${OPTIONAL_LIBRARIES} ${LZMA_LIBRARIES})

pkg_check_modules(CAIRO REQUIRED cairo pangocairo)

IF(ENABLE_GLES2)
  pkg_check_modules(GLES2 REQUIRED egl glesv2)
  ADD_DEFINITIONS(-DENABLE_GLES2)
  ADD_DEFINITIONS(-DNANOVG_GLES2_IMPLEMENTATION)
ENDIF(ENABLE_GLES2)

IF(WIN32)
  SET(EXTRA_LIBS_LIBRARIES ${EXTRA_LIBS_LIBRARIES} ws2_32.lib imagehlp.lib)
ENDIF(WIN32)

IF(ENABLE_LIBAVCODEC)
	pkg_check_modules(FFMPEG libavcodec libavutil libavformat)
	pkg_check_modules(LIBSWRESAMPLE libswresample)
	IF(NOT(FFMPEG_FOUND))
		INCLUDE(FindFFMpeg REQUIRED)
	ENDIF(NOT(FFMPEG_FOUND))
	# Compatibility checks for ffmpeg deprecated functions
  INCLUDE(CheckFunctionExists REQUIRED)
  INCLUDE(CheckCSourceCompiles)
  IF(LIBSWRESAMPLE_FOUND)
    SET(FFMPEG_FLAGS "${LIBAVCODEC_CFLAGS} ${LIBSWRESAMPLE_FLAGS}")
    SET(FFMPEG_INCLUDE_DIRS "${FFMPEG_INCLUDE_DIRS};${LIBSWRESAMPLE_INCLUDE_DIRS}")
    SET(FFMPEG_LIBRARIES "${FFMPEG_LIBRARIES};${LIBSWRESAMPLE_LIBRARIES}")
  ELSE()
    # libavresample is deprecated, so only use it as a fallback for older ffmpeg
    pkg_check_modules(LIBAVRESAMPLE libavresample)
    SET(FFMPEG_FLAGS "${LIBAVCODEC_CFLAGS} ${LIBAVRESAMPLE_FLAGS}")
    SET(FFMPEG_INCLUDE_DIRS "${FFMPEG_INCLUDE_DIRS};${LIBAVRESAMPLE_INCLUDE_DIRS}")
    SET(FFMPEG_LIBRARIES "${FFMPEG_LIBRARIES};${LIBAVRESAMPLE_LIBRARIES}")
  ENDIF(LIBSWRESAMPLE_FOUND)
  SET(CMAKE_REQUIRED_FLAGS ${FFMPEG_FLAGS})
  SET(CMAKE_REQUIRED_INCLUDES ${FFMPEG_INCLUDE_DIRS})
  SET(CMAKE_REQUIRED_LIBRARIES ${FFMPEG_LIBRARIES})
  CHECK_FUNCTION_EXISTS(avcodec_decode_video2 HAVE_AVCODEC_DECODE_VIDEO2)
  CHECK_FUNCTION_EXISTS(avcodec_decode_audio3 HAVE_AVCODEC_DECODE_AUDIO3)
  CHECK_FUNCTION_EXISTS(avcodec_decode_audio4 HAVE_AVCODEC_DECODE_AUDIO4)
  CHECK_FUNCTION_EXISTS(avio_alloc_context HAVE_AVIO_ALLOC_CONTEXT)
  CHECK_FUNCTION_EXISTS(avcodec_alloc_context3 HAVE_AVCODEC_ALLOC_CONTEXT3)
  CHECK_FUNCTION_EXISTS(avcodec_open2 HAVE_AVCODEC_OPEN2)
  CHECK_FUNCTION_EXISTS(avformat_close_input HAVE_AVFORMAT_CLOSE_INPUT)
  CHECK_FUNCTION_EXISTS(avformat_find_stream_info HAVE_AVFORMAT_FIND_STREAM_INFO)
  CHECK_FUNCTION_EXISTS(av_frame_alloc HAVE_AV_FRAME_ALLOC)
  CHECK_FUNCTION_EXISTS(av_frame_unref HAVE_AV_FRAME_UNREF)
  CHECK_FUNCTION_EXISTS(av_packet_unref HAVE_AV_PACKET_UNREF)
  CHECK_FUNCTION_EXISTS(avcodec_send_packet HAVE_AVCODEC_SEND_PACKET)
  CHECK_FUNCTION_EXISTS(avcodec_receive_frame HAVE_AVCODEC_RECEIVE_FRAME)
  CHECK_FUNCTION_EXISTS(av_frame_get_sample_rate HAVE_AV_FRAME_GET_SAMPLE_RATE)

  CHECK_C_SOURCE_COMPILES("#include <libavcodec/avcodec.h>\nint main() { enum AVCodecID c; return 0; }" HAVE_AVCODECID)

  SET(CMAKE_REQUIRED_FLAGS)
  SET(CMAKE_REQUIRED_INCLUDES)
  SET(CMAKE_REQUIRED_LIBRARIES)
  IF(HAVE_AVCODEC_DECODE_VIDEO2)
    ADD_DEFINITIONS(-DHAVE_AVCODEC_DECODE_VIDEO2)
  ENDIF(HAVE_AVCODEC_DECODE_VIDEO2)
  IF(HAVE_AVCODEC_DECODE_AUDIO3)
    ADD_DEFINITIONS(-DHAVE_AVCODEC_DECODE_AUDIO3)
  ENDIF(HAVE_AVCODEC_DECODE_AUDIO3)
  IF(HAVE_AVCODEC_DECODE_AUDIO4)
    ADD_DEFINITIONS(-DHAVE_AVCODEC_DECODE_AUDIO4)
  ENDIF(HAVE_AVCODEC_DECODE_AUDIO4)
  IF(HAVE_AVIO_ALLOC_CONTEXT)
    ADD_DEFINITIONS(-DHAVE_AVIO_ALLOC_CONTEXT)
  ENDIF(HAVE_AVIO_ALLOC_CONTEXT)
  IF(HAVE_AVCODEC_ALLOC_CONTEXT3)
    ADD_DEFINITIONS(-DHAVE_AVCODEC_ALLOC_CONTEXT3)
  ENDIF(HAVE_AVCODEC_ALLOC_CONTEXT3)
  IF(HAVE_AVCODEC_OPEN2)
    ADD_DEFINITIONS(-DHAVE_AVCODEC_OPEN2)
  ENDIF(HAVE_AVCODEC_OPEN2)
  IF(HAVE_AVFORMAT_CLOSE_INPUT)
    ADD_DEFINITIONS(-DHAVE_AVFORMAT_CLOSE_INPUT)
  ENDIF(HAVE_AVFORMAT_CLOSE_INPUT)
  IF(HAVE_AVFORMAT_FIND_STREAM_INFO)
    ADD_DEFINITIONS(-DHAVE_AVFORMAT_FIND_STREAM_INFO)
  ENDIF(HAVE_AVFORMAT_FIND_STREAM_INFO)
  IF(HAVE_AVCODECID)
    ADD_DEFINITIONS(-DHAVE_AVCODECID)
  ENDIF(HAVE_AVCODECID)
  IF(LIBSWRESAMPLE_FOUND)
    ADD_DEFINITIONS(-DHAVE_LIBSWRESAMPLE)
  ELSE()
    # libavresample is deprecated, so only use it as a fallback for older ffmpeg
    IF(LIBAVRESAMPLE_FOUND)
      ADD_DEFINITIONS(-DHAVE_LIBAVRESAMPLE)
    ENDIF(LIBAVRESAMPLE_FOUND)
  ENDIF(LIBSWRESAMPLE_FOUND)
  IF(HAVE_AV_FRAME_ALLOC)
    ADD_DEFINITIONS(-DHAVE_AV_FRAME_ALLOC)
  ENDIF(HAVE_AV_FRAME_ALLOC)
  IF(HAVE_AV_FRAME_UNREF)
    ADD_DEFINITIONS(-DHAVE_AV_FRAME_UNREF)
  ENDIF(HAVE_AV_FRAME_UNREF)
  IF(HAVE_AV_PACKET_UNREF)
    ADD_DEFINITIONS(-DHAVE_AV_PACKET_UNREF)
  ENDIF(HAVE_AV_PACKET_UNREF)
  IF(HAVE_AVCODEC_SEND_PACKET)
    ADD_DEFINITIONS(-DHAVE_AVCODEC_SEND_PACKET)
  ENDIF(HAVE_AVCODEC_SEND_PACKET)
  IF(HAVE_AVCODEC_RECEIVE_FRAME)
    ADD_DEFINITIONS(-DHAVE_AVCODEC_RECEIVE_FRAME)
  ENDIF(HAVE_AVCODEC_RECEIVE_FRAME)
  IF(HAVE_AV_FRAME_GET_SAMPLE_RATE)
    ADD_DEFINITIONS(-DHAVE_AV_FRAME_GET_SAMPLE_RATE)
  ENDIF(HAVE_AV_FRAME_GET_SAMPLE_RATE)
  ADD_DEFINITIONS(-DENABLE_LIBAVCODEC)
ENDIF(ENABLE_LIBAVCODEC)

pkg_check_modules(GLIB REQUIRED glib-2.0)
pkg_check_modules(SDL2 REQUIRED sdl2)

IF (ENABLE_LLVM)
    INCLUDE_DIRECTORIES(${LLVM_INCLUDE_DIR})
ENDIF(ENABLE_LLVM)
INCLUDE_DIRECTORIES(${FREETYPE_INCLUDE_DIRS})
IF(ENABLE_GLES2)
	INCLUDE_DIRECTORIES(${GLES2_INCLUDE_DIRS})
ELSE()
	INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
	INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIR})
ENDIF(ENABLE_GLES2)
IF(EMSCRIPTEN)
SET(ZLIB_INCLUDE_DIR "")
SET(PCRE_INCLUDE_DIR "")
SET(JPEG_INCLUDE_DIR "")
SET(PNG_INCLUDE_DIR "")
ELSE()
INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${PCRE_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${JPEG_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIR})
ENDIF(EMSCRIPTEN)
INCLUDE_DIRECTORIES(${CAIRO_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${GLIB_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS})

IF(ENABLE_LIBAVCODEC)
  INCLUDE_DIRECTORIES(${FFMPEG_INCLUDE_DIRS})
  SET(OPTIONAL_LIBRARIES ${OPTIONAL_LIBRARIES} ${FFMPEG_LIBRARIES})
ENDIF(ENABLE_LIBAVCODEC)

IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  IF(MINGW)
    SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed,--no-undefined -mthreads ${CMAKE_EXE_LINKER_FLAGS}")
    SET(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed,--no-undefined,--enable-stdcall-fixup -mthreads ${CMAKE_SHARED_LINKER_FLAGS}")
  ELSE()
    SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--no-undefined ${CMAKE_EXE_LINKER_FLAGS}")
    SET(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed,-z,noexecstack,--no-undefined ${CMAKE_SHARED_LINKER_FLAGS}")
  ENDIF()
  SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "-s")
  SET(CMAKE_SHARED_LINKER_FLAGS_RELEASE "-s")
ENDIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
IF(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
	SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed ${CMAKE_EXE_LINKER_FLAGS}")
	SET(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed,-z,noexecstack ${CMAKE_SHARED_LINKER_FLAGS}")
	SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "-Wl,--no-undefined -s")
	SET(CMAKE_SHARED_LINKER_FLAGS_RELEASE "-Wl,--no-undefined -s")
ENDIF(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")

IF(ENABLE_LLVM)
    LINK_DIRECTORIES(${LLVM_LIB_DIR})
ENDIF(ENABLE_LLVM)

IF(ENABLE_CURL)
  pkg_check_modules(CURL REQUIRED libcurl)
  INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIR})
  IF(STATICDEPS)
    SET(OPTIONAL_LIBRARIES ${OPTIONAL_LIBRARIES} ${CURL_STATIC_LIBRARIES} crypt32)
	ADD_DEFINITIONS(-DCURL_STATICLIB)
  ELSE()
    SET(OPTIONAL_LIBRARIES ${OPTIONAL_LIBRARIES} ${CURL_LIBRARIES})
  ENDIF()
  ADD_DEFINITIONS(-DENABLE_CURL)
ENDIF(ENABLE_CURL)

IF(ENABLE_RTMP)
  pkg_check_modules(RTMP REQUIRED librtmp)
  SET(OPTIONAL_LIBRARIES ${OPTIONAL_LIBRARIES} ${RTMP_LIBRARIES})
  ADD_DEFINITIONS(-DENABLE_RTMP)
ENDIF(ENABLE_RTMP)

IF(ENABLE_PROFILING)
  ADD_DEFINITIONS(-DPROFILING_SUPPORT)
ENDIF(ENABLE_PROFILING)

IF(ENABLE_MEMORY_USAGE_PROFILING)
	ADD_DEFINITIONS(-DMEMORY_USAGE_PROFILING)
ENDIF(ENABLE_MEMORY_USAGE_PROFILING)

# Compiler defaults flags for different profiles
IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  IF(MINGW)
    SET(CMAKE_CXX_FLAGS
          "${CMAKE_CXX_FLAGS} -mms-bitfields -mthreads -fexceptions -Wall -Wnon-virtual-dtor -Woverloaded-virtual -pipe -std=c++11 -Wdisabled-optimization -Wextra -Wno-unused-parameter -Wno-invalid-offsetof")
  ELSE()
   SET(CMAKE_CXX_FLAGS
          "${CMAKE_CXX_FLAGS} -Wall -Wnon-virtual-dtor -Woverloaded-virtual -pipe -fvisibility=hidden -fvisibility-inlines-hidden -std=c++11 -Wdisabled-optimization -Wextra -Wno-unused-parameter -Wno-invalid-offsetof")
  ENDIF()
  SET(CMAKE_CXX_FLAGS_MEMORY "-g -O0 -DEXPENSIVE_DEBUG -fsanitize=address")
  SET(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -DEXPENSIVE_DEBUG")
  SET(CMAKE_CXX_FLAGS_PROFILE "-g -pg -O2")
  SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
  SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -O2 -DNDEBUG")
  SET(CMAKE_CXX_FLAGS_LEANDEBUG "-g -O2")
  ADD_DEFINITIONS(-DLS_DATADIR="${LSDATADIR}" -DGNASH_PATH="${GNASH_EXE_PATH}" -DPRIVATELIBDIR="${PRIVATELIBDIR}")
ENDIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
IF(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  SET(CMAKE_CXX_FLAGS
	  "${CMAKE_CXX_FLAGS} -Wall -Wnon-virtual-dtor -Woverloaded-virtual -Wno-undefined-var-template -pipe -fvisibility=hidden -fvisibility-inlines-hidden -std=c++11 -Wdisabled-optimization -Wextra -Wno-unused-parameter -Wno-invalid-offsetof")
  SET(CMAKE_CXX_FLAGS_MEMORY "-g -O0 -DEXPENSIVE_DEBUG -fsanitize=address")
  SET(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -DEXPENSIVE_DEBUG")
  SET(CMAKE_CXX_FLAGS_PROFILE "-g -pg -O2")
  SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
  SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -O2 -DNDEBUG")
  SET(CMAKE_CXX_FLAGS_LEANDEBUG "-g -O2")
  ADD_DEFINITIONS(-DLS_DATADIR="${LSDATADIR}" -DGNASH_PATH="${GNASH_EXE_PATH}" -DPRIVATELIBDIR="${PRIVATELIBDIR}")
  ADD_DEFINITIONS(-DUSE_CLANG)
ENDIF(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")

IF(MSVC)
  ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS -DVPCOMPAT -DPTW32_STATIC_LIB -DPCRE_STATIC)
ENDIF(MSVC)

IF(STATICDEPS)
  ADD_DEFINITIONS(-DLIBXML_STATIC -DGLEW_STATIC)
ENDIF()

# Check if the system have atomic header ( gcc >= 4.6 )
INCLUDE (CheckIncludeFileCXX)
CHECK_INCLUDE_FILE_CXX (atomic HAVE_ATOMIC)
IF(HAVE_ATOMIC)
  ADD_DEFINITIONS(-DHAVE_ATOMIC)
ENDIF(HAVE_ATOMIC)

# Setting the output directories, so we can build all profiles without mixmatching
IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_SYSTEM_PROCESSOR}/${CMAKE_BUILD_TYPE}/bin")
  SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_SYSTEM_PROCESSOR}/${CMAKE_BUILD_TYPE}/lib${LIB_SUFFIX}")
  #SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/objs/${CMAKE_SYSTEM_PROCESSOR}/${CMAKE_BUILD_TYPE}/lib${LIB_SUFFIX}" CACHE PATH "Static libraries output directory")
ENDIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
IF(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_SYSTEM_PROCESSOR}/${CMAKE_BUILD_TYPE}/bin")
  SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_SYSTEM_PROCESSOR}/${CMAKE_BUILD_TYPE}/lib${LIB_SUFFIX}")
  #SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/objs/${CMAKE_SYSTEM_PROCESSOR}/${CMAKE_BUILD_TYPE}/lib${LIB_SUFFIX}" CACHE PATH "Static libraries output directory")
ENDIF(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")

#Install icons and desktop file
if(UNIX)
  FOREACH(i 16 22 24 32 36 48 64 72 96 128 192 256 )
    INSTALL(FILES media/lightspark-ico-${i}x${i}.png
            DESTINATION ${DATADIR}/icons/hicolor/${i}x${i}/apps
            RENAME lightspark.png
           )
  ENDFOREACH(i)
  INSTALL(FILES media/lightspark-ico.svg
          DESTINATION ${DATADIR}/icons/hicolor/scalable/apps
          RENAME lightspark.svg
         )
  INSTALL(FILES media/lightspark.desktop DESTINATION ${DATADIR}/applications )
endif(UNIX)

if(UNIX)
	INSTALL(FILES etc/xdg/lightspark.conf
		      DESTINATION ${ETCDIR}/xdg/)
ELSE()
  INSTALL(FILES COPYING DESTINATION "." RENAME COPYING.txt)
  INSTALL(FILES COPYING.LESSER DESTINATION "." RENAME COPYING.LESSER.txt)
endif(UNIX)

SUBDIRS(src)

#-- CPack setup - use 'make package' to build
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Lightspark is an LGPLv3 licensed cross-platform Flash player and browser plugin")
SET(CPACK_PACKAGE_VENDOR "Lightspark Team")
SET(CPACK_PACKAGE_NAME "Lightspark")
SET(CPACK_PACKAGE_VERSION_MAJOR "${MAJOR_VERSION}")
SET(CPACK_PACKAGE_VERSION_MINOR "${MINOR_VERSION}")
IF(SUBPATCH_VERSION EQUAL "0")
  SET(CPACK_PACKAGE_VERSION_PATCH "${PATCH_VERSION}${GIT_VERSION}")
ELSE()
  SET(CPACK_PACKAGE_VERSION_PATCH "${PATCH_VERSION}.${SUBPATCH_VERSION}${GIT_VERSION}")
ENDIF()
SET(CPACK_NSIS_MENU_LINKS "")
#In release builds, we already strip symbols (see LDFLAGS), and compress by upx
#Therefore we cannot strip afterwards
SET(CPACK_STRIP_FILES FALSE)
SET(CPACK_RESOURCE_FILE_LICENSE ${PROJECT_SOURCE_DIR}/COPYING)
SET(CPACK_NSIS_HELP_LINK "http:////www.lightspark.org")
SET(CPACK_NSIS_URL_INFO_ABOUT "http:////www.lightspark.org")
SET(CPACK_PACKAGE_ICON ${PROJECT_SOURCE_DIR}/media/lightspark-logo.svg)
IF(WIN32)
  SET(CPACK_PACKAGE_INSTALL_DIRECTORY "Lightspark")
  SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "
  WriteRegStr HKCU 'Software\\\\MozillaPlugins\\\\@lightspark.github.com/Lightspark;version=1' 'Description' 'Lightspark'
  WriteRegStr HKCU 'Software\\\\MozillaPlugins\\\\@lightspark.github.com/Lightspark;version=1' 'ProductName' 'Lightspark'
  WriteRegStr HKCU 'Software\\\\MozillaPlugins\\\\@lightspark.github.com/Lightspark;version=1' 'Vendor' 'Lightspark Team'
  WriteRegStr HKCU 'Software\\\\MozillaPlugins\\\\@lightspark.github.com/Lightspark;version=1' 'Version' '1'
  WriteRegStr HKCU 'Software\\\\MozillaPlugins\\\\@lightspark.github.com/Lightspark;version=1' 'Path' '$INSTDIR\\\\nplightsparkplugin.dll'
  WriteRegStr HKCU 'Software\\\\MozillaPlugins\\\\@lightspark.github.com/Lightspark;version=1\\\\MimeTypes\\\\application/x-shockwave-flash' '' ''
  ")
  SET(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "
  DeleteRegKey HKLM 'Software\\\\MozillaPlugins\\\\@lightspark.github.com/Lightspark;version=1'
  ")
ENDIF(WIN32)
#Must come after the definitions
INCLUDE(CPack)

if (NOT TARGET uninstall)
  configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY)

  add_custom_target(uninstall
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()
