[Rcpp-commits] r445 - in pkg: . inst/unitTests src src/cxx0x

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jan 25 10:06:46 CET 2010


Author: romain
Date: 2010-01-25 10:06:46 +0100 (Mon, 25 Jan 2010)
New Revision: 445

Added:
   pkg/src/cxx0x/
   pkg/src/cxx0x/cxx0x.R
Modified:
   pkg/configure.win
   pkg/inst/unitTests/runit.IntegerVector.R
   pkg/src/Makevars
   pkg/src/Makevars.win
Log:
added some logic in the Makevars to detect c++0x, for now only based on the RCPP_CXX0X environment variable

Modified: pkg/configure.win
===================================================================
--- pkg/configure.win	2010-01-24 23:31:23 UTC (rev 444)
+++ pkg/configure.win	2010-01-25 09:06:46 UTC (rev 445)
@@ -5,7 +5,7 @@
 
 case ${gxx_version} in
     4.3.*|4.4.*|4.5.*)
-           echo "You can enable support for the C++0x draft by uncommenting"
-           echo "the variable 'CLINK_CPPFLAGS' in src/Makevars.win"
+           echo "You can enable support for the C++0x draft by setting the"
+           echo "'RCPP_CXX0X' environment variable to 'yes' "
     ;;
 esac

Modified: pkg/inst/unitTests/runit.IntegerVector.R
===================================================================
--- pkg/inst/unitTests/runit.IntegerVector.R	2010-01-24 23:31:23 UTC (rev 444)
+++ pkg/inst/unitTests/runit.IntegerVector.R	2010-01-25 09:06:46 UTC (rev 445)
@@ -54,4 +54,30 @@
 	}
 }
 
+test.IntegerVector.matrix.indexing <- function(){
+	funx <- cfunction(signature(x = "integer" ), '
+		IntegerVector m(x) ;
+		int trace = 0.0 ;
+		for( size_t i=0 ; i<4; i++){
+			trace += m(i,i) ;
+		}
+		return wrap( trace ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	x <- matrix( 1:16, ncol = 4 )
+	checkEquals( funx(x), sum(diag(x)), msg = "matrix indexing" )
+	
+	y <- as.vector( x )
+	checkException( funx(y) , msg = "not a matrix" )
+	
+	funx <- cfunction(signature(x = "integer" ), '
+		IntegerVector m(x) ;
+		int trace = 0.0 ;
+		for( size_t i=0 ; i<4; i++){
+			m(i,i) = 2 * i ;
+		}
+		return m ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( diag(funx(x)), 2*0:3, msg = "matrix indexing lhs" )
+	
+}
 

Modified: pkg/src/Makevars
===================================================================
--- pkg/src/Makevars	2010-01-24 23:31:23 UTC (rev 444)
+++ pkg/src/Makevars	2010-01-25 09:06:46 UTC (rev 445)
@@ -10,8 +10,15 @@
 ## files in this directory. userLibrary is an additional target for the second library
 ## that will be installed in ../inst/lib$(R_ARCH) and which users can link against.
 
-all: 		$(SHLIB) userLibrary 
+## C++0x enabled if 
+## - the environment variable RCPP_CXX0X is set to yes
+## - it makes sense (GCC >= 4.3)
+ifeq ($(RCPP_CXX0X),yes)
+CLINK_CPPFLAGS = $(shell Rscript "cxx0x/cxx0x.R" --listen-env-var)
+endif
 
+all: 	$(SHLIB) userLibrary 
+
 ## all sources and objects
 SOURCES =	$(wildcard *.cpp)
 OBJECTS =	$(SOURCES:.cpp=.o)
@@ -28,9 +35,6 @@
 
 PKG_CPPFLAGS += -I. 
 
-## uncomment to enable (non-portable) support for C++0x
-#CLINK_CPPFLAGS = -std=c++0x
-
 userLibrary: 	$(USERLIB) $(USERLIBST)
 		- at if test ! -e $(USERDIR)$(R_ARCH); then mkdir -p $(USERDIR)$(R_ARCH); fi
 		- at if test ! -e $(USERDIR)$(R_ARCH)/Rcpp; then mkdir -p $(USERDIR)$(R_ARCH)/Rcpp; fi

Modified: pkg/src/Makevars.win
===================================================================
--- pkg/src/Makevars.win	2010-01-24 23:31:23 UTC (rev 444)
+++ pkg/src/Makevars.win	2010-01-25 09:06:46 UTC (rev 445)
@@ -37,9 +37,14 @@
 ## to filter out -Wall which generated a ton with older compilers
 newCXXFLAGS := 	$(filter-out -Wall,$(CXXFLAGS))
 CXXFLAGS := 	$(newCXXFLAGS)
-# commented out for now
-# CLINK_CPPFLAGS := $shell(Rscript.exe ../inst/discovery/cxx0x.R)
 
+## C++0X support enabled if 
+## - the RCPP_CXX0X environment variable is set to "yes"
+## - it is possible (GCC>=4.3)
+ifeq ($(RCPP_CXX0X),yes)
+CLINK_CPPFLAGS := $shell(Rscript.exe cxx0x/cxx0x.R)
+endif
+
 ## the 'package' library contains both Rcpp.{cpp,h} and the RcppExample used to demonstrate the package
 ## it is loaded via library(Rcpp) but is not used for compiling / linking against
 PKGLIB = 	Rcpp.dll

Added: pkg/src/cxx0x/cxx0x.R
===================================================================
--- pkg/src/cxx0x/cxx0x.R	                        (rev 0)
+++ pkg/src/cxx0x/cxx0x.R	2010-01-25 09:06:46 UTC (rev 445)
@@ -0,0 +1,60 @@
+#!/bin/env Rscript
+
+# Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+#
+# This file is part of Rcpp.
+#
+# Rcpp is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# Rcpp is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+# This script is used by the Rcpp::RcppCxx0xFlags function to
+# generate the "-std=c++0x" flag when the compiler in use is GCC >= 4.3
+
+local({
+	flag <- function(){
+		
+		cxx0x.code <- '
+		#include <R.h>
+		#include <Rdefines.h>
+		
+		extern "C" SEXP cxx0x(){
+		
+		#ifdef __GNUC__
+			#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
+			#if GCC_VERSION >= 40300
+			return mkString( "-std=c++0x" ) ;
+			#endif
+		#endif
+		return mkString( "" ) ;
+		}
+		'
+		td <- tempfile()
+		dir.create( td )
+		here <- getwd()
+		setwd(td)
+		dll <- sprintf( "cxx0x%s", .Platform$dynlib.ext )
+		on.exit( { 
+			dyn.unload( dll )
+			setwd(here) ; 
+			unlink( td, recursive = TRUE )
+		} )
+		writeLines( cxx0x.code, "cxx0x.cpp" )
+		cmd <- sprintf( '"%s/R" CMD SHLIB cxx0x.cpp', R.home(component="bin") )
+		system( cmd, intern = TRUE )
+		dyn.load( dll )
+		res <- tryCatch( .Call( "cxx0x" ), error = "" )
+		res
+	}
+	cat( flag() )
+})
+



More information about the Rcpp-commits mailing list