go/build: support language-specific go:build comment prefixes for non-Go files#14
Open
hbrooks wants to merge 1 commit into
Open
go/build: support language-specific go:build comment prefixes for non-Go files#14hbrooks wants to merge 1 commit into
go:build comment prefixes for non-Go files#14hbrooks wants to merge 1 commit into
Conversation
…-Go files
Files in languages other than Go that happen to share an extension
recognised by the Go toolchain (most notably MATLAB .m files, which
Go treats as Objective-C) cannot currently carry go:build constraints
using their native comment syntax. The only workaround is to add a
//go:build line at the top of the file, but that line is a syntax
error in MATLAB (and other languages), causing those tools to reject
the file.
This change teaches the build system to recognise language-specific
line-comment prefixes for go:build directives in non-Go source files:
Extension Prefix Language
.m % go:build MATLAB
.f90 ! go:build Fortran (free-form)
A MATLAB file can now opt out of the build with:
% go:build ignore
and MATLAB itself will continue to parse the file normally, because
the line is a valid MATLAB comment.
Implementation
--------------
* go/build/build.go
- extCommentPrefix: map[string][]byte of extension → comment prefix
- goBuildCommentForExt: helper to look up the prefix
- isGoBuildCommentWithPrefix: generalised isGoBuildComment
- parseFileHeaderWithPrefix: like parseFileHeader but for single-
line comment syntaxes (no /* */ block-comment tracking needed)
- shouldBuildWithPrefix: shouldBuild extended with an optional
commentPrefix parameter; shouldBuild delegates to it with nil
- matchFile: passes the appropriate commentPrefix for non-Go files
* cmd/go/internal/modindex/build.go
Same changes mirrored in the module-index copy of the logic:
getConstraintsWithPrefix, parseFileHeaderWithPrefix,
isGoBuildCommentWithPrefix, extCommentPrefix.
Tests
-----
* TestShouldBuildWithPrefix – unit tests for % go:build and # go:build
* TestMatlabFileBuildTags – ImportDir integration test using real
testdata .m files (ignored.m, included.m, linux_only.m)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mirror of golang/go#79537 by @MohammadRaziei