modernc.org/xcb@v1.0.15/generator.go (about)

     1  // Copyright 2021 The Xcb-Go Authors. All rights reserved.
     2  // Use of the source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build ignore
     6  // +build ignore
     7  
     8  package main
     9  
    10  import (
    11  	"fmt"
    12  	"os"
    13  	"os/exec"
    14  	"path/filepath"
    15  	"runtime"
    16  	"strings"
    17  
    18  	"modernc.org/ccgo/v3/lib"
    19  )
    20  
    21  const (
    22  	tarFile = tarName + ".tar.gz"
    23  	tarName = "libxcb-1.14"
    24  )
    25  
    26  type supportedKey = struct{ os, arch string }
    27  
    28  var (
    29  	gcc       = ccgo.Env("GO_GENERATE_CC", ccgo.Env("CC", "gcc"))
    30  	gxx       = ccgo.Env("GO_GENERATE_CXX", ccgo.Env("CXX", "g++"))
    31  	goarch    = ccgo.Env("TARGET_GOARCH", runtime.GOARCH)
    32  	goos      = ccgo.Env("TARGET_GOOS", runtime.GOOS)
    33  	supported = map[supportedKey]struct{}{
    34  		{"darwin", "arm64"}: {},
    35  		{"linux", "386"}:    {},
    36  		{"linux", "amd64"}:  {},
    37  		{"linux", "arm"}:    {},
    38  		{"linux", "arm64"}:  {},
    39  	}
    40  	tmpDir           = ccgo.Env("GO_GENERATE_TMPDIR", "")
    41  	verboseCompiledb = ccgo.Env("GO_GENERATE_VERBOSE", "") == "1"
    42  )
    43  
    44  func main() {
    45  	fmt.Printf("Running on %s/%s.\n", runtime.GOOS, runtime.GOARCH)
    46  	if _, ok := supported[supportedKey{goos, goarch}]; !ok {
    47  		ccgo.Fatalf(true, "unsupported target: %s/%s", goos, goarch)
    48  	}
    49  
    50  	ccgo.MustMkdirs(true,
    51  		"lib",
    52  	)
    53  	if tmpDir == "" {
    54  		tmpDir = ccgo.MustTempDir(true, "", "go-generate-")
    55  		defer os.RemoveAll(tmpDir)
    56  	}
    57  	ccgo.MustUntarFile(true, filepath.Join(tmpDir), tarFile, nil)
    58  	cdb, err := filepath.Abs(filepath.Join(tmpDir, "cdb.json"))
    59  	if err != nil {
    60  		ccgo.Fatal(true, err)
    61  	}
    62  
    63  	cc, err := exec.LookPath(gcc)
    64  	if err != nil {
    65  		ccgo.Fatal(true, err)
    66  	}
    67  
    68  	cxx, err := exec.LookPath(gxx)
    69  	if err != nil {
    70  		ccgo.Fatal(true, err)
    71  	}
    72  
    73  	os.Setenv("CC", cc)
    74  	os.Setenv("CXX", cxx)
    75  	cfg := []string{
    76  		"--enable-shared=no",
    77  		"--enable-devel-docs=no",
    78  		"--enable-ge",
    79  		"--enable-xevie",
    80  		"--enable-xprint",
    81  	}
    82  	make := "make"
    83  	switch goos {
    84  	case "darwin":
    85  		make = "gmake"
    86  	}
    87  	if _, err := os.Stat(cdb); err != nil {
    88  		if !os.IsNotExist(err) {
    89  			ccgo.Fatal(true, err)
    90  		}
    91  
    92  		ccgo.MustInDir(true, filepath.Join(tmpDir, tarName), func() error {
    93  			ccgo.MustShell(true, "./configure", cfg...)
    94  			switch {
    95  			case verboseCompiledb:
    96  				ccgo.MustRun(true, "-verbose-compiledb", "-compiledb", cdb, make, "clean", "all")
    97  			default:
    98  				ccgo.MustRun(true, "-compiledb", cdb, make, "clean", "all")
    99  			}
   100  			return nil
   101  		})
   102  	}
   103  	ccgo.MustRun(true,
   104  		"-export-defines", "",
   105  		"-export-enums", "",
   106  		"-export-externs", "X",
   107  		"-export-fields", "F",
   108  		"-export-structs", "",
   109  		"-export-typedefs", "",
   110  		"-lmodernc.org/xau/lib",
   111  		"-lmodernc.org/xdmcp/lib",
   112  		"-o", filepath.Join("lib", fmt.Sprintf("xcb_%s_%s.go", goos, goarch)),
   113  		"-pkgname", "xcb",
   114  		"-trace-translation-units",
   115  		cdb,
   116  		".libs/libxcb-composite.a",
   117  		".libs/libxcb-damage.a",
   118  		".libs/libxcb-dpms.a",
   119  		".libs/libxcb-dri2.a",
   120  		".libs/libxcb-dri3.a",
   121  		".libs/libxcb-ge.a",
   122  		".libs/libxcb-glx.a",
   123  		".libs/libxcb-present.a",
   124  		".libs/libxcb-randr.a",
   125  		".libs/libxcb-record.a",
   126  		".libs/libxcb-render.a",
   127  		".libs/libxcb-res.a",
   128  		".libs/libxcb-screensaver.a",
   129  		".libs/libxcb-shape.a",
   130  		".libs/libxcb-shm.a",
   131  		".libs/libxcb-sync.a",
   132  		".libs/libxcb-xevie.a",
   133  		".libs/libxcb-xf86dri.a",
   134  		".libs/libxcb-xfixes.a",
   135  		".libs/libxcb-xinerama.a",
   136  		".libs/libxcb-xinput.a",
   137  		".libs/libxcb-xkb.a",
   138  		".libs/libxcb-xprint.a",
   139  		".libs/libxcb-xtest.a",
   140  		".libs/libxcb-xv.a",
   141  		".libs/libxcb-xvmc.a",
   142  		".libs/libxcb.a",
   143  	)
   144  	if err := filepath.Walk("examples", func(path string, info os.FileInfo, err error) error {
   145  		if err != nil {
   146  			return err
   147  		}
   148  
   149  		if info.IsDir() {
   150  			return nil
   151  		}
   152  
   153  		if !strings.HasSuffix(path, ".c") {
   154  			return nil
   155  		}
   156  
   157  		out := path[:len(path)-len(".c")]
   158  		ccgo.MustRun(true,
   159  			"-export-fields", "F",
   160  			"-lmodernc.org/xcb/lib",
   161  			"-I/opt/homebrew/Cellar/libxcb/1.14_1/include",
   162  			"-o", fmt.Sprintf("%s_%s_%s.go", out, goos, goarch),
   163  			path,
   164  		)
   165  		return nil
   166  	}); err != nil {
   167  		ccgo.Fatal(true, err)
   168  	}
   169  }