list(APPEND PROJECT_COMMON_FLAG_LIST
  -D_FILE_OFFSET_BITS=64 
  -D_GNU_SOURCE
  -D_LARGEFILE64_SOURCE
  -D_LARGEFILE_SOURCE
  -D_TIME_BITS=64
  -march=${PROJECT_MARCH}
  -mtune=generic
)

if(PROJECT_IS_MINGW)
  list(APPEND PROJECT_COMMON_FLAG_LIST
    ${PROJECT_COMMON_FLAG_LIST}
    -Wa,-mbig-obj
    -flarge-source-files
  )
endif()

list(APPEND PROJECT_DEBUG_FLAG_LIST
  -DDEBUG
  -D_DEBUG
  -Og
  -fno-omit-frame-pointer
  -g
)

list(APPEND PROJECT_RELEASE_FLAG_LIST
  -DNDEBUG
  -D_NDEBUG
  -O3
)

list(APPEND PROJECT_CXXFLAGS_LIST 
  -Wall
  -Wcast-align
  -Wconversion
  -Wdouble-promotion
  -Wextra
  -Wformat=2
  -Wmisleading-indentation
  -Wnon-virtual-dtor
  -Wnull-dereference
  -Wold-style-cast
  -Woverloaded-virtual
  -Wpedantic
  -Wshadow
  -Wsign-conversion
  -Wunused
)

if (NOT PROJECT_IS_DARWIN)
  list(APPEND PROJECT_CXXFLAGS_LIST 
    -Wduplicated-branches
    -Wduplicated-cond
    -Wlogical-op
    -Wno-useless-cast
  )
endif()

list(APPEND PROJECT_CFLAGS_LIST
  ${PROJECT_COMMON_FLAG_LIST}
  -std=c${CMAKE_C_STANDARD}
)

list(APPEND PROJECT_CXXFLAGS_LIST
  ${PROJECT_COMMON_FLAG_LIST}
  -std=gnu++${CMAKE_CXX_STANDARD}
)

if(NOT PROJECT_IS_DARWIN AND PROJECT_STATIC_LINK)
  list(APPEND PROJECT_CMAKE_EXE_LINKER_FLAGS
    -static-libgcc
    -static-libstdc++
    -static
  )
endif()

list(JOIN PROJECT_COMMON_FLAG_LIST " " PROJECT_COMMON_FLAG_LIST)
list(JOIN PROJECT_DEBUG_FLAG_LIST " " PROJECT_DEBUG_FLAG_LIST)
list(JOIN PROJECT_RELEASE_FLAG_LIST " " PROJECT_RELEASE_FLAG_LIST)

list(JOIN PROJECT_CFLAGS_LIST " " PROJECT_CFLAGS_LIST)
list(JOIN PROJECT_CXXFLAGS_LIST " " PROJECT_CXXFLAGS_LIST)

list(JOIN PROJECT_CMAKE_EXE_LINKER_FLAGS " " PROJECT_CMAKE_EXE_LINKER_FLAGS)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${PROJECT_CFLAGS_LIST}")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${PROJECT_DEBUG_FLAG_LIST}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${PROJECT_RELEASE_FLAG_LIST}")

set(CMAKE_CXX_FLAGS "-include cstdint -include utility ${CMAKE_CXX_FLAGS} ${PROJECT_CXXFLAGS_LIST}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${PROJECT_DEBUG_FLAG_LIST}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${PROJECT_RELEASE_FLAG_LIST}")

set(CMAKE_EXE_LINKER_FLAGS "${PROJECT_CMAKE_EXE_LINKER_FLAGS}")

set(EXTERNAL_CMAKE_CXX_FLAGS "-include cstdint -include utility ${PROJECT_COMMON_FLAG_LIST}")
if (NOT PROJECT_IS_DARWIN)
  set(EXTERNAL_CMAKE_CXX_FLAGS "-fext-numeric-literals ${EXTERNAL_CMAKE_CXX_FLAGS}")
endif()

list(APPEND PROJECT_EXTERNAL_CMAKE_FLAGS
  -DCMAKE_BUILD_TYPE=${PROJECT_CMAKE_BUILD_TYPE}
  -DCMAKE_COLOR_MAKEFILE=${CMAKE_COLOR_MAKEFILE}
  -DCMAKE_CXX_FLAGS=${EXTERNAL_CMAKE_CXX_FLAGS}
  -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
  -DCMAKE_C_FLAGS=${PROJECT_COMMON_FLAG_LIST}
  -DCMAKE_C_STANDARD=${CMAKE_C_STANDARD}
  -DCMAKE_GENERATOR=${CMAKE_GENERATOR}
  -DCMAKE_INSTALL_PREFIX=${PROJECT_EXTERNAL_BUILD_ROOT}
  -DCMAKE_POSITION_INDEPENDENT_CODE=${CMAKE_POSITION_INDEPENDENT_CODE}
)

if(CMAKE_TOOLCHAIN_FILE)
  list(APPEND PROJECT_EXTERNAL_CMAKE_FLAGS 
    -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
  )
endif()

string(REPLACE ";" "|" CMAKE_PREFIX_PATH_ALT_SEP "${CMAKE_PREFIX_PATH}")
list(APPEND PROJECT_EXTERNAL_CMAKE_FLAGS 
  -DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH_ALT_SEP}
)
