Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmake): improve shared libs and pkg config files #1842

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Apteryks
Copy link

@Apteryks Apteryks commented May 6, 2024

What type of PR is this?

/kind bug

Any specific area of the project related to this PR?

/area build

/area libscap-engine-gvisor

/area libpman

/area libsinsp

What this PR does / why we need it:

It sanitizes the generated pkg-config files of libscap.pc and libsinsp.pc, and add missing includes needed to build as shared libraries.

Which issue(s) this PR fixes:

Fixes #1820

Special notes for your reviewer:

Also see #1825, which is not addressed by this PR but can be easily worked around.

Does this PR introduce a user-facing change?:

NONE

@poiana
Copy link
Contributor

poiana commented May 6, 2024

Welcome @Apteryks! It looks like this is your first PR to falcosecurity/libs 🎉

@Apteryks
Copy link
Author

Apteryks commented May 8, 2024

I've now successfully built sysdig against a shared library falcosecurity-libs distinct package with this series, on GNU Guix.

@FedeDP
Copy link
Contributor

FedeDP commented May 9, 2024

Hi! Thanks for this PR!
I will invoke our cmake experts for a review :)
To me, changes look good btw!

cc @federico-sysdig @geraldcombs

EDIT: i will edit the PR title and body to follow our template (as per our commit convention: https://github.com/falcosecurity/.github/blob/main/CONTRIBUTING.md#commit-convention)

@FedeDP FedeDP changed the title Fix shared lib and pkg config files fix(cmake): fixed shared libs and pkg config files May 9, 2024
Copy link
Contributor

@federico-sysdig federico-sysdig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an interesting PR and there's excellent work on the corrections for the generated pkgconfig files.
I do believe that something else is desirable for the management of the dependent libraries other than the proposed generator expressions.

I'm curious; I have tried to implement the ability to integrate falcosecurity/libs in a client project through find_package, which is a better, more CMake-oriented, way to use a library. This is not to say that pkgconfig should be left behind, just another option. What are your thoughts on that?

userspace/libscap/engine/gvisor/CMakeLists.txt Outdated Show resolved Hide resolved
userspace/libsinsp/CMakeLists.txt Outdated Show resolved Hide resolved
@Apteryks
Copy link
Author

It's an interesting PR and there's excellent work on the corrections for the generated pkgconfig files. I do believe that something else is desirable for the management of the dependent libraries other than the proposed generator expressions.

I'll get to these in a bit.

I'm curious; I have tried to implement the ability to integrate falcosecurity/libs in a client project through find_package, which is a better, more CMake-oriented, way to use a library. This is not to say that pkgconfig should be left behind, just another option. What are your thoughts on that?

My immediate thought on this is that a FindFalcosecurityLibs.cmake or similarly named module implementing the logic forfind_package could be implemented viapkg_check_modules, with the obvious drawback that it adds a requirement on a pkg-config binary being available in the environment. Whatever the implementation detail chosen for an eventual CMake-based find_package module for this project, I think it can and should remain a distinct effort from this PR, which focuses on improving the pkg-config generated files :-).

@federico-sysdig
Copy link
Contributor

Whatever the implementation detail chosen for an eventual CMake-based find_package module for this project, I think it can and should remain a distinct effort from this PR, which focuses on improving the pkg-config generated files :-).

Of course, I wasn't suggesting to change the scope of this PR.

@geraldcombs
Copy link
Contributor

The "build-shared-libs-macos-amd64" job is failing because CFlags in libscap.pc no longer includes -I/opt/homebrew/include:

In file included from /tmp/libs-test/include/falcosecurity/libsinsp/sinsp.h:45:
In file included from /tmp/libs-test/include/falcosecurity/libscap/scap.h:66:
/tmp/libs-test/include/falcosecurity/libscap/uthash_ext.h:24:10: fatal error: 'uthash.h' file not found
#include "uthash.h"
         ^~~~~~~~~~

@Apteryks Apteryks force-pushed the fix-shared-lib-and-pkg-config-files branch from 816a718 to 5b0dea2 Compare May 11, 2024 00:51
@Apteryks
Copy link
Author

The "build-shared-libs-macos-amd64" job is failing because CFlags in libscap.pc no longer includes -I/opt/homebrew/include:

In file included from /tmp/libs-test/include/falcosecurity/libsinsp/sinsp.h:45:
In file included from /tmp/libs-test/include/falcosecurity/libscap/scap.h:66:
/tmp/libs-test/include/falcosecurity/libscap/uthash_ext.h:24:10: fatal error: 'uthash.h' file not found
#include "uthash.h"
         ^~~~~~~~~~

The Cflags of the libscap.pc file shouldn't have changed; I've only added two new entries to them. For example, on my machine, the old copy (master) looks like:

 $ cat ./libscap/libscap.pc 
prefix=${pcfiledir}/../..
libdir=${prefix}/lib64
includedir=${prefix}/include

Name: libscap
Description: lib for System CAPture
Version: 0.0.0

Libs: -L${libdir} -L/gnu/store/65iv1fc7z68cammywngh0dn5995ylbmk-profile/lib -L/gnu/store/65iv1fc7z68cammywngh0dn5995ylbmk-profile/lib -L/gnu/store/65iv1fc7z68cammywngh0dn5995ylbmk-profile/lib -L/gnu/store/65iv1fc7z68cammywngh0dn5995ylbmk-profile/lib -L/gnu/store/65iv1fc7z68cammywngh0dn5995ylbmk-profile/lib -lscap -lz -lprotobuf -ljsoncpp -lscap_engine_nodriver -lscap_engine_test_input -lscap_engine_source_plugin -lscap_engine_kmod -lscap_engine_bpf -lelf -lscap_engine_modern_bpf -lpman
Cflags: -I${includedir}/falcosecurity/libscap

With this change it now reads:

$ cat ../build/libscap/libscap.pc
prefix=/usr/local
libdir=${prefix}/lib64
includedir=${prefix}/include

Name: libscap
Description: lib for System CAPture
Version: 0.0.0

Requires: zlib
Libs: -L${libdir} -L{libdir}/falcosecurity/libscap  -lscap -lscap_engine_nodriver -lscap_engine_test_input -lscap_engine_source_plugin -lscap_engine_kmod -lscap_engine_bpf -lscap_engine_modern_bpf
Cflags: -I${includedir}/falcosecurity/libscap -I${includedir}/falcosecurity/driver -I${includedir}/falcosecurity

Probably this header was found via libsinsp.pc, which was capturing a lot of build-specific, non-installed directories, which I think shouldn't be baked in the generated .pc file.

Previously, it looked like:

$ cat ./libsinsp/libsinsp.pc 
prefix=${pcfiledir}/../..
libdir=${prefix}/lib64
includedir=${prefix}/include

Name: libsinsp
Description: lib for System INSPection
Version: 0.0.0

Requires: libscap
Libs: -L${libdir} -lsinsp -L/gnu/store/65iv1fc7z68cammywngh0dn5995ylbmk-profile/lib -lz -lcurl -ljsoncpp -lre2 -lcares -lgRPC::grpc++ -lgRPC::grpc -lgRPC::gpr -lprotobuf -lcares -lrt -lanl -lssl -lcrypto -ldl -lpthread
Cflags: -I${includedir}/falcosecurity/libsinsp -I/home/maxim/src/falcosecurity-libs -I/home/maxim/src/falcosecurity-libs/userspace -I/home/maxim/src/falcosecurity-libs/userspace/libscap -I/home/maxim/src/falcosecurity-libs/build_orig -I/home/maxim/src/falcosecurity-libs/build_orig/driver/src -I/gnu/store/65iv1fc7z68cammywngh0dn5995ylbmk-profile/include/tbb -I/gnu/store/65iv1fc7z68cammywngh0dn5995ylbmk-profile/include

Now, it looks like:

$ cat ../build/libsinsp/libsinsp.pc 
prefix=/usr/local
libdir=${prefix}/lib64
includedir=${prefix}/include

Name: libsinsp
Description: lib for System INSPection
Version: 0.0.0

Requires: libscap jsoncpp libcares gpr grpc grpc++ protobuf libcrypto libssl
Requires.private: libcurl re2 tbb
Libs: -L${libdir} -lsinsp -lrt -lanl -ldl -lpthread
Cflags: -I${includedir}/falcosecurity/libsinsp -I${includedir}/falcosecurity/driver -I${includedir}/falcosecurity

userspace/libscap/libscap.pc.in Outdated Show resolved Hide resolved
userspace/libsinsp/libsinsp.pc.in Outdated Show resolved Hide resolved
@leogr
Copy link
Member

leogr commented Jan 7, 2025

Any updates on this?

Since 0.20 is too close, moving this to
/milestone 0.21.0

@poiana poiana modified the milestones: 0.20.0, 0.21.0 Jan 7, 2025
@Apteryks
Copy link
Author

Apteryks commented Jan 22, 2025

test-scap-{amd64,arm64} tests are failing with:

make[3]: *** No rule to make target 'protobuf-prefix/src/protobuf/target/lib/libprotobuf.a', needed by 'test/libscap/libscap_test'. Stop.

Care to give it a look @Apteryks ?

Sorry for the long wait, I'll take a look at it now.

@FedeDP
Copy link
Contributor

FedeDP commented Jan 22, 2025

No problem :)

@Apteryks Apteryks force-pushed the fix-shared-lib-and-pkg-config-files branch from f74a20d to c96e0f7 Compare January 22, 2025 12:16
@poiana poiana added size/M and removed size/L labels Jan 22, 2025
@Apteryks
Copy link
Author

Lot's have changed (for the better it seems); there's less in this PR following a rebase. I've been conservative in my edits in the hope that even the most exotic platforms shouldn't be impacted in the CI; let's see.

This is to so that includes work whether using e.g. #include <scap.h>
or #include <libscap/scap.h>, and likewise for libsinp.

* userspace/libsinsp/libsinsp.pc.in (Cflags): Add include directive
for falcosecurity/driver.
* userspace/libscap/libscap.pc.in: Likewise.  Also add include
directive for uthash.

Signed-off-by: Maxim Cournoyer <[email protected]>
@Apteryks Apteryks force-pushed the fix-shared-lib-and-pkg-config-files branch from c96e0f7 to ddb89c9 Compare January 22, 2025 13:42
@Apteryks
Copy link
Author

Lot's have changed (for the better it seems); there's less in this PR following a rebase. I've been conservative in my edits in the hope that even the most exotic platforms shouldn't be impacted in the CI; let's see.

Actually, the pkg-config files Libs: entries are back to contain a bit too much, which is correct for the static build case but leads to overlinking on GNU/Linux, which is not fatal. I'm not sure how to fix this cleanly for both builds to pass, so I guess I'll leave it at that.

@Apteryks Apteryks force-pushed the fix-shared-lib-and-pkg-config-files branch from ddb89c9 to d07beb8 Compare January 23, 2025 05:08
@poiana poiana added size/L and removed size/M labels Jan 23, 2025
@Apteryks Apteryks changed the title fix(cmake): fixed shared libs and pkg config files fix(cmake): improve shared libs and pkg config files Jan 23, 2025
@Apteryks
Copy link
Author

Actually, the pkg-config files Libs: entries are back to contain a bit too much, which is correct for the static build case but leads to overlinking on GNU/Linux, which is not fatal. I'm not sure how to fix this cleanly for both builds to pass, so I guess I'll leave it at that.

The latest revision resolves that. It's a bit hacky in the add_pkgconfig_library macro, but to be honest, it already was :-). The result looks great and I could build sysdig (which a few edits there to support the latest falcosecurity-libs).

I'm sharing the content of the freshly built pkg-config files, for a build configured with the flags: cmake -DBUILD_SHARED_LIBS=ON -DUSE_BUNDLED_DEPS=OFF -DCMAKE_INSTALL_PREFIX=$PWD/install "-DBUILD_DRIVER=OFF" "-DENABLE_DKMS=OFF" "-DBUILD_LIBSCAP_MODERN_BPF=ON":

$ cat install/lib64/pkgconfig/libscap.pc
prefix=/home/maxim/src/falcosecurity-libs/buildc/install
libdir=${prefix}/lib64
includedir=${prefix}/include/falcosecurity

Name: libscap
Description: lib for System CAPture
Version: 0.21.0-19+d07beb8

# Note: jsoncpp and protobuf are required by scap_engine_gvisor, which
# currently lacks its own pkg-config file.
Requires: jsoncpp libpman protobuf
Requires.private: zlib
Libs: -L${libdir}  -lscap -lscap_event_schema -lscap_platform -lscap_engine_nodriver -lscap_engine_test_input -lscap_engine_source_plugin -lscap_engine_kmod -lscap_event_schema -lscap_platform -lscap_engine_bpf -lscap_event_schema -lscap_platform -lelf -lscap_engine_modern_bpf -lscap_engine_gvisor -lscap_event_schema
Cflags: -I${includedir} -I${includedir}/libscap -I${includedir}/driver -I/gnu/store/4b30xdqjj81dcyl5p518rp33vlm2iag8-profile/include

$ cat install/lib64/pkgconfig/libsinsp.pc
prefix=/home/maxim/src/falcosecurity-libs/buildc/install
libdir=${prefix}/lib64
includedir=${prefix}/include/falcosecurity

Name: libsinsp
Description: lib for System INSPection
Version: 0.21.0-19+d07beb8

Requires: libscap jsoncpp libcares gpr grpc grpc++ protobuf libcares libcrypto libssl
Requires.private: libcurl re2 tbb
Libs: -L${libdir}  -lsinsp
Cflags: -I${includedir} -I${includedir}/libsinsp -I${includedir}/driver -I/gnu/store/4b30xdqjj81dcyl5p518rp33vlm2iag8-profile/include/tbb -I/gnu/store/4b30xdqjj81dcyl5p518rp33vlm2iag8-profile/include

$ cat install/lib64/pkgconfig/libpman.pc
prefix=/home/maxim/src/falcosecurity-libs/buildc/install
libdir=${prefix}/lib64
includedir=${prefix}/include

Name: libpman
Description: Utility library for BPF probes
Version: 0.21.0-19+d07beb8

Requires: libbpf zlib
Libs: -L${libdir} -lpman -lscap_event_schema -lscap_platform
Cflags: -I${includedir}

@Apteryks Apteryks force-pushed the fix-shared-lib-and-pkg-config-files branch from d07beb8 to 39137a6 Compare January 23, 2025 05:31
* userspace/libpman/libpman.pc.in: New file.
* userspace/libpman/CMakeLists.txt: Configure and install it along the
libpman header.

Signed-off-by: Maxim Cournoyer <[email protected]>
@Apteryks Apteryks force-pushed the fix-shared-lib-and-pkg-config-files branch from 39137a6 to 209ebb6 Compare January 23, 2025 14:46
@Apteryks
Copy link
Author

I think that's ready to be reviewed again.

@Apteryks
Copy link
Author

Only the sinsp example with pkg-config test appears to fail when building as a shared library on Linux and MacOS. I'll look into it; perhaps the install directory needs to be added to PKG_CONFIG_PATH.

@Apteryks
Copy link
Author

Apteryks commented Jan 24, 2025

These tests pass locally, on my Guix System. I needed to use lib64 instead of lib in the PKG_CONFIG_PATH (and LIBRARY_PATH) though, so perhaps the paths in the ci.yml file need a similar adjustment?

@Apteryks Apteryks force-pushed the fix-shared-lib-and-pkg-config-files branch 2 times, most recently from 52a064f to 7f2f802 Compare January 24, 2025 14:41
@Apteryks
Copy link
Author

Phew. I think my last push hopefully addresses the remaining test failures. I've made the Requires and Requires.private for libscap conditionally computed like for libsinsp, and dropped what seemed like an extraneous protobuf link dependency I had added to the scap unit test.

@Apteryks Apteryks force-pushed the fix-shared-lib-and-pkg-config-files branch from 7f2f802 to 7a5dde2 Compare January 24, 2025 14:55
The generated pkg-config files of libscap and libsinsp now makes use
of pkg-config Requires and Requires.static fields, which should reduce
over-linking when linking to shared libraries.

* cmake/modules/BuildPkgConfigDependencies.cmake
(add_pkgconfig_library): Add debug messages and fix an issue where
IN_LIST had no effect.
* cmake/modules/libscap.cmake: Move pkgconfig dependency computation
to, pkg-config file configuration to...
* userspace/libscap/CMakeLists.txt: ... here, conditionally
accumulating Requires and Requires.private values.
* userspace/libscap/libscap.pc.in (prefix): Set directly to
CMAKE_INSTALL_PREFIX.
(Requires, Requires.private): New fields.
* userspace/libsinsp/CMakeLists.txt: Separate libraries into
pkg-config Requires and Requires.private lists.  Add the pkg-config
requirements to the ignored link dependencies, since these are now
recorded as Requires in the pkg-config file.
* userspace/libsinsp/libsinsp.pc.in (Requires): Add
@LIBSINSP_REQUIRES@.
(Requires.private): New field.
(Libs): Remove -lsinsp, automatically computed in SINSP_PKG_CONFIG_LIBS.

Signed-off-by: Maxim Cournoyer <[email protected]>
* driver/CMakeLists.txt (DRIVER_SOURCES): Add missing headers.
* userspace/libsinsp/test/CMakeLists.txt (unit-test-libsinsp): Link to
libgrpc++ to avoid a missing DSO error.
* userspace/libscap/CMakeLists.txt: Do not hardcode STATIC type for
scap_event_schema and scap_platform libraries, so as to install
them (they are referenced in the pkg-config files).

Fixes: falcosecurity#1820
Signed-off-by: Maxim Cournoyer <[email protected]>
@Apteryks Apteryks force-pushed the fix-shared-lib-and-pkg-config-files branch from 7a5dde2 to d7f10d4 Compare January 24, 2025 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

[-DBUILD_SHARED_LIBS=ON] "error: driver/syscall_compat_x86_64.h: No such file or directory" at build time
6 participants