cmake_minimum_required(VERSION 3.10.2)

# Define GNU standard installation directories.
include(GNUInstallDirs)

if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
    set(COMMON_COMPILE_FLAGS "-Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
    set(COMMON_COMPILE_FLAGS "${COMMON_COMPILE_FLAGS} -fno-strict-aliasing -fno-builtin-memcmp")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 ${COMMON_COMPILE_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_COMPILE_FLAGS} -std=c++11 -fno-rtti")
    if (UNIX)
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
    endif()
endif()

file(GLOB TEXTURES
   "${PROJECT_SOURCE_DIR}/via/images/*"
)
file(COPY ${TEXTURES} DESTINATION ${CMAKE_BINARY_DIR}/via/images)


if(WIN32)
    set (LIBRARIES "vulkan-1")

    # For Windows, since 32-bit and 64-bit items can co-exist, we build each in its own build directory.
    # 32-bit target data goes in build32, and 64-bit target data goes into build.  So, include/link the
    # appropriate data at build time.
    if (CMAKE_CL_64)
        set (BUILDTGT_DIR build)
    else ()
        set (BUILDTGT_DIR build32)
    endif()

    # Use static MSVCRT libraries
    foreach(configuration in CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELWITHDEBINFO
                             CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELWITHDEBINFO)
        if(${configuration} MATCHES "/MD")
            string(REGEX REPLACE "/MD" "/MT" ${configuration} "${${configuration}}")
        endif()
    endforeach()

    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_WARNINGS -D_USE_MATH_DEFINES")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_CRT_SECURE_NO_WARNINGS -D_USE_MATH_DEFINES")

else()

    if(UNIX)
        set (LIBRARIES "vulkan")
    endif()

    if (BUILD_WSI_XCB_SUPPORT)
        find_package(XCB REQUIRED)

        include_directories(${XCB_INCLUDE_DIRS})
        link_libraries(${XCB_LIBRARIES})
    endif()
    if (BUILD_WSI_XLIB_SUPPORT)
        find_package(X11 REQUIRED)

        include_directories(${X11_INCLUDE_DIRS})
        link_libraries(${X11_LIBRARIES})
    endif()
    if (BUILD_WSI_WAYLAND_SUPPORT)
        find_package(Wayland REQUIRED)

        include_directories(${WAYLAND_CLIENT_INCLUDE_DIR})
        link_libraries(${WAYLAND_CLIENT_LIBRARIES})
    endif()

    # Disable the RPATH for VIA because we want it to use
    # the environment setup by the user
    SET(CMAKE_SKIP_BUILD_RPATH  TRUE)

    get_filename_component(LIB_DIR "../../x86_64/lib" ABSOLUTE)
    link_directories(${LIB_DIR})
    link_libraries(vulkan m dl)

    get_filename_component(VK_INC_DIR "${CMAKE_SOURCE_DIR}/../../x86_64/include" ABSOLUTE)
    include_directories(${VK_INC_DIR})
endif()


if(WIN32)
add_definitions(-DVIA_WINDOWS_TARGET)
add_executable(vkvia
                  via.cpp
                  via_system.hpp
                  via_system.cpp
                  via_system_windows.hpp
                  via_system_windows.cpp
                  ${JSONCPP_SOURCE_DIR}/jsoncpp.cpp)
else()
if(CMAKE_SYSTEM_NAME MATCHES "BSD")
   add_definitions(-DVIA_BSD_TARGET)
else()
   add_definitions(-DVIA_LINUX_TARGET)
endif()
add_executable(vkvia
                  via.cpp
                  via_system.hpp
                  via_system.cpp
		  via_system_bsd.cpp
                  via_system_linux.hpp
                  via_system_linux.cpp
                  ${JSONCPP_SOURCE_DIR}/jsoncpp.cpp)
endif()
target_include_directories(vkvia PUBLIC ${JSONCPP_INCLUDE_DIR})
target_link_libraries(vkvia ${LIBRARIES})
if(WIN32)
    target_link_libraries(vkvia version)
endif()
if(UNIX)
    install(TARGETS vkvia DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
