How do configure Meson for a project with multiple subdirectories?

Viewed 125

everyone.

I have a C++ project that uses MongoDB and wxWidgets as its dependencies, and the project is structured into multiple subdirectories.

I have recently started using the Meson build system but have no idea how to configure the meson.build file for this project structure.

Here is how my project is structured:

Project/
    meson.build
    BuildDir/
    Source/
        A/
            meson.build
            A1.hpp
            A1.cpp
            A2.hpp
            A2.cpp
            ...
        B/
            meson.build
            B1.hpp
            B1.cpp
            B2.hpp
            B2.cpp
            ...
        C/
            meson.build
            C1.hpp
            C1.cpp
            C2.hpp
            C2.cpp
            ...

Thank you.

1 Answers

in your project A,B and C are sub project, so a possible solution is on every sub project have a structure like that:

project(
  'A',
  'cpp',
  version : '1.0.0',
  default_options : ['warning_level=3']
)
project_description = ' ....'

sourceRoot = meson.source_root()

project_headers = [ # add your project headers ]
public_headers = [# add  public header ]

build_args = [#add build args.... ]

# ======
# Target
# ======

#shared or static lib or executable....
project_target = shared_library(  ...... )


# =======
# Project
# =======

# Make this library usable as a Meson subproject.
project_dep = declare_dependency(
  include_directories: public_headers
)
set_variable(meson.project_name() + '_dep', project_dep)

# Make this library usable from the system's
# package manager.
#install_headers(project_headers, subdir : meson.project_name())

pkg_mod = import('pkgconfig')
pkg_mod.generate(
  name : meson.project_name(),
  filebase : meson.project_name(),
  description : project_description,
  subdirs : meson.project_name(),
)

an the same for B,C sub project, than in the main meson must specify sub projects folders and add the dependency ...

project(
  'MyProjj',
  'cpp',
  version : '1.1.0',
  default_options : ['buildtype=plain','warning_level=3'],
  subproject_dir : 'Source',
  meson_version: '>= <the version you want to use>'
)
.......
dependency('A', fallback : ['<A path>', 'A_dep']),
dependency('B', fallback : ['<B path>', 'B_dep']),
dependency('C', fallback : ['<C path>', 'C_dep']),

so referring to your example:

Project/
    meson.build
    BuildDir/
    Source/
        A/
...

"A path" is A, and the first parameter of dependency(...) is the names of the dependency too look up

Related