diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a47729..3a87de3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,11 @@ cmake_minimum_required(VERSION 3.22) project(Typthon VERSION 3.14.0 LANGUAGES C) +# Prefer Ninja generator for faster builds +if(NOT CMAKE_GENERATOR MATCHES "Ninja") + message(STATUS "Consider using Ninja generator for faster builds: cmake -G Ninja ..") +endif() + # Allow using compilers provided by Conan if available if(EXISTS "${CMAKE_BINARY_DIR}/conan_toolchain.cmake") include("${CMAKE_BINARY_DIR}/conan_toolchain.cmake") @@ -11,45 +16,153 @@ set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -# NOTE: Building CPython requires proper configuration (./configure) -# This CMakeLists.txt is a work in progress to wire to the real interpreter - # Platform detection if(UNIX AND NOT APPLE) set(LINUX TRUE) endif() -# TODO: Collect and build Python core source files from: -# - Python/*.c (interpreter core) -# - Objects/*.c (built-in types) -# - Parser/*.c (parser) -# - Modules/*.c (built-in modules) - -# TODO: Generate or provide pyconfig.h -# CPython normally generates this via ./configure - -# Placeholder: Build Python executable -add_executable(typthon Programs/python.c) - -# TODO: Link against Python core library once built -# target_link_libraries(typthon PRIVATE python_core) - -if(UNIX) - target_link_libraries(typthon PRIVATE m dl pthread util) +# Compiler flags +if(NOT MSVC) + add_compile_options(-Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers) + add_compile_options(-fwrapv -fno-strict-aliasing) endif() -target_include_directories(typthon - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/Include +# Collect Python core source files +file(GLOB PYTHON_SOURCES Python/*.c) +file(GLOB OBJECTS_SOURCES Objects/*.c) +file(GLOB PARSER_SOURCES Parser/*.c) + +# Exclude platform-specific dynload files (we'll add the right one back) +list(FILTER PYTHON_SOURCES EXCLUDE REGEX "Python/dynload_.*\\.c$") + +# Add back the correct dynload file for the platform +if(WIN32) + list(APPEND PYTHON_SOURCES Python/dynload_win.c) +elseif(APPLE) + list(APPEND PYTHON_SOURCES Python/dynload_shlib.c) +else() + list(APPEND PYTHON_SOURCES Python/dynload_shlib.c) +endif() + +# Exclude frozen modules, test files, and code generation source files +list(FILTER PYTHON_SOURCES EXCLUDE REGEX ".*frozen.*") +list(FILTER PYTHON_SOURCES EXCLUDE REGEX ".*_test.*") +list(FILTER PYTHON_SOURCES EXCLUDE REGEX "Python/bytecodes\\.c$") +list(FILTER PYTHON_SOURCES EXCLUDE REGEX "Python/optimizer_bytecodes\\.c$") +list(FILTER PYTHON_SOURCES EXCLUDE REGEX "Python/emscripten_.*\\.c$") +list(FILTER OBJECTS_SOURCES EXCLUDE REGEX ".*_test.*") +list(FILTER PARSER_SOURCES EXCLUDE REGEX ".*_test.*") + +# Essential built-in modules (excluding those that need generated files) +set(BUILTIN_MODULE_SOURCES + Modules/gcmodule.c + Modules/main.c ) +# Note: main.c requires frozen modules that are generated +# We'll add it and handle any missing references + +# POSIX module (Unix-specific) +if(UNIX) + list(APPEND BUILTIN_MODULE_SOURCES Modules/posixmodule.c) +endif() + +# Signal module +list(APPEND BUILTIN_MODULE_SOURCES Modules/signalmodule.c) + +# I/O module sources +set(IO_MODULE_SOURCES + Modules/_io/_iomodule.c + Modules/_io/bufferedio.c + Modules/_io/bytesio.c + Modules/_io/fileio.c + Modules/_io/iobase.c + Modules/_io/stringio.c + Modules/_io/textio.c +) + +# Other essential modules +set(OTHER_MODULE_SOURCES + Modules/errnomodule.c + Modules/pwdmodule.c + Modules/_threadmodule.c + Modules/timemodule.c + Modules/_localemodule.c + Modules/_codecsmodule.c + Modules/_weakref.c + Modules/_functoolsmodule.c + Modules/_operator.c + Modules/_collectionsmodule.c + Modules/_abc.c + Modules/itertoolsmodule.c + Modules/_stat.c + Modules/symtablemodule.c + Modules/xxsubtype.c +) + +# Build Python core library +add_library(python_core STATIC + ${PYTHON_SOURCES} + ${OBJECTS_SOURCES} + ${PARSER_SOURCES} + ${BUILTIN_MODULE_SOURCES} + ${IO_MODULE_SOURCES} + ${OTHER_MODULE_SOURCES} +) + +# Include directories +target_include_directories(python_core + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/Include + ${CMAKE_CURRENT_SOURCE_DIR}/Include/internal + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/Python + ${CMAKE_CURRENT_SOURCE_DIR}/Modules/_io +) + +# Platform-specific defines +target_compile_definitions(python_core + PRIVATE + Py_BUILD_CORE + Py_BUILD_CORE_BUILTIN +) + +if(UNIX) + target_compile_definitions(python_core PRIVATE HAVE_LIBDL=1) +endif() + +# Build Python executable +add_executable(typthon Programs/python.c) +target_link_libraries(typthon PRIVATE python_core) + +# Link required system libraries +if(UNIX) + target_link_libraries(typthon PRIVATE m dl pthread util) + if(NOT APPLE) + target_link_libraries(typthon PRIVATE rt) + endif() +endif() + +# Installation include(GNUInstallDirs) install( - TARGETS typthon + TARGETS python_core typthon RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) +# Install headers +install( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Include/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/typthon + FILES_MATCHING PATTERN "*.h" +) + +# Testing include(CTest) if(BUILD_TESTING) add_test(NAME typthon_version COMMAND typthon --version) + add_test(NAME typthon_help COMMAND typthon --help) endif() diff --git a/Include/pyconfig.h b/Include/pyconfig.h index 53fb3a0..b80a9ac 100644 --- a/Include/pyconfig.h +++ b/Include/pyconfig.h @@ -1,6 +1,5 @@ -/* Minimal pyconfig.h for Typthon CMake build - * NOTE: This is a stub. A proper build requires running ./configure - * to generate a complete pyconfig.h with platform-specific settings. +/* pyconfig.h for Typthon CMake build + * Generated for Unix-like systems (Linux, macOS) */ #ifndef Py_PYCONFIG_H @@ -16,7 +15,16 @@ #define __APPLE__ 1 #endif -/* Basic configuration - these are minimal stubs */ +/* Version info */ +#define VERSION "3.14" +#define ABIFLAGS "" +#define SOABI "cpython-314" + +/* Build-time configuration */ +#define Py_BUILD_CORE 1 +#define Py_BUILD_CORE_BUILTIN 1 + +/* Basic type sizes */ #define SIZEOF_INT 4 #define SIZEOF_LONG 8 #define SIZEOF_VOID_P 8 @@ -28,31 +36,169 @@ #define SIZEOF_DOUBLE 8 #define SIZEOF_FPOS_T 16 #define SIZEOF_LONG_LONG 8 +#define SIZEOF_LONG_DOUBLE 16 +#define SIZEOF_PID_T 4 +#define SIZEOF_UINTPTR_T 8 +#define SIZEOF_PTHREAD_T 8 +#define SIZEOF_OFF_T 8 -/* Enable threading */ +/* Alignments */ +#define ALIGNOF_SIZE_T 8 +#define ALIGNOF_LONG 8 +#define ALIGNOF_DOUBLE 8 + +/* Thread support */ #ifndef MS_WINDOWS #define WITH_THREAD 1 #define HAVE_PTHREAD_H 1 #endif -/* Common POSIX features */ -#ifndef MS_WINDOWS - #define HAVE_UNISTD_H 1 - #define HAVE_FCNTL_H 1 - #define HAVE_SYS_TYPES_H 1 - #define HAVE_SYS_STAT_H 1 - #define HAVE_DIRENT_H 1 - #define HAVE_DLFCN_H 1 -#endif - -/* Standard C features */ +/* Standard headers */ #define HAVE_STDINT_H 1 #define HAVE_STDLIB_H 1 #define HAVE_STRING_H 1 #define HAVE_ERRNO_H 1 #define HAVE_STDIO_H 1 +#define HAVE_LIMITS_H 1 +#define HAVE_LOCALE_H 1 +#define HAVE_SIGNAL_H 1 +#define HAVE_STDDEF_H 1 +#define HAVE_STDARG_H 1 +#define HAVE_STRINGS_H 1 +#define HAVE_WCHAR_H 1 -/* TODO: Add more platform-specific defines as needed */ -/* This stub is incomplete. For a full build, use CPython's configure script */ +/* POSIX headers */ +#ifndef MS_WINDOWS + #define HAVE_UNISTD_H 1 + #define HAVE_FCNTL_H 1 + #define HAVE_GRP_H 1 + #define HAVE_PWD_H 1 + #define HAVE_SYS_TYPES_H 1 + #define HAVE_SYS_STAT_H 1 + #define HAVE_SYS_TIME_H 1 + #define HAVE_SYS_TIMES_H 1 + #define HAVE_SYS_WAIT_H 1 + #define HAVE_SYS_RESOURCE_H 1 + #define HAVE_SYS_SELECT_H 1 + #define HAVE_SYS_SOCKET_H 1 + #define HAVE_NETINET_IN_H 1 + #define HAVE_ARPA_INET_H 1 + #define HAVE_DIRENT_H 1 + #define HAVE_DLFCN_H 1 + #define HAVE_TERMIOS_H 1 + #define HAVE_SYS_IOCTL_H 1 + #define HAVE_SYS_PARAM_H 1 + #define HAVE_SYS_FILE_H 1 + #define HAVE_LANGINFO_H 1 + #define HAVE_SYS_UTSNAME_H 1 +#endif + +/* Functions */ +#define HAVE_ALARM 1 +#define HAVE_CHOWN 1 +#define HAVE_CLOCK 1 +#define HAVE_CONFSTR 1 +#define HAVE_FORK 1 +#define HAVE_FSEEK64 1 +#define HAVE_FSEEKO 1 +#define HAVE_FSTATVFS 1 +#define HAVE_FSYNC 1 +#define HAVE_FTELL64 1 +#define HAVE_FTELLO 1 +#define HAVE_FTRUNCATE 1 +#define HAVE_GAI_STRERROR 1 +#define HAVE_GETGROUPS 1 +#define HAVE_GETHOSTBYNAME 1 +#define HAVE_GETLOGIN 1 +#define HAVE_GETPEERNAME 1 +#define HAVE_GETPGID 1 +#define HAVE_GETPID 1 +#define HAVE_GETPRIORITY 1 +#define HAVE_GETPWENT 1 +#define HAVE_GETTIMEOFDAY 1 +#define HAVE_GETWD 1 +#define HAVE_HYPOT 1 +#define HAVE_KILL 1 +#define HAVE_LINK 1 +#define HAVE_LSTAT 1 +#define HAVE_MKTIME 1 +#define HAVE_NICE 1 +#define HAVE_NL_LANGINFO 1 +#define HAVE_PAUSE 1 +#define HAVE_PLOCK 1 +#define HAVE_POLL 1 +#define HAVE_PTHREAD_KILL 1 +#define HAVE_PUTENV 1 +#define HAVE_READLINK 1 +#define HAVE_REALPATH 1 +#define HAVE_SELECT 1 +#define HAVE_SETGID 1 +#define HAVE_SETLOCALE 1 +#define HAVE_SETPGID 1 +#define HAVE_SETPGRP 1 +#define HAVE_SETSID 1 +#define HAVE_SETUID 1 +#define HAVE_SETVBUF 1 +#define HAVE_SIGACTION 1 +#define HAVE_SIGINTERRUPT 1 +#define HAVE_SIGRELSE 1 +#define HAVE_SNPRINTF 1 +#define HAVE_STATVFS 1 +#define HAVE_STRDUP 1 +#define HAVE_STRERROR 1 +#define HAVE_STRFTIME 1 +#define HAVE_SYMLINK 1 +#define HAVE_SYSCONF 1 +#define HAVE_TCGETPGRP 1 +#define HAVE_TCSETPGRP 1 +#define HAVE_TIMEGM 1 +#define HAVE_TIMES 1 +#define HAVE_UNAME 1 +#define HAVE_UNSETENV 1 +#define HAVE_UTIMES 1 +#define HAVE_WAITPID 1 +#define HAVE_WORKING_TZSET 1 + +/* Dynamic loading */ +#ifndef MS_WINDOWS + #define HAVE_DYNAMIC_LOADING 1 + #define WITH_DYLD 1 +#endif + +/* Other features */ +#define HAVE_LIBDL 1 +#define HAVE_LIBM 1 +#define HAVE_LIBPTHREAD 1 +#define HAVE_LIBUTIL 1 + +/* Enable large file support */ +#define _LARGEFILE_SOURCE 1 +#define _FILE_OFFSET_BITS 64 + +/* Compiler features */ +#define HAVE_GCC_ASM_FOR_X87 1 +#define HAVE_GCC_ASM_FOR_X64 1 +#define HAVE_GCC_UINT128_T 1 + +/* Python-specific */ +#define PLATLIBDIR "lib" +#define PYTHONPATH ":" +#define PREFIX "/usr/local" +#define EXEC_PREFIX "/usr/local" +#define VPATH "" +#define _PYTHONFRAMEWORK "" + +/* Platform */ +#ifdef __linux__ + #define PLATFORM "linux" +#elif defined(__APPLE__) + #define PLATFORM "darwin" +#else + #define PLATFORM "posix" +#endif + +/* Miscellaneous */ +#define HAVE_COMPUTED_GOTOS 1 +#define USE_COMPUTED_GOTOS 1 #endif /* Py_PYCONFIG_H */