Skip to content

Commit

Permalink
Allow headers outside standard path (r-quantities#196)
Browse files Browse the repository at this point in the history
* If using udunits2/udunits2.h, define UDUNITS2_DIR=1 in Makevars and units.h.in

* Include udunits2.h from udunits2 directory if UDUNITS2_DIR != 0

* Update configure with autoreconf

* Define UDUNITS2_DIR=0 to include <udunits2.h> on Windows

* Restore units.h and pass UDUNITS2_DIR from Makevars

* remove unnecessary inst/include/units.h
  • Loading branch information
mjwoods authored and Enchufa2 committed Mar 10, 2019
1 parent b4fde6b commit 7dc9be3
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 48 deletions.
9 changes: 5 additions & 4 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ ac_includes_default="\
#endif"

ac_subst_vars='LTLIBOBJS
UDUNITS2_DIR
UD_CPPFLAGS
ac_ct_CC
CFLAGS
Expand Down Expand Up @@ -3791,12 +3792,12 @@ CPPFLAGS="${UD_CPPFLAGS} ${CPPFLAGS}"

ac_fn_cxx_check_header_mongrel "$LINENO" "udunits2.h" "ac_cv_header_udunits2_h" "$ac_includes_default"
if test "x$ac_cv_header_udunits2_h" = xyes; then :

UDUNITS2_DIR=0
else

ac_fn_cxx_check_header_mongrel "$LINENO" "udunits2/udunits2.h" "ac_cv_header_udunits2_udunits2_h" "$ac_includes_default"
ac_fn_cxx_check_header_mongrel "$LINENO" "udunits2/udunits2.h" "ac_cv_header_udunits2_udunits2_h" "$ac_includes_default"
if test "x$ac_cv_header_udunits2_udunits2_h" = xyes; then :
UD_CPPFLAGS="${UD_CPPFLAGS} -I/usr/include/udunits2"
UDUNITS2_DIR=1
else
UD_ERROR="udunits2.h was not found"
fi
Expand All @@ -3805,7 +3806,6 @@ fi
fi



{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for ut_read_xml in -ludunits2" >&5
$as_echo_n "checking for ut_read_xml in -ludunits2... " >&6; }
if ${ac_cv_lib_udunits2_ut_read_xml+:} false; then :
Expand Down Expand Up @@ -3870,6 +3870,7 @@ See \`config.log' for more details" "$LINENO" 5; } fi
UD_CPPFLAGS="${UD_CPPFLAGS} ${CPPFLAGS}"



ac_config_files="$ac_config_files src/Makevars"

cat >confcache <<\_ACEOF
Expand Down
9 changes: 4 additions & 5 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ fi

CPPFLAGS="${UD_CPPFLAGS} ${CPPFLAGS}"

AC_CHECK_HEADER(udunits2.h, [], [
AC_CHECK_HEADER(udunits2/udunits2.h,
UD_CPPFLAGS="${UD_CPPFLAGS} -I/usr/include/udunits2",
UD_ERROR="udunits2.h was not found") ])

AC_CHECK_HEADER(udunits2.h, UDUNITS2_DIR=0, [
AC_CHECK_HEADER(udunits2/udunits2.h, UDUNITS2_DIR=1,
UD_ERROR="udunits2.h was not found") ])
AC_CHECK_LIB(udunits2, ut_read_xml,
LIBS="${LIBS} -ludunits2",
UD_ERROR="libudunits2.so was not found")
Expand All @@ -78,5 +76,6 @@ if test "${UD_ERROR}" != "" ; then AC_MSG_FAILURE([
UD_CPPFLAGS="${UD_CPPFLAGS} ${CPPFLAGS}"
AC_SUBST([LIBS])
AC_SUBST([UD_CPPFLAGS])
AC_SUBST([UDUNITS2_DIR])
AC_CONFIG_FILES([src/Makevars])
AC_OUTPUT
7 changes: 0 additions & 7 deletions inst/include/units.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/Makevars.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PKG_CPPFLAGS = @UD_CPPFLAGS@
PKG_CPPFLAGS = -DUDUNITS2_DIR=@UDUNITS2_DIR@ @UD_CPPFLAGS@
PKG_LIBS = @LIBS@
2 changes: 1 addition & 1 deletion src/Makevars.win
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PKG_CPPFLAGS = -I../windows/udunits-2.2.20/include
PKG_CPPFLAGS = -DUDUNITS2_DIR=0 -I../windows/udunits-2.2.20/include
PKG_LIBS = -L../windows/udunits-2.2.20/lib${R_ARCH} \
-ludunits2 -lexpat

Expand Down
27 changes: 13 additions & 14 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#include "../inst/include/units.h"
#include <Rcpp.h>

using namespace Rcpp;
Expand All @@ -26,7 +25,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_parse
XPtrUT R_ut_parse(CharacterVector name);
SEXP R_ut_parse(CharacterVector name);
RcppExport SEXP _units_R_ut_parse(SEXP nameSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand All @@ -37,7 +36,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_get_dimensionless_unit_one
XPtrUT R_ut_get_dimensionless_unit_one(CharacterVector name);
SEXP R_ut_get_dimensionless_unit_one(CharacterVector name);
RcppExport SEXP _units_R_ut_get_dimensionless_unit_one(SEXP nameSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand Down Expand Up @@ -73,7 +72,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_new_dimensionless_unit
XPtrUT R_ut_new_dimensionless_unit(CharacterVector name);
SEXP R_ut_new_dimensionless_unit(CharacterVector name);
RcppExport SEXP _units_R_ut_new_dimensionless_unit(SEXP nameSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand All @@ -84,7 +83,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_new_base_unit
XPtrUT R_ut_new_base_unit(CharacterVector name);
SEXP R_ut_new_base_unit(CharacterVector name);
RcppExport SEXP _units_R_ut_new_base_unit(SEXP nameSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand All @@ -105,7 +104,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_scale
XPtrUT R_ut_scale(CharacterVector nw, CharacterVector old, NumericVector d);
SEXP R_ut_scale(CharacterVector nw, CharacterVector old, NumericVector d);
RcppExport SEXP _units_R_ut_scale(SEXP nwSEXP, SEXP oldSEXP, SEXP dSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand All @@ -118,7 +117,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_offset
XPtrUT R_ut_offset(CharacterVector nw, CharacterVector old, NumericVector d);
SEXP R_ut_offset(CharacterVector nw, CharacterVector old, NumericVector d);
RcppExport SEXP _units_R_ut_offset(SEXP nwSEXP, SEXP oldSEXP, SEXP dSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand All @@ -131,7 +130,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_divide
XPtrUT R_ut_divide(SEXP numer, SEXP denom);
SEXP R_ut_divide(SEXP numer, SEXP denom);
RcppExport SEXP _units_R_ut_divide(SEXP numerSEXP, SEXP denomSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand All @@ -143,7 +142,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_multiply
XPtrUT R_ut_multiply(SEXP a, SEXP b);
SEXP R_ut_multiply(SEXP a, SEXP b);
RcppExport SEXP _units_R_ut_multiply(SEXP aSEXP, SEXP bSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand All @@ -155,7 +154,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_invert
XPtrUT R_ut_invert(SEXP a);
SEXP R_ut_invert(SEXP a);
RcppExport SEXP _units_R_ut_invert(SEXP aSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand All @@ -166,7 +165,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_raise
XPtrUT R_ut_raise(SEXP a, IntegerVector i);
SEXP R_ut_raise(SEXP a, IntegerVector i);
RcppExport SEXP _units_R_ut_raise(SEXP aSEXP, SEXP iSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand All @@ -178,7 +177,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_root
XPtrUT R_ut_root(SEXP a, IntegerVector i);
SEXP R_ut_root(SEXP a, IntegerVector i);
RcppExport SEXP _units_R_ut_root(SEXP aSEXP, SEXP iSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand All @@ -190,7 +189,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_log
XPtrUT R_ut_log(SEXP a, NumericVector base);
SEXP R_ut_log(SEXP a, NumericVector base);
RcppExport SEXP _units_R_ut_log(SEXP aSEXP, SEXP baseSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand Down Expand Up @@ -248,7 +247,7 @@ BEGIN_RCPP
END_RCPP
}
// R_ut_map_name_to_unit
XPtrUT R_ut_map_name_to_unit(CharacterVector name, SEXP inunit);
SEXP R_ut_map_name_to_unit(CharacterVector name, SEXP inunit);
RcppExport SEXP _units_R_ut_map_name_to_unit(SEXP nameSEXP, SEXP inunitSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand Down
8 changes: 7 additions & 1 deletion src/io.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include <R.h>
#include <udunits2.h>

#if UDUNITS2_DIR != 0
# include <udunits2/udunits2.h>
#else
# include <udunits2.h>
#endif

#include "io.h"

/* From the enum comments in udunits2.h */
Expand Down
35 changes: 20 additions & 15 deletions src/udunits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
*/

#include <Rcpp.h>
#include <udunits2.h>

#if UDUNITS2_DIR != 0
# include <udunits2/udunits2.h>
#else
# include <udunits2.h>
#endif

extern "C" {
#include "io.h"
Expand Down Expand Up @@ -43,7 +48,7 @@ void udunits_exit() { // #nocov start
} // #nocov end

// wrap a ut_unit pointer in an XPtr
XPtrUT ut_wrap(ut_unit *u) {
SEXP ut_wrap(ut_unit *u) {
XPtrUT p(u);
return p;
}
Expand All @@ -55,7 +60,7 @@ ut_unit *ut_unwrap(SEXP u) {
}

// [[Rcpp::export]]
XPtrUT R_ut_parse(CharacterVector name) {
SEXP R_ut_parse(CharacterVector name) {
ut_unit *u = ut_parse(sys, ut_trim(name[0], enc), enc);
if (u == NULL) {
switch (ut_get_status()) {
Expand All @@ -72,7 +77,7 @@ XPtrUT R_ut_parse(CharacterVector name) {
}

// [[Rcpp::export]]
XPtrUT R_ut_get_dimensionless_unit_one(CharacterVector name) {
SEXP R_ut_get_dimensionless_unit_one(CharacterVector name) {
return ut_wrap(ut_get_dimensionless_unit_one(sys));
}
// [[Rcpp::export]]
Expand All @@ -96,15 +101,15 @@ NumericVector R_convert_doubles(SEXP from, SEXP to, NumericVector val) {
}

// [[Rcpp::export]]
XPtrUT R_ut_new_dimensionless_unit(CharacterVector name) {
SEXP R_ut_new_dimensionless_unit(CharacterVector name) {
ut_unit *u = ut_new_dimensionless_unit(sys);
if (ut_map_name_to_unit(name[0], enc, u) != UT_SUCCESS)
handle_error("R_ut_new_dimensionless_unit"); // #nocov
return ut_wrap(u);
}

// [[Rcpp::export]]
XPtrUT R_ut_new_base_unit(CharacterVector name) {
SEXP R_ut_new_base_unit(CharacterVector name) {
ut_unit *u = ut_new_base_unit(sys);
if (ut_map_name_to_unit(name[0], enc, u) != UT_SUCCESS)
handle_error("R_ut_new_base_unit"); // #nocov
Expand All @@ -128,7 +133,7 @@ void R_ut_remove_unit(CharacterVector name) {
}

// [[Rcpp::export]]
XPtrUT R_ut_scale(CharacterVector nw, CharacterVector old, NumericVector d) {
SEXP R_ut_scale(CharacterVector nw, CharacterVector old, NumericVector d) {
if (d.size() != 1)
stop("d should have size 1"); // #nocov
ut_unit *u_old = ut_parse(sys, ut_trim(old[0], enc), enc);
Expand All @@ -140,7 +145,7 @@ XPtrUT R_ut_scale(CharacterVector nw, CharacterVector old, NumericVector d) {
}

// [[Rcpp::export]]
XPtrUT R_ut_offset(CharacterVector nw, CharacterVector old, NumericVector d) {
SEXP R_ut_offset(CharacterVector nw, CharacterVector old, NumericVector d) {
if (d.size() != 1)
stop("d should have size 1"); // #nocov
ut_unit *u_old = ut_parse(sys, ut_trim(old[0], enc), enc);
Expand All @@ -152,36 +157,36 @@ XPtrUT R_ut_offset(CharacterVector nw, CharacterVector old, NumericVector d) {
}

// [[Rcpp::export]]
XPtrUT R_ut_divide(SEXP numer, SEXP denom) {
SEXP R_ut_divide(SEXP numer, SEXP denom) {
return ut_wrap(ut_divide(ut_unwrap(numer), ut_unwrap(denom)));
}

// [[Rcpp::export]]
XPtrUT R_ut_multiply(SEXP a, SEXP b) {
SEXP R_ut_multiply(SEXP a, SEXP b) {
return ut_wrap(ut_multiply(ut_unwrap(a), ut_unwrap(b)));
}

// [[Rcpp::export]]
XPtrUT R_ut_invert(SEXP a) {
SEXP R_ut_invert(SEXP a) {
return ut_wrap(ut_invert(ut_unwrap(a)));
}

// [[Rcpp::export]]
XPtrUT R_ut_raise(SEXP a, IntegerVector i) {
SEXP R_ut_raise(SEXP a, IntegerVector i) {
if (i.length() != 1)
stop("i should have length 1");
return ut_wrap(ut_raise(ut_unwrap(a), i[0]));
}

// [[Rcpp::export]]
XPtrUT R_ut_root(SEXP a, IntegerVector i) {
SEXP R_ut_root(SEXP a, IntegerVector i) {
if (i.length() != 1)
stop("i should have length 1");
return ut_wrap(ut_root(ut_unwrap(a), i[0]));
}

// [[Rcpp::export]]
XPtrUT R_ut_log(SEXP a, NumericVector base) {
SEXP R_ut_log(SEXP a, NumericVector base) {
if (base.length() != 1)
stop("base should have length 1");
if (base[0] <= 0)
Expand Down Expand Up @@ -260,7 +265,7 @@ CharacterVector R_ut_get_name(CharacterVector ustr) {

// https://github.com/r-quantities/units/issues/89#issuecomment-359251623
// [[Rcpp::export]]
XPtrUT R_ut_map_name_to_unit( CharacterVector name, SEXP inunit) { // #nocov start
SEXP R_ut_map_name_to_unit( CharacterVector name, SEXP inunit) { // #nocov start
ut_unit *unit = ut_unwrap(inunit);
if (ut_map_name_to_unit(name[0], enc, unit) != UT_SUCCESS)
handle_error("R_ut_map_name_to_unit");
Expand Down

0 comments on commit 7dc9be3

Please sign in to comment.