Skip to content

Commit fe21873

Browse files
committed
Add optional parameter target to compilation functions and script
This is for cross compilation.
1 parent c9c8afc commit fe21873

File tree

2 files changed

+71
-21
lines changed

2 files changed

+71
-21
lines changed

script/compile

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,36 @@ PROJECT_ROOT=$(cd "$here/.."; pwd)
1818

1919
LANG=$1
2020

21+
# Parse named parameters.
22+
POSITIONAL=()
23+
while [[ $# -gt 0 ]]; do
24+
key="$1"
25+
case $key in
26+
(-target)
27+
target="'$2"
28+
shift
29+
shift
30+
;;
31+
(*)
32+
POSITIONAL+=("$1")
33+
shift
34+
;;
35+
esac
36+
done
37+
target=${target:-nil}
38+
if [ "$target" == '' ]; then
39+
target=nil
40+
fi
41+
2142
case $LANG in
2243
(all)
23-
code="(tree-sitter-langs-create-bundle)"
44+
code="(tree-sitter-langs-create-bundle nil $target)"
2445
;;
2546
(changed)
26-
code="(tree-sitter-langs-compile-changed-or-all \"${2:-origin/master}\")"
47+
code="(tree-sitter-langs-compile-changed-or-all \"${2:-origin/master}\" $target)"
2748
;;
2849
(*)
29-
code="(tree-sitter-langs-compile '$LANG)"
50+
code="(tree-sitter-langs-compile '$LANG $target)"
3051
esac
3152

3253
(

tree-sitter-langs-build.el

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ If VERSION and OS are not spcified, use the defaults of
257257
(or version tree-sitter-langs--bundle-version)
258258
(or ext "")))
259259

260-
(defun tree-sitter-langs-compile (lang-symbol &optional clean)
260+
(defun tree-sitter-langs-compile (lang-symbol &optional clean target)
261261
"Download and compile the grammar for LANG-SYMBOL.
262262
This function requires git and tree-sitter CLI.
263263
@@ -269,6 +269,12 @@ from the current state of the grammar repo, without cleanup."
269269
(error "Could not find git (needed to download grammars)"))
270270
(unless (executable-find "tree-sitter")
271271
(error "Could not find tree-sitter executable (needed to compile grammars)"))
272+
(setq target
273+
(pcase (format "%s" target)
274+
;; Rust's triple -> system toolchain's triple
275+
("aarch64-apple-darwin" "arm64-apple-macos11")
276+
("nil" nil)
277+
(_ (error "Unsupported cross-compilation target %s" target))))
272278
(let* ((source (tree-sitter-langs--source lang-symbol))
273279
(dir (if source
274280
(file-name-as-directory
@@ -306,18 +312,41 @@ from the current state of the grammar repo, without cleanup."
306312
(lang-symbol (or (cdr-safe path-spec) lang-symbol))
307313
(default-directory (file-name-as-directory (concat dir path))))
308314
(tree-sitter-langs--call "tree-sitter" "generate")
309-
(if (and (memq system-type '(gnu/linux))
310-
(file-exists-p "src/scanner.cc"))
311-
;; Modified from
312-
;; https://github.com/tree-sitter/tree-sitter/blob/v0.20.0/cli/loader/src/lib.rs#L351
313-
(tree-sitter-langs--call
314-
"g++" "-shared" "-fPIC" "-fno-exceptions" "-g" "-O2"
315-
"-static-libgcc" "-static-libstdc++"
316-
"-I" "src"
317-
"src/scanner.cc"
318-
"-xc" "src/parser.c"
319-
"-o" (format "%sbin/%s.so" tree-sitter-langs-grammar-dir lang-symbol))
320-
(tree-sitter-langs--call "tree-sitter" "test"))))
315+
(cond
316+
((and (memq system-type '(gnu/linux))
317+
(file-exists-p "src/scanner.cc"))
318+
;; XXX: Modified from
319+
;; https://github.com/tree-sitter/tree-sitter/blob/v0.20.0/cli/loader/src/lib.rs#L351
320+
(tree-sitter-langs--call
321+
"g++" "-shared" "-fPIC" "-fno-exceptions" "-g" "-O2"
322+
"-static-libgcc" "-static-libstdc++"
323+
"-I" "src"
324+
"src/scanner.cc" "-xc" "src/parser.c"
325+
"-o" (format "%sbin/%s.so" tree-sitter-langs-grammar-dir lang-symbol)))
326+
;; XXX: This is a hack for cross compilation (mainly for Apple Silicon).
327+
(target (cond
328+
((file-exists-p "src/scanner.cc")
329+
(tree-sitter-langs--call
330+
"c++" "-shared" "-fPIC" "-fno-exceptions" "-g" "-O2"
331+
"-I" "src"
332+
"src/scanner.cc" "-xc" "src/parser.c"
333+
"-o" (format "%sbin/%s.so" tree-sitter-langs-grammar-dir lang-symbol)
334+
"-target" target))
335+
((file-exists-p "src/scanner.c")
336+
(tree-sitter-langs--call
337+
"cc" "-shared" "-fPIC" "-g" "-O2"
338+
"-I" "src"
339+
"src/scanner.c" "src/parser.c"
340+
"-o" (format "%sbin/%s.so" tree-sitter-langs-grammar-dir lang-symbol)
341+
"-target" target))
342+
(:default
343+
(tree-sitter-langs--call
344+
"cc" "-shared" "-fPIC" "-g" "-O2"
345+
"-I" "src"
346+
"src/parser.c"
347+
"-o" (format "%sbin/%s.so" tree-sitter-langs-grammar-dir lang-symbol)
348+
"-target" target))))
349+
(:default (tree-sitter-langs--call "tree-sitter" "test")))))
321350
;; Replace underscores with hyphens. Example: c_sharp.
322351
(let ((default-directory bin-dir))
323352
(dolist (file (directory-files default-directory))
@@ -343,7 +372,7 @@ from the current state of the grammar repo, without cleanup."
343372
(tree-sitter-langs--call "git" "reset" "--hard" "HEAD")
344373
(tree-sitter-langs--call "git" "clean" "-f")))))
345374

346-
(defun tree-sitter-langs-create-bundle (&optional clean)
375+
(cl-defun tree-sitter-langs-create-bundle (&optional clean target)
347376
"Create a bundle of language grammars.
348377
The bundle includes all languages tracked in git submodules.
349378
@@ -358,7 +387,7 @@ compile from the current state of the grammar repos, without cleanup."
358387
(message "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
359388
(let ((lang-symbol (intern name)))
360389
(condition-case err
361-
(tree-sitter-langs-compile lang-symbol clean)
390+
(tree-sitter-langs-compile lang-symbol clean target)
362391
(error `[,lang-symbol ,err])))))
363392
(seq-filter #'identity))))
364393
(message "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
@@ -387,20 +416,20 @@ compile from the current state of the grammar repos, without cleanup."
387416
(message "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
388417
(error "Could not compile grammars:\n%s" (pp-to-string errors))))))
389418

390-
(defun tree-sitter-langs-compile-changed-or-all (&optional base)
419+
(defun tree-sitter-langs-compile-changed-or-all (&optional base target)
391420
"Compile languages that have changed since git revision BASE.
392421
If no language-specific change is detected, compile all languages."
393422
(let ((lang-symbols (tree-sitter-langs--changed-langs base))
394423
errors)
395424
(if (null lang-symbols)
396425
(progn
397426
(message "[tree-sitter-langs] Compiling all langs")
398-
(tree-sitter-langs-create-bundle))
427+
(tree-sitter-langs-create-bundle nil target))
399428
(message "[tree-sitter-langs] Compiling langs changed since %s: %s" base lang-symbols)
400429
(dolist (lang-symbol lang-symbols)
401430
(message "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
402431
(condition-case err
403-
(tree-sitter-langs-compile lang-symbol)
432+
(tree-sitter-langs-compile lang-symbol nil target)
404433
(error (setq errors (append errors `([,lang-symbol ,err]))))))
405434
(when errors
406435
(message "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

0 commit comments

Comments
 (0)