Skip to content

Commit

Permalink
Add rust url crate. (denoland#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jul 10, 2018
1 parent e269d97 commit d160de7
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 18 deletions.
13 changes: 9 additions & 4 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ rust_executable("deno") {
]
}

rust_component("handlers") {
rust_staticlib("handlers") {
source_root = "src/handlers.rs"
extern = [ "$rust_build:libc" ]
extern = [
"$rust_build:libc",
"$rust_build:url",
]
}

rust_test("handlers_test") {
source_root = "src/handlers.rs"
extern = [ "$rust_build:libc" ]
extern = [
"$rust_build:libc",
"$rust_build:url",
]
}

executable("deno_cc") {
Expand All @@ -39,7 +45,6 @@ executable("deno_cc") {
":handlers",
":libdeno",
":msg_cpp",
"//build_extra/rust:stdlib",
]
configs += [ ":deno_config" ]
}
Expand Down
58 changes: 46 additions & 12 deletions build_extra/rust/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,23 +1,57 @@
import("rust.gni")

# Dependencies between third party crates is mapped out here manually. This is
# not so difficult and having it be tedious to add dependencies might help us
# avoid dependency hell later on.
# Versioning for third party rust crates is controlled in //gclient_config.py
# TODO(ry) Use Cargo for versioning?

# By compiling an empty file as crate-type=staticlib we get all the code
# for the rust stdlib, which are not included in the object file outputs
# of other libs.
rust_component("stdlib") {
crate_type = "staticlib"
# TODO(ry) This is not used and maybe should be removed along with empty.rs.
rust_staticlib("stdlib") {
source_root = "empty.rs"
if (current_os == "mac") {
libs = [ "resolv" ]
}
if (current_os == "win") {
libs = [ "userenv.lib" ]
}
}

crates = "//third_party/rust_crates"

rust_component("libc") {
source_root = "//third_party/rust_crates/libc/src/lib.rs"
cfg = [
"feature=\"default\"",
"feature=\"use_std\"",
source_root = "$crates/libc/src/lib.rs"
cfg = [ "feature=\"use_std\"" ]
}

rust_component("url") {
source_root = "$crates/url/src/lib.rs"
extern = [
":matches",
":idna",
":percent_encoding",
]
}

rust_component("percent_encoding") {
source_root = "$crates/url/percent_encoding/lib.rs"
}

rust_component("matches") {
source_root = "$crates/rust-std-candidates/matches/lib.rs"
}

rust_component("idna") {
source_root = "$crates/url/idna/src/lib.rs"
extern = [
":matches",
":unicode_bidi",
":unicode_normalization",
]
}

rust_component("unicode_bidi") {
source_root = "$crates/unicode-bidi/src/lib.rs"
extern = [ ":matches" ]
}

rust_component("unicode_normalization") {
source_root = "$crates/unicode-normalization/src/lib.rs"
}
26 changes: 26 additions & 0 deletions build_extra/rust/rust.gni
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ template("run_rustc") {
"--extern",
"$name=" + rebase_path(rlib, root_build_dir),
]

# This is needed for transitive dependencies.
args += [
"-L",
"dependency=" + rebase_path(dir, root_build_dir),
]
}
}
}
Expand Down Expand Up @@ -152,6 +158,26 @@ template("rust_component") {
}
}

template("rust_staticlib") {
rust_component(target_name) {
crate_type = "staticlib"
forward_variables_from(invoker,
[
"crate_name",
"extern",
"cfg",
"source_root",
"testonly",
])
if (current_os == "mac") {
libs = [ "resolv" ]
}
if (current_os == "win") {
libs = [ "userenv.lib" ]
}
}
}

template("rust_executable") {
bin_name = target_name + "_bin"
bin_label = ":" + bin_name
Expand Down
23 changes: 23 additions & 0 deletions gclient_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,27 @@
'https://github.com/rust-lang/libc.git@8a85d662b90c14d458bc4ae9521a05564e20d7ae',
'name':
'rust_crates/libc'
}, {
'url':
'https://github.com/servo/rust-url.git@fbe5e50316105482dcd53d2dabb148c445a5f4cd',
'name':
'rust_crates/url'
}, {
# Needed for url.
'url':
'https://github.com/SimonSapin/rust-std-candidates.git@88a017b79ea146d6fde389c96982fc7518ba98bf',
'name':
'rust_crates/rust-std-candidates'
}, {
# Needed for url.
'url':
'https://github.com/servo/unicode-bidi.git@32c81729db0ac90289ebeca9e0d4886f264e724d',
'name':
'rust_crates/unicode-bidi'
}, {
# Needed for url.
'url':
'https://github.com/behnam/rust-unicode-normalization.git@3898e77b110246cb7243bf29b896c58d8975304a',
'name':
'rust_crates/unicode-normalization'
}]
7 changes: 5 additions & 2 deletions src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
extern crate libc;
extern crate url;

use libc::c_char;
use std::ffi::CStr;
use url::Url;

fn string_from_ptr(ptr: *const c_char) -> String {
let cstr = unsafe { CStr::from_ptr(ptr as *const i8) };
String::from(cstr.to_str().unwrap())
}

#[test]
fn test_example() {
assert_eq!(2 + 2, 4);
fn test_url() {
let issue_list_url = Url::parse("https://github.com/rust-lang").unwrap();
assert!(issue_list_url.scheme() == "https");
}

#[no_mangle]
Expand Down

0 comments on commit d160de7

Please sign in to comment.