mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.49k stars 1.59k forks source link

[feature request] add bison/flex support #11895

Open vtorri opened 1 year ago

vtorri commented 1 year ago

Like autotools does. This could simplify the tacks for users using bison/flex

vanc commented 1 year ago

How about use find_program and custom_target?

For libpcap:

lex = find_program('flex', 'lex')
yacc = find_program('bison', 'yacc', 'byacc')

scanner_c = custom_target('scanner.c',
  output: [ 'scanner.c', 'scanner.h' ],
  input: 'scanner.l',
  command: [lex, '-P', 'pcap_', '-o@OUTPUT0@', '--nounput',
            '--header-file=@OUTPUT1@', '@INPUT@', ],
)
dcbaker commented 1 year ago

I would be open to discussing a module for common code generators (protoc anyone?), but only if part of the plan was to ease the differences between flex vs lex, and yacc vs byacc vs bison, and handling host machine specific flags automatically (ie, does flex need --wincompat).

Mesa has a pile of code for dealing with all of those differences

apache-hb commented 4 months ago

+1 for adding flex/bison modules or special casing find_program, currently i have this chunk of config to get them both reliably working on windows and unix


lexargs = []
parseargs = []

host = host_machine.system()
# setup required flex/bison args
if host == 'windows'
    lexargs += [ '--wincompat' ]
else
    parseargs += [ '-Wdeprecated' ]
endif

flex = find_program('flex', 'win_flex', version : '>=2.6')
bison = find_program('bison', 'win_bison', version : host == 'windows' ? '>=2.6' : '>=3.5')

lex = generator(flex,
    output : [ '@BASENAME@_flex.c', '@BASENAME@_flex.h' ],
    arguments : lexargs + [
        '--outfile=@OUTPUT0@',
        '--header-file=@OUTPUT1@',
        '@INPUT@'
    ]
)

parse = generator(bison,
    output : [ '@BASENAME@_bison.c', '@BASENAME@_bison.h' ],
    arguments : parseargs + [
        '-d', '@INPUT@', '-v',
        '--output=@OUTPUT0@',
        '--defines=@OUTPUT1@'
    ]
)
vvdimako commented 1 month ago

Hello there, I am new to meson but if I am not mistaken, the flex/bison files (e.g. scanner.c) generated by the above solutions are placed in the build directory.

However, when distributing the sources to another user, in our autotools-based project (which I am trying to convert to meson) the generated flex/bison files were included in the source tree so as not to obligate the user to have flex/bison installed. How would this be possible with meson?

Ideally, scanner.c should be generated only if a) there is no scanner.c in the source tree or b) the relevant source files (scanner.l) were modified so a new scanner.c should be generated. Is there any way to have such a behavior?

dcbaker commented 1 month ago

@vvdimako there isn't a good way to do (b), ninja either needs to own the files as generated or be able to assume they're not generated. (a) is not too hard to do, there's a function in the fs module for that. Something like:

fs = import('fs)

if fs.is_file(meson.current_source_dir() / 'scanner.c')
  '<do path for tarball>'
else
  '<do path for git>'
endif`