cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

# Set build type. Do this *before* we set the project name
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
      "Choose the type of build, options are: None Debug Release RelWithDebInfo Profile."
      FORCE)
endif(NOT CMAKE_BUILD_TYPE)

set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE}" CACHE INTERNAL "internal")

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

project(sjaak C)

# Search in the `cmake' directory for additional CMake modules.
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

# Declare program options
option(WANT_WERROR "Treat warnings as errors" off)
option(WANT_NATIVE "Optimise for the current machine" off)
option(WANT_ASSERT "Enable assertions in the code (for debugging)" off)
option(WANT_LTO    "Link time optimisation on/off" on)
option(WANT_OPT    "Standard optimisations on/off" on)
option(WANT_FAST   "Agressive optimisations on/off" on)
option(WANT_POPCNT "Switch on hardware popcount" on)
option(WANT_SSE42  "Switch on SSE 4.2" on)
option(WANT_SSE3   "Switch on SSE 3" off)
option(WANT_SSE2   "Switch on SSE 2" off)
option(WANT_32BIT  "Force compiler to generate 32 bit code" off)
option(WANT_64BIT  "Force compiler to generate 64 bit code" off)
option(WANT_GUI    "Wether you want to build the GUI or not (requires Allegro)" off)
option(WANT_MGUI   "Wether you want to build the mobile GUI or not (requires Allegro) (experimental)" off)
option(WANT_XBOARD "Wether you want to build the XBoard interface" on)
option(WANT_CONSOLE  "Wether you want to build the standard console program" on)
option(WANT_PERFT  "Wether you want to build the perft (performance test) program" on)
option(WANT_DYNAMIC "Wether you want to build a dynamic version of the library" off)
option(WANT_STATIC  "Wether you want to static link to standard libraries" off)
option(WANT_PROFILE_GENERATE "Generate executable suitable for running profile-guided optimisation" off)
option(WANT_PROFILE_USE "Generate executable using data from profile run" off)
option(WANT_PROFILE "Generate executable suitable for running through a profiler" off)
option(WANT_RELEASE "Generate a binary with a mangled name to represent the current platform" off)

# Parse user options
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
if(WANT_OPT)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -finline")
endif(WANT_OPT)
if(WANT_LTO)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto -fwhole-program")
   set (CMAKE_LD_FLAGS "${CMAKE_LD_FLAGS} -flto -fwhole-program")
endif(WANT_LTO)
if(WANT_WERROR)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-unused -Werror")
endif(WANT_WERROR)
if(WANT_NATIVE)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native -mtune=native")
endif(WANT_NATIVE)
if(WANT_FAST)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fast")
endif(WANT_FAST)
if(WANT_POPCNT)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mpopcnt -DPOPCNT")
endif(WANT_POPCNT)
if(WANT_SSE42)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2")
endif(WANT_SSE42)
if(WANT_SSE3)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse3")
endif(WANT_SSE3)
if(WANT_SSE2)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse2")
endif(WANT_SSE2)
if(WANT_PROFILE_GENERATE)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-generate")
endif(WANT_PROFILE_GENERATE)
if(WANT_PROFILE_USE)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-use")
endif(WANT_PROFILE_USE)
if(WANT_ASSERT)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUGMODE")
endif(WANT_ASSERT)
if(WANT_32BIT)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
endif(WANT_32BIT)
if(WANT_64BIT)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")
endif(WANT_64BIT)
if(WANT_STATIC)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static")
endif(WANT_STATIC)
if(WANT_PROFILE)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg -g")
endif(WANT_PROFILE)

# Search for C header files in these directories.
include_directories(
   ${CMAKE_SOURCE_DIR}/include
   ${CMAKE_BINARY_DIR}/include
)

set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSVNVERSION=\\\"83\\\"")

if(WANT_GUI)
   set(WANT_ALLEGRO 1)
endif(WANT_GUI)

if(WANT_MGUI)
   set(WANT_ALLEGRO 1)
endif(WANT_MGUI)

if(WANT_ALLEGRO)
   # Find zlib
   find_package(ZLIB)
   if (ZLIB_FOUND)
      list(APPEND GUI_LIBS ${ZLIB_LIBRARIES})
      include_directories(SYSTEM ${ZLIB_INCLUDE_DIRS})
   else (ZLIB_FOUND)
      message(FATAL_ERROR "Can't find zlib library")
   endif(ZLIB_FOUND)


   # Find Allegro
   set(ALLEGRO5_VERSION "5.1")
   find_package(Allegro5)

   if (ALLEGRO5_FOUND)
      list(APPEND GUI_LIBS ${ALLEGRO5_LIBRARIES})
      include_directories(SYSTEM ${ALLEGRO5_INCLUDE_DIRS})
   else (ALLEGRO5_FOUND)
      message(FATAL_ERROR "Can't find Allegro library")
   endif(ALLEGRO5_FOUND)

   # Find Allegro addons
   find_package(Allegro5Main)
   if (ALLEGRO5MAIN_FOUND)
      list(APPEND GUI_LIBS ${ALLEGRO5MAIN_LIBRARIES})
      include_directories(SYSTEM ${ALLEGRO5MAIN_INCLUDE_DIRS})
   else (ALLEGRO5MAIN_FOUND)
      message(FATAL_ERROR "Can't find Allegro main addon")
   endif(ALLEGRO5MAIN_FOUND)

   find_package(Allegro5Primitives)
   if (ALLEGRO5PRIMITIVES_FOUND)
      list(APPEND GUI_LIBS ${ALLEGRO5PRIMITIVES_LIBRARIES})
      include_directories(SYSTEM ${ALLEGRO5PRIMITIVES_INCLUDE_DIRS})
   else (ALLEGRO5PRIMITIVES_FOUND)
      message(FATAL_ERROR "Can't find Allegro Primitives addon")
   endif(ALLEGRO5PRIMITIVES_FOUND)

   find_package(Allegro5Font)
   if (ALLEGRO5FONT_FOUND)
      list(APPEND GUI_LIBS ${ALLEGRO5FONT_LIBRARIES})
      include_directories(SYSTEM ${ALLEGRO5FONT_INCLUDE_DIRS})
   else (ALLEGRO5FONT_FOUND)
      message(FATAL_ERROR "Can't find Allegro font addon")
   endif(ALLEGRO5FONT_FOUND)

   find_package(Allegro5TTF)
   if (ALLEGRO5TTF_FOUND)
      list(APPEND GUI_LIBS ${ALLEGRO5TTF_LIBRARIES})
      include_directories(SYSTEM ${ALLEGRO5TTF_INCLUDE_DIRS})
   else (ALLEGRO5TTF_FOUND)
      message(FATAL_ERROR "Can't find Allegro TTF addon")
   endif(ALLEGRO5TTF_FOUND)

   find_package(Allegro5Image)
   if (ALLEGRO5IMAGE_FOUND)
      list(APPEND GUI_LIBS ${ALLEGRO5IMAGE_LIBRARIES})
      include_directories(SYSTEM ${ALLEGRO5IMAGE_INCLUDE_DIRS})
   else (ALLEGRO5IMAGE_FOUND)
      message(FATAL_ERROR "Can't find Allegro image addon")
   endif(ALLEGRO5IMAGE_FOUND)

   find_package(Allegro5Dialog)
   if (ALLEGRO5DIALOG_FOUND)
      list(APPEND GUI_LIBS ${ALLEGRO5DIALOG_LIBRARIES})
      include_directories(SYSTEM ${ALLEGRO5DIALOG_INCLUDE_DIRS})
   else (ALLEGRO5DIALOG_FOUND)
      message(FATAL_ERROR "Can't find Allegro DIALOG addon")
   endif(ALLEGRO5DIALOG_FOUND)
endif(WANT_ALLEGRO)

# code source files
include(FileList)

# Make a link library out of the source files
if(WANT_DYNAMIC)
   add_library ("libleonidas" SHARED ${LEONIDAS_SRC_FILES})
else(WANT_DYNAMIC)
   add_library ("libleonidas" STATIC ${LEONIDAS_SRC_FILES})
endif(WANT_DYNAMIC)

# Look for the standard math library
find_library(M_LIB m)
target_link_libraries("libleonidas" ${M_LIB})


# Main executable
if(WANT_XBOARD)
   add_executable ("xleo" src/xboard.c)
   target_link_libraries("xleo" libleonidas)
endif(WANT_XBOARD)

if(WANT_RELEASE)
   set(PLATF "-unknown")
   if(CMAKE_SYSTEM_NAME MATCHES "Windows")
      set(PLATF "-win")
   endif(CMAKE_SYSTEM_NAME MATCHES "Windows")
   if(CMAKE_SYSTEM_NAME MATCHES "Linux")
      set(PLATF "-linux")
   endif(CMAKE_SYSTEM_NAME MATCHES "Linux")
   if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
      set(PLATF "-mac")
   endif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
   set(SSE "-sse")
   if(WANT_SSE2)
      set(SSE "-sse2")
   endif(WANT_SSE2)
   if(WANT_SSE3)
      set(SSE "-sse3")
   endif(WANT_SSE3)
   if(WANT_SSE42)
      set(SSE "-sse42")
   endif(WANT_SSE42)

   set(BITS "")
   if(WANT_64BIT)
      set(BITS "64")
   endif(WANT_64BIT)
   if(WANT_32BIT)
      set(BITS "32")
   endif(WANT_32BIT)
   
   set(VERSION "-${Project_WC_REVISION}")
   
   set(RELEASE_NAME "xleo${VERSION}${PLATF}${BITS}${SSE}")
   set_target_properties(xleo PROPERTIES OUTPUT_NAME "${RELEASE_NAME}")
endif(WANT_RELEASE)

if(WANT_CONSOLE)
   add_executable ("cleo" src/leo.c)
   target_link_libraries("cleo" libleonidas)
endif(WANT_CONSOLE)

if(WANT_PERFT)
   add_executable ("leoperft" src/perft.c)
   target_link_libraries("leoperft" libleonidas)
endif(WANT_PERFT)

