Clean up documentation

This commit is contained in:
puck 2024-10-04 01:21:07 +00:00
parent 55a1efa08f
commit 83d41ef778
16 changed files with 146 additions and 98 deletions

View file

@ -1,3 +1,18 @@
;; Defines the baseline definitions needed to compile Go code using Zilch.
;;
;; To make incremental builds work, this library uses up to four distinct
;; outputs:
;;
;; - `api` is any `.a` file needed for compiling any other Go code that depends
;; on said package, and is usually an archive containing a file named
;; `__.PKGDEF`.
;; - `code` is a `.a` file containing the actual binary code for the target
;; arch, and is used only during linking.
;; - `asmhdr` is a header file generated by the Go code, and used during
;; assembly compilation only.
;; - `symabi` is a text file contaning the functions defined by assembly, and
;; their ABI, which is used while compiling the Go code that contains the
;; stubs for any assembly code.
(define-library (zilch lang go core)
(import
(scheme base) (scheme write) (scheme process-context) (scheme lazy)
@ -22,15 +37,19 @@
(begin
;; The architecture to target the Go code at.
;; Equivalent to `GOARCH`.
(define %goarch (make-parameter "amd64"))
; Import the existing Go from nixpkgs.
;; The Go toolchain to use to compile this all.
(define go-toolchain (cdr (assoc "out" (nixpkgs "go_1_23"))))
;; Builds an importcfg file, containing an alist of packages to .a files,
;; and an alist of package names to actual package names.
;; `++packagefiles++` is a alist of package name to .a file (api type),
;; `++importmap++` is an alist of package name to actual package name (used in cases of e.g. replace)
;; Builds an importcfg file. This file describes the mapping of both
;; packages to their api, and the mapping of package name as used in `import`
;; to the actual package names (e.g in case of `replace`.)
;;
;; - `++packagefiles++` is a alist of package name to .a file.
;; - `++importmap++` is an alist of package name to actual package name.
(define (build-importcfg packagefiles importmap)
(call-with-port (open-output-string)
(lambda (outstr)
@ -53,8 +72,11 @@
importmap)
(get-output-string outstr))))
;; `++patterns++` is an alist of the pattern used to match files (e.g. `++foo/++`, or `++a.*++`) to a list of filenames.
;; `++files++` is an alist of file name to actual path.
;; Builds an embedcfg file, which maps from the pattern used in `go:embed`
;; to a list of files, as well as a filename to on-disk file mapping.
;;
;; - `++patterns++` is an alist of the pattern used to match files (e.g. `++foo/++`, or `++a.*++`) to a list of filenames.
;; - `++files++` is an alist of file name to actual path.
(define (build-embedcfg patterns files)
(call-with-port (open-output-string)
(lambda (outstr)
@ -65,7 +87,7 @@
outstr)
(get-output-string outstr))))
; Clean up the package name to use in drv names.
;; Clean up the package name to use in drv names.
(define (rewrite-package-name name)
(set! name (string-copy name))
(do ((x 0 (+ x 1)))
@ -75,10 +97,10 @@
(when (char=? (string-ref name x) #\space) (string-set! name x #\_))
(when (char=? (string-ref name x) #\]) (string-set! name x #\_))))
;; An empty go_asm.h file used to generate symabis.
;; An empty go_asm.h file used when generating symabis.
(define empty-asmhdr (zdir `(("go_asm.h" . ,(zfile "")))))
;; Environment to append to the build environment for Go.
;; The environment to append to the build environment for Go.
(define (env-for-goarch)
`(("GOARCH" . ,(%goarch))))
@ -89,16 +111,19 @@
"-D" ,(string-append "GOARCH_" (%goarch))
,@(if (string=? (%goarch) "amd64") '("-D" "GOAMD64_v1") '())))
;; Returns an alist of three store paths; `++api++` containing the compiler's output,
;; `++code++` containing the linkobj, and `++asmhdr++` containing the headers needed for assembly
;; code to properly use Go functions and variables.
;; Returns an alist of three store paths.
;;
;; - `++api++` containing the compiler's output, used when compiling
;; - `++code++` contains the compiled code, used during linking only.
;; - `++asmhdr++` contains the headers needed for any assembly code inside this package.
(define (go-compile std package-name importcfg symabis embeds files)
(define args
#~(
,@(if std '("-std") '())
#$@(if symabis `("-symabis" ,#$symabis) '())
#$@(if embeds `("-embedcfg" ,#$embeds) '())
"-buildid" "zilch go-compile" ; this goes into both code and __.PKGDEF, so can't be a reference to the code output, sadly
; this goes into both code and __.PKGDEF, so can't be a reference to the code output, sadly
"-buildid" "zilch go-compile"
"-p" #$package-name
"-o" ,(make-placeholder "api")
"-linkobj" ,(make-placeholder "code")
@ -114,7 +139,7 @@
#~(,(string-append #$go-toolchain "/bin/go") "tool" "compile" . #$args)
(env-for-goarch) '("api" "code" "asmhdr")))
;; Returns a store path containing the symabi for the files provided.
;; Returns a store path containing the symabi for the assembly files provided.
(define (go-generate-symabi package-name include-path files)
(define args
#~(
@ -133,7 +158,9 @@
#~(,(string-append #$go-toolchain "/bin/go") "tool" "asm" . #$args)
(env-for-goarch) '("symabi"))))
;; Returns a store path containing the `++code++` of the provided assembly files.
;; Returns a store path containing the `++code++` of the provided assembly
;; files. Assembly files have no `api`, and cannot be directly interacted
;; with from other packages.
(define (go-compile-assembly package-name include-path include-path2 files)
(define args
#~(