modernc.org/xdmcp@v1.0.17/generator.go (about)

     1  // Copyright 2021 The Xdmcp-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  
    17  	"modernc.org/ccgo/v3/lib"
    18  )
    19  
    20  const (
    21  	tarFile = tarName + ".tar.gz"
    22  	tarName = "libXdmcp-1.1.2"
    23  )
    24  
    25  type supportedKey = struct{ os, arch string }
    26  
    27  var (
    28  	gcc       = ccgo.Env("GO_GENERATE_CC", ccgo.Env("CC", "gcc"))
    29  	gxx       = ccgo.Env("GO_GENERATE_CXX", ccgo.Env("CXX", "g++"))
    30  	goarch    = ccgo.Env("TARGET_GOARCH", runtime.GOARCH)
    31  	goos      = ccgo.Env("TARGET_GOOS", runtime.GOOS)
    32  	supported = map[supportedKey]struct{}{
    33  		{"darwin", "amd64"}: {},
    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  		"internal/test",
    53  	)
    54  	if tmpDir == "" {
    55  		tmpDir = ccgo.MustTempDir(true, "", "go-generate-")
    56  		defer os.RemoveAll(tmpDir)
    57  	}
    58  	ccgo.MustUntarFile(true, filepath.Join(tmpDir), tarFile, nil)
    59  	cdb, err := filepath.Abs(filepath.Join(tmpDir, "cdb.json"))
    60  	if err != nil {
    61  		ccgo.Fatal(true, err)
    62  	}
    63  
    64  	cc, err := exec.LookPath(gcc)
    65  	if err != nil {
    66  		ccgo.Fatal(true, err)
    67  	}
    68  
    69  	cxx, err := exec.LookPath(gxx)
    70  	if err != nil {
    71  		ccgo.Fatal(true, err)
    72  	}
    73  
    74  	os.Setenv("CC", cc)
    75  	os.Setenv("CXX", cxx)
    76  	cfg := []string{
    77  		"--enable-shared=no",
    78  	}
    79  	make := "make"
    80  	switch goos {
    81  	case "darwin":
    82  		make = "gmake"
    83  	}
    84  	if _, err := os.Stat(cdb); err != nil {
    85  		if !os.IsNotExist(err) {
    86  			ccgo.Fatal(true, err)
    87  		}
    88  
    89  		ccgo.MustInDir(true, filepath.Join(tmpDir, tarName), func() error {
    90  			ccgo.MustShell(true, "./configure", cfg...)
    91  			switch {
    92  			case verboseCompiledb:
    93  				ccgo.MustRun(true, "-verbose-compiledb", "-compiledb", cdb, make, "check")
    94  			default:
    95  				ccgo.MustRun(true, "-compiledb", cdb, make, "check")
    96  			}
    97  			return nil
    98  		})
    99  	}
   100  	ccgo.MustRun(true,
   101  		"-export-defines", "",
   102  		"-export-enums", "",
   103  		"-export-externs", "X",
   104  		"-export-fields", "F",
   105  		"-export-structs", "",
   106  		"-export-typedefs", "",
   107  		"-o", filepath.Join("lib", fmt.Sprintf("xdmcp_%s_%s.go", goos, goarch)),
   108  		"-pkgname", "xdmcp",
   109  		"-trace-translation-units",
   110  		cdb, ".libs/libXdmcp.a",
   111  	)
   112  	ccgo.MustRun(true,
   113  		"-export-fields", "F",
   114  		"-lmodernc.org/xdmcp/lib",
   115  		"-o", filepath.Join("internal", "test", fmt.Sprintf("main_%s_%s.go", goos, goarch)),
   116  		"-trace-translation-units",
   117  		cdb, "test/Array",
   118  	)
   119  }