PROJECT(TA3D)
#
# --- Settings ---
#
SET(CPACK_PACKAGE_VERSION_MAJOR "0")
SET(CPACK_PACKAGE_VERSION_MINOR "5")
SET(CPACK_PACKAGE_VERSION_PATCH "1")

SET(TA3D_VERSION_HI      "${CPACK_PACKAGE_VERSION_MAJOR}")
SET(TA3D_VERSION_LO      "${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
SET(TA3D_VERSION_TAG     "")
SET(CPACK_PACKAGE_VERSION "${TA3D_VERSION_HI}.${TA3D_VERSION_LO}")

SET(TA3D_WEBSITE                "http://www.ta3d.org")
SET(TA3D_WEBSITE_FORUM          "http://www.ta3d.org/forums/")
SET(TA3D_MAIL_BUGREPORT         "zuzuf86@gmail.com")
SET(TA3D_WEBSITE_NEW_BUGREPORT  "http://trac.ta3d.org/newticket")

#
# Regression tests
#
SET(TA3D_REGRESSION_TEST 0)



#
# -----------------------------------------------------------------------------
#

SET(PACKAGE_BUGREPORT "${TA3D_MAIL_BUGREPORT}")
SET(PACKAGE_VERSION "${TA3D_VERSION_HI}.${TA3D_VERSION_LO}")
SET(PACKAGE_STRING "TA3D ${PACKAGE_VERSION}")
SET(TA3D_ENGINE_VERSION "${PACKAGE_STRING}")

# Getting the latest revision number
IF(WIN32 AND NOT CMAKE_CROSSCOMPILING)
    EXECUTE_PROCESS(COMMAND "subwcrev" "${PROJECT_SOURCE_DIR}" OUTPUT_VARIABLE SVN_REV)
    STRING(REGEX REPLACE ".* committed at revision ([0-9]+).*" "\\1" SVN_REV "${SVN_REV}")
ELSE(WIN32 AND NOT CMAKE_CROSSCOMPILING)
    EXECUTE_PROCESS(COMMAND "svnversion" "-n" "${PROJECT_SOURCE_DIR}" OUTPUT_VARIABLE SVN_REV)
    STRING(REGEX REPLACE "[0-9]+:" "" SVN_REV "${SVN_REV}")
    STRING(REPLACE "M" "" SVN_REV "${SVN_REV}")
ENDIF(WIN32 AND NOT CMAKE_CROSSCOMPILING)
MESSAGE(STATUS "TA3D -  v${TA3D_VERSION_HI}.${TA3D_VERSION_LO} (Rev: ${SVN_REV})")

IF(SVN_REV)
    SET(CPACK_PACKAGE_VERSION "${TA3D_VERSION_HI}.${TA3D_VERSION_LO}")
ENDIF(SVN_REV)

cmake_minimum_required(VERSION 2.6)
#SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(LIBS "")


Message(STATUS "System: ${CMAKE_SYSTEM} (${CMAKE_SYSTEM_PROCESSOR})")


# We want -O3 -g -ffast-math as default options, but we want to be able to override CXX_FLAGS from commandline
string( LENGTH "${CMAKE_CXX_FLAGS}" VA )

IF( ${VA} EQUAL 0 )
    IF(WIN32)
	    SET( CMAKE_CXX_FLAGS "-O3 -g -ffast-math " )
    ELSE(WIN32)
	    SET( CMAKE_CXX_FLAGS "-O3 -g -Wall -ffast-math " )
    ENDIF(WIN32)
ENDIF( ${VA} EQUAL 0 )

message(STATUS "Default C++ flags set to `${CMAKE_CXX_FLAGS}`")



#
# DEBUG
#
MESSAGE(STATUS "[DEBUG] The Debug mode has been enabled")
ADD_DEFINITIONS("-DLOGS_USE_DEBUG")

#
# AutoTest
#
IF(${TA3D_REGRESSION_TEST} EQUAL 1)
    MESSAGE(STATUS "[DEBUG] The regression tests will be compiled")
ENDIF(${TA3D_REGRESSION_TEST} EQUAL 1)


#
# Platform
#
IF(UNIX AND NOT CMAKE_CROSSCOMPILING)
    IF(APPLE)
        ADD_DEFINITIONS("-DTA3D_PLATFORM_DARWIN -DTA3D_PLATFORM_MAC")
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
    ELSE(APPLE)
    	SET(LINUX 1)
        ADD_DEFINITIONS("-DTA3D_PLATFORM_LINUX")
    ENDIF(APPLE)
    ADD_DEFINITIONS("-DTA3D_PLATFORM_UNIX -DTA3D_PLATFORM_GCC")
ELSE(UNIX AND NOT CMAKE_CROSSCOMPILING)
    IF(WIN32)
        ADD_DEFINITIONS("-DTA3D_PLATFORM_WINDOWS -DTA3D_PLATFORM_WIN32")
    ELSE(WIN32)
        # Win64 ?
    ENDIF(WIN32)
ENDIF(UNIX AND NOT CMAKE_CROSSCOMPILING)


Include( CheckIncludeFile )


#
# OpenGL
#
Include(FindOpenGL)
SET(OPEN_PREFIX "[OpenGL]")
IF(NOT OPENGL_FOUND)
    message( FATAL_ERROR "${OPEN_PREFIX} Libraries not found !!" )
ENDIF(NOT OPENGL_FOUND)

# Includes
IF(NOT WIN32 AND NOT CMAKE_CROSSCOMPILING)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${OPENGL_INCLUDE_DIR}")
ENDIF(NOT WIN32 AND NOT CMAKE_CROSSCOMPILING)

# Infos
MESSAGE(STATUS "${OPEN_PREFIX} Mesa: ${OPENGL_XMESA_FOUND}")
MESSAGE(STATUS "${OPEN_PREFIX} GLu: ${OPENGL_GLU_FOUND}")
MESSAGE(STATUS "${OPEN_PREFIX} Include dir: ${OPENGL_INCLUDE_DIR}")
MESSAGE(STATUS "${OPEN_PREFIX} The GL library: ${OPENGL_gl_LIBRARY}")
MESSAGE(STATUS "${OPEN_PREFIX} The GLU library: ${OPENGL_glu_LIBRARY}")

# Frameworks / Libraries
IF(APPLE)
    MESSAGE(STATUS "${OPEN_PREFIX} Added Frameworks: AGL, OpenGL, Carbon")
    LINK_LIBRARIES("-framework AGL -framework OpenGL -framework Carbon")
ELSE(APPLE)
    STRING(REPLACE ";" "\n      .  " OPENGL_LIBRAIRIES_MSG "${OPENGL_LIBRARIES}")
    MESSAGE(STATUS "${OPEN_PREFIX} Added Libraries :")
    MESSAGE("      .  ${OPENGL_LIBRAIRIES_MSG}")
    LINK_LIBRARIES(${OPENGL_LIBRARIES})
ENDIF(APPLE)






#
# Allegro
#
IF(UNIX)
    FIND_PROGRAM(ALLEGRO_CONFIG_BIN NAME allegro-config PATHS /opt/local/bin /opt/sw/local/bin)
    IF(${ALLEGRO_CONFIG_BIN} MATCHES "ALLEGRO_CONFIG_BIN-NOTFOUND")
        MESSAGE(FATAL_ERROR "Allegro >= 4.2.2 is required")
    ENDIF(${ALLEGRO_CONFIG_BIN} MATCHES "ALLEGRO_CONFIG_BIN-NOTFOUND")
    SET(CMAKE_HAVE_ALLEGRO_H 1)
	EXECUTE_PROCESS(COMMAND ${ALLEGRO_CONFIG_BIN} --cppflags release
        OUTPUT_VARIABLE ALLEGRO_CONFIG_FLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
    STRING(REPLACE '\n' "" ALLEGRO_CONFIG_BIN "${ALLEGRO_CONFIG_BIN}")
    IF( NOT CMAKE_MINOR_VERSION EQUAL 4 OR NOT CMAKE_MAJOR_VERSION EQUAL 2 )
        STRING(STRIP ${ALLEGRO_CONFIG_BIN} ALLEGRO_CONFIG_BIN)
    ENDIF( NOT CMAKE_MINOR_VERSION EQUAL 4 OR NOT CMAKE_MAJOR_VERSION EQUAL 2 )
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ALLEGRO_CONFIG_FLAGS}")
ELSE(UNIX)
    IF(WIN32 AND NOT CMAKE_CROSSCOMPILING)
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${CMAKE_SOURCE_DIR}/src/tools/win32/mingw32/include/allegro")
		SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${CMAKE_SOURCE_DIR}/src/tools/win32/mingw32/include/allegrogl")
        LINK_DIRECTORIES("${CMAKE_SOURCE_DIR}/src/tools/win32/mingw32/libs")
    ELSE(WIN32 AND NOT CMAKE_CROSSCOMPILING)
        CHECK_INCLUDE_FILE( allegro.h CMAKE_HAVE_ALLEGRO_H)
        IF(NOT CMAKE_HAVE_ALLEGRO_H AND NOT CMAKE_CROSSCOMPILING )
	    MESSAGE( FATAL_ERROR "Allegro >= 4.2.2 is required" )
        ENDIF(NOT CMAKE_HAVE_ALLEGRO_H AND NOT CMAKE_CROSSCOMPILING )
    ENDIF(WIN32 AND NOT CMAKE_CROSSCOMPILING)
ENDIF(UNIX)





IF(NOT WIN32 AND NOT CMAKE_CROSSCOMPILING)
    find_library(VAR_LIB_ALLEG_GL agl )
    IF( NOT VAR_LIB_ALLEG_GL )
	message( FATAL_ERROR "AllegroGL >= 0.4.1 is required" )
    ENDIF( NOT VAR_LIB_ALLEG_GL )
ENDIF(NOT WIN32 AND NOT CMAKE_CROSSCOMPILING)

link_directories( ${TA3D_SOURCE_DIR}/src/lua/ ${TA3D_SOURCE_DIR}/src/hawknl/src/ )


IF( WIN32 OR CMAKE_CROSSCOMPILING )
	SET( ALLEGRO_CONFIG "-lalleg" )

	IF( CMAKE_CROSSCOMPILING )
		SET( target i586-mingw32msvc- )
	ENDIF( CMAKE_CROSSCOMPILING )

	FIND_PROGRAM(MAKE_WIN "mingw32-make.exe")
	FIND_PROGRAM(RANLIB_WIN "ranlib.exe")

	IF( NOT BUILT_LUA )
		EXECUTE_PROCESS(WORKING_DIRECTORY "${TA3D_SOURCE_DIR}/src/lua" COMMAND ${MAKE_WIN} clean)
		EXECUTE_PROCESS(WORKING_DIRECTORY "${TA3D_SOURCE_DIR}/src/lua" COMMAND ${MAKE_WIN} mingw CC=${CMAKE_C_COMPILER} RANLIB="${RANLIB_WIN}")
		SET( BUILT_LUA 1 CACHE BOOL "LUA has been built" )
	ENDIF( NOT BUILT_LUA )

	IF( NOT BUILT_NL )
		EXECUTE_PROCESS(WORKING_DIRECTORY "${TA3D_SOURCE_DIR}/src/hawknl/src" COMMAND ${MAKE_WIN} -f makefile.mingw clean RESULT_VARIABLE Piko)
		EXECUTE_PROCESS(WORKING_DIRECTORY "${TA3D_SOURCE_DIR}/src/hawknl/src" COMMAND ${MAKE_WIN} -f makefile.mingw CC=${CMAKE_C_COMPILER} )
		SET( BUILT_NL 1 CACHE BOOL "HawkNL has been built" )
		MESSAGE(${Piko})
	ENDIF( NOT BUILT_NL )
	#EXECUTE_PROCESS( COMMAND cp src/lua/lua51.dll ./ )

    	IF( NOT BUILT_LUA )
	    EXECUTE_PROCESS( COMMAND make -C "src/lua/" clean)
	    EXECUTE_PROCESS( COMMAND make -C "src/lua/" mingw)
	    SET( BUILT_LUA 1 CACHE BOOL "LUA has been built")
	ENDIF( NOT BUILT_LUA )
	EXECUTE_PROCESS( COMMAND cp src/hawknl/src/NL.dll ./ )
    EXECUTE_PROCESS( COMMAND cp src/tools/win32/mingw32/libs/alleg42.dll ./ )
	#SET( LIBS "-llua -lagl_s -lalleg_s -luser32 -lgdi32 -lopengl32 -lglu32 -lz.dll -lNL ${OPENGL_LIBRARIES}")
	SET( LIBS "-llua -lagl -lalleg -luser32 -lgdi32 -lopengl32 -lglu32 -lzlib -lNL")
	IF( MSVC )
#		Do we need something there ?
#	    	LINK_DIRECTORIES("${CMAKE_SOURCE_DIR}/src/tools/win32/msvc/static/")
	ELSE( MSVC )
	    	LINK_DIRECTORIES("${CMAKE_SOURCE_DIR}/src/tools/win32/mingw32/static/")
	ENDIF( MSVC )
ELSE( WIN32 OR CMAKE_CROSSCOMPILING )
    IF(NOT APPLE)
    	IF( NOT BUILT_LUA )
	    	EXECUTE_PROCESS( COMMAND make -C "src/lua/" clean )
	    	EXECUTE_PROCESS( COMMAND make -C "src/lua/" linux )
	    	SET( BUILT_LUA 1 CACHE BOOL "LUA has been built" )
	    ENDIF( NOT BUILT_LUA )
    ENDIF(NOT APPLE)

	EXECUTE_PROCESS( COMMAND allegro-config --libs release OUTPUT_VARIABLE ALLEGRO_CONFIG )
    STRING(REPLACE '\n' "" ALLEGRO_CONFIG "${ALLEGRO_CONFIG}")
    IF( NOT CMAKE_MINOR_VERSION EQUAL 4 OR NOT CMAKE_MAJOR_VERSION EQUAL 2 )
        STRING(STRIP ${ALLEGRO_CONFIG} ALLEGRO_CONFIG)
    ENDIF( NOT CMAKE_MINOR_VERSION EQUAL 4 OR NOT CMAKE_MAJOR_VERSION EQUAL 2 )
    SET(LIBS "-lagl -llua -lz -lNL ${ALLEGRO_CONFIG}")
    IF(NOT APPLE)
	    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -rdynamic -ffast-math")
    ENDIF(NOT APPLE)
ENDIF( WIN32 OR CMAKE_CROSSCOMPILING )



#
# --- HawkNL ---
#
IF (NOT BUILT_NL )
   IF(LINUX)
       EXECUTE_PROCESS( COMMAND make -C "src/hawknl/src/" -f makefile.linux clean )
       EXECUTE_PROCESS( COMMAND make -C "src/hawknl/src/" -f makefile.linux )
   ENDIF(LINUX)
   IF(APPLE)
       EXECUTE_PROCESS( COMMAND make -C "src/hawknl/src/" -f makefile.osx clean )
       EXECUTE_PROCESS( COMMAND make -C "src/hawknl/src/" -f makefile.osx )
   ENDIF(APPLE)
   IF(WIN32)
       EXECUTE_PROCESS( COMMAND set CC=gcc COMMAND make -C "src/hawknl/src/" -f makefile.mingw clean )
       EXECUTE_PROCESS( COMMAND set CC=gcc COMMAND make -C "src/hawknl/src/" -f makefile.mingw )
   ENDIF(WIN32)
   SET( BUILT_NL 1 CACHE BOOL "HawkNL has been built" )
ENDIF(NOT BUILT_NL )



#
# --- ZLib ---
#
IF(WIN32)
	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${TA3D_SOURCE_DIR}/src/tools/win32/mingw32/include/zlib")
ENDIF(WIN32)

#
# --- FMod ---
#
IF(WIN32)
    IF(MSVC)
        FIND_LIBRARY(VAR_LIB_FMODEX fmodex PATHS "${TA3D_SOURCE_DIR}/src/tools/win32/msvc/libs" NO_SYSTEM_ENVIRONMENT_PATH)
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${TA3D_SOURCE_DIR}/src/tools/win32/msvc/include/fmod")
    ELSE(MSVC)
        FIND_LIBRARY(VAR_LIB_FMODEX fmodex PATHS "${TA3D_SOURCE_DIR}/src/tools/win32/mingw32/libs" NO_SYSTEM_ENVIRONMENT_PATH)
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${TA3D_SOURCE_DIR}/src/tools/win32/mingw32/include/fmod")
    ENDIF(MSVC)
ELSE(WIN32) 
    IF(APPLE)
		SET(FMOD_LIB_DARWIN_FOLDER "${TA3D_SOURCE_DIR}/src/tools/darwin/fmod/4.18.04/lib/")
        FIND_LIBRARY(VAR_LIB_FMODEX fmodex PATHS "${FMOD_LIB_DARWIN_FOLDER}" NO_SYSTEM_ENVIRONMENT_PATH)
        IF(NOT VAR_LIB_FMODEX)
            MESSAGE(FATAL_ERROR "FMOD is required")
        ENDIF(NOT VAR_LIB_FMODEX)
		LINK_DIRECTORIES("${FMOD_LIB_DARWIN_FOLDER}")
    ELSE(APPLE)
        FIND_LIBRARY( VAR_LIB_FMODEX   fmodex)
        FIND_LIBRARY( VAR_LIB_FMODEX64 fmodex64)
    ENDIF(APPLE)
ENDIF(WIN32) 
 




SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${TA3D_SOURCE_DIR})
# for compatibility with old versions of cmake
SET(EXECUTABLE_OUTPUT_PATH ${TA3D_SOURCE_DIR})


#
# --- Installation process ---
#

IF(NOT WIN32 AND UNIX)
  message("-- TA3D will be installed in: ${CMAKE_INSTALL_PREFIX}")
ENDIF(NOT WIN32 AND UNIX)

SET(TA3D_RESOURCES_INSTALL_PATH "share/games/ta3d")
SET(TA3D_BINARIES_INSTALL_PATH "games")

install(DIRECTORY docs DESTINATION ${TA3D_RESOURCES_INSTALL_PATH} PATTERN ".*" EXCLUDE PATTERN "*~" EXCLUDE)
install(DIRECTORY gfx DESTINATION ${TA3D_RESOURCES_INSTALL_PATH} PATTERN ".*" EXCLUDE PATTERN "*~" EXCLUDE)
install(DIRECTORY gui DESTINATION ${TA3D_RESOURCES_INSTALL_PATH} PATTERN ".*" EXCLUDE PATTERN "*~" EXCLUDE)
install(DIRECTORY mods/ta3d DESTINATION ${TA3D_RESOURCES_INSTALL_PATH}/mods PATTERN ".*" EXCLUDE PATTERN "*~" EXCLUDE)
install(DIRECTORY objects3d DESTINATION ${TA3D_RESOURCES_INSTALL_PATH} PATTERN ".*" EXCLUDE PATTERN "*~" EXCLUDE)
install(DIRECTORY resources DESTINATION ${TA3D_RESOURCES_INSTALL_PATH} PATTERN ".*" EXCLUDE PATTERN "*~" EXCLUDE)
install(DIRECTORY scripts DESTINATION ${TA3D_RESOURCES_INSTALL_PATH} PATTERN ".*" EXCLUDE PATTERN "*~" EXCLUDE PATTERN "__campaign_script.lua" EXCLUDE)
install(DIRECTORY shaders DESTINATION ${TA3D_RESOURCES_INSTALL_PATH} PATTERN ".*" EXCLUDE PATTERN "*~" EXCLUDE)
install(DIRECTORY sky DESTINATION ${TA3D_RESOURCES_INSTALL_PATH} PATTERN ".*" EXCLUDE PATTERN "*~" EXCLUDE)

# copy the file that will add TA3D and 3DMEditor to the desktop menu
install(FILES distrib/linux/ta3d DESTINATION share/menu)
install(FILES distrib/linux/ta3d.desktop DESTINATION share/applications)
install(FILES ta3d.png DESTINATION ${TA3D_RESOURCES_INSTALL_PATH})
install(FILES distrib/linux/3dmeditor.desktop DESTINATION share/applications)
install(FILES 3dmeditor.png DESTINATION ${TA3D_RESOURCES_INSTALL_PATH})
install(FILES readme.html DESTINATION ${TA3D_RESOURCES_INSTALL_PATH})
install(FILES readme_fr.html DESTINATION ${TA3D_RESOURCES_INSTALL_PATH})
install(FILES TODO DESTINATION ${TA3D_RESOURCES_INSTALL_PATH})
install(FILES AUTHORS DESTINATION ${TA3D_RESOURCES_INSTALL_PATH})
install(FILES README DESTINATION ${TA3D_RESOURCES_INSTALL_PATH})
install(FILES NEWS DESTINATION ${TA3D_RESOURCES_INSTALL_PATH})
install(FILES ta3d.mod DESTINATION ${TA3D_RESOURCES_INSTALL_PATH})
install(FILES ChangeLog DESTINATION ${TA3D_RESOURCES_INSTALL_PATH})

# Things are set to work after installation with packages (Linux only)

IF(NOT WIN32 AND NOT APPLE)
    SET(TA3D_RESOURCES_PATH "/usr/${TA3D_RESOURCES_INSTALL_PATH}/")
    SET(TA3D_OVERRIDE_PATHS true)
ELSE(NOT WIN32 AND NOT APPLE)
    SET(TA3D_RESOURCES_PATH "${TA3D_SOURCE_DIR}/")
    SET(TA3D_OVERRIDE_PATHS false)
ENDIF(NOT WIN32 AND NOT APPLE)

#
# --- CPACK configuration ---
#

SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "TA3D (Total Annihilation 3D), RTS Engine")
SET(CPACK_PACKAGE_VENDOR "the TA3D team") 
SET(CPACK_PACKAGE_DESCRIPTION_FILE "${TA3D_SOURCE_DIR}/README")
SET(CPACK_RESOURCE_FILE_LICENSE "${TA3D_SOURCE_DIR}/COPYING")
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "${PACKAGE_STRING}")
SET(CPACK_GENERATOR "STGZ;DEB;RPM")
SET(CPACK_PACKAGE_CONTACT "${TA3D_MAIL_BUGREPORT}")
SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "Roland Brochard")
SET(CPACK_DEBIAN_PACKAGE_SECTION "games")
SET(CPACK_DEBIAN_PACKAGE_DEPENDS "liballegro4.2(>=4.2.2), allegrogl(>=0.4.4), fmodex")

IF(NOT WIN32 AND UNIX)
  SET(CPACK_STRIP_FILES "ta3d" "3dmeditor")
  SET(CPACK_SOURCE_STRIP_FILES "")
ENDIF(NOT WIN32 AND UNIX)
  SET(CPACK_PACKAGE_EXECUTABLES "ta3d" "3dmeditor")
INCLUDE(CPack)


CONFIGURE_FILE( ${TA3D_SOURCE_DIR}/config.h.cmake ${TA3D_SOURCE_DIR}/config.h )

ADD_SUBDIRECTORY(src)
