Skip to content

Commit 3d49360

Browse files
committed
libdrgn: combine libdrgn and _drgn Python extension into one .so
For the upcoming plugin system, libdrgn needs to call Python extension code and vice versa. Doing this from two separate libraries would be messy, so let's combine them into one library. liblldb seems to do the same thing, so I don't think this is too crazy. Signed-off-by: Omar Sandoval <[email protected]>
1 parent 20e8759 commit 3d49360

File tree

5 files changed

+89
-73
lines changed

5 files changed

+89
-73
lines changed

libdrgn/Makefile.am

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ ACLOCAL_AMFLAGS = -I m4
55

66
.DELETE_ON_ERROR:
77

8-
AM_CPPFLAGS = -I $(top_srcdir)/include -D_GNU_SOURCE
8+
AM_CPPFLAGS = -I $(top_srcdir)/include -iquote $(dir $@) \
9+
-iquote $(srcdir)/$(dir $@) -D_GNU_SOURCE
910
AM_CFLAGS = $(WARN_CFLAGS) $(SANITIZER_CFLAGS)
1011
AM_LDFLAGS= $(SANITIZER_LDFLAGS)
1112

@@ -134,7 +135,7 @@ libdrgnimpl_la_SOURCES = $(ARCH_DEFS_PYS:_defs.py=.c) \
134135

135136
libdrgnimpl_la_CFLAGS = $(AM_CFLAGS) -fvisibility=hidden $(OPENMP_CFLAGS) \
136137
$(elfutils_CFLAGS)
137-
libdrgnimpl_la_CPPFLAGS = $(AM_CPPFLAGS) -iquote .
138+
libdrgnimpl_la_CPPFLAGS = $(AM_CPPFLAGS)
138139
libdrgnimpl_la_LIBADD = $(OPENMP_LIBS) $(elfutils_LIBS) -lm
139140

140141
if WITH_DEBUGINFOD
@@ -152,6 +153,36 @@ libdrgnimpl_la_CFLAGS += $(libkdumpfile_CFLAGS)
152153
libdrgnimpl_la_LIBADD += $(libkdumpfile_LIBS)
153154
endif
154155

156+
if ENABLE_PYTHON
157+
BUILT_SOURCES += python/docstrings.h
158+
159+
libdrgnimpl_la_SOURCES += python/constants.c \
160+
python/debug_info_options.c \
161+
python/docstrings.c \
162+
python/docstrings.h \
163+
python/drgnpy.h \
164+
python/error.c \
165+
python/helpers.c \
166+
python/language.c \
167+
python/main.c \
168+
python/module.c \
169+
python/module_section_addresses.c \
170+
python/object.c \
171+
python/platform.c \
172+
python/program.c \
173+
python/stack_trace.c \
174+
python/symbol.c \
175+
python/symbol_index.c \
176+
python/test.c \
177+
python/thread.c \
178+
python/type.c \
179+
python/type_kind_set.c \
180+
python/util.c
181+
182+
libdrgnimpl_la_CPPFLAGS += $(PYTHON_CPPFLAGS)
183+
libdrgnimpl_la_LIBADD += $(PYTHON_LIBS)
184+
endif
185+
155186
%: %.strswitch build-aux/gen_strswitch.py build-aux/codegen_utils.py
156187
$(AM_V_GEN)$(PYTHON) $(word 2, $^) -o $@ $<
157188

@@ -167,52 +198,6 @@ drgn_section_name_to_index.inc: build-aux/gen_elf_sections.py build-aux/gen_strs
167198
elf_sections.h: build-aux/gen_elf_sections.py build-aux/codegen_utils.py
168199
$(AM_V_GEN)$(PYTHON) $< -H > $@
169200

170-
lib_LTLIBRARIES = libdrgn.la
171-
172-
libdrgn_la_SOURCES =
173-
libdrgn_la_LDFLAGS = $(AM_LDFLAGS) -version-info 0:0:0
174-
libdrgn_la_LIBADD = libdrgnimpl.la
175-
176-
if ENABLE_PYTHON
177-
BUILT_SOURCES += python/docstrings.h
178-
179-
noinst_LTLIBRARIES += _drgn.la
180-
endif
181-
182-
_drgn_la_SOURCES = python/constants.c \
183-
python/debug_info_options.c \
184-
python/docstrings.c \
185-
python/docstrings.h \
186-
python/drgnpy.h \
187-
python/error.c \
188-
python/helpers.c \
189-
python/language.c \
190-
python/main.c \
191-
python/module.c \
192-
python/module_section_addresses.c \
193-
python/object.c \
194-
python/platform.c \
195-
python/program.c \
196-
python/stack_trace.c \
197-
python/symbol.c \
198-
python/symbol_index.c \
199-
python/test.c \
200-
python/thread.c \
201-
python/type.c \
202-
python/type_kind_set.c \
203-
python/util.c
204-
205-
_drgn_la_CFLAGS = $(AM_CFLAGS) -fvisibility=hidden
206-
_drgn_la_CPPFLAGS = $(AM_CPPFLAGS) $(PYTHON_CPPFLAGS) -iquote $(srcdir)/python \
207-
-iquote python
208-
_drgn_la_LDFLAGS = $(AM_LDFLAGS) -Wl,--exclude-libs,ALL -avoid-version -module \
209-
-shared -rpath $(pkgpyexecdir)
210-
_drgn_la_LIBADD = libdrgnimpl.la
211-
212-
if WITH_LIBKDUMPFILE
213-
_drgn_la_CFLAGS += $(libkdumpfile_CFLAGS)
214-
endif
215-
216201
python/constants.c: drgn.h build-aux/gen_constants.py
217202
$(AM_V_GEN)$(PYTHON) $(word 2, $^) < $< > $@
218203

@@ -225,6 +210,12 @@ python/docstrings.c: ../_drgn.pyi $(drgndoc_docstrings_deps)
225210
python/docstrings.h: ../_drgn.pyi $(drgndoc_docstrings_deps)
226211
$(AM_V_GEN)$(drgndoc_docstrings) -H -m _drgn:drgn $< > $@
227212

213+
lib_LTLIBRARIES = libdrgn.la
214+
215+
libdrgn_la_SOURCES =
216+
libdrgn_la_LDFLAGS = $(AM_LDFLAGS) -version-info 0:0:0
217+
libdrgn_la_LIBADD = libdrgnimpl.la
218+
228219
EXTRA_DIST = $(ARCH_DEFS_PYS) \
229220
$(STRSWITCH_INCS:.inc=.inc.strswitch) \
230221
Doxyfile \

libdrgn/configure.ac

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,7 @@ AC_ARG_ENABLE([python],
5252
[], [enable_python=no])
5353

5454
AM_CONDITIONAL([ENABLE_PYTHON], [test "x$enable_python" != xno])
55-
AM_COND_IF([ENABLE_PYTHON],
56-
[AS_IF([test -z "$PYTHON_CPPFLAGS"],
57-
[prog="import sysconfig
58-
include = sysconfig.get_path('include')
59-
platinclude = sysconfig.get_path('platinclude')
60-
include_paths = [[include]]
61-
if platinclude != include:
62-
include_paths.append(plat_include)
63-
print(' '.join('-I' + path for path in include_paths))"
64-
PYTHON_CPPFLAGS=`"$PYTHON" -c "$prog"`])
65-
AC_SUBST(PYTHON_CPPFLAGS)
66-
AC_MSG_CHECKING([for $PYTHON development headers])
67-
save_CPPFLAGS="$CPPFLAGS"
68-
CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS"
69-
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[#include <Python.h>]])],
70-
[AC_MSG_RESULT([yes])],
71-
[AC_MSG_RESULT([no])
72-
AC_MSG_ERROR(
73-
[Could not compile test program with Python headers.
74-
75-
You may need to install your distribution's Python development package (e.g.,
76-
python3-devel or python3-dev) or specify the location of the Python development
77-
headers by setting the PYTHON_CPPFLAGS environment variable.])])
78-
CPPFLAGS="$save_CPPFLAGS"])
55+
AM_COND_IF([ENABLE_PYTHON], [MY_PYTHON_DEVEL])
7956

8057
PKG_PROG_PKG_CONFIG
8158

libdrgn/m4/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
!/my_c_auto.m4
88
!/my_c_switch_enum.m4
99
!/my_check_va_args_comma_deletion.m4
10+
!/my_python_devel.m4

libdrgn/m4/my_python_devel.m4

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
4+
# Check for Python development files and define PYTHON_CPPFLAGS and PYTHON_LIBS
5+
# accordingly.
6+
AC_DEFUN([MY_PYTHON_DEVEL],
7+
[
8+
AS_IF([test -z "$PYTHON_CPPFLAGS"],
9+
[prog="import sysconfig
10+
include = sysconfig.get_path('include')
11+
platinclude = sysconfig.get_path('platinclude')
12+
include_paths = [[include]]
13+
if platinclude != include:
14+
include_paths.append(plat_include)
15+
print(' '.join('-I' + path for path in include_paths))"
16+
PYTHON_CPPFLAGS=`"$PYTHON" -c "$prog"`])
17+
AC_SUBST(PYTHON_CPPFLAGS)
18+
AS_IF([test -z "$PYTHON_LIBS"],
19+
[prog="import sysconfig
20+
print('-L' + sysconfig.get_config_var('LIBDIR') +
21+
' -lpython' + sysconfig.get_config_var('LDVERSION'))"
22+
PYTHON_LIBS=`"$PYTHON" -c "$prog"`])
23+
AC_SUBST(PYTHON_LIBS)
24+
AC_MSG_CHECKING([for $PYTHON development files])
25+
save_CPPFLAGS="$CPPFLAGS"
26+
save_LIBS="$LIBS"
27+
CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS"
28+
LIBS="$LIBS $PYTHON_LIBS"
29+
AC_LINK_IFELSE([AC_LANG_SOURCE([[
30+
#include <Python.h>
31+
32+
int main(void)
33+
{
34+
Py_Initialize();
35+
}
36+
]])],
37+
[AC_MSG_RESULT([yes])],
38+
[AC_MSG_RESULT([no])
39+
AC_MSG_ERROR(
40+
[Could not compile Python development test program.
41+
42+
You may need to install your distribution's Python development package (e.g.,
43+
python3-devel or python3-dev) or set the PYTHON_CPPFLAGS and PYTHON_LIBS
44+
environment variables.])])
45+
CPPFLAGS="$save_CPPFLAGS"
46+
LIBS="$save_LIBS"
47+
])

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def make(self, *make_args):
123123

124124
def run(self):
125125
self.make()
126-
so = os.path.join(self.build_temp, ".libs/_drgn.so")
126+
so = os.path.join(self.build_temp, ".libs/libdrgn.so")
127127
if self.inplace:
128128
self.copy_file(so, self.get_ext_fullpath("_drgn"))
129129
old_inplace, self.inplace = self.inplace, 0

0 commit comments

Comments
 (0)