Skip to content

Commit 6c6dc2b

Browse files
committed
rewrite ldflags process
1 parent 5247c5f commit 6c6dc2b

File tree

1 file changed

+16
-51
lines changed

1 file changed

+16
-51
lines changed

cmd/internal/rungo/run.go

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -139,33 +139,23 @@ func ProcessArgsWithLDFlags(args []string, projectRoot, pythonPath, pythonHome s
139139
result := make([]string, 0, len(args))
140140

141141
// Prepare the -X flags we want to add
142-
xFlags := fmt.Sprintf("-X 'github.com/cpunion/go-python.ProjectRoot=%s'", projectRoot)
142+
ldflags := fmt.Sprintf("-X 'github.com/cpunion/go-python.ProjectRoot=%s'", projectRoot)
143143

144144
// Prepare rpath flag if needed
145-
var rpathFlag string
146-
if pythonHome != "" {
147-
pythonLibDir := filepath.Join(pythonHome, "lib")
148-
switch runtime.GOOS {
149-
case "darwin", "linux":
150-
rpathFlag = fmt.Sprintf("-extldflags '-Wl,-rpath,%s'", pythonLibDir)
151-
case "windows":
152-
// Windows doesn't use rpath
153-
rpathFlag = ""
154-
default:
155-
// Use Linux format for other Unix-like systems
156-
rpathFlag = fmt.Sprintf("-extldflags '-Wl,-rpath=%s'", pythonLibDir)
157-
}
145+
pythonLibDir := env.GetPythonLibDir(projectRoot)
146+
switch runtime.GOOS {
147+
case "darwin", "linux":
148+
ldflags += fmt.Sprintf(" -extldflags '-Wl,-rpath,%s'", pythonLibDir)
149+
case "windows":
150+
// Windows doesn't use rpath
151+
default:
152+
// Use Linux format for other Unix-like systems
153+
ldflags += fmt.Sprintf(" -extldflags '-Wl,-rpath=%s'", pythonLibDir)
158154
}
159155

160-
// Find existing -ldflags if any
161-
foundLDFlags := false
162156
for i := 0; i < len(args); i++ {
163157
arg := args[i]
164158
if strings.HasPrefix(arg, "-ldflags=") || arg == "-ldflags" {
165-
foundLDFlags = true
166-
// Copy everything before this arg
167-
result = append(result, args[:i]...)
168-
169159
// Get existing flags
170160
var existingFlags string
171161
if strings.HasPrefix(arg, "-ldflags=") {
@@ -174,40 +164,15 @@ func ProcessArgsWithLDFlags(args []string, projectRoot, pythonPath, pythonHome s
174164
existingFlags = args[i+1]
175165
i++ // Skip the next arg since we've consumed it
176166
}
177-
178-
// Combine all flags
179-
allFlags := []string{xFlags}
180-
if strings.TrimSpace(existingFlags) != "" {
181-
allFlags = append(allFlags, existingFlags)
182-
}
183-
if rpathFlag != "" {
184-
allFlags = append(allFlags, rpathFlag)
185-
}
186-
187-
// Add combined ldflags
188-
result = append(result, "-ldflags")
189-
result = append(result, strings.Join(allFlags, " "))
190-
191-
// Add remaining args
192-
result = append(result, args[i+1:]...)
193-
break
194-
}
195-
}
196-
197-
// If no existing -ldflags found, add new ones at the beginning if we have any flags to add
198-
if !foundLDFlags {
199-
if len(xFlags) > 0 || rpathFlag != "" {
200-
allFlags := []string{xFlags}
201-
if rpathFlag != "" {
202-
allFlags = append(allFlags, rpathFlag)
167+
existingFlags = strings.TrimSpace(existingFlags)
168+
if ldflags != "" {
169+
ldflags += " " + existingFlags
203170
}
204-
result = append(result, "-ldflags")
205-
result = append(result, strings.Join(allFlags, " "))
171+
} else {
172+
result = append(result, arg)
206173
}
207-
result = append(result, args...)
208174
}
209-
210-
return result
175+
return append([]string{"-ldflags", ldflags}, result...)
211176
}
212177

213178
// GetGoCommandHelp returns the formatted help text for the specified go command

0 commit comments

Comments
 (0)