Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bazel Build #61

Closed
pedrokiefer opened this issue Sep 23, 2015 · 24 comments
Closed

Bazel Build #61

pedrokiefer opened this issue Sep 23, 2015 · 24 comments

Comments

@pedrokiefer
Copy link

It would be really nice if you could add support for building with Bazel.

@dfr
Copy link
Contributor

dfr commented Aug 1, 2016

I spent a bit of time on this for one of my projects. The attached BUILD file might give you a starting point - it works well enough for my needs but your mileage may vary...
BUILD.txt

@liyistc
Copy link

liyistc commented Sep 26, 2016

Agreed. It'd be really useful.

@yliu120
Copy link

yliu120 commented Oct 5, 2016

Why not add a blaze build options to build this library? I still don't understand the point.

@breakds
Copy link

breakds commented Feb 2, 2017

Thank you @dfr for providing such a nice BUILD file, which saves me a lot of time! It does not work out of box for me, but with some modification it works pretty well. Here is what I have:

  1. In WORKSPACE:

    new_git_repository(
        name = "glog",
        remote = "https://github.com/google/glog.git",
        commit = "b6a5e0524c28178985f0d228e9eaa43808dbec3c",
        build_file = "glog.BUILD"
    )
  2. In glog.BUILD:

    # Derived from https://github.com/google/glog/files/393474/BUILD.txt
    #
    # Currently gflags is not included, since having deps
    # of ["//external:gflags"] does not seem to let it find
    # "gflags/gflags.h".
    #
    # Currently we need to do
    #     expoert GLOG_logtostderr=1
    #
    # To allow logging to stderr.
    cc_library(
        visibility = ["//visibility:public"],
        name = "glog",
        includes = ["src"],
        srcs = ["src/base/commandlineflags.h",
                "src/base/googleinit.h",
                "src/demangle.cc",
                "src/logging.cc",
                "src/symbolize.cc",
                "src/vlog_is_on.cc",
                "src/raw_logging.cc",
                "src/utilities.cc"],
        hdrs = ["src/base/mutex.h",
                "src/utilities.h",
                "src/demangle.h",
                "src/symbolize.h",
                ":stl_logging_h",
                ":config_h",
                ":logging_h",
                ":vlog_is_on_h",
                "raw_logging_h"],
        copts = [
            # Disable warnings that exists in glog
            "-Wno-sign-compare",
            "-Wno-unused-local-typedefs",
            # Inject google namespace as "google"
            "-D_START_GOOGLE_NAMESPACE_='namespace google {'",
            "-D_END_GOOGLE_NAMESPACE_='}'",
            "-DGOOGLE_NAMESPACE='google'",
            # Allows src/base/mutex.h to include pthread.h.
            "-DHAVE_PTHREAD",
            # Allows src/logging.cc to determine the host name.
            "-DHAVE_SYS_UTSNAME_H",
            # System header files enabler for src/utilities.cc
            # Enable system calls from syscall.h
            "-DHAVE_SYS_SYSCALL_H",
            # Enable system calls from sys/time.h
            "-DHAVE_SYS_TIME_H",
            "-DHAVE_STDINT_H",
            "-DHAVE_STRING_H",
            # For logging.cc
            "-DHAVE_PREAD",
        ],
    )
    
    # Below are the generation rules that generates the necessary header
    # files for glog. Originally they are generated by CMAKE
    # configure_file() command, which replaces certain template
    # placeholders in the .in files with provided values.
    
    # gen_sh is a bash script that provides the values for generated
    # header files. Under the hood it is just a wrapper over sed.
    genrule(
        name = "gen_sh",
        outs = [
            "gen.sh",
        ],
        cmd = """
    cat > $@ <<"EOF"
    #! /bin/sh
    sed -e 's/@ac_cv_have_unistd_h@/1/g' \
        -e 's/@ac_cv_have_stdint_h@/1/g' \
        -e 's/@ac_cv_have_systypes_h@/1/g' \
        -e 's/@ac_cv_have_libgflags_h@/1/g' \
        -e 's/@ac_cv_have_uint16_t@/1/g' \
        -e 's/@ac_cv_have___builtin_expect@/1/g' \
        -e 's/@ac_cv_have_.*@/0/g' \
        -e 's/@ac_google_start_namespace@/namespace google {/g' \
        -e 's/@ac_google_end_namespace@/}/g' \
        -e 's/@ac_google_namespace@/google/g' \
        -e 's/@ac_cv___attribute___noinline@/__attribute__((noinline))/g' \
        -e 's/@ac_cv___attribute___noreturn@/__attribute__((noreturn))/g' \
        -e 's/@ac_cv___attribute___printf_4_5@/__attribute__((__format__ (__printf__, 4, 5)))/g'
    EOF"""
    )
    
    genrule(
        name = "config_h",
        srcs = [
            "src/config.h.cmake.in",
        ],
        outs = [
            "config.h",
        ],
        cmd = "awk '{ gsub(/^#cmakedefine/, \"//cmakedefine\"); print; }' $(<) > $(@)",
    )
    
    genrule(
        name = "logging_h",
        srcs = [
            "src/glog/logging.h.in",
        ],
        outs = [
            "glog/logging.h",
        ],
        cmd = "$(location :gen_sh) < $(<) > $(@)",
        tools = [":gen_sh"],
    )
    
    genrule(
        name = "raw_logging_h",
        srcs = [
            "src/glog/raw_logging.h.in",
        ],
        outs = [
            "glog/raw_logging.h",
        ],
        cmd = "$(location :gen_sh) < $(<) > $(@)",
        tools = [":gen_sh"],
    )
    
    genrule(
        name = "stl_logging_h",
        srcs = [
            "src/glog/stl_logging.h.in",
        ],
        outs = [
            "glog/stl_logging.h",
        ],
        cmd = "$(location :gen_sh) < $(<) > $(@)",
        tools = [":gen_sh"],
    )
    
    genrule(
        name = "vlog_is_on_h",
        srcs = [
            "src/glog/vlog_is_on.h.in",
        ],
        outs = [
            "glog/vlog_is_on.h",
        ],
        cmd = "$(location :gen_sh) < $(<) > $(@)",
        tools = [":gen_sh"],
    )

With the above setting I was able to use the dependency
//external:glog and #include "glog/logging.h" to have my project
depends on glog.

However, I was not able to compile glog with gflags even though I
have

git_repository(
    name   = "com_github_gflags_gflags",
    remote = "https://github.com/gflags/gflags.git",
    tag = "v2.2.0",
)

bind(
    name = "gflags",
    actual = "@com_github_gflags_gflags//:gflags",
)

Adding dependencies of //external:flags with -DHAVE_LIB_GFLAGS
still produces errors saying gflags/gflags.h is not found. It would be great
if someone with more experience can take a look :)

@kiyofumi-kan
Copy link

According to bazelbuild/bazel#818
In WORKSPACE, I have to write

new_git_repository(
    name = "glog_repo",
    remote = "https://github.com/google/glog.git",
    commit = "b6a5e0524c28178985f0d228e9eaa43808dbec3c",
    build_file = "glog.BUILD"
)

bind(
    name = "glog",
    actual = "@glog_repo//:glog"
)

Instead to make it work.

@ushakov
Copy link

ushakov commented Feb 13, 2017

Re: failing to include gflags/gflags.h: glog includes that using #include <gflags/gflags.h> directive, so it tries to include from system directories. (On my system, that resulted in a havoc after it included an older version from system-wide directories.) I tried to add -isystem flags here and there, and also preprocess the file in question to change <...> to "..." but did not fully succeed.

There are two places where gflags/gflags.h is included: in logging.h and in base/commandlineflags.h. The former is present in the archive as logging.h.in, gets preprocessed through the gen.sh script, and looks fine. The latter however is trickier: if you process it through gen.sh, it will still be included from its original location, not from the genfiles directory.

Does anyone have any idea?

@dfr
Copy link
Contributor

dfr commented Feb 13, 2017 via email

@dfr
Copy link
Contributor

dfr commented Feb 13, 2017 via email

@ushakov
Copy link

ushakov commented Feb 13, 2017

I also have that. Moreover, if I go into the checked-out glog directory and change angle brackets in base/commandlineflags.h to quotes, it starts compiling happily. Of course, if my system has compatible gflags installed, it also compiles well :)

@dfr
Copy link
Contributor

dfr commented Feb 13, 2017 via email

@ushakov
Copy link

ushakov commented Feb 13, 2017

I tried it both ways actually... I probably need to investigate more.

@kiyofumi-kan
Copy link

FYI: I found this workaround: https://github.com/twitter/heron/tree/master/third_party/glog

@ushakov
Copy link

ushakov commented Feb 14, 2017

Oh, this looks interesting -- but this is basically vendoring glog inside your own repo, and I think vendoring as code is better than vendoring as tar.gz :)

@antonovvk
Copy link

Just add to copts the following line:
"-isystem $(GENDIR)/external/gflags_repo/"
And it all works.

@antonovvk
Copy link

Here's my final working WORKSPACE and BUILD with some syntactic sugar to get rid of direct genrule() invocation.
By the way, looks like every direct inclusion of "glog/logging.h" in other libs/bins requires fix for gflags in their copts.

@gigimushroom
Copy link

Thanks, using @antonovvk solution make it work perfect!

@drigz
Copy link
Member

drigz commented Aug 10, 2017

By adjusting @antonovvk's WORKSPACE to use gflags v2.2.1, I was able to avoid the -isystem workaround. I also changed gflags_repo to com_github_gflags_gflags to avoid a Bazel warning.

@yoni386
Copy link

yoni386 commented Aug 14, 2017

Bazel can't fetch any external packages?
The author must first prepare it (make BUILD)? As gflags did.

@qzmfranklin
Copy link
Contributor

Just a note, a Bazel BUILD file is merged to glog here: #232

That PR was heavily inspired by the discussion happened here (credited in the BUILD file in that PR @breakds ).

@drigz
Copy link
Member

drigz commented Dec 20, 2017

Yep, this was fixed by @qzmfranklin - thanks!

@drigz drigz closed this as completed Dec 20, 2017
@zoukyle
Copy link

zoukyle commented Dec 10, 2020

When I use it in the bazel project, I got a lot of notes and warnings:
How can I avoid them? Thanks

bazel-out/k8-fastbuild/bin/external/com_github_google_glog/_virtual_includes/default_glog_headers/glog/logging.h: In instantiation of 'std::__cxx11::string* google::Check_LTImpl(const T1&, const T2&, const char*) [with T1 = int; T2 = long unsigned int; std::__cxx11::string = std::__cxx11::basic_string<char>]':
external/deepmap_base/common/geometry/g2_quadtree_key.h:257:5:   required from here
bazel-out/k8-fastbuild/bin/external/com_github_google_glog/_virtual_includes/default_glog_headers/glog/logging.h:734:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 DEFINE_CHECK_OP_IMPL(Check_LT, < )
                                ^
bazel-out/k8-fastbuild/bin/external/com_github_google_glog/_virtual_includes/default_glog_headers/glog/logging.h:734:1: note: in expansion of macro 'DEFINE_CHECK_OP_IMPL'
 DEFINE_CHECK_OP_IMPL(Check_LT, < )
 ^~~~~~~~~~~~~~~~~~~~
bazel-out/k8-fastbuild/bin/external/com_github_google_glog/_virtual_includes/default_glog_headers/glog/logging.h: In instantiation of 'std::__cxx11::string* google::Check_EQImpl(const T1&, const T2&, const char*) [with T1 = int; T2 = long unsigned int; std::__cxx11::string = std::__cxx11::basic_string<char>]':
external/deepmap_base/common/geometry/plane_3d.h:34:5:   required from here
bazel-out/k8-fastbuild/bin/external/com_github_google_glog/_virtual_includes/default_glog_headers/glog/logging.h:731:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 DEFINE_CHECK_OP_IMPL(Check_EQ, ==)  // Compilation error with CHECK_EQ(NULL, x)?
                                ^
bazel-out/k8-fastbuild/bin/external/com_github_google_glog/_virtual_includes/default_glog_headers/glog/logging.h:731:1: note: in expansion of macro 'DEFINE_CHECK_OP_IMPL'
 DEFINE_CHECK_OP_IMPL(Check_EQ, ==)  // Compilation error with CHECK_EQ(NULL, x)?

@dfr
Copy link
Contributor

dfr commented Dec 10, 2020 via email

@zoukyle
Copy link

zoukyle commented Dec 16, 2020

@dfr Thanks a lot for your reply. I'm actually using the same way as you mentioned in your thread. During the build, the glog will be compiled and it gave me a lot of warnings. I want to hide those warnings.

@dfr
Copy link
Contributor

dfr commented Dec 17, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

14 participants