From f64db80448564702925445be7a563dde3ecf5bab Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Tue, 31 Mar 2020 14:43:44 +0200 Subject: [PATCH 1/5] build: do not treat compiler warnings as errors for unsafe builds with msvc this is the same behaviour as with gcc and clang Signed-off-by: Carlos Gomes Martinho --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e3098cd9..c5fd56233 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -162,7 +162,7 @@ if(CMAKE_COMPILER_IS_IAR) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warn_about_c_style_casts --warnings_are_errors -Ohz") endif(CMAKE_COMPILER_IS_IAR) -if(CMAKE_COMPILER_IS_MSVC) +if(CMAKE_COMPILER_IS_MSVC AND NOT UNSAFE_BUILD) # Strictest warnings, and treat as errors set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX") From a5f0bd34b6cbcc7d50f849d9c9fcd4f5f2289916 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Fri, 3 Apr 2020 09:42:37 +0200 Subject: [PATCH 2/5] build: add option to treat compiler warnings as errors Signed-off-by: Carlos Gomes Martinho --- CMakeLists.txt | 32 +++++++++++++++++++++----------- library/CMakeLists.txt | 6 ------ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c5fd56233..3dd2270f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ option(ENABLE_ZLIB_SUPPORT "Build mbed TLS with zlib library." OFF) option(ENABLE_PROGRAMS "Build mbed TLS programs." ON) option(UNSAFE_BUILD "Allow unsafe builds. These builds ARE NOT SECURE." OFF) +option(MBEDTLS_FATAL_WARNINGS "Compiler warnings treated as errors" ON) string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}") string(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${CMAKE_C_COMPILER_ID}") @@ -140,9 +141,9 @@ if(CMAKE_COMPILER_IS_GNU) set(CMAKE_C_FLAGS_RELEASE "-O2") set(CMAKE_C_FLAGS_DEBUG "-O0 -g3") set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage") - set(CMAKE_C_FLAGS_ASAN "-Werror -fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3") - set(CMAKE_C_FLAGS_ASANDBG "-Werror -fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls") - set(CMAKE_C_FLAGS_CHECK "-Werror -Os") + set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3") + set(CMAKE_C_FLAGS_ASANDBG "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls") + set(CMAKE_C_FLAGS_CHECK "-Os") set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual") endif(CMAKE_COMPILER_IS_GNU) @@ -151,23 +152,32 @@ if(CMAKE_COMPILER_IS_CLANG) set(CMAKE_C_FLAGS_RELEASE "-O2") set(CMAKE_C_FLAGS_DEBUG "-O0 -g3") set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage") - set(CMAKE_C_FLAGS_ASAN "-Werror -fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3") - set(CMAKE_C_FLAGS_ASANDBG "-Werror -fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls") - set(CMAKE_C_FLAGS_MEMSAN "-Werror -fsanitize=memory -O3") - set(CMAKE_C_FLAGS_MEMSANDBG "-Werror -fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2") - set(CMAKE_C_FLAGS_CHECK "-Werror -Os") + set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3") + set(CMAKE_C_FLAGS_ASANDBG "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls") + set(CMAKE_C_FLAGS_MEMSAN "-fsanitize=memory -O3") + set(CMAKE_C_FLAGS_MEMSANDBG "-fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2") + set(CMAKE_C_FLAGS_CHECK "-Os") endif(CMAKE_COMPILER_IS_CLANG) if(CMAKE_COMPILER_IS_IAR) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warn_about_c_style_casts --warnings_are_errors -Ohz") endif(CMAKE_COMPILER_IS_IAR) -if(CMAKE_COMPILER_IS_MSVC AND NOT UNSAFE_BUILD) - # Strictest warnings, and treat as errors +if(CMAKE_COMPILER_IS_MSVC) + # Strictest warnings set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX") endif(CMAKE_COMPILER_IS_MSVC) +if(MBEDTLS_FATAL_WARNINGS) + if(CMAKE_COMPILER_IS_MSVC) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX") + endif(CMAKE_COMPILER_IS_MSVC) + + if(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") + endif(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU) +endif(MBEDTLS_FATAL_WARNINGS) + if(CMAKE_BUILD_TYPE STREQUAL "Coverage") if(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG) set(CMAKE_SHARED_LINKER_FLAGS "--coverage") diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 14dfa221b..cd6cbde98 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -113,12 +113,6 @@ if(CMAKE_COMPILER_IS_CLANG) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code") endif(CMAKE_COMPILER_IS_CLANG) -if(UNSAFE_BUILD) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error") - set(CMAKE_C_FLAGS_ASAN "${CMAKE_C_FLAGS_ASAN} -Wno-error") - set(CMAKE_C_FLAGS_ASANDBG "${CMAKE_C_FLAGS_ASANDBG} -Wno-error") -endif(UNSAFE_BUILD) - if(WIN32) set(libs ${libs} ws2_32) endif(WIN32) From 227a9db71e44b66e0bc368689e333ada805b5fa7 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Fri, 3 Apr 2020 09:42:57 +0200 Subject: [PATCH 3/5] docs: show cross platform instructions Signed-off-by: Carlos Gomes Martinho --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f868a0a80..2058d24d6 100644 --- a/README.md +++ b/README.md @@ -85,11 +85,11 @@ In order to build the source using CMake in a separate directory (recommended), mkdir /path/to/build_dir && cd /path/to/build_dir cmake /path/to/mbedtls_source - make + cmake --build . In order to run the tests, enter: - make test + ctest The test suites need Python to be built and Perl to be executed. If you don't have one of these installed, you'll want to disable the test suites with: From 77764da7cb71564dacf897632c31a1802f1509eb Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Fri, 3 Apr 2020 09:47:26 +0200 Subject: [PATCH 4/5] chore: irgnore build directory in repository Signed-off-by: Carlos Gomes Martinho --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 0f95b4c79..ee2cd4640 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,9 @@ massif-* # CMake generates *.dir/ folders for in-tree builds (used by MSVC projects), ignore all of those: *.dir/ +# Microsoft CMake extension for Visual Studio Code generates a build directory by default +/build/ + # Visual Studio artifacts /visualc/VS2010/.localhistory/ /visualc/VS2010/.vs/ From e150c77969755d59dcd78a8518a0e2dde6a95d6b Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Tue, 21 Apr 2020 22:15:01 +0200 Subject: [PATCH 5/5] build: readd unsafe build to show #warnings in CI Signed-off-by: Carlos Gomes Martinho --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3dd2270f6..88145f238 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -175,6 +175,11 @@ if(MBEDTLS_FATAL_WARNINGS) if(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") + if(UNSAFE_BUILD) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=cpp") + set(CMAKE_C_FLAGS_ASAN "${CMAKE_C_FLAGS_ASAN} -Wno-error=cpp") + set(CMAKE_C_FLAGS_ASANDBG "${CMAKE_C_FLAGS_ASANDBG} -Wno-error=cpp") + endif(UNSAFE_BUILD) endif(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU) endif(MBEDTLS_FATAL_WARNINGS)