SCons Binary Directory - unix

How is the binary/executable directory specified in SCons? It's easy to do as follows:
Program( target = 'bin/helloworld', source = 'src/helloworld.cc' )
The problem with this approach is when trying to do cross-platform builds. Here's an example that works:
StaticLibrary( target = 'helloworld', source = 'src/helloworldlib.cc' )
The output of this on a Unix system is a library named libhelloworld.a. An example where it doesn't work follows:
StaticLibrary( target = 'lib/helloworld', source = 'src/helloworldlib.cc' )
The output on a Unix system of this is a file helloworld.a in directory lib. This causes problems when LIBS is specified as ['helloworld'], which is the cross platform way to do it.
What is the parameter name to pass into StaticLibrary, SharedLibrary, and Program to output binaries into a directory other than the base directory?

The manual suggests that you use a variant directory and a SConscript file in the source directory. In your example, place a SConscript file in the src directory:
StaticLibrary(target="helloworld", source="helloworldlib.cc")
and call that from the main SConstruct file:
SConscript("src/SConscript", variant_dir="lib")

Related

Replicating a java -jar execution through rJava

I have a java file that I would normally execute by doing
java -jar jarname.jar arguments
I want to be able to run this file from R in the most system agnostic way possible. My current pipeline partially relies on rJava do identify JAVA_HOME and run the jar by doing
# path for the example file below
pathToJar = 'pdftk-java.jar'
# start up java session
rJava::.jinit()
# find JAVA_HOME
javaPath = rJava::.jcall( 'java/lang/System', 'S', 'getProperty', 'java.home' )
# get all java files
javaFiles = list.files(javaPath,recursive = TRUE,full.names = TRUE)
# find java command
java = javaFiles[grepl('/java($|\\.exe)',javaFiles)]
# run the jar using system
system(glue::glue('{shQuote(java)} -jar {shQuote(pathToJar)} arguments'))
This does work fine but I was wondering if there was a reliable way to replicate execution of a jar through rJava itself. I want to do this because
I want to avoid any possible system dependent issues when finding the java command from JAVA_HOME
I already started an rJava session just to get the JAVA_HOME. I might as well use it since .jinit isn't undoable
I not that familiar with what calling a jar through -jar does and I am curious. Can it be done in a jar independent way? If not what should I look for in the code to know how to do this.
This is the file in I am working with. Taken from https://gitlab.com/pdftk-java/pdftk/tree/master
Executing JAR file is (essentially) running class file that is embedded inside JAR.
Instead of calling system and executing it as external application, you can do following:
make sure to add your JAR file to CLASSPATH
rJava::.jaddClassPath(pathToJar)
check inside JAR file what is the main class. Look into META-INF/MANIFEST.MF file to identify the main class. (In this case com.gitlab.pdftk_java.pdftk)
instantiate class inside R.
newObj = rJava::.jnew('com/gitlab/pdftk_java/pdftk')
run the class following way: http://www.owsiak.org/running-java-code-in-r/
Update
Running JAR file (calling main method of Main-class) is the same things as calling any other method inside Java based class. Please note that main method takes array of Strings as argument. Take a look here for sample: http://www.owsiak.org/running-jar-file-from-r-using-rjava-without-spawning-new-process/
newObj$main(rJava::.jarray('--version'))
For this specific case if you look at the source code for this class, you'll see that it terminates the session
public static void main(String[] args) {
System.exit(main_noexit(args));
}
This will also terminate your R session. Since all main function does it to call main_noexit then exit, you can replace main with main_noexit in the code above.
newObj$main_noexit(rJava::.jarray('--version'))

R error when using untar

I'm running a script with input parameters that are referenced in the code to automate the directory creation, download of file and untar of file. I would be fine with unzip, however this particular file I want to analyze is .tar.gz. I manually unpacked and it was tar.gz, unpacked to .tar file. Would that be the problem?
Full error: Error in untar2(tarfile, files, list, exdir) : unsupported entry type ‘’
Running Windows 10, 64 bit, R set to: [Default] [64-bit] C:\Program Files\R\R-3.2.2
Script notes one solution found (issues, lines 28-31), but I don't really understand it.
I did install 7-zip on my computer, restart and of course restart R:
`#DOWNLOADING AND UNZIPPING TAR FILE
#load required packages.
#If there is a load package error, use install.packages("[package]")
library(dplyr)
library(lubridate)
library(XML) # HTML processing
options(stringsAsFactors = FALSE)
#Set directory locations, data file and fetch data file from internet
#enter full url including file name between ' ' marks
mainDir<-"C:/R/BEES/"
subDir<-"C:/R/BEES/Killers"
Fetch<-'http://dds.cr.usgs.gov/pub/data/nationalatlas/afrbeep020_nt00218.tar.gz'
ArchFile<-basename(Fetch)
download.file<-(ArchFile)
#Check for file directories and create if directory if it doesn't exist
if(!file.exists(mainDir)){dir.create(mainDir)}
if(!file.exists(subDir)){dir.create(subDir)}
#set the working directory
setwd(file.path(subDir))
#check if file exists and download if it doesn't exist.
if(!file.exists(ArchFile))
{download.file (url=Fetch,destfile=ArchFile,method='auto')}
#unpack and view file list
untar(path.expand(ArchFile),list=TRUE,exdir=subDir,compressed="gzip")
list.files(subDir)
#Error: Error in untar2(tarfile, files, list, exdir) :
# unsupported entry type ‘’
#Need solution to use tar/untar app
#instructions here: https://stevemosher.wordpress.com/step-10-build/`
Appreciate feedback - I've been lurking around StackOverflow for some time to use other people's solutions.

How can I source an R file from the parent directory via the shell?

I'm able to source an R script from the IDE (Rstudio), but not from a command line call. Is there a way to do this without having to supply the full path?
The file I want to source is in the parent directory.
This works:
source('../myfile.R') #in a call from Rstudio
However, this doesn't:
> Rscript filethatsources_myfile.R
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
Calls: source -> file
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file '../myfile.R': No such file or directory
Execution halted
This seems like it should be simple, but...
Edit: I'm using GNU bash, version 3.2.53(1)-release (x86_64-apple-darwin13)
In R, relative file locations are always relative to the current working directory. You can explicitly set your working directory like so:
setwd("~/some/location")
Once this is set, you can get your source file relative to the current working directory.
source("some_script.R") # In this directory
source("../another_script.R") # In the parent directory
source("folder/stuff.R") # In a child directory
Not sure what your current working directory is? You can check by submitting getwd().
What if your source file is in, for example, the parent directory but references files relative to its location? Use the chdir= option in source:
source("../another_script.R", chdir = TRUE)
This temporarily changes the working directory to the directory containing the source file for the duration of the source evaluation. Once that's done, your working directory is set back to what it was prior to running source.

testthat in R: sourcing in tested files

I am using the testthat package in R and I am trying to test a function
defined in a file example.R. This file contains a call source("../utilities/utilities.R") where utilities.R is a file with functions written by me. However, when I am trying to test a function from example.R, sourcing it within the testing script gives the following error:
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file '../utilities/utilities.R': No such file or directory
Could you please clarify how to run tests for functions in files that source another file?
Might be a bit late, but I found a solution. Test_that sets the directory holding the test file as the current working directory. See the code below from test-files.r. This causes the working directory to be /tests. Therefore, your main scripts need to source ("../file.R"), which works for testing, but not for running your app.
https://github.com/hadley/testthat/blob/master/R/test-files.r
source_dir <- function(path, pattern = "\\.[rR]$", env = test_env(),
chdir = TRUE) {
files <- normalizePath(sort(dir(path, pattern, full.names = TRUE)))
if (chdir) {
old <- setwd(path)
on.exit(setwd(old))
}
The solution I found was to add setwd("..") in my test files and simply source the file name without the path. source("file.R") instead of source("../file.R"). Seems to work for me.
testthat allows you to define and source helper files (see ?source_test_helpers):
Helper scripts are R scripts accompanying test scripts but prefixed by helper. These scripts are run once before the tests are run.
So what worked perfectly for me is simply putting a file "helper-functions.R" containing the code that I want to source in "/tests/testthat/". You don't have to call source_test_helpers() yourself, testthat will automatically do that when you run tests (e.g., via devtools::test() or testthat::test_dir()).
No great solution to this problem I've found, so far mine has been to set the working directory within each test using the package here.
test_that('working directory is set',{
setwd(here())
# test code here
})
I put the source("C:/Users/.../Utilities.R") in the test file.

Fail to link to standard library of Ocaml-java (or Cafesterol)

I am a new user of Ocaml-java (or Cafesterol) which compiles primtive Ocaml program to executable jar that is allowed run on JVM. However when I try to compile a test program into executable jar I got error info as follow:
>java -jar ~/ocaml-project/ocamljava-bin-1.4/bin/ocamljava.jar -standalone regexdna.ml -o regexdna.jar
File "regexdna.ml", line 1, characters 0-1:
Error: No implementations provided for the following modules:
Str referenced from regexdna.cmj
Unix referenced from regexdna.cmj
It seems module Str and Unix is missing from Ocaml-java. However, str.jar and unix.jar do exist under ~/ocaml-project/ocamljava-bin-1.4/lib/others/ when I install Ocaml-java, and within these jars we do have Str.class and Unix.class. (I suppose this directory is on the path of the standard library of Ocaml-java, so it should be included in default search path)
Can any Ocaml-java user tell me how Ocaml-java search for dependency libraries?
Quoting Xavier Clerc on this :
Well it should work, but you have to pass explicitly the referenced
library (just as in vanilla OCaml). Leading in your case to:
$ /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java -jar ~/opt/ocamljava-2.0-early-access9/lib/ocamljava.jar str.cmja regexdna.ml
Note that I am using the latest ocamljava preview.

Resources