Skip to content
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

cmd/link: Linking with a non-clang linker fails when the path contains the string "clang" #45241

Closed
woehr opened this issue Mar 25, 2021 · 5 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.

Comments

@woehr
Copy link

woehr commented Mar 25, 2021

What version of Go are you using (go version)?

1.16

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

The following code in

if strings.Contains(argv[0], "clang") {
causes linking with a non-clang linker to fail when using a toolchain that exists in a path with the string "clang". The failure occurs because not all compilers (g++ in my case) support the flag "-Qunused-arguments".

Fix: If you want to know whether clang is being used, search the filename, not the entire path.

	if strings.Contains(argv[0], "clang") {
		argv = append(argv, "-Qunused-arguments")
	}

What did you do?

Build a project with a toolchain under a path with the string "clang". For example, /home/clanger/compiler/bin/g++.

What did you expect to see?

Linking should succeed.

What did you see instead?

Linking failed.

@seankhliao seankhliao changed the title Linking with a non-clang linker fails when the path contains the string "clang" cmd/link: Linking with a non-clang linker fails when the path contains the string "clang" Mar 25, 2021
@cherrymui
Copy link
Member

Fix: If you want to know whether clang is being used, search the filename, not the entire path.

  if strings.Contains(argv[0], "clang") {
  	argv = append(argv, "-Qunused-arguments")
  }

I think I understand your problem, but I'm not sure I followed what you meant here with the "fix'.

I guess we could check if the C linker is clang by running $CC -v or something.
cc @thanm

@seankhliao
Copy link
Member

maybe it should have been strings.Contains(filepath.Base(argv[0]), "clang") ?

@woehr
Copy link
Author

woehr commented Mar 25, 2021

Fix: If you want to know whether clang is being used, search the filename, not the entire path.

  if strings.Contains(argv[0], "clang") {
  	argv = append(argv, "-Qunused-arguments")
  }

I think I understand your problem, but I'm not sure I followed what you meant here with the "fix'.

I guess we could check if the C linker is clang by running $CC -v or something.
cc @thanm

Something like:

	path := "/some/path/with/clang/in/it"
	file := filepath.Base(path)
	if strings.Contains(file, "clang") {
		fmt.Println("is clang")
	} else {
		fmt.Println("not clang")
	}

@ianlancetaylor
Copy link
Contributor

What if we just use linkerFlagSupported rather than trying to guess whether the linker is clang?

@gopherbot
Copy link

Change https://golang.org/cl/304692 mentions this issue: cmd/link/internal/id: use linkerFlagSupported to check -Qunused-arguments

@seankhliao seankhliao added the NeedsFix The path to resolution is known, but the work has not been done. label Mar 26, 2021
@golang golang locked and limited conversation to collaborators Mar 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants