Skip to content

Commit

Permalink
build: nest generated files to clean up repo root
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Mar 10, 2019
1 parent 36b77c4 commit 8f16aa1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 37 deletions.
24 changes: 24 additions & 0 deletions build/libtor_external.go.in
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
80 changes: 43 additions & 37 deletions build/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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")))
Expand Down Expand Up @@ -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 {
Expand All @@ -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"
`
Expand All @@ -171,7 +177,7 @@ var zlibTemplate = `// go-libtor - Self-contained Tor from Go
package libtor
/*
#include <zlib/%s.c>
#include <../zlib/%s.c>
*/
import "C"
`
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
`
Expand All @@ -318,7 +324,7 @@ package libtor
/*
#include <compat/sys/queue.h>
#include <%s.c>
#include <../%s.c>
*/
import "C"
`
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
`
Expand All @@ -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"
`
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -710,7 +716,7 @@ package libtor
/*
#define BUILDDIR ""
#include <%s.c>
#include <../%s.c>
*/
import "C"
`

0 comments on commit 8f16aa1

Please sign in to comment.