Skip to content

Commit c37aa71

Browse files
authored
fix: remove rules_cc_stamp dep (#35)
1 parent 38d796f commit c37aa71

File tree

5 files changed

+130
-8
lines changed

5 files changed

+130
-8
lines changed

BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@rules_cc//cc:defs.bzl", "cc_binary")
2-
load("@rules_cc_stamp//:index.bzl", "cc_stamp_header")
2+
load("//bazel/tools:cc_stamp_header.bzl", "cc_stamp_header")
33
load("//bazel:copts.bzl", "copts")
44

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

WORKSPACE.bazel

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
22

3-
http_archive(
4-
name = "rules_cc_stamp",
5-
strip_prefix = "rules_cc_stamp-63d4861f4d420b574fa0f112599aae2b8aee785e",
6-
urls = ["https://github.com/zaucy/rules_cc_stamp/archive/63d4861f4d420b574fa0f112599aae2b8aee785e.zip"],
7-
sha256 = "f469a3b97eeabeb850c655f59ea17799ff40badd3a0b3e9de38534c89ad2f87d",
8-
)
9-
103
http_archive(
114
name = "ecsact_si_wasm",
125
sha256 = "e208a94d4f4a9c09f32b8a9ea91a4f799492e11c7c852b0329b4a3595a45cee6",

bazel/tools/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
load("//bazel:copts.bzl", "copts")
3+
4+
cc_binary(
5+
name = "cc_stamp_header_generator",
6+
copts = copts,
7+
visibility = ["//:__subpackages__"],
8+
srcs = ["cc_stamp_header_generator.cc"],
9+
)

bazel/tools/cc_stamp_header.bzl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def _cc_stamp_header(ctx):
2+
out = ctx.outputs.out
3+
args = ctx.actions.args()
4+
5+
args.add("--stable_status", ctx.info_file)
6+
args.add("--volatile_status", ctx.version_file)
7+
args.add("--output_header", out)
8+
9+
ctx.actions.run(
10+
outputs = [out],
11+
inputs = [ctx.info_file, ctx.version_file],
12+
arguments = [args],
13+
executable = ctx.executable.tool,
14+
)
15+
16+
return DefaultInfo(files = depset([out]))
17+
18+
cc_stamp_header = rule(
19+
implementation = _cc_stamp_header,
20+
attrs = {
21+
"out": attr.output(
22+
doc = "C++ header file to generate",
23+
mandatory = True,
24+
),
25+
"tool": attr.label(
26+
default = Label("@ecsact_cli//bazel/tools:cc_stamp_header_generator"),
27+
cfg = "exec",
28+
executable = True,
29+
),
30+
},
31+
)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <string>
2+
#include <fstream>
3+
#include <iostream>
4+
#include <stdexcept>
5+
6+
std::string escape_cpp_value(std::string value) {
7+
if(value == "true" || value == "false") {
8+
return value;
9+
}
10+
11+
return "\"" + value + "\"";
12+
}
13+
14+
void write_cc_constexpr_lines(
15+
std::ifstream& status_file_stream,
16+
std::ofstream& output_stream
17+
) {
18+
std::string line;
19+
std::string::size_type ws_idx;
20+
21+
// Reading file based on:
22+
// https://docs.bazel.build/versions/master/user-manual.html#workspace_status
23+
while(status_file_stream.good()) {
24+
line.clear();
25+
std::getline(status_file_stream, line);
26+
if(line.empty()) {
27+
continue;
28+
}
29+
30+
ws_idx = line.find_first_of(' ');
31+
32+
output_stream << "constexpr auto " << line.substr(0, ws_idx) // key
33+
<< " = " << escape_cpp_value(line.substr(ws_idx + 1))
34+
<< ";\n"; // value
35+
}
36+
}
37+
38+
int main(int argc, char* argv[]) {
39+
std::string stable_status_path;
40+
std::string volatile_status_path;
41+
std::string output_header_path;
42+
43+
for(int i = 1; argc > i; ++i) {
44+
std::string arg(argv[i]);
45+
46+
if(arg == "--stable_status") {
47+
stable_status_path = std::string(argv[++i]);
48+
} else if(arg == "--volatile_status") {
49+
volatile_status_path = std::string(argv[++i]);
50+
} else if(arg == "--output_header") {
51+
output_header_path = std::string(argv[++i]);
52+
} else {
53+
std::cerr << "invalid argument: " << arg << std::endl;
54+
return 1;
55+
}
56+
}
57+
58+
if(stable_status_path.empty()) {
59+
std::cerr << "missing --stable_status" << std::endl;
60+
return 1;
61+
}
62+
63+
if(volatile_status_path.empty()) {
64+
std::cerr << "missing --volatile_status" << std::endl;
65+
return 1;
66+
}
67+
68+
if(output_header_path.empty()) {
69+
std::cerr << "missing --output_header" << std::endl;
70+
return 1;
71+
}
72+
73+
try {
74+
std::ifstream stable_status_stream(stable_status_path);
75+
std::ifstream volatile_status_stream(volatile_status_path);
76+
std::ofstream output_header_stream(output_header_path);
77+
78+
output_header_stream << "#pragma once\n\n"
79+
<< "// THIS FILE IS GENERATED - DO NOT EDIT\n\n";
80+
81+
write_cc_constexpr_lines(stable_status_stream, output_header_stream);
82+
write_cc_constexpr_lines(volatile_status_stream, output_header_stream);
83+
} catch(const std::exception& err) {
84+
std::cerr << "EXCEPTION THROWN: err.what() == " << err.what() << std::endl;
85+
return 2;
86+
}
87+
88+
return 0;
89+
}

0 commit comments

Comments
 (0)