From 2e74db5447f89745f4bdc375230e20a7247670a0 Mon Sep 17 00:00:00 2001 From: iphydf Date: Sun, 11 Feb 2018 00:09:07 +0000 Subject: [PATCH] Improve gtest finding, support local checkout. Also fix library dependency order for monolith test. --- CMakeLists.txt | 48 +++++++++++++------------------------ cmake/CompileGTest.cmake | 49 ++++++++++++++++++++++++++++++++++++++ other/astyle/format-source | 7 +++++- third_party/.gitignore | 1 + third_party/README.md | 6 +++++ 5 files changed, 78 insertions(+), 33 deletions(-) create mode 100644 cmake/CompileGTest.cmake create mode 100644 third_party/.gitignore create mode 100644 third_party/README.md diff --git a/CMakeLists.txt b/CMakeLists.txt index 9450ab74..e6d9e2a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,8 +14,8 @@ # ################################################################################ -cmake_minimum_required(VERSION 2.8.6) -cmake_policy(VERSION 2.8.6) +cmake_minimum_required(VERSION 3.1.0) +cmake_policy(VERSION 3.1.0) project(toxcore) set(CMAKE_MODULE_PATH ${toxcore_SOURCE_DIR}/cmake) @@ -66,11 +66,16 @@ enable_testing() set(CMAKE_MACOSX_RPATH ON) -if(NOT MSVC) - # Set standard version for compiler. - add_cflag("-std=c99") - add_cxxflag("-std=c++11") +# Set standard version for compiler. +set(CMAKE_C_STANDARD 99) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_C_EXTENSIONS OFF) +set(CMAKE_CXX_EXTENSIONS OFF) +message(STATUS "Supported C compiler features = ${CMAKE_C_COMPILE_FEATURES}") +message(STATUS "Supported C++ compiler features = ${CMAKE_CXX_COMPILE_FEATURES}") + +if(NOT MSVC) # Warn on non-ISO C. add_cflag("-pedantic") @@ -436,35 +441,13 @@ install_module(toxcore DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tox) # ################################################################################ -# Compile the GTest library. -# -if(EXISTS "/usr/src/gtest/src/gtest-all.cc") - add_library(gtest - /usr/src/gtest/src/gtest-all.cc - /usr/src/gtest/src/gtest_main.cc) - include_directories(/usr/src/gtest/include) - target_include_directories(gtest PRIVATE /usr/src/gtest) - check_cxx_compiler_flag("-w" HAVE_CXX_W QUIET) - check_cxx_compiler_flag("-Wno-global-constructors" HAVE_CXX_W_NO_GLOBAL_CONSTRUCTORS QUIET) - check_cxx_compiler_flag("-Wno-zero-as-null-pointer-constant" HAVE_CXX_W_NO_ZERO_AS_NULL_POINTER_CONSTANT QUIET) - if(HAVE_CXX_W) - set_target_properties(gtest PROPERTIES COMPILE_FLAGS "-w") - endif() - set(HAVE_GTEST TRUE) -endif() +include(CompileGTest) function(unit_test subdir target) if(HAVE_GTEST) add_executable(unit_${target}_test ${subdir}/${target}_test.cpp) target_link_modules(unit_${target}_test ${toxcore_SUBLIBS} gtest) - set(gtest_CFLAGS "") - if(HAVE_CXX_W_NO_GLOBAL_CONSTRUCTORS) - set(gtest_CFLAGS "${gtest_CFLAGS} -Wno-global-constructors") - endif() - if(HAVE_CXX_W_NO_ZERO_AS_NULL_POINTER_CONSTANT) - set(gtest_CFLAGS "${gtest_CFLAGS} -Wno-zero-as-null-pointer-constant") - endif() - set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${gtest_CFLAGS}") + set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}") add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test) endif() endfunction() @@ -518,10 +501,11 @@ if(BUILD_TOXAV) auto_tests/monolith_test.cpp ${ANDROID_CPU_FEATURES}) target_link_modules(auto_monolith_test - ${toxcore_PKGCONFIG_LIBS} ${LIBSODIUM_LIBRARIES} ${OPUS_LIBRARIES} - ${VPX_LIBRARIES}) + ${VPX_LIBRARIES} + ${toxcore_PKGCONFIG_LIBS} + ) add_test(NAME monolith COMMAND ${CROSSCOMPILING_EMULATOR} auto_monolith_test) if(ANDROID_CPU_FEATURES) diff --git a/cmake/CompileGTest.cmake b/cmake/CompileGTest.cmake new file mode 100644 index 00000000..520ca06a --- /dev/null +++ b/cmake/CompileGTest.cmake @@ -0,0 +1,49 @@ +# Find and compile the GTest library. + +message(STATUS "Checking for gtest") + +# Look for the sources. +find_file(GTEST_ALL_CC gtest-all.cc PATHS + ${CMAKE_SOURCE_DIR}/third_party/googletest/googletest/src + /usr/src/gtest/src + NO_DEFAULT_PATH +) + +if(GTEST_ALL_CC) + # ../.. from the source file is the source root. + get_filename_component(GTEST_SRC_DIR ${GTEST_ALL_CC} DIRECTORY) + get_filename_component(GTEST_SRC_ROOT ${GTEST_SRC_DIR} DIRECTORY) + + # Look for the header file. + include(CheckIncludeFileCXX) + include_directories(SYSTEM ${GTEST_SRC_ROOT}/include) + check_include_file_cxx("gtest/gtest.h" HAVE_GTEST_GTEST_H) + + if(HAVE_GTEST_GTEST_H) + message(STATUS "Found gtest: ${GTEST_SRC_ROOT}") + + add_library(gtest + ${GTEST_SRC_DIR}/gtest-all.cc + ${GTEST_SRC_DIR}/gtest_main.cc) + target_include_directories(gtest PRIVATE ${GTEST_SRC_ROOT}) + + # Ignore all warnings for gtest. We don't care about their implementation. + check_cxx_compiler_flag("-w" HAVE_CXX_W QUIET) + if(HAVE_CXX_W) + set_target_properties(gtest PROPERTIES COMPILE_FLAGS "-w") + endif() + + set(HAVE_GTEST TRUE) + set(TEST_CXX_FLAGS "") + + check_cxx_compiler_flag("-Wno-global-constructors" HAVE_CXX_W_NO_GLOBAL_CONSTRUCTORS QUIET) + if(HAVE_CXX_W_NO_GLOBAL_CONSTRUCTORS) + set(TEST_CXX_FLAGS "${TEST_CXX_FLAGS} -Wno-global-constructors") + endif() + + check_cxx_compiler_flag("-Wno-zero-as-null-pointer-constant" HAVE_CXX_W_NO_ZERO_AS_NULL_POINTER_CONSTANT QUIET) + if(HAVE_CXX_W_NO_ZERO_AS_NULL_POINTER_CONSTANT) + set(TEST_CXX_FLAGS "${TEST_CXX_FLAGS} -Wno-zero-as-null-pointer-constant") + endif() + endif() +endif() diff --git a/other/astyle/format-source b/other/astyle/format-source index 3757561b..cf190982 100755 --- a/other/astyle/format-source +++ b/other/astyle/format-source @@ -45,7 +45,12 @@ if grep '' */*.h; then exit 1 fi -SOURCES=`find . "-(" -name "*.[ch]" -or -name "*.cpp" "-)" -and -not -name "*.api.h" -and -not -wholename "*crypto_pwhash*" -and -not -wholename "./super_donators/*"` +SOURCES=`find . \ + "-(" -name "*.[ch]" -or -name "*.cpp" "-)" \ + -and -not -name "*.api.h" \ + -and -not -wholename "./super_donators/*" \ + -and -not -wholename "./third_party/*" \ + -and -not -wholename "./toxencryptsave/crypto_pwhash*"` $ASTYLE -n --options=other/astyle/astylerc $SOURCES diff --git a/third_party/.gitignore b/third_party/.gitignore new file mode 100644 index 00000000..355164c1 --- /dev/null +++ b/third_party/.gitignore @@ -0,0 +1 @@ +*/ diff --git a/third_party/README.md b/third_party/README.md new file mode 100644 index 00000000..11546589 --- /dev/null +++ b/third_party/README.md @@ -0,0 +1,6 @@ +# Third party dependencies + +All toxcore dependencies you want to build yourself should end up here, not in +the source root. This directory is exempt from code style checks. + +TODO(iphydf): Change appveyor.yml to unpack dependencies here.