-
-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add function to expose the NativeLibPath #376
Add function to expose the NativeLibPath #376
Conversation
installer/installer.go
Outdated
@@ -135,6 +135,13 @@ func (i *Installer) getLibDir() string { | |||
return env | |||
} | |||
|
|||
if runtime.GOOS == "windows" { | |||
if out, err := exec.Command("powershell", "-Command", "(Get-Command gcc).Source").Output(); err == nil && len(out) > 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this error need handling?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure. I guess the only sane thing I could do in this case is log the error. What do you think? Is it worth logging? The reason might simpoly be that gcc
is not found on the system.
Thanks for the PR! Anything to improve the developer experience is welcome. I think it could also just be dumped into the "current directory" in windows (I can't recall the exact DLL loading semantics, but I recall it looking up the tree from where you are). |
What do you mean with "current directory"? The dll loading semantics depend on where your gcc is installed on windows AFIK. |
The search order for DLLs in unpackaged apps is defined here: https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order#search-order-for-unpackaged-apps See point (11) here. |
@mefellows I need to experiment a bit more before merge. I still don't fully understand what is going on on Windows. It only works for me on my machine, when I have the Don't really know why this is the case yet. If I only put it in |
It could be because we don't set any additional flags on the windows CGO stanza here: https://github.com/pact-foundation/pact-go/blob/master/internal/native/lib.go#L7. You could try experimenting with that, and if there are universal paths that are both easily written to during install and static they could go there. But probably, it's just the way LD resolves DLLs on Windows. If you know where the common search paths are, that should resolve it. Note the |
@mefellows you are absolutely right. If one can avoid it, it is best to not use Windows at all. Especially not with go and cgo including gcc. It is a total mess. I kind of understood now what is going on. The gcc compiler that I have on my system seems to expect lib files in When I change the linker to |
@mefellows I finally found something more or less satisfying which works on Windows. I ended up putting the dll next to the In the end I have this small go binary in our codebase which can be executed via package main
import (
"fmt"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/pact-foundation/pact-go/v2/installer"
)
//go:generate go run ./...
func main() {
fmt.Printf("Download and install %s if not yet installed\n", installer.FFIPackage)
if runtime.GOOS != "windows" {
panic("Only works on windows right now")
}
fmt.Println("Try to detect gcc environment")
gccDir := ""
if out, err := exec.Command("powershell", "-Command", "(Get-Command gcc).Source").Output(); err == nil && len(out) > 0 {
gccDir = filepath.Dir(strings.TrimSpace(string(out)))
} else {
panic("Failed to get GCC path. Make sure that gcc is installed on your system.")
}
fmt.Println("Get path to pact lib")
assertInstalled(gccDir)
assertInstalled(installer.NativeLibPath())
}
func assertInstalled(dir string) {
fmt.Printf("Assert that dll is installed at %s\n", dir)
i, err := installer.NewInstaller()
if err != nil {
panic(err)
}
i.SetLibDir(dir)
if err := i.CheckInstallation(); err != nil {
panic(err)
}
} In this PR I would like to add |
Absolutely, I'll merge that in. Would you mind please rebasing your commits with just the single We could potentially look up update the |
f6eca29
to
6cdebf8
Compare
feat: add native lib path function
6cdebf8
to
47fcff6
Compare
@mefellows I squashed my changes to a single By using For example when I installed the pact-go binary it ended up in another directory and I would need to make sure that the binary and library versions are somehow synced to end up with the same lib paths. |
Thanks! |
Not sure if you want this in, but I was using pact on Windows and there the default path "user/local/lib" is most likely never right. I am using TDM GCC which is installed at
C:\TDM-GCC-64\bin
.With the powershell
Get-Command
it is possible to detect the install path and put the library in there directly which is a better default on Windows I guess since otherwsie one would set this path manually.