cmake_minimum_required(VERSION 2.8)

project(gl4es LANGUAGES C)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY})

option(PANDORA "Set to ON if targeting an OpenPandora device" ${PANDORA})
option(BCMHOST "Set to ON if targeting an RPi(2) device" ${BCMHOST})
option(ODROID "Set to ON if targeting an ODroid device" ${ODROID})
option(CHIP "Set to ON if targeting an C.H.I.P. device" ${CHIP})
option(AMIGAOS4 "Set to ON if targeting an AmigaOS4/Warp3D platform (activate NOEGL and NOX11)" ${AMIGAOS4})
option(NOX11 "Set to ON to not use X11 (creation of context has to be done outside gl4es)" ${NOX11})
option(NOEGL "Set to ON to not use EGL (all functions are taken in GLES library)" ${NOEGL})
option(STATICLIB "Set to ON to build a static version of gl4es" ${STATICLIB})
option(GBM "Set to ON to not build GBM interface" ${GBM})
option(USE_CCACHE "Set to ON to use ccache if present in the system" ${USE_CCACHE})

# Pandora
if(PANDORA)
    add_definitions(-DPANDORA)
    add_definitions(-DTEXSTREAM)
    add_definitions(-mcpu=cortex-a8 -mfpu=neon -mfloat-abi=softfp -ftree-vectorize -fsingle-precision-constant -ffast-math)
    set(NO_GBM ON)
endif()

if((NOT GBM) OR PANDORA)
    set(NO_GBM ON)
else()
    set(NO_GBM OFF)
endif()


if(NOT DEFAULT_ES)
    if(PANDORA)
        set(DEFAULT_ES 1)
    else()
        set(DEFAULT_ES 2)
    endif()
endif()

# Raspberry PI
if(BCMHOST)
    include_directories(/opt/vc/include /opt/vc/include/interface/vcos/pthreads /opt/vc/include/interface/vmcs_host/linux)
    link_directories(/opt/vc/lib)
    add_definitions(-DBCMHOST)
endif()

# ODROID
if(ODROID)
    add_definitions(-DODROID)
endif()

#PocketCHIP
if(CHIP)
    add_definitions(-DCHIP)
    add_definitions(-mcpu=cortex-a8 -mfpu=neon -mfloat-abi=hard -ftree-vectorize -fsingle-precision-constant -ffast-math)
endif()

# AmigaOS4
if(AMIGAOS4)
    set(CMAKE_C_COMPILER "ppc-amigaos-gcc")
    set(CMAKE_CXX_COMPILER "ppc-amigaos-g++")
    set(CMAKE_LINKER "ppc-amigaos-ld")
    set(CMAKE_AR "ppc-amigaos-ar")
    set(CMAKE_RANLIB "ppc-amigaos-ranlib")
    add_definitions(-DAMIGAOS4)
    set(NOX11 ON)
    set(NOEGL ON)
    set(NO_GBM ON)
endif()

#NOX11
if(NOX11)
    add_definitions(-DNOX11)
endif()

#NOEGL
if(NOEGL)
    add_definitions(-DNOEGL)
    add_definitions(-DNOX11)
endif()

if(NOT NO_GBM)
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(KMSDRM REQUIRED libdrm gbm egl)
    link_directories(${KMSDRM_LIBRARY_DIRS})
    include_directories(${KMSDRM_INCLUDE_DIRS})
endif()

if(NO_GBM)
    add_definitions(-DNO_GBM)
endif()

#DEFAULT_ES=2
if(DEFAULT_ES EQUAL 2)
    add_definitions(-DDEFAULT_ES=2)
endif()
if(DEFAULT_ES EQUAL 1)
    add_definitions(-DDEFAULT_ES=1)
endif()

if(USE_CCACHE)
    find_program(CCACHE_FOUND ccache)
    if(CCACHE_FOUND)
        set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
        set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
    endif()
endif()

link_directories(${CMAKE_BINARY_DIR}/lib)
add_definitions(-std=gnu99 -funwind-tables -fvisibility=hidden)

include_directories(include)
add_subdirectory(src)

enable_testing()

macro(create_test test_name test_filename calls_count tolerance)
    if (${ARGC} EQUAL 5)
        add_test(${test_name}
            ${CMAKE_COMMAND}
            -D LIBRARY_FOLDER=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
            -D TESTS_DIRECTORY=${CMAKE_SOURCE_DIR}/tests
            -D TEST_FILENAME=${test_filename}
            -D CALLS=${calls_count}
            -D TOLERANCE=${tolerance}
            -D EXTRACT_RANGE=${ARGV4}
            -P ${CMAKE_SOURCE_DIR}/test.cmake)
    elseif (${ARGC} EQUAL 6)
        if (${ARGV4} STREQUAL "NOEXTRACT_RANGE")
            add_test(${test_name}
                ${CMAKE_COMMAND}
                -D LIBRARY_FOLDER=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
                -D TESTS_DIRECTORY=${CMAKE_SOURCE_DIR}/tests
                -D TEST_FILENAME=${test_filename}
                -D CALLS=${calls_count}
                -D TOLERANCE_GLES1=${tolerance}
                -D TOLERANCE_GLES2=${ARGV5}
                -P ${CMAKE_SOURCE_DIR}/test.cmake)
        else (${ARGV4} STREQUAL "NOEXTRACT_RANGE")
        add_test(${test_name}
            ${CMAKE_COMMAND}
            -D LIBRARY_FOLDER=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
            -D TESTS_DIRECTORY=${CMAKE_SOURCE_DIR}/tests
            -D TEST_FILENAME=${test_filename}
            -D CALLS=${calls_count}
            -D TOLERANCE_GLES1=${tolerance}
            -D TOLERANCE_GLES2=${ARGV5}
            -D EXTRACT_RANGE=${ARGV4}
            -P ${CMAKE_SOURCE_DIR}/test.cmake)
        endif (${ARGV4} STREQUAL "NOEXTRACT_RANGE")
    else (${ARGC} EQUAL 5)
        add_test(${test_name}
            ${CMAKE_COMMAND}
            -D LIBRARY_FOLDER=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
            -D TESTS_DIRECTORY=${CMAKE_SOURCE_DIR}/tests
            -D TEST_FILENAME=${test_filename}
            -D CALLS=${calls_count}
            -D TOLERANCE=${tolerance}
            -P ${CMAKE_SOURCE_DIR}/test.cmake)
    endif (${ARGC} EQUAL 5)
endmacro(create_test)

macro(create_test_GLES test_name GLES test_filename calls_count tolerance)
    if (${ARGC} EQUAL 5)
        add_test(${test_name}_GLES${GLES}
            ${CMAKE_COMMAND}
            -D LIBRARY_FOLDER=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
            -D TESTS_DIRECTORY=${CMAKE_SOURCE_DIR}/tests
            -D TEST_FILENAME=${test_filename}
            -D CALLS=${calls_count}
            -D TOLERANCE=${tolerance}
            -D EXTRACT_RANGE=${ARGV4}
            -D GLES_FORCED=${GLES}
            -P ${CMAKE_SOURCE_DIR}/test.cmake)
    else (${ARGC} EQUAL 5)
        add_test(${test_name}_GLES${GLES}
            ${CMAKE_COMMAND}
            -D LIBRARY_FOLDER=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
            -D TESTS_DIRECTORY=${CMAKE_SOURCE_DIR}/tests
            -D TEST_FILENAME=${test_filename}
            -D CALLS=${calls_count}
            -D TOLERANCE=${tolerance}
            -D GLES_FORCED=${GLES}
            -P ${CMAKE_SOURCE_DIR}/test.cmake)
    endif (${ARGC} EQUAL 5)
endmacro(create_test_GLES)

create_test(GLXgears glxgears "0000008203" 25 "NOEXTRACT_RANGE" 700)
create_test(StuntCarRacer stuntcarracer "0000118817" 20 "638x478+1+1")
create_test(Neverball neverball "0000078750" 20 "798x478+1+1" 200)
create_test(FoobillardPlus foobillardplus "0000014748" 20 "798x478+1+1" 60)
create_test(PointSprite pointsprite "0000248810" 20)

create_test_GLES(OpenRA 2 openra "0000031249" 20 "638x478+1+1")
create_test_GLES(GLSL_lighting 2 glsl_lighting "0000505393" 20)
