CMake: Add option to build against system-wide Glog
authorSergey Sharybin <sergey.vfx@gmail.com>
Fri, 21 Apr 2017 12:26:25 +0000 (14:26 +0200)
committerSergey Sharybin <sergey.vfx@gmail.com>
Fri, 21 Apr 2017 12:36:34 +0000 (14:36 +0200)
Similar to previous commit for Gflags.

CMakeLists.txt
build_files/cmake/Modules/FindGlog.cmake [new file with mode: 0644]
build_files/cmake/Modules/GTestTesting.cmake
build_files/cmake/macros.cmake
intern/cycles/CMakeLists.txt
intern/cycles/app/CMakeLists.txt
intern/cycles/cmake/external_libs.cmake
source/blenderplayer/CMakeLists.txt
tests/gtests/testing/CMakeLists.txt

index 748ed649800228c06649b966fdb875c4d05ca9ea..c0ea2ef679e07bd9115933edf00f116dc4f2ee8a 100644 (file)
@@ -365,7 +365,9 @@ mark_as_advanced(WITH_LIBMV_SCHUR_SPECIALIZATIONS)
 
 # Logging/unbit test libraries.
 option(WITH_SYSTEM_GFLAGS   "Use system-wide Gflags instead of a bundled one" OFF)
+option(WITH_SYSTEM_GFLOG    "Use system-wide Glog instead of a bundled one" OFF)
 mark_as_advanced(WITH_SYSTEM_GFLAGS)
+mark_as_advanced(WITH_SYSTEM_GLOG)
 
 # Freestyle
 option(WITH_FREESTYLE     "Enable Freestyle (advanced edges rendering)" ON)
@@ -1339,9 +1341,24 @@ if(WITH_LIBMV OR WITH_GTESTS OR (WITH_CYCLES AND WITH_CYCLES_LOGGING))
                set(GFLAGS_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}/extern/gflags/src")
        endif()
 
-       set(GLOG_DEFINES
-               -DGOOGLE_GLOG_DLL_DECL=
-       )
+       if(WITH_SYSTEM_GLOG)
+               find_package(Glog)
+               if(NOT GLOG_FOUND)
+                       message(FATAL_ERROR "System wide Glog is requested but was not found")
+               endif()
+               # FindGlog does not define this, and we are not even sure what to use here.
+               set(GLOG_DEFINES)
+       else()
+               set(GLOG_DEFINES
+                       -DGOOGLE_GLOG_DLL_DECL=
+               )
+               set(GLOG_LIBRARIES extern_glog)
+               if(WIN32)
+                       set(GLOG_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/extern/glog/src/windows)
+               else()
+                       set(GLOG_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/extern/glog/src)
+               endif()
+       endif()
 endif()
 
 #-----------------------------------------------------------------------------
diff --git a/build_files/cmake/Modules/FindGlog.cmake b/build_files/cmake/Modules/FindGlog.cmake
new file mode 100644 (file)
index 0000000..66c2aab
--- /dev/null
@@ -0,0 +1,226 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice,
+#   this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright notice,
+#   this list of conditions and the following disclaimer in the documentation
+#   and/or other materials provided with the distribution.
+# * Neither the name of Google Inc. nor the names of its contributors may be
+#   used to endorse or promote products derived from this software without
+#   specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# Author: alexs.mac@gmail.com (Alex Stewart)
+#
+
+# FindGlog.cmake - Find Google glog logging library.
+#
+# This module defines the following variables:
+#
+# GLOG_FOUND: TRUE iff glog is found.
+# GLOG_INCLUDE_DIRS: Include directories for glog.
+# GLOG_LIBRARIES: Libraries required to link glog.
+#
+# The following variables control the behaviour of this module:
+#
+# GLOG_INCLUDE_DIR_HINTS: List of additional directories in which to
+#                         search for glog includes, e.g: /timbuktu/include.
+# GLOG_LIBRARY_DIR_HINTS: List of additional directories in which to
+#                         search for glog libraries, e.g: /timbuktu/lib.
+# GFLOG_ROOT_DIR,         The base directory to search for Glog.
+#                         This can also be an environment variable.
+#
+# The following variables are also defined by this module, but in line with
+# CMake recommended FindPackage() module style should NOT be referenced directly
+# by callers (use the plural variables detailed above instead).  These variables
+# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
+# are NOT re-called (i.e. search for library is not repeated) if these variables
+# are set with valid values _in the CMake cache_. This means that if these
+# variables are set directly in the cache, either by the user in the CMake GUI,
+# or by the user passing -DVAR=VALUE directives to CMake when called (which
+# explicitly defines a cache variable), then they will be used verbatim,
+# bypassing the HINTS variables and other hard-coded search locations.
+#
+# GLOG_INCLUDE_DIR: Include directory for glog, not including the
+#                   include directory of any dependencies.
+# GLOG_LIBRARY: glog library, not including the libraries of any
+#               dependencies.
+
+# If GLOG_ROOT_DIR was defined in the environment, use it.
+if(NOT GLOG_ROOT_DIR AND NOT $ENV{GLOG_ROOT_DIR} STREQUAL "")
+  set(GLOG_ROOT_DIR $ENV{GLOG_ROOT_DIR})
+endif()
+
+if(DEFINED GLOG_ROOT_DIR)
+  set(GLOG_ROOT_DIR_INCLUDE "${GLOG_ROOT_DIR}/include")
+  set(GLOG_ROOT_DIR_LIB "${GLOG_ROOT_DIR}/lib")
+endif()
+
+# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
+# FindGlog was invoked.
+macro(GLOG_RESET_FIND_LIBRARY_PREFIX)
+  if(MSVC)
+    set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
+  endif()
+endmacro()
+
+# Called if we failed to find glog or any of it's required dependencies,
+# unsets all public (designed to be used externally) variables and reports
+# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
+macro(GLOG_REPORT_NOT_FOUND REASON_MSG)
+  unset(GLOG_FOUND)
+  unset(GLOG_INCLUDE_DIRS)
+  unset(GLOG_LIBRARIES)
+  # Make results of search visible in the CMake GUI if glog has not
+  # been found so that user does not have to toggle to advanced view.
+  mark_as_advanced(CLEAR GLOG_INCLUDE_DIR
+                         GLOG_LIBRARY)
+
+  glog_reset_find_library_prefix()
+
+  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
+  # use the camelcase library name, not uppercase.
+  if(Glog_FIND_QUIETLY)
+    message(STATUS "Failed to find glog - " ${REASON_MSG} ${ARGN})
+  elseif(Glog_FIND_REQUIRED)
+    message(FATAL_ERROR "Failed to find glog - " ${REASON_MSG} ${ARGN})
+  else()
+    # Neither QUIETLY nor REQUIRED, use no priority which emits a message
+    # but continues configuration and allows generation.
+    message("-- Failed to find glog - " ${REASON_MSG} ${ARGN})
+  endif()
+  return()
+endmacro()
+
+# Handle possible presence of lib prefix for libraries on MSVC, see
+# also GLOG_RESET_FIND_LIBRARY_PREFIX().
+if(MSVC)
+  # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
+  # s/t we can set it back before returning.
+  set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
+  # The empty string in this list is important, it represents the case when
+  # the libraries have no prefix (shared libraries / DLLs).
+  set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
+endif()
+
+# Search user-installed locations first, so that we prefer user installs
+# to system installs where both exist.
+list(APPEND GLOG_CHECK_INCLUDE_DIRS
+  ${GLOG_ROOT_DIR_INCLUDE}
+  /usr/local/include
+  /usr/local/homebrew/include # Mac OS X
+  /opt/local/var/macports/software # Mac OS X.
+  /opt/local/include
+  /usr/include
+  /sw/include # Fink
+  /opt/csw/include # Blastwave
+  /opt/lib/glog/include)
+# Windows (for C:/Program Files prefix).
+list(APPEND GLOG_CHECK_PATH_SUFFIXES
+  glog/include
+  glog/Include
+  Glog/include
+  Glog/Include)
+
+list(APPEND GLOG_CHECK_LIBRARY_DIRS
+  ${GLOG_ROOT_DIR_LIB}
+  /usr/local/lib
+  /usr/local/homebrew/lib # Mac OS X.
+  /opt/local/lib
+  /usr/lib
+  /sw/lib # Fink
+  /opt/csw/lib # Blastwave
+  /opt/lib/gflags/lib)
+# Windows (for C:/Program Files prefix).
+list(APPEND GLOG_CHECK_LIBRARY_SUFFIXES
+  glog/lib
+  glog/Lib
+  Glog/lib
+  Glog/Lib)
+
+# Search supplied hint directories first if supplied.
+find_path(GLOG_INCLUDE_DIR
+  NAMES glog/logging.h
+  PATHS ${GLOG_INCLUDE_DIR_HINTS}
+  ${GLOG_CHECK_INCLUDE_DIRS}
+  PATH_SUFFIXES ${GLOG_CHECK_PATH_SUFFIXES})
+if(NOT GLOG_INCLUDE_DIR OR
+   NOT EXISTS ${GLOG_INCLUDE_DIR})
+  glog_report_not_found(
+    "Could not find glog include directory, set GLOG_INCLUDE_DIR "
+    "to directory containing glog/logging.h")
+endif()
+
+find_library(GLOG_LIBRARY NAMES glog
+  PATHS ${GLOG_LIBRARY_DIR_HINTS}
+  ${GLOG_CHECK_LIBRARY_DIRS}
+  PATH_SUFFIXES ${GLOG_CHECK_LIBRARY_SUFFIXES})
+if(NOT GLOG_LIBRARY OR
+   NOT EXISTS ${GLOG_LIBRARY})
+  glog_report_not_found(
+    "Could not find glog library, set GLOG_LIBRARY "
+    "to full path to libglog.")
+endif()
+
+# Mark internally as found, then verify. GLOG_REPORT_NOT_FOUND() unsets
+# if called.
+set(GLOG_FOUND TRUE)
+
+# Glog does not seem to provide any record of the version in its
+# source tree, thus cannot extract version.
+
+# Catch case when caller has set GLOG_INCLUDE_DIR in the cache / GUI and
+# thus FIND_[PATH/LIBRARY] are not called, but specified locations are
+# invalid, otherwise we would report the library as found.
+if(GLOG_INCLUDE_DIR AND
+   NOT EXISTS ${GLOG_INCLUDE_DIR}/glog/logging.h)
+  glog_report_not_found(
+    "Caller defined GLOG_INCLUDE_DIR:"
+    " ${GLOG_INCLUDE_DIR} does not contain glog/logging.h header.")
+endif()
+# TODO: This regex for glog library is pretty primitive, we use lowercase
+#       for comparison to handle Windows using CamelCase library names, could
+#       this check be better?
+string(TOLOWER "${GLOG_LIBRARY}" LOWERCASE_GLOG_LIBRARY)
+if(GLOG_LIBRARY AND
+   NOT "${LOWERCASE_GLOG_LIBRARY}" MATCHES ".*glog[^/]*")
+  glog_report_not_found(
+    "Caller defined GLOG_LIBRARY: "
+    "${GLOG_LIBRARY} does not match glog.")
+endif()
+
+# Set standard CMake FindPackage variables if found.
+if(GLOG_FOUND)
+  set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
+  set(GLOG_LIBRARIES ${GLOG_LIBRARY})
+endif()
+
+glog_reset_find_library_prefix()
+
+# Handle REQUIRED / QUIET optional arguments.
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(GLOG DEFAULT_MSG
+  GLOG_INCLUDE_DIRS GLOG_LIBRARIES)
+
+# Only mark internal variables as advanced if we found glog, otherwise
+# leave them visible in the standard GUI for the user to set manually.
+if(GLOG_FOUND)
+  mark_as_advanced(FORCE GLOG_INCLUDE_DIR
+                         GLOG_LIBRARY)
+endif()
index 9fd082ee40203989fd7891da1e0886ea35b38585..ba1334d750e63958db84e1153f00cd4f7b94b292 100644 (file)
@@ -20,7 +20,7 @@ macro(BLENDER_SRC_GTEST_EX NAME SRC EXTRA_LIBS DO_ADD_TEST)
                set(TEST_INC
                        ${_current_include_directories}
                        ${CMAKE_SOURCE_DIR}/tests/gtests
-                       ${CMAKE_SOURCE_DIR}/extern/glog/src
+                       ${GLOG_INCLUDE_DIRS}
                        ${GFLAGS_INCLUDE_DIRS}
                        ${CMAKE_SOURCE_DIR}/extern/gtest/include
                        ${CMAKE_SOURCE_DIR}/extern/gmock/include
@@ -37,7 +37,7 @@ macro(BLENDER_SRC_GTEST_EX NAME SRC EXTRA_LIBS DO_ADD_TEST)
                                      extern_gmock
                                      # needed for glog
                                      ${PTHREADS_LIBRARIES}
-                                     extern_glog
+                                     ${GLOG_LIBRARIES}
                                      ${GFLAGS_LIBRARIES})
                if(WITH_OPENMP_STATIC)
                        target_link_libraries(${NAME}_test ${OpenMP_LIBRARIES})
index 0221ac55e4207c074a539c9755a19f030ca9a965..f62e55941a7a95c38b5a47af16804951af274112 100644 (file)
@@ -491,6 +491,9 @@ function(setup_liblinks
                        target_link_libraries(${target} ${NDOF_LIBRARIES})
                endif()
        endif()
+       if(WITH_SYSTEM_GLOG)
+               target_link_libraries(${target} ${GLOG_LIBRARIES})
+       endif()
        if(WITH_SYSTEM_GFLAGS)
                target_link_libraries(${target} ${GFLAGS_LIBRARIES})
        endif()
@@ -661,12 +664,15 @@ function(SETUP_BLENDER_SORTED_LIBS)
                extern_rangetree
                extern_wcwidth
                bf_intern_libmv
-               extern_glog
                extern_sdlew
 
                bf_intern_glew_mx
        )
 
+       if(NOT WITH_SYSTEM_GLOG)
+               list(APPEND BLENDER_SORTED_LIBS extern_glog)
+       endif()
+
        if(NOT WITH_SYSTEM_GFLAGS)
                list(APPEND BLENDER_SORTED_LIBS extern_gflags)
        endif()
index 806a8660e8cc8f904e33f641f9788c087f3b5772..c53a9f91cc03795089ecdc1df2aca81c23efcea6 100644 (file)
@@ -191,7 +191,7 @@ endif()
 # Logging capabilities using GLog library.
 if(WITH_CYCLES_LOGGING)
        add_definitions(-DWITH_CYCLES_LOGGING)
-       add_definitions(-DGOOGLE_GLOG_DLL_DECL=)
+       add_definitions(${GLOG_DEFINES})
        add_definitions(-DCYCLES_GFLAGS_NAMESPACE=${GFLAGS_NAMESPACE})
        include_directories(
                SYSTEM
index 231ef58f7aea52aa0e03088c1d6f0ea75bcee52e..08a3931ef46b0b22da7498f695489c338b048f0a 100644 (file)
@@ -35,18 +35,15 @@ if(WITH_CYCLES_OSL)
        list(APPEND LIBRARIES cycles_kernel_osl)
 endif()
 
-if(CYCLES_STANDALONE_REPOSITORY)
-       if(WITH_CYCLES_LOGGING)
-               list(APPEND LIBRARIES
-                       ${GLOG_LIBRARIES}
-                       ${GFLAGS_LIBRARIES}
-               )
-       endif()
-else()
+if(NOT CYCLES_STANDALONE_REPOSITORY)
        list(APPEND LIBRARIES bf_intern_glew_mx bf_intern_guardedalloc)
-       if(WITH_CYCLES_LOGGING)
-               list(APPEND LIBRARIES extern_glog ${GFLAGS_LIBRARIES})
-       endif()
+endif()
+
+if(WITH_CYCLES_LOGGING)
+       list(APPEND LIBRARIES
+               ${GLOG_LIBRARIES}
+               ${GFLAGS_LIBRARIES}
+       )
 endif()
 
 if(WITH_CYCLES_STANDALONE AND WITH_CYCLES_STANDALONE_GUI)
index 68e7f0a6eac56149ba88454aa2abf68048d7d8c0..df88b91f5ac0e49225fcc9f474f91f82bdb3bc8a 100644 (file)
@@ -135,10 +135,5 @@ if(CYCLES_STANDALONE_REPOSITORY)
 
        unset(_lib_DIR)
 else()
-       if(WIN32)
-               set(GLOG_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/extern/glog/src/windows)
-       else()
-               set(GLOG_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/extern/glog/src)
-       endif()
        set(LLVM_LIBRARIES ${LLVM_LIBRARY})
 endif()
index e0afd84db820297a7e013d322a9a7c4374a13951..bee76a12ee5247c22e8cb671d6e1805fb4b74e58 100644 (file)
@@ -176,10 +176,13 @@ endif()
                extern_rangetree
                extern_wcwidth
                bf_intern_libmv
-               extern_glog
                extern_sdlew
        )
 
+       if(NOT WITH_SYSTEM_GLOG)
+               list(APPEND BLENDER_SORTED_LIBS extern_glog)
+       endif()
+
        if(NOT WITH_SYSTEM_GFLAGS)
                list(APPEND BLENDER_SORTED_LIBS extern_gflags)
        endif()
index defa51df6643088cbc9e6e3e6f310734f2d96b00..95ac59e6dce2f8bb28c4fcfa80cfd2430707f2d1 100644 (file)
 set(INC
        .
        ..
+       ${GLOG_INCLUDE_DIRS}
        ${GFLAGS_INCLUDE_DIRS}
        ../../../extern/gtest/include
 )
 
-if(WIN32)
-       list(APPEND INC
-               ../../../extern/glog/src/windows
-       )
-else()
-       list(APPEND INC
-               ../../../extern/glog/src
-       )
-endif()
-
 set(INC_SYS
 )