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

add cfiles #2581

Closed
wants to merge 1 commit into from
Closed

add cfiles #2581

wants to merge 1 commit into from

Conversation

kostya
Copy link
Contributor

@kostya kostya commented May 11, 2016

for fun:

1.cr

@[CFile("1.c")]
lib Bla
  fun bla(x : Int32) : Int32
end

puts Bla.bla(1)

1.c

int bla(int x) { return x + 11; }
./bin/crystal 1.cr
Using compiled compiler at .build/crystal
12

@refi64
Copy link
Contributor

refi64 commented May 12, 2016

This is sort of stepping into build system territory, which can be quite a can of warms. For instance, what if the C source file requires pkg-config info? Generated headers? Extra, compiler-specific flags?

Python's distutils is a great example of how this can get out of control, at which point people start using their own build systems anyway. For instance, I believe Waf has special support for building Python extensions.

@kostya
Copy link
Contributor Author

kostya commented May 12, 2016

this is for simple cases, for big libraries better to use make in shard post install. example: kostya/http_parser.cr@97e6fad

@ozra
Copy link
Contributor

ozra commented May 12, 2016

It's a really cool concept, but as @kirbyfan64 says - it's opening Pandoras box!

@asterite
Copy link
Member

@kostya I like this, but I'll check with @waj. I also agree with @kirbyfan64, but for small code where you simply want to compile a small c file it can be useful. For example we could use it in the compiler for the sigfault handler.

We actually had an idea similar to this, but you'd embed the C code in the Crystal source file, somehow. Something like this:

lib Bla
  fun add(x: LibC::Int, y : LibC::Int) : Int32
    <<-C
   // and here comes the C code...
    >>
  end
end

Obviously not exactly like that, because where do you put the #includes. Maybe having it a separate C file is better because you can do things with that file separately (like compile it), can't do that if it's embedded in another source.

I'd really prefer if you'd first post an issue discussing your ideas, but only because we wouldn't like to waste some of your time if we later decide to reject it (although programming in Crystal is probably something fun on it's own 😸)

I'm really surprised and glad that you were able to tweak the compiler to your needs :-)

@kostya
Copy link
Contributor Author

kostya commented May 12, 2016

may be this can be implemented by macro, like ecr_file (and even with auto schema build)

@jkthorne
Copy link
Contributor

interestingly this might enable some things like http://llvm.org/docs/CoverageMappingFormat.html which need c files.

This could help leverage the llvm for tooling if there where a way to output c files.

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

Successfully merging this pull request may close these issues.

None yet

6 participants