Skip to content

Commit 1142de3

Browse files
committed
Patch #527027: Allow building python as shared library.
1 parent eddd68d commit 1142de3

File tree

6 files changed

+599
-481
lines changed

6 files changed

+599
-481
lines changed

Makefile.pre.in

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ PYTHONFRAMEWORKDIR= @PYTHONFRAMEWORKDIR@
108108
PYTHONFRAMEWORKPREFIX= @PYTHONFRAMEWORKPREFIX@
109109
PYTHONFRAMEWORKINSTALLDIR= @PYTHONFRAMEWORKINSTALLDIR@
110110

111+
# Environment to run shared python without installed libraries
112+
RUNSHARED= @RUNSHARED@
113+
111114
# Modes for directories, executables and data files created by the
112115
# install process. Default to user-only-writable for all file types.
113116
DIRMODE= 755
@@ -136,6 +139,7 @@ LDLIBRARY= @LDLIBRARY@
136139
BLDLIBRARY= @BLDLIBRARY@
137140
DLLLIBRARY= @DLLLIBRARY@
138141
LDLIBRARYDIR= @LDLIBRARYDIR@
142+
INSTSONAME= @INSTSONAME@
139143

140144

141145
LIBS= @LIBS@
@@ -292,20 +296,20 @@ LIBRARY_OBJS= \
292296
all: $(BUILDPYTHON) oldsharedmods sharedmods
293297

294298
# Build the interpreter
295-
$(BUILDPYTHON): Modules/$(MAINOBJ) $(LDLIBRARY)
299+
$(BUILDPYTHON): Modules/$(MAINOBJ) $(LIBRARY) $(LDLIBRARY)
296300
$(LINKCC) $(LDFLAGS) $(LINKFORSHARED) -o $@ \
297301
Modules/$(MAINOBJ) \
298302
$(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
299303

300304
platform: $(BUILDPYTHON)
301-
./$(BUILDPYTHON) -E -c 'import sys ; from distutils.util import get_platform ; print get_platform()+"-"+sys.version[0:3]' >platform
305+
$(RUNSHARED) ./$(BUILDPYTHON) -E -c 'import sys ; from distutils.util import get_platform ; print get_platform()+"-"+sys.version[0:3]' >platform
302306

303307

304308
# Build the shared modules
305309
sharedmods: $(BUILDPYTHON)
306310
case $$MAKEFLAGS in \
307-
*-s*) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' ./$(BUILDPYTHON) -E $(srcdir)/setup.py -q build;; \
308-
*) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' ./$(BUILDPYTHON) -E $(srcdir)/setup.py build;; \
311+
*-s*) $(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' ./$(BUILDPYTHON) -E $(srcdir)/setup.py -q build;; \
312+
*) $(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' ./$(BUILDPYTHON) -E $(srcdir)/setup.py build;; \
309313
esac
310314

311315
# buildno should really depend on something like LIBRARY_SRC
@@ -333,18 +337,11 @@ $(LIBRARY): $(LIBRARY_OBJS)
333337
$(AR) cr $@ $(MODOBJS)
334338
$(RANLIB) $@
335339

336-
# This rule is only here for DG/UX and BeOS!!!
337-
libpython$(VERSION).so: $(LIBRARY)
338-
case `uname -s | tr -d '/ ' | tr '[A-Z]' '[a-z]'` in \
339-
*dgux*) \
340-
test -d dgux || mkdir dgux; \
341-
(cd dgux;ar x ../$^;ld -G -o ../$@ * ); \
342-
/bin/rm -rf ./dgux \
343-
;; \
344-
beos) \
345-
$(AR) so $(LIBRARY) $@ \
346-
;; \
347-
esac
340+
libpython$(VERSION).so: $(LIBRARY_OBJS)
341+
$(LDSHARED) -o $@ $(LIBRARY_OBJS) $(LIBC) $(LIBM)
342+
343+
libpython$(VERSION).sl: $(LIBRARY_OBJS)
344+
$(LDSHARED) -o $@ $(LIBRARY_OBJS) $(LIBC) $(LIBM)
348345

349346
# This rule is here for OPENSTEP/Rhapsody/MacOSX
350347
$(PYTHONFRAMEWORKDIR)/Versions/$(VERSION)/$(PYTHONFRAMEWORK): $(LIBRARY) $(PYTHONFRAMEWORKDIR)
@@ -420,10 +417,10 @@ Parser/metagrammar.o: $(srcdir)/Parser/metagrammar.c
420417
Python/compile.o Python/symtable.o: $(GRAMMAR_H)
421418

422419
Python/getplatform.o: $(srcdir)/Python/getplatform.c
423-
$(CC) -c $(CFLAGS) $(CPPFLAGS) -DPLATFORM='"$(MACHDEP)"' -o $@ $(srcdir)/Python/getplatform.c
420+
$(CC) -c $(CFLAGS) $(CPPFLAGS) $(CFLAGSFORSHARED) -DPLATFORM='"$(MACHDEP)"' -o $@ $(srcdir)/Python/getplatform.c
424421

425422
Python/importdl.o: $(srcdir)/Python/importdl.c
426-
$(CC) -c $(CFLAGS) $(CPPFLAGS) -I$(DLINCLDIR) -o $@ $(srcdir)/Python/importdl.c
423+
$(CC) -c $(CFLAGS) $(CPPFLAGS) $(CFLAGSFORSHARED) -I$(DLINCLDIR) -o $@ $(srcdir)/Python/importdl.c
427424

428425
Objects/unicodectype.o: $(srcdir)/Objects/unicodectype.c \
429426
$(srcdir)/Objects/unicodetype_db.h
@@ -486,7 +483,7 @@ $(LIBRARY_OBJS) $(MODOBJS) Modules/$(MAINOBJ): $(PYTHON_HEADERS)
486483
# Test the interpreter (twice, once without .pyc files, once with)
487484
TESTOPTS= -l
488485
TESTPROG= $(srcdir)/Lib/test/regrtest.py
489-
TESTPYTHON= ./$(BUILDPYTHON) -E -tt
486+
TESTPYTHON= $(RUNSHARED) ./$(BUILDPYTHON) -E -tt
490487
test: all platform
491488
-find $(srcdir)/Lib -name '*.py[co]' -print | xargs rm -f
492489
-$(TESTPYTHON) $(TESTPROG) $(TESTOPTS)
@@ -556,8 +553,8 @@ altbininstall: $(BUILDPYTHON)
556553
fi; \
557554
done
558555
$(INSTALL_PROGRAM) $(BUILDPYTHON) $(BINDIR)/python$(VERSION)$(EXE)
559-
if test -f libpython$(VERSION).so; then \
560-
$(INSTALL_DATA) libpython$(VERSION).so $(LIBDIR); \
556+
if test -f libpython$(VERSION)$(SO); then \
557+
$(INSTALL_SHARED) libpython$(VERSION)$(SO) $(LIBDIR)/$(INSTSONAME); \
561558
else true; \
562559
fi
563560
if test -f "$(DLLLIBRARY)"; then \
@@ -640,10 +637,10 @@ libinstall: $(BUILDPYTHON) $(srcdir)/Lib/$(PLATDIR)
640637
done; \
641638
done
642639
$(INSTALL_DATA) $(srcdir)/LICENSE $(LIBDEST)/LICENSE.txt
643-
PYTHONPATH=$(LIBDEST) \
640+
PYTHONPATH=$(LIBDEST) $(RUNSHARED) \
644641
./$(BUILDPYTHON) -tt $(LIBDEST)/compileall.py -x badsyntax \
645642
$(LIBDEST)
646-
PYTHONPATH=$(LIBDEST) \
643+
PYTHONPATH=$(LIBDEST) $(RUNSHARED) \
647644
./$(BUILDPYTHON) -O $(LIBDEST)/compileall.py -x badsyntax $(LIBDEST)
648645

649646
# Create the PLATDIR source directory, if one wasn't distributed..
@@ -686,12 +683,12 @@ libainstall: all
686683
else true; \
687684
fi; \
688685
done
689-
@if test -d $(LDLIBRARY); then :; else \
686+
@if test -d $(LIBRARY); then :; else \
690687
if test "$(PYTHONFRAMEWORKDIR)" = no-framework; then \
691-
$(INSTALL_DATA) $(LDLIBRARY) $(LIBPL)/$(LDLIBRARY) ; \
692-
$(RANLIB) $(LIBPL)/$(LDLIBRARY) ; \
688+
$(INSTALL_DATA) $(LIBRARY) $(LIBPL)/$(LIBRARY) ; \
689+
$(RANLIB) $(LIBPL)/$(LIBRARY) ; \
693690
else \
694-
echo Skip install of $(LDLIBRARY) - use make frameworkinstall; \
691+
echo Skip install of $(LIBRARY) - use make frameworkinstall; \
695692
fi; \
696693
fi
697694
$(INSTALL_DATA) Modules/config.c $(LIBPL)/config.c
@@ -733,7 +730,8 @@ libainstall: all
733730
# Install the dynamically loadable modules
734731
# This goes into $(exec_prefix)
735732
sharedinstall:
736-
./$(BUILDPYTHON) -E $(srcdir)/setup.py install \
733+
$(RUNSHARED) ./$(BUILDPYTHON) -E $(srcdir)/setup.py install \
734+
--prefix=$(prefix) \
737735
--install-scripts=$(BINDIR) \
738736
--install-platlib=$(DESTSHARED)
739737

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ Todd R. Palmer
335335
Dan Parisien
336336
Harri Pasanen
337337
Randy Pausch
338+
Ondrej Palkovsky
338339
Marcel van der Peijl
339340
Samuele Pedroni
340341
Steven Pemberton

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ Tools/Demos
8989

9090
Build
9191

92+
- On Unix, a shared libpython2.3.so can be created with --enable-shared.
93+
9294
- References to the CACHE_HASH and INTERN_STRINGS preprocessor symbols
9395
were eliminated. They were always defined, and the internal features
9496
they enabled stopped being experimental long ago.

README

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,15 @@ Linker (ld) libraries and flags for threads
558558
(jph@emilia.engr.sgi.com)
559559

560560

561+
Building a shared libpython
562+
---------------------------
563+
564+
Starting with Python 2.3, the majority of the interpreter can be built
565+
into a shared library, which can then be used by the interpreter
566+
executable, and by applications embedding Python. To enable this feature,
567+
configure with --enable-shared.
568+
569+
561570
Configuring additional built-in modules
562571
---------------------------------------
563572

0 commit comments

Comments
 (0)