1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use std::{env, fs};
use std::cell::RefCell;
use std::env::temp_dir;
use std::ops::Deref;
use std::path::PathBuf;
use std::process::Command;
use std::sync::Arc;

use anyhow::anyhow;
use pilota_build::{CodegenBackend, Context, DefId, MakeBackend, Output, ProtobufBackend, rir::Enum, rir::Message, rir::Method, rir::NewType, rir::Service};
use pilota_build::db::RirDatabase;
use pilota_build::ir::ItemKind;
use pilota_build::parser::{Parser, ProtobufParser};
use pilota_build::plugin::{AutoDerivePlugin, PredicateResult};
use pilota_build::rir::{Arg, Item};
use pilota_build::ty::TyKind;

use crate::ffidl::gen_go::GoCodegenBackend;
use crate::ffidl::gen_rust::RustCodegenBackend;

mod gen_rust;
mod gen_go;

const CGOBIN: &'static str = "cgobin";
#[cfg(not(debug_assertions))]
const MODE: &'static str = "release";
#[cfg(debug_assertions)]
const MODE: &'static str = "debug";

#[derive(Debug, Clone)]
pub struct Config {
    pub idl_file_path: PathBuf,
    pub output_dir: PathBuf,
    pub impl_ffi_for_unitstruct: Option<UnitLikeStructPath>,
    pub go_root_path: Option<PathBuf>,
    pub go_mod_path: &'static str,
    pub goffi_impl_of_object: Option<GoObjectPath>,
}

impl Config {
    fn dir_name(&self) -> String {
        self.output_dir.file_name().unwrap().to_os_string().into_string().unwrap()
    }
    fn go_mod_name(&self) -> String {
        self.go_mod_path.rsplit_once("/").expect("invalid go mod path").1.to_string()
    }
}

#[derive(Debug, Clone)]
pub struct UnitLikeStructPath(pub &'static str);

#[derive(Debug, Clone)]
pub struct GoObjectPath {
    pub import: String,
    pub object_ident: String,
}

#[derive(Debug, Clone)]
pub(crate) struct FFIDL {
    config: Arc<Config>,
    rust_c_header_name_base: Arc<RefCell<String>>,
    go_c_header_name_base: Arc<RefCell<String>>,
    clib_dir: Arc<RefCell<PathBuf>>,
    go_pkg_code: Arc<RefCell<String>>,
    go_main_code: Arc<RefCell<String>>,
}

unsafe impl Send for FFIDL {}

impl FFIDL {
    pub(crate) fn generate(config: Config) -> anyhow::Result<()> {
        if config.go_mod_name() != config.dir_name() {
            return Err(anyhow!("The directory name of 'output_dir' is inconsistent with the mod name of 'go_mod_path'"));
        }
        Self {
            config: Arc::new(config),
            go_pkg_code: Arc::new(RefCell::new(String::new())),
            go_main_code: Arc::new(RefCell::new(String::new())),
            rust_c_header_name_base: Arc::new(RefCell::new("".to_string())),
            go_c_header_name_base: Arc::new(RefCell::new("".to_string())),
            clib_dir: Arc::new(RefCell::new(Default::default())),
        }
            .check_idl()?
            .set_clib_paths()
            .gen_rust_and_go()
    }
    fn include_dir(&self) -> PathBuf {
        self.config.idl_file_path.parent().unwrap().to_path_buf()
    }
    fn check_idl(self) -> anyhow::Result<Self> {
        let mut parser = ProtobufParser::default();
        // let mut parser = ThriftParser::default();
        Parser::include_dirs(&mut parser, vec![self.include_dir()]);
        Parser::input(&mut parser, &self.config.idl_file_path);
        let file = Parser::parse(parser).files.pop().unwrap();
        if !file.uses.is_empty() {
            return Err(anyhow!("Does not support Protobuf 'import'."));
        }
        for item in &file.items {
            match &item.kind {
                ItemKind::Message(_) => {}
                ItemKind::Service(service_item) => {
                    match service_item.name.to_lowercase().as_str() {
                        "goffi" | "rustffi" => {}
                        _ => {
                            return Err(anyhow!(
                                "Protobuf Service name can only be: 'GoFFI', 'RustFFI'."
                            ));
                        }
                    }
                }
                _ => {
                    return Err(anyhow!(
                        "Protobuf Item '{}' not supported.",
                        format!("{:?}", item)
                        .trim_start_matches("Item { kind: ")
                        .split_once("(")
                        .unwrap()
                        .0
                        .to_lowercase()
                    ));
                }
            }
        }
        Ok(self)
    }

    fn set_clib_paths(self) -> Self {
        *self.rust_c_header_name_base.borrow_mut() = env::var("CARGO_PKG_NAME").unwrap().replace("-", "_");
        *self.go_c_header_name_base.borrow_mut() = "go_".to_string() + &env::var("CARGO_PKG_NAME").unwrap().replace("-", "_");
        *self.clib_dir.borrow_mut() = env::var("CARGO_TARGET_DIR").map_or_else(
            |_|
                PathBuf::from(env::var("CARGO_WORKSPACE_DIR")
                    .unwrap_or(env::var("CARGO_MANIFEST_DIR").unwrap_or_default())
                ).join("target"),
            PathBuf::from,
        )
            .join(MODE);
        self
    }

    fn gen_rust_and_go(self) -> anyhow::Result<()> {
        fs::create_dir_all(&self.config.output_dir.join(CGOBIN))?;
        let pkg_name = self.config.dir_name();

        let temp_dir = temp_dir();
        let temp_idl = temp_dir.join(pkg_name.clone() + ".proto");
        fs::copy(&self.config.idl_file_path, &temp_idl)?;
        let output = new_shell_cmd()
            .arg(format!(
                "protoc --proto_path={} --go_out {} {}",
                temp_dir.to_str().unwrap(),
                self.config.output_dir.to_str().unwrap(),
                temp_idl.to_str().unwrap(),
            ))
            .output()
            .unwrap();
        if !output.status.success() {
            eprintln!("gen_protobuf_code: {:?}", output)
        }

        let import_gen_pkg = self.config.go_mod_path;
        let rust_c_header_name_base = self.rust_c_header_name_base.borrow().to_string();
        let rust_c_lib_dir = self.clib_dir.borrow().as_os_str().to_str().unwrap().to_string();
        *self.go_main_code.borrow_mut() = format!(r###"package main

/*
#cgo CFLAGS: -I{rust_c_lib_dir}
#cgo LDFLAGS: -L{rust_c_lib_dir} -l{rust_c_header_name_base}

#include "{rust_c_header_name_base}.h"
*/
import "C"
import (
	"reflect"
	"unsafe"

	"{import_gen_pkg}"
	"github.com/andeya/gust"
)

// main function is never called by C to.
func main() {{}}

var (
	_ reflect.SliceHeader
	_ unsafe.Pointer
	_ gust.EnumResult[any, any]
	_ gen.ResultCode
)

        "###);

        *self.go_pkg_code.borrow_mut() = format!(
            r###"package {pkg_name}
            /*
            #cgo CFLAGS: -I{rust_c_lib_dir}
            #cgo LDFLAGS: -L{rust_c_lib_dir} -l{rust_c_header_name_base}

            #include "{rust_c_header_name_base}.h"
            */
            import "C"

            import (
                "errors"
                "fmt"
                "reflect"
                "unsafe"

                "github.com/andeya/gust/valconv"
                "github.com/bytedance/sonic"
                "github.com/golang/protobuf/proto"
            )

            var (
                _ = errors.New
                _ = fmt.Sprintf
                _ reflect.SliceHeader
                _ unsafe.Pointer
                _ valconv.ReadonlyBytes
                _ = sonic.Marshal
                _ = proto.Marshal
            )

            "###);

        pilota_build::Builder::protobuf_with_backend(self.clone())
            // pilota_build::Builder::thrift_with_backend(self.clone())
            .include_dirs(vec![self.include_dir()])
            .plugin(AutoDerivePlugin::new(Arc::new(["#[derive(::serde::Serialize, ::serde::Deserialize)]".into()]), |_| PredicateResult::GoOn))
            .ignore_unused(true)
            .compile(
                [&self.config.idl_file_path],
                Output::File(self.config.output_dir.join("mod.rs")),
            );

        self.gen_rust_clib()?;

        fs::write(
            &self.config.output_dir.join(pkg_name + ".go"),
            self.go_pkg_code.borrow().as_str(),
        )?;
        fs::write(
            &self.config.output_dir.join("go.mod"),
            format!(r###"module {}

            go 1.19

            require (
                github.com/andeya/gust v1.5.2
                github.com/bytedance/sonic v1.9.2
                github.com/golang/protobuf v1.5.3
            )

            "###, self.config.go_mod_path),
        )?;
        fs::write(
            self.config.output_dir.join(CGOBIN).join("main.go"),
            self.go_main_code.borrow().as_str(),
        )?;

        let output = Command::new(self.config.go_root_path.as_ref().map_or("gofmt".to_string(), |p| p.join("gofmt").to_str().unwrap().to_string()))
            .arg("-l")
            .arg("-w")
            .arg(self.config.output_dir.to_str().unwrap())
            .output()?;
        if !output.status.success() {
            eprintln!("{:?}", output);
        }

        let output = new_shell_cmd()
            .current_dir(&self.config.output_dir)
            .arg("go mod tidy").arg(self.config.output_dir.to_str().unwrap())
            .output()?;
        if !output.status.success() {
            eprintln!("{:?}", output);
        }

        self.gen_go_clib()?;
        Ok(())
    }
    fn gen_rust_clib(&self) -> anyhow::Result<()> {
        cbindgen::Builder::new()
            // .with_crate(env::var("CARGO_MANIFEST_DIR").unwrap())
            .with_src(self.config.output_dir.join("mod.rs"))
            // .with_src("/Users/henrylee2cn/rust/fcplug/rust/fcplug/src/lib.rs")
            .with_language(cbindgen::Language::C)
            .with_after_include(r###"
typedef int8_t ResultCode;

typedef struct Buffer {
  uint8_t *ptr;
  uintptr_t len;
  uintptr_t cap;
} Buffer;

typedef struct RustFfiResult {
  ResultCode code;
  struct Buffer data;
} RustFfiResult;

typedef struct GoFfiResult {
  ResultCode code;
  uintptr_t data_ptr;
} GoFfiResult;

void free_buffer(struct Buffer buf);
uintptr_t leak_buffer(struct Buffer buf);

"###)
            .generate()?
            .write_to_file(self.clib_dir.borrow().join(self.rust_c_header_name_base.borrow().to_string() + ".h"));
        Ok(())
    }
    fn gen_go_clib(&self) -> anyhow::Result<()> {
        let output = new_shell_cmd()
            .arg(format!(
                "CGO_ENABLED=1 go build -buildmode=c-archive -o {} {}",
                self.clib_dir.borrow().join("lib".to_string() + &self.go_c_header_name_base.borrow() + ".a").to_str().unwrap(),
                self.config.output_dir.join(CGOBIN).to_str().unwrap(),
            ))
            .output()
            .unwrap();
        if !output.status.success() {
            return Err(anyhow!(format!("{:?}",output)));
        }
        println!("cargo:rustc-link-search={}", self.clib_dir.borrow().to_str().unwrap());
        println!("cargo:rustc-link-lib={}", self.go_c_header_name_base.borrow());
        println!(
            "cargo:rerun-if-changed={}",
            self.clib_dir.borrow().join("lib".to_string() + &self.go_c_header_name_base.borrow() + ".h").to_str().unwrap(),
        );
        Ok(())
    }
}


impl MakeBackend for FFIDL {
    type Target = FFIDLBackend;

    fn make_backend(self, context: Context) -> Self::Target {
        let protobuf = ProtobufBackend::new(context.clone());
        let context = Arc::new(context);
        FFIDLBackend {
            rust: RustCodegenBackend {
                config: self.config.clone(),
                context: Cx(context.clone()),
            },
            go: GoCodegenBackend {
                config: self.config.clone(),
                context: Cx(context.clone()),
                go_pkg_code: self.go_pkg_code.clone(),
                go_main_code: self.go_main_code.clone(),
            },
            protobuf,
            context: Cx(context),
            config: self.config,
        }
    }
}

#[allow(dead_code)]
#[derive(Clone)]
pub struct FFIDLBackend {
    config: Arc<Config>,
    context: Cx,
    rust: RustCodegenBackend,
    go: GoCodegenBackend,
    protobuf: ProtobufBackend,
}

unsafe impl Send for FFIDLBackend {}

#[derive(Clone)]
pub(crate) struct Cx(Arc<Context>);

enum ServiceType {
    RustFfi,
    GoFfi,
}

impl Cx {
    fn service_type(&self, service_def_id: DefId) -> ServiceType {
        match self.rust_name(service_def_id).to_lowercase().as_str() {
            "rustffi" => ServiceType::RustFfi,
            "goffi" => ServiceType::GoFfi,
            _ => { unreachable!() }
        }
    }
    fn is_empty_ty(&self, kind: &TyKind) -> bool {
        match kind {
            TyKind::Path(path) => {
                if let Item::Message(m) = self.item(path.did).unwrap().as_ref() {
                    m.fields.is_empty()
                } else {
                    false
                }
            }
            TyKind::Void => true,
            _ => false
        }
    }
}

impl Deref for Cx {
    type Target = Context;

    fn deref(&self) -> &Self::Target {
        self.0.as_ref()
    }
}

impl FFIDLBackend {
    fn fix_empty_params(&self, method: &Method) -> Method {
        let mut method = method.clone();
        method.args = method.args.into_iter().filter(|arg| !self.context.is_empty_ty(&arg.ty.kind)).collect::<Vec<Arc<Arg>>>();
        if self.context.is_empty_ty(&method.ret.kind) {
            method.ret.kind = TyKind::Void;
        }
        method
    }
}

impl CodegenBackend for FFIDLBackend {
    fn cx(&self) -> &Context {
        self.context.0.as_ref()
    }
    fn codegen_struct_impl(&self, def_id: DefId, stream: &mut String, s: &Message) {
        self.protobuf.codegen_struct_impl(def_id, stream, s)
    }
    fn codegen_service_impl(&self, service_def_id: DefId, stream: &mut String, s: &Service) {
        let mut s = s.clone();
        s.methods = s.methods.iter().map(|method| Arc::new(self.fix_empty_params(method))).collect::<Vec<Arc<Method>>>();
        self.protobuf.codegen_service_impl(service_def_id, stream, &s);
        self.rust.codegen_service_impl(service_def_id, stream, &s);
        self.go.codegen(service_def_id, &s)
    }
    fn codegen_service_method(&self, service_def_id: DefId, method: &Method) -> String {
        let method = self.fix_empty_params(method);
        self.protobuf.codegen_service_method(service_def_id, &method);
        self.rust.codegen_service_method(service_def_id, &method)
    }
    fn codegen_enum_impl(&self, def_id: DefId, stream: &mut String, e: &Enum) {
        self.protobuf.codegen_enum_impl(def_id, stream, e);
    }
    fn codegen_newtype_impl(&self, def_id: DefId, stream: &mut String, t: &NewType) {
        self.protobuf.codegen_newtype_impl(def_id, stream, t);
    }
}

fn new_shell_cmd() -> Command {
    let mut param = ("sh", "-c");
    if cfg!(target_os = "windows") {
        param.0 = "cmd";
        param.1 = "/c";
    }
    let mut cmd = Command::new(param.0);
    cmd.arg(param.1);
    cmd
}

#[cfg(test)]
mod tests {
    use crate::ffidl::{Config, FFIDL, UnitLikeStructPath};

    #[test]
    fn test_idl() {
        FFIDL::generate(Config {
            idl_file_path: "/Users/henrylee2cn/rust/fcplug/demo/ffidl.proto".into(),
            output_dir: "/Users/henrylee2cn/rust/fcplug/demo/src/gen".into(),
            impl_ffi_for_unitstruct: Some(UnitLikeStructPath("crate::Test")),
            go_mod_path: "github.com/andeya/fcplug/demo/src/gen",
            go_root_path: Some("/Users/henrylee2cn/.gvm/gos/go1.19.9/bin".into()),
            goffi_impl_of_object: None,
        })
            .unwrap();
    }
}