Calling Go library from C Code - go

I've found a ton of information about how to use cgo to call C libraries from Go, but I'm interested in the opposite: writing a library in Go, and linking/using it in various C programs.
Is this possible? Any good resources for this? Thanks.

This can not be done currently. Go has to be the entry point and without the use of gccgo, you can't compile Go into a shared library.
There is a proposal to change this, so it may or may not be an option at some point. Refer to this document for details.

Related

How to use Go -buildmode=archive

Using -buildmode=archive produces mylib.a. I'm not fully understanding the steps required to then use this library in another Go program.
I've tried instead generating -buildmode=c-archive which produces a header file and archive, but the headerfile is not designed to be imported using cgo (there is conflicts with imported types).
Research online has yielded the conclusion that -buildmode=c-archive is specifically not designed for cgo use for this reason.
My query is what is -buildmode=archive actually used for if it cannot be included?
I'm not fully understanding the steps required to then use this library in another Go program.
It can be tricky. Need to look into each compiler toolchain for linking them correctly, i.e. gcc/clang etc. and find how, and if it's even possible to link them and use them.
What is -buildmode=archive actually used for if it cannot be included?
From the docs: https://pkg.go.dev/cmd/go
-buildmode=archive
Build the listed non-main packages into .a files. Packages named
main are ignored.
That's what it does, what you do with it, it's up to you, there's tons of information online, for example: https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

Is there a way to call c code from a substrate project?

It's trivial to call c from rust normally, is there a way to do so from a substrate project? I can't find anything online saying it's possible or it's not possible.
I have a c library I'd like to use as part of a substrate project and I was wondering if it would be possible to use it without rewriting it.
Many FFI libraries are no_std compliant, this is the only hard requirement for being used in substrate runtimes. Checkout the list here and look for that tag for options to move forward:
https://lib.rs/development-tools/ffi
If you are using this library outside the runtime, you should be able to use any rust library.

Can VPP plugins be implemented using Go?

VPP provides the I/S for developing custom plugins that can be hooked into a graph of nodes. I've only seen examples for such plugins written in the C language, and was wondering whether other language, Go for instance, can also be used to write such plugins.
I have no idea what "VPP" is but nonetheless the answer is: "maybe"; here's why:
Go code is able to interface with C libraries via its facility known as cgo.
cgo is a multiple-faceted thing: it allows you to "export" certain Go functions in a certain way so that they can be called from the C side, and it allows you to call functions from the C side. It also allows you to write bits of inline C code to provide glue for the C side, when necessary.
Since some time Go building toolset (at least its "reference" implementation) provides for compiling Go code into a static or dynamic library with C-compatible API.
See this.
With these things in mind, in theory, it should be possible to do what you're after.
Note some possible obstacles:
Most of the time, if a "platform" allows you to write a "plugin" in C, it presupposes your plugin will make extensive use of the platform's own API.
This usually means your plugin is supposed to include certain header files provided by the platform.
The platform might also require your plugin to link against some platform-provided library (usually shared), or libraries.
cgo can do all of the above, but you will need to scrutinize the API provided by the platform and maybe write Go helpers to make its usage more natural for the Go code.
Building/linking issues (usually the locations of the header files and the libs) may also be a thing to solve.

Go target language

What is the Go language compiled to? Nobody seems to want to write it anywhere on the net. I am searching for the target language. I am thinking it's probably assembly, C, or relocatable machine code?
The reference implementation compiles Go to native machine code. The code is generated to be not relocateable.
The language has been designed to allow other target platforms as well. For instance, there are implementations that compile Go code into Javascript and PHP.
It is not possible to use Go code in a shared library.

Modification of the AST-tree of the GCC compiler

It is needed to gather the necessary information about the translation unit using the plugin for GCC and to modify AST on its base.
I've already understood how to gather information. But I haven't understand yet how to modify AST before it's passed into CRT. Very little information is available on this subject.
Tell me plese what should I read on this subject? Share thoughts, links.
Thank's.
P.S.
I've already read everything on these links:
http://en.wikibooks.org/wiki/GNU_C_Compiler_Internals/Print_version
http://developer.apple.com/library/mac/#documentation/DeveloperTools/gcc-4.2.1/gccint/index.html#Top
The GCC test suite contains a basic examples of such modifications. See http://gcc.gnu.org/viewcvs/gcc/trunk/gcc/testsuite/gcc.dg/plugin/finish_unit_plugin.c and start_unit_plugin.c shows how to create a var. Unfortunately for more serious modifications the GCC source code are probably your best bet.
Are you tied to GCC for this endeavor? The ROSE compiler is built specifically for performing source-level modification, then handing the resulting code off to a backend compiler.

Resources