cpp_to_rust 0.5.3

Automatic generator of C++ library wrappers
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
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
extern crate num_cpus;

use config::Config;
use cpp_code_generator::CppCodeGenerator;
use cpp_data::CppData;
use cpp_ffi_data::CppAndFfiData;
use cpp_ffi_generator;
use cpp_lib_builder::CppLibBuilder;
use cpp_parser;
use errors::{Result, ChainErr};
use file_utils::{PathBufWithAdded, move_files, create_dir_all, load_json, save_json, canonicalize,
                 remove_dir_all, remove_dir, read_dir, path_to_str, create_file};
use log;
use rust_code_generator::{RustCodeGeneratorDependency, RustLinkItem, RustLinkKind};
use rust_code_generator;
use rust_generator;
use rust_info::{InputCargoTomlData, RustExportInfo};
use string_utils::JoinWithString;
use utils::{is_msvc, MapIfOk};

use std::path::PathBuf;
use std;

pub enum BuildProfile {
  Debug,
  Release,
}

pub struct BuildEnvironment {
  pub config: Config,
  pub output_dir_path: PathBuf,
  pub source_dir_path: PathBuf,
  pub dependency_paths: Vec<PathBuf>,
  pub num_jobs: Option<i32>,
  pub build_profile: BuildProfile,
  pub pipe_output: bool,
  pub aggressive_caching: bool,
}

fn read_cache_env() -> Result<(PathBuf, PathBuf, bool)> {
  let source_dir_path = PathBuf::from(try!(std::env::var("CARGO_MANIFEST_DIR")
    .chain_err(|| "failed to read required env var: CARGO_MANIFEST_DIR")));
  let out_dir = PathBuf::from(try!(std::env::var("OUT_DIR")
    .chain_err(|| "failed to read required env var: OUT_DIR")));
  if let Ok(cache) = std::env::var("CPP_TO_RUST_CACHE") {
    let input_cargo_toml_path = source_dir_path.with_added("Cargo.toml");
    if !input_cargo_toml_path.exists() {
      return Err(format!("Input Cargo.toml does not exist: {}",
                         input_cargo_toml_path.display())
        .into());
    }
    let input_cargo_toml_data = try!(InputCargoTomlData::from_file(&input_cargo_toml_path));
    let cache_path = PathBuf::from(cache).with_added(&input_cargo_toml_data.name);
    let lib_in_file_path = out_dir.with_added("lib.in.rs");
    let mut lib_in_file = try!(create_file(lib_in_file_path));
    try!(lib_in_file.write(format!("include!(\"{}\");\n",
                                   cache_path.with_added("lib.in.rs").display())));
    log::info(format!("Using cache directory: {}", cache_path.display()));
    Ok((source_dir_path, cache_path, true))
  } else {
    Ok((source_dir_path, out_dir, false))
  }
}

const COMPLETED_MARKER_FILE_NAME: &'static str = "cpp_to_rust_completed";

pub fn is_processing_skipped() -> Result<bool> {
  let (_source_dir_path, output_dir_path, aggressive_caching) = try!(read_cache_env());
  Ok(aggressive_caching &&
     output_dir_path.with_added(COMPLETED_MARKER_FILE_NAME).as_path().exists())
}

struct DependencyInfo {
  pub rust_export_info: RustExportInfo,
  pub path: PathBuf,
}

fn load_dependency(path: &PathBuf) -> Result<(RustExportInfo, CppData)> {
  let cpp_data_path = path.with_added("cpp_data.json");
  if !cpp_data_path.exists() {
    return Err(format!("file not found: {}", cpp_data_path.display()).into());
  }
  let cpp_data = try!(load_json(&cpp_data_path));

  let rust_export_info_path = path.with_added("rust_export_info.json");
  if !rust_export_info_path.exists() {
    return Err(format!("file not found: {}", rust_export_info_path.display()).into());
  }
  let rust_export_info = try!(load_json(&rust_export_info_path));
  Ok((rust_export_info, cpp_data))
}

pub fn run_from_build_script(config: Config) -> Result<()> {
  let mut dependency_paths = Vec::new();
  for (name, value) in std::env::vars_os() {
    if let Ok(name) = name.into_string() {
      if name.starts_with("DEP_") && name.ends_with("_CPP_TO_RUST_DATA_PATH") {
        log::info(format!("Found dependency: {}", value.to_string_lossy()));
        dependency_paths.push(PathBuf::from(value));
      }
    }
  }
  let (source_dir_path, output_dir_path, aggressive_caching) = try!(read_cache_env());
  run(BuildEnvironment {
    config: config,
    source_dir_path: source_dir_path,
    output_dir_path: output_dir_path,
    num_jobs: try!(std::env::var("NUM_JOBS")
        .chain_err(|| "failed to read required env var: NUM_JOBS"))
      .parse()
      .ok(),
    build_profile: match try!(std::env::var("PROFILE")
        .chain_err(|| "failed to read required env var: PROFILE"))
      .as_ref() {
      "debug" | "test" | "doc" => BuildProfile::Debug,
      "release" | "bench" => BuildProfile::Release,
      a => return Err(format!("unsupported profile: {}", a).into()),
    },
    dependency_paths: dependency_paths,
    pipe_output: false,
    aggressive_caching: aggressive_caching,
  })
}



// TODO: simplify this function
#[cfg_attr(feature="clippy", allow(cyclomatic_complexity))]
pub fn run(env: BuildEnvironment) -> Result<()> {
  // canonicalize paths
  if !env.source_dir_path.as_path().exists() {
    return Err(format!("source dir doesn't exist: {}",
                       env.source_dir_path.display())
      .into());
  }
  if !env.output_dir_path.as_path().exists() {
    try!(create_dir_all(&env.output_dir_path));
  }
  let output_dir_path = try!(canonicalize(&env.output_dir_path));
  let source_dir_path = try!(canonicalize(&env.source_dir_path));

  log::info("Reading input Cargo.toml");
  let input_cargo_toml_path = source_dir_path.with_added("Cargo.toml");
  if !input_cargo_toml_path.exists() {
    return Err(format!("Input Cargo.toml does not exist: {}",
                       input_cargo_toml_path.display())
      .into());
  }
  let input_cargo_toml_data = try!(InputCargoTomlData::from_file(&input_cargo_toml_path));
  if env.config.linked_libs().iter().any(|x| x == &input_cargo_toml_data.name) {
    return Err(format!("Rust crate name ({}) must not be the same as linked library name \
            because it can cause library name conflict and linker failure.",
                       input_cargo_toml_data.name)
      .into());
  }

  for &(caption, paths) in &[("Include path", env.config.include_paths()),
                             ("Lib path", env.config.lib_paths()),
                             ("Target include path", env.config.target_include_paths())] {
    for path in paths {
      if !path.is_absolute() {
        return Err(format!("{} is not absolute: {}", caption, path.display()).into());
      }
      if !path.exists() {
        return Err(format!("{} does not exist: {}", caption, path.display()).into());
      }
      if !path.is_dir() {
        return Err(format!("{} is not a directory: {}", caption, path.display()).into());
      }
    }
  }
  let cpp_lib_dirs = Vec::from(env.config.lib_paths());
  let framework_dirs = Vec::from(env.config.framework_paths());
  let include_dirs = Vec::from(env.config.include_paths());
  let target_include_dirs = Vec::from(env.config.target_include_paths());
  let mut link_items = Vec::new();
  for item in env.config.linked_libs() {
    link_items.push(RustLinkItem {
      name: item.to_string(),
      kind: RustLinkKind::SharedLibrary,
    });
  }
  for item in env.config.linked_frameworks() {
    link_items.push(RustLinkItem {
      name: item.to_string(),
      kind: RustLinkKind::Framework,
    });
  }
  if let Some(ref links) = input_cargo_toml_data.links {
    if !link_items.iter().any(|x| &x.name == links) {
      log::warning(format!("Value of 'links' field in Cargo.toml ({}) should be one of \
        linked libraries or frameworks ({}).",
                           links,
                           link_items.iter().map(|x| &x.name).join(", ")));
    }
  } else {
    log::warning("It's recommended to add 'links' field to Cargo.toml.");
  }

  // TODO: move other effects of this var to qt_build_tools
  let is_qt_library = link_items.iter().any(|x| x.name.starts_with("Qt"));


  let c_lib_parent_path = output_dir_path.with_added("c_lib");
  let c_lib_install_path = c_lib_parent_path.with_added("install");
  let c_lib_lib_path = c_lib_install_path.with_added("lib");
  let num_jobs = env.num_jobs.unwrap_or_else(|| num_cpus::get() as i32);
  let c_lib_is_shared = is_msvc();
  if env.aggressive_caching &&
     output_dir_path.with_added(COMPLETED_MARKER_FILE_NAME).as_path().exists() {
    log::info("No processing! cpp_to_rust uses previous results.");
    log::info(format!("Remove \"{}\" file to force processing.",
                      output_dir_path.with_added(COMPLETED_MARKER_FILE_NAME).display()));
  } else {
    if !env.dependency_paths.is_empty() {
      log::info("Loading dependencies");
    }
    let mut dependencies = Vec::new();
    let mut dependencies_cpp_data = Vec::new();
    for path in &env.dependency_paths {
      let (info, cpp_data) = try!(load_dependency(&try!(canonicalize(path)))
        .chain_err(|| "failed to load dependency"));
      dependencies.push(DependencyInfo {
        path: path.clone(),
        rust_export_info: info,
      });
      dependencies_cpp_data.push(cpp_data);
    }

    let cpp_data_cache_file_path = output_dir_path.with_added("cpp_data.json");
    let loaded_cpp_data = if env.aggressive_caching &&
                             cpp_data_cache_file_path.as_path().is_file() {
      match load_json(&cpp_data_cache_file_path) {
        Ok(r) => {
          log::info(format!("C++ data is loaded from file: {}",
                            cpp_data_cache_file_path.display()));
          Some(r)
        }
        Err(err) => {
          log::warning(format!("Failed to load C++ data: {}", err));
          err.discard_expected();
          None
        }
      }
    } else {
      None
    };
    let cpp_data = if let Some(r) = loaded_cpp_data {
      r
    } else {
      let raw_cpp_data_cache_file_path = output_dir_path.with_added("raw_cpp_data.json");
      let mut cpp_data = if env.aggressive_caching &&
                            raw_cpp_data_cache_file_path.as_path().is_file() {
        try!(load_json(&raw_cpp_data_cache_file_path))
      } else {
        log::info("Parsing C++ headers.");
        let cpp_data =
          try!(cpp_parser::run(cpp_parser::CppParserConfig {
                                 include_paths: include_dirs.clone(),
                                 framework_paths: framework_dirs.clone(),
                                 include_directives: Vec::from(env.config.include_directives()),
                                 target_include_paths: target_include_dirs,
                                 tmp_cpp_path: output_dir_path.with_added("1.cpp"),
                                 name_blacklist: Vec::from(env.config.cpp_parser_blocked_names()),
                                 flags: Vec::from(env.config.cpp_parser_flags()),
                               },
                               dependencies_cpp_data)
            .chain_err(|| "C++ parser failed"));
        if env.aggressive_caching {
          try!(save_json(&raw_cpp_data_cache_file_path, &cpp_data));
          log::info(format!("Raw C++ data is saved to file: {}",
                            raw_cpp_data_cache_file_path.display()));
        }
        cpp_data
      };
      for filter in env.config.cpp_data_filters() {
        try!(filter(&mut cpp_data).chain_err(|| "cpp_data_filter failed"));
      }
      log::info("Post-processing parse result.");
      try!(cpp_data.post_process());

      try!(save_json(&cpp_data_cache_file_path, &cpp_data));
      log::info(format!("C++ data is saved to file: {}",
                        cpp_data_cache_file_path.display()));
      cpp_data
    };

    let c_lib_name = format!("{}_c", &input_cargo_toml_data.name);
    let c_lib_path = c_lib_parent_path.with_added("source");
    let c_lib_tmp_path = c_lib_parent_path.with_added("source.new");
    if c_lib_tmp_path.as_path().exists() {
      try!(remove_dir_all(&c_lib_tmp_path));
    }
    try!(create_dir_all(&c_lib_tmp_path));
    log::info(format!("Generating C wrapper library ({}).", c_lib_name));

    let cpp_ffi_headers = try!(cpp_ffi_generator::run(&cpp_data,
                                                      c_lib_name.clone(),
                                                      env.config.cpp_ffi_generator_filters())
      .chain_err(|| "FFI generator failed"));

    let mut cpp_libs_for_shared_c_lib = Vec::new();
    if c_lib_is_shared {
      for lib in env.config.linked_libs() {
        cpp_libs_for_shared_c_lib.push(lib.clone());
      }
      for dep in &dependencies {
        for lib in &dep.rust_export_info.linked_libs {
          cpp_libs_for_shared_c_lib.push(lib.clone());
        }
      }
    }
    let code_gen = CppCodeGenerator::new(c_lib_name.clone(),
                                         c_lib_tmp_path.clone(),
                                         c_lib_is_shared,
                                         cpp_libs_for_shared_c_lib);
    let include_dirs_str = try!(include_dirs.iter()
      .map_if_ok(|x| -> Result<_> { Ok(try!(path_to_str(x)).to_string()) }));
    let framework_dirs_str = try!(framework_dirs.iter()
      .map_if_ok(|x| -> Result<_> { Ok(try!(path_to_str(x)).to_string()) }));
    try!(code_gen.generate_template_files(env.config.include_directives(),
                                          &include_dirs_str,
                                          &framework_dirs_str,
                                          env.config.cpp_compiler_flags()));
    try!(code_gen.generate_files(&cpp_ffi_headers));

    try!(move_files(&c_lib_tmp_path, &c_lib_path));

    log::info("Building C wrapper library.");
    let c_lib_build_path = c_lib_parent_path.with_added("build");
    try!(create_dir_all(&c_lib_build_path));
    try!(create_dir_all(&c_lib_install_path));

    try!(CppLibBuilder {
        cmake_source_dir: &c_lib_path,
        build_dir: &c_lib_build_path,
        install_dir: &c_lib_install_path,
        num_jobs: num_jobs,
        linker_env_library_dirs: if c_lib_is_shared {
          Some(&cpp_lib_dirs)
        } else {
          None
        },
        pipe_output: env.pipe_output,
      }
      .run()
      .chain_err(|| "C wrapper build failed")
      .into());

    let crate_new_path = output_dir_path.with_added(format!("{}.new", &input_cargo_toml_data.name));
    if crate_new_path.as_path().exists() {
      try!(remove_dir_all(&crate_new_path));
    }
    try!(create_dir_all(&crate_new_path));
    let rustfmt_config_path = source_dir_path.with_added("rustfmt.toml");
    let rust_config = rust_code_generator::RustCodeGeneratorConfig {
      crate_name: input_cargo_toml_data.name.clone(),
      crate_authors: input_cargo_toml_data.authors.clone(),
      crate_version: input_cargo_toml_data.version.clone(),
      output_path: crate_new_path.clone(),
      final_output_path: output_dir_path.clone(),
      template_path: source_dir_path.clone(),
      c_lib_name: c_lib_name,
      c_lib_is_shared: c_lib_is_shared,
      link_items: link_items,
      framework_dirs: framework_dirs_str,
      rustfmt_config_path: if rustfmt_config_path.as_path().exists() {
        Some(rustfmt_config_path)
      } else {
        None
      },
      dependencies: dependencies.iter()
        .map(|x| {
          RustCodeGeneratorDependency {
            crate_name: x.rust_export_info.crate_name.clone(),
            crate_path: x.path.clone(),
          }
        })
        .collect(),
    };
    let mut dependency_rust_types = Vec::new();
    for dep in &dependencies {
      dependency_rust_types.extend_from_slice(&dep.rust_export_info.rust_types);
    }
    log::info("Preparing Rust functions");
    let rust_data = try!(rust_generator::run(CppAndFfiData {
                                               cpp_data: cpp_data,
                                               cpp_ffi_headers: cpp_ffi_headers,
                                             },
                                             dependency_rust_types,
                                             rust_generator::RustGeneratorConfig {
                                               crate_name: input_cargo_toml_data.name.clone(),
                                               remove_qt_prefix: is_qt_library,
                                             })
      .chain_err(|| "Rust data generator failed"));
    log::info(format!("Generating Rust crate ({}).", &input_cargo_toml_data.name));
    try!(rust_code_generator::run(rust_config, &rust_data)
      .chain_err(|| "Rust code generator failed"));
    {
      let rust_export_path = output_dir_path.with_added("rust_export_info.json");
      try!(save_json(&rust_export_path,
                     &RustExportInfo {
                       crate_name: input_cargo_toml_data.name.clone(),
                       rust_types: rust_data.processed_types,
                       linked_libs: Vec::from(env.config.linked_libs()),
                       linked_frameworks: Vec::from(env.config.linked_frameworks()),
                     }));
      log::info(format!("Rust export info is saved to file: {}",
                        rust_export_path.display()));
    }

    for item in try!(read_dir(&crate_new_path)) {
      let item = try!(item);
      try!(move_files(&crate_new_path.with_added(item.file_name()),
                      &output_dir_path.with_added(item.file_name())));
    }
    try!(remove_dir(&crate_new_path));
  }


  // match env.invokation_method {
  // InvokationMethod::CommandLine => {
  // log::info("Compiling Rust crate.");
  // let mut all_cpp_lib_dirs = cpp_lib_dirs.clone();
  // if c_lib_is_shared {
  // all_cpp_lib_dirs.push(c_lib_lib_path.clone());
  // }
  // if output_dir_path.with_added("Cargo.lock").exists() {
  // try!(remove_file(output_dir_path.with_added("Cargo.lock")));
  // }
  // for cargo_cmd in &["build", "test", "doc"] {
  // let mut command = Command::new("cargo");
  // command.arg(cargo_cmd);
  // command.arg("--verbose");
  // command.arg(format!("-j{}", num_jobs));
  // command.current_dir(&output_dir_path);
  // if !all_cpp_lib_dirs.is_empty() {
  // for name in &["LIBRARY_PATH", "LD_LIBRARY_PATH", "LIB", "PATH"] {
  // let value = try!(add_env_path_item(name, all_cpp_lib_dirs.clone()));
  // command.env(name, value);
  // }
  // }
  // if !framework_dirs.is_empty() {
  // command.env("DYLD_FRAMEWORK_PATH",
  // try!(add_env_path_item("DYLD_FRAMEWORK_PATH", framework_dirs.clone())));
  // }
  // if is_msvc() && *cargo_cmd == "test" {
  // cargo doesn't pass this flag to rustc when it compiles qt_core,
  // so it's compiled with static std and the tests fail with
  // "cannot satisfy dependencies so `std` only shows up once" error.
  // command.env("RUSTFLAGS", "-C prefer-dynamic");
  // }
  // try!(run_command(&mut command, false, env.pipe_output)
  // .chain_err(|| "failed to build generated crate"));
  // }
  // log::info("Completed successfully.");
  // }
  // InvokationMethod::BuildScript => {
  println!("cargo:rustc-link-search={}",
           try!(path_to_str(&c_lib_lib_path)));
  for dir in &cpp_lib_dirs {
    println!("cargo:rustc-link-search=native={}", try!(path_to_str(dir)));
  }
  println!("cargo:cpp_to_rust_data_path={}",
           try!(path_to_str(&output_dir_path)));
  for dir in &framework_dirs {
    println!("cargo:rustc-link-search=framework={}",
             try!(path_to_str(dir)));
  }
  try!(create_file(output_dir_path.with_added("cpp_to_rust_completed")));
  Ok(())
}