I have the following project
$ tree
.
├── lib
│ └── MyModule.raku
└── main.raku
$ cat lib/MyModule.raku
use v6;
unit module MyModule;
sub hello { say 'hello' }
$ cat lib/main.raku
use v6;
use MyModule;
MyModule.hello();
I would like to run main.raku using the latest rakudo-star image. However the following occurs
$ docker run -i --rm -u $(id -u) \
--workdir /work \
--volume $PWD:/work \
--entrypoint bash \
rakudo-star perl6 -I ./lib main.raku
===SORRY!===
Could not find MyModule at line 3 in:
file#/work/lib
inst#/.perl6
inst#/usr/share/perl6/site
inst#/usr/share/perl6/vendor
inst#/usr/share/perl6
ap#
nqp#
perl5#
I have also tried inserting use lib '/work/lib' before use MyModule in main.raku with the same result.
There are several problems.
Modules don't end with .raku. They end with .rakumod or .pm6 (for now).
(Technically after you install, it doesn't actually matter what the extension is as long as you have it properly declared in META6.json.)
Subroutines are lexically scoped by default (my), and are also not exported by default.
So there is no way to access hello() outside of the module it is defined in.
Modules don't have methods, so you can't call hello as a method.
Even if they did have methods they wouldn't start with the sub keyword.
You can globally scope the subroutine with our:
lib/MyModule.rakumod
use v6.d;
unit module MyModule;
our sub hello () { say 'hello' }
#^
# \
# globally scoped
main.raku
use v6.d;
# use lib './lib';
use module MyModule;
MyModule::hello();
# ^^
# \
# namespace delimiter
You could export it instead:
lib/MyModule.rakumod
use v6.d;
unit MyModule;
# mark for export
# v-------v
sub hello () is export {
say 'hello'
}
main.raku
use v6.d;
# use lib './lib';
use MyModule;
hello();
In addition to is export, there are other more fine grained ways to export.
I would recommend that if you are going to export, that you also make it global with our. That way if someone uses your module, but doesn't want to import your subs; they still have access to them.
New Raku versions uses the .rakumod as file extension for modules. The older .pm6 extension is still supported. More information regarding the new file extensions can be found in the Path-to-Raku ticket. More information about modules is available in the raku documentation.
Related
My project contains a custom target which generates some output via .cmake script. It looks like this:
add_custom_target(TargetName
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_SOURCE_DIR}/cmake/script.cmake
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/generated/output
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
VERBATIM
)
But now I want to set come cache variables inside the script. I tried doing like that:
message("MY_CACHE_VARIABLE = ${MY_CACHE_VARIABLE}")
set(MY_CACHE_VARIABLE "VALUE" CACHE INTERNAL "")
And I faced with the problem that cache variables are not saved. It always prints me empty output:
MY_CACHE_VARIABLE =
I already tried setting working directory as CMAKE_BINARY_DIR, or passing CMAKE_BINARY_DIR of the last argument of cmake command, or passing -B ${CMAKE_BINARY_DIR} or -C ${CMAKE_BINARY_DIR}/CMakeCache.txt as arguments and etc. None of these worked.
So is there any way to reuse existing cache inside CMake subprocess or I just should write my own cache inside the script?
You have to distinguish between running CMake to generate build files (for Make, Ninja, etc.) and running CMake in script mode:
Script mode simply runs the commands in the given CMake Language source file and does not generate a build system. It does not allow CMake commands that define build targets or actions.
-- cmake-language(7)
No configure or generate step is performed and the cache is not modified.
-- cmake(1)
So in script mode (-P), CMake is not aware of the cache or any variable/target/etc. defined in your regular CMakeLists.txt files. It is more similar to executing a bash/shell script than to processing a "usual" CMakeLists.txt.
But don't worry, there is still a solution to your problem. You can simply pass your arguments as -D options to your script:
add_custom_target(TargetName
COMMAND ${CMAKE_COMMAND}
-DMY_VAR="..."
-DANOTHER_VAR="..."
-P ${CMAKE_SOURCE_DIR}/cmake/script.cmake
...
)
Note however:
If variables are defined using -D, this must be done before the -P argument.
-- cmake(1)
When I'm building a Singularity container I'd like to read environment variables from the host system in the %post section. I've been looking online for a way to achieve this, but to no avail. I'm starting to question if this is even possible at the moment, but I can't find any mentions of it being possible/impossible.
Example:
Singularity definition file: recipe
BootStrap: docker
From: continuumio/anaconda3
%runscript
%post
echo $TEST_ENV_VARIABLE
On the host system / OS
export TEST_ENV_VARIABLE='foo'
sudo singularity build test.sif recipe
prints only a blank line when echoing TEST_ENV_VARIABLE.
If there is no way of reading host system's environment variables in the %post section, are there any other ways of passing arguments into the recipe that could be used build-time?
That is not currently possible, though there is an open issue for that functionality. I'm not personally a fan of dynamic build options as it makes it harder to guarantee reproducibility.
If you do want something more dynamic, you could use a template to create different definition files. A very simplistic example:
$ cat gen_def.py
#!/usr/bin/env python3
import sys
my_def = """BootStrap: docker
From: continuumio/anaconda3
%post
echo This is {0}
echo This is {1}"""
print(my_def.format(*sys.argv[1:]))
$ ./gen_def.py one two > Singularity.custom
$ sudo singularity build test.sif Singularity.custom
I am following the direnv wiki on PS1. I have the following relevant entries in my files.
.bashrc
DEFAULT_PS1='\[$(ppwd)\]\u#\h:\w$(__git_ps1 " (%s)")'
# add some more things to DEFAULT_PS1, conditionally
DEFAULT_PS1+='> '
PS1=${CUSTOM_PS1:-$DEFAULT_PS1}
# optional bashrc file extensions
for f in ~/.bashrc_*; do test -s $f && . $f || true; done
eval "$(direnv hook bash)"
.envrc
export KUBECONFIG=~/.config/kube/homelab.yaml
export KUBE_PS1_ENABLED=on
export CUSTOM_PS1='$(kube_ps1) $ '
PATH_add scripts
I have allowed the latest version of the .envrc with direnv allow. However, when changing to the directory, the custom PS1 value is not set, although the values seems to be right
$ cd -
/home/robert/sources/oss/sling-cloud-native
direnv: loading .envrc
direnv: export +CUSTOM_PS1 +KUBE_PS1_ENABLED ~KUBECONFIG ~PATH
$ echo $PS1
\[$(ppwd)\]\u#\h:\w$(__git_ps1 " (%s)")$(kube_ps1)>
$ echo $CUSTOM_PS1
$(kube_ps1) $
I am not sure how the solution in the wiki is supposed to work, as apparently the value of PS1 is set to the DEFAULT_PS1 when the .bashrc file is loaded the first time and is not re-evaluated as part of the direnv hook.
How can I change the value of PS1 using direnv?
The direnv wiki mentions that the author had to "blacklist PS1 as an environment variable that can be changed," mainly because "The core issue is that PS1 is a local variable." So I don't think workarounds that involve using the .envrc file to indirectly modify the PS1 can work.
I had a similar issue with python virtual environments, which I realize is different to your use case, but there is an example in this blog that could be helpful.
Because links can die I reproduce it here:
add the following to ~/.bashrc (me: I tested this with ~/.zshrc and it also works)
show_virtual_env() {
if [[ -n "$VIRTUAL_ENV" && -n "$DIRENV_DIR" ]]; then
echo "($(basename $VIRTUAL_ENV))"
fi
}
export -f show_virtual_env
PS1='$(show_virtual_env)'$PS1
Then source the file again
source ~/.bashrc
The wiki also mentions adding unset PS1 to the .envrc file, which removes any error about direnv: PS1 cannot be exported... and I can confirm that also works with this scenario.
Perhaps you can do something similar; use .envrc to export the environment variables as you are doing, but remove the line export CUSTOM_PS1='$(kube_ps1) $ ' and in your ~/.bashrc make a function that checks if you have set KUBE_PS1_ENABLED and appends '$(kube_ps1) $ ' to PS1 if it is set.
I am completely new to Kotlin, and I am trying to run the Kotlin REPL.
Following this, and considering I am using OS X, and I have tried this:
$ /usr/local/bin/kotlinc-jvm
which is equivalent to:
$ kotlinc-jvm
Then in the following link, I found that a nicer way to run it is:
$ kotlinc
Is there any differences between this two commands, and which one should I choose?
If you look inside the kotlinc-jvm files, they actually just launch the kotlinc that's in the same folder they are in, and pass any arguments they were started with to it:
kotlinc-jvm for Unix:
#!/usr/bin/env bash
# (License here)
DIR="${BASH_SOURCE[0]%/*}"
: ${DIR:="."}
"${DIR}"/kotlinc "$#"
kotlinc-jvm.bat for Windows:
#echo off
rem (License here)
call %~dps0kotlinc.bat %*
I'm not sure why kotlinc-jvm is there in this form, it's basically just a very simple redirect. I'd just use kotlinc.
How can I call to a cmake file in script mode (-P) from other cmake file, so this "cmake child" knows all variable of its parent? Because, if I have a lot of variables the child needs, I have to write many -D options, and I want to avoid it.
Example:
// CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
set(teststr "Hello World!")
add_custom_command(test
${CMAKE_COMMAND} -Dteststr=${teststr} -P test.cmake
)
// test.cmake
message("${teststr}")
$ cmake .
$ make test
Hello world!
Built target test
Works fine!. But, without "-Dteststr":
// CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
set(teststr "Hello World!")
add_custom_command(test
${CMAKE_COMMAND} -P test.cmake
)
// test.cmake
message("${teststr}")
$ cmake .
$ make test
Built target test
Of course, without -D option, the "teststr" variable, in test.cmake, is unset, and thus, the output is empty.
Any option to call test.cmake in "heritage mode", or something like that?
You can pass arguments to a script with cmake -P.
If you call:
cmake -P <script-file> <arg3> <arg4> <arg5> ...
then the variables CMAKE_ARGC, CMAKE_ARGV0, CMAKE_ARGV1, ... will be available for the script.
See documentation for CMAKE_ARGC and CMAKE_ARGV0.
The other way is to define variables, just like with the non-script cmake command. However there's one thing to be aware of: you need to define the variables before -P:
cmake -DVAR=VALUE -DFOO=BAR -P <script-file> <arg5> <arg6> ...
Now in the cmake VAR and FOO will be available.
Also, note that the numbering of the args after the script-file will be shifted accordingly.
There's no particularly easy way to do this that I know of.
You could write all the current variables in the parent CMakeLists.txt to a separate file and then include this in your test.cmake:
# CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
set(teststr "Hello World!")
set(CacheForScript ${CMAKE_BINARY_DIR}/CMakeCacheForScript.cmake)
file(WRITE ${CacheForScript} "")
get_cmake_property(Vars VARIABLES)
foreach(Var ${Vars})
if(${Var})
string(REPLACE "\\" "\\\\" ${Var} ${${Var}})
endif()
file(APPEND ${CacheForScript} "set(${Var} \"${${Var}}\")\n")
endforeach()
add_custom_target(test ${CMAKE_COMMAND} -P ${CMAKE_SOURCE_DIR}/test.cmake)
# test.cmake
include(${CMAKE_BINARY_DIR}/CMakeCacheForScript.cmake)
message("${teststr}")