Skip to content

Commit

Permalink
feat: add codegen plugin bazel api
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Sep 19, 2023
1 parent 2e93b5a commit 2027a9f
Show file tree
Hide file tree
Showing 22 changed files with 300 additions and 307 deletions.
15 changes: 1 addition & 14 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
build --enable_platform_specific_config
build --incompatible_use_platforms_repo_for_constraints
build --incompatible_enable_cc_toolchain_resolution
build --incompatible_strict_action_env
build --enable_runfiles

build:windows --platforms=//bazel/platforms:windows

build:linux --platforms=//bazel/platforms:linux
build:linux --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux

common:ci --announce_rc
common:ci --disk_cache=~/.cache/bazel-disk-cache
test:ci --test_output=errors
import %workspace%/bazel/common.bazelrc

try-import %workspace%/user.bazelrc
15 changes: 5 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# bazel
/bazel-*
/user.bazelrc

# https://github.com/hedronvision/bazel-compile-commands-extractor
/compile_commands.json
/external

# clangd
/.cache/
bazel-*
user.bazelrc
compile_commands.json
external
.cache
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ cc_library(
deps = [
"@ecsact_runtime//:common",
"@ecsact_runtime//:meta",
"@ecsact_runtime//:codegen_plugin_cc",
"@ecsact_lang_cpp//:support",
"@ecsact_codegen//:plugin",
],
)

Expand Down
10 changes: 10 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module(
name = "ecsact_lang_cpp",
version = "0.1.0",
compatibility_level = 1,
)

bazel_dep(name = "rules_cc", version = "0.0.8")
bazel_dep(name = "ecsact_runtime", version = "0.5.0")
bazel_dep(name = "rules_ecsact", version = "0.4.0")
bazel_dep(name = "ecsact_codegen", version = "0.1.2")
38 changes: 1 addition & 37 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -1,39 +1,5 @@
workspace(name = "ecsact_lang_cpp")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "ecsact_runtime",
sha256 = "35b03aaef0925fda5b5aefb2d6a6e2c9593f31d6414ab157091f9ca26a992da3",
strip_prefix = "ecsact_runtime-0.3.0",
urls = ["https://github.com/ecsact-dev/ecsact_runtime/archive/refs/tags/0.3.0.tar.gz"],
)

http_archive(
name = "ecsact_codegen",
sha256 = "5a1a466dbddbe042c2e5910f3b65ca44a5ffcef5831fdd0c3b2e2863c13cfb31",
strip_prefix = "ecsact_codegen-0.0.2",
urls = ["https://github.com/ecsact-dev/ecsact_codegen/archive/refs/tags/0.0.2.tar.gz"],
)

http_archive(
name = "boost",
sha256 = "4a79c389add7e3d54d0e12c83098d471d24536ba2d6b8593d3a95f151f25eebb",
strip_prefix = "boost-da62319c330d81ef032517cbe13b6f35d97387cb",
urls = ["https://github.com/bazelboost/boost/archive/da62319c330d81ef032517cbe13b6f35d97387cb.zip"],
)

load("@boost//:index.bzl", "boost_http_archives")

boost_http_archives()

http_archive(
name = "com_google_googletest",
sha256 = "d4cb2430eb82495a49a24310a609b99726726bf126c2fbfa92fd04c14f6a3b31",
strip_prefix = "googletest-d1a0039b97291dd1dc14f123b906bb7622ffe07c",
url = "https://github.com/google/googletest/archive/d1a0039b97291dd1dc14f123b906bb7622ffe07c.zip",
)

http_archive(
name = "hedron_compile_commands",
sha256 = "9d1ce53443747dd574a309fbe6774d1cfc94d20abdb7e7de1bb337f9437761bb",
Expand All @@ -60,9 +26,7 @@ load("@com_grail_bazel_toolchain//toolchain:rules.bzl", "llvm_toolchain")

llvm_toolchain(
name = "llvm_toolchain",
cxx_standard = {"linux": "c++20"},
distribution = "clang+llvm-15.0.6-x86_64-linux-gnu-ubuntu-18.04.tar.xz",
llvm_version = "15.0.6",
llvm_version = "16.0.4",
)

load("@llvm_toolchain//:toolchains.bzl", "llvm_register_toolchains")
Expand Down
19 changes: 0 additions & 19 deletions bazel/platforms/BUILD.bazel

This file was deleted.

97 changes: 97 additions & 0 deletions codegen_plugin.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
load("@rules_ecsact//ecsact:defs.bzl", "ecsact_codegen_plugin")
load("@rules_ecsact//ecsact/private:ecsact_codegen_plugin.bzl", "EcsactCodegenPluginInfo")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")

def _cc_ecsact_codegen_plugin_impl(ctx):
plugin = None
files = ctx.attr.cc_binary[DefaultInfo].files

for file in files.to_list():
if file.extension == "so":
plugin = file
if file.extension == "dll":
plugin = file

return [
EcsactCodegenPluginInfo(
output_extension = ctx.attr.output_extension,
plugin = plugin,
data = [plugin],
),
]

_cc_ecsact_codegen_plugin = rule(
implementation = _cc_ecsact_codegen_plugin_impl,
attrs = {
"cc_binary": attr.label(mandatory = True),
"output_extension": attr.string(mandatory = True),
},
)

_generated_src = """
#include "ecsact/codegen/plugin.h"
const char* ecsact_codegen_plugin_name() {{
return "{output_extension}";
}}
"""

def _cc_ecsact_codegen_plugin_src_impl(ctx):
output_cc_src = ctx.actions.declare_file("{}.plugin_name.cc".format(ctx.attr.name))
ctx.actions.write(
output = output_cc_src,
content = _generated_src.format(output_extension = ctx.attr.output_extension),
)

return [
DefaultInfo(files = depset([output_cc_src])),
]

_cc_ecsact_codegen_plugin_src = rule(
implementation = _cc_ecsact_codegen_plugin_src_impl,
attrs = {
"output_extension": attr.string(mandatory = True),
},
)

def cc_ecsact_codegen_plugin(name = None, srcs = [], deps = [], defines = [], no_validate_test = False, output_extension = None, **kwargs):
"""Create ecsact codegen plugin with C++
NOTE: ecsact_codegen_plugin_name() is automatically generated for you based
on the `output_extension` attribute.
Args:
name: Passed to underling cc_binary
srcs: Passed to underling cc_binary
deps: Passed to underling cc_binary
defines: Passed to underling cc_binary
output_extension: File extension the plugin writes to
no_validate_test: Don't create plugin validation test (not recommended)
**kwargs: Passed to underling cc_binary
"""
cc_binary(
name = "{}_bin".format(name),
srcs = srcs + [
"@ecsact_runtime//dylib:dylib.cc",
":{}__pn".format(name),
],
deps = deps + [
"@ecsact_runtime//:dylib",
"@ecsact_runtime//dylib:meta",
"@ecsact_codegen//:plugin",
],
defines = defines + ["ECSACT_META_API_LOAD_AT_RUNTIME"],
linkshared = True,
**kwargs
)

_cc_ecsact_codegen_plugin_src(
name = "{}__pn".format(name),
output_extension = output_extension,
)

_cc_ecsact_codegen_plugin(
name = name,
cc_binary = ":{}_bin".format(name),
output_extension = output_extension,
)
2 changes: 1 addition & 1 deletion cpp_header_codegen/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//bazel:copts.bzl", "copts")
load("@ecsact_codegen//:codegen_plugin.bzl", "cc_ecsact_codegen_plugin")
load("//:codegen_plugin.bzl", "cc_ecsact_codegen_plugin")

package(default_visibility = ["//visibility:public"])

Expand Down
4 changes: 2 additions & 2 deletions cpp_header_codegen/cpp_header_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <string>
#include <cassert>
#include "ecsact/runtime/meta.hh"
#include "ecsact/codegen_plugin.h"
#include "ecsact/codegen_plugin.hh"
#include "ecsact/codegen/plugin.h"
#include "ecsact/codegen/plugin.hh"
#include "ecsact/lang-support/lang-cc.hh"

constexpr auto GENERATED_FILE_DISCLAIMER = R"(// GENERATED FILE - DO NOT EDIT
Expand Down
2 changes: 1 addition & 1 deletion cpp_meta_header_codegen/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//bazel:copts.bzl", "copts")
load("@ecsact_codegen//:codegen_plugin.bzl", "cc_ecsact_codegen_plugin")
load("//:codegen_plugin.bzl", "cc_ecsact_codegen_plugin")

package(default_visibility = ["//visibility:public"])

Expand Down
4 changes: 2 additions & 2 deletions cpp_meta_header_codegen/cpp_meta_header_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include <tuple>
#include <map>
#include "ecsact/runtime/meta.hh"
#include "ecsact/codegen_plugin.h"
#include "ecsact/codegen_plugin.hh"
#include "ecsact/codegen/plugin.h"
#include "ecsact/codegen/plugin.hh"
#include "ecsact/lang-support/lang-cc.hh"
#include "ecsact/cpp_codegen_plugin_util.hh"

Expand Down
2 changes: 1 addition & 1 deletion cpp_systems_header_codegen/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//bazel:copts.bzl", "copts")
load("@ecsact_codegen//:codegen_plugin.bzl", "cc_ecsact_codegen_plugin")
load("//:codegen_plugin.bzl", "cc_ecsact_codegen_plugin")

package(default_visibility = ["//visibility:public"])

Expand Down
4 changes: 2 additions & 2 deletions cpp_systems_header_codegen/cpp_systems_header_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <filesystem>
#include <set>
#include "ecsact/runtime/meta.hh"
#include "ecsact/codegen_plugin.h"
#include "ecsact/codegen_plugin.hh"
#include "ecsact/codegen/plugin.h"
#include "ecsact/codegen/plugin.hh"
#include "ecsact/lang-support/lang-cc.hh"

namespace fs = std::filesystem;
Expand Down
2 changes: 1 addition & 1 deletion cpp_systems_source_codegen/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//bazel:copts.bzl", "copts")
load("@ecsact_codegen//:codegen_plugin.bzl", "cc_ecsact_codegen_plugin")
load("//:codegen_plugin.bzl", "cc_ecsact_codegen_plugin")

package(default_visibility = ["//visibility:public"])

Expand Down
4 changes: 2 additions & 2 deletions cpp_systems_source_codegen/cpp_systems_source_codegen.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <string>
#include <filesystem>
#include "ecsact/runtime/meta.hh"
#include "ecsact/codegen_plugin.h"
#include "ecsact/codegen_plugin.hh"
#include "ecsact/codegen/plugin.h"
#include "ecsact/codegen/plugin.hh"
#include "ecsact/lang-support/lang-cc.hh"

namespace fs = std::filesystem;
Expand Down
2 changes: 1 addition & 1 deletion ecsact/cpp_codegen_plugin_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <string>
#include <utility>
#include <concepts>
#include "ecsact/codegen_plugin.hh"
#include "ecsact/codegen/plugin.hh"
#include "ecsact/runtime/meta.hh"

namespace ecsact::cpp_codegen_plugin_util {
Expand Down
2 changes: 1 addition & 1 deletion systems_header_codegen/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//bazel:copts.bzl", "copts")
load("@ecsact_codegen//:codegen_plugin.bzl", "cc_ecsact_codegen_plugin")
load("//:codegen_plugin.bzl", "cc_ecsact_codegen_plugin")

package(default_visibility = ["//visibility:public"])

Expand Down
4 changes: 2 additions & 2 deletions systems_header_codegen/systems_header_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <cstring>
#include <algorithm>
#include "ecsact/runtime/meta.h"
#include "ecsact/codegen_plugin.h"
#include "ecsact/codegen_plugin.hh"
#include "ecsact/codegen/plugin.h"
#include "ecsact/codegen/plugin.hh"
#include "ecsact/lang-support/lang-cc.hh"

constexpr auto GENERATED_FILE_DISCLAIMER = R"(// GENERATED FILE - DO NOT EDIT
Expand Down
17 changes: 1 addition & 16 deletions test/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
common --enable_bzlmod
build --enable_platform_specific_config
build --incompatible_use_platforms_repo_for_constraints
build --incompatible_enable_cc_toolchain_resolution
build --incompatible_strict_action_env
build --enable_runfiles

build:windows --platforms=@ecsact_lang_cpp//bazel/platforms:windows
build:windows --host_platform=@ecsact_lang_cpp//bazel/platforms:windows

build:linux --platforms=@ecsact_lang_cpp//bazel/platforms:linux
build:linux --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux

common:ci --announce_rc
common:ci --disk_cache=~/.cache/bazel-disk-cache
test:ci --test_output=errors
import %workspace%/../bazel/common.bazelrc

try-import %workspace%/user.bazelrc
Loading

0 comments on commit 2027a9f

Please sign in to comment.