if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
  set(IS_CLANG_COMPILER TRUE)
endif()

if (UNIX OR MINGW)
  set(REPERTORY_COMMON_FLAG_LIST
    ${REPERTORY_COMMON_FLAG_LIST}
    -D_FILE_OFFSET_BITS=64
    -march=${REPERTORY_UNIX_ARCH}
    -mtune=generic
  )

  if (MINGW)
    set(REPERTORY_COMMON_FLAG_LIST
      ${REPERTORY_COMMON_FLAG_LIST}
      -Wa,-mbig-obj
      -flarge-source-files
    )
  endif()

  set(REPERTORY_COMMON_FLAG_LIST_DEBUG
    ${REPERTORY_COMMON_FLAG_LIST_DEBUG}
    -DDEBUG
    -D_DEBUG
    -Og
    -fno-omit-frame-pointer
    -g
    -gdwarf-4
  )

  set(REPERTORY_COMMON_FLAG_LIST_RELEASE
    ${REPERTORY_COMMON_FLAG_LIST_RELEASE}
    -O3
    -DNDEBUG
  )

  if (NOT IS_CLANG_COMPILER)
    set(REPERTORY_GCC_FLAGS 
      -Wall
      -Wcast-align
      -Wconversion
      -Wdouble-promotion
      -Wduplicated-branches
      -Wduplicated-cond
      -Wextra
      -Wformat=2
      -Wlogical-op
      -Wmisleading-indentation
      -Wnull-dereference
      -Wpedantic
      -Wshadow
      -Wsign-conversion
      -Wunused
    )

    set(REPERTORY_GCC_CXX_FLAGS 
      -Wnon-virtual-dtor
      -Wold-style-cast
      -Woverloaded-virtual
      -Wuseless-cast
    )
  endif()

  set(REPERTORY_C_FLAGS_LIST
    ${REPERTORY_C_FLAGS_LIST}
    ${REPERTORY_COMMON_FLAG_LIST}
  )

  set(REPERTORY_CXX_FLAGS_LIST
    ${REPERTORY_CXX_FLAGS_LIST}
    ${REPERTORY_COMMON_FLAG_LIST}
    -std=c++${CMAKE_CXX_STANDARD}
  )
endif()

list(JOIN REPERTORY_COMMON_FLAG_LIST_DEBUG " " REPERTORY_COMMON_FLAG_LIST_DEBUG)
list(JOIN REPERTORY_COMMON_FLAG_LIST_RELEASE " " REPERTORY_COMMON_FLAG_LIST_RELEASE)

list(JOIN REPERTORY_CXX_FLAGS_LIST " " REPERTORY_CXX_FLAGS_LIST)
list(JOIN REPERTORY_C_FLAGS_LIST " " REPERTORY_C_FLAGS_LIST)

list(JOIN REPERTORY_SHARED_LINKER_FLAGS_LIST " " REPERTORY_SHARED_LINKER_FLAGS_LIST)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${REPERTORY_CXX_FLAGS_LIST}")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${REPERTORY_C_FLAGS_LIST}")

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
  set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${REPERTORY_COMMON_FLAG_LIST_DEBUG}")
  set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${REPERTORY_COMMON_FLAG_LIST_DEBUG}")
else()
  set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${REPERTORY_COMMON_FLAG_LIST_RELEASE}")
  set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${REPERTORY_COMMON_FLAG_LIST_RELEASE}")
endif()

set (REPERTORY_CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} ${REPERTORY_COMMON_FLAG_LIST_DEBUG}")
set (REPERTORY_CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} ${REPERTORY_COMMON_FLAG_LIST_DEBUG}")
set (REPERTORY_CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} ${REPERTORY_COMMON_FLAG_LIST_RELEASE}")
set (REPERTORY_CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} ${REPERTORY_COMMON_FLAG_LIST_RELEASE}")

set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${REPERTORY_SHARED_LINKER_FLAGS_LIST}")
if (ALPINE_FOUND OR MINGW)
  set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static")
endif()
