summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/hello/TARGETS9
-rw-r--r--src/hello/hello.cpp11
-rw-r--r--src/proto/TARGETS18
-rw-r--r--src/proto/example.proto7
-rw-r--r--src/proto/read.cc18
-rw-r--r--src/proto/write.cc17
-rw-r--r--src/shell/TARGETS13
-rw-r--r--src/withExtendedPath/TARGETS6
-rw-r--r--src/withExtendedPath/extend-path.cc37
9 files changed, 136 insertions, 0 deletions
diff --git a/src/hello/TARGETS b/src/hello/TARGETS
new file mode 100644
index 0000000..80380c7
--- /dev/null
+++ b/src/hello/TARGETS
@@ -0,0 +1,9 @@
+{ "":
+ { "type": ["@", "rules", "CC", "binary"]
+ , "name": ["hello"]
+ , "srcs": ["hello.cpp"]
+ , "private-deps": ["fmt"]
+ }
+, "fmt":
+ {"type": ["@", "rules", "CC/pkgconfig", "system_library"], "name": ["fmt"]}
+}
diff --git a/src/hello/hello.cpp b/src/hello/hello.cpp
new file mode 100644
index 0000000..0c9df99
--- /dev/null
+++ b/src/hello/hello.cpp
@@ -0,0 +1,11 @@
+#include <iostream>
+
+#include <fmt/format.h>
+#include <string>
+
+int main(int argc, char **argv) {
+ std::cout << fmt::format("Hello {}!",
+ argc > 1 ? std::string{argv[1]} : "World")
+ << std::endl;
+ return 0;
+}
diff --git a/src/proto/TARGETS b/src/proto/TARGETS
new file mode 100644
index 0000000..512d7c2
--- /dev/null
+++ b/src/proto/TARGETS
@@ -0,0 +1,18 @@
+{ "example":
+ { "type": ["@", "rules", "proto", "library"]
+ , "name": ["example"]
+ , "srcs": ["example.proto"]
+ }
+, "write":
+ { "type": ["@", "rules", "CC", "binary"]
+ , "name": ["write"]
+ , "srcs": ["write.cc"]
+ , "private-proto": ["example"]
+ }
+, "read":
+ { "type": ["@", "rules", "CC", "binary"]
+ , "name": ["read"]
+ , "srcs": ["read.cc"]
+ , "private-proto": ["example"]
+ }
+}
diff --git a/src/proto/example.proto b/src/proto/example.proto
new file mode 100644
index 0000000..69b5953
--- /dev/null
+++ b/src/proto/example.proto
@@ -0,0 +1,7 @@
+syntax = "proto3";
+package sample;
+
+message Example {
+ string foo = 1;
+ int32 bar = 2;
+}
diff --git a/src/proto/read.cc b/src/proto/read.cc
new file mode 100644
index 0000000..f97a39a
--- /dev/null
+++ b/src/proto/read.cc
@@ -0,0 +1,18 @@
+#include <fstream>
+#include <iostream>
+
+#include "example.pb.h"
+
+int main(int argc, char **argv) {
+ sample::Example example;
+
+ {
+ std::fstream input(argv[1], std::ios::in | std::ios::binary);
+ example.ParseFromIstream(&input);
+ }
+
+ std::cout << "foo=" << example.foo() << "\n";
+ std::cout << "bar=" << example.bar() << std::endl;
+
+ return 0;
+}
diff --git a/src/proto/write.cc b/src/proto/write.cc
new file mode 100644
index 0000000..2e206e5
--- /dev/null
+++ b/src/proto/write.cc
@@ -0,0 +1,17 @@
+#include <fstream>
+#include <string>
+
+#include "example.pb.h"
+
+int main(int argc, char **argv) {
+ sample::Example example;
+ example.set_foo(std::string{argv[1]});
+ example.set_bar(atoi(argv[2]));
+ {
+ std::fstream output(argv[3],
+ std::ios::out | std::ios::trunc | std::ios::binary);
+ example.SerializeToOstream(&output);
+ }
+
+ return 0;
+}
diff --git a/src/shell/TARGETS b/src/shell/TARGETS
new file mode 100644
index 0000000..f4b2dbf
--- /dev/null
+++ b/src/shell/TARGETS
@@ -0,0 +1,13 @@
+{ "out":
+ { "type": ["@", "rules", "shell", "cmds"]
+ , "outs": ["out.txt"]
+ , "cmds": ["./hello > out.txt"]
+ , "deps": [["hello", ""]]
+ }
+, "":
+ { "type": ["@", "rules", "shell", "cmds"]
+ , "outs": ["wc.txt", "tr.txt"]
+ , "cmds": ["wc out.txt > wc.txt", "cat out.txt | tr a-z A-Z > tr.txt"]
+ , "deps": ["out"]
+ }
+}
diff --git a/src/withExtendedPath/TARGETS b/src/withExtendedPath/TARGETS
new file mode 100644
index 0000000..e226541
--- /dev/null
+++ b/src/withExtendedPath/TARGETS
@@ -0,0 +1,6 @@
+{ "":
+ { "type": ["@", "rules", "CC", "binary"]
+ , "name": ["withExtendedPath"]
+ , "srcs": ["extend-path.cc"]
+ }
+}
diff --git a/src/withExtendedPath/extend-path.cc b/src/withExtendedPath/extend-path.cc
new file mode 100644
index 0000000..a466728
--- /dev/null
+++ b/src/withExtendedPath/extend-path.cc
@@ -0,0 +1,37 @@
+#include <algorithm>
+#include <cstdlib>
+#include <string>
+#include <unistd.h>
+#include <vector>
+
+int main(int argc, char* argv[]) {
+ constexpr int kForkFailed = 65;
+ constexpr int kPrerequisiteError = 97;
+ auto kEnv = std::string{"/usr/bin/env"};
+
+ if (argc < 3) {
+ return kPrerequisiteError;
+ }
+
+ // compute PATH
+ auto path = std::string{argv[1]};
+ auto const* env_path = std::getenv("PATH");
+ if (env_path && (*env_path != '\0')) {
+ path = std::string{env_path} + std::string{":"} + path;
+ }
+ auto path_arg = std::string{"PATH="} + path;
+
+ // create new argument vector
+ std::vector<char*> nargv{};
+ nargv.reserve(argc+3);
+ nargv.push_back(kEnv.data());
+ nargv.push_back(path_arg.data());
+ for (int i=2; i < argc; i++) {
+ nargv.push_back(argv[i]);
+ }
+ nargv.push_back(nullptr);
+
+ // exec
+ (void) execvp(nargv[0], static_cast<char* const*>(nargv.data()));
+ return kForkFailed;
+}