ctp-sys 0.1.3

ctp rust binding
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
use inflector::Inflector;

use std::env::var;
use std::{
    env,
    fs::File,
    io::Write,
    path::{Path, PathBuf},
};

use clang::*;
fn get_full_name_of_entity(e: &Entity) -> String {
    let mut v = vec![e.get_name().expect("")];
    let mut xe = Box::new(e.clone());
    while let Some(e) = xe.get_lexical_parent() {
        if e.get_kind() == EntityKind::TranslationUnit || e.get_kind() == EntityKind::NotImplemented
        {
            break;
        }
        v.push(e.get_name().expect(""));
        xe = Box::new(e);
    }
    v.reverse();
    v.join("_")
}

fn parse_api(tu: &TranslationUnit, api_name: &str) -> String {
    let mut v_code = vec![];
    tu.get_entity().visit_children(|e, _parent| {
        let name = e.get_display_name();
        if let Some(name) = name {
            if name == api_name {
                let full_api_name = get_full_name_of_entity(&e);
                v_code.push(format!("impl {} {{", full_api_name));
                for e in e.get_children().iter().filter(|e| e.get_name().is_some()) {
                    let snake_fn_name = Inflector::to_snake_case(&e.get_name().unwrap());
                    let full_fn_name = get_full_name_of_entity(e);
                    if e.get_kind() == EntityKind::Destructor || e.is_static_method() {
                        continue;
                    }
                    let mut rust_code = String::new();
                    let mut c_code = String::new();
                    let arguments = e.get_arguments();
                    if let Some(arguments) = arguments {
                        for p in &arguments {
                            let tp = p.get_type().unwrap();
                            let (mut rust_type, c_type) = match tp.get_kind() {
                                TypeKind::Pointer => {
                                    let tp = tp.get_pointee_type().unwrap();
                                    let d = tp.get_declaration();
                                    if let Some(d) = d {
                                        (
                                            "&mut ".to_string() + &get_full_name_of_entity(&d),
                                            " as * mut ".to_string() + &get_full_name_of_entity(&d),
                                        )
                                    } else {
                                        match tp.get_kind() {
                                            // 这个是char*, register_front() 会有这样的参数
                                            TypeKind::CharS => (
                                                "std::ffi::CString".to_string(),
                                                ".into_raw()".to_string(),
                                            ),
                                            TypeKind::Pointer => {
                                                // 这是char**
                                                // mdapi.subscribe() 有这样的参数
                                                let tp = tp.get_pointee_type().unwrap();
                                                match tp.get_kind() {
                                                    TypeKind::CharS => (
                                                        "Vec<std::ffi::CString>".to_string(),
                                                        ".to_char_pp()".to_string(),
                                                    ),
                                                    _ => panic!(""),
                                                }
                                            }
                                            _ => (tp.get_display_name(), "".to_string()),
                                        }
                                    }
                                }
                                TypeKind::Typedef => {
                                    match tp
                                        .get_declaration()
                                        .unwrap()
                                        .get_typedef_underlying_type()
                                        .unwrap()
                                        .get_kind()
                                    {
                                        TypeKind::CharS => {
                                            ("std::os::raw::c_char".to_string(), "".to_string())
                                        }
                                        _ => {
                                            // (tp.get_display_name(), "".to_string())
                                            println!("tp={:?}", tp);
                                            panic!("");
                                        }
                                    }
                                }
                                TypeKind::Int => {
                                    ("std::os::raw::c_int".to_string(), "".to_string())
                                }
                                TypeKind::Enum => {
                                    let d = tp.get_declaration().unwrap();
                                    (get_full_name_of_entity(&d), "".to_string())
                                }
                                TypeKind::IncompleteArray => {
                                    ("Vec<std::ffi::CString>".to_string(), ".iter().map(|cs| cs.as_ptr()).collect::<Vec<_>>().as_mut_ptr() as *mut *mut i8".to_string())
                                }
                                _ => {
                                    // (tp.get_display_name(), "".to_string())
                                    println!("tp={:?}", tp);
                                    panic!("");
                                }
                            };
                            if rust_type == "int" {
                                // 或者要转为 std::os::raw::c_int
                                rust_type = "std::os::raw::c_int".to_string();
                            }
                            rust_code.push_str(&format!(
                                ", {}: {}",
                                Inflector::to_snake_case(&p.get_name().unwrap()),
                                rust_type
                            ));
                            c_code.push_str(&format!(
                                r#",
                                             {}{}"#,
                                Inflector::to_snake_case(&p.get_name().unwrap()),
                                c_type
                            ))
                        }
                    }
                    let result_type = e.get_result_type().unwrap().get_display_name();
                    let result_type = match result_type.as_str() {
                        "void" => "()",
                        "int" => "std::os::raw::c_int",
                        "const char *" => "*const std::os::raw::c_char",
                        _ => panic!("没处理的result_type={}", result_type),
                    };
                    let mut code = format!(
                        r#"
                            pub fn {}(&mut self{}) -> {} {{
                                    unsafe {{
                                           ((*(*self).vtable_).{})(self as *mut {}{})
                                        }}
                            }}"#,
                        snake_fn_name, rust_code, result_type, full_fn_name, full_api_name, c_code
                    );
                    if snake_fn_name == "register_spi" {
                        let static_vtable_var_name = Inflector::to_snake_case(&full_api_name)
                            .trim_end_matches("api")
                            .to_uppercase()
                            + "SPI_VTABLE";
                        code = format!(
                            r#"
                                pub fn {}(&mut self{}) -> {} {{
                                    let p_spi = Box::into_raw(Box::new(( &{}, p_spi)));
                                        unsafe {{
                                               ((*(*self).vtable_).{})(self as *mut {}{})
                                            }}
                                }}"#,
                            snake_fn_name,
                            rust_code.replace("&mut", "*const dyn") + "_trait",
                            result_type,
                            static_vtable_var_name,
                            full_fn_name,
                            full_api_name,
                            c_code
                        );
                    }
                    v_code.push(code);
                }
                v_code.push(format!(
                    r#"}}
                unsafe impl Send for {full_api_name} {{}}"#
                ));
                return EntityVisitResult::Break;
            }
        }
        EntityVisitResult::Recurse
    });
    v_code.join("")
}

fn parse_spi(tu: &TranslationUnit, spi_name: &str) -> String {
    let mut trait_lines = vec![];
    let mut vtable_lines = vec![];
    let mut static_table_lines = vec![];
    let mut c_fn_lines = vec![];
    let mut spi_output_enum_lines = vec![];
    let mut spi_output_enum_struct_lines = vec![];
    let mut impl_spi_fn_line = vec![];

    let mut fat_spi_code = "".to_string();
    let mut spi_stream_code = "".to_string();
    let vf = |e: Entity, _parent: Entity| {
        let name = e.get_name();
        if name.is_none() {
            return EntityVisitResult::Recurse;
        }
        if name.unwrap() != spi_name {
            return EntityVisitResult::Recurse;
        }
        let full_spi_name = get_full_name_of_entity(&e);
        let vtable_struct_name = format!("{full_spi_name}VTable");
        let full_trait_name = format!("{full_spi_name}_trait",);
        let full_spi_output_enum_name = format!("{full_spi_name}Output");
        let full_static_vtable_var_name =
            Inflector::to_snake_case(&full_spi_name).to_uppercase() + "_VTABLE";
        trait_lines.push(format!(r#"pub trait {full_trait_name}: Send {{"#,));
        vtable_lines.push(format!(
            r#"
        #[repr(C)]
        #[derive(Debug)]
        struct {vtable_struct_name} {{
        "#
        ));
        static_table_lines.push(format!(
            r#"static {full_static_vtable_var_name}: {vtable_struct_name} = {vtable_struct_name} {{
                "#
        ));
        spi_output_enum_lines.push(format!(
            r#"
        #[derive(Clone, Debug)]
        pub enum {full_spi_output_enum_name} {{"#
        ));
        impl_spi_fn_line.push(format!(
            r#"impl {full_trait_name} for {full_spi_name}Stream {{"#,
        ));
        for e in e.get_children().iter().filter(|e| e.get_name().is_some()) {
            let snake_fn_name = Inflector::to_snake_case(&e.get_name().unwrap());
            let fn_name = e.get_name().unwrap();
            let _full_fn_name = get_full_name_of_entity(e);
            if e.get_kind() == EntityKind::Destructor
                || e.get_kind() == EntityKind::Constructor
                || e.is_static_method()
            {
                continue;
            }
            let mut arg_list = vec![];
            if let Some(arguments) = e.get_arguments() {
                arg_list = arguments
                    .iter()
                    .map(|p| {
                        let arg_name = Inflector::to_snake_case(&p.get_name().unwrap());
                        let tp = p.get_type().unwrap();
                        match tp.get_kind() {
                            TypeKind::Pointer => {
                                let tp = tp.get_pointee_type().unwrap();
                                let d = tp.get_declaration();
                                if let Some(d) = d {
                                    (
                                        format!(
                                            "{arg_name} : Option<&{}",
                                            get_full_name_of_entity(&d) + ">"
                                        ),
                                        format!(
                                            "{arg_name} : * const {}",
                                            get_full_name_of_entity(&d)
                                        ),
                                        format!("{arg_name}.as_ref()"),
                                        format!("{arg_name}:{arg_name}.cloned()"),
                                    )
                                } else {
                                    panic!("");
                                }
                            }
                            TypeKind::Typedef => {
                                match tp
                                    .get_declaration()
                                    .unwrap()
                                    .get_typedef_underlying_type()
                                    .unwrap()
                                    .get_kind()
                                {
                                    TypeKind::CharS => (
                                        format!("{} : std::os::raw::c_char", arg_name),
                                        format!("{} : * const std::os::raw::c_char", arg_name,),
                                        arg_name.clone(),
                                        format!("{arg_name}:{arg_name}"),
                                    ),
                                    _ => panic!(""),
                                }
                            }
                            TypeKind::Int => (
                                format!("{} : std::os::raw::c_int", arg_name),
                                format!("{} : std::os::raw::c_int", arg_name,),
                                arg_name.clone(),
                                format!("{arg_name}:{arg_name}"),
                            ),
                            TypeKind::Bool => (
                                format!("{} : bool", arg_name),
                                format!("{} : bool", arg_name,),
                                arg_name.clone(),
                                format!("{arg_name}:{arg_name}"),
                            ),
                            _ => {
                                println!("kind={:?}", tp.get_kind());
                                panic!("");
                            }
                        }
                    })
                    .collect::<Vec<_>>();
            }
            let trait_line = format!(
                "fn {snake_fn_name}(&mut self, {}) {{}}\n",
                arg_list
                    .iter()
                    .map(|arg| { arg.0.clone() })
                    .collect::<Vec<_>>()
                    .join(",")
            );
            trait_lines.push(trait_line.clone());
            vtable_lines.push(format!(
                r#"{snake_fn_name}: extern "C" fn(spi: *mut {full_spi_name}Fat, {} ),
                "#,
                arg_list
                    .iter()
                    .map(|arg| { arg.1.clone() })
                    .collect::<Vec<_>>()
                    .join(",")
            ));
            static_table_lines.push(format!(
                r#"{snake_fn_name}: spi_{snake_fn_name},
            "#
            ));
            spi_output_enum_lines.push(format!(r#"{fn_name}({full_spi_name}{fn_name}Packet),"#,));
            spi_output_enum_struct_lines.push(format!(
                r#"
            #[derive(Clone, Debug)]
            pub struct {full_spi_name}{fn_name}Packet {{
                {}
            }}"#,
                arg_list
                    .iter()
                    .map(|arg| { format!("pub {},", arg.0.replace("&", "").clone()) })
                    .collect::<Vec<_>>()
                    .join("")
            ));
            let trait_line_front = trait_line.replace("{}", "");
            impl_spi_fn_line.push(format!(
                r#"{trait_line_front} {{
            self.inner.lock().unwrap().push({})
                }}
            "#,
                format!(r#"{full_spi_output_enum_name}::{fn_name}( {full_spi_name}{fn_name}Packet {{ {} }} )"#,
                arg_list.iter().map(|arg|{arg.3.clone()}).collect::<Vec<_>>().join(","))
            ));

            c_fn_lines.push(format!(
                r#"extern "C" fn spi_{}(spi: *mut {}Fat, {}) {{
                    unsafe {{
                        (*(*spi).md_spi_ptr).{}({})
                    }}
                }}"#,
                snake_fn_name,
                full_spi_name,
                arg_list
                    .iter()
                    .map(|arg| { arg.1.clone() })
                    .collect::<Vec<_>>()
                    .join(","),
                snake_fn_name,
                arg_list
                    .iter()
                    .map(|arg| { arg.2.clone() })
                    .collect::<Vec<_>>()
                    .join(","),
            ));
        }

        fat_spi_code = format!(
            r#"
        #[repr(C)]
        pub struct {full_spi_name}Fat {{
            vtable: *const {vtable_struct_name},
            pub md_spi_ptr: *mut dyn {full_trait_name},
        }}
        "#
        );
        spi_stream_code = format!(
            r#"
        use futures::stream::Stream;
        use std::{{
            pin::Pin,
            sync::{{Arc, Mutex}},
            task::Waker,
        }};

        struct {full_spi_name}Inner {{
            buf: std::collections::VecDeque<{full_spi_output_enum_name}>,
            waker: Option<Waker>,
        }}

        impl {full_spi_name}Inner {{
            fn push(&mut self, msg: {full_spi_output_enum_name}) {{
                self.buf.push_back(msg);
                if let Some(ref waker) = &self.waker {{
                    waker.clone().wake()
                }}
            }}
        }}

        pub struct {full_spi_name}Stream {{
            inner: Arc<Mutex<{full_spi_name}Inner>>,
        }}

        impl Stream for {full_spi_name}Stream {{
            type Item = {full_spi_output_enum_name};

            fn poll_next(
                self: Pin<&mut Self>,
                cx: &mut futures::task::Context<'_>,
            ) -> futures::task::Poll<Option<Self::Item>> {{
                use futures::task::Poll;
                let mut inner = self.inner.lock().unwrap();
                if let Some(i) = inner.buf.pop_front() {{
                    Poll::Ready(Some(i))
                }} else {{
                    inner.waker = Some(cx.waker().clone());
                    Poll::Pending
                }}
            }}

            fn size_hint(&self) -> (usize, Option<usize>) {{
                (0, None)
            }}
        }}

        pub fn create_spi() -> (Box<{full_spi_name}Stream>, *mut {full_spi_name}Stream) {{
            let i = {full_spi_name}Inner {{
                buf: std::collections::VecDeque::new(),
                waker: None,
            }};
            let xspi = {full_spi_name}Stream {{
                inner: Arc::new(Mutex::new(i)),
            }};
            let myspi = Box::new(xspi);
            let pp = Box::into_raw(myspi);
            let pp2 = pp.clone();
            (unsafe {{ Box::from_raw(pp2) }}, pp)
        }}
        "#
        );

        EntityVisitResult::Break
    };
    tu.get_entity().visit_children(vf);

    format!(
        r#"{} }}
{} }}
{} }}
{}
{} }};
{}
{}
{}
{} }}
"#,
        trait_lines.join(""),
        vtable_lines.join(""),
        spi_output_enum_lines.join(""),
        spi_output_enum_struct_lines.join(""),
        static_table_lines.join(""),
        c_fn_lines.join(""),
        fat_spi_code,
        spi_stream_code,
        impl_spi_fn_line.join("")
    )
}

use bindgen::callbacks::{DeriveInfo, ParseCallbacks};

#[derive(Debug)]
struct MyCallback {}

impl ParseCallbacks for MyCallback {
    // Test the "custom derives" capability by adding `PartialEq` to the `Test` struct.
    fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
        if info.name.starts_with("CThost")
            && !info.name.contains("Api")
            && !info.name.contains("Spi")
        {
            vec!["Decode".into(), "Encode".into()]
        } else {
            vec![]
        }
    }
}

fn generated() {
    clang_sys::load().expect("");
    let clang = Clang::new().unwrap();
    let index = Index::new(&clang, false, false);
    let tu = index.parser("wrapper.hpp").parse().unwrap();

    // 开着这个的话会导致每次重新编译
    let mut f = File::create("./src/md_impl.rs").expect("unable to create file");
    let code = parse_api(&tu, "CThostFtdcMdApi");
    f.write_all(code.as_bytes()).unwrap();
    let code = parse_spi(&tu, "CThostFtdcMdSpi");
    f.write_all(code.as_bytes()).unwrap();

    let mut f = File::create("./src/trade_impl.rs").expect("unable to create file");
    let code = parse_api(&tu, "CThostFtdcTraderApi");
    f.write_all(code.as_bytes()).unwrap();
    let code = parse_spi(&tu, "CThostFtdcTraderSpi");
    f.write_all(code.as_bytes()).unwrap();
}

fn main() {
    // 生成c++->C->Rust实现
    generated();

    // 库配置
    // ctp 所在目录名称
    let ctp_path = "ctp";
    // 版本
    let ctp_version = "v6.6.5_20210924";

    // 平台名称
    let platform = if cfg!(windows) { "win" } else { "linux" };
    // 架构
    let arch = if cfg!(target_arch = "x86_64") {
        "x64"
    } else {
        panic!("can not build on this platform, linux_x64 and windows_x64.")
    };

    // so 所在目录
    let so_path = format!("{}/{}/{}_{}", ctp_path, ctp_version, platform, arch);

    let library_path = Path::new(&so_path);
    // println!("cargo:rustc-link-search={}",so_path);
    // println!("cargo:rustc-link-search=native={}", library_path.display());

    // 平台差异化处理
    if cfg!(windows) {
        let output = var("OUT_DIR").unwrap();
        std::fs::copy(
            library_path.join("thostmduserapi_se.dll"),
            Path::new(&output)
                .join("..")
                .join("..")
                .join("..")
                .join("thostmduserapi_se.dll"),
        )
        .unwrap();
        std::fs::copy(
            library_path.join("thosttraderapi_se.dll"),
            Path::new(&output)
                .join("..")
                .join("..")
                .join("..")
                .join("thosttraderapi_se.dll"),
        )
        .unwrap();
    } else if cfg!(unix) {
        // 行情
        copy_lib_to(&so_path, &String::from("thostmduserapi_se.so"));
        // 交易
        copy_lib_to(&so_path, &String::from("thosttraderapi_se.so"));
    } else {
        println!("cargo:rustc-env=LD_LIBRARY_PATH={}", library_path.display());
    }

    // 告诉 rustc 需要 link thostmduserapi_se thosttraderapi_se
    println!("cargo:rustc-link-lib=thostmduserapi_se");
    println!("cargo:rustc-link-lib=thosttraderapi_se");

    // 告诉 cargo 当 wrapper.h 变化时重新运行,只有结构使用wrapper.h, 包含类定义使用wrapper.cpp
    println!("cargo:rerun-if-changed=wrapper.hpp");
    println!("cargo:rustc-link-lib=dylib=stdc++");

    // 配置 bindgen,并生成 Bindings 结构
    let bindings = bindgen::Builder::default()
        .header("wrapper.hpp")
        .clang_arg("-x")
        .clang_arg("c++")
        .derive_default(true)
        .derive_debug(true)
        .vtable_generation(true)
        .generate_comments(false) // 不需形成doc ,默认true
        .layout_tests(false) //不需要test,默认true
        .generate_comments(false) //不需注释,默认true
        .derive_copy(true)
        .derive_hash(false) //不要实现hash
        .parse_callbacks(Box::new(MyCallback {}))
        .generate()
        .expect("Unable to generate bindings");

    // 生成 Rust 代码
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    let binding_file = out_path.join("bindings.rs");
    bindings
        .write_to_file(&binding_file)
        .expect("Couldn't write bindings!");
}

///
/// 分发动态连接库
///
fn copy_lib_to(so_path: &String, so_filename: &String) {
    let out_dir = std::env::var("OUT_DIR").unwrap();
    let current_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
    let so_symlink_string = format!("{}/lib{}", out_dir, so_filename);
    let so_symlink = std::path::Path::new(&so_symlink_string);

    println!("cargo:rustc-link-search=native={}", out_dir);
    let target_so = format!("{}/{}", out_dir, so_filename);
    let source_so = format!("{}/{}/{}", current_dir, so_path, so_filename);

    println!("debug source_so:{:?}, target_so:{:?}", source_so, target_so);
    std::fs::copy(&source_so, &target_so).expect("failed to copy so to outdir");
    println!("cargo:resource={}", target_so);

    println!(
        "debug so_symlink:{:?} is exists:{:?}",
        so_symlink,
        so_symlink.try_exists().unwrap()
    );
    if so_symlink.exists() {
        std::fs::remove_file(so_symlink).expect("symlink exists, but failed to remove it");
    }
    std::os::unix::fs::symlink(
        &format!("{}/{}/{}", current_dir, so_path, so_filename),
        so_symlink,
    )
    .expect("failed to create new symlink");
}