mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.51k stars 1.6k forks source link

linux mix build with cmake failed. #13430

Open yuuch opened 1 month ago

yuuch commented 1 month ago

Describe the bug My project requires the use of duckdb, which is compiled using CMake. My project is built using Meson, but when I try to compile it using Meson on my Linux system, it throws an error: "Can't link non-PIC static library 'duckdb_static' into shared library duckdb. "

I have tried to use cmake to compile duckdb alone, it works well. I read the CMake files for duckdb, and it has set(CMAKE_POSITION_INDEPENDENT_CODE ON) enabled for global PIC, so I think this might be a Meson bug. I read the generated meson.build for duckdb, the duckdb_static is not pic. generated meson.build

 duckdb_static = static_library(
  'duckdb_static',
  # ignore something else
  c_args : [],
  pic : false
)

Then, I read through the Meson code and found that it defaults to enabling PIC for OBJECT targets. So, I'm wondering if it's possible to also enable PIC for STATIC targets in Meson? On my machine, I modify the meson code ,it works well now, so can i pull request to this project?

--- a/mesonbuild/cmake/interpreter.py
+++ b/mesonbuild/cmake/interpreter.py
@@ -335,7 +335,7 @@ class ConverterTarget:
             self.compile_opts[i] = temp

         # Make sure to force enable -fPIC for OBJECT libraries
-        if self.type.upper() == 'OBJECT_LIBRARY':
+        if self.type.upper() in ['OBJECT_LIBRARY', 'STATIC_LIBRARY']:
             self.pie = True

To Reproduce here is the directory structure:

├── meson.build
└── subprojects
    └── duckdb

meson.build file:

cmake = import('cmake')
opt_var = cmake.subproject_options()
opt_var.add_cmake_defines({'G': 'Ninja'})
opt_var.set_override_option('cpp_std', 'c++17')
sub_proj = cmake.subproject('duckdb', options: opt_var)
duckdblib  = sub_proj.target('duckdb')
duckdb_inc = sub_proj.include_directories('duckdb')
libduckdb_dep = declare_dependency(include_directories: duckdb_inc, link_with: duckdblib)

Expected behavior compile success

system parameters Ubuntu 22.04 meson master

dcbaker commented 1 month ago

This looks like a Meson bug, we should be honoring the Meson global configuration for pic based on the Meson b_staticpic option, which helps to ensure that all subprojects and are built with the same PIC-ness as the superproject is.

dcbaker commented 1 month ago

This looks like a larger issue that we don't look at the builtin options inside CMake at all. This makes me wonder how we're not running into more of these issues...

yuuch commented 1 month ago

This looks like a larger issue that we don't look at the builtin options inside CMake at all. This makes me wonder how we're not running into more of these issues...

Yes, there are lots of things to do, if you want to consider all the builtin options inside CMake.