Meson targets that depend on subdir siblings

时光总嘲笑我的痴心妄想 提交于 2019-12-24 16:53:18

问题


Here is my project structure:

.
├── include
├── src
│   ├── abc
│   │   ├── include
│   │   └── src
│   ├── def
│   │   ├── include
│   │   └── src
│   └── ghi
│       ├── include
│       └── src
└── vendor
    ├── bar
    │   ├── include
    │   └── src
    └── foo

16 directories

I would like to port my build to Meson. However, I'm not sure how to link targets defined in sibling folders.

My dependency graph looks like this:

  • src/abc/meson.build defines a static library abc
  • src/def/meson.build defines a static library def that depends on abc and foo
  • src/ghi/meson.build defines a static library ghi that depends on bar
  • vendor/bar/meson.build defines a static library bar
  • vendor/foo/meson.build defines a static library foo
  • The top-level meson.build defines an executable app that depends on abc, def and ghi

In the documentation, there seem to be two mechanisms:

  • subdir
  • subproject

It is not clear to me which is best here. I do not have any dependencies outside of my source-code.

What should I write in my meson.build files to link these targets together?


回答1:


You can use subdir from the top-level meson.build file down. All variables you declare in the subdir meson.build files are available to later meson.build files. As long as you get the order of subdir calls correct, it will work.




回答2:


Besides @sdgfsdh's (correct) answer, another approach I like is to define libraries and executables only in the top-level meson file, and use subdir calls to define a sets of source files and "local" include paths. Done this way, the subdir files don't implicitly depend on each other; the entire dependency tree lives in the top-level meson file.

The advantages of this approach are:

  • subdirs don't need to know their own path (files() and include_directories() will track this for you)
  • The top-level file only needs to know subdir paths to call the subdir meson files. After that, you can define everything in terms of variables created in the subdirs
  • subdir files don't directly depend on any other meson files

Disadvantages:

  • The top-level file is more cluttered
  • Variable names in subdir files need to be globally unique, since everything is ultimately defined in the top-level scope

An example for your case:

# Top-level meson.build
subdir('src/abc')
subdir('src/def')
subdir('src/ghi')
subdir('vendor/foo')
subdir('vendor/bar')

libabc = static_library('abc', abc_files, include_directories: abc_includes)

libabc_dep = declare_dependency(include_directories: abc_includes, link_with : libabc)

libfoo = static_library('foo', foo_files, include_directories: foo_includes)

libfoo_dep = declare_dependency(include_directories: foo_includes, link_with : libfoo)

libdef = library('def', def_files, include_directories: def_includes, dependencies : [ libabc_dep, libfoo_dep])
# src/abc/meson.build (others would be similar)
abc_files = files(['1.c','2.c',...])
abc_includes = include_directories('include')


来源:https://stackoverflow.com/questions/55107396/meson-targets-that-depend-on-subdir-siblings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!