Snapcraft: easy way to know what is target arch in scriptlets

when using scriptlets to handle more complex build use cases, it’s difficult to keep cross compilation option working when one does not know what is target architecture.
Can you we add something like: SNAPCRAFT_TARGET_ARCH

2 Likes

i tend to use something like:

if [ "$(arch)" = "x86_64" ]; then
....
fi

while having a single variable exposing it might be easier, the above isnt really a massive piece of code either …

I think we can easily expose that. It can even default to the native arch when not cross-compiling so there’s no need to double-check it.

That’s not going to work when cross-compiling. The reported environment is native for all intents and purposes.

cross compiling is the only place where i use it … what would not be working there ?

I wonder if something like on x86_64 could be useful as well, if only one architecture would need the script to be run, now that selectors are being decoupled from stage-packages.

Can you provide an example snippet of your scriptlet? To see which would be more straightforward in your case.

How can arch possibly know the target architecture?

target arch is in the architectures filed … all i need to know is the build arch in this case …

https://github.com/ogra1/nanopi-air-gadget/blob/master/snapcraft.yaml

Interesting. I was literally expecting this to evaluate $(arch) as a command which would print the native architecture. Can this actually be disambiguated in a scriptlet?

I suppose that works. Maybe it can be documented somewhere.

this works but only for your case, where you have only one target architecture. So in fact you are checking is your host arch != target arch.
But in my example I want to build for all 4 architectures, with ability to cross build as well.
And then this falls apart….
e.g.

  • cross building on x86_64 for arm64
  • cross building on x86_64 for armhf
  • natively building for x86_64
    how do you make decision there? You need to be able to see what is the target arch……

indeed, that would be a bit more complex to achieve in a scriptlet …

if you get target arch, this could work for many cases.
example:
I have maven build, even though it’s java project, it still has some native libraries bundled in to access things like USB. Now project chooses to know that is target device to bundle blobs only for that architecture rather for all like other projects do. With snaps we can reduce target “devices” to 4 architectures (unlike 10+ they have now), but still I need to tell what is target arch when building so I get right native blobs….

with the new creation of the snapcraftctl tool i wonder if it could grow a “get-targetarch” option that returns the actual used target arch for the current build so it could be used in scriptlets.

@kyrofa any opinion ?

Sorry for the delay @ogra, just got back from holidays. I’m not 100% sure what you’re looking for here… but does $SNAPCRAFT_ARCH_TRIPLET do it?

# <snip>
parts:
  my-part:
    plugin: nil
    override-pull: echo $SNAPCRAFT_ARCH_TRIPLET
$ snapcraft pull
Preparing to pull my-part 
Pulling my-part 
x86_64-linux-gnu

$ snapcraft pull --target-arch=i386
Setting target machine to 'i386'
Preparing to pull my-part 
Pulling my-part 
i386-linux-gnu
2 Likes

Awesome !

Is there a way to get just the arch (i can surely parse the triplet and/or chop it when needed but having just the arch in general might make sense here)

Today we only have the triplet, but it wouldn’t be difficult to add the deb arch.

1 Like