Skip to content

Commit b41ad57

Browse files
committed
Checkpoint getting runfiles
1 parent 73df7f4 commit b41ad57

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

devserver/defs.bzl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
def devserver(name, port):
1+
def devserver(name, port, static_file, data = []):
2+
normalized_static_file = Label(native.repository_name() + "//" + native.package_name()).relative(static_file)
23
native.cc_binary(
34
name = name,
45
srcs = [
56
Label("@rules_devserver//devserver:main"),
67
Label("@rules_devserver//devserver:httplib")
78
],
8-
args = ["--port=%d" % port],
9+
args = [
10+
"--port=%d" % port,
11+
"--static_file=%s" % normalized_static_file
12+
],
13+
data = data,
14+
deps = ["@bazel_tools//tools/cpp/runfiles"],
915
linkopts = ["-lpthread"],
1016
)

devserver/main.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
#include "devserver/httplib/httplib.h"
2+
#include "tools/cpp/runfiles/runfiles.h"
3+
4+
using bazel::tools::cpp::runfiles::Runfiles;
25

36
int main(int argc, char **argv) {
47
httplib::Server svr;
58
std::string port = "8080";
69

10+
std::string runfiles_error;
11+
std::unique_ptr<Runfiles> runfiles(
12+
Runfiles::Create(argv[0], BAZEL_CURRENT_REPOSITORY, &runfiles_error));
13+
14+
std::string path = runfiles->Rlocation("devserver/");
15+
std::cout << "path: " << path << std::endl;
16+
17+
// print all args
18+
for (int i = 0; i < argc; i++) {
19+
std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
20+
}
21+
722
for (int i = 0; i < argc; i++) {
823
if (std::string(argv[i]).rfind("--port", 0) != std::string::npos) {
924
port = std::string(argv[i]).substr(7);

examples/BUILD

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/single_static_file/BUILD

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("//devserver:defs.bzl", "devserver")
2+
3+
filegroup(
4+
name = "index",
5+
srcs = ["index.html"],
6+
visibility = ["//visibility:public"],
7+
)
8+
9+
devserver(
10+
name = "serve",
11+
port = 8081,
12+
static_file = ":index",
13+
data = [":index"],
14+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
3+
<head>
4+
<title>Single Static File</title>
5+
</head>
6+
7+
<body>
8+
<h1>Single Static File</h1>
9+
<p>This is a single static file.</p>
10+
</body>
11+
12+
</html>

0 commit comments

Comments
 (0)