2017-09-29 23:36:37 +02:00
|
|
|
// Copyright (c) 2017 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package rpctest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// compileMtx guards access to the executable path so that the project is
|
|
|
|
// only compiled once.
|
|
|
|
compileMtx sync.Mutex
|
|
|
|
|
|
|
|
// executablePath is the path to the compiled executable. This is the empty
|
|
|
|
// string until btcd is compiled. This should not be accessed directly;
|
|
|
|
// instead use the function btcdExecutablePath().
|
|
|
|
executablePath string
|
|
|
|
)
|
|
|
|
|
|
|
|
// btcdExecutablePath returns a path to the btcd executable to be used by
|
|
|
|
// rpctests. To ensure the code tests against the most up-to-date version of
|
|
|
|
// btcd, this method compiles btcd the first time it is called. After that, the
|
|
|
|
// generated binary is used for subsequent test harnesses. The executable file
|
|
|
|
// is not cleaned up, but since it lives at a static path in a temp directory,
|
|
|
|
// it is not a big deal.
|
|
|
|
func btcdExecutablePath() (string, error) {
|
|
|
|
compileMtx.Lock()
|
|
|
|
defer compileMtx.Unlock()
|
|
|
|
|
|
|
|
// If btcd has already been compiled, just use that.
|
|
|
|
if len(executablePath) != 0 {
|
|
|
|
return executablePath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
testDir, err := baseDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build btcd and output an executable in a static temp path.
|
2021-10-15 07:45:32 +02:00
|
|
|
outputPath := filepath.Join(testDir, "lbcd")
|
2017-09-29 23:36:37 +02:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
outputPath += ".exe"
|
|
|
|
}
|
2018-11-29 15:13:37 +01:00
|
|
|
cmd := exec.Command(
|
2021-10-15 07:45:32 +02:00
|
|
|
"go", "build", "-o", outputPath, "github.com/lbryio/lbcd",
|
2018-11-29 15:13:37 +01:00
|
|
|
)
|
2017-09-29 23:36:37 +02:00
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
2021-10-15 07:45:32 +02:00
|
|
|
return "", fmt.Errorf("Failed to build lbcd: %v", err)
|
2017-09-29 23:36:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save executable path so future calls do not recompile.
|
|
|
|
executablePath = outputPath
|
|
|
|
return executablePath, nil
|
|
|
|
}
|