package com.elphel.imagej.gpu.jna;

import com.sun.jna.Native;
import com.sun.jna.Pointer;

/**
 * Stage-0b proof: from Java, load libtileproc.so via JNA, call tp_create_module, assert all kernels.
 * Same result as the C++ probe but across the JNA boundary on the RTX 5060 Ti. By Claude on 2026-06-25.
 *   java -Djna.library.path=<dir with libtileproc.so> -cp target/classes:<jna.jar> \
 *        com.elphel.imagej.gpu.jna.Stage0 [kernel_src_dir] [libcudadevrt.a]
 */
public class Stage0 {
	private static final int EXPECTED_KERNELS = 40; // 36 (through rung C1) + 2 DP-cycles (D1: pose_lma_dp_cycles/commit) + measure-chain DP (D2: pose_measure_dp + launch helpers) + scene DP parent family (D3: pose_scene_dp lma/step/commit) - the native D1-D3 sessions could not touch this Java constant, synced at D4 // By Claude on 07/17/2026
    public static void main(String[] args) {
        String srcdir = (args.length > 0) ? args[0] : "/home/elphel/git/tile_processor_gpu/src";
        String devrt  = (args.length > 1) ? args[1] : "/usr/local/cuda/targets/x86_64-linux/lib/libcudadevrt.a";
        System.out.println("Stage0 (JNA): loading libtileproc, srcdir=" + srcdir);
        TpJna lib = Native.load("tileproc", TpJna.class);
        Pointer m = lib.tp_create_module(srcdir, devrt);
        if (m == null) {
            System.out.println("FAIL: tp_create_module returned null:\n" + lib.tp_last_error());
            System.exit(1);
        }
        int n = lib.tp_module_num_functions(m);
        String err = lib.tp_last_error();
        System.out.println("Stage0 (JNA): module created, functions loaded = " + n + (err != null && !err.isEmpty() ? ("  [warn: " + err + "]") : ""));
        lib.tp_destroy_module(m);
        System.out.println(n == EXPECTED_KERNELS ?
				"RESULT: PASS ("+EXPECTED_KERNELS+"/"+EXPECTED_KERNELS+" via JNA)" :
				("RESULT: FAIL (" + n + "/"+EXPECTED_KERNELS+")"));
        System.exit(n == EXPECTED_KERNELS ? 0 : 2);
    }
}
