Skip to content

feat(tool/merge): merge per-platform packages - #1173

Merged
xushiwei merged 3 commits into
goplus:mainfrom
MeteorsLiu:codex/merge-platform-packages
Nov 24, 2026
Merged

xushiwei merged 3 commits into
goplus:mainfrom
MeteorsLiu:codex/merge-platform-packages

Conversation

@MeteorsLiu

Copy link
Copy Markdown
Member

Add AST-based merging for llcppg's per-platform output in tool/merge. Related to #1129.

  • Accept platform-tagged source ASTs through merge.Merge and merge.PlatformPackage.
  • Compare types, function and method signatures, and individual constants and variables across platforms, accounting for import paths and compiler directives.
  • Return shared and build-tagged platform files as ASTs with regenerated imports and common declaration comments.
  • Add libc golden fixtures for complex signatures, distinct link targets, platform-specific variables, and single-platform input.
  • Verify with llgo test -count=1 ./tool ./tool/merge/....

Callers can consume bindings for all input platforms through one Go package.

@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 new tool/merge package is well organized and well tested. I found two correctness bugs:

  • A var A, B = pair() declaration is written twice in the shared output, which doesn't compile.
  • The z_ check rejects z_*.go files that llcppg itself generates from _-prefixed headers.

There are also some smaller hardening, edge-case and license-header items. Details are inline. Performance looks fine: there is some repeated hashing and comparing per type spec in replaced and collectTypes, but nothing blocking.

blockDoc := declBlockDoc(shared.decl)
if i > 0 && groups[i-1].declIndex == group.declIndex {
gen := list[len(list)-1].(*ast.GenDecl)
gen.Specs = append(gen.Specs, shared.spec)

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.

Bug: a spec that declares several names is written twice. collectValues makes one group per name. For var A, B = pair() (which the comment at collect.go:55-56 says is supported), A and B both become shared groups, and chooseRef gives them the same declRef. After sorting they sit next to each other with the same declIndex, so this line appends the same spec a second time:

var (
	A, B = pair()
	A, B = pair()
)

That fails to compile (A redeclared). Skip a group whose declRef equals the previous one, or build shared value groups per spec instead of per name. A multi-name case in _testmerge would catch this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in 46c4a9e

Comment thread tool/merge/merge.go
for _, pkg := range pkgs {
platform := pkg.platform()
for name, file := range pkg.Files {
if strings.HasPrefix(name, "z_") {

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 check rejects files that llcppg itself generates. goFileOf (tool/gen.go:302-304) turns a header that starts with _ into a z_*.go file. For example, Darwin's _types.h becomes z_types.go. Merge would fail on that input with reserved file name. Only names that can actually collide with what build writes should be rejected, i.e. z_<goos>_<goarch>.go for the platforms being merged.

Comment thread tool/merge/build.go
constraints := make(map[string]string) // file name => build constraint
for platform, pkg := range p.files.pkgs {
if list := p.buildPlatform(platform); len(list) != 0 {
name := "z_" + pkg.Tags[0] + "_" + pkg.Tags[1] + ".go"

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.

Two smaller points:

  • Shared output keeps the source file name. Go reads some file names as build constraints: a header foo_linux.h produces foo_linux.go, and x_test.h produces x_test.go. A shared declaration written into such a file is silently dropped on the other platforms, or left out of the package entirely for _test.go. Rename or reject these names.
  • Tags go straight into the file name and the //go:build line. Only non-empty tags are checked. Tags {"linux_x","amd64"} and {"linux","x_amd64"} both map to the same file name, and the second overwrites the first in decls. A tag containing spaces, ||, / or a newline would corrupt the constraint or the path. Validating tags as identifiers in sortPackages (e.g. ^[a-z0-9]+$) covers all of this.

Comment thread tool/merge/collect.go

func (p *mergeIndex) collectValue(name string, decl *ast.GenDecl, ref declRef) {
refs := p.values[name]
if len(refs) != 0 {

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.

Minor: constants that rely on implicit repetition can be wrongly treated as equal. Specs are compared on their own syntax. In const (A = 1 << iota; B), the spec for B has no type and no value, so it matches a bare B on another platform even when the line above it differs. B then becomes shared and is printed alone as const B, which doesn't compile. llcppg may always write explicit values, but it would be safer to never share a ValueSpec with empty Values (or to compare it together with the initializer it inherits).

Comment thread tool/merge/type_map.go
@@ -0,0 +1,468 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in tool/merge/internal/astutil/LICENSE.

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.

The license file this header points to doesn't exist. This header (and type_map_test.go) refers to tool/merge/internal/astutil/LICENSE, and internal/astutil/equal.go refers to "the LICENSE file", but this PR adds no such file. The repository root LICENSE is Apache-2.0. Add the Go BSD LICENSE file there, or embed the text the way filter.go does.

Also, setHasher, theHasher and makeHasher are unused upstream leftovers. Their "do not use astHasher" comment is now misleading, because mergeIndex relies on astHasher.identName. Consider removing them.

@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.20253% with 33 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.30%. Comparing base (a60361c) to head (46c4a9e).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
tool/merge/type_map.go 92.59% 14 Missing ⚠️
tool/merge/collect_import.go 90.74% 5 Missing ⚠️
tool/merge/comment.go 95.96% 5 Missing ⚠️
tool/merge/build.go 96.47% 3 Missing ⚠️
tool/merge/internal/astutil/equal.go 92.85% 3 Missing ⚠️
tool/merge/build_shared.go 98.58% 2 Missing ⚠️
tool/merge/collect.go 98.76% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1173      +/-   ##
==========================================
+ Coverage   91.35%   92.30%   +0.94%     
==========================================
  Files          27       39      +12     
  Lines        3587     4456     +869     
==========================================
+ Hits         3277     4113     +836     
- Misses        310      343      +33     
Flag Coverage Δ
llgo-tests 92.30% <96.20%> (+0.94%) ⬆️

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.

Group const and var declarations by spec. Add golden coverage for input validation, types and methods, import references, and comments.
@xushiwei
xushiwei merged commit 597bc33 into goplus:main Nov 24, 2026
4 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