Skip to content

feat(cmd): add -merge mode for per-platform packages - #1176

Open
MeteorsLiu wants to merge 1 commit into
goplus:mainfrom
MeteorsLiu:codex/merge-cli
Open

MeteorsLiu wants to merge 1 commit into
goplus:mainfrom
MeteorsLiu:codex/merge-cli

Conversation

@MeteorsLiu

Copy link
Copy Markdown
Member

Expose the AST merger introduced in #1173 through llcppg -merge <dest-dir> [<src-dir>], as the CLI follow-up to #1129.

The implementation includes:

  • Adds an independent merge mode that consumes previously generated platform packages without reading llcppg.cfg or invoking Clang.
  • Discovers GOOS-GOARCH directories and reuses pputil.ListFiles and tool.LoadGoFiles to parse their Go files with comments into a shared file set.
  • Calls merge.Merge and writes formatted Go files through a private CLI writer, preserving build constraints and link directives without changing the merge API.
  • Reuses existing golden fixtures for multi-platform output, single-platform output, comments, and merge errors. Verified with 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.

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.
  • writeMergedFile is a copy of the test helper in tool/merge.
  • The CLI test covers only 4 hard-coded fixtures.

I found no performance problems.

Comment thread cmd/llcppg/merge.go
}
tags := strings.Split(platform.Name(), "-")
if len(tags) != 2 || tags[0] == "" || tags[1] == "" {
continue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 bare linux (the invalid_missing_arch fixture), quietly drops that platform from the output. The user only gets an error when no valid directory is left, and then it's the vague no platform packages, not merge's expected 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 the z_<os>_<arch>.go file 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.

Comment thread cmd/llcppg/merge.go
if err != nil {
return err
}
if err := os.MkdirAll(destDir, 0755); err != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread cmd/llcppg/merge.go
return nil
}

func writeMergedFile(path string, fset *token.FileSet, input *ast.File) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread cmd/llcppg/merge_test.go

func TestMergeCLI(t *testing.T) {
const root = "../../tool/merge/_testmerge"
for _, fixture := range []string{"dlfcn", "locale_single", "comment_edges", "invalid_package_names"} {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.30%. Comparing base (597bc33) to head (0501ebe).

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           
Flag Coverage Δ
llgo-tests 92.30% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant