Skip to content

Distribute and run LLMs with a single file.

Add `make tags` for IDE support

Justine Tunney41beb344f8da
diff --git a/.gitignore b/.gitignore
index b52823cd..ed1f208f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,3 @@
/o
+/TAGS
+/HTAGS
diff --git a/Makefile b/Makefile
index b5c81526..dad2569a 100644
--- a/Makefile
+++ b/Makefile
@@ -14,3 +14,4 @@ include build/rules.mk
include llama.cpp/BUILD.mk
include build/deps.mk
+include build/tags.mk
diff --git a/build/bootstrap/ctags b/build/bootstrap/ctags
new file mode 100755
index 00000000..5ceb5e10
Binary files /dev/null and b/build/bootstrap/ctags differ
diff --git a/build/bootstrap/mkdeps.com b/build/bootstrap/mkdeps
similarity index 100%
rename from build/bootstrap/mkdeps.com
rename to build/bootstrap/mkdeps
diff --git a/build/ctags b/build/ctags
new file mode 100755
index 00000000..275f1aae
--- /dev/null
+++ b/build/ctags
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec build/bootstrap/ctags "$@"
diff --git a/build/htags b/build/htags
new file mode 100755
index 00000000..a580edf2
--- /dev/null
+++ b/build/htags
@@ -0,0 +1,78 @@
+#!/bin/sh
+#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
+#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
+#
+# OVERVIEW
+#
+# Header Symbol Index Generator
+#
+# DESCRIPTION
+#
+# This is a static source analyzer that lets us configure Emacs
+# keybindings to insert #include lines.
+#
+# EXAMPLES
+#
+# build/htags -o HTAGS $(find . -name \*.h)
+#
+# (defun jart-add-include ()
+# (interactive)
+# (let* ((tag-file "HTAGS")
+# (case-fold-search nil)
+# (search (thing-at-point 'symbol))
+# (buffer (find-file-noselect (format "%s/%s"
+# (locate-dominating-file
+# (buffer-name) tag-file)
+# tag-file)))
+# (header (with-current-buffer buffer
+# (save-excursion
+# (goto-char 0)
+# (when (re-search-forward
+# (concat "\177" search "\001") nil t)
+# (when (re-search-backward "\f\n\\([^,]*\\)," nil t)
+# (match-string 1)))))))
+# (when header
+# (save-excursion
+# (goto-char 0)
+# (re-search-forward "#include")
+# (re-search-forward "^$")
+# (insert (concat "#include \"" header "\"\n"))))))
+# (defun jart-c-mode-common-hook ()
+# (define-key c-mode-base-map (kbd "C-c C-h") 'jart-add-include))
+# (eval-after-load 'markdown-mode
+# '(progn
+# (add-hook 'c-mode-common-hook 'jart-c-mode-common-hook)))
+
+TAGS="$1"
+shift
+
+# ctags doesn't understand atomics, e.g.
+# extern char **environ;
+set -- --regex-c='/_Atomic(\([^)]*\))/\1/b' "$@"
+
+# ctags doesn't understand variable prototypes, e.g.
+# extern char **environ;
+set -- --regex-c='/^\(\(hidden\|extern\|const\) \)*[_[:alpha:]][_[:alnum:]]*[ *][ *]*\([_[:alpha:]][_[:alnum:]]*[ *][ *]*\)*\([_[:alpha:]][_$[:alnum:]]*\)/\4/b' "$@"
+
+# ctags doesn't understand function prototypes, e.g.
+# bool isheap(void *p) dontthrow nocallback;
+set -- --regex-c='/^[_[:alpha:]][_[:alnum:]]*[ *][ *]*\([_[:alpha:]][_[:alnum:]]*[ *][ *]*\)*\([_[:alpha:]][_$[:alnum:]]*\)(.*/\2/b' "$@"
+
+# ctags doesn't understand function pointers, e.g.
+# extern int32_t (*const SetEvent)(int64_t hEvent) wincall;
+set -- --regex-c='/^extern [^(]*(\*const \([^)]*\))(/\1/b' "$@"
+
+# ctags doesn't understand forward declarations, e.g.
+# struct WorstSoftwareEver;
+set -- --regex-c='/^struct.*;$/uehocruehcroue/b' "$@"
+
+exec $TAGS \
+ -e \
+ --langmap=c:.c.h \
+ --exclude=libc/nt/struct/imagefileheader.internal.h \
+ --exclude=libc/nt/struct/imageseparatedebugheader.internal.h \
+ --exclude=libc/nt/struct/importobjectheader.h \
+ --exclude=libc/nt/struct/nonpageddebuginfo.h \
+ --exclude=libc/nt/struct/ansistring.h \
+ --exclude=libc/nt/struct/filesegmentelement.h \
+ "$@"
diff --git a/build/mkdeps b/build/mkdeps
index 58c380f7..c90914fb 100755
--- a/build/mkdeps
+++ b/build/mkdeps
@@ -1,2 +1,2 @@
#!/bin/sh
-exec build/bootstrap/mkdeps.com "$@"
+exec build/bootstrap/mkdeps "$@"
diff --git a/build/tags.mk b/build/tags.mk
new file mode 100644
index 00000000..2b1d6f96
--- /dev/null
+++ b/build/tags.mk
@@ -0,0 +1,27 @@
+#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
+#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
+
+TAGSFLAGS = \
+ -e \
+ -a \
+ --if0=no \
+ --langmap=c:.c.h.i \
+ --line-directives=yes
+
+tags: TAGS HTAGS
+
+TAGS: o/$(MODE)/tags-srcs.txt $(SRCS)
+ @rm -f $@
+ build/ctags $(TAGSFLAGS) -L $< -o $@
+
+HTAGS: o/$(MODE)/tags-hdrs.txt $(HDRS)
+ @rm -f $@
+ build/htags build/ctags -L $< -o $@
+
+o/$(MODE)/tags-srcs.txt: $(call uniq,$(foreach x,$(SRCS),$(dir $(x))))
+ @mkdir -p $(@D)
+ $(file >$@) $(foreach x,$(SRCS),$(file >>$@,$(x)))
+
+o/$(MODE)/tags-hdrs.txt: $(call uniq,$(foreach x,$(HDRS) $(INCS),$(dir $(x))))
+ @mkdir -p $(@D)
+ $(file >$@) $(foreach x,$(HDRS) $(INCS),$(file >>$@,$(x)))