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

Generate "import C" for cgo #26

Closed
pekim opened this Issue Jan 9, 2018 · 10 comments

Comments

Projects
None yet
2 participants
@pekim

pekim commented Jan 9, 2018

I'm generating a source file that uses cgo. This means that following some comment lines, I need a import "C" line.

// #include <stdio.h>
// #include <errno.h>
import "C"

The import must immediately follow the preamble comments. That's a cgo requirement. And I cannot find a way to do that with the current api.

As this is a somewhat special case, I think that it possibly merits a new api function.

So perhaps something like this?

  f.Comment("#include <stdio.h>")
  f.Comment("#include <errno.h>")
  f.ImportC()    // <--- this would be an addition to the api.

Or is there already support for this, and I'm overlooking it?

@dave

This comment has been minimized.

Show comment
Hide comment
@dave

dave Jan 9, 2018

Owner

No, you're right - there's no support for this, but definitely something I'll add. I'll have to have a read up on the syntax to make a call on how to make it happen...

Owner

dave commented Jan 9, 2018

No, you're right - there's no support for this, but definitely something I'll add. I'll have to have a read up on the syntax to make a call on how to make it happen...

@dave

This comment has been minimized.

Show comment
Hide comment
@dave

dave Jan 9, 2018

Owner

How about:

  • "C" import is added by simply using Qual("C", "foo") in your code, as is the case for all other imports.
  • It's treated as a special case when the page renders
  • The Qual method always renders the upper-case "C" (as is convention).
  • Imports are usually aliased - with "C" the alias will be suppressed (not sure if this is a requirement?).
  • The import is separated below the other imports.
  • A new method CommentC() is added with the same functionality as "Comment()". The comment (or comments) are rendered directly above the import... So you'd get something like:
import (
    fmt "fmt"
    baz "foo.bar/baz"
)

// foo
/*
bar
baz
*/
import "C"

func main() {
    fmt.Println(C.create_object(), baz.Quz)
}
Owner

dave commented Jan 9, 2018

How about:

  • "C" import is added by simply using Qual("C", "foo") in your code, as is the case for all other imports.
  • It's treated as a special case when the page renders
  • The Qual method always renders the upper-case "C" (as is convention).
  • Imports are usually aliased - with "C" the alias will be suppressed (not sure if this is a requirement?).
  • The import is separated below the other imports.
  • A new method CommentC() is added with the same functionality as "Comment()". The comment (or comments) are rendered directly above the import... So you'd get something like:
import (
    fmt "fmt"
    baz "foo.bar/baz"
)

// foo
/*
bar
baz
*/
import "C"

func main() {
    fmt.Println(C.create_object(), baz.Quz)
}
@pekim

This comment has been minimized.

Show comment
Hide comment
@pekim

pekim Jan 9, 2018

That all sounds good to me.

Imports are usually aliased - with "C" the alias will be suppressed (not sure if this is a requirement?).

I'm not sure either, but I've never seen an example of cgo usage with the import aliased. Suppressing the alias sounds like the right thing to do.

pekim commented Jan 9, 2018

That all sounds good to me.

Imports are usually aliased - with "C" the alias will be suppressed (not sure if this is a requirement?).

I'm not sure either, but I've never seen an example of cgo usage with the import aliased. Suppressing the alias sounds like the right thing to do.

@dave

This comment has been minimized.

Show comment
Hide comment
@dave

dave Jan 9, 2018

Owner

Yes the only reason we always alias the imports is because we don't actually know the package name, so dropping the alias on "C" is perfectly fine. I should have some time tomorrow to implement this.

Owner

dave commented Jan 9, 2018

Yes the only reason we always alias the imports is because we don't actually know the package name, so dropping the alias on "C" is perfectly fine. I should have some time tomorrow to implement this.

@dave

This comment has been minimized.

Show comment
Hide comment
@dave

dave Jan 10, 2018

Owner

Hey - a question: Are there times where you might want to import the "C" pseudo-package, but not reference it in code?

e.g.:

package foo

import "C"

func main() {
    // never use C
}

In this case would you use the import _ "C" syntax?

Owner

dave commented Jan 10, 2018

Hey - a question: Are there times where you might want to import the "C" pseudo-package, but not reference it in code?

e.g.:

package foo

import "C"

func main() {
    // never use C
}

In this case would you use the import _ "C" syntax?

@pekim

This comment has been minimized.

Show comment
Hide comment
@pekim

pekim Jan 10, 2018

If there were to be such a scenario then it would likely be to control a compiler or linker flag, and the import would be preceded by some comment lines.

// #cgo LDFLAGS: -lpng
import "C"

I don't think that I'm likely to need this in my current use cases. But I can't rule it out.

I'm not sure whether import _ "C" would be recognised by the compiler for cgo purposes. I would need to try it with a simple use case. Unfortunately I won't be in a position to do that for about 10 or 11 hours.

pekim commented Jan 10, 2018

If there were to be such a scenario then it would likely be to control a compiler or linker flag, and the import would be preceded by some comment lines.

// #cgo LDFLAGS: -lpng
import "C"

I don't think that I'm likely to need this in my current use cases. But I can't rule it out.

I'm not sure whether import _ "C" would be recognised by the compiler for cgo purposes. I would need to try it with a simple use case. Unfortunately I won't be in a position to do that for about 10 or 11 hours.

@dave

This comment has been minimized.

Show comment
Hide comment
@dave

dave Jan 10, 2018

Owner

Yes it looks like "C" is indeed a special case, and can be imported without a "_" even without being used...

screen shot 2018-01-10 at 10 52 52

Owner

dave commented Jan 10, 2018

Yes it looks like "C" is indeed a special case, and can be imported without a "_" even without being used...

screen shot 2018-01-10 at 10 52 52

@dave

This comment has been minimized.

Show comment
Hide comment
@dave

dave Jan 10, 2018

Owner

I'm happy with this... Let me know if you think it needs any tweaks.

Owner

dave commented Jan 10, 2018

I'm happy with this... Let me know if you think it needs any tweaks.

@dave

This comment has been minimized.

Show comment
Hide comment
@dave

dave Jan 10, 2018

Owner

BTW @pekim thanks very much for the excellent suggestion, and feel free to open issues if you have any more questions. I'm really glad you're enjoying my work!

Owner

dave commented Jan 10, 2018

BTW @pekim thanks very much for the excellent suggestion, and feel free to open issues if you have any more questions. I'm really glad you're enjoying my work!

@pekim

This comment has been minimized.

Show comment
Hide comment
@pekim

pekim Jan 10, 2018

I've just tried it out, and it works nicely. Thank you.

I appreciate that you tag releases, including v0.17.0 with these changes.

pekim commented Jan 10, 2018

I've just tried it out, and it works nicely. Thank you.

I appreciate that you tag releases, including v0.17.0 with these changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment