Skip to content

Commit 522feb1

Browse files
jcfradrianbroher
andcommitted
Add BUILD_LIBPYTHON_SHARED option, deprecate BUILD_SHARED and BUILD_STATIC
Fixes python-cmake-buildsystem#121 Co-authored-by: Marcel Metz <mmetz@adrian-broher.net> Suggested-by: Marcel Metz <mmetz@adrian-broher.net>
1 parent 4856e34 commit 522feb1

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

CMakeLists.txt

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,28 @@ include(CTest)
3939

4040
# Options
4141
option(DOWNLOAD_SOURCES "Automatically download the Python sources" ON)
42-
option(BUILD_SHARED "Build a shared libpython library" OFF)
43-
option(BUILD_STATIC "Build a static libpython library" ON)
42+
set(_build_libpython_shared_default 0)
43+
set(_build_libpython_shared_desc "Build libpython as a shared library")
44+
45+
if(DEFINED BUILD_SHARED OR DEFINED BUILD_STATIC)
46+
message(AUTHOR_WARNING "Deprecated options `BUILD_SHARED` or `BUILD_STATIC` are set, "
47+
"please change configure options to use `BUILD_LIBPYTHON_SHARED` instead.")
48+
if(BUILD_SHARED AND BUILD_STATIC)
49+
message(AUTHOR_WARNING "Both BUILD_SHARED and BUILD_STATIC options are set to ON. Forcing BUILD_SHARED to OFF")
50+
endif()
51+
if(BUILD_STATIC)
52+
set(_build_libpython_shared_default 0)
53+
set(_build_libpython_shared_desc "Intialized from `BUILD_STATIC` (deprecated)")
54+
elseif(BUILD_SHARED)
55+
set(_build_libpython_shared_default 1)
56+
set(_build_libpython_shared_desc "Intialized from `BUILD_SHARED` (deprecated)")
57+
endif()
58+
message(AUTHOR_WARNING "Unsetting cache variables BUILD_SHARED and BUILD_STATIC")
59+
unset(BUILD_SHARED CACHE)
60+
unset(BUILD_STATIC CACHE)
61+
endif()
62+
63+
option(BUILD_LIBPYTHON_SHARED ${_build_libpython_shared_desc} ${_build_libpython_shared_default})
4464
option(BUILD_EXTENSIONS_AS_BUILTIN "Default all modules as builtin to libpython" OFF)
4565
option(USE_LIB64 "Search for dependencies and install to prefix/lib64 instead of prefix/lib" OFF)
4666
if(WIN32)
@@ -121,11 +141,8 @@ if(UNIX)
121141
set(BUILD_EXTENSIONS_AS_BUILTIN ON CACHE BOOL "Forced to ON${_reason}" FORCE)
122142
message(STATUS "Setting BUILD_EXTENSIONS_AS_BUILTIN to ON${_reason}")
123143

124-
set(BUILD_SHARED OFF CACHE BOOL "Forced to OFF${_reason}" FORCE)
125-
message(STATUS "Setting BUILD_SHARED to OFF${_reason}")
126-
127-
set(BUILD_STATIC ON CACHE BOOL "Forced to ON${_reason}" FORCE)
128-
message(STATUS "Setting BUILD_STATIC to ON${_reason}")
144+
set(BUILD_LIBPYTHON_SHARED OFF CACHE BOOL "Forced to OFF${_reason}" FORCE)
145+
message(STATUS "Setting BUILD_LIBPYTHON_SHARED to OFF${_reason}")
129146
endif()
130147
endif()
131148
else()
@@ -234,6 +251,14 @@ if(PY_VERSION_MAJOR VERSION_GREATER 2)
234251
set(IS_PY2 0)
235252
endif()
236253

254+
# Convenience boolean variables to easily test the type of python library being build
255+
set(BUILD_STATIC 1)
256+
set(BUILD_SHARED 0)
257+
if(BUILD_LIBPYTHON_SHARED)
258+
set(BUILD_STATIC 0)
259+
set(BUILD_SHARED 1)
260+
endif()
261+
237262
# Options depending of the python version
238263
if(IS_PY2)
239264
option(Py_USING_UNICODE "Enable unicode support" ON)

README.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,9 @@ options on the commandline with `-DOPTION=VALUE`, or use the "ccmake" gui.
6868
Download, check MD5 sum and extract python sources in the parent directory.
6969
Source archive is downloaded from http://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
7070

71-
BUILD_SHARED=ON|OFF (defaults to OFF)
72-
BUILD_STATIC=ON|OFF (defaults to ON)
71+
BUILD_LIBPYTHON_SHARED=ON|OFF (defaults to OFF)
7372
Build libpython as a shared library (.so or .dll) or a static library
74-
(.a). At least one of these options must be set to ON - if they are both
75-
set to ON then the Python executable will be linked against the shared
76-
version of libpython.
73+
(.a).
7774

7875
Note that Python extensions are always built as shared libraries. On
7976
Windows it is not possible to build shared .dll extensions against a

0 commit comments

Comments
 (0)