Skip to content

util: conditionally build CGO functions #190

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

Merged
merged 1 commit into from
Aug 15, 2016

Conversation

heyitsanthony
Copy link
Contributor

Some projects don't need the CGO functions in the util package;
split out the functions into a separate file so CGO_ENABLED=0
works if those functions are never used.

Fixes #171

@lucab
Copy link
Contributor

lucab commented Aug 12, 2016

This sounds good to me.

I'm just wondering if the non-cgo stub should still implement the functions that are being moved out here, just by erroring out. This would keep the same public interface, regardless of tags.

I'm not sure what is the idiomatic Go way here, @jonboulle?

@gyuho
Copy link
Contributor

gyuho commented Aug 15, 2016

@lucab @jonboulle Can we get this merged?

go-systemd build in etcd is broken.

And I believe this is idiomatic, based on Go source tree.

$ find . -name '*_cgo_*'

./src/crypto/x509/root_cgo_darwin.go
./src/runtime/crash_cgo_test.go

And more

$ find . -name '*cgo_*'

./src/net/cgo_unix_test.go
./src/net/cgo_bsd.go
...

Thanks!

// the current process is running.
// This function is a wrapper around the libsystemd C library; if it cannot be
// opened, an error is returned.
func GetRunningSlice() (slice string, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function declaration and docstring should probably be retained, otherwise this part will be missing from godoc and we would get different public symbols depending on build tags. It can be simply implemented as a stub that always errors out. Same for the other functions moved out from this file.

@lucab
Copy link
Contributor

lucab commented Aug 15, 2016

I had another look at this, and I think that the current approach may be non-optimal: some public symbols will depend on build tags and will disappear from godoc (I guess, not sure).

Thus, I'd suggest a different way this could be done, by splitting current util.go into three parts:

  • util.go without any tag, only with the pure-Go functions (GetMachineID and IsRunningSystemd)
  • util_cgo.go gated behind a cgo tag, as you already did
  • util_stub.go gated behind a not-cgo tag, with stubs and docstring to match the cgo counterparts

@heyitsanthony
Copy link
Contributor Author

@lucab all fixed up; I made the public functions call into private build-specific functions. It seemed weird to have the full godoc in _stub.go when the code there does nothing. Thanks!

"fmt"
)

var errNoCGO = fmt.Errorf("go-systemd built with CGO disabled")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved to util.go in order to let consumers error-match on this in all situations.

@lucab
Copy link
Contributor

lucab commented Aug 15, 2016

I made the public functions call into private build-specific functions. It seemed weird to have the full godoc in _stub.go when the code there does nothing.

I agree this is even better than my half-botched proposal, thanks! Just one final remark inline, other than that it should be ok.

Some projects don't need the CGO functions in the util package;
split out the functions into a separate file so CGO_ENABLED=0
works if those functions are never used.

Fixes coreos#171
@heyitsanthony
Copy link
Contributor Author

@lucab ok, ErrNoCGO exported

@lucab
Copy link
Contributor

lucab commented Aug 15, 2016

Great, LGTM!

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

Successfully merging this pull request may close these issues.

3 participants