Merge pull request #169 from mayah/windows-no-return
[platform/upstream/glog.git] / CMakeLists.txt
index a1978c4..7ede6e7 100644 (file)
@@ -4,6 +4,10 @@ if (POLICY CMP0042)
   cmake_policy (SET CMP0042 NEW)
 endif (POLICY CMP0042)
 
+if (POLICY CMP0063)
+  cmake_policy (SET CMP0063 NEW)
+endif (POLICY CMP0063)
+
 project (google-glog)
 
 enable_testing ()
@@ -23,11 +27,10 @@ set (CPACK_PACKAGE_VERSION_PATCH ${GLOG_PATCH_VERSION})
 set (CPACK_PACKAGE_VERSION ${GLOG_VERSION})
 
 option (WITH_GFLAGS "Use gflags" ON)
+option (WITH_THREADS "Enable multithreading support" ON)
 
 list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
 
-include (CMakePackageConfigHelpers)
-include (CPack)
 include (CheckCSourceCompiles)
 include (CheckCXXCompilerFlag)
 include (CheckCXXSourceCompiles)
@@ -38,6 +41,9 @@ include (CheckLibraryExists)
 include (CheckStructHasMember)
 include (CheckSymbolExists)
 include (CheckTypeSize)
+include (CMakePackageConfigHelpers)
+include (CPack)
+include (CTest)
 include (DetermineGflagsNamespace)
 
 set (CMAKE_THREAD_PREFER_PTHREAD 1)
@@ -51,7 +57,9 @@ if (WITH_GFLAGS)
   endif (gflags_FOUND)
 endif (WITH_GFLAGS)
 
-find_package (Threads)
+if (WITH_THREADS)
+  find_package (Threads)
+endif (WITH_THREADS)
 
 check_include_file (dlfcn.h HAVE_DLFCN_H)
 check_include_file (execinfo.h HAVE_EXECINFO_H)
@@ -94,8 +102,14 @@ check_function_exists (pwrite HAVE_PWRITE)
 check_function_exists (sigaction HAVE_SIGACTION)
 check_function_exists (sigaltstack HAVE_SIGALSTACK)
 
-check_cxx_compiler_flag (-Wno-deprecated HAVE_NO_DEPRECATED)
-check_cxx_compiler_flag (-Wno-unnamed-type-template-args
+# NOTE gcc does not fail if you pass a non-existent -Wno-* option as an
+# argument. However, it will happily fail if you pass the corresponding -W*
+# option. So, we check whether options that disable warnings exist by testing
+# the availability of the corresponding option that enables the warning. This
+# eliminates the need to check for compiler for several (mainly Clang) options.
+
+check_cxx_compiler_flag (-Wdeprecated HAVE_NO_DEPRECATED)
+check_cxx_compiler_flag (-Wunnamed-type-template-args
     HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS)
 
 # NOTE: Cannot use check_function_exists here since >=vc-14.0 can define
@@ -306,9 +320,13 @@ endif (HAVE_USING_OPERATOR)
 
 set (SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
 
-if (Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
-  set (HAVE_PTHREAD 1)
-endif (Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
+if (WITH_THREADS AND Threads_FOUND)
+  if (CMAKE_USE_PTHREADS_INIT)
+    set (HAVE_PTHREAD 1)
+  endif (CMAKE_USE_PTHREADS_INIT)
+else (WITH_THREADS AND Threads_FOUND)
+  set (NO_THREADS 1)
+endif (WITH_THREADS AND Threads_FOUND)
 
 set (TEST_SRC_DIR \"${CMAKE_CURRENT_SOURCE_DIR}\")
 
@@ -357,25 +375,29 @@ if (WIN32)
   )
 endif (WIN32)
 
+add_compile_options ($<$<BOOL:${HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS}>:-Wno-unnamed-type-template-args>)
+
 add_library (glog
   ${GLOG_SRCS}
 )
 
+set_target_properties (glog PROPERTIES POSITION_INDEPENDENT_CODE ON)
+
 if (UNWIND_LIBRARY)
   target_link_libraries (glog PUBLIC ${UNWIND_LIBRARY})
 endif (UNWIND_LIBRARY)
 
+if (HAVE_PTHREAD)
+  target_link_libraries (glog PUBLIC ${CMAKE_THREAD_LIBS_INIT})
+endif (HAVE_PTHREAD)
+
 if (WIN32 AND HAVE_SNPRINTF)
   set_property (SOURCE src/windows/port.cc APPEND PROPERTY COMPILE_DEFINITIONS
     HAVE_SNPRINTF)
 endif (WIN32 AND HAVE_SNPRINTF)
 
-if (HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS)
-  target_compile_options (glog PUBLIC -Wno-unnamed-type-template-args)
-endif (HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS)
-
 if (gflags_FOUND)
-  target_include_directories (glog PUBLIC ${gflags_INCLUDE_DIR})
+  target_include_directories (glog PUBLIC $<BUILD_INTERFACE:${gflags_INCLUDE_DIR}>)
   target_link_libraries (glog PUBLIC ${gflags_LIBRARIES})
 
   if (NOT BUILD_SHARED_LIBS)
@@ -396,6 +418,7 @@ set_target_properties (glog PROPERTIES PUBLIC_HEADER "${GLOG_PUBLIC_H}")
 target_include_directories (glog BEFORE PUBLIC
   "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
   "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>"
+  "$<INSTALL_INTERFACE:include>"
   PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
   PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
 
@@ -428,12 +451,6 @@ else (NOT BUILD_SHARED_LIBS)
     "GOOGLE_GLOG_DLL_DECL_FOR_UNITTESTS=${_IMPORT}")
 endif (NOT BUILD_SHARED_LIBS)
 
-if (HAVE_PTHREAD)
-  target_link_libraries (glog PUBLIC ${CMAKE_THREAD_LIBS_INIT})
-else (HAVE_PTHREAD)
-  target_compile_definitions (glog PUBLIC NO_THREADS)
-endif (HAVE_PTHREAD)
-
 if (HAVE_EXECINFO_H)
   set (HAVE_STACKTRACE 1)
 endif (HAVE_EXECINFO_H)
@@ -444,95 +461,97 @@ endif (UNIX OR (APPLE AND HAVE_DLADDR))
 
 # Unit testing
 
-add_executable (logging_unittest
-  src/logging_unittest.cc
-)
-
-target_link_libraries (logging_unittest PRIVATE glog)
+if (BUILD_TESTING)
+  add_executable (logging_unittest
+    src/logging_unittest.cc
+  )
 
-add_executable (stl_logging_unittest
-  src/stl_logging_unittest.cc
-)
+  target_link_libraries (logging_unittest PRIVATE glog)
 
-target_link_libraries (stl_logging_unittest PRIVATE glog)
+  add_executable (stl_logging_unittest
+    src/stl_logging_unittest.cc
+  )
 
-if (HAVE_NO_DEPRECATED)
-  set_property (TARGET stl_logging_unittest APPEND PROPERTY COMPILE_OPTIONS
-    -Wno-deprecated)
-endif (HAVE_NO_DEPRECATED)
+  target_link_libraries (stl_logging_unittest PRIVATE glog)
 
-if (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
-  target_compile_definitions (stl_logging_unittest PRIVATE
-    GLOG_STL_LOGGING_FOR_UNORDERED)
-endif (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
+  if (HAVE_NO_DEPRECATED)
+    set_property (TARGET stl_logging_unittest APPEND PROPERTY COMPILE_OPTIONS
+      -Wno-deprecated)
+  endif (HAVE_NO_DEPRECATED)
 
-if (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
-  target_compile_definitions (stl_logging_unittest PRIVATE
-    GLOG_STL_LOGGING_FOR_TR1_UNORDERED)
-endif (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
+  if (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
+    target_compile_definitions (stl_logging_unittest PRIVATE
+      GLOG_STL_LOGGING_FOR_UNORDERED)
+  endif (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
 
-if (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
-  target_compile_definitions (stl_logging_unittest PRIVATE
-    GLOG_STL_LOGGING_FOR_EXT_HASH)
-endif (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
+  if (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
+    target_compile_definitions (stl_logging_unittest PRIVATE
+      GLOG_STL_LOGGING_FOR_TR1_UNORDERED)
+  endif (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
 
-if (HAVE_EXT_SLIST)
-  target_compile_definitions (stl_logging_unittest PRIVATE
-    GLOG_STL_LOGGING_FOR_EXT_SLIST)
-endif (HAVE_EXT_SLIST)
+  if (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
+    target_compile_definitions (stl_logging_unittest PRIVATE
+      GLOG_STL_LOGGING_FOR_EXT_HASH)
+  endif (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
 
-if (HAVE_SYMBOLIZE)
-  add_executable (symbolize_unittest
-    src/symbolize_unittest.cc
-  )
+  if (HAVE_EXT_SLIST)
+    target_compile_definitions (stl_logging_unittest PRIVATE
+      GLOG_STL_LOGGING_FOR_EXT_SLIST)
+  endif (HAVE_EXT_SLIST)
 
-  target_link_libraries (symbolize_unittest PRIVATE glog)
-endif (HAVE_SYMBOLIZE)
+  if (HAVE_SYMBOLIZE)
+    add_executable (symbolize_unittest
+      src/symbolize_unittest.cc
+    )
 
-add_executable (demangle_unittest
-  src/demangle_unittest.cc
-)
+    target_link_libraries (symbolize_unittest PRIVATE glog)
+  endif (HAVE_SYMBOLIZE)
 
-target_link_libraries (demangle_unittest PRIVATE glog)
-
-if (HAVE_STACKTRACE)
-  add_executable (stacktrace_unittest
-    src/stacktrace_unittest.cc
+  add_executable (demangle_unittest
+    src/demangle_unittest.cc
   )
 
-  target_link_libraries (stacktrace_unittest PRIVATE glog)
-endif (HAVE_STACKTRACE)
+  target_link_libraries (demangle_unittest PRIVATE glog)
 
-add_executable (utilities_unittest
-  src/utilities_unittest.cc
-)
+  if (HAVE_STACKTRACE)
+    add_executable (stacktrace_unittest
+      src/stacktrace_unittest.cc
+    )
 
-target_link_libraries (utilities_unittest PRIVATE glog)
+    target_link_libraries (stacktrace_unittest PRIVATE glog)
+  endif (HAVE_STACKTRACE)
 
-if (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
-  add_executable (signalhandler_unittest
-    src/signalhandler_unittest.cc
+  add_executable (utilities_unittest
+    src/utilities_unittest.cc
   )
 
-  target_link_libraries (signalhandler_unittest PRIVATE glog)
-endif (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
+  target_link_libraries (utilities_unittest PRIVATE glog)
+
+  if (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
+    add_executable (signalhandler_unittest
+      src/signalhandler_unittest.cc
+    )
+
+    target_link_libraries (signalhandler_unittest PRIVATE glog)
+  endif (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
 
-add_test (NAME demangle COMMAND demangle_unittest)
-add_test (NAME logging COMMAND logging_unittest)
+  add_test (NAME demangle COMMAND demangle_unittest)
+  add_test (NAME logging COMMAND logging_unittest)
 
-if (TARGET signalhandler_unittest)
-  add_test (NAME signalhandler COMMAND signalhandler_unittest)
-endif (TARGET signalhandler_unittest)
+  if (TARGET signalhandler_unittest)
+    add_test (NAME signalhandler COMMAND signalhandler_unittest)
+  endif (TARGET signalhandler_unittest)
 
-if (TARGET stacktrace_unittest)
-  add_test (NAME stacktrace COMMAND stacktrace_unittest)
-endif (TARGET stacktrace_unittest)
+  if (TARGET stacktrace_unittest)
+    add_test (NAME stacktrace COMMAND stacktrace_unittest)
+  endif (TARGET stacktrace_unittest)
 
-add_test (NAME stl_logging COMMAND stl_logging_unittest)
+  add_test (NAME stl_logging COMMAND stl_logging_unittest)
 
-if (TARGET symbolize_unittest)
-  add_test (NAME symbolize COMMAND symbolize_unittest)
-endif (TARGET symbolize_unittest)
+  if (TARGET symbolize_unittest)
+    add_test (NAME symbolize COMMAND symbolize_unittest)
+  endif (TARGET symbolize_unittest)
+endif (BUILD_TESTING)
 
 install (TARGETS glog
   EXPORT glog-targets
@@ -541,44 +560,24 @@ install (TARGETS glog
   LIBRARY DESTINATION lib
   ARCHIVE DESTINATION lib)
 
-# Build tree config
-
-set (glog_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
-set (glog_PACKAGE_DEPS)
-
 if (gflags_FOUND)
-  set (glog_PACKAGE_DEPS
-"
-include (CMakeFindDependencyMacro)
-
-find_dependency (gflags ${gflags_VERSION})
-")
+  set (gflags_DEPENDENCY "find_dependency (gflags ${gflags_VERSION})")
 endif (gflags_FOUND)
 
 configure_package_config_file (glog-config.cmake.in
-  ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake INSTALL_DESTINATION
-  lib/cmake/glog PATH_VARS glog_INCLUDE_DIR
+  ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake
+  INSTALL_DESTINATION lib/cmake/glog
   NO_CHECK_REQUIRED_COMPONENTS_MACRO)
 
-# The version file is the same both for build tree and install mode config
 write_basic_package_version_file (glog-config-version.cmake VERSION
   ${GLOG_VERSION} COMPATIBILITY SameMajorVersion)
 
-# Install config
-
-set (glog_INCLUDE_DIR include)
-
-configure_package_config_file (glog-config.cmake.in
-  ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/glog-config.cmake
-  INSTALL_DESTINATION lib/cmake/glog PATH_VARS glog_INCLUDE_DIR
-  NO_CHECK_REQUIRED_COMPONENTS_MACRO)
-
-export (TARGETS glog FILE glog-targets.cmake)
+export (TARGETS glog NAMESPACE glog:: FILE glog-targets.cmake)
 export (PACKAGE glog)
 
 install (FILES
-  ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/glog-config.cmake
+  ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake
   ${CMAKE_CURRENT_BINARY_DIR}/glog-config-version.cmake
   DESTINATION lib/cmake/glog)
 
-install (EXPORT glog-targets DESTINATION lib/cmake/glog)
+install (EXPORT glog-targets NAMESPACE glog:: DESTINATION lib/cmake/glog)