import paramiko
import sys

# Connect to BC250
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect('192.168.178.150', username='fabian', key_filename=r'C:\Users\fabia\.ssh\id_ed25519')
sftp = c.open_sftp()

# Upload hip_probe.cpp
with open(r'c:\Users\fabia\Desktop\VibeROCm\hip_probe.cpp', 'r') as f:
    probe_src = f.read()
with sftp.open('/tmp/hip_probe.cpp', 'w') as f:
    f.write(probe_src)
print("Uploaded hip_probe.cpp")

# Upload hip_minimal_test.cpp
with open(r'c:\Users\fabia\Desktop\VibeROCm\hip_minimal_test.cpp', 'r') as f:
    minimal_src = f.read()
with sftp.open('/tmp/hip_minimal_test.cpp', 'w') as f:
    f.write(minimal_src)
print("Uploaded hip_minimal_test.cpp")

# Upload hip_vector_add.cpp
with open(r'c:\Users\fabia\Desktop\VibeROCm\hip_vector_add.cpp', 'r') as f:
    vector_src = f.read()
with sftp.open('/tmp/hip_vector_add.cpp', 'w') as f:
    f.write(vector_src)
print("Uploaded hip_vector_add.cpp")

sftp.close()

# Compile all three
def run_cmd(client, cmd):
    stdin, stdout, stderr = client.exec_command(cmd, timeout=120)
    out = stdout.read().decode()
    err = stderr.read().decode()
    rc = stdout.channel.recv_exit_status()
    return out, err, rc

env = "HSA_OVERRIDE_GFX_VERSION=10.1.0 HIP_VISIBLE_DEVICES=0 HSA_ENABLE_SDMA=0 HSA_TOOLS_LIB= HSA_TOOLS_REPORT_LOAD_FAILURE=0"

# Compile hip_probe
print("\nCompiling hip_probe...")
out, err, rc = run_cmd(c, f"bash -c 'export {env}; /opt/rocm/bin/hipcc --offload-arch=gfx1010 -o /tmp/hip_probe /tmp/hip_probe.cpp 2>&1'")
print(f"  Exit code: {rc}")
if rc != 0:
    print(f"  Error: {out}{err}")

# Compile hip_minimal_test
print("Compiling hip_minimal_test...")
out, err, rc = run_cmd(c, f"bash -c 'export {env}; /opt/rocm/bin/hipcc --offload-arch=gfx1010 -o /tmp/hip_minimal_test /tmp/hip_minimal_test.cpp 2>&1'")
print(f"  Exit code: {rc}")
if rc != 0:
    print(f"  Error: {out}{err}")

# Compile hip_vector_add
print("Compiling hip_vector_add...")
out, err, rc = run_cmd(c, f"bash -c 'export {env}; /opt/rocm/bin/hipcc --offload-arch=gfx1010 -o /tmp/hip_vector_add /tmp/hip_vector_add.cpp 2>&1'")
print(f"  Exit code: {rc}")
if rc != 0:
    print(f"  Error: {out}{err}")

c.close()
print("\nAll compilations done.")
