From 1b417346c85d5267d50df44ecb3f55d5a818e6d1 Mon Sep 17 00:00:00 2001 From: Dup4 Date: Tue, 27 Dec 2022 20:53:22 +0800 Subject: [PATCH] chore: support bazel --- .gitignore | 10 ++++++++++ .vscode/settings.json | 2 +- BUILD.bazel | 45 +++++++++++++++++++++++++++++++++++++++++++ Makefile | 12 ++++++++++++ WORKSPACE | 31 +++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 BUILD.bazel create mode 100644 WORKSPACE diff --git a/.gitignore b/.gitignore index 34324f7..a16b45c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,13 @@ .coverage build __snapshots__ + +### Automatically added by Hedron's Bazel Compile Commands Extractor: https://github.com/hedronvision/bazel-compile-commands-extractor +# Ignore the `external` link (that is added by `bazel-compile-commands-extractor`). The link differs between macOS/Linux and Windows, so it shouldn't be checked in. The pattern must not end with a trailing `/` because it's a symlink on macOS/Linux. +/external +# Ignore links to Bazel's output. The pattern needs the `*` because people can change the name of the directory into which your repository is cloned (changing the `bazel-` symlink), and must not end with a trailing `/` because it's a symlink on macOS/Linux. +/bazel-* +# Ignore generated output. Although valuable (after all, the primary purpose of `bazel-compile-commands-extractor` is to produce `compile_commands.json`!), it should not be checked in. +/compile_commands.json +# Ignore the directory in which `clangd` stores its local index. +/.cache/ diff --git a/.vscode/settings.json b/.vscode/settings.json index 0296d44..01d369a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,7 +6,7 @@ "files.insertFinalNewline": true, "files.trimFinalNewlines": true, "clangd.arguments": [ - "--compile-commands-dir=./build", + "--compile-commands-dir=./", "--background-index", "--header-insertion=iwyu", "--all-scopes-completion", diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000..b5ec5c9 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,45 @@ +package(default_visibility = ["//visibility:public"]) + +common_copts = [ + "-std=c++17", + "-O3", + "-g", + "-Wall", + "-Wextra", + "-Werror", +] + +cc_library( + name = "snapshot", + hdrs = glob(["include/snapshot/**/*.h"]), + includes = [ + "include", + ], +) + +cc_binary( + name = "benchmark", + srcs = glob([ + "test/*_benchmark.cc", + ]), + copts = common_copts, + deps = [ + ":snapshot", + "@google_benchmark//:benchmark_main", + ], +) + +cc_binary( + name = "unittest", + srcs = glob([ + "test/*_test.cc", + "test/**/*_test.cc", + ]), + copts = common_copts + [ + "-Wno-unused-result", + ], + deps = [ + ":snapshot", + "@gtest//:gtest_main", + ], +) diff --git a/Makefile b/Makefile index 3148673..532d772 100644 --- a/Makefile +++ b/Makefile @@ -38,3 +38,15 @@ clean_test: fi .PHONY: clean clean_test + +bazel_bench: + bazel run :benchmark --compilation_mode=opt + +bazel_ut: + bazel run :unittest --compilation_mode=opt + +bazel_clean: + rm -rf bazel-* external + +bazel_refresh_all: + bazel run @bazel_compile_commands_extractor//:refresh_all diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 0000000..f184967 --- /dev/null +++ b/WORKSPACE @@ -0,0 +1,31 @@ +load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") + +git_repository( + name = "bazel_build_files", + remote = "https://github.com/Dup4/bazel-build-files.git", + tag = "v0.0.1", +) + +git_repository( + name = "google_benchmark", + branch = "main", + build_file = "@bazel_build_files//google-benchmark:BUILD.bazel", + remote = "https://github.com/google/benchmark.git", +) + +git_repository( + name = "gtest", + branch = "main", + build_file = "@bazel_build_files//gtest:BUILD.bazel", + remote = "https://github.com/google/googletest.git", +) + +git_repository( + name = "bazel_compile_commands_extractor", + branch = "main", + remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git", +) + +load("@bazel_compile_commands_extractor//:workspace_setup.bzl", "hedron_compile_commands_setup") + +hedron_compile_commands_setup()