How to run post build commands in meson?
Asked Answered
A

2

5

How can I do in meson to run a command after building a target? Eg. I have an executable:

executable('target.elf', 'source1.c', 'source2.c')

And after target.elf built I want to execute a command (eg. chmod -x target.elf) on it.

I tried custom_target(), but that requires an output. I don't have new output, I just have target.elf. I tried run_command() but I didn't know how to execute it after the building.

Animated answered 6/1, 2020 at 23:40 Comment(0)
P
4

The following code should do.

project('tutorial', 'c')
exec = executable('target.elf', 'main.c', build_by_default : false)
custom_target('final binary',
          depends : exec,
          input : exec,
          output : 'fake',
          command : ['chmod', '+x', '@INPUT@'],
          build_by_default : true)

Note that because I want to always run the fake target, I'm using custom_target(). However, the command chmod + x demo doesn't generate the file fake specified in custom_target(), successive ninja command will always run the target.

If you don't want this behaviour, there are two ways:

  1. You can write a script which chmod the target.elf and then copies it to target, thus effectively creates the target file. Make sure to change the output file in the meson.build if you do so.

  2. If you don't mind typing ninja chmod instead of ninja, you can use run_target().

# optional
run_target('chmod',
       command : ['chmod', '+x', exec])

Another alternative is to use install_mode for executable().

Also note that you should always use find_program() instead of plain chmod. This example doesn't use it for simplicity.

Pennington answered 8/1, 2020 at 9:0 Comment(2)
Thank you for your answer. My problem is I don't want fake files and I don't want to write all target names after ninja. I have more targets, and each of it will have a fake file. It's not elegant. And if someone forgot to write all targets nothing will warn him/her that something is missing. (A few scripts can solve this problem, but I don't think we should use scripts for it.)Animated
Maybe I'm completely on a wrong way. I have a problem that nobody else complaining about and there isn't an elegant or simple solution. I think I'm using meson in a wrong way.Animated
D
3

executable now has an argument install_mode (added 0.47.0) to specify the file mode in symbolic format and optionally the owner/uid and group/gid for the installed files.

I just noticed that yasushi-shoji has provided this answer already.

Dickdicken answered 1/6, 2020 at 12:38 Comment(1)
Thanks for your reply. Yes, I have seen that answer. Sorry, but install_mode affects only the mode of installed files not the files in the build directory. I don't want to install that files, I use them in an image file (they are cross compiled binaries for another architecture). I have foreign binaries in the build directory with execute permission (of course they won't execute on the build machine).Animated

© 2022 - 2024 — McMap. All rights reserved.