#!/bin/bash # # Builds the tree-sitter grammars the syntaxes depend on. # # Nova loads grammars as dylibs named libtree-sitter-.dylib and # expects ABI 14, so both grammars are pinned to a revision that generates it. # Run from anywhere: ./Tools/build-syntaxes.sh # set -euo pipefail root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" work="${TMPDIR:-/tmp}/apple-container-grammars" out="$root/Syntaxes" dockerfile_repo="https://github.com/camdencheek/tree-sitter-dockerfile.git" dockerfile_ref="971acdd908568b4531b0ba28a445bf0bb720aba5" yaml_repo="https://github.com/tree-sitter-grammars/tree-sitter-yaml.git" yaml_ref="v0.7.2" # Universal so the extension works on Apple silicon and Intel alike. arches=(-arch arm64 -arch x86_64) fetch() { local name="$1" local repo="$2" local ref="$3" local dir="$work/$name" if [ ! -d "$dir" ]; then git clone --quiet "$repo" "$dir" fi git -C "$dir" fetch --quiet --tags origin git -C "$dir" checkout --quiet "$ref" local version version="$(grep -m1 'define LANGUAGE_VERSION' "$dir/src/parser.c" | awk '{print $3}')" if [ "$version" != "14" ]; then echo "error: $name generates tree-sitter ABI $version, Nova expects 14" >&2 exit 1 fi } mkdir -p "$work" "$out" fetch dockerfile "$dockerfile_repo" "$dockerfile_ref" fetch yaml "$yaml_repo" "$yaml_ref" echo "building libtree-sitter-dockerfile.dylib" cc "${arches[@]}" -dynamiclib -O2 -fPIC -std=c11 \ -I "$work/dockerfile/src" \ "$work/dockerfile/src/parser.c" "$work/dockerfile/src/scanner.c" \ -o "$out/libtree-sitter-dockerfile.dylib" # Nova already ships a grammar registered as "yaml", so the compose grammar is # built from the same sources under its own symbol names. That keeps the # Compose syntax on the grammar these queries were written against instead of # whichever yaml grammar happens to win. echo "building libtree-sitter-compose.dylib" renames=( -Dtree_sitter_yaml=tree_sitter_compose -Dtree_sitter_yaml_external_scanner_create=tree_sitter_compose_external_scanner_create -Dtree_sitter_yaml_external_scanner_destroy=tree_sitter_compose_external_scanner_destroy -Dtree_sitter_yaml_external_scanner_scan=tree_sitter_compose_external_scanner_scan -Dtree_sitter_yaml_external_scanner_serialize=tree_sitter_compose_external_scanner_serialize -Dtree_sitter_yaml_external_scanner_deserialize=tree_sitter_compose_external_scanner_deserialize ) cc "${arches[@]}" -dynamiclib -O2 -fPIC -std=c11 -DYAML_SCHEMA=core "${renames[@]}" \ -I "$work/yaml/src" \ "$work/yaml/src/parser.c" "$work/yaml/src/scanner.c" \ -o "$out/libtree-sitter-compose.dylib" for library in "$out"/libtree-sitter-*.dylib; do echo "$(basename "$library"): $(lipo -archs "$library")" done