libk  Check-in [d27f92e3b7]

Overview
Comment:restructure file naming conventions and conclusively fix longstanding architectural build system problems with loads and loads of variables and a shit ton of function calls
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d27f92e3b7e88d34291cddd71dd199a5fa7a95341818b125443a2e615e19e1cb
User & Date: lexi on 2019-06-28 04:32:40
Other Links: manifest | tags
Context
2019-06-28
04:35
fix useless params check-in: f5e894f1c4 user: lexi tags: trunk
04:32
restructure file naming conventions and conclusively fix longstanding architectural build system problems with loads and loads of variables and a shit ton of function calls check-in: d27f92e3b7 user: lexi tags: trunk
01:59
add {,un}install rule check-in: 2ed2a90fe5 user: lexi tags: trunk
Changes

Deleted kcore/boot.c version [6007ea2ce5].

     1         -#include <k/core.h>
     2         -extern stat entry(kenv);
     3         -
     4         -stat _boot(unsigned int argc, char** argv) {
     5         -	kenv e = { 
     6         -		// todo: determine terminal class and set term vs ansi correctly!
     7         -		{ {kiostream_term, 0}, {kiostream_term, 1} }, // chan std
     8         -		{ {kiostream_closed},  {kiostream_term, 2} }, // chan err
     9         -		argc, argv,
    10         -		null // no environment yet
    11         -	};
    12         -	return entry(e);
    13         -}
    14         -	

Added kcore/boot.rt.c version [6007ea2ce5].

            1  +#include <k/core.h>
            2  +extern stat entry(kenv);
            3  +
            4  +stat _boot(unsigned int argc, char** argv) {
            5  +	kenv e = { 
            6  +		// todo: determine terminal class and set term vs ansi correctly!
            7  +		{ {kiostream_term, 0}, {kiostream_term, 1} }, // chan std
            8  +		{ {kiostream_closed},  {kiostream_term, 2} }, // chan err
            9  +		argc, argv,
           10  +		null // no environment yet
           11  +	};
           12  +	return entry(e);
           13  +}
           14  +	

Added kcore/boot.rt.x86.lin.64.s version [2671a43f32].

            1  +; vim: ft=nasm
            2  +bits 64
            3  +%include "../arch/x86.lin.64.s"
            4  +global _start:function
            5  +extern _boot
            6  +extern entry;
            7  +
            8  +_start:
            9  +	mov rbp, rsp
           10  +	mov rdi, [rbp + 0] ; argc
           11  +	lea rsi, [rbp + 8] ; argv
           12  +
           13  +	call _boot
           14  +
           15  +	mov sys.reg.1, sys.reg.ret
           16  +	mov sys.reg.0, sys.exit
           17  +	sys.call

Deleted kcore/boot.x86.lin.64.s version [2671a43f32].

     1         -; vim: ft=nasm
     2         -bits 64
     3         -%include "../arch/x86.lin.64.s"
     4         -global _start:function
     5         -extern _boot
     6         -extern entry;
     7         -
     8         -_start:
     9         -	mov rbp, rsp
    10         -	mov rdi, [rbp + 0] ; argc
    11         -	lea rsi, [rbp + 8] ; argv
    12         -
    13         -	call _boot
    14         -
    15         -	mov sys.reg.1, sys.reg.ret
    16         -	mov sys.reg.0, sys.exit
    17         -	sys.call

Added kcore/def.h version [5f84d85008].

            1  +#include <k/type.h>
            2  +/* <k/def.h>
            3  + * ~ lexi hale <lexi@hale.su>
            4  + * define flags so we can reason about
            5  + * our environment. */
            6  +
            7  +#if (KVos == lin)  ||\
            8  +    (KVos == fbsd) ||\
            9  +    (KVos == obsd) ||\
           10  +    (KVos == nbsd) ||\
           11  +    (KVos == dar) ||\
           12  +    (KVos == and)  
           13  +#   define KFenv_unix
           14  +#endif
           15  +
           16  +#if defined(KFenv_unix)  ||\
           17  +           (KVos == hai) ||\
           18  +           (KVos == mgw)
           19  +#   define KFenv_posix
           20  +#endif
           21  +

Added kcore/def.win.i version [11ccc3c2e7].

            1  +#define KVos win
            2  +typedef u32 stat;

Modified kcore/makefile from [a535931ecd] to [121a62ca71].

    23     23   	cp $< $@
    24     24   
    25     25   # generating C source in makeā€¦ yaaay
    26     26   define arch =
    27     27   ${TMP}/type.$(1).%.$(2).i: type.$(1).$(2).i def.%.i ${TMP}
    28     28   	echo '#ifndef KItype' > $$@
    29     29   	echo '#define KItype' >> $$@
    30         -	cat def.$$*.i >> $$@
    31     30   	cat $$< >> $$@
           31  +	cat def.$$*.i >> $$@
    32     32   	echo '#endif' >> $$@
    33     33   endef
    34     34   
    35     35   $(eval $(call arch,x86,32))
    36     36   $(eval $(call arch,x86,64))
    37     37   

Added kgraft/attach.exe.c version [572a900790].

            1  +#include <k/core.h>
            2  +stat entry(kenv e) {
            3  +	return 0;
            4  +}

Deleted kgraft/exe.attach.c version [572a900790].

     1         -#include <k/core.h>
     2         -stat entry(kenv e) {
     3         -	return 0;
     4         -}

Modified kio/io.h from [824a86b217] to [aa4095c969].

     4      4    * ~ lexi hale <lexi@hale.su>
     5      5    * this header declares IO primitive functions and
     6      6    * structures. it is the same for all platforms.
     7      7    * platform-specific code is found in the *.platform.h
     8      8    * files.
     9      9    */
    10     10   
           11  +#include <k/str.h>
    11     12   #include <k/type.h>
           13  +#include <k/mem.h>
    12     14   
    13     15   typedef enum kiostream_kind {
    14     16   	kiostream_closed,
    15     17   	  // this kiostream cannot be written to
    16     18   	kiostream_file,
    17     19   	  // this kiostream represents a file
    18     20   	kiostream_sock,
................................................................................
    36     38   typedef struct kiochan {
    37     39   	kiostream in;
    38     40   	  // text can be read from this stream
    39     41   	kiostream out;
    40     42   	  // text can be written to this stream
    41     43   } kiochan;
    42     44   
    43         -unsigned long long kiosend(kiochan); // send data to a channel
    44         -unsigned long long kiorecv(kiochan); // receive data from a channel
    45         -
    46     45   typedef enum kiocond {
    47     46   	kiocond_ok,
    48     47   	  // success
    49     48       kiocond_fail,
    50     49   	  // action failed
    51     50   } kiocond;
    52     51   
           52  +kiocond kiosend(kiochan, ksraw, sz*); // send data to a channel
           53  +kiocond kiorecv(kiochan, ksraw*); // receive data from a channel
           54  +kmptr kiorecvall(kiochan, kmcell*, kmkind); // automatically allocate a bufer for a channel
           55  +  // kmkind is only used if kmcell* is NULL
           56  +kiocond kiocon(kiochan, kiochan); // connect one channel to another
           57  +
    53     58   #endif

Added kio/kio_posix_fd_write.fn.x86.lin.32.s version [7b77d86bab].

            1  +bits 32
            2  +global kio_posix_fd_write
            3  +
            4  +%include "../arch/x86.lin.32.s"
            5  +; vim: ft=nasm
            6  +
            7  +kio_posix_fd_write:
            8  +	mov sys.reg.0, sys.call.write
            9  +	mov sys.reg.1, [esp + 4] ; holy god but this took the most
           10  +	mov sys.reg.2, [esp + 8] ; stupidly long time to fucking
           11  +	mov sys.reg.3, [esp + 12]; figure out
           12  +	sys.call
           13  +	ret
           14  +

Added kio/kio_posix_fd_write.fn.x86.lin.64.s version [b72b3eff18].

            1  +bits 64
            2  +global kio_posix_fd_write
            3  +
            4  +%include "../arch/x86.lin.64.s"
            5  +; vim: ft=nasm
            6  +
            7  +kio_posix_fd_write:
            8  +	mov sys.reg.0, sys.write
            9  +	; mov sys.reg.1, ccall.reg.0 - nop
           10  +	; mov sys.reg.2, ccall.reg.1 - nop
           11  +	; mov sys.reg.3, ccall.reg.2 - nop
           12  +	sys.call
           13  +	ret
           14  +

Deleted kio/kio_posix_fd_write.x86.lin.32.s version [7b77d86bab].

     1         -bits 32
     2         -global kio_posix_fd_write
     3         -
     4         -%include "../arch/x86.lin.32.s"
     5         -; vim: ft=nasm
     6         -
     7         -kio_posix_fd_write:
     8         -	mov sys.reg.0, sys.call.write
     9         -	mov sys.reg.1, [esp + 4] ; holy god but this took the most
    10         -	mov sys.reg.2, [esp + 8] ; stupidly long time to fucking
    11         -	mov sys.reg.3, [esp + 12]; figure out
    12         -	sys.call
    13         -	ret
    14         -

Deleted kio/kio_posix_fd_write.x86.lin.64.s version [b72b3eff18].

     1         -bits 64
     2         -global kio_posix_fd_write
     3         -
     4         -%include "../arch/x86.lin.64.s"
     5         -; vim: ft=nasm
     6         -
     7         -kio_posix_fd_write:
     8         -	mov sys.reg.0, sys.write
     9         -	; mov sys.reg.1, ccall.reg.0 - nop
    10         -	; mov sys.reg.2, ccall.reg.1 - nop
    11         -	; mov sys.reg.3, ccall.reg.2 - nop
    12         -	sys.call
    13         -	ret
    14         -

Added kio/send.fn.c version [430158e543].

            1  +#include <k/io.h>
            2  +#include <k/core.h>
            3  +#include <k/def.h>
            4  +/* send.c - kiosend()
            5  + * ~ lexi hale <lexi@hale.su>
            6  + * kiosend() writes to a channel with an open out stream
            7  + */
            8  +
            9  +// we define all platform functions here,
           10  +// whether or not they're for the correct
           11  +// platform - only the ones actually called
           12  +// by the generated code will be linked
           13  +extern sz kio_posix_fd_write(int fd, const char* buf, sz len);
           14  +
           15  +kiocond kiosend(kiochan target, ksraw string, sz* len) {
           16  +#	ifdef KFenv_posix
           17  +		sz size = kio_posix_fd_write(target.out.platform_fd, string.ptr, string.size);
           18  +		if (size == -1) return kiocond_fail; //TODO: retrieve errno and offer more specific errors
           19  +#	else
           20  +#		if KVos == win
           21  +#			error windows IO send function not yet defined
           22  +#		else
           23  +			_Pragma("GCC diagnostic error \"" // fancy error for gcc
           24  +				"IO send fn for platform " #KVos " not defined"
           25  +			"\""))
           26  +#			error IO send fn not defined for platform
           27  +			   // boring error for plebs
           28  +#		endif
           29  +#	endif
           30  +
           31  +	if (len != null) *len = size;
           32  +	return kiocond_ok;
           33  +}

Modified libk.md from [523e766d88] to [3c28c4d693].

    99     99   
   100    100   libk uses a strict directory structure for code, and deviations from this structure will not be tolerated without extremely good reason.
   101    101   
   102    102   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.
   103    103   
   104    104   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).
   105    105   
   106         -each function should be kept in a separate file within its module's directory. when OS or architecture-specific code is needed, the file's name should be a list of one or more of the fields [arch, OS, bits, format] separated by a `.` -- for instance, the 32-bit x86 haiku version of a function called `write` defined in assembly would be named `write.x86.hai.32.s`. however, if a function has an extraordinarily large number of versions, they may instead be stored in a folder with the same name as the function.
          106  +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:
          107  +
          108  + * a C file in the module `kstr` named `kscomp` would be named `kstr/comp.fn.c`
          109  + * a runtime assembly file called `boot` in the module `kcore` for x86-64 linux would be named `kcore/boot.rt.x86.lin.64.s`
          110  + * the 32-bit x86 haiku version of a function called `kiowrite` defined in assembly would be named `kio/write.fn.x86.hai.32.s`.
   107    111   
   108    112   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.
   109    113   
   110    114   each module directory should contain a makefile that can build that module. see **makefiles** below. all makefiles should be named `makefile` (**not** `Makefile`).
   111    115   
   112    116   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 
   113    117   
   114         -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`
          118  +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`
   115    119   
   116    120   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.
   117    121   
   118    122   the folder `arch` in the root of the repository contains syscall tables and ABI implementations for various architectures.
   119    123   
   120    124   ## makefiles
   121    125   

Modified makefile from [6234448ba2] to [dc16a02cfa].

     4      4   export OS = lin
     5      5   export BITS = 64
     6      6   export TMP = $(PWD)/tmp
     7      7   
     8      8   export TARGET = $(ARCH).$(OS).$(BITS)
     9      9   
    10     10   moddirs = $(wildcard k*)
    11         -binaries = $(wildcard */exe.*.c)
           11  +binaries = $(wildcard k*/*.exe.c)
           12  +functions = $(wildcard k*/*.fn.c) 
           13  +assemblies = $(wildcard k*/*.fn.${TARGET}.s)
    12     14   binmods = $(sort $(dir $(binaries)))
    13     15   
           16  +# i'm sorry
           17  +collect = $(strip $(foreach dir,$(moddirs),$(addprefix $(OUT)/$(dir).,$(notdir $(wildcard $(dir)/$1)))))
           18  +
           19  +cfnsources = $(call collect,*.fn.c)
           20  +sfnsources = $(call collect,*.fn.${TARGET}.s)
           21  +crtsources = $(call collect,*.rt.c)
           22  +srtsources = $(call collect,*.rt.${TARGET}.s)
           23  +fnsources = $(cfnsources) $(sfnsources)
           24  +rtsources = $(crtsources) $(srtsources)
           25  +sources = $(fnsources) $(rtsources)
           26  +
           27  +cfnobjects = $(cfnsources:%.c=%.o)
           28  +sfnobjects = $(sfnsources:%.s=%.o)
           29  +crtobjects = $(crtsources:%.c=%.o)
           30  +srtobjects = $(srtsources:%.s=%.o)
           31  +fnobjects = $(cfnobjects) $(sfnobjects)
           32  +rtobjects = $(crtobjects) $(srtobjects)
           33  +objects = $(fnobjects) $(rtobjects)
           34  +
    14     35   header-dir = /usr/include
    15     36   lib-dir = /usr/lib
    16     37   
    17     38   posix-oses = lin fbsd dar and hai mgw
    18     39   
    19     40   ifeq ($(findstring $(OS),$(posix-oses)),$(OS))
    20     41   export POSIX = yes
................................................................................
    23     44   endif
    24     45   
    25     46   # include libgcc.a in gcc builds, just in case
    26     47   ifeq ($(CC),gcc)
    27     48   export COMPLIB = -lgcc
    28     49   endif
    29     50   
    30         -all: defs obj tool lib.static lib.shared
           51  +all: $(OUT) defs obj tool lib.static $(OUT)/boot.o lib.shared
    31     52   lib.static: defs obj $(OUT)/libk.a
    32     53   lib.shared: defs obj $(OUT)/libk.so
    33     54   obj: $(moddirs:%=%.obj)
    34     55   defs: $(moddirs:%=%.def)
    35     56   tool: $(OUT)/libk.a $(binmods:%=%.tool) 
    36     57   clean:
    37     58   	rm -rf $(TMP) $(OUT)
................................................................................
    42     63   	install -d $(lib-dir)/k -o root -g wheel
    43     64   	install $(OUT)/libk.a $(OUT)/libk.so $(OUT)/boot.o \
    44     65   		$(lib-dir)/k/ -o root -g wheel -m 0644
    45     66   
    46     67   uninstall: $(header-dir)/k $(lib-dir)/k
    47     68   	rm -rf $^
    48     69   
    49         -lists = moddirs objects binaries binmods POSIX
           70  +lists = moddirs functions assemblies cfnobjects sfnobjects crtobjects srtobjects rtobjects binaries binmods POSIX
    50     71   dbg:
    51     72   	@echo -e lists: $(foreach var, $(lists), "\\n - \\e[1m$(var)\\e[m = $($(var))")
    52     73   
    53     74   %.obj: %/makefile ${TARGET}.calls $(OUT)
    54     75   	cd $* && $(MAKE) obj
    55     76   
    56     77   %.tool: %/makefile $(OUT)
................................................................................
    61     82   
    62     83   %.def: %/makefile $(OUT) $(OUT)/k
    63     84   	cd $* && $(MAKE) def
    64     85   
    65     86   %.calls: arch/makefile
    66     87   	cd arch && $(MAKE) $(TMP)/calls.$*.s
    67     88   
    68         -$(OUT)/libk.so: obj $(OUT) $(OUT)/boot.o
    69         -	ld -shared $(COMPLIB) -o $@ $(filter-out kcore.boot.%, $(wildcard *.o))
    70         -	# $(CC) -shared -fPIC -nostdlib $(COMPLIB) -o $@ $(OUT)/*.o
           89  +$(OUT)/libk.so: $(fnobjects) 
           90  +	ld -shared $(COMPLIB) -o $@ $^
           91  +	@# $(CC) -shared -fPIC -nostdlib $(COMPLIB) -o $@ $(OUT)/*.o
    71     92   
    72         -$(OUT)/boot.o: $(OUT)/kcore.boot.o $(OUT)/kcore.boot.$(TARGET).o
           93  +$(OUT)/boot.o: $(rtobjects)
    73     94   	ld -r $^ -o $(OUT)/boot.o
    74     95   
    75         -$(OUT)/libk.a: obj $(OUT)
    76         -	# using `ar rc` and ranlib here instead of
    77         -	# `ar rcs` in case `ar` isn't the GNU version
           96  +$(OUT)/libk.a: $(fnobjects) $(rtobjects) obj $(OUT)
           97  +	@# using `ar rc` and ranlib here instead of
           98  +	@# `ar rcs` in case `ar` isn't the GNU version
    78     99   	ar rc $@ $(OUT)/*.o
    79    100   	ranlib $@
    80    101   
    81    102   $(OUT) $(OUT)/k:
    82    103   	mkdir -p $@

Modified modmake from [86d81e4675] to [3c50b488d5].

     4      4   # vim: ft=make
     5      5   
     6      6   mod = $(notdir $(PWD))
     7      7   src = $(wildcard *.c) $(wildcard *.s)
     8      8   bare = $(mod:k%=%)
     9      9   headers = $(wildcard *.h) $(gen-headers)
    10     10   
    11         -tools    = $(filter     exe.%.c,   $(src))
    12         -nontools = $(filter-out exe.%.c,   $(src))
           11  +tools    = $(filter     %.exe.c,   $(src))
           12  +nontools = $(filter-out %.exe.c,   $(src))
    13     13   cobjects = $(filter     %.c,       $(nontools))
    14     14   sobjects = $(filter %.${TARGET}.s, $(nontools))
    15     15   
    16     16   cflags = -isystem ${OUT} -fPIC -nostdlib ${COMPLIB} -L${OUT} -lk
    17     17   
    18     18   obj: $(cobjects:%.c=${OUT}/$(mod).%.o) \
    19     19   	 $(sobjects:%.s=${OUT}/$(mod).%.o)
    20         -tool: $(tools:exe.%.c=${OUT}/$(mod).%) \
           20  +tool: $(tools:%.exe.c=${OUT}/$(mod).%) \
    21     21   	  ${OUT}/libk.a
    22     22   
    23     23   def: $(headers:%=${OUT}/k/%)
    24     24   
    25     25   dbg:
    26     26   	@echo tools = $(tools)
    27     27   	@echo TARGET = ${TARGET}
................................................................................
    32     32   
    33     33   ${OUT}/$(mod).%.o: %.c
    34     34   	$(CC) $(cflags) -c $< -o $@
    35     35   
    36     36   ${OUT}/k/%.h: %.h
    37     37   	cp $< $@
    38     38   
    39         -${OUT}/$(mod).%: exe.%.c
           39  +${OUT}/$(mod).%: %.exe.c
    40     40   	$(CC) $(cflags) $< -o $@
    41     41   
    42     42   ${TMP}:
    43     43   	mkdir -p ${TMP}
    44     44   
    45     45   #- assembly
    46     46   # compiling  the assembly  code will  be  faster but  a lot  more