[librsvg: 1/2] Generate version.rs from librsvg/build.rs




commit 5554b63d5d2d1666c48f55609e1988cba353c065
Author: Sven Neumann <sven svenfoo org>
Date:   Thu Sep 24 09:03:23 2020 +0200

    Generate version.rs from librsvg/build.rs
    
    Parse the version numbers from configure.ac and create version.rs. This should
    work better with build_dir != src_dir.
    
    Based on code kindly suggested by user notriddle at https://users.rust-lang.org/

 .gitignore            |  1 -
 Cargo.lock            |  1 +
 Makefile.am           |  1 +
 configure.ac          |  1 -
 librsvg/.dirstamp     |  0
 librsvg/Cargo.toml    |  4 ++++
 librsvg/build.rs      | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++
 librsvg/c_api.rs      |  2 ++
 librsvg/lib.rs        |  1 -
 librsvg/version.rs.in | 10 --------
 10 files changed, 71 insertions(+), 13 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 8488a9c3..072ddc67 100644
--- a/.gitignore
+++ b/.gitignore
@@ -77,6 +77,5 @@ Rsvg-2.0.typelib
 librsvg-2.0.vapi
 librsvg-*.tar.xz
 librsvg-*.tar.bz2
-librsvg/version.rs
 rust/target
 _rsvg_dummy.c
diff --git a/Cargo.lock b/Cargo.lock
index fecf2566..b56e69fa 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -699,6 +699,7 @@ dependencies = [
  "glib-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "gobject-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "libc 0.2.77 (registry+https://github.com/rust-lang/crates.io-index)",
+ "regex 1.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
  "rgb 0.8.25 (registry+https://github.com/rust-lang/crates.io-index)",
  "rsvg_internals 0.0.1",
  "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Makefile.am b/Makefile.am
index 9ca857cf..cdc509d1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -122,6 +122,7 @@ LIBRSVG_CRATE_SRC =                                                 \
 
 LIBRSVG_C_API_SRC =                                            \
        librsvg/Cargo.toml                                      \
+       librsvg/build.rs                                        \
        librsvg/c_api.rs                                        \
        librsvg/color_utils.rs                                  \
        librsvg/dpi.rs                                          \
diff --git a/configure.ac b/configure.ac
index 8857f249..eafae701 100644
--- a/configure.ac
+++ b/configure.ac
@@ -380,7 +380,6 @@ AC_SUBST([AM_LDFLAGS])
 
 AC_CONFIG_FILES([
 librsvg/librsvg-features.h
-librsvg/version.rs
 Makefile
 librsvg-zip
 gdk-pixbuf-loader/Makefile
diff --git a/librsvg/.dirstamp b/librsvg/.dirstamp
new file mode 100644
index 00000000..e69de29b
diff --git a/librsvg/Cargo.toml b/librsvg/Cargo.toml
index edb17da9..e9ab1519 100644
--- a/librsvg/Cargo.toml
+++ b/librsvg/Cargo.toml
@@ -3,6 +3,7 @@ name = "librsvg_c_api"
 version = "0.0.1"
 authors = ["Federico Mena Quintero <federico gnome org>"]
 workspace = "../"
+build = "build.rs"
 edition = "2018"
 
 [lib]
@@ -30,6 +31,9 @@ url = "2"
 [dev-dependencies]
 criterion = "0.3"
 
+[build-dependencies]
+regex = "1.3.9"
+
 [[bench]]
 name = "pixbuf_from_surface"
 harness = false
diff --git a/librsvg/build.rs b/librsvg/build.rs
new file mode 100644
index 00000000..36e595d0
--- /dev/null
+++ b/librsvg/build.rs
@@ -0,0 +1,63 @@
+use regex::Regex;
+use std::env;
+use std::fs::File;
+use std::io::prelude::*;
+use std::io::BufReader;
+use std::path::Path;
+
+fn main() {
+    let mut major = None;
+    let mut minor = None;
+    let mut micro = None;
+
+    {
+        let file = File::open("../configure.ac")
+            .expect("builds must take place within the librsvg source tree");
+
+        let major_regex = Regex::new(r#"^m4_define\(\[rsvg_major_version\],\[(\d+)\]\)"#).unwrap();
+        let minor_regex = Regex::new(r#"^m4_define\(\[rsvg_minor_version\],\[(\d+)\]\)"#).unwrap();
+        let micro_regex = Regex::new(r#"^m4_define\(\[rsvg_micro_version\],\[(\d+)\]\)"#).unwrap();
+
+        for line in BufReader::new(file).lines() {
+            if let Ok(line) = line {
+                if let Some(nums) = major_regex.captures(&line) {
+                    major = Some(String::from(
+                        nums.get(1).expect("major_regex matched once").as_str(),
+                    ));
+                } else if let Some(nums) = minor_regex.captures(&line) {
+                    minor = Some(String::from(
+                        nums.get(1).expect("minor_regex matched once").as_str(),
+                    ));
+                } else if let Some(nums) = micro_regex.captures(&line) {
+                    micro = Some(String::from(
+                        nums.get(1).expect("micro_regex matched once").as_str(),
+                    ));
+                }
+            }
+        }
+    }
+
+    let output = Path::new(&env::var("OUT_DIR").unwrap()).join("version.rs");
+    let mut file = File::create(output).expect("open version.rs for writing");
+    file.write_all(
+        format!(
+            r#"
+use std::os::raw::c_uint;
+
+#[no_mangle]
+pub static rsvg_major_version: c_uint = {};
+
+#[no_mangle]
+pub static rsvg_minor_version: c_uint = {};
+
+#[no_mangle]
+pub static rsvg_micro_version: c_uint = {};
+"#,
+            major.expect("major version is set"),
+            minor.expect("minor version is set"),
+            micro.expect("micro version is set")
+        )
+        .as_bytes(),
+    )
+    .expect("write version.rs");
+}
diff --git a/librsvg/c_api.rs b/librsvg/c_api.rs
index 2a11a710..b58d5780 100644
--- a/librsvg/c_api.rs
+++ b/librsvg/c_api.rs
@@ -40,6 +40,8 @@ use crate::dpi::Dpi;
 use crate::messages::{rsvg_g_critical, rsvg_g_warning};
 use crate::pixbuf_utils::{empty_pixbuf, pixbuf_from_surface};
 
+include!(concat!(env!("OUT_DIR"), "/version.rs"));
+
 mod handle_flags {
     // The following is entirely stolen from the auto-generated code
     // for GBindingFlags, from gtk-rs/glib/src/gobject/auto/flags.rs
diff --git a/librsvg/lib.rs b/librsvg/lib.rs
index 7a258ceb..8e239c3b 100644
--- a/librsvg/lib.rs
+++ b/librsvg/lib.rs
@@ -52,4 +52,3 @@ mod c_api;
 mod color_utils;
 mod dpi;
 pub mod pixbuf_utils;
-mod version;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]