# The following variable can be defined on the command line:
#
#   SHARED
#   NDEBUG
#   XXH_HEADER_NAME
#   XXH_INCLUDE_DIR
#   LSHPACK_XXH

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(ls-hpack C)

include(CheckIncludeFiles)

IF (SHARED EQUAL 1)
    ADD_LIBRARY(ls-hpack SHARED lshpack.c)
ELSE()
    ADD_LIBRARY(ls-hpack STATIC lshpack.c)
ENDIF()
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})

IF (NOT DEFINED XXH_HEADER_NAME)
    SET(XXH_HEADER_NAME "xxhash.h")
ENDIF()

IF (DEFINED XXH_INCLUDE_DIR)
    INCLUDE_DIRECTORIES("${XXH_INCLUDE_DIR}")
ELSE()
    INCLUDE_DIRECTORIES(deps/xxhash)
ENDIF()

IF (LSHPACK_XXH)
    TARGET_SOURCES(ls-hpack PRIVATE deps/xxhash/xxhash.c)
ENDIF()

check_include_files("sys/queue.h" HAVE_SYS_QUEUE_H)

IF (NOT HAVE_SYS_QUEUE_H)
    INCLUDE_DIRECTORIES(compat/queue)
ENDIF()

IF (MSVC)
    INCLUDE_DIRECTORIES(compat/windows)
ENDIF()

IF (CMAKE_C_COMPILER_ID STREQUAL GNU
	OR CMAKE_C_COMPILER_ID STREQUAL Clang)
	SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wall -Wextra -Wno-unused-parameter")
	SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fno-omit-frame-pointer")
        INCLUDE(CheckCCompilerFlag)
        CHECK_C_COMPILER_FLAG(-Wno-implicit-fallthrough HAS_NO_IMPLICIT_FALLTHROUGH)
        IF (HAS_NO_IMPLICIT_FALLTHROUGH)
            SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Wno-implicit-fallthrough")
        ENDIF()
        IF (PROFILE EQUAL 1)
            SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -g -pg")
        ENDIF()
        IF (CMAKE_BUILD_TYPE STREQUAL Release)
            SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -g0 -O3")
        ELSE()
            SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -g3 -O0 -fsanitize=address")
        ENDIF()
ENDIF()

IF (NOT CMAKE_BUILD_TYPE STREQUAL Release)
    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLS_HPACK_EMIT_TEST_CODE=1")
ENDIF()

IF (CMAKE_BUILD_TYPE STREQUAL MinSizeRel)
    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLS_HPACK_USE_LARGE_TABLES=0")
ENDIF()

IF (NDEBUG EQUAL 1)
    SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DNDEBUG")
ENDIF()

SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DXXH_HEADER_NAME=\\\"${XXH_HEADER_NAME}\\\"")

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MY_CMAKE_FLAGS} $ENV{EXTRA_CFLAGS}")

MESSAGE(STATUS "Compiler flags: ${CMAKE_C_FLAGS}")

IF (NOT CMAKE_BUILD_TYPE STREQUAL Release)
    ENABLE_TESTING()
    INCLUDE_DIRECTORIES("test")
    ADD_SUBDIRECTORY("test")
ENDIF()

IF (CMAKE_SYSTEM_NAME STREQUAL Linux)
    ADD_SUBDIRECTORY(bin)
ENDIF()
