[CDRIVER-4813] Document library consumption from Scons & meson Created: 12/Jan/24  Updated: 06/Feb/24  Resolved: 06/Feb/24

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: 1.21.0
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: LIU Hao Assignee: Unassigned
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Updated description:

Based on the discussion in this ticket, it is apparent that we need to document the proper way to consume libbson and libmongoc from some build systems. We currently provide pkg-config and CMake scripts which can be used directly by autotools and CMake, respectively. However, for other build systems it is necessary to document that consumption of libbson and libmongoc requires leveraging the build system's pkg-config compatibility features. See the discussion in this ticket for details and pointers.

Original description follows:

First of all, thank you. I appreciate your great work!

I am using the prebuilt libmongoc from Ubuntu Jammy, installable with APT. I notice that libmogoc and libbson headers are installed to `/usr/include/libmongoc-1.0/mongoc/` and `/usr/include/libbson-1.0/bson/`, instead of the conventional `/usr/include/`. So when I do

#include <libmongoc-1.0/mongoc/mongoc.h> 

I get errors such as

/usr/include/libmongoc-1.0/mongoc/mongoc.h:22:10: fatal error: bson/bson.h: No such file or directory
   22 | #include <bson/bson.h>
      |          ^~~~~~~~~~~~~

 

The documentation only mentions cmake and pkg-config. For other build systems, such scons and meson, I have to hard-code the include path, by passing

-isystem /usr/include/libbson-1.0 -isystem /usr/include/libmongoc-1.0 

This is not only inconvenient, but also prevents overriding the system library with a custom one, typically installed into `/usr/local/include`.

So, are there any reasons why these headers are not installed to `/usr/include/libmongoc-1.0/mongoc/` instead of the more conventional `/usr/include/mongoc/`? Thanks in advance.



 Comments   
Comment by LIU Hao [ 27/Jan/24 ]

I'm migrating one of my C++ projects to meson now.

dep_mongoc = dependency('libmongoc-1.0', required: false) 

This seems to yield a dependency object for the C language. It adds libraries i.e. LIBS but doesn't seem to pass necessary CFLAGS to the C++ compiler. (still looking for a practical solution.)

Apologies for the confusion; forgot to attach the dependency to the program.

Comment by Roberto Sanchez [ 27/Jan/24 ]

That is a good point about the difference between -I and -isystem. I will raise the issue within the team to see if we might be able to change our generated .pc files.

Comment by LIU Hao [ 26/Jan/24 ]

Well, using a standard include path has a side effect: GCC knows it is a system header and suppress warnings.

 

For mongoc, pkg-config gives two directories using -I instead of -isystem:

$ pkg-config --cflags libmongoc-1.0 
-I/usr/include/libmongoc-1.0 -I/usr/include/libbson-1.0

 

If I add -Werror=sign-conversion then I get errors from bson headers:

/usr/include/libbson-1.0/bson/bson-iter.h: In function ‘uint32_t bson_iter_utf8_len_unsafe(const bson_iter_t*)’:
/usr/include/libbson-1.0/bson/bson-endian.h:87:33: error: conversion to ‘int32_t’ {aka ‘int’} from ‘uint32_t’ {aka ‘unsigned int’} may change the sign of the result [-Werror=sign-conversion]
   87 | #define BSON_UINT32_FROM_LE(v) ((uint32_t) v)
      |                                ^~~~~~~~~~~~

Comment by LIU Hao [ 26/Jan/24 ]

Thanks! This works for me:

project('mongoc_version', 'c')
libmongoc_dep = dependency('libmongoc-1.0', version : '>=1.21.0')
exe = executable('mongoc_version', 'main.c', dependencies : libmongoc_dep)

Comment by Roberto Sanchez [ 25/Jan/24 ]

lh_mouse@126.com, have you considered making use of the facilities available in SCons and meson for this particular need?

From the SCons documentation:

I'm already using ldconfig, pkg-config, gtk-config, etc. Do I have to rewrite their logic to use SCons?

SCons provides explicit support for getting information from programs like ldconfig and pkg-config. The relevant method is ParseConfig(), which executes a *-config command, parses the returned flags, and puts them in the environment through which the ParseConfig() method is called:

env.ParseConfig('pkg-config --cflags --libs libxml')

If you need to provide some special-purpose processing, you can supply a function to process the flags and apply them to the environment in any way you want.

From the Meson documentation:

Meson also allows one to get variables that are defined in a pkg-config file. This can be done by using the dep.get_pkgconfig_variable() function.

zdep_prefix = zdep.get_pkgconfig_variable('prefix')

These variables can also be redefined by passing the define_variable parameter, which might be useful in certain situations:

zdep_prefix = zdep.get_pkgconfig_variable('libdir', define_variable: ['prefix', '/tmp'])

The dependency detector works with all libraries that provide a pkg-config file.

Consuming the libmongoc and libbson in this way will also ensure that you have the correct linker flags, preprocessor definitions, etc.

Please let us know if this works for you.

Comment by LIU Hao [ 24/Jan/24 ]

Proposed solution

Install two additional symbol links, which point to directories of the latest version:

bson    =>  libbson-1.0/bson
mongoc  =>  libmongoc-1.0/mongoc

Comment by PM Bot [ 12/Jan/24 ]

Hi lh_mouse@126.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Wed Feb 07 21:22:01 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.