New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using generated sources as argument of a custom target #3667

Open
bochecha opened this Issue Jun 2, 2018 · 6 comments

Comments

Projects
None yet
5 participants
@bochecha
Copy link

bochecha commented Jun 2, 2018

I have a custom target as follows:

custom_sources = custom_target(
  'custom-sources',
  input : a_list_of_files,
  output : [ 'foo.c', 'foo.h' ],
  command : [
    prog_python, files('script.py'), '--code', '@OUTPUT0@', '--header', '@OUTPUT1@',
    '@INPUT@',
  ],
)

I'm trying to preprocess the script.py, passing it through 2to3 and using the result as the script of that custom_command.

Since I have quite a few of those scripts, I wrote a generator:

prog_2to3 = generator(
    find_program('2to3'),
    arguments: [
        '--no-diffs', '--nobackups', '--write-unchanged-files', '--output-dir=@BUILD_DIR@', '@INPUT@'
    ],
    output: '@PLAINNAME@',
)

Then I can simply call that generator with:

script = prog_2to3.process(
    files('script.py'), preserve_path_from: meson.current_source_dir()
)

The problem is then how can I use the result of the generator as an argument of that custom_target's command?

The documentation doesn't say anything about how to use the object returned by generator.process(). (https://mesonbuild.com/Reference-manual.html#generator-object)

@QuLogic

This comment has been minimized.

Copy link
Member

QuLogic commented Jun 3, 2018

Unrelated to meson, but it's better to write 3-compatible code than use 2to3. It's only done once instead of at install time, and that makes editable/developer installs easier without have to re-run it (though meson doesn't really have an editable install mode, I guess.)

@bochecha

This comment has been minimized.

Copy link
Author

bochecha commented Jun 3, 2018

Unrelated to meson, but it's better to write 3-compatible code than use 2to3. It's only done once instead of at install time, and that makes editable/developer installs easier without have to re-run it (though meson doesn't really have an editable install mode, I guess.)

Thanks, I know.

@jpakkane

This comment has been minimized.

Copy link
Member

jpakkane commented Jun 3, 2018

The manual has an example. You can also look at the unit tests of Meson for further samples (grep for generator).

@bochecha

This comment has been minimized.

Copy link
Author

bochecha commented Jun 4, 2018

@jpakkane I don't see which example from the manual helps me.

See how the arguments to the custom_target's command are separated? I can't just add the generator output as input:, because then I'd need a way to pass @INPUT0@ somewhere (the generated script) and « the rest of inputs » elsewhere. (the list of input files)

@Akaricchi

This comment has been minimized.

Copy link
Contributor

Akaricchi commented Jun 11, 2018

IIRC you should be able to just shove the generator's output into the arguments array, no need for @INPUT@.

@bochecha

This comment has been minimized.

Copy link
Author

bochecha commented Jul 5, 2018

IIRC you should be able to just shove the generator's output into the arguments array, no need for @input@.

That doesn't work unfortunately. (that's what I had tried in the first place)

Here is a simple reproducer:

  • $ cat build-script.py`
    import sys
    
    output = sys.argv[1]
    inputs = sys.argv[2:]
    
    with open(output, 'w') as f:
        for filename in inputs:
            print >>f, filename
    
  • $ cat meson.build 
    project(
      'test-meson-generator',
      ['c'],
      version: '1.0',
      license: 'MIT',
      meson_version : '>= 0.44.1',
    )
    
    prog_2to3 = generator(
      find_program('2to3'),
      arguments: [
          '--no-diffs', '--nobackups', '--write-unchanged-files', '--output-dir=@BUILD_DIR@', '@INPUT@'
      ],
      output: '@PLAINNAME@',
    )
    
    script = prog_2to3.process(
      files('build-script.py'), preserve_path_from: meson.current_source_dir()
    )
    
    custom_sources = custom_target(
      'custom-sources',
      input : files(['input1, 'input2']),
      output : ['foo.c'],
      command : [
        'python3', script, '@OUTPUT@', '@INPUT@',
      ],
    )
    
  • touch input1 input2

Then simply try building:

$ meson _build
The Meson build system
Version: 0.46.1
Source dir: /home/mathieu/Projects/misc/test-meson-generator
Build dir: /home/mathieu/Projects/misc/test-meson-generator/_build
Build type: native build
Project name: test-meson-generator
Native C compiler: cc (gcc 8.1.1 "cc (GCC) 8.1.1 20180502 (Red Hat 8.1.1-1)")
Build machine cpu family: x86_64
Build machine cpu: x86_64
Program 2to3 found: YES (/usr/bin/2to3)

meson.build:21:0: ERROR:  Argument <mesonbuild.build.GeneratedList object at 0x7ff5ee78f240> in "command" is invalid

That GeneratedList object is not accepted as an argument for a command. It does not support indexing either (script[0] does not work).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment