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

Comments

@woehr
Copy link

@woehr 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
Contributor

@cherrymui cherrymui 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

@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented Mar 25, 2021

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

@woehr
Copy link
Author

@woehr 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

@ianlancetaylor ianlancetaylor commented Mar 25, 2021

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

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 26, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants