aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorGravatar David Chen <dzc@google.com>2015-09-07 21:49:56 +0000
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2015-09-08 09:08:50 +0000
commit87419786a7f80c72f5c89c76d33e807d6166a07b (patch)
treedce310e876fc54eef557dee02697bbe9a8b00f9a /examples
parent56c7d552545bde9d471154ad9844db9801907a33 (diff)
Add initial D rules to Bazel.
-- MOS_MIGRATED_REVID=102513092
Diffstat (limited to 'examples')
-rw-r--r--examples/d/hello_lib/BUILD40
-rw-r--r--examples/d/hello_lib/greeter.d51
-rw-r--r--examples/d/hello_lib/greeter_test.d22
-rw-r--r--examples/d/hello_lib/native-greeter.c49
-rw-r--r--examples/d/hello_lib/native-greeter.h28
-rw-r--r--examples/d/hello_lib/native_greeter.d43
-rw-r--r--examples/d/hello_world/BUILD12
-rw-r--r--examples/d/hello_world/hello_world.d26
8 files changed, 271 insertions, 0 deletions
diff --git a/examples/d/hello_lib/BUILD b/examples/d/hello_lib/BUILD
new file mode 100644
index 0000000000..7ed1624a41
--- /dev/null
+++ b/examples/d/hello_lib/BUILD
@@ -0,0 +1,40 @@
+package(default_visibility = ["//visibility:public"])
+
+load(
+ "/tools/build_defs/d/d",
+ "d_docs",
+ "d_library",
+ "d_source_library",
+ "d_test",
+)
+
+d_library(
+ name = "greeter",
+ srcs = ["greeter.d"],
+)
+
+d_test(
+ name = "greeter_test",
+ srcs = ["greeter_test.d"],
+ deps = [":greeter"],
+)
+
+cc_library(
+ name = "native_greeter_lib",
+ srcs = ["native-greeter.c"],
+ hdrs = ["native-greeter.h"],
+)
+
+d_source_library(
+ name = "native_greeter",
+ srcs = ["native_greeter.d"],
+ deps = [":native_greeter_lib"],
+)
+
+d_docs(
+ name = "greeter_docs",
+ srcs = glob(
+ ["*.d"],
+ exclude = ["*_test.d"],
+ ),
+)
diff --git a/examples/d/hello_lib/greeter.d b/examples/d/hello_lib/greeter.d
new file mode 100644
index 0000000000..ec916ffbe5
--- /dev/null
+++ b/examples/d/hello_lib/greeter.d
@@ -0,0 +1,51 @@
+// Copyright 2015 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+module greeter;
+
+import std.stdio;
+import std.string;
+
+/// Displays a greeting.
+class Greeter {
+ private string greeting;
+
+ public:
+ /// Creates a new greeter.
+ ///
+ /// Params:
+ /// greeting = The greeting to use.
+ this(in string greeting) {
+ this.greeting = greeting.dup;
+ }
+
+ /// Returns the greeting as a string.
+ ///
+ /// Params:
+ /// thing = The thing to greet
+ ///
+ /// Returns:
+ /// A greeting as a string.
+ string makeGreeting(in immutable string thing) {
+ return format("%s %s!", this.greeting, thing);
+ }
+
+ /// Prints a greeting.
+ ///
+ /// Params:
+ /// thing = The thing to greet.
+ void greet(in immutable string thing) {
+ writeln(makeGreeting(thing));
+ }
+}
diff --git a/examples/d/hello_lib/greeter_test.d b/examples/d/hello_lib/greeter_test.d
new file mode 100644
index 0000000000..6ebd7f9cb1
--- /dev/null
+++ b/examples/d/hello_lib/greeter_test.d
@@ -0,0 +1,22 @@
+// Copyright 2015 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import examples.d.hello_lib.greeter;
+
+unittest {
+ auto greeter = new Greeter("Hello");
+ assert(greeter.makeGreeting("world") == "Hello world!");
+}
+
+void main() {}
diff --git a/examples/d/hello_lib/native-greeter.c b/examples/d/hello_lib/native-greeter.c
new file mode 100644
index 0000000000..cdbb388e32
--- /dev/null
+++ b/examples/d/hello_lib/native-greeter.c
@@ -0,0 +1,49 @@
+// Copyright 2015 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "examples/d/hello_lib/native-greeter.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+NativeGreeter* native_greeter_new(const char* greeting) {
+ if (greeting == NULL) {
+ return NULL;
+ }
+ NativeGreeter* greeter = NULL;
+ greeter = (NativeGreeter*)malloc(sizeof(*greeter));
+ if (greeter == NULL) {
+ return NULL;
+ }
+ greeter->greeting = strdup(greeting);
+ return greeter;
+}
+
+void native_greeter_greet(const NativeGreeter* greeter, const char* thing) {
+ if (greeter == NULL || thing == NULL) {
+ return;
+ }
+ printf("%s %s!\n", greeter->greeting, thing);
+}
+
+void native_greeter_free(NativeGreeter* greeter) {
+ if (greeter == NULL) {
+ return;
+ }
+ if (greeter->greeting != NULL) {
+ free(greeter->greeting);
+ }
+ free(greeter);
+}
diff --git a/examples/d/hello_lib/native-greeter.h b/examples/d/hello_lib/native-greeter.h
new file mode 100644
index 0000000000..d01d8cae8b
--- /dev/null
+++ b/examples/d/hello_lib/native-greeter.h
@@ -0,0 +1,28 @@
+// Copyright 2015 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef EXAMPLES_D_HELLO_LIB_NATIVE_GREETER_H_
+#define EXAMPLES_D_HELLO_LIB_NATIVE_GREETER_H_
+
+typedef struct NativeGreeter {
+ char* greeting;
+} NativeGreeter;
+
+NativeGreeter* native_greeter_new(const char* greeting);
+
+void native_greeter_greet(const NativeGreeter* greeter, const char* thing);
+
+void native_greeter_free(NativeGreeter* greeter);
+
+#endif // EXAMPLES_D_HELLO_LIB_NATIVE_GREETER_H_
diff --git a/examples/d/hello_lib/native_greeter.d b/examples/d/hello_lib/native_greeter.d
new file mode 100644
index 0000000000..417492aaf6
--- /dev/null
+++ b/examples/d/hello_lib/native_greeter.d
@@ -0,0 +1,43 @@
+// Copyright 2015 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+module native_greeter;
+
+extern (C):
+
+struct NativeGreeter {
+ char* greeting;
+};
+
+/// Creates a new NativeGreeter.
+///
+/// Params:
+/// greeting = The greeting to use.
+///
+/// Returns:
+/// A pointer to a new NativeGreeting struct.
+NativeGreeter* native_greeter_new(const(char)* greeting);
+
+/// Prints a greeting to stdout.
+///
+/// Params:
+/// greeter = The pointer to the NativeGreeter object to use.
+/// thing = The thing to greet.
+void native_greeter_greet(const(NativeGreeter)* greeter, const(char)* thing);
+
+/// Frees the NativeGreeter.
+///
+/// Params:
+/// greeter = The pointer to the NativeGreeter object to use.
+void native_greeter_free(NativeGreeter* greeter);
diff --git a/examples/d/hello_world/BUILD b/examples/d/hello_world/BUILD
new file mode 100644
index 0000000000..47c7342060
--- /dev/null
+++ b/examples/d/hello_world/BUILD
@@ -0,0 +1,12 @@
+package(default_visibility = ["//visibility:public"])
+
+load("/tools/build_defs/d/d", "d_binary")
+
+d_binary(
+ name = "hello_world",
+ srcs = ["hello_world.d"],
+ deps = [
+ "//examples/d/hello_lib:greeter",
+ "//examples/d/hello_lib:native_greeter",
+ ],
+)
diff --git a/examples/d/hello_world/hello_world.d b/examples/d/hello_world/hello_world.d
new file mode 100644
index 0000000000..3c60f4a24e
--- /dev/null
+++ b/examples/d/hello_world/hello_world.d
@@ -0,0 +1,26 @@
+// Copyright 2015 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import std.stdio;
+import examples.d.hello_lib.greeter;
+import examples.d.hello_lib.native_greeter;
+
+void main() {
+ Greeter greeter = new Greeter("Hello");
+ greeter.greet("World");
+
+ NativeGreeter* nativeGreeter = native_greeter_new("Hello");
+ native_greeter_greet(nativeGreeter, "World");
+ native_greeter_free(nativeGreeter);
+}