Skip to content

Commit

Permalink
Fix Windows build and static linking
Browse files Browse the repository at this point in the history
  • Loading branch information
kleisauke authored and Federico Mena Quintero committed Jul 1, 2019
1 parent fd24ae3 commit e310e36
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
29 changes: 21 additions & 8 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ NULL =
BUILT_SOURCES =

lib_LTLIBRARIES = librsvg-@RSVG_API_MAJOR_VERSION@.la
noinst_LTLIBRARIES = librsvg_internals.la

bin_PROGRAMS = rsvg-convert
if HAVE_GTK_3
Expand All @@ -25,7 +26,7 @@ librsvg_@RSVG_API_MAJOR_VERSION@_la_SOURCES = \
librsvg/rsvg.h \
$(NULL)

RUST_SRC = \
librsvg_internals_la_SOURCES = \
Cargo.toml \
rsvg_internals/Cargo.toml \
rsvg_internals/build.rs \
Expand Down Expand Up @@ -147,16 +148,25 @@ cargo_verbose_ = $(cargo_verbose_$(AM_DEFAULT_VERBOSITY))
cargo_verbose_0 =
cargo_verbose_1 = --verbose

RUST_LIB=@abs_top_builddir@/target/@RUST_TARGET_SUBDIR@/librsvg_internals.a
RUST_LIB=@abs_top_builddir@/.libs/librsvg_internals.a
CARGO_TARGET_DIR=@abs_top_builddir@/target

LIBRSVG_BUILD_DIR=@abs_top_builddir@
LIBRSVG_TARGET_DIR=@abs_top_builddir@/target/@RUST_TARGET_SUBDIR@

check-local:
cd $(srcdir) && \
CARGO_TARGET_DIR=$(CARGO_TARGET_DIR) $(CARGO) --locked test $(CARGO_VERBOSE) $(CARGO_TARGET_ARGS) $(CARGO_RELEASE_ARGS)
CARGO_TARGET_DIR=$(CARGO_TARGET_DIR) \
LIBRSVG_BUILD_DIR=$(LIBRSVG_BUILD_DIR) \
LIBRSVG_TARGET_DIR=$(LIBRSVG_TARGET_DIR) \
$(CARGO) --locked test $(CARGO_VERBOSE) $(CARGO_TARGET_ARGS) $(CARGO_RELEASE_ARGS)

clean-local:
cd $(top_srcdir) && \
CARGO_TARGET_DIR=$(CARGO_TARGET_DIR) $(CARGO) clean
CARGO_TARGET_DIR=$(CARGO_TARGET_DIR) \
LIBRSVG_BUILD_DIR=$(LIBRSVG_BUILD_DIR) \
LIBRSVG_TARGET_DIR=$(LIBRSVG_TARGET_DIR) \
$(CARGO) clean

distcheck-hook:
if test -z "${DESTDIR}"; \
Expand All @@ -171,12 +181,15 @@ dist-hook:
mkdir .cargo && \
cp cargo-vendor-config .cargo/config)

$(RUST_LIB): $(RUST_SRC)
librsvg_internals.la: $(librsvg_internals_la_SOURCES)
+cd $(top_srcdir)/rsvg_internals && \
PKG_CONFIG_ALLOW_CROSS=1 \
PKG_CONFIG='$(PKG_CONFIG)' \
CARGO_TARGET_DIR=$(CARGO_TARGET_DIR) \
$(CARGO) --locked build $(CARGO_VERBOSE) $(CARGO_TARGET_ARGS) $(CARGO_RELEASE_ARGS) --features "c-library"
LIBRSVG_BUILD_DIR=$(LIBRSVG_BUILD_DIR) \
LIBRSVG_TARGET_DIR=$(LIBRSVG_TARGET_DIR) \
$(CARGO) --locked build $(CARGO_VERBOSE) $(CARGO_TARGET_ARGS) $(CARGO_RELEASE_ARGS) --features "c-library" \
&& $(AR) d $(RUST_LIB) clzsi2.o # HACK: https://github.com/rust-lang/rust/issues/58277

librsvg_@RSVG_API_MAJOR_VERSION@_la_CPPFLAGS = \
-I$(top_srcdir) \
Expand All @@ -202,7 +215,7 @@ librsvg_@RSVG_API_MAJOR_VERSION@_la_LDFLAGS = \
$(AM_LDFLAGS)

librsvg_@RSVG_API_MAJOR_VERSION@_la_LIBADD = \
$(RUST_LIB) \
librsvg_internals.la \
$(LIBRSVG_LIBS) \
$(LIBM) \
$(DLOPEN_LIBS)
Expand Down Expand Up @@ -270,7 +283,7 @@ dist_doc_DATA = \
code-of-conduct.md

EXTRA_DIST = \
$(RUST_SRC) \
$(librsvg_internals_la_SOURCES) \
$(RUST_EXTRA) \
$(LIBRSVG_CRATE_SOURCES) \
librsvg.doap \
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ AC_CHECK_PROGS(RUSTC, [rustc], [no])
AS_IF(test x$RUSTC = xno,
AC_MSG_ERROR([rustc is required. Please install the Rust toolchain from https://www.rust-lang.org/])
)
AC_CHECK_PROGS(AR, ar)

dnl Minimum version of rustc that we support
dnl If you change this, please update COMPILING.md
Expand Down
50 changes: 50 additions & 0 deletions rsvg_internals/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::env;
use std::fs;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::os::unix::fs::symlink;
use std::path::Path;
use std::path::PathBuf;

fn main() {
generate_srgb_tables();
generate_convenience_lib().unwrap();
}

/// Converts an sRGB color value to a linear sRGB color value (undoes the gamma correction).
Expand Down Expand Up @@ -63,3 +67,49 @@ fn generate_srgb_tables() {
print_table(&mut file, "LINEARIZE", &linearize_table);
print_table(&mut file, "UNLINEARIZE", &unlinearize_table);
}

/// Generate libtool archive file librsvg_internals.la
/// From: https://docs.rs/libtool/0.1.1/libtool/
pub fn generate_convenience_lib() -> std::io::Result<()> {
let target = env::var("TARGET").expect("TARGET was not set");
let build_dir = env::var("LIBRSVG_BUILD_DIR").expect("LIBRSVG_BUILD_DIR was not set");
let target_dir = env::var("LIBRSVG_TARGET_DIR").expect("LIBRSVG_TARGET_DIR was not set");
let libs_dir = format!("{}/.libs", build_dir);
let libs_path = PathBuf::from(&libs_dir);
let la_path = PathBuf::from(format!("{}/librsvg_internals.la", build_dir));
let rust_lib = if target.contains("windows") {
/* https://github.com/rust-lang/rust/issues/43749 */
"rsvg_internals.lib"
} else {
"librsvg_internals.a"
};
let old_lib_path = PathBuf::from(format!("{}/{}", target_dir, rust_lib));
let new_lib_path = PathBuf::from(format!("{}/librsvg_internals.a", libs_dir));

match fs::create_dir_all(&libs_path) {
Ok(()) => println!("libs_path created"),
_ => panic!("Failed to create libs_path"),
}

if la_path.exists() {
fs::remove_file(&la_path)?;
}

/* PathBuf.exists() traverses symlinks so just try and remove it */
match fs::remove_file(&new_lib_path) {
Ok(_v) => {},
Err(e) => println!("Error removing symlink: {:?}", e),
}

let mut file = File::create(&la_path).unwrap();
writeln!(file, "# librsvg_internals.la - a libtool library file")?;
writeln!(file, "# Generated by libtool-rust")?;
writeln!(file, "dlname=''")?;
writeln!(file, "library_names=''")?;
writeln!(file, "old_library='librsvg_internals.a'")?;
writeln!(file, "inherited_linker_flags=''")?;
writeln!(file, "installed=no")?;
writeln!(file, "shouldnotlink=no")?;
symlink(&old_lib_path, &new_lib_path)?;
Ok(())
}
2 changes: 1 addition & 1 deletion rsvg_internals/src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ macro_rules! compute_value {
impl SpecifiedValues {
#[cfg_attr(rustfmt, rustfmt_skip)]
pub fn set_parsed_property(&mut self, prop: &ParsedProperty) {
use ParsedProperty::*;
use crate::properties::ParsedProperty::*;

use crate::properties as p;

Expand Down

0 comments on commit e310e36

Please sign in to comment.