Name

mk_target — Define a build rule

Synopsis

MODULES="... core ..."

mk_target TARGET=target DEPS=deps command...

Options

TARGET=target

The target to build in MakeKit target notation (see mk_resolve_target).

DEPS=deps

An internally-quoted list of dependency targets in target notation.

command...

A command to run to create the target when the user runs make. Each parameter is subject to further processing described below.

Description

This function defines a generic build rule consisting of a target, a list of other targets it depends on, and a command to run to produce the target from its dependencies.

The positional parameters forming command are subject to additional processing if they match any of the following forms:

@file

Indicates that the parameter is a fully-qualified target. The parameter will be replaced with just file. Among other uses, this allows the output of a prior invocation of mk_target to be used verbatim in command.

&targets

Indicates that the parameter should be interpreted as a space separated list of targets in MakeKit target notation. The list will be expanded into multiple parameters according to the same quoting and expansion rules as the deps parameter above.

%VAR

Indicates that the parameter should be interpreted as the name of a variable. If the variable is set to a non-empty string, the parameter will be converted to the form VAR=value. If the variable is set to the empty string or is unset, the parameter will be omitted entirely. This provides a concise syntax for passing through keyword parameters.

%<, %>, %<<, %>>

These forms are replaced with the appropriate shell redirection token. Keep in mind that mk_target does not execute the build command immediately, but merely defines a rule for make. Therefore, using shell redirection directly will not achieve the desired effect. Use these forms instead.

*params

Indicates that the parameter should be interpreted as a space-separated, internally quoted list. The list will be expanded into multiple parameters, with each subject to further processing according to this list of forms.

#params

Indicates that the parameter should be interpreted as a space-separated, internally-quoted list. The list will be expanded into multiple parameters, but no further processing will occur.

make macro forms

Macro forms such as $@ and $* anywhere within a parameter will be expanded by make as usual.

Examples

# Generate bar.txt from foo.txt by replacing "apple" with "orange"
mk_target \
    TARGET="bar.txt" \
    DEPS="foo.txt" \
    sed "s/apple/orange/g" "%<" "&foo.txt" "%>" "&bar.txt"

# Generate "a bar.txt" from "a foo.txt" by replacing "apple" with "orange"
# This shows one way of dealing with spaces in filenames
mk_target \
    TARGET="a bar.txt" \
    DEPS="'a foo.txt'" \
    sed "s/apple/orange/g" "%<" "&'a foo.txt'" "%>" "&'a bar.txt'"

# An alternate approach to the above
mk_target \
    TARGET="a bar.txt" \
    DEPS="a\ foo.txt" \
    sed "s/apple/orange/g" "%<" "&a\ foo.txt" "%>" "&a\ bar.txt"

# Generate all.txt by concatenating all .txt files in the directory containing MakeKitBuild
mk_target \
    TARGET="all.txt" \
    DEPS="*.txt" \
    cat "&*.txt" "%>" "&all.txt"