From 8f16aa158385f1ee3a0f757cdea5d1d917c47f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Sun, 10 Mar 2019 14:39:39 +0200 Subject: [PATCH] build: nest generated files to clean up repo root --- build/libtor_external.go.in | 24 ++++++ build/{libtor.go.in => libtor_internal.go.in} | 0 build/wrap.go | 80 ++++++++++--------- 3 files changed, 67 insertions(+), 37 deletions(-) create mode 100644 build/libtor_external.go.in rename build/{libtor.go.in => libtor_internal.go.in} (100%) diff --git a/build/libtor_external.go.in b/build/libtor_external.go.in new file mode 100644 index 00000000..3b3194f9 --- /dev/null +++ b/build/libtor_external.go.in @@ -0,0 +1,24 @@ +// go-libtor - Self-contained Tor from Go +// Copyright (c) 2018 Péter Szilágyi. All rights reserved. + +// Package libtor is a self-contained static tor library. +package libtor + +// This file is a wrapper around the internal libtor package to keep the original +// Go API, but move the thousands of generated Go files into a sub-folder, out of +// the way of the repo root. + +import ( + "github.com/cretz/bine/process" + "github.com/ipsn/go-libtor/libtor" +) + +// ProviderVersion returns the Tor provider name and version exposed from the +// Tor embedded API. +func ProviderVersion() string { + return libtor.ProviderVersion() +} + +// Creator implements the bine.process.Creator, permitting libtor to act as an API +// backend for the bine/tor Go interface. +var Creator process.Creator = libtor.Creator diff --git a/build/libtor.go.in b/build/libtor_internal.go.in similarity index 100% rename from build/libtor.go.in rename to build/libtor_internal.go.in diff --git a/build/wrap.go b/build/wrap.go index 64ae21fb..dc99d291 100644 --- a/build/wrap.go +++ b/build/wrap.go @@ -34,7 +34,7 @@ func main() { } for _, file := range files { if file.IsDir() { - if strings.HasSuffix(file.Name(), "_config") { + if strings.HasSuffix(file.Name(), "_config") || file.Name() == "libtor" { os.RemoveAll(file.Name()) } continue @@ -44,8 +44,11 @@ func main() { } } // Copy in the library preamble with the architecture definitions + if err := os.MkdirAll("libtor", 0755); err != nil { + panic(err) + } blob, _ := ioutil.ReadFile(filepath.Join("build", "libtor_preamble.go.in")) - ioutil.WriteFile("libtor_preamble.go", blob, 0644) + ioutil.WriteFile(filepath.Join("libtor", "libtor_preamble.go"), blob, 0644) // Wrap each of the component libraries into megator zlibVer, zlibHash, err := wrapZlib(*nobuild) @@ -64,8 +67,11 @@ func main() { if err != nil { panic(err) } - // Copy in the tor entrypoint wrapper, fill out the readme template - blob, _ = ioutil.ReadFile(filepath.Join("build", "libtor.go.in")) + // Copy in the tor entrypoint wrappers, fill out the readme template + blob, _ = ioutil.ReadFile(filepath.Join("build", "libtor_internal.go.in")) + ioutil.WriteFile(filepath.Join("libtor", "libtor.go"), blob, 0644) + + blob, _ = ioutil.ReadFile(filepath.Join("build", "libtor_external.go.in")) ioutil.WriteFile("libtor.go", blob, 0644) tmpl := template.Must(template.ParseFiles(filepath.Join("build", "README.md"))) @@ -136,10 +142,10 @@ func wrapZlib(nobuild bool) (string, string, error) { } if ext := filepath.Ext(file.Name()); ext == ".c" { name := strings.TrimSuffix(file.Name(), ext) - ioutil.WriteFile("zlib_"+name+".go", []byte(fmt.Sprintf(zlibTemplate, name)), 0644) + ioutil.WriteFile(filepath.Join("libtor", "zlib_"+name+".go"), []byte(fmt.Sprintf(zlibTemplate, name)), 0644) } } - ioutil.WriteFile("zlib_preamble.go", []byte(zlibPreamble), 0644) + ioutil.WriteFile(filepath.Join("libtor", "zlib_preamble.go"), []byte(zlibPreamble), 0644) // Ensure the library builds if !nobuild { @@ -159,7 +165,7 @@ var zlibPreamble = `// go-libtor - Self-contained Tor from Go package libtor /* -#cgo CFLAGS: -I${SRCDIR}/zlib +#cgo CFLAGS: -I${SRCDIR}/../zlib */ import "C" ` @@ -171,7 +177,7 @@ var zlibTemplate = `// go-libtor - Self-contained Tor from Go package libtor /* -#include +#include <../zlib/%s.c> */ import "C" ` @@ -266,9 +272,9 @@ func wrapLibevent(nobuild bool) (string, string, error) { } // Generate Go wrappers for each C source individually for _, dep := range deps { - ioutil.WriteFile("libevent_"+dep[1]+".go", []byte(fmt.Sprintf(libeventTemplate, dep[1])), 0644) + ioutil.WriteFile(filepath.Join("libtor", "libevent_"+dep[1]+".go"), []byte(fmt.Sprintf(libeventTemplate, dep[1])), 0644) } - ioutil.WriteFile("libevent_preamble.go", []byte(libeventPreamble), 0644) + ioutil.WriteFile(filepath.Join("libtor", "libevent_preamble.go"), []byte(libeventPreamble), 0644) // Inject the configuration headers and ensure everything builds os.MkdirAll(filepath.Join("libevent_config", "event2"), 0755) @@ -302,10 +308,10 @@ var libeventPreamble = `// go-libtor - Self-contained Tor from Go package libtor /* -#cgo CFLAGS: -I${SRCDIR}/libevent_config -#cgo CFLAGS: -I${SRCDIR}/libevent -#cgo CFLAGS: -I${SRCDIR}/libevent/compat -#cgo CFLAGS: -I${SRCDIR}/libevent/include +#cgo CFLAGS: -I${SRCDIR}/../libevent_config +#cgo CFLAGS: -I${SRCDIR}/../libevent +#cgo CFLAGS: -I${SRCDIR}/../libevent/compat +#cgo CFLAGS: -I${SRCDIR}/../libevent/include */ import "C" ` @@ -318,7 +324,7 @@ package libtor /* #include -#include <%s.c> +#include <../%s.c> */ import "C" ` @@ -447,9 +453,9 @@ func wrapOpenSSL(nobuild bool) (string, string, error) { } // Anything else is wrapped directly with Go gofile := strings.Replace(dep[1], "/", "_", -1) + ".go" - ioutil.WriteFile("openssl_"+gofile, []byte(fmt.Sprintf(opensslTemplate, dep[1])), 0644) + ioutil.WriteFile(filepath.Join("libtor", "openssl_"+gofile), []byte(fmt.Sprintf(opensslTemplate, dep[1])), 0644) } - ioutil.WriteFile("openssl_preamble.go", []byte(opensslPreamble), 0644) + ioutil.WriteFile(filepath.Join("libtor", "openssl_preamble.go"), []byte(opensslPreamble), 0644) // Inject the configuration headers and ensure everything builds os.MkdirAll(filepath.Join("openssl_config", "internal"), 0755) @@ -496,13 +502,13 @@ var opensslPreamble = `// go-libtor - Self-contained Tor from Go package libtor /* -#cgo CFLAGS: -I${SRCDIR}/openssl_config -#cgo CFLAGS: -I${SRCDIR}/openssl -#cgo CFLAGS: -I${SRCDIR}/openssl/include -#cgo CFLAGS: -I${SRCDIR}/openssl/crypto/include -#cgo CFLAGS: -I${SRCDIR}/openssl/crypto/ec/curve448 -#cgo CFLAGS: -I${SRCDIR}/openssl/crypto/ec/curve448/arch_32 -#cgo CFLAGS: -I${SRCDIR}/openssl/crypto/modes +#cgo CFLAGS: -I${SRCDIR}/../openssl_config +#cgo CFLAGS: -I${SRCDIR}/../openssl +#cgo CFLAGS: -I${SRCDIR}/../openssl/include +#cgo CFLAGS: -I${SRCDIR}/../openssl/crypto/include +#cgo CFLAGS: -I${SRCDIR}/../openssl/crypto/ec/curve448 +#cgo CFLAGS: -I${SRCDIR}/../openssl/crypto/ec/curve448/arch_32 +#cgo CFLAGS: -I${SRCDIR}/../openssl/crypto/modes */ import "C" ` @@ -517,7 +523,7 @@ package libtor #define OPENSSLDIR "/usr/local/ssl" #define ENGINESDIR "/usr/local/lib/engines" -#include <%s.c> +#include <../%s.c> */ import "C" ` @@ -637,19 +643,19 @@ func wrapTor(nobuild bool) (string, string, error) { if strings.HasSuffix(dep[1], "-c64") { for _, arch := range []string{"amd64", "arm64"} { gofile := strings.Replace(dep[1], "/", "_", -1) + "_" + arch + ".go" - ioutil.WriteFile("tor_"+gofile, []byte(fmt.Sprintf(torTemplate, dep[1])), 0644) + ioutil.WriteFile(filepath.Join("libtor", "tor_"+gofile), []byte(fmt.Sprintf(torTemplate, dep[1])), 0644) } for _, arch := range []string{"386", "arm"} { gofile := strings.Replace(dep[1], "/", "_", -1) + "_" + arch + ".go" - ioutil.WriteFile("tor_"+gofile, []byte(fmt.Sprintf(torTemplate, strings.Replace(dep[1], "-c64", "", -1))), 0644) + ioutil.WriteFile(filepath.Join("libtor", "tor_"+gofile), []byte(fmt.Sprintf(torTemplate, strings.Replace(dep[1], "-c64", "", -1))), 0644) } continue } // Anything else gets wrapped directly gofile := strings.Replace(dep[1], "/", "_", -1) + ".go" - ioutil.WriteFile("tor_"+gofile, []byte(fmt.Sprintf(torTemplate, dep[1])), 0644) + ioutil.WriteFile(filepath.Join("libtor", "tor_"+gofile), []byte(fmt.Sprintf(torTemplate, dep[1])), 0644) } - ioutil.WriteFile("tor_preamble.go", []byte(torPreamble), 0644) + ioutil.WriteFile(filepath.Join("libtor", "tor_preamble.go"), []byte(torPreamble), 0644) // Inject the configuration headers and ensure everything builds os.MkdirAll("tor_config", 0755) @@ -686,13 +692,13 @@ var torPreamble = `// go-libtor - Self-contained Tor from Go package libtor /* -#cgo CFLAGS: -I${SRCDIR}/tor_config -#cgo CFLAGS: -I${SRCDIR}/tor -#cgo CFLAGS: -I${SRCDIR}/tor/src -#cgo CFLAGS: -I${SRCDIR}/tor/src/core/or -#cgo CFLAGS: -I${SRCDIR}/tor/src/ext -#cgo CFLAGS: -I${SRCDIR}/tor/src/ext/trunnel -#cgo CFLAGS: -I${SRCDIR}/tor/src/feature/api +#cgo CFLAGS: -I${SRCDIR}/../tor_config +#cgo CFLAGS: -I${SRCDIR}/../tor +#cgo CFLAGS: -I${SRCDIR}/../tor/src +#cgo CFLAGS: -I${SRCDIR}/../tor/src/core/or +#cgo CFLAGS: -I${SRCDIR}/../tor/src/ext +#cgo CFLAGS: -I${SRCDIR}/../tor/src/ext/trunnel +#cgo CFLAGS: -I${SRCDIR}/../tor/src/feature/api #cgo CFLAGS: -DED25519_CUSTOMRANDOM -DED25519_CUSTOMHASH -DED25519_SUFFIX=_donna @@ -710,7 +716,7 @@ package libtor /* #define BUILDDIR "" -#include <%s.c> +#include <../%s.c> */ import "C" `