modernc.org/xrender@v1.0.9/generator.go (about)

     1  //  Copyright 2021 The Xrender-Go Authors. All rights reserved.
     2  //  Use of this 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 = "libXrender-0.9.10"
    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", "arm64"}: {},
    34  		{"linux", "386"}:    {},
    35  		{"linux", "amd64"}:  {},
    36  		{"linux", "arm"}:    {},
    37  		{"linux", "arm64"}:  {},
    38  	}
    39  	tmpDir           = ccgo.Env("GO_GENERATE_TMPDIR", "")
    40  	verboseCompiledb = ccgo.Env("GO_GENERATE_VERBOSE", "") == "1"
    41  )
    42  
    43  func main() {
    44  	fmt.Printf("Running on %s/%s.\n", runtime.GOOS, runtime.GOARCH)
    45  	if _, ok := supported[supportedKey{goos, goarch}]; !ok {
    46  		ccgo.Fatalf(true, "unsupported target: %s/%s", goos, goarch)
    47  	}
    48  
    49  	ccgo.MustMkdirs(true,
    50  		"lib",
    51  	)
    52  	if tmpDir == "" {
    53  		tmpDir = ccgo.MustTempDir(true, "", "go-generate-")
    54  		defer os.RemoveAll(tmpDir)
    55  	}
    56  	ccgo.MustUntarFile(true, filepath.Join(tmpDir), tarFile, nil)
    57  	cdb, err := filepath.Abs(filepath.Join(tmpDir, "cdb.json"))
    58  	if err != nil {
    59  		ccgo.Fatal(true, err)
    60  	}
    61  
    62  	cc, err := exec.LookPath(gcc)
    63  	if err != nil {
    64  		ccgo.Fatal(true, err)
    65  	}
    66  
    67  	cxx, err := exec.LookPath(gxx)
    68  	if err != nil {
    69  		ccgo.Fatal(true, err)
    70  	}
    71  
    72  	os.Setenv("CC", cc)
    73  	os.Setenv("CXX", cxx)
    74  	cfg := []string{}
    75  	make := "make"
    76  	switch goos {
    77  	case "darwin":
    78  		make = "gmake"
    79  	}
    80  	if _, err := os.Stat(cdb); err != nil {
    81  		if !os.IsNotExist(err) {
    82  			ccgo.Fatal(true, err)
    83  		}
    84  
    85  		ccgo.MustInDir(true, filepath.Join(tmpDir, tarName), func() error {
    86  			ccgo.MustShell(true, "./configure", cfg...)
    87  			switch {
    88  			case verboseCompiledb:
    89  				ccgo.MustRun(true, "-verbose-compiledb", "-compiledb", cdb, make)
    90  			default:
    91  				ccgo.MustRun(true, "-compiledb", cdb, make)
    92  			}
    93  			return nil
    94  		})
    95  	}
    96  	ccgo.MustRun(true,
    97  		"-export-defines", "",
    98  		"-export-enums", "",
    99  		"-export-externs", "X",
   100  		"-export-fields", "F",
   101  		"-export-structs", "",
   102  		"-export-typedefs", "",
   103  		"-lmodernc.org/x11/lib",
   104  		"-o", filepath.Join("lib", fmt.Sprintf("xrender_%s_%s.go", goos, goarch)),
   105  		"-pkgname", "xrender",
   106  		"-trace-translation-units",
   107  		cdb, "libXrender.a",
   108  	)
   109  }