Comment: | begin move away from legacy build system |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
26c340d29ec3bfb3f93a3137b800ad4d |
User & Date: | lexi on 2019-08-19 04:51:01 |
Other Links: | manifest | tags |
2019-08-19
| ||
05:42 | switch over to new build mechanism and formally deprecate makefiles check-in: 34c625a47b user: lexi tags: trunk | |
04:51 | begin move away from legacy build system check-in: 26c340d29e user: lexi tags: trunk | |
02:18 | clean out legacy code check-in: 724bbbbe91 user: lexi tags: trunk | |
Added build.sh version [75da38cbfd].
1 +#!/usr/bin/env bash 2 +. global/common.sh 3 +noimpl() { 4 + say "$1 is not currently implemented on your system. an exciting chance for you to contribute, no?" 5 + exit 1 6 +} 7 + 8 +if test "$os$arch$bits" = ""; then 9 + say "set the following environment variables to the appropriate atoms describing your system. for help determining the appropriate atoms, see libk.md" 10 + say ' - $os={lin|fbsd|hai|osx…}' 11 + say ' - $arch={x86|arm|ia64|mips…}' 12 + say ' - $bits={|32|64…}' 13 + exit 1 14 +fi 15 + 16 +# TODO: make it possible for user to change 17 +# default set with environment vars 18 +modules=(kcore kmem kstr kio kgraft kfile) 19 + 20 +target=$arch.$os 21 +if test "$bits" != ""; then 22 + target=$target.$bits 23 +fi 24 + 25 +case $os in 26 + lin|?bsd|and|dar|osx) posix=yes; unix=yes;; 27 + hai) posix=yes; unix=no;; 28 + *) posix=no; unix=no;; 29 +esac 30 + 31 +case $os.$bits in 32 + win.32) bin_fmt=win32;; 33 + win.64) bin_fmt=win64;; 34 + osx.32|dar.32) bin_fmt=macho32;; 35 + osx.64|dar.64) bin_fmt=macho64;; 36 + dos.*) bin_fmt=dosexe;; 37 + none.*) bin_fmt=bin;; 38 + 32) bin_fmt=elf32;; 39 + 64) bin_fmt=elf64;; 40 +esac 41 + 42 +# first, we establish the 43 +# parameters of the build 44 +has gcc && _cc=gcc 45 +has clang && _cc=clang 46 +has cc && _cc=cc 47 +cc=${cc:-$_cc} 48 +has m4 && _m4=m4 49 +m4=${m4:-$_m4} 50 +has yasm && asm=yasm 51 +has nasm && asm=nasm 52 +export out=${out:-out} 53 +export gen=${gen:-gen} 54 +library=${libkind:-static} # {static|shared|both} 55 + 56 +case $library in 57 + static) build_static_library=yes 58 + build_shared_library=no;; 59 + 60 + shared) build_static_library=no 61 + build_shared_library=yes;; 62 + 63 + both) build_static_library=yes 64 + build_shared_library=yes;; 65 +esac 66 + 67 +check cc "your C compiler of choice" 68 +check asm "an assembler that takes Intel syntax and nasm-style-macros" 69 +check m4 "the path to your m4 installation" 70 + 71 +export build=$(global/build-id.sh) 72 + 73 +case $os in 74 + lin) p_headers_syscall=${p_headers_syscall:-/usr/include/asm/unistd_${bits}.h} 75 + p_headers_errno=${p_headers_errno:-/usr/include/asm-generic/errno.h /usr/include/asm-generic/errno-base.h};; 76 + 77 + fbsd) p_headers_syscall=${p_headers_syscall:-/usr/include/sys/syscall.h} 78 + p_headers_errno=${p_headers_errno:-/usr/include/errno.h};; 79 +esac 80 + 81 +check p_headers_syscall \ 82 + 'the location of a header defining your syscall numbers' 83 +check p_headers_errno \ 84 + 'the location of a header defining the values of each errno symbol' 85 + 86 +macro_compile_env="-Datom_target_arch=$arch -Datom_target_os=$os -Dtarget_posix=$posix -Dtarget_unix=$unix" 87 + 88 +if test "$bits" != ""; then 89 + macro_compile_env="$macro_compile_env -Datom_target_bits=$bits" 90 +fi 91 + 92 +for mod in ${modules[@]}; do 93 + say "building [1mlibk[21m with [1m$mod[21m" 94 +done 95 + 96 +comp_mac() { $m4 $macro_compile_env -I "$gen" $3 "$1" > "$2"; } 97 +comp_asm() { $asm "-f$bin_fmt" "-i$gen" $1 -o "$2"; } 98 +comp_co() { comp_clib $1 $2 -c; } 99 +comp_clib(){ 100 + src=$1 101 + output=$2 102 + flags=$3 103 + $cc $src $3 -std=c11 -isystem "$out" -isystem "$gen" -isystem "arch/" -fPIC -nostdlib "-L$out" "-o$output" 104 +} 105 + 106 +scan() { find "$1" -name "$2"; } 107 + 108 +# now we make sure all the appropriate 109 +# directories exist 110 + 111 +set -x 112 + 113 +# get type data 114 +mkdir -p $gen 115 +$cc -D_emit_m4_include arch/typesize.c -o $gen/typesize 116 +$gen/typesize > gen/typesize.m 117 + 118 +# generate syscall tables 119 +case $os in 120 + lin) grep -h "#define __NR_" $p_headers_syscall | sed 's;^#define __NR_;;' > $gen/calls.tbl;; 121 + fbsd) grep -h "#define SYS_" $p_headers_syscall | sed 's;^#define SYS_;;' | sed 's;[\t ]\+; ;' > $gen/calls.tbl;; 122 + *) noimpl 'system call table generation';; 123 +esac 124 + 125 +awk -f arch/syscall.awk -v out=s <$gen/calls.tbl >$gen/system_calls.s 126 +awk -f arch/syscall.awk -v out=h <$gen/calls.tbl >$gen/system_calls.h 127 + 128 +# generate errno tables 129 +grep -h "#[ ]*define[ ]\+E" $p_headers_errno | sed 's;^#[\t ]*define[\t ]\+\(E[A-Z0-9]\+\).*$;k_platform_error_\1 \1;' > $gen/error_names.tbl 130 +cat $p_headers_errno $gen/error_names.tbl | cpp -P >$gen/error_numbers.tbl 131 +awk -f arch/errtbl.awk <$gen/error_numbers.tbl >$gen/error_table.h 132 + 133 +# generate symbol tables for error handling functions 134 +mkdir -p $out/k 135 +awk -f global/gen-conds.awk <global/modules >$out/k/internal.egroup.h 136 +awk -f global/gen-ident.awk <global/modules >$gen/internal.ident.c 137 +comp_co $gen/internal.ident.c $out/internal.ident.o 138 + 139 +# first pass: copy all raw headers into place 140 +for mod in ${modules[@]}; do 141 + for h in $(scan $mod '*.h'); do 142 + base=$(basename $h) 143 + cp "$h" "$out/k/$base" 144 + done 145 +done 146 + 147 +# second pass: run macro headers through m4 and 148 +# copy the resulting file into place 149 + 150 +for mod in ${modules[@]}; do 151 + for h in $(scan $mod '*.h.m'); do 152 + base=$(basename $h) 153 + dest=${base%%.m} 154 + comp_mac "$h" "$out/k/$dest" 155 + done 156 +done 157 + 158 +# third pass: generate manpage, html, and pdf 159 +# versions of the documentation from the md src 160 + 161 +mkdir -p $out/doc/{man,html,pdf} 162 +global/build-manpage.sh libk.md 163 +for mod in ${modules[@]}; do 164 + for doc in $(scan $mod '*.md'); do 165 + base="$(basename $doc)" 166 + stem="${base%%.md}" 167 + global/build-manpage.sh "$doc" 168 + # TODO html 169 + # TODO pdf 170 + done 171 +done 172 + 173 +# fourth pass: compile sources and save the 174 +# resulting object files to $out, tracking 175 +# which is a runtime or function unit. exe's 176 +# will not be compiled until a later pass 177 + 178 +fn_objects=() 179 +rt_objects=() 180 +data_objects=( $out/internal.ident.o ) 181 +for mod in ${modules[@]}; do 182 + for fn in $(scan $mod '*.fn.c'); do 183 + base="$(basename $fn)" 184 + dest="$out/$mod.${base%%.c}.o" 185 + comp_co "$fn" "$dest" 186 + fn_objects+="$dest" 187 + done 188 + 189 + for rt in $(scan $mod '*.rt.c'); do 190 + base="$(basename $rt)" 191 + dest="$out/$mod.${base%%.c}.o" 192 + comp_co "$rt" "$dest" 193 + rt_objects+="$dest" 194 + done 195 + 196 + for fn in $(scan $mod "*.fn.$target.s"); do 197 + base="$(basename $fn)" 198 + dest="$out/$mod.${base%%.s}.o" 199 + comp_asm "$fn" "$dest" 200 + fn_objects+="$dest" 201 + done 202 + 203 + for rt in $(scan $mod "*.rt.$target.s"); do 204 + base="$(basename $rt)" 205 + dest="$out/$mod.${base%%.s}.o" 206 + comp_asm "$rt" "$dest" 207 + rt_objects+="$dest" 208 + done 209 +done 210 + 211 +echo fns: ${fn_objects[@]} 212 +echo rts: ${rt_objects[@]}
Added clean.sh version [61a9f12cd8].
1 +#!/usr/bin/env bash 2 +. global/common.sh 3 + 4 +out=${out:-out} 5 +gen=${gen:-gen} 6 + 7 +say "cleaning libk build artifacts" 8 + 9 +set -x 10 +rm -r $out 11 +rm -r $gen
Modified global/build-id.sh from [5d0ff4e06b] to [a95857b352].
2 2 . global/common.sh 3 3 4 4 origin=1566169297 5 5 now=$(date +%s) 6 6 delta=$(expr $now - $origin) 7 7 8 8 9 -if test "$1" = "private"; then 9 +if test "$id_mode" = "private"; then 10 10 # for privacy's sake, we're not to show 11 11 # the hostname. instead, we calculate a 12 12 # hash based on the time delta and the 13 13 # output of uname -a for extra entropy 14 14 if has "$HASH"; then 15 15 hash="$HASH" 16 16 elif has sha1; then
Modified global/build-manpage.sh from [199ffb3202] to [fbe8d4ad27].
1 1 #!/usr/bin/env sh 2 -(test -d global && test -d $1) || { 2 +(test -d global && test -f build.sh) || { 3 3 echo >&2 "($0) run [1m$me[21m from root of [1mlibk[21m source directory" 4 4 exit 1 5 5 } 6 6 7 7 . global/common.sh 8 -test "$OUT" = "" && { 9 - say "\$OUT environment variable must be set to your build directory - are you running this script by hand? run [1mmake doc[21m in the root directory instead!" 8 +test "$out" = "" && { 9 + say "\$out environment variable must be set to your build directory - are you running this script by hand? run [1mmake doc[21m in the root directory instead!" 10 10 exit 2 11 11 } 12 12 13 13 if ! has cmark; then 14 14 say "to generate documentation for [1mlibk[21m, install the [1mcmark[21m package and try again" 15 15 exit 3 16 16 fi 17 17 18 18 19 -name=$1 20 -mandest=$OUT/doc/man 21 -out=$2 22 -file=$name/$name.md 23 -desc="$(grep -m1 "^$name:" global/modules | cut -d: -f4)" 19 +file="$1" 20 +filename="$(basename "$file")" 21 +stem=${filename%%.md} 22 +mandest="$out/doc/man" 23 +section=${section:-4} 24 +out="$mandest/$stem.$section" 25 +desc_rec="$(grep -m1 "^$name:" global/modules)" 26 +if test $? -eq 0; then 27 + desc=" - $(echo $desc_rec | cut -d: -f4)" 28 +else 29 + desc="" 30 +fi 24 31 date="$(date -r"$file" "+%d %A %Y")" 25 32 26 -mkdir -p $mandest 33 +mkdir -p "$mandest" 27 34 28 -echo >$out ".TH ${name^^} \"$section\" \"$date\" \"hale.su libk [r$BUILD]\" \"modules\"" 35 +echo >"$out" ".TH ${name^^} \"$section\" \"$date\" \"hale.su libk [r$build]\" \"modules\"" 29 36 30 -echo >>$out ".SH NAME" 31 -echo >>$out "$name - $desc" 32 -echo >>$out ".SH SYNOPSIS" 37 +echo >>"$out" ".SH NAME" 38 +echo >>"$out" "$stem$desc" 39 +echo >>"$out" ".SH SYNOPSIS" 33 40 34 -dhead=$(grep -m1 -n "^# description$" $file) 41 +dhead=$(grep -m1 -n "^# description$" "$file") 35 42 if test $? -eq 0; then 36 43 descline=$(echo "$dhead" | cut -d: -f1) 37 44 offset=1 38 45 else 39 - descline=$(grep -m2 -n "^#" $file | tail -n1 | cut -d: -f1) 46 + descline=$(grep -m2 -n "^#" "$file" | tail -n1 | cut -d: -f1) 40 47 offset=0 41 48 fi 42 49 43 -tail -n+2 $file | head -n$(expr $descline - 2) | cmark -t man >> $out 50 +tail -n+2 $file | head -n$(expr $descline - 2) | cmark -t man >>"$out" 51 + 52 +echo >>"$out" ".SH DESCRIPTION" 53 + 54 +tail -n+$(expr $descline + $offset) "$file" | cmark -t man >> "$out" 44 55 45 -echo >>$out ".SH DESCRIPTION" 56 +if has gzip; then 57 + gzip -f "$out" 58 + out="$out.gz" 59 +fi 46 60 47 -tail -n+$(expr $descline + $offset) $file | cmark -t man >> $out 48 - 49 -say "wrote manpage for $name to $file" 61 +say "wrote manpage for $stem to $out"
Name change from global/genconds.awk to global/gen-conds.awk.
Name change from global/genident.awk to global/gen-ident.awk.
Added install.sh version [3dc81017e7].
1 +#!/usr/bin/env bash 2 +. global/common.sh 3 + 4 +say "this component of the build system does not yet exist"
Modified kcore/type.h.m from [ebd330ccf3] to [88fed336c0].
1 1 dnl kcore/type.h.m → <k/type.h> 2 2 dnl ~ lexi hale <lexi@hale.su> 3 3 dnl this file gathers information on the environment it's 4 4 dnl being compiled in, defining types that our code 5 5 dnl needs. it will be emitted as <k/type.h>. 6 6 dnl vim: ft=c 7 +include(`typesize.m') 7 8 changequote(`“',`”') 8 9 #ifndef KItype 9 10 #define KItype 10 11 11 12 /* we define 64-bit types first due to an oddity in how 12 13 * 128-bit types are handled: we want kc_?big to reference 13 14 * the absolute largest type available to the compiler,
Modified kio/io.h.m from [2374315a32] to [f45741da3f].
35 35 // another running process 36 36 kiostream_other 37 37 // no fuckin idea 38 38 } kiostream_kind; 39 39 40 40 typedef struct kiostream { 41 41 kiostream_kind kind; 42 - #include "kiostream.platform.h" 42 + ifelse(target_posix,`yes',` 43 + int platform_fd; 44 + ')dnl 43 45 } kiostream; 44 46 45 47 typedef struct kiochan { 46 48 kiostream in; 47 49 // text can be read from this stream 48 50 kiostream out; 49 51 // text can be written to this stream
Name change from kio/kiostream.posix.i to legacy/kiostream.posix.i.
Modified legacy/makefile from [0343c70b67] to [f9a6a3944e].
142 142 @# `ar rcs` in case `ar` isn't the GNU version 143 143 ar rc $@ $^ 144 144 ranlib $@ 145 145 146 146 $(OUT)/man/doc: 147 147 mkdir -p $@ 148 148 149 -$(OUT)/k/internal.egroup.h: global/modules global/genconds.awk $(OUT)/k 150 - awk -f global/genconds.awk <$< >$@ 151 -$(TMP)/internal.ident.c: global/modules global/genident.awk $(OUT)/k/internal.egroup.h $(TMP) 152 - awk -f global/genident.awk <$< >$@ 149 +$(OUT)/k/internal.egroup.h: global/modules global/gen-conds.awk $(OUT)/k 150 + awk -f global/gen-conds.awk <$< >$@ 151 +$(TMP)/internal.ident.c: global/modules global/gen-ident.awk $(OUT)/k/internal.egroup.h $(TMP) 152 + awk -f global/gen-ident.awk <$< >$@ 153 153 $(OUT)/%.o: $(TMP)/%.c $(OUT) 154 154 $(CC) $(cflags) -c $< -o $@ 155 155 156 156 $(OUT) $(OUT)/k $(TMP): 157 157 mkdir -p $@
Name change from makerule to legacy/makerule.
Name change from modmake to legacy/modmake.
Modified libk.md from [7bf377a763] to [debe7693f6].
12 12 13 13 hence, libk. 14 14 15 15 libk aims to offer a better, safer, and most importantly, less unpleasant foundation for modern code in C or any other language. it also aims to be much smaller, simpler, and faster than glibc to build so that there's no arduous bootstrapping process for new architectures. 16 16 17 17 currently, the only dependency on libc in any form is `arch/typesize.c`, a small binary tool which uses libc IO routines to print type information it calculates; however, this could also be augmented to use POISX IO routines, or even potentially libk IO routines to remove any external dependency at all -- the work would be nontrivial, but fully feasible. further, the file it creates can also _in extremis_ be created by hand. the final compiled libc binaries and headers do not depend on or reference libc in any way; typesize is only a makedepend. 18 18 19 -## goals 19 +# goals 20 20 21 21 libk's goals are far-reaching, and suggestions are welcome. note however that libk is *not* intended to be a kitchen-sink library like libiberty. it's meant to do one thing, and to it well: to provide an easy- and pleasant-to-use foundation for modern open source projects. below is a list of some of the project's major goals. 22 22 23 23 1. **IO.** libc's basic input/output mechanisms are dreadful, built at entirely the wrong level of abstraction. libk is intended to make many more primitives available to the user, and offer a sliding scale of abstraction so libk is suitable for a wide range of needs. 24 24 2. **file manipulation.** libc's file manipulation primitives are a relic of a bygone age and in dire need of upgrading. 25 25 3. **terminal manipulation.** libc has no provision for simple output formatting, a task that requires a combination of ANSI codes and in some cases pty manipulation with POSIX APIs, both of which are somewhat dark wizardry. this situation forces many innocent coders to drag in the entire unholy bulk of the aptly named library `ncurses`, much of whose code has been utterly obsolete for the last twenty years and whose API is one of the most singularly hateful ones in existence. libk therefore should offer a simple, straightforward way to do gracefully-degrading terminal sorcery. 26 26 4. **memory management.** the single memory management function `malloc()` provided by libc is absolutely pitiful. this is 2019. modern applications have much more exotic allocation needs, and a standard library should offer a range of allocators and management techniques, as well as abstract pointer objects so that pointers to objects of different allocation types (including static or stack allocation!) can be mixed freely and safely. ................................................................................ 27 27 5. **intrinsic reentrancy.** because *jesus christ,* libc. 28 28 6. **interprocess communication.** libc offers no useful IPC abstractions over the paltry array of tools POSIX &co. give us to work with. we can do better. 29 29 7. **tooling.** libk is intended as more than just a library. it's also intended to work with some basic tooling to automate tasks that current binary tooling is inadequate for -- for instance, embedding binary data into a program binary. (see module [kgraft](kgraft)) 30 30 8. **modularity.** libk is not part of the C specification and it isn't always going to be practical for developers to expect the entire library to be present on the end-user's computer. so libk is designed to be usable in many different ways -- as a traditional library, as a static library, in full form or with only components needed by the developer, to be distributed either on its own or as part of a binary. 31 31 9. **compatibility.** code that links against libk should be able to compile and run on any operating system. in the ideal case (Linux or FreeBSD) it will be able to do so without touching any other system libraries; for less ideal environments like Windows, libk will when necessary abstract over system libraries or libc itself. 32 32 10. **sane error-handling.** every time you type `errno` god murders a puppy. 33 33 34 -## dependencies 34 +# dependencies 35 35 36 36 libk is designed to be as portable and depedency-free as possible. ideally, it will be possible to compile code against libk using nothing but libk itself. 37 37 38 38 compiling libk is also designed to be as easy as possible. it has only two external dependencies, the macro processor [m4], needed for compile-time header generation, and the [GNU make] utility, whose advanced features are needed to perform the relatively complex task of building all of libk from the ground up. 39 39 40 40 [m4]: http://www.gnu.org/software/m4 41 41 [GNU make]: http://www.gnu.org/software/make 42 42 43 43 a different macro processor, gpp, was used in early versions of libk, however, it was so obscure and took so much overly fragile infrastructure to make it work that the cleaner syntax just wasn't worth it; i've since deleted the gpp infra and ported the macro files to m4. 44 44 45 -## naming conventions 45 +# naming conventions 46 46 47 47 one of the most frustrating things about libc is its complete and total *lack* of a naming convention. in C, every function and global is injected into a single global namespace, including macros. this means that every libc header you include scatters words all over that namespace, potentially clobbering your function with a macro! 48 48 49 49 libk is designed to fix this (in hindsight) glaring error. 50 50 51 51 however, a common problem with libraries is the proliferation of inordinately long and hard-to-type function names such as `SuperWidget_Widget_Label_Font_Size_Set()`. this may be tolerable in IDEs with robust auto-complete or when referencing a highly-specific, sparsely-used library; it is however completely intolerable in the case of a core library with heavily used functionality. 52 52 ................................................................................ 61 61 in both naming conventions, the following rules apply: 62 62 63 63 1. the possible values of enumeration types are always preceded by the name of the enumeration type and an underscore. for instance, the enum `ksalloc` has a value named `ksalloc_static`. **exception:** an enum named `<S>_kind`, where `<S>` is a struct type, may simply use the prefix `<S>_`. 64 64 2. macros begin with the uppercase letter `K` -- e.g. `Kmacro`. macros that can be defined by the user to alter the behavior of the api should begin with `KF` if they are on/off flags, or `KV` otherwise. 65 65 3. capital letters are only used in macro prefixes. 66 66 4. low-level function names are prefixed with the API they call into. for example, the function that performs the POSIX syscall `write` is named `kio_posix_fd_write`. a wrapper around the Windows function `CreateProcess()` might be called `kproc_win_createprocess`. 67 67 68 -### atoms 68 +## atoms 69 69 70 70 libk uses the concept of "atoms" (small, regular strings of text) to standardize common references, such as operating systems or processor architectures. 71 71 72 -#### operating systems 72 +### operating systems 73 73 74 74 these atoms will be used to reference operating systems. 75 75 76 76 * Linux: `lin` 77 + 1. aaaa 77 78 * Haiku: `hai` 78 79 * Android: `and` 79 80 * FreeBSD: `fbsd` 80 81 * NetBSD: `nbsd` 81 82 * OpenBSD: `obsd` 82 83 * Darwin/Mac OS X/iOS: `dar` 83 84 * MS-DOS: `dos` 84 85 * FreeDOS: `fdos` 85 86 * Windows: `win` 86 87 * Windows MinGW: `mgw` 87 88 88 -#### file extensions 89 +### file extensions 89 90 90 91 * C function implementations: `*.c` 91 92 * C module headers: `*.h` 92 93 * ancillary C headers: `*.inc.h` 93 94 * assembly code: `*.s` 94 95 95 -#### arches 96 +### arches 96 97 97 98 these atoms will be used to reference particular system architectures. these will mostly be used in the filenames of assembly code. 99 + * Intel/AMD x86: `x86` 100 + * ARM: `arm` (aarch64 is specified by `os=arm bits=64`) 101 + * MIPS: `mips` 102 + * Itanium: `ia64` (no bits) 103 + * PowerPC: `ppc` 98 104 99 -## macros 105 +# localization 106 + 107 +libk does not interface, respect, or wrap system locale APIs in any way. localization, for the most part, is the responsibility of the developer. this is necessary in order to prevent hidden state from accreting, which lets us make certain invariant guarantees about library behavior that can prevent highly confusing bugs or potentially even have security implications. 108 + 109 +this is not to say that libk supports only one language, one calendar, and one culture. mechanisms will exist to produce localized output; however, they require the developer to explicitly pass localization flags and state. that is to say, they are stateless and opt-in only. the user changing an environment variable will never cause, e.g., decimal points to turn into commas unless the coder explicitly specified that behavior. 110 + 111 +libk uses UTF8 exclusively. it has no concept of codepages or non-unicode charsets. 112 + 113 +# macros 100 114 101 115 libk will not in any circumstance use macros to encode magic numbers, instead using typedef'd enums. all libk macros begin with the uppercase letter `K` -- e.g. `Kmacro`. macros that can be defined by the user to alter the behavior of the api should begin with `KF` if they are on/off flags, or `KV` otherwise. **macros should only be defined by the libk headers if the flag `KFclean` is *not* defined at the time of inclusion.** 102 116 103 117 include guards take the form of the bare module name prefixed by `KI`. so to test if `k/term.h` has been included, you could write `#ifdef KIterm`. 104 118 105 -## languages 119 +# languages 106 120 107 -libk uses only three languages: C (\*.c, \*.h), yasm (\*.s), and make (makefile). 121 +libk uses only five widely-used and standardized computer languages: C (\*.c, \*.h), yasm (\*.s), awk (\*.awk), commonmark (\*.md), and bash (\*.sh). further languages may not be introduced into the project without explicit advance approval of the maintainer herself. 108 122 109 123 other assemblers will probably be necessary for the more exotic targets, however. 110 124 111 -## repository structure 125 +# repository structure 112 126 113 127 libk uses a strict directory structure for code, and deviations from this structure will not be tolerated without extremely good reason. 114 128 115 -total segregation is maintained between source code, temporary files, and output objects. source is found in module directories (`k*/`). the destination for temporary files and output objects are retargetable via the `make` parameters `TMP= OUT=`, but default to `tmp/` and `out/`, which are excluded from repo with fossil's `ignore-glob` setting. 129 +total segregation is maintained between source code, temporary files, and output objects. source is found in module directories (`k*/`). the destination for temporary files and output objects are retargetable via the environment variables `gen= out=`, but default to `gen/` and `out/`, which are excluded from repo with fossil's `ignore-glob` settingapproval of the maintainer herself. 116 130 117 131 all libk code is dispersed into modules: `kcore` for internals, `kio` for I/O, `kgraft` for binary packing, etc. each module has a folder in the root directory. (libk does not have submodules.) inside each module's directory should be a header with the same name as the module (see **naming conventions** above). 118 132 119 133 each function should be kept in a separate file within its module's directory. the file's name should consist of the dot-separated fields [name, class, "c"] for C sources, or [name, class, arch, OS, bits, format, "s"] for assembly sources, where "name" is the name of the function without the module prefix and "class" is `rt` if the file is part of the libk runtime, or `fn` otherwise. this distinction is necessary because while the static library `libk.a` can include runtime objects, the shared library `libk.so` cannot. examples: 120 134 121 135 * a C file in the module `kstr` named `kscomp` would be named `kstr/comp.fn.c` 122 136 * a runtime assembly file called `boot` in the module `kcore` for x86-64 linux would be named `kcore/boot.rt.x86.lin.64.s` 123 137 * the 32-bit x86 haiku version of a function called `kiowrite` defined in assembly would be named `kio/write.fn.x86.hai.32.s`. 124 138 125 139 each module should have a header named the same thing as the module except without the `k` prefix. (e.g. the header for `kio` is `kio/io.h`) located in its folder. this is the header that the end-user will be importing, and should handle any user-defined flags to present the API the user has selected. 126 140 127 -each module directory should contain a makefile that can build that module. see **makefiles** below. all makefiles should be named `makefile` (**not** `Makefile`). 128 - 129 141 each module should contain a markdown file. this file's name should be the name of the parent directory suffixed with `.md`; for instance, `kterm` should contain the file `kterm/kterm.md`. this file should document the module as thoroughly as possible 130 142 131 143 each module may contain any number of files of the name `*.exe.c`. this files will be treated as *tools* by the build system and compiled as executables, rather than libraries. they should be compiled to `out/$module.$tool` 132 144 133 145 the repository root and each module may also contain the directory `misc`. this directory may be used to store miscellaneous data such as ABI references, developer discussions, and roadmaps. if the `misc` directory is deleted, this must not affect the library or build system's function in any way - that is, nothing outside a `misc` folder may reference a `misc` folder or anything inside it, including documentation. the `misc` directory should be removed when its contents are no longer needed. in most cases, the repository wiki and forum should be used instead of the `misc` folder. 134 146 135 147 the folder `arch` in the root of the repository contains syscall tables and ABI implementations for various architectures. 136 148 137 -## makefiles 149 +## build sysem 138 150 139 -libk uses `make` as its build system. makefiles should be handwritten. there will be one global makefile in the root of the repository, and one makefile for each module. 151 +libk uses a very simple build system. the entire project is built with the `build.sh` script in the project root. `build.sh` need not be modified so long as all you're adding is functions, runtime stubs, headers, documentation files, or macro files in line with the standard naming convention. it will need to be modified to add or remove modules. it does not track dependencies, recompiling the entire project in one fell swoop. while in theory this could be slow and inefficient, in practice, it's not meaningfully slower on the average run than the old make-based system was. 140 152 141 -each rule should be prefixed with ${OUT}, to allow retargeting of the build-dir with the OUT environment variable. this is particularly important since the makefiles chain. 153 +the shell-based build system was chosen for a number of reason. firstly, libk originally used a GNU make-based build system, but this was unwieldy and unreliably - make simply does not have the necessary capabilities to build a project of this nature. the original build system relied heavily on recursion, which is impossible to use while preserving idempotency. multithreaded building was likewise impossible. 142 154 143 -the rest is TBD. 155 +relying on make also had a number of subtler downsides. GNU make's capacities vary significantly across versions, and not all users will necessarily have access to the correct version in their repos. even a version as recent as make 4.1 couldn't build a project that build without issue in 4.2. given that libk is explicitly intended to be widely portable, this variance was alarming. make is also very indirect - it's a pain in the ass to tell it what to do sometimes, and it's well nigh on impossible to decipher the intricate way in which a sufficiently complex set of makefiles interlocks. this is both user- and developer-hostile. by contrast, a simple, linear build script with clearly delineated functions makes the build process much easier to understand, and therefore to tweak should a user need to modify some aspect of the process to compile `libk` on her system. 144 156 145 -## design principles 157 +finally, make simply has a smaller install base than `bash`. with the `build.sh` build system, more people can successfully build libk with less effort. 158 + 159 +any time you make a change to the build script, always specify precisely what changed in your commit log and what affects this might have on the surrounding code. be sure to comment any new code as thoroughly as you can. the goal is that anyone who is familiar with bash should be able to learn the build process simply by reading `build.sh`. 160 + 161 +# design principles 146 162 147 163 there are four overriding principles that guide the design of libk. 148 164 149 165 1. it should be easy to write code that uses it. 150 166 2. it should be easy to read code that uses it. 151 167 3. the simple, obvious way of using libk should produce the most optimal code. 152 168 4. code that uses libk should be idiomatic C. ................................................................................ 168 184 rule_target_bourgeoisie 169 185 }; 170 186 171 187 this makes code much more legible and has the added benefit of making the definitions easier to expand at a later date if new functionality is needed without breaking the API or ABI. 172 188 173 189 ## build process 174 190 175 -libk has a number of targets. all files generated by a `make` invocation will be stored in the folder "out" at the root of the repository. this directory may be deleted entirely to clean the repository. 191 +to build libk, you must invoke `build.sh` with the proper parameters. at minimum you must set the following environment variables: 176 192 177 -**defs** will create the directory `out/k/` and populate it with module header files. the `k/` directory shall be suitable to copy to `/usr/include` or similar. these header files will copied by building the `${OUT}/$(module).h` target of each module's makefile. 193 + * `os={atom}` - an atom representing the operating system you are building libk for 194 + * `arch={atom}` - an atom representing the processor architecture you are building libk for 195 + * `bits={atom}` - if your processor has multiple variants with different word lengths (such as x86-32 vs. x86-64), specify the word length in this variable; otherwise, leave it unset. 178 196 179 -**libk.so** will build the dynamically linked form of libk, according to the build variables set 197 +further optional variables may be set to control the build process and determine what targets it produces. 198 + * `library=static {static|shared|both}` - this variable controls whether the build process will produce `libk.a`, `libk.so`, or both. 199 + * `out=out {path}` - an alternate path to store build artifacts in 200 + * `gen=gen {path}` - an alternate path to store generated source in 201 + * `cc=<autodetect> {executable}` - the compiler to compile C sources with 202 + * `m4=<autodetect> {executable}` - the m4 binary to compile the macro sources with 203 + * `asm=<autodetect> {executable}` - the assembler to assemble the assembly listings with. it must take Intel-syntax input and handle nasm-style macros. only `yasm` and `nasm` are likely to be viable. 180 204 181 -**libk.a** will build the statically linked form of libk, according to the build variables set 182 - 183 -**tool** will build the executables used for modules such as `kgraft`. 184 - 185 -**clean** will delete the `tmp` and `out` trees. 205 +two other shell scripts complete the build system: 206 + * `install.sh` - installs compiled libraries, objects, documentation, and headers into the appropriate directories. 186 207 187 208 ## authors 188 209 189 210 so far, this is a one-woman show. contributions are welcome however. 190 211 191 212 * lexi hale <lexi@hale.su> 192 213 193 214 ## caveats 194 215 195 216 the main coder, lexi hale, is first and foremost a writer, not a coder. this is a side-project of hers and will remain so unless it picks up a significant amount of attention. 196 217 197 -while PRs adding support for Windows, OS X, and other operating systems will be gratefully accepted, the maintainer is a Linux and FreeBSD developer, will not be writing such support infrastructure herself, and has limited ability to vet code for those platforms. 218 +while MRs adding support for Windows, OS X, and other operating systems will be gratefully accepted, the maintainer is a Linux and FreeBSD developer, will not be writing such support infrastructure herself, and has limited ability even to vet code for those platforms. 198 219 199 220 ## license 200 221 201 222 libk is released under the terms of the [GNU AGPLv3](LICENSE). contributors do not relinquish ownership of the code they contribute, but agree to release it under the same terms as the overall project license. 202 223 203 224 the AGPL may seem like an inappropriately restrictive license for a project with such grandiose ambitions. it is an ideological choice. i selected it because libk is intended very specifically as a contribution to the *free software* community, a community that i hope will continue to grow at the expense of closed-source ecosystems. i have no interest in enabling people or corporations to profit from keeping secrets, especially not with my own free labor (or anyone else's, for that matter). 204 225