Skip to content

Commit

Permalink
[VL] Add find and build glog/gflags to cmake (#3638)
Browse files Browse the repository at this point in the history
  • Loading branch information
marin-ma authored Nov 8, 2023
1 parent 427971d commit 2b9891b
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 36 deletions.
41 changes: 41 additions & 0 deletions cpp/CMake/BuildGflags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
include_guard(GLOBAL)

set(GLUTEN_GFLAGS_BUILD_SHA256_CHECKSUM
34af2f15cf7367513b352bdcd2493ab14ce43692d2dcd9dfc499492966c64dcf)
string(CONCAT GLUTEN_GFLAGS_SOURCE_URL
"https://github.com/gflags/gflags/archive/refs/tags/"
"v${GLUTEN_GFLAGS_VERSION}.tar.gz")

message(STATUS "Building gflags from source")
FetchContent_Declare(
gflags
URL ${GLUTEN_GFLAGS_SOURCE_URL}
URL_HASH SHA256=${GLUTEN_GFLAGS_BUILD_SHA256_CHECKSUM})

set(GFLAGS_BUILD_STATIC_LIBS ON)
set(GFLAGS_BUILD_SHARED_LIBS ON)
set(GFLAGS_BUILD_gflags_LIB ON)
# glog relies on the old `google` namespace
set(GFLAGS_NAMESPACE "google;gflags")

FetchContent_MakeAvailable(gflags)

# the flag has to be added to each target we build so adjust to settings choosen
# above
target_compile_options(gflags_static PRIVATE -Wno-cast-function-type)
51 changes: 51 additions & 0 deletions cpp/CMake/BuildGlog.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (c) Facebook, Inc. and its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
include_guard(GLOBAL)

set(GLUTEN_GLOG_BUILD_SHA256_CHECKSUM
8a83bf982f37bb70825df71a9709fa90ea9f4447fb3c099e1d720a439d88bad6)
set(GLUTEN_GLOG_SOURCE_URL
"https://github.com/google/glog/archive/refs/tags/v${GLUTEN_GLOG_VERSION}.tar.gz"
)

message(STATUS "Building glog from source")
FetchContent_Declare(
glog
URL ${GLUTEN_GLOG_SOURCE_URL}
URL_HASH SHA256=${GLUTEN_GLOG_BUILD_SHA256_CHECKSUM}
PATCH_COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/glog/glog-no-export.patch
&& git apply ${CMAKE_CURRENT_LIST_DIR}/glog/glog-config.patch)

set(BUILD_SHARED_LIBS OFF)
set(WITH_UNWIND OFF)
set(BUILD_TESTING OFF)
FetchContent_MakeAvailable(glog)
unset(BUILD_TESTING)
unset(BUILD_SHARED_LIBS)

list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/glog)
set(GLOG_INCLUDE_DIR ${glog_BINARY_DIR})
string(TOUPPER "${CMAKE_BUILD_TYPE}" UPPERCASE_BUILD_TYPE)
if(UPPERCASE_BUILD_TYPE MATCHES "DEBUG")
set(GLOG_LIBRARY "${glog_BINARY_DIR}/libglogd.a")
else()
set(GLOG_LIBRARY "${glog_BINARY_DIR}/libglog.a")
endif()

# These headers are missing from the include dir but adding the src dir causes
# issues with folly so we just copy it to the include dir
file(COPY ${glog_SOURCE_DIR}/src/glog/platform.h
DESTINATION ${glog_BINARY_DIR}/glog)
file(COPY ${glog_SOURCE_DIR}/src/glog/log_severity.h
DESTINATION ${glog_BINARY_DIR}/glog)
98 changes: 71 additions & 27 deletions cpp/CMake/Findglog.cmake
Original file line number Diff line number Diff line change
@@ -1,37 +1,81 @@
# Copyright (c) Facebook, Inc. and its affiliates.
# - Try to find Glog
# Once done, this will define
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# GLOG_FOUND - system has Glog
# GLOG_INCLUDE_DIRS - the Glog include directories
# GLOG_LIBRARIES - link these to use Glog
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

set(GLUTEN_GLOG_MINIMUM_VERSION 0.4.0)
set(GLUTEN_GLOG_VERSION 0.6.0)

if(NOT BUILD_GLOG)
include(FindPackageHandleStandardArgs)
include(SelectLibraryConfigurations)

find_library(GLOG_LIBRARY_RELEASE glog
PATHS ${GLOG_LIBRARYDIR})
find_library(GLOG_LIBRARY_DEBUG glogd
PATHS ${GLOG_LIBRARYDIR})

find_path(GLOG_INCLUDE_DIR glog/logging.h
PATHS ${GLOG_INCLUDEDIR})

include(FindPackageHandleStandardArgs)
include(SelectLibraryConfigurations)
select_library_configurations(GLOG)

find_library(GLOG_LIBRARY_RELEASE glog
PATHS ${GLOG_LIBRARYDIR})
find_library(GLOG_LIBRARY_DEBUG glogd
PATHS ${GLOG_LIBRARYDIR})
find_package_handle_standard_args(glog DEFAULT_MSG
GLOG_LIBRARY
GLOG_INCLUDE_DIR)

find_path(GLOG_INCLUDE_DIR glog/logging.h
PATHS ${GLOG_INCLUDEDIR})
mark_as_advanced(
GLOG_LIBRARY
GLOG_INCLUDE_DIR)
endif()

select_library_configurations(GLOG)
if(NOT glog_FOUND)
include(BuildGlog)
endif()

find_package_handle_standard_args(glog DEFAULT_MSG
GLOG_LIBRARY
GLOG_INCLUDE_DIR)
get_filename_component(libglog_ext ${GLOG_LIBRARY} EXT)
if(libglog_ext STREQUAL ".a")
set(libglog_type STATIC)
set(libgflags_component static)
else()
set(libglog_type SHARED)
set(libgflags_component shared)
endif()

mark_as_advanced(
GLOG_LIBRARY
GLOG_INCLUDE_DIR)
# glog::glog may already exist. Use google::glog to avoid conflicts.
add_library(google::glog ${libglog_type} IMPORTED)
set_target_properties(google::glog PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${GLOG_INCLUDE_DIR}")
set_target_properties(google::glog PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C" IMPORTED_LOCATION "${GLOG_LIBRARY}")

set(GLOG_LIBRARIES ${GLOG_LIBRARY})
set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
set(GLUTEN_GFLAGS_VERSION 2.2.2)
find_package(gflags ${GLUTEN_GFLAGS_VERSION} CONFIG COMPONENTS ${libgflags_component})

if(NOT gflags_FOUND AND glog_FOUND)
message(FATAL_ERROR "Glog found but Gflags not found. Set BUILD_GLOG=ON and reload cmake.")
endif()

if (NOT TARGET glog::glog)
add_library(glog::glog UNKNOWN IMPORTED)
set_target_properties(glog::glog PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${GLOG_INCLUDE_DIRS}")
set_target_properties(glog::glog PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C" IMPORTED_LOCATION "${GLOG_LIBRARIES}")
if(gflags_FOUND)
if(NOT TARGET gflags::gflags_${libgflags_component} AND NOT TARGET gflags_${libgflags_component})
message(FATAL_ERROR "Found Gflags but missing component gflags_${libgflags_component}")
endif()
if(TARGET gflags::gflags_${libgflags_component})
set_target_properties(google::glog PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES gflags::gflags_${libgflags_component})
else()
set_target_properties(google::glog PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES gflags_${libgflags_component})
endif()
else()
include(BuildGflags)
set_target_properties(google::glog PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES gflags_static)
endif()
20 changes: 20 additions & 0 deletions cpp/CMake/glog/glog-config.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) Facebook, Inc. and its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
--- a/glog-config.cmake.in
+++ b/glog-config.cmake.in
@@ -10,4 +10,3 @@ include (${CMAKE_CURRENT_LIST_DIR}/glog-modules.cmake)
@gflags_DEPENDENCY@
@Unwind_DEPENDENCY@

-include (${CMAKE_CURRENT_LIST_DIR}/glog-targets.cmake)
32 changes: 32 additions & 0 deletions cpp/CMake/glog/glog-no-export.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) Facebook, Inc. and its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1025,7 +1025,7 @@ write_basic_package_version_file (
${CMAKE_CURRENT_BINARY_DIR}/glog-config-version.cmake
COMPATIBILITY SameMajorVersion)

-export (TARGETS glog NAMESPACE glog:: FILE glog-targets.cmake)
+# export (TARGETS glog NAMESPACE glog:: FILE glog-targets.cmake)
export (PACKAGE glog)

get_filename_component (_PREFIX "${CMAKE_INSTALL_PREFIX}" ABSOLUTE)
@@ -1076,5 +1076,5 @@ install (DIRECTORY ${_glog_BINARY_CMake_DATADIR}
FILES_MATCHING PATTERN "*.cmake"
)

-install (EXPORT glog-targets NAMESPACE glog:: DESTINATION
- ${_glog_CMake_INSTALLDIR})
+# install (EXPORT glog-targets NAMESPACE glog:: DESTINATION
+# ${_glog_CMake_INSTALLDIR})
4 changes: 3 additions & 1 deletion cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ option(BUILD_EXAMPLES "Build Examples" OFF)
option(BUILD_BENCHMARKS "Build Benchmarks" OFF)
option(BUILD_PROTOBUF "Build Protobuf from Source" OFF)
option(BUILD_JEMALLOC "Build Jemalloc from Source" OFF)
option(BUILD_GLOG "Build Glog from Source" OFF)
option(USE_AVX512 "Build with AVX-512 optimizations" OFF)
option(ENABLE_HBM "Enable HBM allocator" OFF)
option(ENABLE_QAT "Enable QAT for de/compression" OFF)
Expand Down Expand Up @@ -122,6 +123,8 @@ find_package(Threads REQUIRED)

find_package(JNI REQUIRED)

find_package(glog REQUIRED)

if(BUILD_TESTS)
set(GLUTEN_GTEST_MIN_VERSION "1.13.0")
find_package(GTest ${GLUTEN_GTEST_MIN_VERSION} CONFIG)
Expand All @@ -130,7 +133,6 @@ if(BUILD_TESTS)
endif()
include(GoogleTest)
enable_testing()
find_package(glog REQUIRED)
endif()

function(ADD_TEST_CASE TEST_NAME)
Expand Down
6 changes: 0 additions & 6 deletions cpp/velox/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,6 @@ set_target_properties(velox PROPERTIES
## It is also applicable to other dependencies.
find_package(Folly REQUIRED CONFIG)

if(ENABLE_GLUTEN_VCPKG)
find_package(gflags REQUIRED COMPONENTS static CONFIG)
else()
find_package(gflags REQUIRED COMPONENTS shared CONFIG)
endif()

target_include_directories(velox PUBLIC
${GTEST_INCLUDE_DIRS}
${PROTOBUF_INCLUDE})
Expand Down
2 changes: 1 addition & 1 deletion cpp/velox/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ find_arrow_lib(${PARQUET_LIB_NAME})
set(VELOX_BENCHMARK_COMMON_SRCS common/FileReaderIterator.cc common/BenchmarkUtils.cc)
add_library(velox_benchmark_common STATIC ${VELOX_BENCHMARK_COMMON_SRCS})
target_include_directories(velox_benchmark_common PUBLIC ${CMAKE_SOURCE_DIR}/velox ${CMAKE_SOURCE_DIR}/core)
target_link_libraries(velox_benchmark_common PUBLIC Arrow::parquet velox benchmark::benchmark gflags simdjson)
target_link_libraries(velox_benchmark_common PUBLIC Arrow::parquet velox benchmark::benchmark google::glog simdjson)

function(add_velox_benchmark BM_EXEC BM_FILE)
add_executable(${BM_EXEC} ${BM_FILE})
Expand Down
2 changes: 1 addition & 1 deletion cpp/velox/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function(add_velox_test TEST_EXEC)
endif()
add_executable(${TEST_EXEC} ${SOURCES})
target_include_directories(${TEST_EXEC} PRIVATE ${CMAKE_SOURCE_DIR}/velox ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(${TEST_EXEC} gflags velox GTest::gtest GTest::gtest_main glog::glog benchmark::benchmark simdjson)
target_link_libraries(${TEST_EXEC} velox GTest::gtest GTest::gtest_main google::glog benchmark::benchmark simdjson)
gtest_discover_tests(${TEST_EXEC} DISCOVERY_MODE PRE_TEST)
endfunction()

Expand Down

0 comments on commit 2b9891b

Please sign in to comment.