mesonbuild / mesonbuild/meson

Meson not fully respecting -I/-L inside CLFAGS/LDFLAGS

Open
#4,597 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
6.6k
Forks
1.9k
Avg merge
2d 6h
Merged PRs (30d)
33

Description

It's not unusual for projects to have a custom ` contrib` tree that contains bundled third party dependencies (+ their dependencies) to build these inside a custom tree ([example](https://git.videolan.org/?p=vlc.git;a=tree;f=contrib;h=f4e4131290921b7af736b0cb04c3a7c23be8ada9;hb=HEAD)). This is usually done by adding extra cflags and ldflags to set the correct `-L` and `-I` so that projects can find their own dependencies.

With autotools, for example, this works fine - doing the equivalent of `cc.find_library()` or `cc.get_define()` in autotools respects the set `-L` and `-I` set via e.g. the environment. With meson, however, this same approach does not work. Even with something like `LDFLAGS="-L/path/to/contrib/build/lib" CFLAGS="$LDFLAGS -I/path/to/contrib/build/include" meson --prefix /path/to/contrib/build`, meson fails to find a simple library or header, as in the following example:

```shell
$ cd /path/to/contrib && tree
.
├── bar.autotools
│   └── configure.ac
├── bar.meson
│   └── meson.build
└── build
├── include
│   └── foo.h
└── lib
└── libfoo.so

5 directories, 4 files
```

`bar.meson/meson.build`:
```meson
project('bar', 'c')

cc = meson.get_compiler('c')
foo = cc.find_library('foo', required: true)
ver = cc.get_define('FOO_VERSION', prefix: '#include ').to_int()
```

To work around the issue in this example, I could do something like this:

```meson
project('bar', 'c')

prefix = get_option('prefix')
prefix_lib = join_paths(prefix, get_option('libdir'))
prefix_inc = include_directories(join_paths(prefix, get_option('includedir')))

foo = cc.find_library('libfoo.so', dirs: prefix_lib, required: true)
ver = cc.get_define('FOO_VERSION',
prefix: '#include ',
include_directories: prefix_inc,
).to_int()
```

Alternatively, I could expose the equivalent of `prefix_lib` and `prefix_inc` directly as string options that the user can provide.

However both of these approaches rely on either patching the meson.build of every project, or somehow communicating a change like this upstream to every one of the projects. An ideal solution would consist of some way to explicitly add extra lib/include search paths at configure time, either via CFLAGS/LDFLAGS as before, explicitly via some meson built-in option that we can set via `-D`, or implicitly via `--prefix`.

Contributor guide

Open the contributing guide

Research direction

Reproduce the example from bar.meson/meson.build using the shown CFLAGS and LDFLAGS, then compare cc.find_library() and cc.get_define() with the prefix-based workaround. Review how configure-time search paths are handled and define a supported approach that lets both checks find the bundled header and library without patching each project.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.