diff --git a/CMakeLists.txt b/CMakeLists.txt index 40e555a..30e4eb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,62 @@ cmake_minimum_required(VERSION 2.8) add_definitions("-std=c++1y") +# Check if a supported compiler is used and add c++14 flag: +if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) + message(FATAL_ERROR "Need at least gcc 4.9 to compile.") + endif() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19) + message(FATAL_ERROR "Visual Studio 2015 or newer is required.") + endif() +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.4) + message(FATAL_ERROR "Clang below version 3.4 will most likely not work. Please upgrade your compiler.") + endif() + + if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") + endif() +else() # no GNU, no MSVC, no Clang + message(WARNING "You are using an unsupported compiler. Compilation has only been tested with MSVC, GCC and Clang.") + + include(CheckCXXCompilerFlag) + check_cxx_compiler_flag(-std=c++14 HAS_CXX14_FLAG) + if(HAS_CXX14_FLAG) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") + else() + message(FATAL_ERROR "Your compiler doesn't support the '-std=c++14' flag.") + endif() +endif() + + +if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" + OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") + set(warning_options -Weverything + -Wno-c++98-compat + -Wno-padded + -Wno-c++98-compat-pedantic + -Wno-exit-time-destructors + -Wno-missing-prototypes + -Wno-weak-vtables + -Wno-global-constructors + -Wno-format-nonliteral + -Wno-switch-enum + -Wno-missing-noreturn + -Wno-covered-switch-default + ) +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set(warning_options -Wall -Wpedantic -Wextra) +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + set(warning_options /WX /W4 /wd4503 /wd4512) +endif() + enable_testing() add_subdirectory(lint) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 7fb8cec..46b3621 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 2.8) include_directories(..) -add_definitions("-std=c++1y") +add_compile_options(${cxx11_options} ${warning_options}) add_executable(calc calc.cc) target_link_libraries(calc pthread) diff --git a/language/culebra/CMakeLists.txt b/language/culebra/CMakeLists.txt index d47cc47..2a19fbf 100644 --- a/language/culebra/CMakeLists.txt +++ b/language/culebra/CMakeLists.txt @@ -1,5 +1,4 @@ cmake_minimum_required(VERSION 2.8) include_directories(../..) -add_definitions("-std=c++1y") add_executable(culebra main.cc) diff --git a/language/pl0/CMakeLists.txt b/language/pl0/CMakeLists.txt index 2a049e8..0465843 100644 --- a/language/pl0/CMakeLists.txt +++ b/language/pl0/CMakeLists.txt @@ -1,5 +1,4 @@ cmake_minimum_required(VERSION 2.8) include_directories(../..) -add_definitions("-std=c++1y") add_executable(pl0 pl0.cc) diff --git a/lint/CMakeLists.txt b/lint/CMakeLists.txt index 1ec9399..d4e20ca 100644 --- a/lint/CMakeLists.txt +++ b/lint/CMakeLists.txt @@ -1,5 +1,4 @@ cmake_minimum_required(VERSION 2.8) include_directories(..) -add_definitions("-std=c++1y") add_executable(peglint peglint.cc server.cc) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7c372af..9d57012 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 2.8) include_directories(..) -add_definitions("-std=c++1y") +add_compile_options(${cxx11_options} ${warning_options}) add_executable(test-main test.cc) target_link_libraries(test-main pthread)