question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Difficulty to use custom_target() to generate data files

See original GitHub issue

I am trying to use custom_target to generate some data files via a python script. I am struggling on two points:

  • I don’t want to have to manually list all the output file in the output argument, since they depend on the content of the input file. I may have found a workaround though: I output all the files in a fixed folder and then reference this folder in output. Not sure if it has any negative implications.
  • I would like the script to be only run when the input file changes, and I couldn’t make this work: either the script never runs or it is run each time I build. I tried many combinations of install and build_by_default, and even tried to use the output files as dependencies with no success.

From what I understood of the manual, custom_target() should be the way to go here, but let me know if there is something more adapted for my use-case.

To Reproduce A simplified version of my meson file (in this case, script never runs):

prog_python = import('python').find_installation('python3')

custom_target('my_target',
    input: 'input.json',
    output: 'out', # folder where the script will write the output files
    output_dir: out_dir,
    command: [prog_python, 'my_script.py', '@INPUT@', out_dir / 'out'],
    install: true,
    install_dir: out_dir,
    build_by_default: false,
)

System parameters

  • windows 10
  • python: 3.8.2
  • meson: 0.58.0
  • ninja: 1.10.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
eli-schwartzcommented, Jul 4, 2021
output: ['out']
command: [prog_python, in_dir / 'my_script.py', '@INPUT@', meson.current_build_dir() / 'out'],
install_dir: get_option('datadir') / 'myprog', # ends up in /usr/share/myprog/out/{foo.txt,bar.log}

If your output is a directory, ninja becomes a bit clueless about how it got generated, and meson is pretty fuzzy on how to install it so it simply copies over the entire directory. But it does only build when the input gets changed, which is the important bit.

You just need the out/ directory which the python program is told to use, to be in the current_build_dir() to correspond to the kwarg value of output.

In fact, you may just use '@OUTPUT@' directly.

1reaction
eli-schwartzcommented, Jul 4, 2021
out_dir = meson.current_source_dir()

With this context the problem becomes clear, when you originally tried passing output_dir: out_dir, you no doubt intended for meson to generate a build.ninja which generates rules targeting the source directory rather than the build directory.

meson does not support in tree builds, so trying to tell it to do so was futile… and since my_script.py never tried to generate the output which you specified (instead it created totally unrelated files in out_dir) the build rule notices on every single build that the output still hasn’t been created, so it tries to rerun the rules which should create it.

Your install_dir argument is also nonsense – if you fix the out_dir location, then run ninja install, it will install builddir/out/foo.txt to $PWD/builddir/out/foo.txt. It then fails, because installing a file will first remove the existing destination file, then install the source file to the destination.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Glide Issue when CustomTarget is used in recyclerView
I have a recycler view with GridLayoutManager to list the images. When there are fewer data to populate it is working fine but...
Read more >
Allow configure_file, custom_target and generator output file ...
I'm evaluating meson and am trying to figure out if i can use it to build a FPGA project. One of the problems...
Read more >
CMake: dependencies between targets and files and custom ...
Custom commands in different directories. Another use case for add_custom_command() is generating source code files using 3rd party tools.
Read more >
Generated Sources In CMake Builds - Crascit
Learn how to use CMake commands like configure_file() and file() to generate source files that you can then add to the build.
Read more >
How to optimize memory consumption when using Glide
This article I describe a few interesting facts I found when looking for reasons for OOM errors. I focus mainly on Glide but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found