Skip to content

Commit

Permalink
feat: enable backward-cpp (#1)
Browse files Browse the repository at this point in the history
* feat: enable backward-cpp
* fix BINDIR for different compilation modes in elfutils
  • Loading branch information
fzakaria authored Jun 19, 2023
1 parent 7fb3174 commit 117f002
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 12 deletions.
71 changes: 59 additions & 12 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ http_archive(
url = "https://github.com/SRombauts/SQLiteCpp/archive/refs/tags/3.3.0.zip",
)

# backward-cpp
http_archive(
name = "backward-cpp",
build_file = "//third_party/backward-cpp:backward-cpp.BUILD",
sha256 = "42d9151d383f91813c12d06b0c76fd1caf6c6bfa07d1aa2e7ca8a775ad643bdc",
strip_prefix = "backward-cpp-2395cfa2422edb71929c9d166a6a614571331db3",
url = "https://github.com/bombela/backward-cpp/archive/2395cfa2422edb71929c9d166a6a614571331db3.tar.gz",
)

# Capstone
http_archive(
name = "capstone",
Expand All @@ -99,11 +108,50 @@ load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_

hedron_compile_commands_setup()

# NASM
http_archive(
name = "io_buildbuddy_buildbuddy_toolchain",
sha256 = "e899f235b36cb901b678bd6f55c1229df23fcbc7921ac7a3585d29bff2bf9cfd",
strip_prefix = "buildbuddy-toolchain-fd351ca8f152d66fc97f9d98009e0ae000854e8f",
urls = ["https://github.com/buildbuddy-io/buildbuddy-toolchain/archive/fd351ca8f152d66fc97f9d98009e0ae000854e8f.tar.gz"],
name = "nasm",
build_file_content = BUILD_ALL_CONTENT,
sha256 = "d833bf0f5716e89dbcd345b7f545f25fe348c6e2ef16dbc293e1027bcd22d881",
strip_prefix = "nasm-2.16.01",
url = "https://www.nasm.us/pub/nasm/releasebuilds/2.16.01/nasm-2.16.01.tar.gz",
)

# ELFUTILS
http_archive(
name = "elfutils",
build_file = "//third_party:elfutils.BUILD",
sha256 = "39bd8f1a338e2b7cd4abc3ff11a0eddc6e690f69578a57478d8179b4148708c8",
strip_prefix = "elfutils-0.189",
url = "https://sourceware.org/elfutils/ftp/0.189/elfutils-0.189.tar.bz2",
)

# ZLIB
http_archive(
name = "zlib",
build_file = "//third_party:zlib.BUILD",
sha256 = "c2856951bbf30e30861ace3765595d86ba13f2cf01279d901f6c62258c57f4ff",
strip_prefix = "zlib-1.2.13",
url = "https://github.com/madler/zlib/archive/refs/tags/v1.2.13.zip",
)

# LIBUNWIND
http_archive(
name = "libunwind",
build_file_content = BUILD_ALL_CONTENT,
sha256 = "c24c913d2337d6eff851b6ab32aadfb683a86fee48d28fe1fc9cd56c8e9dfa58",
strip_prefix = "libunwind-1.70",
url = "https://github.com/libunwind/libunwind/releases/download/v1.7.0/libunwind-1.70.tar.gz",
)

# LIBDWARF
http_archive(
name = "libdwarf",
#build_file = "//third_party:libdwarf.BUILD",
build_file_content = BUILD_ALL_CONTENT,
sha256 = "4518de76a92af80919945dc4d37dd88414d3fae9116dc424f07f25e1ecc7d004",
strip_prefix = "libdwarf-code-db0d0c4de28d3de217d79ed6ce6c8cf34cd017b9",
urls = ["https://github.com/davea42/libdwarf-code/archive/db0d0c4de28d3de217d79ed6ce6c8cf34cd017b9.tar.gz"],
)

http_archive(
Expand All @@ -115,18 +163,17 @@ http_archive(
],
)

http_archive(
name = "nasm",
build_file_content = BUILD_ALL_CONTENT,
sha256 = "d833bf0f5716e89dbcd345b7f545f25fe348c6e2ef16dbc293e1027bcd22d881",
strip_prefix = "nasm-2.16.01",
url = "https://www.nasm.us/pub/nasm/releasebuilds/2.16.01/nasm-2.16.01.tar.gz",
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

http_archive(
name = "io_buildbuddy_buildbuddy_toolchain",
sha256 = "e899f235b36cb901b678bd6f55c1229df23fcbc7921ac7a3585d29bff2bf9cfd",
strip_prefix = "buildbuddy-toolchain-fd351ca8f152d66fc97f9d98009e0ae000854e8f",
urls = ["https://github.com/buildbuddy-io/buildbuddy-toolchain/archive/fd351ca8f152d66fc97f9d98009e0ae000854e8f.tar.gz"],
)

load("@io_buildbuddy_buildbuddy_toolchain//:deps.bzl", "buildbuddy_deps")

buildbuddy_deps()
Expand Down
30 changes: 30 additions & 0 deletions third_party/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,33 @@ genrule(
cmd = "cp $< $@",
executable = True,
)

configure_make(
name = "libdwarf",
configure_in_place = True,
lib_source = "@libdwarf//:all",
out_static_libs = [
"libdwarf.a",
],
deps = [
"@elfutils//:elf",
],
alwayslink = True,
)

configure_make(
name = "libunwind",
configure_in_place = True,
configure_options = [
"--enable-static",
"--disable-shared",
],
copts = [
"-w",
],
lib_source = "@libunwind//:all",
out_static_libs = [
"libunwind.a",
],
alwayslink = False,
)
9 changes: 9 additions & 0 deletions third_party/backward-cpp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports_files(["backward.cc"])

cc_binary(
name = "suicide",
srcs = ["suicide.cc"],
deps = ["@backward-cpp"],
)

exports_files(["backward.cpp"])
24 changes: 24 additions & 0 deletions third_party/backward-cpp/backward-cpp.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cc_library(
name = "backward-cpp",
include_prefix = "backward",
srcs = ["@//third_party/backward-cpp:backward.cpp"],
hdrs = ["backward.hpp"],
deps = [
"@//third_party:libdwarf",
"@elfutils//:elf",
"@//third_party:libunwind",
],
visibility = ["//visibility:public"],
# Link any binary that depends (directly or indirectly) on this C++ library
# in all the object files for the files listed in srcs, even if some contain
# no symbols referenced by the binary.
# Without this the backward.cpp file won't be included and then won't
# initialize
alwayslink = True,
defines = [
# For libdwarf
"BACKWARD_HAS_DWARF=1",
# For libunwind
"BACKWARD_HAS_LIBUNWIND=1",
],
)
7 changes: 7 additions & 0 deletions third_party/backward-cpp/backward.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "backward.hpp"

namespace backward {

backward::SignalHandling sh;

} // namespace backward
11 changes: 11 additions & 0 deletions third_party/backward-cpp/suicide.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>

/**
* Obvious suicide function to test stacktrace printing of backward-cpp
*/
int main() {
std::cout << "Running suicide application" << std::endl;
int* ptr = nullptr;
*ptr += 1;
return 0;
}
65 changes: 65 additions & 0 deletions third_party/elfutils.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
LIBELF_HDRS = glob(["libelf/elf32_*.c"]) + [
"lib/crc32.c",
"lib/next_prime.c",
"libelf/elf.h",
"libelf/gelf.h",
"libelf/libelf.h",
]

cc_library(
name = "elf",
srcs = glob([
"libelf/*.c",
]) + [
"config.h",
"lib/eu-config.h",
"lib/fixedsizehash.h",
"lib/system.h",
"libelf/abstract.h",
"libelf/chdr_xlate.h",
"libelf/common.h",
"libelf/dl-hash.h",
"libelf/elf-knowledge.h",
"libelf/exttypes.h",
"libelf/gelf_xlate.h",
"libelf/gnuhash_xlate.h",
"libelf/libelfP.h",
"libelf/nlist.h",
"libelf/note_xlate.h",
"libelf/version_xlate.h",
],
hdrs = LIBELF_HDRS,
copts = [
"-Iexternal/elfutils/libelf",
"-Iexternal/elfutils/lib",
"-I$(BINDIR)/external/elfutils",
"-DHAVE_CONFIG_H",
# Disable all warnings
"-w",
],
visibility = ["//visibility:public"],
# Code seems to just include <libelf.h>
includes = ["libelf"],
deps = [
"@zlib",
],
)

genrule(
name = "config_h",
srcs = [
"configure",
"libelf/libelf.h",
"libdw/known-dwarf.h",
"config/config.rpath",
"config/ar-lib",
"config/compile",
"config/config.guess",
"config/config.sub",
"config/missing",
"config/install-sh",
] + glob(["**/*.in"]),
outs = ["config.h"],
cmd = "./$(location configure) --disable-debuginfod --disable-libdebuginfod" +
"&& cp config.h $(location config.h)",
)
41 changes: 41 additions & 0 deletions third_party/libdwarf.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
cc_library(
name = "libdwarf",
srcs = glob([
"libdwarf/*.c",
]) + [
"config.h",
],
hdrs = glob([
"libdwarf/*.h",
]),
copts = [
"-Ibazel-out/k8-dbg/bin/external/libdwarf ",
"-DHAVE_CONFIG_H",
],
strip_include_prefix = "libdwarf",
includes = [
"libdwarf",
],
visibility = ["//visibility:public"],
deps = [
"@elfutils//:elf",
],
)

genrule(
name = "config_h",
srcs = [
"configure",
"install-sh",
"config.guess",
"config.sub",
"configure.ac",
"ltmain.sh",
"ar-lib",
"compile",
"missing",
] + glob(["**/*.in"]),
outs = ["config.h"],
cmd = "./$(location configure) " +
"&& cp config.h $(location config.h)",
)
36 changes: 36 additions & 0 deletions third_party/zlib.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cc_library(
name = "zlib",
srcs = [
"adler32.c",
"compress.c",
"crc32.c",
"crc32.h",
"deflate.c",
"deflate.h",
"gzclose.c",
"gzguts.h",
"gzlib.c",
"gzread.c",
"gzwrite.c",
"infback.c",
"inffast.c",
"inffast.h",
"inffixed.h",
"inflate.c",
"inflate.h",
"inftrees.c",
"inftrees.h",
"trees.c",
"trees.h",
"uncompr.c",
"zconf.h",
"zutil.c",
"zutil.h",
],
hdrs = ["zlib.h"],
copts = [
# Ignore all warnings
"-w",
],
visibility = ["//visibility:public"],
)

0 comments on commit 117f002

Please sign in to comment.