You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by ki...@apache.org on 2025/09/09 13:48:55 UTC

(opendal) 01/01: fix(bindings/d): Enhance D bindings build script and tests for OpenDAL

This is an automated email from the ASF dual-hosted git repository.

kingsword09 pushed a commit to branch fix-d-lang-test
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit a66887e4bbe5bc8c12f49787d46bc93c717a135b
Author: Kingsword <ki...@gmail.com>
AuthorDate: Tue Sep 9 21:48:41 2025 +0800

    fix(bindings/d): Enhance D bindings build script and tests for OpenDAL
---
 bindings/d/README.md                |   2 +
 bindings/d/build.d                  | 237 ++++++++++++++++++++++++++++++++----
 bindings/d/source/opendal/package.d |  92 +++++++++++---
 3 files changed, 293 insertions(+), 38 deletions(-)

diff --git a/bindings/d/README.md b/bindings/d/README.md
index ead891476..9eca623b2 100644
--- a/bindings/d/README.md
+++ b/bindings/d/README.md
@@ -11,6 +11,8 @@ To compile OpenDAL d binding from source code, you need:
 - [dmd/ldc/gdc](https://dlang.org/download)
 
 ```bash
+# Test a specific backend
+export OPENDAL_TEST=memory
 # build libopendal_c (underneath call make -C ../c)
 dub build -b release
 # build and run unit tests
diff --git a/bindings/d/build.d b/bindings/d/build.d
index 9b5387a80..a259dc229 100644
--- a/bindings/d/build.d
+++ b/bindings/d/build.d
@@ -1,42 +1,231 @@
+#!/usr/bin/env dub
+/+ dub.sdl:
+    name "build"
+    dependency "std" version="*"
++/
+
+/**
+ * OpenDAL D Bindings Build Script
+ *
+ * This script builds the OpenDAL C library with specified features
+ * and copies the necessary files for D bindings.
+ *
+ * This script is automatically executed during:
+ *   dub build
+ *   dub test
+ *   dub run
+ *
+ * For manual execution with custom options:
+ *   OPENDAL_TEST=memory dmd -run build.d
+ *   OPENDAL_TEST=fs OPENDAL_VERBOSE=1 dmd -run build.d release
+ */
 module build;
-import std.stdio: writeln;
-import std.path: buildPath;
-import std.process: spawnShell, wait;
-import std.exception;
-import std.file: copy, mkdir, exists;
-import std.conv: to;
+
+import std.stdio : writeln, stderr;
+import std.path : buildPath;
+import std.process : spawnShell, wait, environment;
+import std.exception : enforce;
+import std.file : copy, mkdir, exists;
+import std.conv : to;
+import std.string : startsWith, split, strip, toLower;
+import std.array : join;
+
 
 version (Windows)
     enum staticlib = "opendal_c.lib";
 else
     enum staticlib = "libopendal_c.a";
 
-void main (string[] args)
+struct BuildConfig
 {
-    bool isRelease = args.length > 1 && args[1] == "release";
-    string buildType = isRelease ? "release" : "debug";
+    bool isRelease;
+    string[] services;
+    bool verbose;
+    bool help;
+}
 
-    // Run cargo build
-    auto cargoCmd = "cargo build --manifest-path " ~ buildPath(
-        "..", "c", "Cargo.toml") ~ (isRelease ? " --release" : "");
+void printHelp()
+{
+    writeln("OpenDAL D Bindings Build Script");
+    writeln();
+    writeln("This script is automatically executed during:");
+    writeln("  dub build    - Build the D bindings");
+    writeln("  dub test     - Run unit tests");
+    writeln("  dub run      - Build and run examples");
+    writeln();
+    writeln("Environment Variables (for use with dub commands):");
+    writeln("  OPENDAL_TEST         The service to test (e.g., fs, s3, memory, etc.)");
+    writeln("  OPENDAL_{SERVICE}_*  Service-specific configuration (e.g., OPENDAL_FS_ROOT)");
+    writeln("  OPENDAL_VERBOSE      Set to '1' or 'true' for verbose output");
+    writeln();
+    writeln("Examples with environment variables:");
+    writeln("  OPENDAL_TEST=memory dub build");
+    writeln("  OPENDAL_TEST=fs OPENDAL_FS_ROOT=/tmp/test dub test");
+    writeln("  OPENDAL_TEST=memory OPENDAL_VERBOSE=1 dub build -b release");
+    writeln();
+    writeln("For manual execution with command line options:");
+    writeln("  dmd -run build.d [options]");
+    writeln();
+    writeln("Command line options:");
+    writeln("  release              Build in release mode (default: debug)");
+    writeln("  --verbose            Enable verbose output");
+    writeln("  --help               Show this help message");
+    writeln();
+    writeln("Manual execution examples:");
+    writeln("  OPENDAL_TEST=memory dmd -run build.d");
+    writeln("  OPENDAL_TEST=fs OPENDAL_FS_ROOT=/tmp/test dmd -run build.d release");
+    writeln("  OPENDAL_TEST=fs OPENDAL_FS_ROOT=/tmp OPENDAL_VERBOSE=1 dmd -run build.d release");
+}
 
-    auto status = wait(spawnShell(cargoCmd));
-    if (status != 0)
+BuildConfig parseArgs(string[] args)
+{
+    BuildConfig config;
+    config.services = []; // No default services - user must specify
+
+    // Check environment variables first
+    auto envTest = environment.get("OPENDAL_TEST", "");
+    if (envTest.length > 0)
     {
-        throw new Exception("Cargo build failed with status: " ~ status.to!string);
+        config.services = [envTest];
     }
-    else
+
+    auto envVerbose = environment.get("OPENDAL_VERBOSE", "");
+    if (envVerbose == "1" || envVerbose.toLower() == "true")
     {
-        writeln("Cargo build completed successfully");
+        config.verbose = true;
+    }
+
+    foreach (arg; args[1..$])
+    {
+        if (arg == "release")
+        {
+            config.isRelease = true;
+        }
+        else if (arg == "--verbose")
+        {
+            config.verbose = true;
+        }
+        else if (arg == "--help" || arg == "-h")
+        {
+            config.help = true;
+        }
+
+        else
+        {
+            stderr.writeln("Unknown argument: ", arg);
+            stderr.writeln("Use --help for usage information");
+        }
     }
 
-    // Get opendal.h
-    copy(buildPath("..", "c", "include", "opendal.h"), buildPath("source", "opendal", "opendal.h"));
+    return config;
+}
+
+void buildCargoLibrary(BuildConfig config)
+{
+    // Check if services are specified
+    if (config.services.length == 0)
+    {
+        stderr.writeln("Warning: No OpenDAL service specified for testing!");
+        stderr.writeln("Use OPENDAL_TEST environment variable to specify the service to test.");
+        stderr.writeln("Example: OPENDAL_TEST=memory dub build");
+        stderr.writeln("Available services: memory, fs, s3, etc. (see OpenDAL documentation)");
+        stderr.writeln();
+        stderr.writeln("Building with only core functionality (no storage backends)...");
+    }
+
+    // Build features list
+    string[] features = ["opendal/blocking"];
+    foreach (service; config.services)
+    {
+        features ~= "opendal/services-" ~ service.strip();
+    }
+
+    // Construct cargo command
+    auto cargoCmd = "cargo build --manifest-path " ~ buildPath("..", "c", "Cargo.toml");
+
+    if (config.isRelease)
+        cargoCmd ~= " --release";
+
+    cargoCmd ~= " --features \"" ~ features.join(",") ~ "\"";
+
+    if (config.verbose)
+        cargoCmd ~= " --verbose";
+
+    writeln("Building OpenDAL C library...");
+    writeln("Features: ", features.join(", "));
+    if (config.verbose)
+        writeln("Command: ", cargoCmd);
 
-    // Get libopendal_c.a
-    auto libPath = buildPath("..", "c", "target", buildType, staticlib);
-    writeln("Copying ", libPath, " to ", buildPath("lib", staticlib));
+    auto status = wait(spawnShell(cargoCmd));
+    enforce(status == 0, "Cargo build failed with status: " ~ status.to!string);
+
+    writeln("✓ Cargo build completed successfully");
+}
+
+void copyArtifacts(BuildConfig config)
+{
+    string buildType = config.isRelease ? "release" : "debug";
+
+    writeln("Copying build artifacts...");
+
+    // Copy header file
+    auto headerSrc = buildPath("..", "c", "include", "opendal.h");
+    auto headerDst = buildPath("source", "opendal", "opendal.h");
+
+    if (config.verbose)
+        writeln("Copying header: ", headerSrc, " -> ", headerDst);
+
+    copy(headerSrc, headerDst);
+
+    // Ensure lib directory exists
     if (!exists("lib"))
-        mkdir ("lib");
-    copy(libPath, buildPath("lib", staticlib));
+    {
+        if (config.verbose)
+            writeln("Creating lib directory");
+        mkdir("lib");
+    }
+
+    // Copy static library
+    auto libSrc = buildPath("..", "c", "target", buildType, staticlib);
+    auto libDst = buildPath("lib", staticlib);
+
+    if (config.verbose)
+        writeln("Copying library: ", libSrc, " -> ", libDst);
+
+    copy(libSrc, libDst);
+
+    writeln("✓ Build artifacts copied successfully");
+}
+
+void main(string[] args)
+{
+    try
+    {
+        auto config = parseArgs(args);
+
+        if (config.help)
+        {
+            printHelp();
+            return;
+        }
+
+        writeln("OpenDAL D Bindings Build");
+        writeln("========================");
+        writeln("Build mode: ", config.isRelease ? "release" : "debug");
+        writeln("Services: ", config.services.join(", "));
+        writeln();
+
+        buildCargoLibrary(config);
+        copyArtifacts(config);
+
+        writeln();
+        writeln("✓ Build completed successfully!");
+        writeln("Ready for: dub build, dub test, or dub run");
+    }
+    catch (Exception e)
+    {
+        stderr.writeln("❌ Build failed: ", e.msg);
+        import core.stdc.stdlib : exit;
+        exit(1);
+    }
 }
diff --git a/bindings/d/source/opendal/package.d b/bindings/d/source/opendal/package.d
index 523b1ed8c..f85c17b79 100644
--- a/bindings/d/source/opendal/package.d
+++ b/bindings/d/source/opendal/package.d
@@ -26,20 +26,48 @@ version (unittest)
 	@("Test basic Operator creation")
 	@safe unittest
 	{
-		/* Initialize a operator for "memory" backend, with no options */
-		OperatorOptions options = new OperatorOptions();
-		Operator op = Operator("memory", options);
+		import std.process: environment;
+
+		// Get test service from environment variable
+		auto testService = environment.get("OPENDAL_TEST", "memory");
+
+		/* Initialize operator with automatic configuration */
+		auto options = new OperatorOptions();
+
+		// Auto-configure based on environment variables with prefix OPENDAL_{SERVICE}_
+		import std.string: toUpper, startsWith, toLower;
+		auto prefix = "OPENDAL_" ~ testService.toUpper() ~ "_";
+		foreach (key, value; environment.toAA())
+		{
+			if (key.startsWith(prefix))
+			{
+				auto configKey = key[prefix.length .. $].toLower();
+				options.set(configKey, value);
+			}
+		}
+
+		Operator op = Operator(testService, options);
 
 		/* Prepare some data to be written */
 		string data = "this_string_length_is_24";
 
-		/* Write this into path "/testpath" */
-		op.write("/testpath", cast(ubyte[])data.dup);
+		/* Write this into path - use UUID-like path for uniqueness */
+		import std.uuid: randomUUID;
+		string testPath = randomUUID().toString();
+
+		/* Write this into path */
+		op.write(testPath, cast(ubyte[])data.dup);
 
 		/* We can read it out, make sure the data is the same */
-		auto read_bytes = op.read("/testpath");
+		auto read_bytes = op.read(testPath);
 		assert(read_bytes.length == 24);
 		assert(cast(string)read_bytes.idup == data);
+
+		/* Clean up */
+		if (op.exists(testPath))
+		{
+			op.remove(testPath);
+		}
 	}
 
 	@("Benchmark parallel and normal functions")
@@ -50,12 +78,31 @@ version (unittest)
 		import std.path: buildPath;
 		import std.datetime.stopwatch: StopWatch;
 		import std.stdio: writeln;
+		import std.process: environment;
+
+		// Get test service from environment variable
+		auto testService = environment.get("OPENDAL_TEST", "memory");
 
+		/* Initialize operator with automatic configuration */
 		auto options = new OperatorOptions();
-		options.set("root", tempDir);
-		auto op = Operator("fs", options, true);
 
-		auto testPath = buildPath(tempDir, "benchmark_test.txt");
+		// Auto-configure based on environment variables with prefix OPENDAL_{SERVICE}_
+		import std.string: toUpper, startsWith, toLower;
+		auto prefix = "OPENDAL_" ~ testService.toUpper() ~ "_";
+		foreach (key, value; environment.toAA())
+		{
+			if (key.startsWith(prefix))
+			{
+				auto configKey = key[prefix.length .. $].toLower();
+				options.set(configKey, value);
+			}
+		}
+
+		auto op = Operator(testService, options, true);
+
+		/* Use UUID for unique test path */
+		import std.uuid: randomUUID;
+		string testPath = randomUUID().toString();
 		auto testData = cast(ubyte[])"Benchmarking OpenDAL async and normal functions".dup;
 
 		// Benchmark write operations
@@ -85,19 +132,33 @@ version (unittest)
 		sw.stop();
 		auto parallelReadTime = sw.peek();
 
-		// Benchmark list operations
+		// Benchmark list operations - create a test directory first
+		import std.uuid: randomUUID;
+		string dirPath = randomUUID().toString() ~ "/";
+
+		// Create directory and a file in it for meaningful list operation
+		op.createDir(dirPath);
+		string fileInDir = dirPath ~ randomUUID().toString();
+		op.write(fileInDir, cast(ubyte[])"test file in directory".dup);
+
 		sw.reset();
 		sw.start();
-		op.list(tempDir);
+		op.list(dirPath);
 		sw.stop();
 		auto normalListTime = sw.peek();
 
 		sw.reset();
 		sw.start();
-		op.listParallel(tempDir);
+		op.listParallel(dirPath);
 		sw.stop();
 		auto parallelListTime = sw.peek();
 
+		// Clean up the directory and file
+		if (op.exists(fileInDir))
+		{
+			op.remove(fileInDir);
+		}
+
 		// Print benchmark results
 		writeln("Write benchmark:");
 		writeln("  Normal:   ", normalWriteTime);
@@ -116,8 +177,11 @@ version (unittest)
 		assert(parallelReadData == testData);
 
 		// Clean up
-		op.remove(testPath);
-		assert(!op.exists(testPath));
+		if (op.exists(testPath))
+		{
+			op.remove(testPath);
+			assert(!op.exists(testPath));
+		}
 	}
 
 }