modernc.org/z@v1.7.4/generator.go (about)

     1  // Copyright 2021 The Zlib-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  	tarDir  = "zlib-1.2.11"
    22  	tarFile = tarName + ".tar.gz"
    23  	tarName = tarDir
    24  )
    25  
    26  type supportedKey = struct{ os, arch string }
    27  
    28  var (
    29  	gcc       = ccgo.Env("GO_GENERATE_CC", ccgo.Env("CC", "gcc"))
    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  		{"freebsd", "386"}:   {},
    36  		{"freebsd", "amd64"}: {},
    37  		{"freebsd", "arm"}:   {},
    38  		{"linux", "386"}:     {},
    39  		{"linux", "amd64"}:   {},
    40  		{"linux", "arm"}:     {},
    41  		{"linux", "arm64"}:   {},
    42  		{"linux", "riscv64"}: {},
    43  		{"linux", "ppc64le"}: {},
    44  		{"linux", "s390x"}:   {},
    45  		{"netbsd", "amd64"}:  {},
    46  		{"openbsd", "amd64"}: {},
    47  		{"openbsd", "arm64"}: {},
    48  		{"windows", "386"}:   {},
    49  		{"windows", "amd64"}: {},
    50  		{"windows", "arm64"}: {},
    51  	}
    52  	tmpDir = ccgo.Env("GO_GENERATE_TMPDIR", "")
    53  )
    54  
    55  func main() {
    56  	fmt.Printf("Running on %s/%s.\n", runtime.GOOS, runtime.GOARCH)
    57  	if _, ok := supported[supportedKey{goos, goarch}]; !ok {
    58  		ccgo.Fatalf(true, "unsupported target: %s/%s", goos, goarch)
    59  	}
    60  
    61  	ccgo.MustMkdirs(true,
    62  		"internal",
    63  		"lib",
    64  	)
    65  	if tmpDir == "" {
    66  		tmpDir = ccgo.MustTempDir(true, "", "go-generate-")
    67  		defer os.RemoveAll(tmpDir)
    68  	}
    69  	srcDir := tmpDir + "/" + tarDir
    70  	os.RemoveAll(srcDir)
    71  	ccgo.MustUntarFile(true, tmpDir, tarFile, nil)
    72  	cdb, err := filepath.Abs(tmpDir + "/cdb.json")
    73  	if err != nil {
    74  		ccgo.Fatal(true, err)
    75  	}
    76  
    77  	cc, err := exec.LookPath(gcc)
    78  	if err != nil {
    79  		ccgo.Fatal(true, err)
    80  	}
    81  
    82  	os.Setenv("CC", cc)
    83  	if _, err := os.Stat(cdb); err != nil {
    84  		if !os.IsNotExist(err) {
    85  			ccgo.Fatal(true, err)
    86  		}
    87  
    88  		make := "make"
    89  		ccgo.MustInDir(true, srcDir, func() error {
    90  			switch goos {
    91  			case "windows":
    92  				ccgo.MustRun(true, "-compiledb", cdb, "make", "-fwin32/Makefile.gcc", "example.exe", "minigzip.exe")
    93  			case "darwin", "freebsd", "netbsd", "openbsd":
    94  				make = "gmake"
    95  				fallthrough
    96  			case "linux":
    97  				ccgo.MustShell(true, "./configure", "--static")
    98  				ccgo.MustRun(true, "-compiledb", cdb, make, "test64")
    99  			}
   100  			return nil
   101  		})
   102  	}
   103  	switch goos {
   104  	case "windows":
   105  		ccgo.MustRun(true,
   106  			"-export-defines", "",
   107  			"-export-enums", "",
   108  			"-export-externs", "X",
   109  			"-export-fields", "F",
   110  			"-export-structs", "",
   111  			"-export-typedefs", "",
   112  			"-o", filepath.Join("lib", fmt.Sprintf("z_%s_%s.go", goos, goarch)),
   113  			"-pkgname", "z",
   114  			"-trace-translation-units",
   115  			cdb, "libz.a",
   116  		)
   117  		ccgo.MustRun(true,
   118  			"-lmodernc.org/z/lib",
   119  			"-o", filepath.Join("internal", fmt.Sprintf("minigzip_%s_%s.go", goos, goarch)),
   120  			"-trace-translation-units",
   121  			cdb, "minigzip.exe",
   122  		)
   123  		ccgo.MustRun(true,
   124  			"-lmodernc.org/z/lib",
   125  			"-o", filepath.Join("internal", fmt.Sprintf("example_%s_%s.go", goos, goarch)),
   126  			"-trace-translation-units",
   127  			cdb, "example.exe",
   128  		)
   129  	case "darwin", "linux", "freebsd", "netbsd", "openbsd":
   130  		ccgo.MustRun(true,
   131  			"-export-defines", "",
   132  			"-export-enums", "",
   133  			"-export-externs", "X",
   134  			"-export-fields", "F",
   135  			"-export-structs", "",
   136  			"-export-typedefs", "",
   137  			"-o", filepath.Join("lib", fmt.Sprintf("z_%s_%s.go", goos, goarch)),
   138  			"-pkgname", "z",
   139  			"-trace-translation-units",
   140  			cdb, "libz.a",
   141  		)
   142  		ccgo.MustRun(true,
   143  			"-lmodernc.org/z/lib",
   144  			"-o", filepath.Join("internal", fmt.Sprintf("minigzip_%s_%s.go", goos, goarch)),
   145  			"-trace-translation-units",
   146  			cdb, "minigzip64",
   147  		)
   148  		ccgo.MustRun(true,
   149  			"-lmodernc.org/z/lib",
   150  			"-o", filepath.Join("internal", fmt.Sprintf("example_%s_%s.go", goos, goarch)),
   151  			"-trace-translation-units",
   152  			cdb, "example64",
   153  		)
   154  	}
   155  }