Files
mtgodot-poc/extension/third_party/cpython-2.7.18/CMakeLists.txt
T
shenleiandClaude Opus 5 5fad769ee8 port 2P step 1: vendor CPython 2.7.18 as the mtpython static library
The embedded 40250 script layer needs its own interpreter on five platforms.
2.7 is end-of-life, so nothing can be fetched from the target SDKs and the
source is vendored (official 2.7.18 tarball, trimmed to 25MB; Lib/ stays for
step 3's python27.zip).

cpython-2.7.18/CMakeLists.txt builds one `mtpython` static library from exactly
the 133 objects the reference libpython2.7.a contains, off by default behind
-DMTGODOT_EMBED_PYTHON=ON. The source list and the built-in module table
(config/config.c, 39 entries) are shared; pyconfig.h is a probe result and is
not, so each platform keeps its own under config/<platform>/, regenerated by
tools/py_embed/gen_pyconfig.sh and checked against the shared table.

Three vendor patches, documented in docs/THIRD-PARTY.md: configure/configure.ac
learn arm64 on macOS, and posixmodule.c undefines the process-control calls it
hard-defines past pyconfig.h when building for iOS.

macOS (ctest python.embed: every builtin imports, codecs and pickle work off the
vendored Lib/), Android arm64 and iOS arm64 build. Linux needs a Linux host and
Windows needs a MinGW-vs-MSVC decision; both are noted for step 4.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-23 08:27:35 +09:00

212 lines
6.7 KiB
CMake

# mtpython — CPython 2.7.18 as a static library for the embedded 40250 script layer
# (docs/PORT-PLAN.md 批次 2P step 1). Upstream source is the official Python-2.7.18 tarball
# (sha256 b62c0e79...def43), trimmed of Doc/Demo/Tools/Mac/PCbuild and the stdlib test trees;
# see docs/THIRD-PARTY.md for the two vendor patches.
#
# The source list and the built-in module table (config/config.c, config/Setup.static) are SHARED
# by every platform: they are exactly the objects the reference autotools build puts in
# libpython2.7.a. `pyconfig.h` is NOT shared — it is the result of probing one platform's type
# sizes and system calls, so each platform keeps its own under config/<platform>/.
#
# Windows is not built here yet: it needs PC/pyconfig.h, PC/getpathp.c, Python/dynload_win.c and a
# PC built-in table of its own (nt instead of posix, no fcntl/pwd). Upstream 2.7 supports MSVC there,
# while our Windows gate cross-compiles with MinGW, where PC/pyconfig.h's gnu-win32 branch leaves
# HAVE_UNISTD_H unset and posixmodule.c/dynload_win.c collide with the MinGW headers — so it needs a
# patch series, decided in PORT-PLAN 批次 2P step 4. See docs/THIRD-PARTY.md.
set(MT_PYTHON_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
# MT_PYTHON_PLATFORM is the reference build's MACHDEP, which becomes sys.platform.
if(ANDROID)
set(MT_PYTHON_CONFIG android)
set(MT_PYTHON_PLATFORM linux2)
elseif(IOS OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
set(MT_PYTHON_CONFIG ios)
set(MT_PYTHON_PLATFORM darwin)
elseif(APPLE)
set(MT_PYTHON_CONFIG macos)
set(MT_PYTHON_PLATFORM darwin)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(MT_PYTHON_CONFIG linux)
set(MT_PYTHON_PLATFORM linux2)
else()
message(FATAL_ERROR "mtpython: no pyconfig.h for ${CMAKE_SYSTEM_NAME} yet (see the header of this file)")
endif()
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/config/${MT_PYTHON_CONFIG}/pyconfig.h")
message(FATAL_ERROR "mtpython: config/${MT_PYTHON_CONFIG}/pyconfig.h is missing; generate it with "
"tools/py_embed/gen_pyconfig.sh ${MT_PYTHON_CONFIG}")
endif()
add_library(mtpython STATIC
config/config.c
Modules/_bisectmodule.c
Modules/_codecsmodule.c
Modules/_collectionsmodule.c
Modules/_functoolsmodule.c
Modules/_heapqmodule.c
Modules/_localemodule.c
Modules/_math.c
Modules/_randommodule.c
Modules/_sre.c
Modules/_struct.c
Modules/_weakref.c
Modules/arraymodule.c
Modules/binascii.c
Modules/cPickle.c
Modules/cStringIO.c
Modules/datetimemodule.c
Modules/errnomodule.c
Modules/fcntlmodule.c
Modules/gcmodule.c
Modules/getbuildinfo.c
Modules/getpath.c
Modules/itertoolsmodule.c
Modules/main.c
Modules/mathmodule.c
Modules/operator.c
Modules/posixmodule.c
Modules/pwdmodule.c
Modules/selectmodule.c
Modules/signalmodule.c
Modules/stropmodule.c
Modules/symtablemodule.c
Modules/threadmodule.c
Modules/timemodule.c
Modules/xxsubtype.c
Modules/zipimport.c
Objects/abstract.c
Objects/boolobject.c
Objects/bufferobject.c
Objects/bytearrayobject.c
Objects/bytes_methods.c
Objects/capsule.c
Objects/cellobject.c
Objects/classobject.c
Objects/cobject.c
Objects/codeobject.c
Objects/complexobject.c
Objects/descrobject.c
Objects/dictobject.c
Objects/enumobject.c
Objects/exceptions.c
Objects/fileobject.c
Objects/floatobject.c
Objects/frameobject.c
Objects/funcobject.c
Objects/genobject.c
Objects/intobject.c
Objects/iterobject.c
Objects/listobject.c
Objects/longobject.c
Objects/memoryobject.c
Objects/methodobject.c
Objects/moduleobject.c
Objects/object.c
Objects/obmalloc.c
Objects/rangeobject.c
Objects/setobject.c
Objects/sliceobject.c
Objects/stringobject.c
Objects/structseq.c
Objects/tupleobject.c
Objects/typeobject.c
Objects/unicodectype.c
Objects/unicodeobject.c
Objects/weakrefobject.c
Parser/acceler.c
Parser/bitset.c
Parser/firstsets.c
Parser/grammar.c
Parser/grammar1.c
Parser/listnode.c
Parser/metagrammar.c
Parser/myreadline.c
Parser/node.c
Parser/parser.c
Parser/parsetok.c
Parser/pgen.c
Parser/tokenizer.c
Python/Python-ast.c
Python/_warnings.c
Python/asdl.c
Python/ast.c
Python/bltinmodule.c
Python/ceval.c
Python/codecs.c
Python/compile.c
Python/dtoa.c
Python/dynload_shlib.c
Python/errors.c
Python/formatter_string.c
Python/formatter_unicode.c
Python/frozen.c
Python/frozenmain.c
Python/future.c
Python/getargs.c
Python/getcompiler.c
Python/getcopyright.c
Python/getopt.c
Python/getplatform.c
Python/getversion.c
Python/graminit.c
Python/import.c
Python/importdl.c
Python/marshal.c
Python/modsupport.c
Python/mysnprintf.c
Python/mystrtoul.c
Python/peephole.c
Python/pyarena.c
Python/pyctype.c
Python/pyfpe.c
Python/pymath.c
Python/pystate.c
Python/pystrcmp.c
Python/pystrtod.c
Python/pythonrun.c
Python/random.c
Python/structmember.c
Python/symtable.c
Python/sysmodule.c
Python/thread.c
Python/traceback.c
)
# Python/mactoolboxglue.c is compiled only where pyconfig.h sets USE_TOOLBOX_OBJECT_GLUE (macOS).
if(MT_PYTHON_CONFIG STREQUAL macos)
target_sources(mtpython PRIVATE Python/mactoolboxglue.c)
endif()
target_include_directories(mtpython PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/config/${MT_PYTHON_CONFIG}"
"${CMAKE_CURRENT_SOURCE_DIR}/Include"
)
target_include_directories(mtpython PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/Modules")
# The reference Makefile's PY_CFLAGS, plus the per-file defines it passes to getpath.c and
# getplatform.c. The interpreter runs with Py_IgnoreEnvironmentFlag/Py_NoSiteFlag and gets its
# sys.path from the host, so PREFIX/PYTHONPATH only feed getpath.c's unused defaults.
target_compile_definitions(mtpython PRIVATE
Py_BUILD_CORE
NDEBUG
PREFIX="/usr/local"
EXEC_PREFIX="/usr/local"
VERSION="2.7"
VPATH=""
PYTHONPATH=""
PLATFORM="${MT_PYTHON_PLATFORM}"
)
target_compile_options(mtpython PRIVATE -fno-strict-aliasing -fwrapv -w)
# The 2.7 headers still use `register`, which C++17 removed; every C++ consumer needs this (MSVC: /wd5033).
target_compile_options(mtpython INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-Wno-register>)
set_target_properties(mtpython PROPERTIES C_STANDARD 99 POSITION_INDEPENDENT_CODE ON)
if(NOT ANDROID AND NOT IOS AND NOT CMAKE_SYSTEM_NAME STREQUAL "iOS")
find_library(MT_PYTHON_DL dl)
if(MT_PYTHON_DL)
target_link_libraries(mtpython PUBLIC ${MT_PYTHON_DL})
endif()
endif()
find_package(Threads REQUIRED)
target_link_libraries(mtpython PUBLIC Threads::Threads m)
add_library(mt3p::python ALIAS mtpython)