PROJECT(projectM)
cmake_minimum_required(VERSION 2.6.0)
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "The projectM core library.")

include(CheckCXXCompilerFlag)

cmake_policy(SET CMP0005 OLD)

# This policy was only added in cmake 2.8.4
if (CMAKE_VERSION VERSION_GREATER "2.8.3")
  cmake_policy(SET CMP0017 OLD)
endif (CMAKE_VERSION VERSION_GREATER "2.8.3")


set(USE_DEVIL OFF)
set(USE_FBO ON)
set(USE_FTGL OFF)
set(USE_GLES1 OFF)
set(USE_THREADS OFF)
set(USE_OPENMP ON)
set(USE_NATIVE_GLEW OFF)
set(USE_CG OFF)
set(BUILD_PROJECTM_STATIC ON)
set(DISABLE_NATIVE_PRESETS ON)
set(DISABLE_MILKDROP_PRESETS OFF)

if(DISABLE_NATIVE_PRESETS)
  ADD_DEFINITIONS(-DDISABLE_NATIVE_PRESETS)
endif(DISABLE_NATIVE_PRESETS)

ADD_DEFINITIONS(-DCMAKE_INSTALL_PREFIX="\\\"${CMAKE_INSTALL_PREFIX}\\\"")

if (USE_NATIVE_GLEW)
  ADD_DEFINITIONS(-DUSE_NATIVE_GLEW)
  SET(GLEW_SOURCES glew.h glew.c)
  SET(GLEW_LIBRARIES )
else(USE_NATIVE_GLEW)
  SET(GLEW_SOURCES )
  find_library(GLEW_LIBRARIES NAMES glew GLEW glew32)
  find_path(GLEW_INCLUDE_DIRS GL/glew.h HINTS /sw/include)
  include_directories(${GLEW_INCLUDE_DIRS})
endif(USE_NATIVE_GLEW)

SET(projectM_SOURCES projectM.cpp PCM.cpp Preset.cpp fftsg.cpp KeyHandler.cpp
timer.cpp wipemalloc.cpp PresetLoader.cpp  PresetChooser.cpp PipelineMerger.cpp ConfigFile.cpp  TimeKeeper.cpp PresetFactory.cpp PresetFactoryManager.cpp ${GLEW_SOURCES})

if (MSVC)
SET(projectM_SOURCES ${projectM_SOURCES} dlfcn.c win32-dirent.cpp)
endif(MSVC)

if (NOT DISABLE_NATIVE_PRESETS)
add_subdirectory(NativePresetFactory)
SET(PRESET_FACTORY_SOURCES ${PRESET_FACTORY_SOURCES} ${NativePresetFactory_SOURCE_DIR})
SET(PRESET_FACTORY_BINARY_DIR ${PRESET_FACTORY_BINARY_DIR} ${NativePresetFactory_BINARY_DIR})
SET(PRESET_FACTORY_LINK_TARGETS ${PRESET_FACTORY_LINK_TARGETS} NativePresetFactory)
endif(NOT DISABLE_NATIVE_PRESETS)

if (NOT DISABLE_MILKDROP_PRESETS)
add_subdirectory(MilkdropPresetFactory)
SET(PRESET_FACTORY_SOURCES ${PRESET_FACTORY_SOURCES} ${MilkdropPresetFactory_SOURCE_DIR})
SET(PRESET_FACTORY_BINARY_DIR ${PRESET_FACTORY_BINARY_DIR} ${MilkdropPrmaesetFactory_BINARY_DIR})
SET(PRESET_FACTORY_LINK_TARGETS ${PRESET_FACTORY_LINK_TARGETS} MilkdropPresetFactory)
endif(NOT DISABLE_MILKDROP_PRESETS)

if (USE_DEVIL)
SET (projectM_SOURCES ${projectM_SOURCES})
ADD_DEFINITIONS(-DUSE_DEVIL)
SET (IMAGE_LINK_TARGETS IL ILU ILUT)
else (USE_DEVIL)
SET (projectM_SOURCES ${projectM_SOURCES})
SET (IMAGE_LINK_TARGETS )
endif (USE_DEVIL)

if (USE_CG)
ADD_DEFINITIONS(-DUSE_CG)
SET (CG_LINK_TARGETS Cg CgGL)
else (USE_CG)
SET (CG_LINK_TARGETS)
endif(USE_CG)


if(USE_GLES1)
ADD_DEFINITIONS(-DUSE_GLES1)
endif(USE_GLES1)

if(USE_FBO)
ADD_DEFINITIONS(-DUSE_FBO)
endif(USE_FBO)

if(USE_FTGL)
  ADD_DEFINITIONS(-DUSE_FTGL)

  pkg_check_modules(FREETYPE2 freetype2)
  pkg_check_modules(FTGL ftgl)
  include_directories( ${FREETYPE2_INCLUDE_DIRS} )
endif(USE_FTGL)


if(COMMAND cmake_policy)
       cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)

if (BUILD_PROJECTM_STATIC)
ADD_LIBRARY(projectM STATIC ${projectM_SOURCES})
else(BUILD_PROJECTM_STATIC)
ADD_LIBRARY(projectM SHARED ${projectM_SOURCES})
endif(BUILD_PROJECTM_STATIC)

SET_TARGET_PROPERTIES(projectM PROPERTIES VERSION 2.00 SOVERSION 2)
set_target_properties(projectM PROPERTIES COMPILE_FLAGS "-Wno-unused-value")

if (USE_THREADS)
ADD_DEFINITIONS(-DUSE_THREADS)
endif(USE_THREADS)

if (APPLE)
ADD_DEFINITIONS(-DMACOS -DSTBI_NO_DDS)
set(RESOURCE_PREFIX "Resources")
else (APPLE)
if (WIN32)
set (RESOURCE_PREFIX "Resources")
ADD_DEFINITIONS(-DWIN32 -DSTBI_NO_DDS)
else(WIN32)
set (RESOURCE_PREFIX "share/projectM")
ADD_DEFINITIONS(-DLINUX -DSTBI_NO_DDS)
endif(WIN32)
endif(APPLE)

add_subdirectory(Renderer)

#ADD_DEFINITIONS(-DCMAKE_INSTALL_PREFIX="\\\"${CMAKE_INSTALL_PREFIX}\\\"") 

FIND_PACKAGE(X11)
FIND_PACKAGE(OpenGL)


#set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH};/opt/local/include;/Developer/SDKs/MACOSX10.5.sdk/usr/X11/include)
#set(CMAKE_LIBRARY_PATH /opt/local/lib;/Developer/SDKs/MACOSX10.5.sdk;/Developer/SDKs/MacOSX10.5.sdk/usr/X11/lib/)

CHECK_CXX_COMPILER_FLAG("-fopenmp" SUPPORTS_OPENMP)

IF(USE_OPENMP AND SUPPORTS_OPENMP)
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp ")
	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp ")
ENDIF(USE_OPENMP AND SUPPORTS_OPENMP)

INCLUDE_DIRECTORIES(${projectM_SOURCE_DIR} ${FTGL_INCLUDE_DIRS} ${X11_INCLUDE_DIR} ${Renderer_SOURCE_DIR} ${PRESET_FACTORY_SOURCES})
LINK_DIRECTORIES(${FTGL_LIBRARY_DIRS} ${Renderer_BINARY_DIR} ${PRESET_FACTORY_BINARY_DIR})

if(MSVC)
	SET (MATH_LIBRARIES )
else(MSVC)
	SET (MATH_LIBRARIES m)
endif(MSVC)

if(BUILD_PROJECTM_STATIC)
    TARGET_LINK_LIBRARIES(projectM ${GLEW_LIBRARIES} ${MATH_LIBRARIES} ${FTGL_LIBRARIES} ${OPENGL_LIBRARIES} ${IMAGE_LINK_TARGETS} ${CG_LINK_TARGETS} ${PRESET_FACTORY_LINK_TARGETS})
else(BUILD_PROJECTM_STATIC)
  TARGET_LINK_LIBRARIES(projectM ${GLEW_LIBRARIES} ${MATH_LIBRARIES} ${FTGL_LIBRARIES} ${OPENGL_LIBRARIES}  ${IMAGE_LINK_TARGETS} ${CG_LINK_TARGETS} ${PRESET_FACTORY_LINK_TARGETS})
endif(BUILD_PROJECTM_STATIC)


CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/libprojectM.pc.in" "${CMAKE_CURRENT_SOURCE_DIR}/libprojectM.pc" @ONLY)
CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/config.inp.in" "${CMAKE_CURRENT_SOURCE_DIR}/config.inp" @ONLY)

if(BUNDLE_PROJECTM_PRESETS)
  FILE(GLOB presets "presets/*.milk" "presets/*.prjm" "presets/*.tga")
  if (APPLE)
    foreach (preset ${presets})
      get_filename_component(PRESET_NAME ${preset} NAME)
      configure_file(
        "${preset}"
        "${CMAKE_BINARY_DIR}/clementine.app/Contents/Resources/projectm-presets/${PRESET_NAME}"
        COPY_ONLY
      )
    endforeach (preset)
  else (APPLE)
    INSTALL(FILES ${presets} DESTINATION ${CMAKE_INSTALL_PREFIX}/share/clementine/projectm-presets)
  endif (APPLE)
endif(BUNDLE_PROJECTM_PRESETS)
