modernc.org/xau@v1.0.16/generator.go (about) 1 // Copyright 2021 The Xau-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 = "libXau-1.0.9" 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/autest", 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 "--disable-xthreads", //TODO 79 } 80 make := "make" 81 switch goos { 82 case "darwin": 83 make = "gmake" 84 } 85 if _, err := os.Stat(cdb); err != nil { 86 if !os.IsNotExist(err) { 87 ccgo.Fatal(true, err) 88 } 89 90 ccgo.MustInDir(true, filepath.Join(tmpDir, tarName), func() error { 91 ccgo.MustShell(true, "./configure", cfg...) 92 switch { 93 case verboseCompiledb: 94 ccgo.MustRun(true, "-verbose-compiledb", "-compiledb", cdb, make, "check") 95 default: 96 ccgo.MustRun(true, "-compiledb", cdb, make, "check") 97 } 98 return nil 99 }) 100 } 101 ccgo.MustRun(true, 102 "-export-defines", "", 103 "-export-enums", "", 104 "-export-externs", "X", 105 "-export-fields", "F", 106 "-export-structs", "", 107 "-export-typedefs", "", 108 "-o", filepath.Join("lib", fmt.Sprintf("xau_%s_%s.go", goos, goarch)), 109 "-pkgname", "xau", 110 "-trace-translation-units", 111 cdb, "libXau.a", 112 ) 113 ccgo.MustRun(true, 114 "-export-fields", "F", 115 "-lmodernc.org/xau/lib", 116 "-o", filepath.Join("internal", "autest", fmt.Sprintf("main_%s_%s.go", goos, goarch)), 117 "-trace-translation-units", 118 cdb, "Autest", 119 ) 120 }