The Crystal Programming Language Forum

Compiling a C library with postinstall

Hello,

I have a Crystal project that needs to use a C library and I wanted to use the ‘postinstall’ script of the shard.yml to compile the C library, but the postinstall script seems to do nothing.
I even wrote the build commands in a script. If I call it directly (my_build_script), it builds correctly but if I try to call it with the shard, then nothing happens:
scripts:
postinstall: my_build_script
What is the recommended way to build C libraries within shard.yml?

Can you share the shard.yml and maybe the relvant part of the shard layout folder?

I a trying with test project, the shard is:

name: skin-test
version: 1.0.0

targets:
  skin-test:
    main: main.cr

crystal: 0.34.0

license: GPL

libraries:
  libskin: "*"

scripts:
  postinstall: make

Then I have a make.cmd whose content is:
cd …/skin/build && make
(if I use this line as postinstall, it does not work either).

In the documentation it is written:

The postinstall hook of a dependency will be run whenever that dependency is installed or upgraded in a project that requires it.

So I guess I need to have a dependency, but since it is a C library, it has no ‘shard’ so if I put it as dependency then I have this error: Missing “shard.yml” for “skin”
Should I add a dummy ‘shard.yml’ to the C library?

The postinstall hook of a dependency will be run whenever that dependency is installed or upgraded in a project that requires it.

That means that when someone installs your shard (uses it as a dependency) then the postinstall hook runs.

Are you using your shard in another project, or are you trying your shard in that same shard (developing it?). If it’s the latter, you probably need to run that make file yourself, because the shard isn’t being installed/used by other shards or projects at that point.

That said, I think it would be nice if shards could let you run the postintstall script locally so you could check that it works.

It is the main project that needs to link with the C library, so I understand I cannot compile it within the shard of the main project. How should I handle this case, what is the best practice?

I think it’s common practice to use a Makefile for that