cmake_minimum_required(VERSION 3.5)

include(CheckCXXCompilerFlag)

function(append_if condition value)
  if (${condition})
    foreach(variable ${ARGN})
      set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
    endforeach(variable)
  endif()
endfunction()

project(z80)

enable_testing()

check_cxx_compiler_flag("-fdiagnostics-color" DIAGNOSTICS_COLOR_FLAG)
check_cxx_compiler_flag("-fno-exceptions" NO_EXCEPTIONS_FLAG)
check_cxx_compiler_flag("-fno-rtti" NO_RTTI_FLAG)
check_cxx_compiler_flag("-std=c++11" STD_CXX11_FLAG)
check_cxx_compiler_flag("-pedantic" PEDANTIC_FLAG)
check_cxx_compiler_flag("-Wall" WALL_FLAG)
check_cxx_compiler_flag("-Wextra" WEXTRA_FLAG)
check_cxx_compiler_flag("-W" W_FLAG)
check_cxx_compiler_flag("-Wconversion" WCONVERSION_FLAG)
check_cxx_compiler_flag("-Weverything" WEVERYTHING_FLAG)
check_cxx_compiler_flag("-Wno-c++98-compat" WNO_CXX98_COMPAT_FLAG)
check_cxx_compiler_flag("-Wno-c++98-compat-pedantic"
                        WNO_CXX98_COMPAT_PEDANTIC_FLAG)
check_cxx_compiler_flag("-Wno-shadow-field-in-constructor"
                        WNO_SHADOW_FIELD_IN_CONSTRUCTOR_FLAG)
check_cxx_compiler_flag("-Wno-shadow-field" WNO_SHADOW_FIELD)
check_cxx_compiler_flag("-Wno-padded" WNO_PADDED_FLAG)
check_cxx_compiler_flag("-Werror" WERROR_FLAG)
check_cxx_compiler_flag("-ferror-limit=1" ERROR_LIMIT_FLAG)
check_cxx_compiler_flag("-g" G_FLAG)

append_if(DIAGNOSTICS_COLOR_FLAG "-fdiagnostics-color" CMAKE_CXX_FLAGS)
append_if(NO_EXCEPTIONS_FLAG "-fno-exceptions" CMAKE_CXX_FLAGS)
append_if(NO_RTTI_FLAG "-fno-rtti" CMAKE_CXX_FLAGS)
append_if(STD_CXX11_FLAG "-std=c++11" CMAKE_CXX_FLAGS)
append_if(PEDANTIC_FLAG "-pedantic" CMAKE_CXX_FLAGS)
append_if(WALL_FLAG "-Wall" CMAKE_CXX_FLAGS)
append_if(WEXTRA_FLAG "-Wextra" CMAKE_CXX_FLAGS)
append_if(W_FLAG "-W" CMAKE_CXX_FLAGS)
append_if(WCONVERSION_FLAG "-Wconversion" CMAKE_CXX_FLAGS)
append_if(WEVERYTHING_FLAG "-Weverything" CMAKE_CXX_FLAGS)
append_if(WNO_CXX98_COMPAT_FLAG "-Wno-c++98-compat" CMAKE_CXX_FLAGS)
append_if(WNO_CXX98_COMPAT_PEDANTIC_FLAG "-Wno-c++98-compat-pedantic"
          CMAKE_CXX_FLAGS)
append_if(WNO_SHADOW_FIELD_IN_CONSTRUCTOR_FLAG
          "-Wno-shadow-field-in-constructor" CMAKE_CXX_FLAGS)
append_if(WNO_SHADOW_FIELD "-Wno-shadow-field" CMAKE_CXX_FLAGS)
append_if(WNO_PADDED_FLAG "-Wno-padded" CMAKE_CXX_FLAGS)
append_if(WERROR_FLAG "-Werror" CMAKE_CXX_FLAGS)
append_if(ERROR_LIMIT_FLAG "-ferror-limit=1" CMAKE_CXX_FLAGS)
append_if(G_FLAG "-g" CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${CMAKE_SOURCE_DIR}")

add_subdirectory(tests)
add_subdirectory(examples)
