ctp-rust 1.0.3

Safe Rust bindings for CTP (Comprehensive Transaction Platform) and its variations for Chinese financial markets
Documentation
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
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;

/// 检查源文件是否比目标文件新
fn source_newer_than_target(source: &PathBuf, target: &PathBuf) -> bool {
    if !target.exists() {
        return true;
    }

    let source_time = fs::metadata(source)
        .and_then(|m| m.modified())
        .unwrap_or(std::time::SystemTime::UNIX_EPOCH);

    let target_time = fs::metadata(target)
        .and_then(|m| m.modified())
        .unwrap_or(std::time::SystemTime::UNIX_EPOCH);

    source_time > target_time
}

fn main() {
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=libs/");
    println!("cargo:rerun-if-changed=src/");

    // 获取当前构建目标
    let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
    let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();

    // 获取包的根目录和输出目录
    let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
    let package_root = PathBuf::from(&manifest_dir);

    // 使用统一wrapper,根据平台选择对应的include和lib路径
    let (include_path, lib_path, wrapper_path, lib_suffix) =
        match (target_os.as_str(), target_arch.as_str()) {
            ("macos", "x86_64") | ("macos", "aarch64") => {
                // macOS使用统一wrapper
                (
                    package_root.join("libs/ctp/mac64/include"),
                    package_root.join("libs/ctp/mac64/lib"),
                    package_root.join("libs/ctp/wrapper"), // 使用统一wrapper
                    "dylib",
                )
            }
            ("linux", "x86_64") => {
                // Linux使用统一wrapper
                (
                    package_root.join("libs/ctp/linux/include"),
                    package_root.join("libs/ctp/linux/lib"),
                    package_root.join("libs/ctp/wrapper"), // 使用统一wrapper
                    "so",
                )
            }
            ("linux", "aarch64") => {
                // ARM64 Linux,如果没有专用库则报错
                let arm_path = package_root.join("libs/ctp/linux_arm64");
                if arm_path.exists() {
                    (
                        arm_path.join("include"),
                        arm_path.join("lib"),
                        package_root.join("libs/ctp/wrapper"), // 使用统一wrapper
                        "so",
                    )
                } else {
                    panic!("不支持ARM64 Linux平台,CTP库仅支持x86_64架构");
                }
            }
            _ => panic!("不支持的平台: {} {}", target_os, target_arch),
        };

    // 检查库文件是否存在
    if !lib_path.exists() {
        println!("cargo:warning=CTP库目录不存在: {}", lib_path.display());
        println!("cargo:warning=请确保将CTP SDK库文件放置在正确位置");
        println!("cargo:warning=参考README.md中的CTP SDK配置说明");
        println!("cargo:rustc-cfg=feature=\"test_mode\"");
        return;
    }

    // 检查头文件是否存在
    if !include_path.exists() {
        println!(
            "cargo:warning=CTP头文件目录不存在: {}",
            include_path.display()
        );
        println!("cargo:warning=请确保将CTP SDK头文件放置在正确位置");
        println!("cargo:rustc-cfg=feature=\"test_mode\"");
        return;
    }

    // 验证必要的库文件是否存在
    let (md_lib, trader_lib) = (
        lib_path.join(format!("libthostmduserapi_se.{}", lib_suffix)),
        lib_path.join(format!("libthosttraderapi_se.{}", lib_suffix)),
    );

    let has_md_lib = md_lib.exists();
    let has_trader_lib = trader_lib.exists();

    if !has_md_lib && !has_trader_lib {
        println!("cargo:warning=CTP库文件不存在,将跳过动态链接。如需完整功能,请将CTP库文件放置在正确位置。");
        println!("cargo:warning=行情API库: {}", md_lib.display());
        println!("cargo:warning=交易API库: {}", trader_lib.display());

        // 在测试模式下不进行链接
        println!("cargo:rustc-cfg=feature=\"test_mode\"");
        println!("cargo:warning=构建目标: {} {}", target_os, target_arch);
        println!("cargo:warning=测试模式:将跳过CTP库链接");
        return;
    }

    // 编译 C++ 包装器
    let wrapper_cpp = wrapper_path.join("ctp_wrapper.cpp");
    let spi_bridge_cpp = wrapper_path.join("spi_bridge.cpp");
    let logger_cpp = wrapper_path.join("logger.cpp");

    // 使用 OUT_DIR 存放编译产物
    let wrapper_obj = out_dir.join("ctp_wrapper.o");
    let spi_bridge_obj = out_dir.join("spi_bridge.o");
    let logger_obj = out_dir.join("logger.o");

    // 检查包装器库是否已经存在(同时检查源目录和输出目录)
    let existing_wrapper_lib_src = if target_os == "macos" {
        wrapper_path.join("libctp_wrapper.dylib")
    } else {
        wrapper_path.join("libctp_wrapper.so")
    };

    let wrapper_lib_out = if target_os == "macos" {
        out_dir.join("libctp_wrapper.dylib")
    } else {
        out_dir.join("libctp_wrapper.so")
    };

    // 检查源文件是否存在
    if !wrapper_cpp.exists() {
        println!(
            "cargo:warning=CTP wrapper源文件不存在: {}",
            wrapper_cpp.display()
        );
        println!("cargo:warning=请确保完整下载了ctp-rust包的源代码");
        println!("cargo:rustc-cfg=feature=\"test_mode\"");
        return;
    }

    if !spi_bridge_cpp.exists() {
        println!(
            "cargo:warning=SPI bridge源文件不存在: {}",
            spi_bridge_cpp.display()
        );
        println!("cargo:rustc-cfg=feature=\"test_mode\"");
        return;
    }

    if !logger_cpp.exists() {
        println!(
            "cargo:warning=Debug logger源文件不存在: {}",
            logger_cpp.display()
        );
        println!("cargo:rustc-cfg=feature=\"test_mode\"");
        return;
    }

    // 始终重新编译wrapper库以确保兼容性
    let should_compile = !wrapper_lib_out.exists()
        || source_newer_than_target(&wrapper_cpp, &wrapper_lib_out)
        || source_newer_than_target(&spi_bridge_cpp, &wrapper_lib_out)
        || source_newer_than_target(&logger_cpp, &wrapper_lib_out);

    if should_compile {
        println!("cargo:warning=编译CTP C++包装器");

        // 编译wrapper
        let mut cmd = if target_os == "macos" {
            Command::new("clang++")
        } else {
            Command::new("g++")
        };
        cmd.arg("-c")
            .arg("-std=c++11")
            .arg("-fPIC")
            .arg("-I")
            .arg(&include_path)
            .arg("-o")
            .arg(&wrapper_obj)
            .arg(&wrapper_cpp);

        // 添加平台特定的编译选项和宏定义
        if target_os == "macos" {
            cmd.arg("-mmacosx-version-min=14.0")
                .arg("-DCTP_PLATFORM_MACOS");
        } else {
            cmd.arg("-DCTP_PLATFORM_LINUX");
        }

        let output = cmd.output().expect("Failed to compile CTP wrapper");

        if !output.status.success() {
            panic!(
                "Failed to compile CTP wrapper: {}",
                String::from_utf8_lossy(&output.stderr)
            );
        }

        // 编译SPI bridge
        let mut spi_cmd = if target_os == "macos" {
            Command::new("clang++")
        } else {
            Command::new("g++")
        };
        spi_cmd
            .arg("-c")
            .arg("-std=c++11")
            .arg("-fPIC")
            .arg("-I")
            .arg(&include_path)
            .arg("-o")
            .arg(&spi_bridge_obj)
            .arg(&spi_bridge_cpp);

        // 添加平台特定的编译选项和宏定义
        if target_os == "macos" {
            spi_cmd
                .arg("-mmacosx-version-min=14.0")
                .arg("-DCTP_PLATFORM_MACOS");
        } else {
            spi_cmd.arg("-DCTP_PLATFORM_LINUX");
        }

        let spi_output = spi_cmd.output().expect("Failed to compile SPI bridge");

        if !spi_output.status.success() {
            panic!(
                "Failed to compile SPI bridge: {}",
                String::from_utf8_lossy(&spi_output.stderr)
            );
        }

        // 编译debug logger
        let mut debug_cmd = if target_os == "macos" {
            Command::new("clang++")
        } else {
            Command::new("g++")
        };
        debug_cmd
            .arg("-c")
            .arg("-std=c++11")
            .arg("-fPIC")
            .arg("-I")
            .arg(&include_path)
            .arg("-o")
            .arg(&logger_obj)
            .arg(&logger_cpp);

        // 添加平台特定的编译选项和宏定义
        if target_os == "macos" {
            debug_cmd
                .arg("-mmacosx-version-min=14.0")
                .arg("-DCTP_PLATFORM_MACOS");
        } else {
            debug_cmd.arg("-DCTP_PLATFORM_LINUX");
        }

        let debug_output = debug_cmd.output().expect("Failed to compile debug logger");

        if !debug_output.status.success() {
            panic!(
                "Failed to compile debug logger: {}",
                String::from_utf8_lossy(&debug_output.stderr)
            );
        }

        // 创建动态库到OUT_DIR
        let wrapper_lib = wrapper_lib_out.clone();

        // 创建动态库而不是静态库
        let mut link_cmd = if target_os == "macos" {
            let mut cmd = Command::new("clang++");
            cmd.arg("-shared")
                .arg("-o")
                .arg(&wrapper_lib)
                .arg(&wrapper_obj)
                .arg(&spi_bridge_obj)
                .arg(&logger_obj)
                .arg("-L")
                .arg(&lib_path)
                .arg("-lthostmduserapi_se")
                .arg("-lthosttraderapi_se");
            cmd
        } else {
            let mut cmd = Command::new("g++");
            cmd.arg("-shared")
                .arg("-fPIC")
                .arg("-o")
                .arg(&wrapper_lib)
                .arg(&wrapper_obj)
                .arg(&spi_bridge_obj)
                .arg(&logger_obj)
                .arg("-L")
                .arg(&lib_path)
                .arg("-lthostmduserapi_se")
                .arg("-lthosttraderapi_se")
                .arg("-lpthread")
                .arg("-ldl");
            cmd
        };

        let link_output = link_cmd
            .output()
            .expect("Failed to create wrapper dynamic library");

        if !link_output.status.success() {
            panic!(
                "Failed to create wrapper dynamic library: {}",
                String::from_utf8_lossy(&link_output.stderr)
            );
        }

        println!("cargo:warning=CTP C++包装器编译完成");
    } else {
        // 检查是否存在预编译的库
        if existing_wrapper_lib_src.exists() {
            println!("cargo:warning=发现预编译的包装器库,复制到构建目录");
            if let Err(e) = std::fs::copy(&existing_wrapper_lib_src, &wrapper_lib_out) {
                println!("cargo:warning=复制包装器库失败: {}", e);
                // 继续尝试直接使用源目录中的库
                println!("cargo:rustc-link-search=native={}", wrapper_path.display());
            }
        } else if !wrapper_lib_out.exists() {
            println!("cargo:warning=未找到CTP包装器库,尝试重新编译");
            // 强制重新编译
            println!("cargo:warning=编译CTP C++包装器");

            // ... 这里可以复制编译逻辑,或者设置一个标志重新执行编译
        }

        if wrapper_lib_out.exists() {
            println!("cargo:warning=使用已存在的CTP C++包装器库");
        }
    }

    // 无论如何都要确保输出链接指令
    println!("cargo:rustc-link-search=native={}", out_dir.display());
    println!("cargo:rustc-link-lib=dylib=ctp_wrapper");

    // 输出给 Cargo,让 Rust 知道去哪里找库
    println!("cargo:rustc-link-search=native={}", lib_path.display());

    // 设置条件编译标志
    if has_md_lib {
        println!("cargo:rustc-cfg=feature=\"md_api\"");
        println!("cargo:rustc-link-lib=dylib=thostmduserapi_se");
        println!("cargo:warning=找到行情API库: {}", md_lib.display());
    } else {
        println!("cargo:warning=行情API库不存在: {}", md_lib.display());
    }

    if has_trader_lib {
        println!("cargo:rustc-cfg=feature=\"trader_api\"");
        println!("cargo:rustc-link-lib=dylib=thosttraderapi_se");
        println!("cargo:warning=找到交易API库: {}", trader_lib.display());
    } else {
        println!("cargo:warning=交易API库不存在: {}", trader_lib.display());
    }

    // 如果没有任何库文件,启用测试模式
    if !has_md_lib && !has_trader_lib {
        println!("cargo:rustc-cfg=feature=\"test_mode\"");
    }

    // 在macOS上可能需要额外的系统库
    if target_os == "macos" {
        println!("cargo:rustc-link-lib=dylib=c++");
        println!("cargo:rustc-link-lib=framework=Security");
        println!("cargo:rustc-link-lib=framework=SystemConfiguration");
    }

    // 在Linux上可能需要额外的系统库
    if target_os == "linux" {
        println!("cargo:rustc-link-lib=dylib=stdc++");
        println!("cargo:rustc-link-lib=dylib=pthread");
        println!("cargo:rustc-link-lib=dylib=dl");
    }

    // 设置rpath以便运行时能找到动态库
    if target_os == "macos" {
        println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path");
        println!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib_path.display());
        // 设置macOS最低版本以匹配系统环境
        println!("cargo:rustc-link-arg=-Wl,-macosx_version_min,14.0");
    } else if target_os == "linux" {
        println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN");
        println!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib_path.display());
    }

    // rerun 规则
    println!("cargo:rerun-if-changed={}", include_path.display());
    println!("cargo:rerun-if-changed={}", lib_path.display());
    println!("cargo:rerun-if-changed={}", wrapper_path.display());
    println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_OS");
    println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_ARCH");

    // 输出构建信息
    println!("cargo:warning=构建目标: {} {}", target_os, target_arch);
    println!("cargo:warning=使用库目录: {}", lib_path.display());
}