www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to create meson.build with external libs?

reply p.shkadzko <p.shkadzko gmail.com> writes:
Ok, I am trying to meson and is struggling with meson.build file. 
I looked up the examples page: 
https://github.com/mesonbuild/meson/tree/master/test%20cases/d 
which has a lot of examples but not the one that shows you how to 
build your project with some external dependency :)

Let's say we have a simple dir "myproj" with "meson.build" in it 
and some source files like "app.d" and "helper_functions.d".

~/myproj
    app.d
    helper_functions.d
    meson.build

"helper_functions.d" uses let's say lubeck library which 
according to 
https://forum.dlang.org/thread/nghoprwkihazjikyhshz forum.dlang.org is
supported by meson.

Here is my meson.build:
-----------------------
project('demo', 'd',
   version : '0.1',
   default_options : ['warning_level=3']
   )

lubeck = dependency('lubeck', version: '>=1.1.7')
ed = executable('mir_quickstart', 'app.d', dependencies: lubeck, 
install : true)


However, when I try to build it I get the following error:
-----
$ meson build
The Meson build system
Version: 0.52.1
Source dir: /home/user/dev/github/demo
Build dir: /home/user/dev/github/demo/build
Build type: native build
Project name: demo
Project version: 0.1
D compiler for the host machine: ldc2 (llvm 1.18.0 "LDC - the 
LLVM D compiler (1.18.0):")
D linker for the host machine: GNU ld.gold 2.33.1
Host machine cpu family: x86_64
Host machine cpu: x86_64
Found pkg-config: /usr/bin/pkg-config (1.6.3)
Found CMake: /usr/bin/cmake (3.16.2)
Run-time dependency lubeck found: NO (tried pkgconfig and cmake)

meson.build:8:0: ERROR: Dependency "lubeck" not found, tried 
pkgconfig and cmake

A full log can be found at 
/home/user/dev/github/demo/build/meson-l
-----

What do I need to do in order to build the project with "lubeck" 
dependency in meson?
Jan 12 2020
next sibling parent reply Rasmus Thomsen <oss cogitri.dev> writes:
On Sunday, 12 January 2020 at 22:00:33 UTC, p.shkadzko wrote:
 What do I need to do in order to build the project with 
 "lubeck" dependency in meson?
In difference to dub, meson will _not_ auto-download required software for you. You have to ways to go forward with this: 1 (IMHO the better way, especially if you ever want a distro to package your thing): Install lubeck ala `git clone https://github.com/kaleidicassociates/lubeck && cd lubeck && meson build && ninja -C build install`. This will install lubeck to your system (by default into `/usr/local`, you can set a different by passing `--prefix` to meson). This will generate a so called pkg-config (`.pc`) file: https://github.com/kaleidicassociates/lubeck/blob/master/meson.build#L49 which meson will discover. 2 (The probably easier way in the short term): Install lubeck via meson, then discover the dependency like specified here: https://mesonbuild.com/Dependencies.html#dependency-method
Jan 12 2020
next sibling parent reply p.shkadzko <p.shkadzko gmail.com> writes:
On Sunday, 12 January 2020 at 22:12:14 UTC, Rasmus Thomsen wrote:
 On Sunday, 12 January 2020 at 22:00:33 UTC, p.shkadzko wrote:
 What do I need to do in order to build the project with 
 "lubeck" dependency in meson?
In difference to dub, meson will _not_ auto-download required software for you. You have to ways to go forward with this: 1 (IMHO the better way, especially if you ever want a distro to package your thing): Install lubeck ala `git clone https://github.com/kaleidicassociates/lubeck && cd lubeck && meson build && ninja -C build install`. This will install lubeck to your system (by default into `/usr/local`, you can set a different by passing `--prefix` to meson). This will generate a so called pkg-config (`.pc`) file: https://github.com/kaleidicassociates/lubeck/blob/master/meson.build#L49 which meson will discover. 2 (The probably easier way in the short term): Install lubeck via meson, then discover the dependency like specified here: https://mesonbuild.com/Dependencies.html#dependency-method
Why do you think 1 is the better way? I feel like it is a lot of manual work for just one dependency. Also, it is not a good idea to pollute your /usr/local with non-distro packages.
Jan 12 2020
parent Rasmus Thomsen <oss cogitri.dev> writes:
On Sunday, 12 January 2020 at 22:20:16 UTC, p.shkadzko wrote:
 Why do you think 1 is the better way?
Well, I suppose I might be a bit biased being a distro packager, but not using dub ensures your package builds properly with what your distro uses (e.g. pkg-config). It _is_ more effort up front though, yes. Also, I feel like using a more standardized way of packaging via meson with pkg-config is better (and as a distro packager I would refrain from packaging something which uses a dub dependency instead of a (shared) pkg-config one).
 I feel like it is a lot of manual work for just one dependency. 
 Also, it is not a good idea to pollute your /usr/local with 
 non-distro packages.
Well, the only purpose of /usr/local is for you to put your non-distro stuff there, /usr is the package manager's territory :)
Jan 12 2020
prev sibling next sibling parent p.shkadzko <p.shkadzko gmail.com> writes:
On Sunday, 12 January 2020 at 22:12:14 UTC, Rasmus Thomsen wrote:
 On Sunday, 12 January 2020 at 22:00:33 UTC, p.shkadzko wrote:
 [...]
In difference to dub, meson will _not_ auto-download required software for you. You have to ways to go forward with this: [...]
Thanks! I shall try it out.
Jan 12 2020
prev sibling parent reply p.shkadzko <p.shkadzko gmail.com> writes:
On Sunday, 12 January 2020 at 22:12:14 UTC, Rasmus Thomsen wrote:
 On Sunday, 12 January 2020 at 22:00:33 UTC, p.shkadzko wrote:
 [...]
In difference to dub, meson will _not_ auto-download required software for you. You have to ways to go forward with this: [...]
I followed the 1 step, namely git cloned the lubeck and installed it with meson into /usr/local however, I still get the same error. Could I miss anything?
Jan 13 2020
parent reply p.shkadzko <p.shkadzko gmail.com> writes:
On Monday, 13 January 2020 at 17:14:29 UTC, p.shkadzko wrote:
 On Sunday, 12 January 2020 at 22:12:14 UTC, Rasmus Thomsen 
 wrote:
 On Sunday, 12 January 2020 at 22:00:33 UTC, p.shkadzko wrote:
 [...]
In difference to dub, meson will _not_ auto-download required software for you. You have to ways to go forward with this: [...]
I followed the 1 step, namely git cloned the lubeck and installed it with meson into /usr/local however, I still get the same error. Could I miss anything?
I had to set PKG_CONFIG_PATH to "/usr/local/lib/pkgconfig". For some reason Manjaro distro doesn't have it set by default. After setting the pkgconfig path, lubeck is getting found and everything works.
Jan 13 2020
parent reply p.shkadzko <p.shkadzko gmail.com> writes:
On Monday, 13 January 2020 at 19:56:35 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 17:14:29 UTC, p.shkadzko wrote:
 On Sunday, 12 January 2020 at 22:12:14 UTC, Rasmus Thomsen 
 wrote:
 On Sunday, 12 January 2020 at 22:00:33 UTC, p.shkadzko wrote:
 [...]
In difference to dub, meson will _not_ auto-download required software for you. You have to ways to go forward with this: [...]
I followed the 1 step, namely git cloned the lubeck and installed it with meson into /usr/local however, I still get the same error. Could I miss anything?
I had to set PKG_CONFIG_PATH to "/usr/local/lib/pkgconfig". For some reason Manjaro distro doesn't have it set by default. After setting the pkgconfig path, lubeck is getting found and everything works.
After I ran "meson build" I got the following output: ----------------------- The Meson build system Version: 0.52.1 Source dir: /home/tastyminerals/dev/test_proj Build dir: /home/tastyminerals/dev/test_proj/build Build type: native build Project name: mir_quickstart Project version: 0.1 D compiler for the host machine: ldc2 (llvm 1.18.0 "LDC - the LLVM D compiler (1.18.0):") D linker for the host machine: GNU ld.gold 2.33.1 Host machine cpu family: x86_64 Host machine cpu: x86_64 Found pkg-config: /usr/bin/pkg-config (1.6.3) Run-time dependency mir-algorithm found: YES 3.4.0 Run-time dependency lubeck found: YES 1.0.0 Build targets in project: 1 Found ninja-1.9.0 at /usr/bin/ninja I then cd to build/ dir and run "ninja" ------------------------ [2/2] Linking target test_proj.tart exe/app.d.o'. But when I try to run compiled app.d file as demo_run, I get: ------------------------ ./demo_run: error while loading shared libraries: libmir-algorithm.so: cannot open shared object file: No such file or directory I thought that if meson builds and finds the external libs successfully and ninja links everything, all should be fine. I don't understand. Why the compiled file cannot find the external library?
Jan 13 2020
parent reply p.shkadzko <p.shkadzko gmail.com> writes:
On Monday, 13 January 2020 at 20:53:43 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 19:56:35 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 17:14:29 UTC, p.shkadzko wrote:
 [...]
I had to set PKG_CONFIG_PATH to "/usr/local/lib/pkgconfig". For some reason Manjaro distro doesn't have it set by default. After setting the pkgconfig path, lubeck is getting found and everything works.
After I ran "meson build" I got the following output: ----------------------- The Meson build system Version: 0.52.1 Source dir: /home/tastyminerals/dev/test_proj Build dir: /home/tastyminerals/dev/test_proj/build Build type: native build Project name: mir_quickstart Project version: 0.1 D compiler for the host machine: ldc2 (llvm 1.18.0 "LDC - the LLVM D compiler (1.18.0):") D linker for the host machine: GNU ld.gold 2.33.1 Host machine cpu family: x86_64 Host machine cpu: x86_64 Found pkg-config: /usr/bin/pkg-config (1.6.3) Run-time dependency mir-algorithm found: YES 3.4.0 Run-time dependency lubeck found: YES 1.0.0 Build targets in project: 1 Found ninja-1.9.0 at /usr/bin/ninja I then cd to build/ dir and run "ninja" ------------------------ [2/2] Linking target test_proj.tart exe/app.d.o'. But when I try to run compiled app.d file as demo_run, I get: ------------------------ ./demo_run: error while loading shared libraries: libmir-algorithm.so: cannot open shared object file: No such file or directory I thought that if meson builds and finds the external libs successfully and ninja links everything, all should be fine. I don't understand. Why the compiled file cannot find the external library?
$LD_LIBRARY_PATH was not set to "/usr/local/lib" where libmir-algorithm.so is located. I had to set it explicitly and recompile the project but then I get another error message: ./demo_proj: error while loading shared libraries: subprojects/mir-algorithm/libmir-algorithm.so.3.4.0: cannot open shared object file: No such file or directory (ಠ_ಠ)
Jan 13 2020
parent reply p.shkadzko <p.shkadzko gmail.com> writes:
On Monday, 13 January 2020 at 21:15:51 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 20:53:43 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 19:56:35 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 17:14:29 UTC, p.shkadzko wrote:
 [...]
I had to set PKG_CONFIG_PATH to "/usr/local/lib/pkgconfig". For some reason Manjaro distro doesn't have it set by default. After setting the pkgconfig path, lubeck is getting found and everything works.
After I ran "meson build" I got the following output: ----------------------- The Meson build system Version: 0.52.1 Source dir: /home/tastyminerals/dev/test_proj Build dir: /home/tastyminerals/dev/test_proj/build Build type: native build Project name: demo_proj Project version: 0.1 D compiler for the host machine: ldc2 (llvm 1.18.0 "LDC - the LLVM D compiler (1.18.0):") D linker for the host machine: GNU ld.gold 2.33.1 Host machine cpu family: x86_64 Host machine cpu: x86_64 Found pkg-config: /usr/bin/pkg-config (1.6.3) Run-time dependency mir-algorithm found: YES 3.4.0 Run-time dependency lubeck found: YES 1.0.0 Build targets in project: 1 Found ninja-1.9.0 at /usr/bin/ninja I then cd to build/ dir and run "ninja" ------------------------ [2/2] Linking target test_proj.tart exe/app.d.o'. But when I try to run compiled app.d file as demo_run, I get: ------------------------ ./demo_run: error while loading shared libraries: libmir-algorithm.so: cannot open shared object file: No such file or directory I thought that if meson builds and finds the external libs successfully and ninja links everything, all should be fine. I don't understand. Why the compiled file cannot find the external library?
$LD_LIBRARY_PATH was not set to "/usr/local/lib" where libmir-algorithm.so is located. I had to set it explicitly and recompile the project but then I get another error message: ./demo_proj: error while loading shared libraries: subprojects/mir-algorithm/libmir-algorithm.so.3.4.0: cannot open shared object file: No such file or directory (ಠ_ಠ)
My test app.d didn't have any imports for mir.ndslice and lubeck. After I imported them, ninja threw the following error. ----------- [1/2] Compiling D object 'demo_proj exe/app.d.o'. FAILED: demo_proj exe/app.d.o ldc2 -I=demo_proj exe -I=. -I=.. -I/usr/local/include/d/lubeck -I/usr/local/include/d/mir-algorithm -I/usr/local/include/d/mir-core -I/usr/local/include/d/mir-blas -I/usr/local/include/d/mir-lapack -I/usr/local/include/d/mir-random -enable-color -wi -dw -g -d-debug -of='demo_proj exe/app.d.o' -c ../app.d /usr/local/include/d/lubeck/lubeck.d(8): Error: module cblas is in file 'cblas.d' which cannot be read import path[0] = demo_proj exe import path[1] = . import path[2] = .. import path[3] = /usr/local/include/d/lubeck import path[4] = /usr/local/include/d/mir-algorithm import path[5] = /usr/local/include/d/mir-core import path[6] = /usr/local/include/d/mir-blas import path[7] = /usr/local/include/d/mir-lapack import path[8] = /usr/local/include/d/mir-random import path[9] = /usr/include/dlang/ldc ninja: build stopped: subcommand failed. cblas.d is located in /usr/local/include/d/cblas/cblas/cblas.d I added /usr/local/include/d/cblas/cblas to $PATH but that didn't help :(
Jan 14 2020
next sibling parent reply Andre Pany <andre s-e-a-p.de> writes:
On Tuesday, 14 January 2020 at 09:54:18 UTC, p.shkadzko wrote:
 [...]
May I ask, whether you have tried to use Dub, or is s.th. blocking you from using Dub? Kind regards André
Jan 14 2020
parent p.shkadzko <p.shkadzko gmail.com> writes:
On Tuesday, 14 January 2020 at 11:26:30 UTC, Andre Pany wrote:
 On Tuesday, 14 January 2020 at 09:54:18 UTC, p.shkadzko wrote:
 [...]
May I ask, whether you have tried to use Dub, or is s.th. blocking you from using Dub? Kind regards André
I tested dub and it fetched and compiled mir.ndslice and lubeck without issues. Then somebody mentioned meson and that it gives you more control and speed over the build. I also saw that mir library uses meson so decided to give it a try.
Jan 14 2020
prev sibling parent reply Rasmus Thomsen <oss cogitri.dev> writes:
On Tuesday, 14 January 2020 at 09:54:18 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 21:15:51 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 20:53:43 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 19:56:35 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 17:14:29 UTC, p.shkadzko wrote:
 [...]
I had to set PKG_CONFIG_PATH to "/usr/local/lib/pkgconfig". For some reason Manjaro distro doesn't have it set by default. After setting the pkgconfig path, lubeck is getting found and everything works.
It's very odd to me that Manjaros pkg-config doesn't include that pkg-config path by default and also doesn't include that library path by default, every distro I've so far used did that.
 I added /usr/local/include/d/cblas/cblas to $PATH but that 
 didn't help :(
Can you show us your meson.build? You need to set `dependencies:` properly to include all dependencies so ninja includes the right dirs and links the required libraries. You can look at other projects using D for that, e.g. I do it like this: https://github.com/Cogitri/corecollector/blob/master/source/corectl/meson.build
Jan 14 2020
parent reply p.shkadzko <p.shkadzko gmail.com> writes:
On Tuesday, 14 January 2020 at 15:15:09 UTC, Rasmus Thomsen wrote:
 On Tuesday, 14 January 2020 at 09:54:18 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 21:15:51 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 20:53:43 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 19:56:35 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 17:14:29 UTC, p.shkadzko 
 wrote:
 [...]
I had to set PKG_CONFIG_PATH to "/usr/local/lib/pkgconfig". For some reason Manjaro distro doesn't have it set by default. After setting the pkgconfig path, lubeck is getting found and everything works.
It's very odd to me that Manjaros pkg-config doesn't include that pkg-config path by default and also doesn't include that library path by default, every distro I've so far used did that.
 I added /usr/local/include/d/cblas/cblas to $PATH but that 
 didn't help :(
Can you show us your meson.build? You need to set `dependencies:` properly to include all dependencies so ninja includes the right dirs and links the required libraries. You can look at other projects using D for that, e.g. I do it like this: https://github.com/Cogitri/corecollector/blob/master/source/corectl/meson.build
Here is my meson.build file --------------------------- project('demo_proj', 'd', version : '0.1', default_options : ['warning_level=3'] ) mir_alg = dependency('mir-algorithm', method: 'pkg-config') lubeck = dependency('lubeck', method: 'pkg-config') required_deps = [mir_alg, lubeck] ed = executable('demo_proj', 'app.d', dependencies: required_deps, install : true) As I mentioned earlier cblas.d is installed in /usr/local/include/d/cblas/cblas/cblas.d
Jan 14 2020
parent p.shkadzko <p.shkadzko gmail.com> writes:
On Tuesday, 14 January 2020 at 20:14:30 UTC, p.shkadzko wrote:
 On Tuesday, 14 January 2020 at 15:15:09 UTC, Rasmus Thomsen 
 wrote:
 On Tuesday, 14 January 2020 at 09:54:18 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 21:15:51 UTC, p.shkadzko wrote:
 On Monday, 13 January 2020 at 20:53:43 UTC, p.shkadzko wrote:
[...]
It's very odd to me that Manjaros pkg-config doesn't include that pkg-config path by default and also doesn't include that library path by default, every distro I've so far used did that.
 I added /usr/local/include/d/cblas/cblas to $PATH but that 
 didn't help :(
Can you show us your meson.build? You need to set `dependencies:` properly to include all dependencies so ninja includes the right dirs and links the required libraries. You can look at other projects using D for that, e.g. I do it like this: https://github.com/Cogitri/corecollector/blob/master/source/corectl/meson.build
Here is my meson.build file --------------------------- project('demo_proj', 'd', version : '0.1', default_options : ['warning_level=3'] ) mir_alg = dependency('mir-algorithm', method: 'pkg-config') lubeck = dependency('lubeck', method: 'pkg-config') required_deps = [mir_alg, lubeck] ed = executable('demo_proj', 'app.d', dependencies: required_deps, install : true) As I mentioned earlier cblas.d is installed in /usr/local/include/d/cblas/cblas/cblas.d
If I replace lubeck with mir-lapack (which I also installed into /usr/local/lib), repeat "meson build && cd build && ninja" and call ./demo_proj, I get this: ----- ./demo_proj: error while loading shared libraries: subprojects/mir-algorithm/libmir-algorithm.so.3.4.0:ject file: No such file or directory But libmir-algorithm.so.3.4.0 is installed in /usr/local/lib and it is in $PATH :(
Jan 14 2020
prev sibling parent 9il <ilyayaroshenko gmail.com> writes:
On Sunday, 12 January 2020 at 22:00:33 UTC, p.shkadzko wrote:
 Ok, I am trying to meson and is struggling with meson.build 
 file. I looked up the examples page: 
 https://github.com/mesonbuild/meson/tree/master/test%20cases/d 
 which has a lot of examples but not the one that shows you how 
 to build your project with some external dependency :)

 Let's say we have a simple dir "myproj" with "meson.build" in 
 it and some source files like "app.d" and "helper_functions.d".

 ~/myproj
    app.d
    helper_functions.d
    meson.build

 "helper_functions.d" uses let's say lubeck library which 
 according to 
 https://forum.dlang.org/thread/nghoprwkihazjikyhshz forum.dlang.org is
supported by meson.

 Here is my meson.build:
 -----------------------
 project('demo', 'd',
   version : '0.1',
   default_options : ['warning_level=3']
   )

 lubeck = dependency('lubeck', version: '>=1.1.7')
 ed = executable('mir_quickstart', 'app.d', dependencies: 
 lubeck, install : true)


 However, when I try to build it I get the following error:
 -----
 $ meson build
 The Meson build system
 Version: 0.52.1
 Source dir: /home/user/dev/github/demo
 Build dir: /home/user/dev/github/demo/build
 Build type: native build
 Project name: demo
 Project version: 0.1
 D compiler for the host machine: ldc2 (llvm 1.18.0 "LDC - the 
 LLVM D compiler (1.18.0):")
 D linker for the host machine: GNU ld.gold 2.33.1
 Host machine cpu family: x86_64
 Host machine cpu: x86_64
 Found pkg-config: /usr/bin/pkg-config (1.6.3)
 Found CMake: /usr/bin/cmake (3.16.2)
 Run-time dependency lubeck found: NO (tried pkgconfig and cmake)

 meson.build:8:0: ERROR: Dependency "lubeck" not found, tried 
 pkgconfig and cmake

 A full log can be found at 
 /home/user/dev/github/demo/build/meson-l
 -----

 What do I need to do in order to build the project with 
 "lubeck" dependency in meson?
Seems like you have missed the *.wrap files in subprojects folder. It's a bad idea to install D meson libs into system as probably you will want to control versions easily. You need all *wrap files recursively. Check this https://github.com/kaleidicassociates/lubeck/tree/master/subprojects and add a wrap file for Lubeck. You can specify tags instead of the master branches. - it is recommended for stable work. Ilya
Jan 15 2020