From d160de7f445e5d79883f2e8d97584b8afd009cc7 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 10 Jul 2018 14:56:12 -0400 Subject: [PATCH] Add rust url crate. (#355) --- BUILD.gn | 13 ++++++--- build_extra/rust/BUILD.gn | 58 +++++++++++++++++++++++++++++++-------- build_extra/rust/rust.gni | 26 ++++++++++++++++++ gclient_config.py | 23 ++++++++++++++++ src/handlers.rs | 7 +++-- 5 files changed, 109 insertions(+), 18 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 6aec7e536c..2027562af9 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -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") { @@ -39,7 +45,6 @@ executable("deno_cc") { ":handlers", ":libdeno", ":msg_cpp", - "//build_extra/rust:stdlib", ] configs += [ ":deno_config" ] } diff --git a/build_extra/rust/BUILD.gn b/build_extra/rust/BUILD.gn index 0889ad8403..4aeac67e56 100644 --- a/build_extra/rust/BUILD.gn +++ b/build_extra/rust/BUILD.gn @@ -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" +} diff --git a/build_extra/rust/rust.gni b/build_extra/rust/rust.gni index 4c6b4ddd61..095904d4b3 100644 --- a/build_extra/rust/rust.gni +++ b/build_extra/rust/rust.gni @@ -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), + ] } } } @@ -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 diff --git a/gclient_config.py b/gclient_config.py index 28c8b8d73e..9f87d0f979 100644 --- a/gclient_config.py +++ b/gclient_config.py @@ -39,4 +39,27 @@ solutions = [{ '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' }] diff --git a/src/handlers.rs b/src/handlers.rs index 2b8e51602e..c826af7c0e 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,9 +1,11 @@ // Copyright 2018 Ryan Dahl // 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) }; @@ -11,8 +13,9 @@ fn string_from_ptr(ptr: *const c_char) -> String { } #[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]