THRIFT-4732:refine cmake scripts (#1688)
* add cmake support for clib and remove unused variables
* add targets for code analysis
* add wall to all compilers and add utf-8 options for msvc
diff --git a/build/cmake/DefinePlatformSpecifc.cmake b/build/cmake/DefinePlatformSpecifc.cmake
index b87fd65..c0bb529 100644
--- a/build/cmake/DefinePlatformSpecifc.cmake
+++ b/build/cmake/DefinePlatformSpecifc.cmake
@@ -23,6 +23,31 @@
# For Debug build types, append a "d" to the library names.
set(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "Set debug library postfix" FORCE)
+# basic options
+foreach(lang IN ITEMS C CXX)
+ if(CMAKE_${lang}_COMPILER_ID STREQUAL "Clang")
+ set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS} -Wall")
+ set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS} -ferror-limit=1")
+ elseif(CMAKE_${lang}_COMPILER_ID STREQUAL "GNU")
+ set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS} -Wall -Wextra")
+ set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS} -fmax-errors=1")
+ elseif(CMAKE_${lang}_COMPILER_ID STREQUAL "MSVC")
+ set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS} /MP") # parallel build
+ set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS} /W3") # warning level 3
+ include(CheckCXXCompilerFlag)
+ set(CMAKE_REQUIRED_QUIET ON)
+ check_cxx_compiler_flag("/source-charset:utf-8" res_var)
+ if (res_var)
+ set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS} /source-charset:utf-8")
+ endif()
+ check_cxx_compiler_flag("/execution-charset:utf-8" res_var)
+ if (res_var)
+ set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS} /execution-charset:utf-8")
+ endif()
+ add_definitions("-DUNICODE -D_UNICODE")
+ endif()
+endforeach()
+
# Visual Studio specific options
if(MSVC)
# Allow for shared library builds
@@ -66,11 +91,6 @@
# Disable boost auto linking pragmas - cmake includes the right files
add_definitions("-DBOOST_ALL_NO_LIB")
-
- add_definitions("/MP") # parallel build
- add_definitions("/W3") # warning level 3
-
- add_definitions("-DUNICODE -D_UNICODE")
elseif(UNIX)
find_program( MEMORYCHECK_COMMAND valgrind )
set( MEMORYCHECK_COMMAND_OPTIONS "--gen-suppressions=all --leak-check=full" )
diff --git a/build/cmake/FindClangTools.cmake b/build/cmake/FindClangTools.cmake
new file mode 100644
index 0000000..b72bea7
--- /dev/null
+++ b/build/cmake/FindClangTools.cmake
@@ -0,0 +1,28 @@
+# - Try to find Clang tools
+#
+# The following are set after configuration is done:
+# clang-tidy_FOUND
+# ClangTools::clang-tidy
+# clang-apply-replacements_FOUND
+# ClangTools::clang-apply-replacements
+# run-clang-tidy_FOUND
+# ClangTools::run-clang-tidy
+
+include_guard()
+include(FindPackageHandleStandardArgs)
+
+foreach(program_name IN ITEMS clang-tidy clang-apply-replacements)
+ find_program(${program_name}_BINARY NAMES ${program_name}-devel ${program_name}-8 ${program_name} PATH_SUFFIXES "LLVM/bin")
+ find_package_handle_standard_args(${program_name} DEFAULT_MSG ${program_name}_BINARY)
+ if(${program_name}_FOUND AND NOT TARGET ClangTools::${program_name})
+ add_executable(ClangTools::${program_name} IMPORTED)
+ set_property(TARGET ClangTools::${program_name} PROPERTY IMPORTED_LOCATION "${${program_name}_BINARY}")
+ endif()
+endforeach()
+
+find_program(run-clang-tidy_BINARY NAMES run-clang-tidy run-clang-tidy.py PATH_SUFFIXES "LLVM/bin" "llvm-devel/share/clang")
+find_package_handle_standard_args(run-clang-tidy DEFAULT_MSG run-clang-tidy_BINARY)
+if(run-clang-tidy_FOUND AND NOT TARGET ClangTools::run-clang-tidy)
+ add_executable(ClangTools::run-clang-tidy IMPORTED)
+ set_property(TARGET ClangTools::run-clang-tidy PROPERTY IMPORTED_LOCATION "${run-clang-tidy_BINARY}")
+endif()
diff --git a/build/cmake/StaticCodeAnalysis.cmake b/build/cmake/StaticCodeAnalysis.cmake
new file mode 100644
index 0000000..3356c76
--- /dev/null
+++ b/build/cmake/StaticCodeAnalysis.cmake
@@ -0,0 +1,9 @@
+find_package(ClangTools QUIET)
+if(clang-tidy_FOUND AND run-clang-tidy_FOUND AND NOT TARGET do_run_clang_tidy)
+ add_custom_target(
+ do_run_clang_tidy
+ COMMAND ClangTools::run-clang-tidy -clang-tidy-binary "$<TARGET_FILE:ClangTools::clang-tidy>" -p ${CMAKE_BINARY_DIR} "-quiet" > ./run-clang-tidy.txt
+ DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json
+ WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+ )
+endif()