Examples

This chapter presents examples. The examples are available on BitBucket site in examples folder: https://bitbucket.org/godfryd/barabash/src.

Simple examples

This set of examples present various use cases of Barabash.

from barabash import ReduceOp, Command, MapOp, Files, set_env, run
from barabash import patsubst, define_reduce_op, define_map_op, define_ops_chain
import glob

# files
src_c = Files("src_c", ["a.c", "b.c"])
src_js = Files("src_js", ["a.js", "b.js"])

# reduce
ReduceOp("js.tar.gz", src_js, "tar -C {srcdir} -zcvf {out} {in.basename}")

# custom operation
Tar = define_reduce_op("tar -C {srcdir} -zcvf {out} {in.basename}")

Tar("js2.tar.gz", src_js)

# map + reduce
MapOp("objects", src_c, "%.o:%.c", "{CC} -c {in} -o {out}")
ReduceOp("program", objects, "{CC} -o {out} {in}")

# map + reduce: custom operations
Compile = define_map_op("%.o:%.c", "{CC} -c {in} -o {out}")
Link = define_reduce_op("{CC} -o {out} {in}")

Compile("objects2", src_c)
Link("program2", objects2)

# chaining operations
Compile = define_map_op("%.o:%.c", "{CC} -c {in} -o {out}")
Link = define_reduce_op("{CC} -o {out} {in}")
CompileAndLink = define_ops_chain(Compile, Link)

CompileAndLink("program3", src_c)

# custom command
Command("ls", [], "ls -al")

# custom command with error
Command("err", [], "rm non-existing-file")

# func as actions
def map_func(env):
    os.system("touch %s" % env['out'])
    return 0
MapOp("map_func", src_c, "%.o:%.c", map_func)

def red_func(env):
    os.system("touch %s" % env['out'])
    return 0
ReduceOp("c.c", src_c, red_func)

def cmd_func(env):
    return 4
Command("cmd_func", src_c, cmd_func)

# multiline script as action
Command("script",
        """
        a=5
        if [ $a -eq 5 ]; then
           exit $a
        fi
        """)

run()

Compiling nested source code

Barabash operations library provides convenient set of operations. There can be found a few operations for compiling and linking C programs and libraries (both static and shared).

This top level Barabash build script shows compiling and linking whole program. Here nested C libraries are included: one static and one shared. They are referenced in building program operation.

from barabash import include, BuildProgram, run

liba = include("liba/bb-liba.py")
libb = include("libb/bb-libb.py")

CFLAGS = "-DAAA=1"

BuildProgram("prog", "main.c", static_libs=liba.liba, shared_libs=libb.libb, external_libs=["m"])

run()

This is nested Barabash module the defines a shared C library.

from barabash import BuildSharedLib

libb = BuildSharedLib("libb.so.1.0", "b.c")

This is another nested Barabash module the defines a static C library.

from barabash import BuildStaticLib

liba = BuildStaticLib("liba.a", "main.c")






blog comments powered by Disqus