Staging header files

I have a python app that requires a later version of libgit2 than is available in the repo, so I’m building libgit2 v1.5.0 in a separate part. When my python part builds, if fails with “git2.h” can’t be found.

What I noticed is that libgit2 DESTDIR is set to the PART_STAGE, i.e. /build/gitless-snap/parts/libgit2/install. I also noticed that pygit2 does not include this directory in the CFLAGS or LDFLAGS environment.

So my hack is to override CFLAGS and LDFLAGS but I have a feeling this is wrong. What am I missing? If I could just install libgit2 to /usr/local all would be good. OR if I could include the header and object files from the libgit2 part all would be good.

parts:
  libgit2:
    source: https://github.com/libgit2/libgit2/archive/refs/tags/v1.5.0.tar.gz
    plugin: cmake
    build-packages:
      -  libssl-dev
  gitless-cli:
    after: 
      - libgit2
    stage: 
      - libgit2
    plugin: python
    source: .
    build-environment:
      - CFLAGS: "$CFLAGS -I/build/gitless-snap/parts/libgit2/install/usr/local/include"
      - LDFLAGS: "$LDFLAGS -L/build/gitless-snap/parts/libgit2/install/usr/local/lib"
    build-packages:
      - libffi-dev
    stage-packages:
      - git

After some trial and error I think I may have figured it out. I need to “stage” the library and include files from libgit2 then tell the “part” how to find them via LDFLAGS and CFLAGS

...
parts:
  libgit2:
    source: https://github.com/libgit2/libgit2/archive/refs/tags/v1.5.0.tar.gz
    plugin: cmake
...
    stage: 
      - usr/local/include/*
      - usr/local/lib/*

  gitless-cli:
    after: 
      - libgit2
...
    build-environment:
      - CFLAGS: "$CFLAGS -I$SNAPCRAFT_STAGE/usr/local/include"
      - LDFLAGS: "$LDFLAGS -L$SNAPCRAFT_STAGE/usr/local/lib"

Another possibility is to configure the libgit2 part to install in usr instead of usr/local (using -DCMAKE_INSTALL_PREFIX=/usr in cmake-parameters), and in this case the gitless-cli part should find headers and libraries automatically.

What I noticed is that libgit2 DESTDIR is set to the PART_STAGE, i.e. /build/gitless-snap/parts/libgit2/install.

One technical nitpick: the part artifacts are installed to that directory, but DESTDIR is PART_INSTALL ($SNAPCRAFT_PART_INSTALL or $CRAFT_PART_INSTALL if you’re using base core22 or later) and not the stage directory.

You may also want to remove the headers from the final snap by adding a prime filter in part libgit2, if they’re not needed at runtime:

parts:
  libgit2:
    source: ...
    plugin: cmake
    cmake-parameters:
      - -DCMAKE_INSTALL_PREFIX=/usr
    ...
    prime:
      - -usr/local/include
1 Like