Skip to content

cli: Adapt default Makefile/CMakeLists for repos not storing grammar.json#4646

Merged
ObserverOfTime merged 3 commits intotree-sitter:masterfrom
wetneb:makefile
Aug 2, 2025
Merged

cli: Adapt default Makefile/CMakeLists for repos not storing grammar.json#4646
ObserverOfTime merged 3 commits intotree-sitter:masterfrom
wetneb:makefile

Conversation

@wetneb
Copy link
Contributor

@wetneb wetneb commented Jul 27, 2025

This is a new version of #4580, to make it easier not to include tree-sitter generated files in grammar repos, as outlined in #930.

This makes it possible to run make or cmake --build . in grammar repos where grammar.json hasn't been included, and still get the parser compilation to work. The build scripts still work if grammar.json is included.

When grammar.json is present, this gives:

$ make
tree-sitter generate --stage=parser src/grammar.json
cc -Isrc -std=c11 -fPIC   -c -o src/parser.o src/parser.c
…

When only grammar.js is present, this gives:

$ make
tree-sitter generate --stage=json grammar.js
tree-sitter generate --stage=parser src/grammar.json
cc -Isrc -std=c11 -fPIC   -c -o src/parser.o src/parser.c
…

(and similarly for cmake --build .)

I don't know if it's possible to encourage make to just run tree-sitter generate grammar.js in this case, generating all files in one go. The rules with grouped targets seem to be theoretically designed for that, but I couldn't get it to work without overriding the more granular rules. I think it could also work without this feature, as the overhead of starting two processes shouldn't be significant compared to the complexity of the generation work.

…json

This is a new version of tree-sitter#4580, to make it easier not to include
tree-sitter generated files in grammar repos, as outlined in tree-sitter#930.

This makes it possible to run `make` or `cmake --build .` in grammar repos where
`grammar.json` hasn't been included, and still get the parser
compilation to work. The build scripts still work if `grammar.json` is
included.
Copy link
Member

@ObserverOfTime ObserverOfTime left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add migrations to init.rs.

@ObserverOfTime
Copy link
Member

I don't know if it's possible to encourage make to just run tree-sitter generate grammar.js in this case, generating all files in one go.

Maybe we can use a variable (TS_GENERATE_ALL?) for it.

@wetneb
Copy link
Contributor Author

wetneb commented Jul 31, 2025

I've added migrations and tested them on a couple of grammars with tree-sitter init -u. They seem to work as expected.

Maybe we can use a variable (TS_GENERATE_ALL?) for it.

I don't know how to do that, and am not sure if it's worth it if people need to enable a specific variable for it. I propose to stick with the two-stage generation. People can also edit the Makefile if they prefer a different process, I would say.

@ObserverOfTime
Copy link
Member

ifdef TS_GENERATE_ALL
$(PARSER): grammar.js
	$(TS) generate $^
else
$(SRC_DIR)/grammar.json: grammar.js
	$(TS) generate --stage=json $^

$(PARSER): $(SRC_DIR)/grammar.json
	$(TS) generate --stage=parser $^
endif

Similarly for CMake, but as you said it's probably not worth it.

Co-authored-by: ObserverOfTime <chronobserver@disroot.org>
@ObserverOfTime ObserverOfTime merged commit b787f31 into tree-sitter:master Aug 2, 2025
29 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants