elisp-tree-sitter icon indicating copy to clipboard operation
elisp-tree-sitter copied to clipboard

Support local compilation of dynamic module and grammar binaries

Open ubolonton opened this issue 3 years ago • 22 comments

Most importantly, this would allow M1 macOS users to use the package, without waiting for us to set up all the CI infrastructure (#88). It's also necessary for users who want increased control over their setup.

  • [x] Support local compilation in tsc-dyn-get.el: #190. (Note that the Rust source is included in the package.)
  • [ ] Support better local compilation in tree-sitter-langs-build.el. It already has functions to compile the grammars, but they assume a checked-out git repository, instead of downloading the source: emacs-tree-sitter/tree-sitter-langs#36.

ubolonton avatar Aug 16 '21 14:08 ubolonton

How is everything going? And thanks for your work

nowislewis avatar Sep 04 '21 10:09 nowislewis

Local compilation for tsc is almost done in the compilation-utilities branch.

ubolonton avatar Sep 26 '21 16:09 ubolonton

Good to read! Wasn't able to get it working, yet.

ckruse avatar Oct 03 '21 09:10 ckruse

Good to read! Wasn't able to get it working, yet.

What issues did you encounter? Local compilation should be more or less working. I'm just improving error reporting and fixing CI.

ubolonton avatar Oct 03 '21 11:10 ubolonton

Sorry that it took me so long to answer, I had to reproduce everything.

The problem I encounter is that I am not able to compile the grammars (they only exist as x86 binaries: bin/tsx.dylib: Mach-O 64-bit dynamically linked shared library x86_64). I fail to get the tree-parser binary at the correct version and the correct architecture. Installing it via NPM installs the x86 binary.

ckruse avatar Oct 05 '21 06:10 ckruse

Ok, after compiling tree-sitter myself and installing the CLI tool on my machine via cargo install --path . I am now able to generate the languages via ./script/compile all.

ckruse avatar Oct 05 '21 06:10 ckruse

I've used the work you've done for local compilation of the dynamic module to package emacs tree-sitter for GNU Guix. You can find that linked here if anyone is interested. I'm hoping to contribute this package to Guix (or NonGuix) upstream at some point.

Tass0sm avatar Nov 12 '21 18:11 Tass0sm

After I've compiled a language, where is the dylib? Alternatively, are there step-by-step instructions for doing a full build on this branch? I'm trying to get it working on a mac M1. So far I've got tsc-dyn.dylib compiled for arm, but I can't figure out how to get language bindings.

Thanks!

aaronjensen avatar Dec 05 '21 04:12 aaronjensen

After I've compiled a language, where is the dylib? Alternatively, are there step-by-step instructions for doing a full build on this branch?

This branch is only for the core package itself. I haven't started on the grammar bundle yet.

I'm trying to get it working on a mac M1. So far I've got tsc-dyn.dylib compiled for arm, but I can't figure out how to get language bindings.

Maybe this would work https://github.com/emacs-tree-sitter/tree-sitter-langs#building-grammars-from-source. I don't have an M1 to check, though.

ubolonton avatar Dec 05 '21 14:12 ubolonton

I’ve run those scripts but I don’t know what it is doing. I don’t end up with a dylib so I can’t tell what it’s actually producing.

aaronjensen avatar Dec 05 '21 15:12 aaronjensen

The dylib should be under the bin directory if the build succeeds.

ubolonton avatar Dec 05 '21 15:12 ubolonton

I must be missing something. The only thing I can see that actually builds binaries is here: https://github.com/emacs-tree-sitter/tree-sitter-langs/blob/master/tree-sitter-langs-build.el#L293-L303

And that's disabled on macOS, and only works for cc files. Typescript has a scanner.c, for example. The build completes successfully on my machine and emits nothing to bin.

aaronjensen avatar Dec 05 '21 16:12 aaronjensen

I was able to build them with:

(if (and ;; (memq system-type '(gnu/linux))
                   (file-exists-p "src/scanner.c"))
              ;; Modified from
              ;; https://github.com/tree-sitter/tree-sitter/blob/v0.20.0/cli/loader/src/lib.rs#L351
              (tree-sitter-langs--call
               "g++" "-shared" "-fPIC" "-fno-exceptions" "-g" "-O2"
               "-static-libstdc++"
               "-I" "src"
               "-xc" "-std=c99" "src/scanner.c"
               "-xc" "src/parser.c"
               "-o" (format "%sbin/%s.so" tree-sitter-langs-grammar-dir lang-symbol))
            (tree-sitter-langs--call "tree-sitter" "test"))```

aaronjensen avatar Dec 05 '21 16:12 aaronjensen

Nice idea to use -xc to convince g++ to compile C. You might also try https://github.com/nvim-treesitter/nvim-treesitter to compile parser binaries for more languages or export the compile commands for additional languages. We could add a pipe line for arm64 Mac.

theHamsta avatar Dec 05 '21 20:12 theHamsta

It wasn't my idea, I was just expanding on what was in https://github.com/tree-sitter/tree-sitter/blob/v0.20.0/cli/loader/src/lib.rs#L351. It looks like the current build assumes c++ and linux. I have no idea how it ever build any libs for typescript...

aaronjensen avatar Dec 05 '21 23:12 aaronjensen

It looks like the current build assumes c++ and linux. I have no idea how it ever build any libs for typescript...

It special-cases C++ on Linux so that stdc++ is linked statically, to ease distribution. It calls the tree-sitter CLI tool otherwise.

The build completes successfully on my machine and emits nothing to bin.

It's probably because you have a new version of the CLI tool, which doesn't allow customizing the output directory. See tree-sitter/tree-sitter#1336.

I think we will eventually remove the reliance on the CLI tool to build the binaries.

ubolonton avatar Dec 06 '21 15:12 ubolonton

It's probably because you have a new version of the CLI tool

Arg, you are right. Thank you. That makes it work better.

aaronjensen avatar Dec 08 '21 02:12 aaronjensen

On the tree-sitter-langs side of this feature, is it planned to allow installation for abritrary parsers via github (something like a straight.el for ts parsers), or is the focus more on supporting new architectures?

ethan-leba avatar Dec 17 '21 12:12 ethan-leba

On the tree-sitter-langs side of this feature, is it planned to allow installation for abritrary parsers via github (something like a straight.el for ts parsers)

Yes, that's an important goal.

ubolonton avatar Dec 29 '21 16:12 ubolonton

Can you bump tree-sitter?

https://github.com/emacs-tree-sitter/elisp-tree-sitter/pull/206/files

They fixed the download for m1

the-moriarty avatar Feb 02 '22 04:02 the-moriarty

@the-moriarty wrong issue? :-)

ckruse avatar Feb 02 '22 06:02 ckruse

Went to build the dynamic module, received: Unsupported system: OpenBSD...Uh-oh... #243

anoduck avatar Oct 24 '22 04:10 anoduck

Support local compilation in tsc-dyn-get.el: https://github.com/emacs-tree-sitter/elisp-tree-sitter/pull/190. (Note that the Rust source is included in the package.)

This bit doesn't seem to work, or I'm using it wrong. doom config.el:

;; workaround for https://github.com/emacs-tree-sitter/elisp-tree-sitter/issues/242
;; but it doesn't seem to do anything, still get x86_64 binaries
 (use-package tsc
   :init (setq tsc-dyn-get-from '(:compilation)))

Still tries to use pre-built x86_64-linux so on aarch64-linux.

LunNova avatar Apr 26 '23 04:04 LunNova