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
extern crate num_cpus;

use config::Config;
use cpp_code_generator::CppCodeGenerator;
use cpp_ffi_generator;
use cpp_lib_builder::CppLibBuilder;
use cpp_parser;
use dependency_info::DependencyInfo;
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};
use log;
use qt_doc_parser::QtDocData;
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::env;

pub enum BuildProfile {
  Debug,
  Release,
}

pub use rust_code_generator::InvokationMethod;

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 fn run_from_build_script(config: Config) -> Result<()> {
  let mut dependency_paths = Vec::new();
  for (name, value) in 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));
      }
    }
  }
  run(BuildEnvironment {
    config: config,
    source_dir_path: PathBuf::from(try!(env::var("CARGO_MANIFEST_DIR")
      .chain_err(|| "failed to read required env var: CARGO_MANIFEST_DIR"))),
    output_dir_path: PathBuf::from(try!(env::var("OUT_DIR")
      .chain_err(|| "failed to read required env var: OUT_DIR"))),
    num_jobs: try!(env::var("NUM_JOBS").chain_err(|| "failed to read required env var: NUM_JOBS"))
      .parse()
      .ok(),
    build_profile: match try!(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,
  })
}



// 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() {
    // TODO: exclude GL if msvc in qt_gui build script
    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 !link_items.iter().any(|x| &x.name == &input_cargo_toml_data.links) {
    log::warning(format!("Value of 'links' field in Cargo.toml ({}) should be one of \
      linked libraries or frameworks ({}).",
                         input_cargo_toml_data.links,
                         link_items.iter().map(|x| &x.name).join(", ")));
  }

  // 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 qt_doc_data = if is_qt_library {
    // TODO: find a better way to specify doc source (#35)
    let env_var_name = format!("{}_DOC_DATA", "QT5CORE".to_uppercase());
    if let Ok(env_var_value) = env::var(&env_var_name) {
      log::info(format!("Loading Qt doc data from {}", &env_var_value));
      match QtDocData::new(&PathBuf::from(&env_var_value)) {
        Ok(r) => Some(r),
        Err(msg) => {
          log::warning(format!("Failed to load Qt doc data: {}", msg));
          None
        }
      }
    } else {
      log::warning(format!("Building without Qt doc data (no env var: {})",
                           env_var_name));
      None
    }
  } else {
    None
  };
  if !env.dependency_paths.is_empty() {
    log::info("Loading dependencies");
  }
  let dependencies: Vec<_> = try!(env.dependency_paths
    .iter()
    .map_if_ok(|path| DependencyInfo::load(&try!(canonicalize(path))))
    .chain_err(|| "failed to load dependency"));

  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 mut dependency_cpp_types = Vec::new();
  for dep in &dependencies {
    dependency_cpp_types.extend_from_slice(&dep.cpp_data.types);
  }
  let c_lib_is_shared = is_msvc();
  if output_dir_path.with_added("skip_processing").as_path().exists() {
    log::info("Processing skipped!");
  } else {
    let parse_result_cache_file_path = output_dir_path.with_added("cpp_data.json");
    let loaded_parse_result = if parse_result_cache_file_path.as_path().is_file() {
      match load_json(&parse_result_cache_file_path) {
        Ok(r) => {
          log::info(format!("C++ data is loaded from file: {}",
                            parse_result_cache_file_path.display()));
          Some(r)
        }
        Err(err) => {
          log::warning(format!("Failed to load C++ data: {}", err));
          err.discard_expected();
          None
        }
      }
    } else {
      None
    };

    let parse_result = if let Some(r) = loaded_parse_result {
      r
    } else {
      log::info("Parsing C++ headers.");
      let mut parse_result =
        try!(cpp_parser::run(cpp_parser::CppParserConfig {
                               include_dirs: include_dirs.clone(),
                               framework_dirs: framework_dirs.clone(),
                               include_directives: Vec::from(env.config.include_directives()),
                               target_include_dirs: target_include_dirs,
                               tmp_cpp_path: output_dir_path.with_added("1.cpp"),
                               name_blacklist: Vec::from(env.config.cpp_parser_blocked_names()),
                             },
                             &dependencies.iter().map(|x| &x.cpp_data).collect::<Vec<_>>())
          .chain_err(|| "C++ parser failed"));
      if let Some(filter) = env.config.cpp_data_filter() {
        try!(filter(&mut parse_result).chain_err(|| "cpp_data_filter failed"));
      }
      log::info("Post-processing parse result.");
      try!(parse_result.post_process(&dependencies.iter().map(|x| &x.cpp_data).collect::<Vec<_>>()));

      try!(save_json(&parse_result_cache_file_path, &parse_result));
      log::info(format!("Header parse result is saved to file: {}",
                        parse_result_cache_file_path.display()));
      parse_result
    };

    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(&parse_result,
                                                      c_lib_name.clone(),
                                                      env.config.cpp_ffi_generator_filter())
      .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));
    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(),
      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(cpp_ffi_generator::CppAndFfiData {
                                               cpp_data: parse_result,
                                               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,
                                               qt_doc_data: qt_doc_data,
                                             })
      .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)));
  }
  Ok(())
}