layouttest

package
v1.9.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2026 License: BSD-3-Clause Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package layouttest checks the Go mirrors of Win32 and COM structures, and the positions of COM methods in their vtables, against the mingw-w64 headers (SPEC §25.2, §6.3 rule 11).

A test describes what Go believes (the size and field offsets of a mirror read by reflection, the index of each method, the value of each constant). The package writes a C file of _Static_assert lines from the description and compiles it with `x86_64-w64-mingw32-gcc -fsyntax-only` (-DCOBJMACROS, for the C declarations of the vtables) against the real headers. It starts no process but the compiler, so it needs no Wine and runs on the Linux host; a wrong mirror is a compile error that names the structure and the field. It is skipped, with the install command, when the compiler or a header is missing.

The package has no OS tag, so it is listed on every OS; its compiler tests run on the host only.

Flutter: no counterpart

Index

Constants

View Source
const (
	// TagLayoutCompilerMissing is the tag of the error for a mingw-w64 compiler that is not installed.
	TagLayoutCompilerMissing = "LayoutCompilerMissing"
	// TagLayoutHeaderMissing is the tag of the error for a header the compiler does not find.
	TagLayoutHeaderMissing = "LayoutHeaderMissing"
	// TagLayoutMismatch is the tag of the error for a Go mirror that differs from the C header.
	TagLayoutMismatch = "LayoutMismatch"
	// TagLayoutCompileFailed is the tag of the error for any other compiler failure.
	TagLayoutCompileFailed = "LayoutCompileFailed"
	// TagLayoutCompilerRunFailed is the tag of the error for a compiler that cannot be started.
	TagLayoutCompilerRunFailed = "LayoutCompilerRunFailed"
	// TagLayoutFileFailed is the tag of the error for a C file that cannot be written.
	TagLayoutFileFailed = "LayoutFileFailed"
)

The tags of the errors that this file makes (rm4n0s/errors, R5).

View Source
const DefaultCompiler = "x86_64-w64-mingw32-gcc"

DefaultCompiler is the mingw-w64 compiler for 64-bit Windows (Fedora: mingw64-gcc).

Flutter: no counterpart

View Source
const InstallCommand = "sudo dnf install mingw64-gcc mingw64-headers"

InstallCommand installs the compiler and the headers on Fedora.

Flutter: no counterpart

Variables

This section is empty.

Functions

func Check

func Check(t testing.TB, spec Spec)

Check runs the default Checker over spec in a test. A missing compiler or header skips the test with the install command; any other failure fails it, with the compiler's own messages.

Flutter: no counterpart

func Command

func Command(compiler string, spec Spec, file string) []string

Command is the command line that compiles the C file of spec: the compiler, -fsyntax-only, the defines (COBJMACROS first), the include directories and the file.

Flutter: no counterpart

Types

type Checker

type Checker struct {
	// Compiler is the program to run; empty means DefaultCompiler.
	Compiler string
	// LookPath finds the compiler; nil means exec.LookPath.
	LookPath func(string) (string, error)
	// Run runs the compiler with its arguments and returns its output (standard output and error
	// together) and its exit code; the error is for a compiler that cannot be started, and is
	// returned by Check as it is (it must be tagged). nil runs the program with exec.
	Run func(ctx context.Context, argv []string) (output []byte, exitCode int, err error)
}

Checker compiles a Spec. The zero value uses the real compiler; the fields let a test use a fake runner or another compiler.

Flutter: no counterpart

func (Checker) Check

func (c Checker) Check(ctx context.Context, spec Spec) error

Check writes the C file of spec to a temporary directory and compiles it with -fsyntax-only. It returns nil when every assertion holds, and fails with:

  • LayoutCompilerMissing when the compiler is not installed (metadata "compiler" and "install");
  • LayoutHeaderMissing when a header is not found (metadata "header" and "install");
  • LayoutMismatch when a Go mirror differs from the header (metadata "failed", the messages of the assertions that failed, which name the structure and the field, and "output");
  • LayoutCompileFailed for any other compiler error, such as a type or member that the header does not have (metadata "output", "exit code" and "command");
  • LayoutCompilerRunFailed when the compiler cannot be started;
  • LayoutFileFailed when the C file cannot be written.

Flutter: no counterpart

type Const

type Const struct {
	// Name is the macro or enumerator in the headers.
	Name string
	// Value is the value Go uses, as the unsigned number the headers write (0x80004005).
	Value uint32
}

Const is a 32-bit constant, such as an HRESULT, with the value Go uses.

Flutter: no counterpart

type Field

type Field struct {
	// CName is the name of the member in the header.
	CName string
	// Offset is the offset of the Go field.
	Offset uintptr
	// Size is the size of the Go field.
	Size uintptr
}

Field is one member of a structure.

Flutter: no counterpart

type Method

type Method struct {
	// Name is the name of the method in the header, such as "QueryInterface".
	Name string
	// Index is the position Go uses, counted in pointers from the start of the vtable.
	Index int
}

Method is one entry of a vtable.

Flutter: no counterpart

type Spec

type Spec struct {
	// Headers are the headers to include, such as "windows.h" or "d3d12.h".
	Headers []string
	// Defines are macros to define besides COBJMACROS, such as "INITGUID".
	Defines []string
	// IncludeDirs are extra directories of headers (-I). The mingw-w64 compiler needs none.
	IncludeDirs []string
	// Structs are the mirrors of C structures.
	Structs []Struct
	// VTables are the vtables of COM interfaces.
	VTables []VTable
	// Consts are constants (HRESULT values, flags).
	Consts []Const
}

Spec is what a test believes about the C headers.

Flutter: no counterpart

func (Spec) Source

func (s Spec) Source() string

Source writes the C file that the spec stands for: the includes, then one _Static_assert for each size, offset, field size, method index and constant. The message of each assertion names what it checks ("RECT.left: offset"), so the compiler's output names the structure.

Flutter: no counterpart

type Struct

type Struct struct {
	// CName is the name the header gives the type, such as "RECT" or "WNDCLASSEXW".
	CName string
	// Size is the size of the Go type.
	Size uintptr
	// Fields are the fields, in Go's order.
	Fields []Field
}

Struct is the Go mirror of one C structure: its C name, size and fields.

Flutter: no counterpart

func StructOf

func StructOf(cName string, v any) Struct

StructOf reads the size and the fields of a Go struct value by reflection. Each field must carry a `c:"name"` tag with the member's name in the C header; a field named "_" (padding) is skipped, and so is a field tagged `c:"-"`. A value that is not a struct, or a field without the tag, is a mistake of the test and panics.

Flutter: no counterpart

type VTable

type VTable struct {
	// CName is the name of the vtable structure, such as "IUnknownVtbl" or "ID3D12DeviceVtbl".
	CName string
	// Methods are the methods Go calls or implements, with the index Go uses.
	Methods []Method
}

VTable is the vtable of a COM interface as the C header declares it (the type IFooVtbl).

Flutter: no counterpart

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL