diff options
author | johannst <johannes.stoelp@gmail.com> | 2020-09-18 20:08:35 +0200 |
---|---|---|
committer | johannst <johannes.stoelp@gmail.com> | 2020-09-18 20:08:35 +0200 |
commit | 0bfbf564ff8f653d2412eb196bd362c76fee4ae6 (patch) | |
tree | b31f8f88cff1871494e9fdc7a0c708f5ea16e996 | |
parent | 7a8d3b4eec4056600625f2d359320c8a01fa88b3 (diff) | |
download | notes-0bfbf564ff8f653d2412eb196bd362c76fee4ae6.tar.gz notes-0bfbf564ff8f653d2412eb196bd362c76fee4ae6.zip |
added gnu make notes
-rw-r--r-- | src/SUMMARY.md | 1 | ||||
-rw-r--r-- | src/development/README.md | 1 | ||||
-rw-r--r-- | src/development/make.md | 105 |
3 files changed, 107 insertions, 0 deletions
diff --git a/src/SUMMARY.md b/src/SUMMARY.md index de39c2d..9d3db19 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -37,6 +37,7 @@ - [c++](./development/c++.md) - [glibc](./development/glibc.md) - [gcc](./development/gcc.md) + - [make](./development/make.md) - [ld.so](./development/ld.so.md) - [Arch](./arch/README.md) diff --git a/src/development/README.md b/src/development/README.md index bda0ca3..2446271 100644 --- a/src/development/README.md +++ b/src/development/README.md @@ -4,4 +4,5 @@ - [c++](./c++.md) - [glibc](./glibc.md) - [gcc](./gcc.md) +- [make] (./make.md) - [ld.so](./ld.so.md) diff --git a/src/development/make.md b/src/development/make.md new file mode 100644 index 0000000..751b460 --- /dev/null +++ b/src/development/make.md @@ -0,0 +1,105 @@ +# make(1) + +## Anatomy of `make` rules +```make +target .. : prerequisite .. + recipe + .. +``` + +- `target`: an output generated by the rule +- `prerequisite`: an input that is used to generate the target +- `recipe`: list of actions to generate the output from the input + +## Pattern rules & Automatic variables +### Pattern rules +A pattern rule contains the `%` char (exactly one of them) and look like this example: +```make +%.o : %.c + $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ +``` +The target matches files of the pattern `%.o`, where `%` matches any none-empty +substring and other character match just them self. + +The substring matched by `%` is called the `stem`. + +`%` in the prerequisite stands for the matched `stem` in the target. + +### Automatic variables +As targets and prerequisites in pattern rules can't be spelled explicitly in +the recipe, make provides a set of automatic variables to work with: +- `$@`: Name of the target that triggered the rule. +- `$<`: Name of the first prerequisite. +- `$^`: Names of all prerequisites (without duplicates). +- `$+`: Names of all prerequisites (with duplicates). +- `$*`: Stem of the pattern rule. + +```make +# file: Makefile + +all: foobar blabla + +foo% bla%: aaa bbb bbb + @echo "@ = $@" + @echo "< = $<" + @echo "^ = $^" + @echo "+ = $+" + @echo "* = $*" + @echo "----" + +aaa: +bbb: +``` + +Running above `Makefile` gives: +```test +@ = foobar +< = aaa +^ = aaa bbb ++ = aaa bbb bbb +* = bar +---- +@ = blabla +< = aaa +^ = aaa bbb ++ = aaa bbb bbb +* = bla +---- +``` + +## Useful functions + +### Substitution references +Substitute strings matching pattern in a list. +```make +in := a.o l.a c.o +out := $(in:.o=.c) +# => out = a.c l.a c.c +``` + +### `filter` +Keep strings matching a pattern in a list. +```make +in := a.a b.b c.c d.d +out := $(filter %.b %.c, $(in)) +# => out = b.b c.c +``` + +### `filter-out` +Remove strings matching a pattern from a list. +```make +in := a.a b.b c.c d.d +out := $(filter-out %.b %.c, $(in)) +# => out = a.a d.d +``` + +### `abspath` +Resolve each file name as absolute path (don't resolve symlinks). +```make +$(abspath fname1 fname2 ..) + +### `realpath` +Resolve each file name as canonical path. +```make +$(realpath fname1 fname2 ..) +``` |