Skip to content

Commit

Permalink
rework public header structure
Browse files Browse the repository at this point in the history
Promote generated headers to regular headers. This enables direct
editing of corresponding files and running automatic source code
formatting and static analysis tools, such as clang-format and
clang-tidy on the whole project which previously was not
straightforward.
  • Loading branch information
sergiud committed Dec 30, 2023
1 parent 80d7fa3 commit 3b278fe
Show file tree
Hide file tree
Showing 38 changed files with 782 additions and 924 deletions.
96 changes: 11 additions & 85 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ endif (Unwind_FOUND)

check_include_file_cxx (dlfcn.h HAVE_DLFCN_H)
check_include_file_cxx (glob.h HAVE_GLOB_H)
check_include_file_cxx (memory.h HAVE_MEMORY_H)
check_include_file_cxx (pwd.h HAVE_PWD_H)
check_include_file_cxx (strings.h HAVE_STRINGS_H)
check_include_file_cxx (sys/stat.h HAVE_SYS_STAT_H)
check_include_file_cxx (sys/syscall.h HAVE_SYS_SYSCALL_H)
check_include_file_cxx (sys/time.h HAVE_SYS_TIME_H)
check_include_file_cxx (sys/types.h HAVE_SYS_TYPES_H)
Expand Down Expand Up @@ -190,28 +187,6 @@ set (CMAKE_REQUIRED_LIBRARIES dbghelp)
check_cxx_symbol_exists (UnDecorateSymbolName "windows.h;dbghelp.h" HAVE_DBGHELP)
cmake_pop_check_state ()

check_cxx_source_compiles ("
#include <cstdlib>
static void foo(void) __attribute__ ((unused));
int main(void) { return 0; }
" HAVE___ATTRIBUTE__)

check_cxx_source_compiles ("
#include <cstdlib>
static void foo(void) __attribute__ ((visibility(\"default\")));
int main(void) { return 0; }
" HAVE___ATTRIBUTE__VISIBILITY_DEFAULT)

check_cxx_source_compiles ("
#include <cstdlib>
static void foo(void) __attribute__ ((visibility(\"hidden\")));
int main(void) { return 0; }
" HAVE___ATTRIBUTE__VISIBILITY_HIDDEN)

check_cxx_source_compiles ("
int main(void) { if (__builtin_expect(0, 0)) return 1; return 0; }
" HAVE___BUILTIN_EXPECT)

check_cxx_source_compiles ("
int main(void)
{
Expand Down Expand Up @@ -285,58 +260,6 @@ if (HAVE_UCONTEXT_H AND NOT DEFINED PC_FROM_UCONTEXT)
cmake_pop_check_state ()
endif (HAVE_UCONTEXT_H AND NOT DEFINED PC_FROM_UCONTEXT)

set (GOOGLE_NAMESPACE google)
set (_START_GOOGLE_NAMESPACE_ "namespace ${GOOGLE_NAMESPACE} {")
set (_END_GOOGLE_NAMESPACE_ "}")
set (ac_cv_have_glog_export 1)

if (HAVE_LIB_GFLAGS)
set (ac_cv_have_libgflags 1)
else (HAVE_LIB_GFLAGS)
set (ac_cv_have_libgflags 0)
endif (HAVE_LIB_GFLAGS)

if (HAVE_SYS_TYPES_H)
set (ac_cv_have_systypes_h 1)
else (HAVE_SYS_TYPES_H)
set (ac_cv_have_systypes_h 0)
endif (HAVE_SYS_TYPES_H)

if (HAVE_SSIZE_T)
set (ac_cv_have_ssize_t 1)
else (HAVE_SSIZE_T)
set (ac_cv_have_ssize_t 0)
endif (HAVE_SSIZE_T)

if (HAVE_MODE_T)
set (ac_cv_have_mode_t 1)
else (HAVE_MODE_T)
set (ac_cv_have_mode_t 0)
endif (HAVE_MODE_T)

if (HAVE_UNISTD_H)
set (ac_cv_have_unistd_h 1)
else (HAVE_UNISTD_H)
set (ac_cv_have_unistd_h 0)
endif (HAVE_UNISTD_H)

set (ac_google_namespace ${GOOGLE_NAMESPACE})
set (ac_google_end_namespace ${_END_GOOGLE_NAMESPACE_})
set (ac_google_start_namespace ${_START_GOOGLE_NAMESPACE_})

if (HAVE___ATTRIBUTE__)
set (ac_cv___attribute___noinline "__attribute__((noinline))")
set (ac_cv___attribute___printf_4_5 "__attribute__((__format__(__printf__, 4, 5)))")
elseif (HAVE___DECLSPEC)
#set (ac_cv___attribute___noinline "__declspec(noinline)")
endif (HAVE___ATTRIBUTE__)

if (HAVE___BUILTIN_EXPECT)
set (ac_cv_have___builtin_expect 1)
else (HAVE___BUILTIN_EXPECT)
set (ac_cv_have___builtin_expect 0)
endif (HAVE___BUILTIN_EXPECT)

if (HAVE_EXECINFO_BACKTRACE AND HAVE_EXECINFO_BACKTRACE_SYMBOLS)
set (HAVE_STACKTRACE 1)
endif (HAVE_EXECINFO_BACKTRACE AND HAVE_EXECINFO_BACKTRACE_SYMBOLS)
Expand Down Expand Up @@ -414,10 +337,6 @@ else (CYGWIN)
endif (CYGWIN)

configure_file (src/config.h.cmake.in config.h)
configure_file (src/glog/logging.h.in glog/logging.h @ONLY)
configure_file (src/glog/raw_logging.h.in glog/raw_logging.h @ONLY)
configure_file (src/glog/stl_logging.h.in glog/stl_logging.h @ONLY)
configure_file (src/glog/vlog_is_on.h.in glog/vlog_is_on.h @ONLY)

set (_glog_CMake_BINDIR ${CMAKE_INSTALL_BINDIR})
set (_glog_CMake_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
Expand Down Expand Up @@ -459,12 +378,12 @@ endif (_glog_CMake_MODULES)

set (GLOG_PUBLIC_H
${glog_BINARY_DIR}/glog/export.h
${glog_BINARY_DIR}/glog/logging.h
${glog_BINARY_DIR}/glog/raw_logging.h
${glog_BINARY_DIR}/glog/stl_logging.h
${glog_BINARY_DIR}/glog/vlog_is_on.h
src/glog/log_severity.h
src/glog/logging.h
src/glog/platform.h
src/glog/raw_logging.h
src/glog/stl_logging.h
src/glog/vlog_is_on.h
)

set (GLOG_SRCS
Expand Down Expand Up @@ -517,6 +436,13 @@ add_library (glog::glog ALIAS glog)

set (glog_libraries_options_for_static_linking)

# CMake always uses the generated export header
target_compile_definitions (glog PUBLIC GLOG_USE_GLOG_EXPORT)

if (HAVE_LIB_GFLAGS)
target_compile_definitions (glog PUBLIC GLOG_USE_GFLAGS)
endif (HAVE_LIB_GFLAGS)

if (Unwind_FOUND)
target_link_libraries (glog PRIVATE unwind::unwind)
set (glog_libraries_options_for_static_linking "${glog_libraries_options_for_static_linking} -lunwind")
Expand Down
79 changes: 13 additions & 66 deletions bazel/glog.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ expand_template = rule(
},
)

def dict_union(x, y):
z = {}
z.update(x)
z.update(y)
return z

def glog_library(namespace = "google", with_gflags = 1, **kwargs):
def glog_library(with_gflags = 1, **kwargs):
if native.repository_name() != "@":
repo_name = native.repository_name()[1:] # Strip the first leading @
gendir = "$(GENDIR)/external/" + repo_name
Expand All @@ -55,11 +49,9 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
common_copts = [
"-std=c++14",
"-DGLOG_BAZEL_BUILD",
# Inject a C++ namespace.
"-DGOOGLE_NAMESPACE='%s'" % namespace,
"-DHAVE_STRING_H",
"-I%s/glog_internal" % gendir,
] + (["-DHAVE_LIB_GFLAGS"] if with_gflags else [])
] + (["-DGLOG_USE_GFLAGS"] if with_gflags else [])

wasm_copts = [
# Disable warnings that exists in glog.
Expand All @@ -83,6 +75,9 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):

linux_or_darwin_copts = wasm_copts + [
"-DGLOG_EXPORT=__attribute__((visibility(\\\"default\\\")))",
"-DHAVE_MODE_T",
"-DHAVE_SSIZE_T",
"-DHAVE_SYS_TYPES_H",
# For src/utilities.cc.
"-DHAVE_SYS_SYSCALL_H",
# For src/logging.cc to create symlinks.
Expand Down Expand Up @@ -194,11 +189,11 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
}),
hdrs = [
"src/glog/log_severity.h",
"src/glog/logging.h",
"src/glog/platform.h",
":logging_h",
":raw_logging_h",
":stl_logging_h",
":vlog_is_on_h",
"src/glog/raw_logging.h",
"src/glog/stl_logging.h",
"src/glog/vlog_is_on.h",
],
# https://github.com/google/glog/issues/837: Replacing
# `strip_include_prefix` with `includes` would avoid spamming
Expand Down Expand Up @@ -261,10 +256,10 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
name = "strip_include_prefix_hack",
hdrs = [
"src/glog/log_severity.h",
":logging_h",
":raw_logging_h",
":stl_logging_h",
":vlog_is_on_h",
"src/glog/logging.h",
"src/glog/raw_logging.h",
"src/glog/stl_logging.h",
"src/glog/vlog_is_on.h",
],
)

Expand All @@ -274,51 +269,3 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
out = "glog_internal/config.h",
substitutions = {"#cmakedefine": "//cmakedefine"},
)

common_config = {
"@ac_cv_have_u_int16_t@": "0",
"@ac_cv_have_glog_export@": "0",
"@ac_google_start_namespace@": "namespace google {",
"@ac_google_end_namespace@": "}",
"@ac_google_namespace@": "google",
}

posix_config = dict_union(common_config, {
"@ac_cv___attribute___noinline@": "__attribute__((noinline))",
"@ac_cv___attribute___printf_4_5@": "__attribute__((__format__(__printf__, 4, 5)))",
"@ac_cv_have___builtin_expect@": "1",
"@ac_cv_have_libgflags@": "1" if with_gflags else "0",
"@ac_cv_have_mode_t@": "1",
"@ac_cv_have_ssize_t@": "1",
"@ac_cv_have_systypes_h@": "1",
"@ac_cv_have_unistd_h@": "1",
})

windows_config = dict_union(common_config, {
"@ac_cv___attribute___noinline@": "",
"@ac_cv___attribute___printf_4_5@": "",
"@ac_cv_have___builtin_expect@": "0",
"@ac_cv_have_libgflags@": "0",
"@ac_cv_have_mode_t@": "0",
"@ac_cv_have_ssize_t@": "0",
"@ac_cv_have_systypes_h@": "0",
"@ac_cv_have_unistd_h@": "0",
})

[
expand_template(
name = "%s_h" % f,
template = "src/glog/%s.h.in" % f,
out = "src/glog/%s.h" % f,
substitutions = select({
"@bazel_tools//src/conditions:windows": windows_config,
"//conditions:default": posix_config,
}),
)
for f in [
"vlog_is_on",
"stl_logging",
"raw_logging",
"logging",
]
]
14 changes: 6 additions & 8 deletions src/base/commandlineflags.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#include <cstring> // for memchr
#include <string>

#ifdef HAVE_LIB_GFLAGS
#ifdef GLOG_USE_GFLAGS

#include <gflags/gflags.h>

Expand All @@ -80,18 +80,16 @@
DEFINE_VARIABLE(bool, B, name, value, meaning, bool)

// int32 specialization
#define DECLARE_int32(name) \
DECLARE_VARIABLE(GOOGLE_NAMESPACE::int32, I, name, int32)
#define DECLARE_int32(name) DECLARE_VARIABLE(google::int32, I, name, int32)
#define DEFINE_int32(name, value, meaning) \
DEFINE_VARIABLE(GOOGLE_NAMESPACE::int32, I, name, value, meaning, int32)
DEFINE_VARIABLE(google::int32, I, name, value, meaning, int32)

// uint32 specialization
#ifndef DECLARE_uint32
#define DECLARE_uint32(name) \
DECLARE_VARIABLE(GOOGLE_NAMESPACE::uint32, U, name, uint32)
#define DECLARE_uint32(name) DECLARE_VARIABLE(google::uint32, U, name, uint32)
#endif // DECLARE_uint64
#define DEFINE_uint32(name, value, meaning) \
DEFINE_VARIABLE(GOOGLE_NAMESPACE::uint32, U, name, value, meaning, uint32)
DEFINE_VARIABLE(google::uint32, U, name, value, meaning, uint32)

// Special case for string, because we have to specify the namespace
// std::string, which doesn't play nicely with our FLAG__namespace hackery.
Expand All @@ -108,7 +106,7 @@
} \
using fLS::FLAGS_##name

#endif // HAVE_LIB_GFLAGS
#endif // GLOG_USE_GFLAGS

// Define GLOG_DEFINE_* using DEFINE_* . By using these macros, we
// have GLOG_* environ variables even if we have gflags installed.
Expand Down
8 changes: 4 additions & 4 deletions src/cleanup_immediately_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "glog/raw_logging.h"
#include "googletest.h"

#ifdef HAVE_LIB_GFLAGS
#ifdef GLOG_USE_GFLAGS
#include <gflags/gflags.h>
using namespace GFLAGS_NAMESPACE;
#endif
Expand All @@ -42,7 +42,7 @@ using namespace GFLAGS_NAMESPACE;

#include "mock-log.h"
// Introduce several symbols from gmock.
using GOOGLE_NAMESPACE::glog_testing::ScopedMockLog;
using google::glog_testing::ScopedMockLog;
using testing::_;
using testing::AllOf;
using testing::AnyNumber;
Expand All @@ -52,7 +52,7 @@ using testing::StrictMock;
using testing::StrNe;
#endif

using namespace GOOGLE_NAMESPACE;
using namespace google;

TEST(CleanImmediately, logging) {
google::SetLogFilenameExtension(".foobar");
Expand All @@ -68,7 +68,7 @@ TEST(CleanImmediately, logging) {
int main(int argc, char **argv) {
FLAGS_colorlogtostderr = false;
FLAGS_timestamp_in_logfile_name = true;
#ifdef HAVE_LIB_GFLAGS
#ifdef GLOG_USE_GFLAGS
ParseCommandLineFlags(&argc, &argv, true);
#endif
// Make sure stderr is not buffered as stderr seems to be buffered
Expand Down
8 changes: 4 additions & 4 deletions src/cleanup_with_absolute_prefix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "glog/raw_logging.h"
#include "googletest.h"

#ifdef HAVE_LIB_GFLAGS
#ifdef GLOG_USE_GFLAGS
#include <gflags/gflags.h>
using namespace GFLAGS_NAMESPACE;
#endif
Expand All @@ -42,7 +42,7 @@ using namespace GFLAGS_NAMESPACE;

#include "mock-log.h"
// Introduce several symbols from gmock.
using GOOGLE_NAMESPACE::glog_testing::ScopedMockLog;
using google::glog_testing::ScopedMockLog;
using testing::_;
using testing::AllOf;
using testing::AnyNumber;
Expand All @@ -52,7 +52,7 @@ using testing::StrictMock;
using testing::StrNe;
#endif

using namespace GOOGLE_NAMESPACE;
using namespace google;

TEST(CleanImmediatelyWithAbsolutePrefix, logging) {
google::EnableLogCleaner(0);
Expand All @@ -73,7 +73,7 @@ TEST(CleanImmediatelyWithAbsolutePrefix, logging) {
int main(int argc, char **argv) {
FLAGS_colorlogtostderr = false;
FLAGS_timestamp_in_logfile_name = true;
#ifdef HAVE_LIB_GFLAGS
#ifdef GLOG_USE_GFLAGS
ParseCommandLineFlags(&argc, &argv, true);
#endif
// Make sure stderr is not buffered as stderr seems to be buffered
Expand Down
Loading

0 comments on commit 3b278fe

Please sign in to comment.