Repository navigation
feat(cmd): add -merge mode for per-platform packages - #1176
MeteorsLiu wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Review summary
The -merge subcommand is wired in cleanly and reuses tool/merge. I left four inline comments. The main ones:
- Platform directory names aren't validated. Misnamed directories are skipped without a warning, and the names go into the build tag and the output file name without escaping.
- Old files in the output directory are never removed.
writeMergedFileis a copy of the test helper intool/merge.- The CLI test covers only 4 hard-coded fixtures.
I found no performance problems.
| } | ||
| tags := strings.Split(platform.Name(), "-") | ||
| if len(tags) != 2 || tags[0] == "" || tags[1] == "" { | ||
| continue |
There was a problem hiding this comment.
Directory names that aren't exactly GOOS-GOARCH are skipped with no warning, and names that do have one - are trusted as they are:
- A typo like
linux_amd64, or a barelinux(theinvalid_missing_archfixture), quietly drops that platform from the output. The user only gets an error when no valid directory is left, and then it's the vagueno platform packages, not merge'sexpected GOOS and GOARCH tags. - With the default
srcDir=".", any unrelated hyphenated folder (test-data,my-lib) is read as a platform. - Both tags are pasted unescaped into
//go:build <os> && <arch>and into thez_<os>_<arch>.gofile name. A name containing spaces or||changes the build constraint.
Suggestion: check both parts against the known GOOS/GOARCH values (or at least ^[a-z0-9]+$), and return an error for directories that don't match instead of continue.
| if err != nil { | ||
| return err | ||
| } | ||
| if err := os.MkdirAll(destDir, 0755); err != nil { |
There was a problem hiding this comment.
Files already in destDir are left alone. If the directory holds output from an earlier run, for example a z_linux_arm64.go from a run with more platforms, or a file whose header was later removed, the old file stays next to the new ones. The package then fails to build with duplicate or out-of-date declarations. Consider clearing the generated *.go files first, or refusing to write into a directory that isn't empty.
| return nil | ||
| } | ||
|
|
||
| func writeMergedFile(path string, fset *token.FileSet, input *ast.File) error { |
There was a problem hiding this comment.
This is a copy of writeMergedFile in tool/merge/merge_test.go. The golden tests in tool/merge exercise the test copy, not this one, so the two can drift apart without any test failing. Consider exporting a single helper from tool/merge (for example merge.Format(w io.Writer, fset, f)) and using it in both places.
|
|
||
| func TestMergeCLI(t *testing.T) { | ||
| const root = "../../tool/merge/_testmerge" | ||
| for _, fixture := range []string{"dlfcn", "locale_single", "comment_edges", "invalid_package_names"} { |
There was a problem hiding this comment.
This fixture list is hard-coded. Cases where the CLI's directory handling differs from merge.Merge, such as invalid_missing_arch (which this CLI currently skips silently), are never run. Consider looping over every directory in _testmerge, or at least adding the cases the CLI handles differently, each with the error the CLI is meant to produce.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1176 +/- ##
=======================================
Coverage 92.30% 92.30%
=======================================
Files 39 39
Lines 4456 4456
=======================================
Hits 4113 4113
Misses 343 343
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Expose the AST merger introduced in #1173 through
llcppg -merge <dest-dir> [<src-dir>], as the CLI follow-up to #1129.The implementation includes:
llcppg.cfgor invoking Clang.GOOS-GOARCHdirectories and reusespputil.ListFilesandtool.LoadGoFilesto parse their Go files with comments into a shared file set.merge.Mergeand writes formatted Go files through a private CLI writer, preserving build constraints and link directives without changing the merge API.llgo test -count=1 ./cmd/llcppg ./tool/merge/... ./tool/pputil/...and an actual CLI invocation.This lets users merge generated bindings into one Go package while keeping ordinary generation unchanged;
llcppg.pub, recursive subpackages, and stale-file cleanup remain outside this change.