@@ -42,18 +42,24 @@ class VesperaBridgePlugin : Plugin<Project> {
4242 .create(" vespera" , VesperaBridgeExtension ::class .java)
4343 ext.autoBuildCargo.convention(false )
4444 ext.cargoSourceRoots.convention(listOf (" src" , " crates" , " examples" ))
45+ ext.cargoProfile.convention(" release" )
4546
4647 // Compute platform-derived values eagerly (host machine info).
4748 val os = detectOs()
4849 val arch = detectArch()
4950 val generatedResourcesDir = project.layout.buildDirectory.dir(" generated/vesperaNativeResources" )
5051 val targetSubdir = " native/$os -$arch "
5152
52- // Lazy file references — evaluated at task execution.
53+ // Lazy file references — evaluated at task execution. The cdylib
54+ // lives under `<targetDir|cargoRoot/target>/<profileDir>/`, so a
55+ // debug / custom-profile build or a redirected CARGO_TARGET_DIR is
56+ // located correctly instead of being hardcoded to `target/release/`.
5357 val cdylibFile = project.provider {
54- val root = ext.cargoRoot.get().asFile
5558 val name = ext.crateName.get()
56- File (root, " target/release/" + mapLibraryName(os, name))
59+ val targetBase =
60+ if (ext.targetDir.isPresent) ext.targetDir.get().asFile
61+ else File (ext.cargoRoot.get().asFile, " target" )
62+ File (targetBase, profileDir(ext.cargoProfile.get()) + " /" + mapLibraryName(os, name))
5763 }
5864
5965 val cargoBuildTask = project.tasks.register(
@@ -64,7 +70,24 @@ class VesperaBridgePlugin : Plugin<Project> {
6470 t.group = " vespera"
6571 t.description = " Build the Rust cdylib via `cargo build --release`."
6672 t.workingDir = ext.cargoRoot.get().asFile
67- t.commandLine(" cargo" , " build" , " -p" , ext.crateName.get(), " --release" )
73+ // Profile-aware command: `release` → `--release`, `dev`/
74+ // `debug` → default build, any other → `--profile <p>`.
75+ val profile = ext.cargoProfile.get()
76+ val cmd = mutableListOf (" cargo" , " build" , " -p" , ext.crateName.get())
77+ when (profile) {
78+ " release" -> cmd.add(" --release" )
79+ " dev" , " debug" -> {} // default profile → target/debug
80+ else -> { cmd.add(" --profile" ); cmd.add(profile) }
81+ }
82+ t.commandLine(cmd)
83+ // Honour a redirected target dir so cargo writes where
84+ // `bundleNativeLib` later looks for the cdylib.
85+ if (ext.targetDir.isPresent) {
86+ t.environment(
87+ " CARGO_TARGET_DIR" ,
88+ ext.targetDir.get().asFile.absolutePath,
89+ )
90+ }
6891 // Up-to-date check: re-run on workspace manifests, Cargo.lock,
6992 // and Rust sources in configured roots. This repository keeps
7093 // Rust code under crates/* and examples/*, not only src/.
@@ -97,8 +120,12 @@ class VesperaBridgePlugin : Plugin<Project> {
97120 val src = cdylibFile.get()
98121 require(src.exists()) {
99122 " Native library not found: $src \n " +
100- " Run: cargo build -p ${ext.crateName.get()} --release " +
101- " (or set vespera.autoBuildCargo = true)"
123+ " Build the '${ext.crateName.get()} ' cdylib for the " +
124+ " '${ext.cargoProfile.get()} ' profile (or set " +
125+ " vespera.autoBuildCargo = true). If the workspace " +
126+ " redirects Cargo output (CARGO_TARGET_DIR / " +
127+ " .cargo/config.toml build.target-dir), set " +
128+ " vespera.targetDir to that directory."
102129 }
103130 }
104131 })
@@ -167,4 +194,14 @@ class VesperaBridgePlugin : Plugin<Project> {
167194 " macos" -> " lib$name .dylib"
168195 else -> " lib$name .so"
169196 }
197+
198+ /* *
199+ * Map a Cargo profile name to its `target/` output subdirectory.
200+ * Cargo's built-in `dev` profile emits to `debug`; every other profile
201+ * (`release`, or a custom `[profile.X]`) uses its own name verbatim.
202+ */
203+ private fun profileDir (profile : String ): String = when (profile) {
204+ " dev" , " debug" -> " debug"
205+ else -> profile
206+ }
170207}
0 commit comments