Skip to content

Commit

Permalink
fix: remove rules_cc_stamp dep
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Sep 21, 2023
1 parent 38d796f commit 9615f4b
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 8 deletions.
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@rules_cc_stamp//:index.bzl", "cc_stamp_header")
load("//bazel/tools:cc_stamp_header.bzl", "cc_stamp_header")
load("//bazel:copts.bzl", "copts")

package(default_visibility = ["//visibility:public"])
Expand Down
7 changes: 0 additions & 7 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")

http_archive(
name = "rules_cc_stamp",
strip_prefix = "rules_cc_stamp-63d4861f4d420b574fa0f112599aae2b8aee785e",
urls = ["https://github.com/zaucy/rules_cc_stamp/archive/63d4861f4d420b574fa0f112599aae2b8aee785e.zip"],
sha256 = "f469a3b97eeabeb850c655f59ea17799ff40badd3a0b3e9de38534c89ad2f87d",
)

http_archive(
name = "ecsact_si_wasm",
sha256 = "e208a94d4f4a9c09f32b8a9ea91a4f799492e11c7c852b0329b4a3595a45cee6",
Expand Down
9 changes: 9 additions & 0 deletions bazel/tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//bazel:copts.bzl", "copts")

cc_binary(
name = "cc_stamp_header_generator",
copts = copts,
visibility = ["//:__subpackages__"],
srcs = ["cc_stamp_header_generator.cc"],
)
31 changes: 31 additions & 0 deletions bazel/tools/cc_stamp_header.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def _cc_stamp_header(ctx):
out = ctx.outputs.out
args = ctx.actions.args()

args.add("--stable_status", ctx.info_file)
args.add("--volatile_status", ctx.version_file)
args.add("--output_header", out)

ctx.actions.run(
outputs = [out],
inputs = [ctx.info_file, ctx.version_file],
arguments = [args],
executable = ctx.executable.tool,
)

return DefaultInfo(files = depset([out]))

cc_stamp_header = rule(
implementation = _cc_stamp_header,
attrs = {
"out": attr.output(
doc = "C++ header file to generate",
mandatory = True,
),
"tool": attr.label(
default = Label("@ecsact_cli//bazel/tools:cc_stamp_header_generator"),
cfg = "exec",
executable = True,
),
},
)
92 changes: 92 additions & 0 deletions bazel/tools/cc_stamp_header_generator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <string>
#include <fstream>
#include <iostream>
#include <stdexcept>

std::string escape_cpp_value
( std::string value
)
{
if(value == "true" || value == "false") {
return value;
}

return "\"" + value + "\"";
}

void write_cc_constexpr_lines
( std::ifstream& status_file_stream
, std::ofstream& output_stream
)
{
std::string line;
std::string::size_type ws_idx;

// Reading file based on:
// https://docs.bazel.build/versions/master/user-manual.html#workspace_status
while(status_file_stream.good()) {
line.clear();
std::getline(status_file_stream, line);
if(line.empty()) continue;

ws_idx = line.find_first_of(' ');

output_stream
<< "constexpr auto " << line.substr(0, ws_idx) // key
<< " = " << escape_cpp_value(line.substr(ws_idx + 1)) << ";\n"; // value
}
}

int main(int argc, char* argv[]) {
std::string stable_status_path;
std::string volatile_status_path;
std::string output_header_path;

for(int i=1; argc > i; ++i) {
std::string arg(argv[i]);

if(arg == "--stable_status") {
stable_status_path = std::string(argv[++i]);
} else if(arg == "--volatile_status") {
volatile_status_path = std::string(argv[++i]);
} else if(arg == "--output_header") {
output_header_path = std::string(argv[++i]);
} else {
std::cerr << "invalid argument: " << arg << std::endl;
return 1;
}
}

if(stable_status_path.empty()) {
std::cerr << "missing --stable_status" << std::endl;
return 1;
}

if(volatile_status_path.empty()) {
std::cerr << "missing --volatile_status" << std::endl;
return 1;
}

if(output_header_path.empty()) {
std::cerr << "missing --output_header" << std::endl;
return 1;
}

try {
std::ifstream stable_status_stream(stable_status_path);
std::ifstream volatile_status_stream(volatile_status_path);
std::ofstream output_header_stream(output_header_path);

output_header_stream
<< "#pragma once\n\n"
<< "// THIS FILE IS GENERATED - DO NOT EDIT\n\n";

write_cc_constexpr_lines(stable_status_stream, output_header_stream);
write_cc_constexpr_lines(volatile_status_stream, output_header_stream);
} catch(const std::exception& err) {
std::cerr << "EXCEPTION THROWN: err.what() == " << err.what() << std::endl;
return 2;
}

return 0;
}

0 comments on commit 9615f4b

Please sign in to comment.