SMOLNET PORTAL home about changes
iInitial commit of english_knight. - english_knight - A ninja replacement.	Err	bitreich.org	70
hgit clone git://bitreich.org/english_knight git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/english_knight	URL:git://bitreich.org/english_knight git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/english_knight	bitreich.org	70
1Log	/scm/english_knight/log.gph	bitreich.org	70
1Files	/scm/english_knight/files.gph	bitreich.org	70
1Refs	/scm/english_knight/refs.gph	bitreich.org	70
1Tags	/scm/english_knight/tag	bitreich.org	70
1README	/scm/english_knight/file/README.md.gph	bitreich.org	70
1LICENSE	/scm/english_knight/file/LICENSE.gph	bitreich.org	70
i---	Err	bitreich.org	70
1commit 6a9c9e7385c6976e2297bcc743abde34a0eceb99	/scm/english_knight/commit/6a9c9e7385c6976e2297bcc743abde34a0eceb99.gph	bitreich.org	70
hAuthor: Christoph Lohmann <20h@r-36.net>	URL:mailto:20h@r-36.net	bitreich.org	70
iDate:   Sun, 10 Apr 2022 19:27:37 +0200	Err	bitreich.org	70
i	Err	bitreich.org	70
iInitial commit of english_knight.	Err	bitreich.org	70
i	Err	bitreich.org	70
iA ninja replacement.	Err	bitreich.org	70
i	Err	bitreich.org	70
iDiffstat:	Err	bitreich.org	70
i  A LICENSE                             |       2 ++	Err	bitreich.org	70
i  A README.md                           |      33 +++++++++++++++++++++++++++++++	Err	bitreich.org	70
i  A english_knight                      |     231 +++++++++++++++++++++++++++++++	Err	bitreich.org	70
i	Err	bitreich.org	70
i3 files changed, 266 insertions(+), 0 deletions(-)	Err	bitreich.org	70
i---	Err	bitreich.org	70
1diff --git a/LICENSE b/LICENSE	/scm/english_knight/file/LICENSE.gph	bitreich.org	70
i@@ -0,0 +1,2 @@	Err	bitreich.org	70
i+This content is in public domain.	Err	bitreich.org	70
i+	Err	bitreich.org	70
1diff --git a/README.md b/README.md	/scm/english_knight/file/README.md.gph	bitreich.org	70
i@@ -0,0 +1,33 @@	Err	bitreich.org	70
i+# English Knight	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+Are you #french?	Err	bitreich.org	70
i+gopher://bitreich.org/9/memecache/french.mkv	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+Just a question.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The topic is about replacing ninjas. Have you ever thought of not being a	Err	bitreich.org	70
i+ninja, but some english knight?	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+Here is your chance.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+## Let's start.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+Just run	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+        ./english_knight	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+and you will see the light.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+## Got Problems?	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+Be a #black-knight.	Err	bitreich.org	70
i+gopher://bitreich.org/9/memecache/black-knight.mp4	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+Fight for the glory of the good old ways!	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+In case you get stuck, use the #holy-hand-grenade and retry.	Err	bitreich.org	70
i+gopher://bitreich.org/9/memecache/holy-hand-grenade.mkv	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+Have fun!	Err	bitreich.org	70
i+	Err	bitreich.org	70
1diff --git a/english_knight b/english_knight	/scm/english_knight/file/english_knight.gph	bitreich.org	70
i@@ -0,0 +1,231 @@	Err	bitreich.org	70
i+#!/bin/sh	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+cat <<EOF	Err	bitreich.org	70
i+#[1]Seninha's notes	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+                  A Template for Portable Idiomatic Makefiles	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+[2]seninha.org	Err	bitreich.org	70
i+2022-04-07	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+In this post, I present the template I use to write portable, idiomatic	Err	bitreich.org	70
i+Makefiles for building C programs.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+I use ${MYVAR}, with curly braces, rather than $(MYVAR), with parentheses, but	Err	bitreich.org	70
i+both notations are supported. In this document I identify two different people:	Err	bitreich.org	70
i+the developer or Makefile author, who writes the Makefile; and the user or	Err	bitreich.org	70
i+package maintainer, who defines the proper variables and run make(1).	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The Project Files	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+There are several files involved in the building process: the target files to be	Err	bitreich.org	70
i+built, the source files, and the intermediate files. They may be referenced	Err	bitreich.org	70
i+several times by different rules, so it is a good practice to name them with	Err	bitreich.org	70
i+variables. Suppose we're building a program called myprog composed of three	Err	bitreich.org	70
i+modules. These are the variables to be defined:	Err	bitreich.org	70
i+PROG = myprog	Err	bitreich.org	70
i+SRCS = main.c parse.c util.c	Err	bitreich.org	70
i+OBJS = main.o parse.o util.o	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The varialbe PROG is the final file; SRCS lists the source files; and OBJS lists	Err	bitreich.org	70
i+the intermediate, object files. Note that both the list of source files and of	Err	bitreich.org	70
i+object files are almost equal, differing only by the extension of the files.	Err	bitreich.org	70
i+POSIX make(1) has a notation for changing the ending of each word in a variable.	Err	bitreich.org	70
i+In order to avoid repeating ourselves, we can use this notation to define	Err	bitreich.org	70
i+${OBJS}.	Err	bitreich.org	70
i+PROG = myprog	Err	bitreich.org	70
i+SRCS = main.c parse.c util.c	Err	bitreich.org	70
i+OBJS = ${SRCS:.c=.o}	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+We want to build our program when we call make without any arguments. To do	Err	bitreich.org	70
i+this, the first target should be ${PROG} itself. However, it is a common	Err	bitreich.org	70
i+practice to use the target all to build the final files. So the first target is	Err	bitreich.org	70
i+all, which just has ${PROG} as prerequisite.	Err	bitreich.org	70
i+all: ${PROG}	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+Next we need to declare the dependencies between the program modules. This is	Err	bitreich.org	70
i+done with rules without commands.	Err	bitreich.org	70
i+main.o: parse.h util.h	Err	bitreich.org	70
i+parse.o: parse.h util.h	Err	bitreich.org	70
i+util.o: util.h	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The Compilation Rules	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The compilation process is split in two parts: generate the object files from	Err	bitreich.org	70
i+the source files, and generate the program from the object files. So we need two	Err	bitreich.org	70
i+rules.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The following rule builds object files (.o) from source files (.c). The .c.o is	Err	bitreich.org	70
i+a inference rule that declare each .c file to be the prerequisite of a	Err	bitreich.org	70
i+homonymous .o file. This notation is defined by POSIX and is, therefore,	Err	bitreich.org	70
i+portable (different from the %.o: %.c rule, which is a GNU extension). In the	Err	bitreich.org	70
i+command of an inference rule (and only in the command of an inference rule), the	Err	bitreich.org	70
i+$< variable evaluates to the prerequisite file.	Err	bitreich.org	70
i+.c.o:	Err	bitreich.org	70
i+        ${CC} -I/usr/X11R6/include ${CFLAGS} ${CPPFLAGS} -c $<	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+We use the variable ${CC} to expand to the proper C compiler command. This	Err	bitreich.org	70
i+variable is set by default to the proper command. We should use this variable	Err	bitreich.org	70
i+rather than hardcoding it to gcc, for example.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The variables ${CFLAGS} and ${CPPFLAGS} contains options that the user or	Err	bitreich.org	70
i+package maintainer wants to pass to the compiler or preprocessor. It is a bad	Err	bitreich.org	70
i+practice to define those variables in a Makefile; let the user (or package	Err	bitreich.org	70
i+maintainer) define them. Any option that must be passed to the compiler (such as	Err	bitreich.org	70
i+-I/usr/X11R6/include above) should be passed before those variables. If the	Err	bitreich.org	70
i+Makefile author, for example, define ${CFLAGS} to -I/usr/X11R6/include, either	Err	bitreich.org	70
i+this value may override the values set by the user, or the values set by the	Err	bitreich.org	70
i+user may shadow the option set by the Makefile author.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The following rule links all object files into the program. We define ${OBJS} to	Err	bitreich.org	70
i+be the prerequisites of ${PROG}. The $@ variable evaluates to the target file	Err	bitreich.org	70
i+(${PROG} in our case). Since this is not an inference rule, the $< variable	Err	bitreich.org	70
i+cannot be used; we must write ${OBJS} both in the rule and in the command.	Err	bitreich.org	70
i+${PROG}: ${OBJS}	Err	bitreich.org	70
i+        ${CC} -o $@ ${OBJS} -L/usr/X11R6/lib -lX11 ${LDFLAGS}	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The variable ${LDFLAGS} contains options that the user or package maintainer	Err	bitreich.org	70
i+wants to pass to the linker. Again, it is a bad practice to define it in the	Err	bitreich.org	70
i+Makefile. The options -L/usr/X11R6/lib and -lX11 are passed before this variable	Err	bitreich.org	70
i+(so the user can override or increment them if necessary).	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The Installation rules	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+Your Makefile may include rules for installing the final files in the system. In	Err	bitreich.org	70
i+this example, two files are installed, ${PROG} (the final, compiled program),	Err	bitreich.org	70
i+and ${PROG}.1 (the manpage, named as the program followed by .1). The following	Err	bitreich.org	70
i+rule performs the installation.	Err	bitreich.org	70
i+PREFIX    = /usr/local	Err	bitreich.org	70
i+MANPREFIX = ${PREFIX}/share/man	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+install: all	Err	bitreich.org	70
i+        mkdir -p ${DESTDIR}${PREFIX}/bin	Err	bitreich.org	70
i+        mkdir -p ${DESTDIR}${MANPREFIX}/man1	Err	bitreich.org	70
i+        install -m 755 ${PROG} ${DESTDIR}${PREFIX}/bin/${PROG}	Err	bitreich.org	70
i+        install -m 644 ${PROG}.1 ${DESTDIR}${MANPREFIX}/man1/${PROG}.1	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+Before installing, the program should have been built; therefore all must be a	Err	bitreich.org	70
i+prerequisite for install.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The user or package maintainer can set the variable ${DESTDIR} to specify a	Err	bitreich.org	70
i+different installation destination. This variable must be prepended to each	Err	bitreich.org	70
i+installation path; and the Makefile author should not define it (it is left to	Err	bitreich.org	70
i+the user or package maintainer to define it). Note that there is no bar	Err	bitreich.org	70
i+separating ${DESTDIR} from what follows, because the ${PREFIX} and ${MANPREFIX}	Err	bitreich.org	70
i+variables should already begin with a bar.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The Makefile author, however, is expected to define two variables pointing to	Err	bitreich.org	70
i+installation prefixes: ${PREFIX}, pointing to the general installation prefix;	Err	bitreich.org	70
i+and ${MANPREFIX}, pointing to the manual page installation prefix. There are	Err	bitreich.org	70
i+other commonly defined prefixes, such as ${bindir}, set to ${PREFIX}/bin. The	Err	bitreich.org	70
i+user or package maintainer can then invoke make(1) with those variables assigned	Err	bitreich.org	70
i+to different prefixes. On most GNU/Linux systems, for example, ${PREFIX} is	Err	bitreich.org	70
i+assigned to /usr; and on OpenBSD, ${MANPREFIX} is assigned to ${PREFIX}/man	Err	bitreich.org	70
i+(without the share/ part).	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The variables ${PREFIX} and ${MANPREFIX} are not automatically assigned, but	Err	bitreich.org	70
i+they can be changed by the user or package maintainer. These variables are	Err	bitreich.org	70
i+commonly assigned in the Makefile by the Makefile author with the ?= operator,	Err	bitreich.org	70
i+which assign them only if not already defined, rather than with the common =	Err	bitreich.org	70
i+operator. Thus, the values of these variables can be inherited from the	Err	bitreich.org	70
i+environment, and the user need not have to assign them on each invocation. This	Err	bitreich.org	70
i+operator is a non-POSIX extension, however, although supported by both GNU and	Err	bitreich.org	70
i+BSD make implementations.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+Looking back at the installation commands, we first use mkdir(1) to create the	Err	bitreich.org	70
i+destination directories, and then use install(1) to install them. We could	Err	bitreich.org	70
i+simply call install with the -D flag, which automatically creates the	Err	bitreich.org	70
i+destination directories if necessary. However, this option is an extension and	Err	bitreich.org	70
i+is not supported by some implementations (such as FreeBSD's). Remember to	Err	bitreich.org	70
i+install each file with its proper permission modes with the -m option.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The Makefile author can also create a uninstallation rule, which simply remove	Err	bitreich.org	70
i+the files from their destination directories.	Err	bitreich.org	70
i+uninstall:	Err	bitreich.org	70
i+        rm ${DESTDIR}${PREFIX}/bin/${PROG}	Err	bitreich.org	70
i+        rm ${DESTDIR}${MANPREFIX}/man1/${PROG}.1	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The Cleaning Rule	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The Makefile author can define a rule to clean the build directory and revert it	Err	bitreich.org	70
i+to its original state. Such rule is commonly called clean. It removes the	Err	bitreich.org	70
i+intermediate object files and the final files. As convenience, for the developer	Err	bitreich.org	70
i+to clean the build directory from core files that may be created by the system	Err	bitreich.org	70
i+during the development, the clean rule can also delete .core files.	Err	bitreich.org	70
i+clean:	Err	bitreich.org	70
i+        -rm -f ${OBJS} ${PROG} ${PROG:=.core}	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+Note that the command of this rule begins with an hyphen -. This causes make to	Err	bitreich.org	70
i+not return error (non-zero) exit status when the command fails. This is handy,	Err	bitreich.org	70
i+for cleaning an already cleaned build directory to not print errors.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The Phony Targets	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+In a Makefile, some rules specify “virtual” targets which do not correspond to	Err	bitreich.org	70
i+any file to be created. These are the “phony” targets. The .PHONY special target	Err	bitreich.org	70
i+is used to mark its prerequisites as phony targets. In our Makefile, we have	Err	bitreich.org	70
i+four phony targets: all, install, uninstall, and clean.	Err	bitreich.org	70
i+.PHONY: all clean install uninstall	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+The Makefile	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+In the end, our Makefile should look like this:	Err	bitreich.org	70
i+PROG = myprog	Err	bitreich.org	70
i+SRCS = main.c util.c	Err	bitreich.org	70
i+OBJS = ${SRCS:.c=.o}	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+PREFIX    = /usr/local	Err	bitreich.org	70
i+MANPREFIX = ${PREFIX}/share/man	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+all: ${PROG}	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+main.o: parse.h util.h	Err	bitreich.org	70
i+parse.o: parse.h util.h	Err	bitreich.org	70
i+util.o: util.h	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+.c.o:	Err	bitreich.org	70
i+        ${CC} -I/usr/X11R6/include ${CFLAGS} ${CPPFLAGS} -c $<	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+${PROG}: ${OBJS}	Err	bitreich.org	70
i+        ${CC} -o $@ ${OBJS} -L/usr/X11R6/lib -lX11 ${LDFLAGS}	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+install: all	Err	bitreich.org	70
i+        mkdir -p ${DESTDIR}${PREFIX}/bin	Err	bitreich.org	70
i+        mkdir -p ${DESTDIR}${MANPREFIX}/man1	Err	bitreich.org	70
i+        install -m 755 ${PROG} ${DESTDIR}${PREFIX}/bin/${PROG}	Err	bitreich.org	70
i+        install -m 644 ${PROG}.1 ${DESTDIR}${MANPREFIX}/man1/${PROG}.1	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+uninstall:	Err	bitreich.org	70
i+        rm ${DESTDIR}${PREFIX}/bin/${PROG}	Err	bitreich.org	70
i+        rm ${DESTDIR}${MANPREFIX}/man1/${PROG}.1	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+clean:	Err	bitreich.org	70
i+        -rm -f ${OBJS} ${PROG} ${PROG:=.core}	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+.PHONY: all clean install uninstall	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+tl;dr	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+  * Define variables for the final files to be built, the source files, and the	Err	bitreich.org	70
i+intermediate object files created by the building process. Those are commonly	Err	bitreich.org	70
i+named ${PROG}, ${SRCS} and ${OBJS}, respectively.	Err	bitreich.org	70
i+  * Include in the Makefile, but do not assign them, the variables ${CFLAGS},	Err	bitreich.org	70
i+${CPPFLAGS}, ${LDFLAGS} and ${DESTDIR}. They should be assigned by the user or	Err	bitreich.org	70
i+package maintainer.	Err	bitreich.org	70
i+  * Evaluate the flag variables (${CFLAGS}, ${CPPFLAGS}, and ${LDFLAGS}) after	Err	bitreich.org	70
i+any hardcoded flag, so the user or package maintainer can override it.	Err	bitreich.org	70
i+  * Include the all and clean phony targets. Optionally include install and	Err	bitreich.org	70
i+uninstall phony targets. Always mark them as .PHONY.	Err	bitreich.org	70
i+  * Do not use $< on anything but on the command of inference rules.	Err	bitreich.org	70
i+  * Do not use -D with install(1).	Err	bitreich.org	70
i+  * Do not call c99 or gcc manually. Call the command set in ${CC} instead.	Err	bitreich.org	70
i+  * Assign ${PREFIX} and ${MANPREFIX} to the proper installation prefixes. You	Err	bitreich.org	70
i+can assign them with the ?= operator for the user convenience, but this	Err	bitreich.org	70
i+assignment operatior is not portable, although commonly supported.	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+References	Err	bitreich.org	70
i+	Err	bitreich.org	70
i+   1. file:///feed.xml	Err	bitreich.org	70
i+   2. https://seninha.org	Err	bitreich.org	70
i+EOF	Err	bitreich.org	70
i+	Err	bitreich.org	70
.
Response: text/plain
Original URLgopher://bitreich.org/0/scm/english_knight/commit/6a9c9e7...
Content-Typetext/plain; charset=utf-8