Skip to content

Commit

Permalink
Squash warnings, scope stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
MaulingMonkey committed Dec 30, 2019
1 parent 82db2a0 commit 85585ed
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 117 deletions.
1 change: 1 addition & 0 deletions jni-bindgen/src/config/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ impl File {

/// Search the current directory - or failing that, it's ancestors - until we find "jni-bindgen.toml" or reach the
/// root of the filesystem and cannot continue.
#[allow(dead_code)]
pub fn from_current_directory() -> io::Result<FileWithContext> {
Self::from_directory(std::env::current_dir()?.as_path())
}
Expand Down
1 change: 1 addition & 0 deletions jni-bindgen/src/java/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub struct Field {
_incomplete: (),
}

#[allow(dead_code)]
impl Field {
pub fn new(flags: Flags, name: String, descriptor: String) -> io::Result<Self> {
Descriptor::from_str(descriptor.as_str())?;
Expand Down
1 change: 1 addition & 0 deletions jni-bindgen/src/java/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ impl<'a> Descriptor<'a> {
Ok(Descriptor { string: desc, end_paren })
}

#[allow(dead_code)]
pub fn as_str(&self) -> &'a str { self.string }
pub fn return_type(&self) -> Type<'a> { Type::from_str(&self.string[(1+self.end_paren)..]).unwrap() } // Already validated in Descriptor::new
pub fn arguments(&self) -> ArgumentsIter { ArgumentsIter(&self.string[1..self.end_paren]) }
Expand Down
239 changes: 122 additions & 117 deletions jni-bindgen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
use bugsalot::debugger;

use clap::load_yaml;

use std::fs::{File};
use std::io::{self, BufRead, BufReader, BufWriter, Write};

use std::ops::*;
use std::path::*;
use std::process::exit;



#[macro_use] mod java;

/// Configuration formats for invoking jni_bindgen
Expand Down Expand Up @@ -88,125 +75,143 @@ use run::RunResult;


fn main() {
std::panic::set_hook(Box::new(|panic|{ bugsalot::bug!("{:?}", panic); }));
// jni_bindgen::run(jni_bindgen::config::toml::File::from_current_directory().unwrap()).unwrap(); // Old legacy behavior

let yaml = load_yaml!("../cli.yml");
let matches = clap::App::from_yaml(yaml).get_matches();

let _help = matches.is_present("help");
let directory : &Path = Path::new(matches.value_of("directory").unwrap_or("../jni-android-sys"));
let _verbose = matches.is_present("verbose");
let min_api_level : i32 = matches.value_of("min-api-level").unwrap_or( "7").parse().expect("--min-api-level must specify an integer version");
let max_api_level : i32 = matches.value_of("max-api-level").unwrap_or("28").parse().expect("--max-api-level must specify an integer version");

if min_api_level < 7 {
eprintln!("\
WARNING: Untested api level {} (<7).\n\
If you've found where I can grab such an early version of the Android SDK/APIs,\n\
please comment on / reopen https://github.com/MaulingMonkey/jni-bindgen/issues/10 !",
min_api_level
);
}
entry::main();
}

let api_levels = min_api_level..=max_api_level;
mod entry {
use crate::*;
use bugsalot::debugger;

use clap::load_yaml;

use std::fs::{File};
use std::io::{self, BufRead, BufReader, BufWriter, Write};

use std::ops::*;
use std::path::*;
use std::process::exit;

pub fn main() {
std::panic::set_hook(Box::new(|panic|{ bugsalot::bug!("{:?}", panic); }));
// jni_bindgen::run(jni_bindgen::config::toml::File::from_current_directory().unwrap()).unwrap(); // Old legacy behavior

let yaml = load_yaml!("../cli.yml");
let matches = clap::App::from_yaml(yaml).get_matches();

let _help = matches.is_present("help");
let directory : &Path = Path::new(matches.value_of("directory").unwrap_or("../jni-android-sys"));
let _verbose = matches.is_present("verbose");
let min_api_level : i32 = matches.value_of("min-api-level").unwrap_or( "7").parse().expect("--min-api-level must specify an integer version");
let max_api_level : i32 = matches.value_of("max-api-level").unwrap_or("28").parse().expect("--max-api-level must specify an integer version");

if min_api_level < 7 {
eprintln!("\
WARNING: Untested api level {} (<7).\n\
If you've found where I can grab such an early version of the Android SDK/APIs,\n\
please comment on / reopen https://github.com/MaulingMonkey/jni-bindgen/issues/10 !",
min_api_level
);
}

let subcommand = matches.subcommand_name().unwrap_or("generate");
let api_levels = min_api_level..=max_api_level;

match subcommand {
"generate" => {
let mut config_file = config::toml::File::from_directory(directory).unwrap();
let subcommand = matches.subcommand_name().unwrap_or("generate");

let mut result = None;
for api_level in api_levels.clone() {
let sdk_android_jar = if std::env::var_os("ANDROID_HOME").is_some() {
format!("%ANDROID_HOME%/platforms/android-{}/android.jar", api_level)
} else if cfg!(windows) {
format!("%LOCALAPPDATA%/Android/Sdk/platforms/android-{}/android.jar", api_level)
} else {
panic!("ANDROID_HOME not defined and not automatically inferrable on this platform");
};
match subcommand {
"generate" => {
let mut config_file = config::toml::File::from_directory(directory).unwrap();

config_file.file.input.files.clear();
config_file.file.input.files.push(PathBuf::from(sdk_android_jar));
config_file.file.output.path = PathBuf::from(format!("src/generated/api-level-{}.rs", api_level));
result = run(config_file.clone()).ok();
}
let result = result.unwrap();
let mut result = None;
for api_level in api_levels.clone() {
let sdk_android_jar = if std::env::var_os("ANDROID_HOME").is_some() {
format!("%ANDROID_HOME%/platforms/android-{}/android.jar", api_level)
} else if cfg!(windows) {
format!("%LOCALAPPDATA%/Android/Sdk/platforms/android-{}/android.jar", api_level)
} else {
panic!("ANDROID_HOME not defined and not automatically inferrable on this platform");
};

config_file.file.input.files.clear();
config_file.file.input.files.push(PathBuf::from(sdk_android_jar));
config_file.file.output.path = PathBuf::from(format!("src/generated/api-level-{}.rs", api_level));
result = run(config_file.clone()).ok();
}
let result = result.unwrap();

if let Err(e) = generate_toml(directory, api_levels.clone(), &result) {
eprintln!("ERROR: Failed to regenerate Cargo.toml:\n {:?}", e);
if let Err(e) = generate_toml(directory, api_levels.clone(), &result) {
eprintln!("ERROR: Failed to regenerate Cargo.toml:\n {:?}", e);
exit(1);
}
},
"verify" => {
eprintln!("verify not yet implemented");
debugger::break_if_attached();
exit(1);
}
},
"verify" => {
eprintln!("verify not yet implemented");
debugger::break_if_attached();
exit(1);
},
unknown => {
eprintln!("Unexpected subcommand: {}", unknown);
debugger::break_if_attached();
exit(1);
},
},
unknown => {
eprintln!("Unexpected subcommand: {}", unknown);
debugger::break_if_attached();
exit(1);
},
}
}
}

fn generate_toml(directory: &Path, api_levels: RangeInclusive<i32>, result: &RunResult) -> io::Result<()> {
// XXX: Check that Cargo.toml is marked as generated

let template = BufReader::new(File::open(directory.join("Cargo.toml.template"))?);
let mut out = BufWriter::new(File::create(directory.join("Cargo.toml"))?);

writeln!(out, "# WARNING: This file was autogenerated by jni-bindgen. Any changes to this file may be lost!!!")?;
writeln!(out, "")?;

for line in template.lines() {
let line = line?;
let line = line.trim_end_matches(|ch| ch == '\n' || ch == '\r');
match line {
"# PLACEHOLDER:FEATURES:api-level-NN" => {
writeln!(out, "{}:BEGIN", line)?;
for api_level in api_levels.clone() {
write!(out, "api-level-{} = [", api_level)?;
if api_level > *api_levels.start() {
write!(out, "\"api-level-{}\"", api_level-1)?;
fn generate_toml(directory: &Path, api_levels: RangeInclusive<i32>, result: &RunResult) -> io::Result<()> {
// XXX: Check that Cargo.toml is marked as generated

let template = BufReader::new(File::open(directory.join("Cargo.toml.template"))?);
let mut out = BufWriter::new(File::create(directory.join("Cargo.toml"))?);

writeln!(out, "# WARNING: This file was autogenerated by jni-bindgen. Any changes to this file may be lost!!!")?;
writeln!(out, "")?;

for line in template.lines() {
let line = line?;
let line = line.trim_end_matches(|ch| ch == '\n' || ch == '\r');
match line {
"# PLACEHOLDER:FEATURES:api-level-NN" => {
writeln!(out, "{}:BEGIN", line)?;
for api_level in api_levels.clone() {
write!(out, "api-level-{} = [", api_level)?;
if api_level > *api_levels.start() {
write!(out, "\"api-level-{}\"", api_level-1)?;
}
writeln!(out, "]")?;
}
writeln!(out, "]")?;
}
writeln!(out, "{}:END", line)?;
},
"# PLACEHOLDER:FEATURES:sharded-api" => {
writeln!(out, "{}:BEGIN", line)?;
for (feature, dependencies) in result.features.iter() {
write!(out, "{:?} = [", feature)?;
for (idx, dependency) in dependencies.iter().enumerate() {
if idx != 0 {
write!(out, ", ")?;
writeln!(out, "{}:END", line)?;
},
"# PLACEHOLDER:FEATURES:sharded-api" => {
writeln!(out, "{}:BEGIN", line)?;
for (feature, dependencies) in result.features.iter() {
write!(out, "{:?} = [", feature)?;
for (idx, dependency) in dependencies.iter().enumerate() {
if idx != 0 {
write!(out, ", ")?;
}
write!(out, "{:?}", dependency)?;
}
write!(out, "{:?}", dependency)?;
writeln!(out, "]")?;
}
writeln!(out, "]")?;
}

// Wildcard feature "*". While it's tempting to make this depend on all other features, this
// causes problems on windows where we run into command line length limits invoking rustc.
writeln!(out, "\"all\" = []")?;
writeln!(out, "{}:END", line)?;
},
"# PLACEHOLDER:FEATURES:docs.rs" => {
writeln!(out, "{}:BEGIN", line)?;
writeln!(out, "features = [\"all\", \"api-level-{}\", \"force-define\"]", api_levels.end())?;
writeln!(out, "{}:END", line)?;
}
line => {
if line.starts_with("# PLACEHOLDER:") {
eprintln!("WARNING: Unexpected Cargo.toml placeholder:\n {}", line);
// Wildcard feature "*". While it's tempting to make this depend on all other features, this
// causes problems on windows where we run into command line length limits invoking rustc.
writeln!(out, "\"all\" = []")?;
writeln!(out, "{}:END", line)?;
},
"# PLACEHOLDER:FEATURES:docs.rs" => {
writeln!(out, "{}:BEGIN", line)?;
writeln!(out, "features = [\"all\", \"api-level-{}\", \"force-define\"]", api_levels.end())?;
writeln!(out, "{}:END", line)?;
}
line => {
if line.starts_with("# PLACEHOLDER:") {
eprintln!("WARNING: Unexpected Cargo.toml placeholder:\n {}", line);
}
writeln!(out, "{}", line)?;
}
writeln!(out, "{}", line)?;
}
}
}

Ok(())
Ok(())
}
}

0 comments on commit 85585ed

Please sign in to comment.