Bazel: Run a command without an output - qemu

I am using bazel for building bare metal programs. I would like to run the unit tests generated by bazel on qemu.
qemu-system-* -some_args -kernel bazel-bin/whatever/generated.elf
I've tried to run those by creating my own rule in a '.bzl'-file, but it seems that outputs are mandatory on all the rule actions. Note, that I need to invoke different qemu commands with different arguments depending on the target architecture. I would like to pass those to the rule.
Is there a way to invoke a shell command without any outputs?
If needed, this is what I have so far (yet I'm not sure which parts are correct as bazel stops in the analysis phase):
# run_tests.bzl
===============
def _impl(ctx):
qemu = ctx.attr.qemu
machine = ctx.attr.machine
cpu = ctx.attr.cpu
target = ctx.attr.target
# The command may only access files declared in inputs.
ctx.actions.run_shell(
arguments = [qemu, machine, cpu, target],
command="$1 -M $2 -cpu $3 -nographic -monitor null -serial null -semihosting -kernel $4")
run_tests = rule(
implementation=_impl,
attrs = {"qemu" : attr.string(),
"machine" : attr.string(),
"cpu" : attr.string(),
"target" : attr.string(),},
executable = True
)
And my BUILD-File:
# BUILD
=======
load("//make:run_tests.bzl", "run_tests")
run_tests(
name = "portos",
qemu = "qemu-system-arm",
machine = "realview-pbx-a9",
cpu = "cortex-a9",
target = ":test_portos.elf"
)
cc_binary(
name = "test_portos.elf",
srcs = glob(["*.cc"]),
deps = ["//src:portos",
"#unity//:unity"],
copts = ["-Isrc",
"-Iexternal/unity/src",
"-Iexternal/unity/extras/fixture/src"]
)

You are almost there: yes, you need outputs, otherwise bazel has nothing to do. For the rule output, you probably want the test logs or tests results.

Skylark has support for writing test rules. Basically instead of setting executable = True, you would set test = True, and then your rule would create an executable that is the test, then you set ctx.outputs.executable to that executable. Then you can use the bazel test command with your rule.
See:
docs: https://docs.bazel.build/versions/master/skylark/rules.html#test-rules
example: https://github.com/bazelbuild/examples/tree/master/rules/test_rule
rule.test: https://docs.bazel.build/versions/master/skylark/lib/globals.html#rule.test

Related

QEmu’s `-nodefaults` not working as expected to me

I expected QEmu’s -nodefaults to prevent the creation of default devices, but either I don’t use it correctly or it does not work as I expected.
I get this message, using the stripped down command line below, with QEmu 3.0.
The message:
qemu-system-i386: warning: multiple floppy disk controllers
with iobase=0x3f0 have been found
the one being picked for CMOS setup might not reflect your intent
Additional message in the VM’s terminal:
could not read the boot disk
The command line:
qemu-system-i386 \
-machine type=isapc,usb=no \
-nodefaults \
-device isa-vga \
-blockdev driver=file,node-name=fda-img,filename=fda.img \
-blockdev driver=raw,node-name=fda,file=fda-img \
-device isa-fdc,driveA=fda,fdtypeA=144,fdtypeB=none,bootindexA=0
I tried removing -machine type=isapc, but it changed nothing.
This is so, although the documentation says this:
-nodefaults
Don’t create default devices. Normally, QEMU sets the default devices
like serial port, parallel port, virtual console, monitor device, VGA
adapter, floppy and CD-ROM drive and others. The -nodefaults option will
disable all those default devices.
I tried to add a --verbose option to the command line, with the hope it will request a dump of the full configuration QEmu creates, but such an option unfortunately does not exist.
My naive feeling is that it’s broken, but I must also consider I may be missing something … (hence this naive question).
— Update for more details —
Although there is no way to dump the created machine from the commande line, I found there is a info qtree in the monitor, which dumps the devices tree. Below, what it says:
[…]
dev: isa-fdc, id ""
iobase = 1008 (0x3f0)
irq = 6 (0x6)
dma = 2 (0x2)
driveA = ""
driveB = ""
check_media_rate = true
fdtypeA = "144"
fdtypeB = "none"
fallback = "288"
isa irq 6
bus: floppy-bus.1
type floppy-bus
dev: floppy, id ""
unit = 0 (0x0)
drive = "fda"
logical_block_size = 512 (0x200)
physical_block_size = 512 (0x200)
min_io_size = 0 (0x0)
opt_io_size = 0 (0x0)
discard_granularity = 4294967295 (0xffffffff)
write-cache = "auto"
share-rw = false
drive-type = "144"
[…]
dev: isa-fdc, id ""
iobase = 1008 (0x3f0)
irq = 6 (0x6)
dma = 2 (0x2)
driveA = ""
driveB = ""
check_media_rate = true
fdtypeA = "auto"
fdtypeB = "auto"
fallback = "288"
isa irq 6
bus: floppy-bus.0
type floppy-bus
[…]
It creates two floppy controllers, ignoring -nodefaults. Am I still missing something?
Also, surprisingly, for the first controller, which is the one I create, driveA and driveB are empty strings while driveA is assigned in the command line.
I finally believe the documentation is unclear and that’s rather -machine none which does not create any device. However, doing so, there is no way to add a bus which would be required to attach any device. I guess in this particular case, the isapc cannot be created without some device and it must be used as‑is, as a starting point. Then, the backends are to be attached without creating the frontends (if the wording is correct).
Here is an example command line snippet:
[…]
-blockdev driver=file,node-name=fda-img,filename=dos-6-22/Dos622-1.img \
-blockdev driver=raw,node-name=fda,file=fda-img \
-global isa-fdc.driveA=fda \
[…]
Note the -global isa-fdc.driveA=fda, which the important part in this snippet ; this is how the backend is attached to the forcefully created frontend.
Update:
Devices created by -machine <model> seems to be a special case not taken into account by -nodefaults ; this is what the actual documentation forget to mention.
Update 2:
Submitted as a documentation bug, here: https://bugs.launchpad.net/qemu/+bug/1799768

Rails Engines with multiple databases

I am creating application in Ruby on Rails which is having many engines(for modularity).
I want different databases for each engine. How to configure this?
Database - MYSQL
There is a good explanation by the link http://www.blrice.net/blog/2016/04/09/one-rails-app-with-many-databases/
General approach is to take a look at the framework sources and decide can it be reused.
Let's take a look at activerecord/lib/active_record/railties/databases.rake (v5.0.7) first. For example on how db:create implemented.
We will see ActiveRecord::Tasks::DatabaseTasks.create_current.
Let's open ActiveRecord::Tasks::DatabaseTasks and take a look at
# The possible config values are:
#
# * +env+: current environment (like Rails.env).
# * +database_configuration+: configuration of your databases (as in +config/database.yml+).
# * +db_dir+: your +db+ directory.
# * +fixtures_path+: a path to fixtures directory.
# * +migrations_paths+: a list of paths to directories with migrations.
# * +seed_loader+: an object which will load seeds, it needs to respond to the +load_seed+ method.
# * +root+: a path to the root of the application.
#
# Example usage of DatabaseTasks outside Rails could look as such:
#
# include ActiveRecord::Tasks
# DatabaseTasks.database_configuration = YAML.load_file('my_database_config.yml')
# DatabaseTasks.db_dir = 'db'
# # other settings...
This way we are getting to following solution:
namespace :your_engine do
namespace :db do
task :load_config do
ActiveRecord::Tasks::DatabaseTasks.database_configuration = YAML.load_file("config/database_your_engine.yml")
ActiveRecord::Tasks::DatabaseTasks.db_dir = "db_your_engine"
ActiveRecord::Tasks::DatabaseTasks.migrations_paths = [ "components/your_engine/db/migrate" ]
ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration
ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
# You can observe following values to see how settings applied.
# puts ActiveRecord::Base.configurations
# puts ActiveRecord::Migrator.migrations_paths
# puts ActiveRecord::Tasks::DatabaseTasks.database_configuration
# puts ActiveRecord::Tasks::DatabaseTasks.migrations_paths
end
desc "Create Your DB"
task create: :load_config do
ActiveRecord::Tasks::DatabaseTasks.create_current
end
end
end
The same approach for drop/migrate and other needed tasks.
It is good general rule - know you stack at least one level lower than your work with. Sometimes reading underlying sources much more helpful than direct answer.
I will update this answer while going forward with my solution...

Next free device option for qemu-nbd

Is there an option for the qemu-nbd command to get the next free, i.e. unused NBD like losetup -f does? The manpage of 0.0.1 (which is version of the currently stable release 1.7.0 of qemu) doesn't mention anything.
You can query attributes about nbd devices in sysfs.
For example:
cat /sys/class/block/nbd0/size
Will return 0, or the size of the mapped image file otherwise, if /dev/ndb0 is in use.
So you could iterate each device until you find one with 0 and attempt to try that with qemu-nbd.
Something like this should do it:
for x in /sys/class/block/nbd* ; do
S=`cat $x/size`
if [ "$S" == "0" ] ; then
qemu-nbd -c /dev/`basename $x` some_file.img
break
fi
done

Autoconf Produces configure with Broken Functions (ac_fn_set_status, ac_fn_exit)

I'm trying to set up autoconf for my project. I have everything working "properly" except that the ac_set_<...> functions are not being found in ./configure. They work fine in configure.status if I run it directly.
Specifically, I am having trouble with as_fn_set_status and as_fn_exit.
If I manually edit the config file and move the two functions to the top of the configure script, everything works fine.
To get to this point I:
Wrote configure.ac
ran autoreconf -i
ran ./configure
The resulting lines are something like:
./configure: line 1366: as_fn_set_status: command not found
There are 3-4 lines on which the error occurs.
Any ideas on what might be producing this effect? Here is my configure.ac:
##########################################
# Autoconf Configuration File for RPDB #
##########################################
# RPDB: An Object-Oriented Wrapper for Oracle's Berkeley Database (BDB/libdb),
# which is available at: http://www.oracle.com/technology/software/products/berkeley-db/index.html
###########################
# Init Autoconf >= 2.61 #
###########################
AC_CANONICAL_SYSTEM
AC_PREREQ(2.61)
AC_INIT([rpdb], [0.1.0], [asher#ridiculouspower.com])
AC_CONFIG_AUX_DIR([.])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_FILES([Makefile])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE
#################################
# Check for Library Functions #
#################################
AC_FUNC_ERROR_AT_LINE
AC_FUNC_MALLOC
AC_CHECK_FUNCS([strdup])
################################
# Check for Working Compiler #
################################
AC_PROG_CC
AC_PROG_RANLIB
#########################
# Check for Libraries #
#########################
AC_SEARCH_LIBS([db_create], [db], [have_libdb=yes])
#######################
# "Root Sourcefile" #
#######################
# "Root Sourcefile" is only used nominally to specify base path
AC_CONFIG_SRCDIR([src/RPDB_Base/RPDB.h])
#######################
# Check for Headers #
#######################
AC_HEADER_STDC
AC_CHECK_HEADERS([stdlib.h string.h])
# If we found libdb then check for db.h - we need to have both or we throw an error
if test "x${have_libdb}" = xyes; then
AC_CHECK_HEADERS([db.h], [], [have_libdb=no])
fi
if test "x${have_libdb}" = xno; then
echo "------------------------------------------"
echo " Oracle's Berkeley Database (libdb) "
echo " library and header file is required to "
echo " build RPDB. Stopping... "
echo " Check 'config.log' for more information. "
echo "------------------------------------------"
(exit 1); exit 1;
fi
#####################################################
# Check For Type-Related Compiler Characteristics #
#####################################################
AC_C_CONST
AC_HEADER_STDBOOL
AC_TYPE_INT32_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_TYPE_UINT32_T
AC_TYPE_UINT64_T
AC_TYPE_UINT8_T
###############################
# Generate Configure Script #
###############################
AC_OUTPUT
You cannot invoke any macros that have a non-empty expansion prior to AC_INIT. If you move the invocation of AC_CANONICAL_SYSTEM to be after AC_INIT, your problem should go away.
I had a simillar problem which drove me crazy and eventually found the problem:
I tried to compile an older project that provided its own m4-macros. In included m4-files (in the config subdirectory) there have been calls to AC_DEFFUN that provided a third argument.
I did not understand why this caused that problem but taking away the third argument helped.
Do you perhaps have different autoconf's installed. I think that function originates from the m4 macro: m4sugar/m4sh.m4. You could check that file to see if it has the function.
It should look something like:
[AS_REQUIRE_SHELL_FN([as_fn_set_status],
[AS_FUNCTION_DESCRIBE([as_fn_set_status], [STATUS],
[Set $? to STATUS, without forking.])], [ return $[]1])]dnl

How to configure Mercurial to use Kompare when merging?

I've tried adding merge=kompare to my ~/.hgrc, but when I run hg merge, it runs kompare, but there's no UI to be seen. Hg says merging path/to/first-file and stays there, actionless.
The kompare.args posted earlier probably wont work. I've had difficulty using Kompare for merging, especially 3-way merges (which are preferred and safe).
BTW, most of the other options are enabled by default I believe, but you can verify with: hg showconfig merge-tools
You are better off using kdiff3. Incase you are on Ubuntu Intrepid, kdiff3 was erroneously removed from the repos - but you can easily compile from source.
You will also need to add a section that explains how to call Kompare. I don't know Kompare, so I don't know what the command line should look like (no guarantees for the kompare.args line), but it should be something like this:
[merge-tools]
kompare.executable = C:\<path...>\kompare.exe
kompare.args = $base $local $other -o $output
kompare.priority = 1
kompare.gui = True
kompare.binary = True
If merges aren't detected correctly, you might want to add
kompare.checkconflicts = True
kompare.checkchanged = True
Kompare does not support 3-way diffs, so it can be used for Visual Diff feature only. Here's the config to make it work:
[merge-tools]
kompare.executable = kompare
kompare.diffargs = -c $parent $child
kompare.gui = True
Note the diffargs field instead of args.