import json, os, shutil, subprocess, time, glob, threading
ROOT="/Users/oscar/.worktree/hot-reload/labs/hot-reload"; REC=f"{ROOT}/target/kan-hot/debug/rustc"
SRC=f"{ROOT}/api-common/src/lib.rs"; BASE="/private/var/folders/ft/fkt3bwqd01g7f54wvyz9wgjc0000gn/T/opencode/pipe/out29"
def load(n): return json.load(open(f"{REC}/{n}.json"))
def strip_owned(argv):
    out=[]; i=1
    while i<len(argv):
        a=argv[i]
        if a=="-C" and i+1<len(argv) and any(argv[i+1].startswith(p) for p in ("incremental=","codegen-units=","debuginfo=")): i+=2; continue
        if a.startswith("-C") and any(a[2:].startswith(p) for p in ("incremental=","codegen-units=","debuginfo=")): i+=1; continue
        out.append(a); i+=1
    return out
def rewrite_extern(argv,name,path):
    out=[]; i=0
    while i<len(argv):
        a=argv[i]
        if a=="--extern" and i+1<len(argv) and argv[i+1].startswith(name+"="):
            out+=["--extern",f"{name}={path}"]; i+=2; continue
        if a.startswith(f"--extern={name}="):
            out.append(f"--extern={name}={path}"); i+=1; continue
        out.append(a); i+=1
    return out
common=load("api_common"); api=load("api"); orig=open(SRC).read(); assert "nested-v1" in orig
shutil.rmtree(BASE,ignore_errors=True); os.makedirs(BASE)
def run(mode,mark,i):
    d=f"{BASE}/{mode}"; os.makedirs(d,exist_ok=True)
    open(SRC,"w").write(orig.replace("nested-v1",mark))
    ca=[common["argv"][0]]+strip_owned(common["argv"])+["-C","debuginfo=0","-C","codegen-units=1"]
    j=ca.index("--out-dir"); ca[j+1]=d
    env=dict(common["env"]); env["RUSTC_BOOTSTRAP"]="1"
    if mode=="mir": ca+=["-Zalways-encode-mir"]
    t0=time.monotonic()
    p=subprocess.Popen(ca,cwd=common["cwd"],env=env,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,text=True,bufsize=1)
    tmeta=None; api_result=None; meta_copy=None
    lines=[]
    def reader():
        nonlocal tmeta
        for line in p.stdout:
            lines.append(line)
            if tmeta is None and '"emit":"metadata"' in line:
                tmeta=time.monotonic()-t0
    th=threading.Thread(target=reader,daemon=True); th.start()
    # poll for the metadata artifact, then copy + start api
    while tmeta is None and p.poll() is None:
        time.sleep(0.0005)
    if tmeta is not None:
        # locate rmeta, copy to unique name to avoid being overwritten/clobbered
        rm=[f for f in os.listdir(d) if f.endswith(".rmeta")]
        if rm:
            meta_copy=os.path.join(BASE,f"meta-{mode}-{i}.rmeta")
            shutil.copy(os.path.join(d,rm[0]),meta_copy)
            rmeta=meta_copy
            aa=rewrite_extern([api["argv"][0]]+strip_owned(api["argv"]),"api_common",rmeta)
            aa+=["-C","debuginfo=0","-C","codegen-units=8","-C",f"incremental={BASE}/inc/{mode}"]
            j=aa.index("--out-dir"); aa[j+1]=d
            ta=time.monotonic(); pa=subprocess.run(aa,cwd=api["cwd"],env=env,stdout=subprocess.DEVNULL,stderr=subprocess.PIPE); api_result=((time.monotonic()-ta)*1000,pa.returncode,pa.stderr[:200])
    p.wait(); tdone=time.monotonic()-t0
    mok=meta_copy is not None and any(mark.encode() in open(f,"rb").read() for f in glob.glob(os.path.join(d,"libapi-*.rlib")))
    return tmeta,tdone,api_result,mok
try:
    for mode in ["nof", "mir"]:
        print("=====",mode)
        for i in range(3):
            tmeta,tdone,ares,mok=run(mode,f"e29-{mode}-{i}",i)
            print(f" {i}: meta@{tmeta and round(tmeta*1000,1)}ms done@{tdone*1000:.1f}ms api={ares and (round(ares[0],1),ares[1])} marker={mok}")
            if ares and ares[1]: print("   err",ares[2][:300])
            time.sleep(.15)
finally:
    open(SRC,"w").write(orig)
