cmake_minimum_required(VERSION 3.16)
project(c_gaborator
        DESCRIPTION "C++ to C simple bridge for Gaborator"
        VERSION 1.7)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_INSTALL_RPATH "$ORIGIN")
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
cmake_policy(SET CMP0069 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
cmake_policy(SET CMP0074 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0074 NEW)


if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb -O0 -fno-exceptions")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb -O0 -fno-exceptions")

if(EMSCRIPTEN)
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG -O3 -ffast-math -fno-exceptions")
    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DNDEBUG -O3 -ffast-math -fno-exceptions")
    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -DNDEBUG -O3 -ffast-math -fno-exceptions")
    set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -DNDEBUG -O3 -ffast-math -fno-exceptions")
else()
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG -Ofast -fno-exceptions")
    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DNDEBUG -Ofast -fno-exceptions")
    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -DNDEBUG -Ofast -fno-exceptions")
    set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -DNDEBUG -Ofast -fno-exceptions")

    if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
        set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -frecord-gcc-switches -Rpass=loop-vectorize -Rpass-missed=loop-vectorize -Rpass-analysis=loop-vectorize")
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -frecord-gcc-switches -Rpass=loop-vectorize -Rpass-missed=loop-vectorize -Rpass-analysis=loop-vectorize")
        set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -frecord-gcc-switches -Rpass=loop-vectorize -Rpass-missed=loop-vectorize -Rpass-analysis=loop-vectorize")
    elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
#        set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fipa-pta -ftree-loop-ivcanon -floop-nest-optimize -ftree-vectorize -ftree-loop-im -fgraphite-identity -floop-parallelize-all -fdevirtualize-speculatively -fdevirtualize-at-ltrans -ftree-parallelize-loops=4 -fopt-info-all -frecord-gcc-switches")
#        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fipa-pta -ftree-loop-ivcanon -floop-nest-optimize -ftree-vectorize -ftree-loop-im -fgraphite-identity -floop-parallelize-all -fdevirtualize-speculatively -fdevirtualize-at-ltrans -ftree-parallelize-loops=4 -fopt-info-all -frecord-gcc-switches")

        set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fvect-cost-model=unlimited -fipa-pta -ftree-loop-ivcanon -ftree-loop-im -fdevirtualize-speculatively -fdevirtualize-at-ltrans -fopt-info-all -frecord-gcc-switches")
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fvect-cost-model=unlimited -fipa-pta -ftree-loop-ivcanon -ftree-loop-im -fdevirtualize-speculatively -fdevirtualize-at-ltrans -fopt-info-all -frecord-gcc-switches")
        set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -fvect-cost-model=unlimited -fipa-pta -ftree-loop-ivcanon -ftree-loop-im -fdevirtualize-speculatively -fdevirtualize-at-ltrans -fopt-info-all -frecord-gcc-switches")
    endif()
endif()



option(USE_TYPE_FLOAT "" ON)
option(USE_TYPE_DOUBLE "" ON)
option(USE_SIMD "" ON)
option(USE_FFTPACK "" OFF)
option(USE_BENCH_POCKET "" OFF)
option(USE_BENCH_KISS "" OFF)
option(USE_BENCH_GREEN "" OFF)

include_directories(include)
include_directories(lib/gaborator)
include_directories(lib/pffft)
include_directories(lib/MIPP/src)

set(MIPP_ROOT "${CMAKE_SOURCE_DIR}/lib/MIPP/src")
add_subdirectory(lib/pffft EXCLUDE_FROM_ALL)

add_executable(test test.cpp)
add_library(cgaborator SHARED cgaborator.cpp)
set_target_properties(cgaborator PROPERTIES PUBLIC_HEADER "include/cgaborator.h")

target_link_libraries(cgaborator PFFFT)
target_link_libraries(test cgaborator)
add_test(test test)

set(target1 cgaborator)

set(pc_libs_private)
set(pc_req_private)
set(pc_req_public)

configure_file(cgaborator.pc.in cgaborator.pc @ONLY)

install(
        TARGETS cgaborator
        PUBLIC_HEADER DESTINATION include
)
install(
        FILES "${CMAKE_CURRENT_BINARY_DIR}/cgaborator.pc" DESTINATION lib/pkgconfig
)