Giter Site home page Giter Site logo

docker container for c2go about c2go HOT 8 OPEN

tijeco avatar tijeco commented on June 2, 2024
docker container for c2go

from c2go.

Comments (8)

drpaneas avatar drpaneas commented on June 2, 2024 5

At the moment the project seems to not be working with recent versions of clang. Even a simple helloworld.c fails to transpile:

Source C

$ cat main.c
#include <stdio.h>
int main() {
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}

Run the command to convert it into Go code:*

c2go transpile main.c

Error ...

panic: unknown node type: 'ColdAttr 0x7f9fe58af708 <col:42>'

goroutine 76 [running]:
github.com/elliotchance/c2go/ast.Parse({0xc00032893a, 0x20})
	/Users/drpaneas/gocode/pkg/mod/github.com/elliotchance/[email protected]/ast/ast.go:294 +0x22d4
main.convertLinesToNodes({0xc000333d40, 0xe, 0x0})
	/Users/drpaneas/gocode/pkg/mod/github.com/elliotchance/[email protected]/main.go:89 +0x115
main.convertLinesToNodesParallel.func1.2({0xc000333d40, 0x0, 0x0}, 0x0)
	/Users/drpaneas/gocode/pkg/mod/github.com/elliotchance/[email protected]/main.go:121 +0x39
created by main.convertLinesToNodesParallel.func1
	/Users/drpaneas/gocode/pkg/mod/github.com/elliotchance/[email protected]/main.go:119 +0x2da

After a couple hours of debugging I found out that the most recent supported version of clang seems to be:

11.0.0 (https://github.com/llvm/llvm-project.git 0160ad802e899c2922bc9b29564080c22eb0908c)

Since I don't want to mess up my system with different clang versions, I took a docker image that comes with clang 11 (see https://hub.docker.com/r/silkeh/clang/tags) and then I've installed Go and c2go on top. In that way it works in any machine, without worrying about the local system dependencies (well, you need docker obviously).

The dockerfile:

FROM docker.io/silkeh/clang:11

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get -o Dpkg::Options::="--force-confold" upgrade -q -y --force-yes && \
    apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -q -y --force-yes \
    && apt-get install golang-go -q -y --force-yes \
    && mkdir -p ~/go/{bin,src,pkg} \
    && mkdir /code && mkdir /gocode

ENV GOPATH="$HOME/go"
ENV GOBIN="$GOPATH/bin"
ENV GO111MODULE="on"
ENV PATH=$PATH:$GOBIN

RUN go get -u github.com/elliotchance/c2go \
    && echo "#!/usr/bin/env bash" > /gocode/script.sh \
    && echo "\$GOBIN/c2go transpile /code/\$1" >> /gocode/script.sh \
    && echo "NAME=\$(basename \$1 .c)"  >> /gocode/script.sh \
    && echo "cat /\$NAME.go"  >> /gocode/script.sh \
    && chmod +x /gocode/script.sh

ENTRYPOINT [ "/gocode/script.sh" ]

You can run it like this:

 $ docker run --rm -it -v $PWD:/code docker.io/drpaneas/c2go:v0.26.0 main.c

The output now works fine, e.g. see the main() function of the helloworld example (the one that failed earlier):

// removed C types for brevity (it produces a long file)
func main() {
	noarch.Printf((&[]byte("Hello, World!\x00")[0]))
	return
}
func init() {
	stdin = noarch.Stdin
	stdout = noarch.Stdout
	stderr = noarch.Stderr
}

For shortcut (so you don't have to remember the long train-like docker command) you can put a function in our bash/zshell like this (e.g. vim ~/.zshenv or vim ~/.bashrc):

c2go() {
  docker run --rm -it -v $PWD:/code docker.io/drpaneas/c2go:v0.26.0 $1
}

then source your file (e.g. source ~/.zshenv or source ~/.bashrc) and try again passing only the filename you want to transpile: c2go main.c or ``c2go main.c > main.go` (if you want to put it into a file)

Now it works like a charm :)

from c2go.

tijeco avatar tijeco commented on June 2, 2024 2

@drpaneas you are an absolute hero! I may finally be able to play around with this tool!

from c2go.

drpaneas avatar drpaneas commented on June 2, 2024 1

The answer is the Dockerfile which I have posted earlier. You can just copy paste it and build it locally. I've literally described the whole process in my previous step.

from c2go.

elliotchance avatar elliotchance commented on June 2, 2024

Not that I know of, but it’s a good idea.

from c2go.

tijeco avatar tijeco commented on June 2, 2024

@elliotchance Gotcha! I can take a stab at it, though I'm far from a docker expert. Are there particular versions of clang that you use on your system that you know for sure work with the test examples?

from c2go.

elliotchance avatar elliotchance commented on June 2, 2024

It been so long since I've worked on c2go that I don't remember which versions I was using.

from c2go.

marvjone avatar marvjone commented on June 2, 2024

@drpaneas I'm kinda new to docker. My question is how did you get go and then have c2go install in your container ? any help on this. I really would like to get c2go to work.

from c2go.

marvjone avatar marvjone commented on June 2, 2024

@drpaneas thanks for the reply. I did the process you described and got the following error. like I said I'm new to docker. but will love to learn:

=> ERROR [2/3] RUN apt-get update && apt-get -o Dpkg::Options::="--force-confold" upgrade -q -y --force-yes && apt-get -o Dpkg::Options::="- 3.7s


[2/3] RUN apt-get update && apt-get -o Dpkg::Options::="--force-confold" upgrade -q -y --force-yes && apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -q -y --force-yes && apt-get install golang-go -q -y --force-yes && mkdir -p /go/{bin,src,pkg} && mkdir /code && mkdir /gocode:
#5 0.342 Hit:1 http://deb.debian.org/debian bullseye InRelease
#5 0.342 Get:2 http://security.debian.org/debian-security bullseye-security InRelease [44.1 kB]
#5 0.365 Hit:3 http://deb.debian.org/debian bullseye-updates InRelease
#5 0.654 Fetched 44.1 kB in 0s (103 kB/s)
#5 0.654 Reading package lists...
#5 1.152 Reading package lists...
#5 1.629 Building dependency tree...
#5 1.772 Reading state information...
#5 1.798 Calculating upgrade...
#5 1.984 W: --force-yes is deprecated, use one of the options starting with --allow instead.
#5 1.984 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
#5 1.997 Reading package lists...
#5 2.473 Building dependency tree...
#5 2.614 Reading state information...
#5 2.643 Calculating upgrade...
#5 2.872 W: --force-yes is deprecated, use one of the options starting with --allow instead.
#5 2.873 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
#5 2.885 Reading package lists...
#5 3.365 Building dependency tree...
#5 3.504 Reading state information...
#5 3.693 W: --force-yes is deprecated, use one of the options starting with --allow instead.
#5 3.693 golang-go is already the newest version (2:1.15
1).
#5 3.693 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
#5 3.698 mkdir: cannot create directory '/code': File exists


executor failed running [/bin/sh -c apt-get update && apt-get -o Dpkg::Options::="--force-confold" upgrade -q -y --force-yes && apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -q -y --force-yes && apt-get install golang-go -q -y --force-yes && mkdir -p ~/go/{bin,src,pkg} && mkdir /code && mkdir /gocode]: exit code: 1

from c2go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.