From 7443cb262c06a2ba3c33adb5c566eeff155e0983 Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Mon, 26 Aug 2024 22:34:24 -0400 Subject: [PATCH 01/16] wip: split up build to have a lib build --- .gitignore | 5 + build.zig | 1671 +++++++++++++++++++++++++++------------------------- 2 files changed, 869 insertions(+), 807 deletions(-) diff --git a/.gitignore b/.gitignore index df285d29c7ea19..264ca052398acd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ /.zig-cache/ /zig-out/ __pycache__/ + + +# nix stuff +.envrc +.direnv \ No newline at end of file diff --git a/build.zig b/build.zig index c7fc831039a7f9..dafebfb14aed66 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,8 @@ const std = @import("std"); -pub fn build(b: *std.Build) void { +const Step = std.Build.Step; + +pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const t = target.result; @@ -15,827 +17,882 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); - const exe = b.addExecutable(.{ + const config_header = getConfigHeader(b, t); + + const libpython = try buildLibPython(b, target, optimize, config_header); + libpython.linkLibrary(libz_dep.artifact("z")); + libpython.linkLibrary(openssl_dep.artifact("openssl")); + + b.installArtifact(libpython); + + const cpython = try buildCpython(b, target, optimize, libpython); + cpython.linkLibrary(libz_dep.artifact("z")); + cpython.linkLibrary(openssl_dep.artifact("openssl")); + cpython.rdynamic = true; + b.installArtifact(cpython); +} + +fn buildCpython( + b: *std.Build, + target: std.Build.ResolvedTarget, + optimize: std.builtin.OptimizeMode, + libpython: *std.Build.Step.Compile, +) !*std.Build.Step.Compile { + const cpython = b.addExecutable(.{ .name = "cpython", .target = target, .optimize = optimize, }); - exe.linkLibrary(libz_dep.artifact("z")); - exe.linkLibrary(openssl_dep.artifact("openssl")); - exe.linkLibC(); + cpython.linkLibrary(libpython); + cpython.addCSourceFile(.{ .file = b.path("Programs/python.c") }); - exe.addIncludePath(b.path("Include/internal")); - exe.addIncludePath(b.path(".")); - exe.addIncludePath(b.path("Include")); - exe.rdynamic = true; - - @setEvalBranchQuota(10000); - exe.addConfigHeader(b.addConfigHeader(.{ - .style = .{ .autoconf = b.path("pyconfig.h.in") }, - .include_path = "pyconfig.h", - }, .{ - .ALIGNOF_LONG = 8, - .ALIGNOF_SIZE_T = 8, - .DOUBLE_IS_LITTLE_ENDIAN_IEEE754 = 1, - .ENABLE_IPV6 = 1, - .HAVE_ACCEPT = 1, - .HAVE_ACCEPT4 = 1, - .HAVE_ACOSH = 1, - .HAVE_ADDRINFO = 1, - .HAVE_ALARM = 1, - .HAVE_ALLOCA_H = 1, - .HAVE_ASINH = 1, - .HAVE_ASM_TYPES_H = 1, - .HAVE_ATANH = 1, - .HAVE_BIND = 1, - .HAVE_BIND_TEXTDOMAIN_CODESET = 1, - .HAVE_BUILTIN_ATOMIC = 1, - .HAVE_CHMOD = 1, - .HAVE_CHOWN = 1, - .HAVE_CHROOT = 1, - .HAVE_CLOCK = 1, - .HAVE_CLOCK_GETRES = 1, - .HAVE_CLOCK_GETTIME = 1, - .HAVE_CLOCK_NANOSLEEP = 1, - .HAVE_CLOCK_SETTIME = 1, - .HAVE_CLOSE_RANGE = have(t.isGnuLibC()), - .HAVE_COMPUTED_GOTOS = 1, - .HAVE_CONFSTR = 1, - .HAVE_CONNECT = 1, - .HAVE_COPY_FILE_RANGE = 1, - .HAVE_CTERMID = 1, - .HAVE_DECL_RTLD_DEEPBIND = 0, - .HAVE_DECL_RTLD_GLOBAL = 1, - .HAVE_DECL_RTLD_LAZY = 1, - .HAVE_DECL_RTLD_LOCAL = 1, - .HAVE_DECL_RTLD_MEMBER = 0, - .HAVE_DECL_RTLD_NODELETE = 1, - .HAVE_DECL_RTLD_NOLOAD = 1, - .HAVE_DECL_RTLD_NOW = 1, - .HAVE_DEVICE_MACROS = 1, - .HAVE_DEV_PTMX = 1, - .HAVE_DIRENT_D_TYPE = 1, - .HAVE_DIRENT_H = 1, - .HAVE_DIRFD = 1, - .HAVE_DLFCN_H = 1, - .HAVE_DLOPEN = 1, - .HAVE_DUP = 1, - .HAVE_DUP2 = 1, - .HAVE_DUP3 = 1, - .HAVE_DYNAMIC_LOADING = 0, - .HAVE_ENDIAN_H = 1, - .HAVE_EPOLL = 1, - .HAVE_EPOLL_CREATE1 = 1, - .HAVE_ERF = 1, - .HAVE_ERFC = 1, - .HAVE_ERRNO_H = 1, - .HAVE_EVENTFD = 1, - .HAVE_EXECV = 1, - .HAVE_EXPLICIT_BZERO = 1, - .HAVE_EXPM1 = 1, - .HAVE_FACCESSAT = 1, - .HAVE_FCHDIR = 1, - .HAVE_FCHMOD = 1, - .HAVE_FCHMODAT = 1, - .HAVE_FCHOWN = 1, - .HAVE_FCHOWNAT = 1, - .HAVE_FCNTL_H = 1, - .HAVE_FDATASYNC = 1, - .HAVE_FDOPENDIR = 1, - .HAVE_FEXECVE = 1, - .HAVE_FLOCK = 1, - .HAVE_FORK = 1, - .HAVE_FORKPTY = 1, - .HAVE_FPATHCONF = 1, - .HAVE_FSEEKO = 1, - .HAVE_FSTATAT = 1, - .HAVE_FSTATVFS = 1, - .HAVE_FSYNC = 1, - .HAVE_FTELLO = 1, - .HAVE_FTIME = 1, - .HAVE_FTRUNCATE = 1, - .HAVE_FUTIMENS = 1, - .HAVE_FUTIMES = 1, - .HAVE_FUTIMESAT = 1, - .HAVE_GAI_STRERROR = 1, - .HAVE_GCC_ASM_FOR_X64 = 1, - .HAVE_GCC_ASM_FOR_X87 = 1, - .HAVE_GCC_UINT128_T = 1, - .HAVE_GETADDRINFO = 1, - .HAVE_GETC_UNLOCKED = 1, - .HAVE_GETEGID = 1, - .HAVE_GETENTROPY = 1, - .HAVE_GETEUID = 1, - .HAVE_GETGID = 1, - .HAVE_GETGRGID = 1, - .HAVE_GETGRGID_R = 1, - .HAVE_GETGRNAM_R = 1, - .HAVE_GETGROUPLIST = 1, - .HAVE_GETGROUPS = 1, - .HAVE_GETHOSTBYADDR = 1, - .HAVE_GETHOSTBYNAME = 1, - .HAVE_GETHOSTBYNAME_R = 1, - .HAVE_GETHOSTBYNAME_R_6_ARG = 1, - .HAVE_GETHOSTNAME = 1, - .HAVE_GETITIMER = 1, - .HAVE_GETLOADAVG = 1, - .HAVE_GETLOGIN = 1, - .HAVE_GETNAMEINFO = 1, - .HAVE_GETPAGESIZE = 1, - .HAVE_GETPEERNAME = 1, - .HAVE_GETPGID = 1, - .HAVE_GETPGRP = 1, - .HAVE_GETPID = 1, - .HAVE_GETPPID = 1, - .HAVE_GETPRIORITY = 1, - .HAVE_GETPROTOBYNAME = 1, - .HAVE_GETPWENT = 1, - .HAVE_GETPWNAM_R = 1, - .HAVE_GETPWUID = 1, - .HAVE_GETPWUID_R = 1, - .HAVE_GETRANDOM = 1, - .HAVE_GETRANDOM_SYSCALL = 1, - .HAVE_GETRESGID = 1, - .HAVE_GETRESUID = 1, - .HAVE_GETRUSAGE = 1, - .HAVE_GETSERVBYNAME = 1, - .HAVE_GETSERVBYPORT = 1, - .HAVE_GETSID = 1, - .HAVE_GETSOCKNAME = 1, - .HAVE_GETSPENT = 1, - .HAVE_GETSPNAM = 1, - .HAVE_GETUID = 1, - .HAVE_GETWD = 1, - .HAVE_GRP_H = 1, - .HAVE_HSTRERROR = 1, - .HAVE_HTOLE64 = 1, - .HAVE_IF_NAMEINDEX = 1, - .HAVE_INET_ATON = 1, - .HAVE_INET_NTOA = 1, - .HAVE_INET_PTON = 1, - .HAVE_INITGROUPS = 1, - .HAVE_INTTYPES_H = 1, - .HAVE_KILL = 1, - .HAVE_KILLPG = 1, - .HAVE_LANGINFO_H = 1, - .HAVE_LCHOWN = 1, - .HAVE_LIBDL = 1, - .HAVE_LIBINTL_H = 1, - .HAVE_LINK = 1, - .HAVE_LINKAT = 1, - .HAVE_LINUX_AUXVEC_H = 1, - .HAVE_LINUX_CAN_BCM_H = 1, - .HAVE_LINUX_CAN_H = 1, - .HAVE_LINUX_CAN_J1939_H = 1, - .HAVE_LINUX_CAN_RAW_FD_FRAMES = 1, - .HAVE_LINUX_CAN_RAW_H = 1, - .HAVE_LINUX_CAN_RAW_JOIN_FILTERS = 1, - .HAVE_LINUX_MEMFD_H = 1, - .HAVE_LINUX_NETLINK_H = 1, - .HAVE_LINUX_QRTR_H = 1, - .HAVE_LINUX_RANDOM_H = 1, - .HAVE_LINUX_SOUNDCARD_H = 1, - .HAVE_LINUX_TIPC_H = 1, - .HAVE_LINUX_VM_SOCKETS_H = 1, - .HAVE_LINUX_WAIT_H = 1, - .HAVE_LISTEN = 1, - .HAVE_LOCKF = 1, - .HAVE_LOG1P = 1, - .HAVE_LOG2 = 1, - .HAVE_LOGIN_TTY = 1, - .HAVE_LONG_DOUBLE = 1, - .HAVE_LSTAT = 1, - .HAVE_LUTIMES = 1, - .HAVE_MADVISE = 1, - .HAVE_MAKEDEV = 1, - .HAVE_MBRTOWC = 1, - .HAVE_MEMFD_CREATE = 1, - .HAVE_MEMRCHR = 1, - .HAVE_MKDIRAT = 1, - .HAVE_MKFIFO = 1, - .HAVE_MKFIFOAT = 1, - .HAVE_MKNOD = 1, - .HAVE_MKNODAT = 1, - .HAVE_MKTIME = 1, - .HAVE_MMAP = 1, - .HAVE_MREMAP = 1, - .HAVE_NANOSLEEP = 1, - .HAVE_NETDB_H = 1, - .HAVE_NETINET_IN_H = 1, - .HAVE_NETPACKET_PACKET_H = 1, - .HAVE_NET_IF_H = 1, - .HAVE_NICE = 1, - .HAVE_OPENAT = 1, - .HAVE_OPENDIR = 1, - .HAVE_OPENPTY = 1, - .HAVE_PATHCONF = 1, - .HAVE_PAUSE = 1, - .HAVE_PIPE = 1, - .HAVE_PIPE2 = 1, - .HAVE_POLL = 1, - .HAVE_POLL_H = 1, - .HAVE_POSIX_FADVISE = 1, - .HAVE_POSIX_FALLOCATE = 1, - .HAVE_POSIX_SPAWN = 1, - .HAVE_POSIX_SPAWNP = 1, - .HAVE_PREAD = 1, - .HAVE_PREADV = 1, - .HAVE_PREADV2 = have(t.isGnuLibC()), - .HAVE_PRLIMIT = 1, - .HAVE_PROTOTYPES = 1, - .HAVE_PTHREAD_CONDATTR_SETCLOCK = 1, - .HAVE_PTHREAD_GETCPUCLOCKID = 1, - .HAVE_PTHREAD_H = 1, - .HAVE_PTHREAD_KILL = 1, - .HAVE_PTHREAD_SIGMASK = 1, - .HAVE_PTY_H = 1, - .HAVE_PWRITE = 1, - .HAVE_PWRITEV = 1, - .HAVE_PWRITEV2 = have(t.isGnuLibC()), - .HAVE_READLINK = 1, - .HAVE_READLINKAT = 1, - .HAVE_READV = 1, - .HAVE_REALPATH = 1, - .HAVE_RECVFROM = 1, - .HAVE_RENAMEAT = 1, - .HAVE_SCHED_GET_PRIORITY_MAX = 1, - .HAVE_SCHED_H = 1, - .HAVE_SCHED_RR_GET_INTERVAL = 1, - .HAVE_SCHED_SETAFFINITY = 1, - .HAVE_SCHED_SETPARAM = 1, - .HAVE_SCHED_SETSCHEDULER = 1, - .HAVE_SEM_CLOCKWAIT = have(t.isGnuLibC()), - .HAVE_SEM_GETVALUE = 1, - .HAVE_SEM_OPEN = 1, - .HAVE_SEM_TIMEDWAIT = 1, - .HAVE_SEM_UNLINK = 1, - .HAVE_SENDFILE = 1, - .HAVE_SENDTO = 1, - .HAVE_SETEGID = 1, - .HAVE_SETEUID = 1, - .HAVE_SETGID = 1, - .HAVE_SETGROUPS = 1, - .HAVE_SETHOSTNAME = 1, - .HAVE_SETITIMER = 1, - .HAVE_SETJMP_H = 1, - .HAVE_SETLOCALE = 1, - .HAVE_SETPGID = 1, - .HAVE_SETPGRP = 1, - .HAVE_SETPRIORITY = 1, - .HAVE_SETREGID = 1, - .HAVE_SETRESGID = 1, - .HAVE_SETRESUID = 1, - .HAVE_SETREUID = 1, - .HAVE_SETSID = 1, - .HAVE_SETSOCKOPT = 1, - .HAVE_SETUID = 1, - .HAVE_SETVBUF = 1, - .HAVE_SHADOW_H = 1, - .HAVE_SHM_OPEN = 1, - .HAVE_SHM_UNLINK = 1, - .HAVE_SHUTDOWN = 1, - .HAVE_SIGACTION = 1, - .HAVE_SIGALTSTACK = 1, - .HAVE_SIGFILLSET = 1, - .HAVE_SIGINFO_T_SI_BAND = 1, - .HAVE_SIGINTERRUPT = 1, - .HAVE_SIGNAL_H = 1, - .HAVE_SIGPENDING = 1, - .HAVE_SIGRELSE = 1, - .HAVE_SIGTIMEDWAIT = 1, - .HAVE_SIGWAIT = 1, - .HAVE_SIGWAITINFO = 1, - .HAVE_SNPRINTF = 1, - .HAVE_SOCKADDR_ALG = 1, - .HAVE_SOCKADDR_STORAGE = 1, - .HAVE_SOCKET = 1, - .HAVE_SOCKETPAIR = 1, - .HAVE_SPAWN_H = 1, - .HAVE_SPLICE = 1, - .HAVE_SSIZE_T = 1, - .HAVE_STATVFS = 1, - .HAVE_STAT_TV_NSEC = 1, - .HAVE_STDARG_PROTOTYPES = 1, - .HAVE_STDINT_H = 1, - .HAVE_STDLIB_H = 1, - .HAVE_STD_ATOMIC = 1, - .HAVE_STRFTIME = 1, - .HAVE_STRINGS_H = 1, - .HAVE_STRING_H = 1, - .HAVE_STRSIGNAL = 1, - .HAVE_STRUCT_PASSWD_PW_GECOS = 1, - .HAVE_STRUCT_PASSWD_PW_PASSWD = 1, - .HAVE_STRUCT_STAT_ST_BLKSIZE = 1, - .HAVE_STRUCT_STAT_ST_BLOCKS = 1, - .HAVE_STRUCT_STAT_ST_RDEV = 1, - .HAVE_STRUCT_TM_TM_ZONE = 1, - .HAVE_SYMLINK = 1, - .HAVE_SYMLINKAT = 1, - .HAVE_SYNC = 1, - .HAVE_SYSCONF = 1, - .HAVE_SYSEXITS_H = 1, - .HAVE_SYSLOG_H = 1, - .HAVE_SYSTEM = 1, - .HAVE_SYS_AUXV_H = 1, - .HAVE_SYS_EPOLL_H = 1, - .HAVE_SYS_EVENTFD_H = 1, - .HAVE_SYS_FILE_H = 1, - .HAVE_SYS_IOCTL_H = 1, - .HAVE_SYS_MMAN_H = 1, - .HAVE_SYS_PARAM_H = 1, - .HAVE_SYS_POLL_H = 1, - .HAVE_SYS_RANDOM_H = 1, - .HAVE_SYS_RESOURCE_H = 1, - .HAVE_SYS_SELECT_H = 1, - .HAVE_SYS_SENDFILE_H = 1, - .HAVE_SYS_SOCKET_H = 1, - .HAVE_SYS_SOUNDCARD_H = 1, - .HAVE_SYS_STATVFS_H = 1, - .HAVE_SYS_STAT_H = 1, - .HAVE_SYS_SYSCALL_H = 1, - .HAVE_SYS_SYSMACROS_H = 1, - .HAVE_SYS_TIMES_H = 1, - .HAVE_SYS_TIME_H = 1, - .HAVE_SYS_TYPES_H = 1, - .HAVE_SYS_UIO_H = 1, - .HAVE_SYS_UN_H = 1, - .HAVE_SYS_UTSNAME_H = 1, - .HAVE_SYS_WAIT_H = 1, - .HAVE_SYS_XATTR_H = 1, - .HAVE_TCGETPGRP = 1, - .HAVE_TCSETPGRP = 1, - .HAVE_TEMPNAM = 1, - .HAVE_TERMIOS_H = 1, - .HAVE_TIMEGM = 1, - .HAVE_TIMES = 1, - .HAVE_TMPFILE = 1, - .HAVE_TMPNAM = 1, - .HAVE_TMPNAM_R = 1, - .HAVE_TM_ZONE = 1, - .HAVE_TRUNCATE = 1, - .HAVE_TTYNAME = 1, - .HAVE_UMASK = 1, - .HAVE_UNAME = 1, - .HAVE_UNISTD_H = 1, - .HAVE_UNLINKAT = 1, - .HAVE_UTIMENSAT = 1, - .HAVE_UTIMES = 1, - .HAVE_UTIME_H = 1, - .HAVE_UTMP_H = 1, - .HAVE_VFORK = 1, - .HAVE_WAIT = 1, - .HAVE_WAIT3 = 1, - .HAVE_WAIT4 = 1, - .HAVE_WAITID = 1, - .HAVE_WAITPID = 1, - .HAVE_WCHAR_H = 1, - .HAVE_WCSCOLL = 1, - .HAVE_WCSFTIME = 1, - .HAVE_WCSXFRM = 1, - .HAVE_WMEMCMP = 1, - .HAVE_WORKING_TZSET = 1, - .HAVE_WRITEV = 1, - .MAJOR_IN_SYSMACROS = 1, - .PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT = 1, - .PTHREAD_SYSTEM_SCHED_SUPPORTED = 1, - .PY_BUILTIN_HASHLIB_HASHES = "md5,sha1,sha256,sha512,sha3,blake2", - .PY_COERCE_C_LOCALE = 1, - .PY_FORMAT_SIZE_T = "z", - .PY_SSL_DEFAULT_CIPHERS = 1, - .PY_SUPPORT_TIER = 2, - .RETSIGTYPE = .void, - .SIZEOF_DOUBLE = 8, - .SIZEOF_FLOAT = 4, - .SIZEOF_FPOS_T = 16, - .SIZEOF_INT = 4, - .SIZEOF_LONG = 8, - .SIZEOF_LONG_DOUBLE = 16, - .SIZEOF_LONG_LONG = 8, - .SIZEOF_OFF_T = 8, - .SIZEOF_PID_T = 4, - .SIZEOF_PTHREAD_KEY_T = 4, - .SIZEOF_PTHREAD_T = 8, - .SIZEOF_SHORT = 2, - .SIZEOF_SIZE_T = 8, - .SIZEOF_TIME_T = 8, - .SIZEOF_UINTPTR_T = 8, - .SIZEOF_VOID_P = 8, - .SIZEOF_WCHAR_T = 4, - .SIZEOF__BOOL = 1, - .STDC_HEADERS = 1, - .SYS_SELECT_WITH_SYS_TIME = 1, - .TIME_WITH_SYS_TIME = 1, - .WITH_DECIMAL_CONTEXTVAR = 1, - .WITH_DOC_STRINGS = 1, - .WITH_FREELISTS = 1, - .WITH_PYMALLOC = 1, - ._DARWIN_C_SOURCE = 1, - ._FILE_OFFSET_BITS = 64, - ._LARGEFILE_SOURCE = 1, - ._NETBSD_SOURCE = 1, - ._POSIX_C_SOURCE = .@"200809L", - ._PYTHONFRAMEWORK = "", - ._REENTRANT = 1, - ._XOPEN_SOURCE = 700, - ._XOPEN_SOURCE_EXTENDED = 1, - .__BSD_VISIBLE = 1, - ._ALL_SOURCE = 1, - ._GNU_SOURCE = 1, - ._POSIX_PTHREAD_SEMANTICS = 1, - ._TANDEM_SOURCE = 1, - .__EXTENSIONS__ = 1, + return cpython; +} - .AC_APPLE_UNIVERSAL_BUILD = null, - .AIX_BUILDDATE = null, - .AIX_GENUINE_CPLUSPLUS = null, - .ALT_SOABI = null, - .ANDROID_API_LEVEL = null, - .DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754 = null, - .DOUBLE_IS_BIG_ENDIAN_IEEE754 = null, - .FLOAT_WORDS_BIGENDIAN = null, - .GETPGRP_HAVE_ARG = null, - .HAVE_ALIGNED_REQUIRED = null, - .HAVE_ALTZONE = null, - .HAVE_BLUETOOTH_BLUETOOTH_H = null, - .HAVE_BLUETOOTH_H = null, - .HAVE_BROKEN_MBSTOWCS = null, - .HAVE_BROKEN_NICE = null, - .HAVE_BROKEN_PIPE_BUF = null, - .HAVE_BROKEN_POLL = null, - .HAVE_BROKEN_POSIX_SEMAPHORES = null, - .HAVE_BROKEN_PTHREAD_SIGMASK = null, - .HAVE_BROKEN_SEM_GETVALUE = null, - .HAVE_BROKEN_UNSETENV = null, - .HAVE_BZLIB_H = null, - .HAVE_CHFLAGS = null, - .HAVE_CONIO_H = null, - .HAVE_CRYPT_H = null, - .HAVE_CRYPT_R = null, - .HAVE_CTERMID_R = null, - .HAVE_CURSES_FILTER = null, - .HAVE_CURSES_H = null, - .HAVE_CURSES_HAS_KEY = null, - .HAVE_CURSES_IMMEDOK = null, - .HAVE_CURSES_IS_PAD = null, - .HAVE_CURSES_IS_TERM_RESIZED = null, - .HAVE_CURSES_RESIZETERM = null, - .HAVE_CURSES_RESIZE_TERM = null, - .HAVE_CURSES_SYNCOK = null, - .HAVE_CURSES_TYPEAHEAD = null, - .HAVE_CURSES_USE_ENV = null, - .HAVE_CURSES_WCHGAT = null, - .HAVE_DB_H = null, - .HAVE_DECL_TZNAME = null, - .HAVE_DEV_PTC = null, - .HAVE_DIRECT_H = null, - .HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH = null, - .HAVE_EXPLICIT_MEMSET = null, - .HAVE_FDWALK = null, - .HAVE_FORK1 = null, - .HAVE_FSEEK64 = null, - .HAVE_FTELL64 = null, - .HAVE_GCC_ASM_FOR_MC68881 = null, - .HAVE_GDBM_DASH_NDBM_H = null, - .HAVE_GDBM_H = null, - .HAVE_GDBM_NDBM_H = null, - .HAVE_GETHOSTBYNAME_R_3_ARG = null, - .HAVE_GETHOSTBYNAME_R_5_ARG = null, - .HAVE_GLIBC_MEMMOVE_BUG = null, - .HAVE_IEEEFP_H = null, - .HAVE_IO_H = null, - .HAVE_IPA_PURE_CONST_BUG = null, - .HAVE_KQUEUE = null, - .HAVE_LARGEFILE_SUPPORT = null, - .HAVE_LCHFLAGS = null, - .HAVE_LCHMOD = null, - .HAVE_LIBB2 = null, - .HAVE_LIBDB = null, - .HAVE_LIBDLD = null, - .HAVE_LIBGDBM_COMPAT = null, - .HAVE_LIBIEEE = null, - .HAVE_LIBNDBM = null, - .HAVE_LIBREADLINE = null, - .HAVE_LIBRESOLV = null, - .HAVE_LIBSENDFILE = null, - .HAVE_LIBSQLITE3 = null, - .HAVE_LIBUTIL_H = null, - .HAVE_LZMA_H = null, - .HAVE_MEMORY_H = null, - .HAVE_NCURSES_H = null, - .HAVE_NDBM_H = null, - .HAVE_NDIR_H = null, - .HAVE_NETCAN_CAN_H = null, - .HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION = null, - .HAVE_PLOCK = null, - .HAVE_PROCESS_H = null, - .HAVE_PTHREAD_DESTRUCTOR = null, - .HAVE_PTHREAD_INIT = null, - .HAVE_PTHREAD_STUBS = null, - .HAVE_RL_APPEND_HISTORY = null, - .HAVE_RL_CATCH_SIGNAL = null, - .HAVE_RL_COMPLETION_APPEND_CHARACTER = null, - .HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK = null, - .HAVE_RL_COMPLETION_MATCHES = null, - .HAVE_RL_COMPLETION_SUPPRESS_APPEND = null, - .HAVE_RL_PRE_INPUT_HOOK = null, - .HAVE_RL_RESIZE_TERMINAL = null, - .HAVE_RPC_RPC_H = null, - .HAVE_RTPSPAWN = null, - .HAVE_SOCKADDR_SA_LEN = null, - .HAVE_STAT_TV_NSEC2 = null, - .HAVE_STRLCPY = null, - .HAVE_STROPTS_H = null, - .HAVE_STRUCT_STAT_ST_BIRTHTIME = null, - .HAVE_STRUCT_STAT_ST_FLAGS = null, - .HAVE_STRUCT_STAT_ST_GEN = null, - .HAVE_SYS_AUDIOIO_H = null, - .HAVE_SYS_BSDTTY_H = null, - .HAVE_SYS_DEVPOLL_H = null, - .HAVE_SYS_DIR_H = null, - .HAVE_SYS_ENDIAN_H = null, - .HAVE_SYS_EVENT_H = null, - .HAVE_SYS_KERN_CONTROL_H = null, - .HAVE_SYS_LOADAVG_H = null, - .HAVE_SYS_LOCK_H = null, - .HAVE_SYS_MEMFD_H = null, - .HAVE_SYS_MKDEV_H = null, - .HAVE_SYS_MODEM_H = null, - .HAVE_SYS_NDIR_H = null, - .HAVE_SYS_SYS_DOMAIN_H = null, - .HAVE_SYS_TERMIO_H = null, - .HAVE_TERM_H = null, - .HAVE_TZNAME = null, - .HAVE_USABLE_WCHAR_T = null, - .HAVE_UTIL_H = null, - .HAVE_UUID_CREATE = null, - .HAVE_UUID_ENC_BE = null, - .HAVE_UUID_GENERATE_TIME_SAFE = null, - .HAVE_UUID_H = null, - .HAVE_UUID_UUID_H = null, - .HAVE_ZLIB_COPY = 1, - .HAVE_ZLIB_H = 1, - .HAVE__GETPTY = null, - .MAJOR_IN_MKDEV = null, - .MVWDELCH_IS_EXPRESSION = null, - .PACKAGE_BUGREPORT = null, - .PACKAGE_NAME = null, - .PACKAGE_STRING = null, - .PACKAGE_TARNAME = null, - .PACKAGE_URL = null, - .PACKAGE_VERSION = null, - .POSIX_SEMAPHORES_NOT_ENABLED = null, - .PYLONG_BITS_IN_DIGIT = null, - .PY_SQLITE_ENABLE_LOAD_EXTENSION = null, - .PY_SQLITE_HAVE_SERIALIZE = null, - .PY_SSL_DEFAULT_CIPHER_STRING = null, - .Py_DEBUG = null, - .Py_ENABLE_SHARED = null, - .Py_HASH_ALGORITHM = null, - .Py_STATS = null, - .Py_TRACE_REFS = null, - .SETPGRP_HAVE_ARG = null, - .SIGNED_RIGHT_SHIFT_ZERO_FILLS = null, - .THREAD_STACK_SIZE = null, - .TIMEMODULE_LIB = null, - .TM_IN_SYS_TIME = null, - .USE_COMPUTED_GOTOS = null, - .WINDOW_HAS_FLAGS = null, - .WITH_DTRACE = null, - .WITH_DYLD = null, - .WITH_EDITLINE = null, - .WITH_LIBINTL = null, - .WITH_NEXT_FRAMEWORK = null, - .WITH_VALGRIND = null, - .X87_DOUBLE_ROUNDING = null, - ._BSD_SOURCE = null, - ._INCLUDE__STDC_A1_SOURCE = null, - ._LARGE_FILES = null, - ._MINIX = null, - ._POSIX_1_SOURCE = null, - ._POSIX_SOURCE = null, - ._POSIX_THREADS = null, - ._WASI_EMULATED_GETPID = null, - ._WASI_EMULATED_PROCESS_CLOCKS = null, - ._WASI_EMULATED_SIGNAL = null, - .clock_t = null, - .@"const" = null, - .gid_t = null, - .mode_t = null, - .off_t = null, - .pid_t = null, - .signed = null, - .size_t = null, - .socklen_t = null, - .uid_t = null, - .WORDS_BIGENDIAN = null, - })); +// https://github.com/Rexicon226/zig-cpython/blob/master/build.zig +fn buildLibPython( + b: *std.Build, + target: std.Build.ResolvedTarget, + optimize: std.builtin.OptimizeMode, + config_header: *std.Build.Step.ConfigHeader, +) !*std.Build.Step.Compile { + const t = target.result; - exe.addCSourceFiles(.{ .files = &.{ - "Modules/_abc.c", - "Modules/_codecsmodule.c", - "Modules/_collectionsmodule.c", - "Modules/_contextvarsmodule.c", - "Modules/_functoolsmodule.c", - "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", - "Modules/_localemodule.c", - "Modules/_operator.c", - "Modules/_posixsubprocess.c", - "Modules/_randommodule.c", - "Modules/_sre/sre.c", - "Modules/_ssl.c", - "Modules/_stat.c", - "Modules/_struct.c", - "Modules/_threadmodule.c", - "Modules/_tracemalloc.c", - "Modules/_weakref.c", - "Modules/arraymodule.c", - "Modules/atexitmodule.c", - "Modules/binascii.c", - "Modules/config.c", - "Modules/errnomodule.c", - "Modules/faulthandler.c", - "Modules/gcmodule.c", - "Modules/getbuildinfo.c", - "Modules/itertoolsmodule.c", - "Modules/main.c", - "Modules/mathmodule.c", - "Modules/md5module.c", - "Modules/sha1module.c", - "Modules/sha256module.c", + const libpython = b.addStaticLibrary(.{ + .name = "python", + .target = target, + .optimize = optimize, + }); + libpython.linkLibC(); + libpython.addIncludePath(b.path("Include/internal")); + libpython.addIncludePath(b.path(".")); + libpython.addIncludePath(b.path("Include")); - "Modules/_blake2/blake2module.c", - "Modules/_blake2/blake2b_impl.c", - "Modules/_blake2/blake2s_impl.c", + libpython.addConfigHeader(config_header); + libpython.addCSourceFiles(.{ + .files = core_files, + .flags = &.{ + "-fwrapv", + "-std=c11", + "-fvisibility=hidden", + "-DPy_BUILD_CORE", + }, + }); - "Modules/_sha3/sha3module.c", - "Modules/_sha3/sha3.c", + libpython.addCSourceFiles(.{ + .files = &.{ + "Modules/getpath.c", + }, + .flags = &.{ + "-fwrapv", + "-std=c11", + "-fvisibility=hidden", + "-DPy_BUILD_CORE", - "Modules/posixmodule.c", - "Modules/pwdmodule.c", - "Modules/selectmodule.c", - "Modules/sha512module.c", - "Modules/signalmodule.c", - "Modules/socketmodule.c", - "Modules/symtablemodule.c", - "Modules/timemodule.c", - "Modules/unicodedata.c", - "Modules/xxsubtype.c", - "Modules/zlibmodule.c", - "Objects/abstract.c", - "Objects/accu.c", - "Objects/boolobject.c", - "Objects/bytearrayobject.c", - "Objects/bytes_methods.c", - "Objects/bytesobject.c", - "Objects/call.c", - "Objects/capsule.c", - "Objects/cellobject.c", - "Objects/classobject.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/genericaliasobject.c", - "Objects/genobject.c", - "Objects/interpreteridobject.c", - "Objects/iterobject.c", - "Objects/listobject.c", - "Objects/longobject.c", - "Objects/memoryobject.c", - "Objects/methodobject.c", - "Objects/moduleobject.c", - "Objects/namespaceobject.c", - "Objects/object.c", - "Objects/obmalloc.c", - "Objects/odictobject.c", - "Objects/picklebufobject.c", - "Objects/rangeobject.c", - "Objects/setobject.c", - "Objects/sliceobject.c", - "Objects/structseq.c", - "Objects/tupleobject.c", - "Objects/typeobject.c", - "Objects/unicodectype.c", - "Objects/unicodeobject.c", - "Objects/unionobject.c", - "Objects/weakrefobject.c", - "Parser/action_helpers.c", - "Parser/myreadline.c", - "Parser/parser.c", - "Parser/peg_api.c", - "Parser/pegen.c", - "Parser/pegen_errors.c", - "Parser/string_parser.c", - "Parser/token.c", - "Parser/tokenizer.c", - "Programs/python.c", - "Python/Python-ast.c", - "Python/Python-tokenize.c", - "Python/_warnings.c", - "Python/asdl.c", - "Python/ast.c", - "Python/ast_opt.c", - "Python/ast_unparse.c", - "Python/bltinmodule.c", - "Python/bootstrap_hash.c", - "Python/ceval.c", - "Python/codecs.c", - "Python/compile.c", - "Python/context.c", - "Python/deepfreeze/deepfreeze.c", - "Python/dtoa.c", - "Python/dynamic_annotations.c", - "Python/errors.c", - "Python/fileutils.c", - "Python/formatter_unicode.c", - "Python/frame.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/hamt.c", - "Python/hashtable.c", - "Python/import.c", - "Python/importdl.c", - "Python/initconfig.c", - "Python/marshal.c", - "Python/modsupport.c", - "Python/mysnprintf.c", - "Python/mystrtoul.c", - "Python/pathconfig.c", - "Python/preconfig.c", - "Python/pyarena.c", - "Python/pyctype.c", - "Python/pyfpe.c", - "Python/pyhash.c", - "Python/pylifecycle.c", - "Python/pymath.c", - "Python/pystate.c", - "Python/pystrcmp.c", - "Python/pystrhex.c", - "Python/pystrtod.c", - "Python/pythonrun.c", - "Python/pytime.c", - "Python/specialize.c", - "Python/structmember.c", - "Python/suggestions.c", - "Python/symtable.c", - "Python/sysmodule.c", - "Python/thread.c", - "Python/traceback.c", - }, .flags = &.{ - "-fwrapv", - "-std=c11", - "-fvisibility=hidden", - "-DPy_BUILD_CORE", - } }); - exe.addCSourceFiles(.{ .files = &.{ - "Modules/getpath.c", - }, .flags = &.{ - "-fwrapv", - "-std=c11", - "-fvisibility=hidden", - "-DPy_BUILD_CORE", + "-DPYTHONPATH=\"\"", + "-DPREFIX=\"/usr/local\"", + "-DEXEC_PREFIX=\"/usr/local\"", + "-DVERSION=\"3.11\"", + "-DVPATH=\"\"", + "-DPLATLIBDIR=\"lib\"", + "-DPYTHONFRAMEWORK=\"\"", + }, + }); - "-DPYTHONPATH=\"\"", - "-DPREFIX=\"/usr/local\"", - "-DEXEC_PREFIX=\"/usr/local\"", - "-DVERSION=\"3.11\"", - "-DVPATH=\"\"", - "-DPLATLIBDIR=\"lib\"", - "-DPYTHONFRAMEWORK=\"\"", - } }); + libpython.addCSourceFiles(.{ + .files = &.{ + "Python/dynload_shlib.c", + }, + .flags = &.{ + "-fwrapv", + "-std=c11", + "-fvisibility=hidden", + "-DPy_BUILD_CORE", - exe.addCSourceFiles(.{ .files = &.{ - "Python/dynload_shlib.c", - }, .flags = &.{ - "-fwrapv", - "-std=c11", - "-fvisibility=hidden", - "-DPy_BUILD_CORE", + b.fmt("-DSOABI=\"cpython-311-{s}\"", .{try t.linuxTriple(b.allocator)}), + }, + }); - "-DSOABI=\"cpython-311-x86_64-linux-gnu\"", - } }); - b.installArtifact(exe); + return libpython; } fn have(x: bool) ?u1 { return if (x) 1 else null; } + +fn getConfigHeader(b: *std.Build, t: std.Target) *Step.ConfigHeader { + @setEvalBranchQuota(10000); + return b.addConfigHeader( + .{ + .style = .{ .autoconf = b.path("pyconfig.h.in") }, + .include_path = "pyconfig.h", + }, + .{ + .ALIGNOF_LONG = 8, + .ALIGNOF_SIZE_T = 8, + .DOUBLE_IS_LITTLE_ENDIAN_IEEE754 = 1, + .ENABLE_IPV6 = 1, + .HAVE_ACCEPT = 1, + .HAVE_ACCEPT4 = 1, + .HAVE_ACOSH = 1, + .HAVE_ADDRINFO = 1, + .HAVE_ALARM = 1, + .HAVE_ALLOCA_H = 1, + .HAVE_ASINH = 1, + .HAVE_ASM_TYPES_H = 1, + .HAVE_ATANH = 1, + .HAVE_BIND = 1, + .HAVE_BIND_TEXTDOMAIN_CODESET = 1, + .HAVE_BUILTIN_ATOMIC = 1, + .HAVE_CHMOD = 1, + .HAVE_CHOWN = 1, + .HAVE_CHROOT = 1, + .HAVE_CLOCK = 1, + .HAVE_CLOCK_GETRES = 1, + .HAVE_CLOCK_GETTIME = 1, + .HAVE_CLOCK_NANOSLEEP = 1, + .HAVE_CLOCK_SETTIME = 1, + .HAVE_CLOSE_RANGE = have(t.isGnuLibC()), + .HAVE_COMPUTED_GOTOS = 1, + .HAVE_CONFSTR = 1, + .HAVE_CONNECT = 1, + .HAVE_COPY_FILE_RANGE = 1, + .HAVE_CTERMID = 1, + .HAVE_DECL_RTLD_DEEPBIND = 0, + .HAVE_DECL_RTLD_GLOBAL = 1, + .HAVE_DECL_RTLD_LAZY = 1, + .HAVE_DECL_RTLD_LOCAL = 1, + .HAVE_DECL_RTLD_MEMBER = 0, + .HAVE_DECL_RTLD_NODELETE = 1, + .HAVE_DECL_RTLD_NOLOAD = 1, + .HAVE_DECL_RTLD_NOW = 1, + .HAVE_DEVICE_MACROS = 1, + .HAVE_DEV_PTMX = 1, + .HAVE_DIRENT_D_TYPE = 1, + .HAVE_DIRENT_H = 1, + .HAVE_DIRFD = 1, + .HAVE_DLFCN_H = 1, + .HAVE_DLOPEN = 1, + .HAVE_DUP = 1, + .HAVE_DUP2 = 1, + .HAVE_DUP3 = 1, + .HAVE_DYNAMIC_LOADING = 0, + .HAVE_ENDIAN_H = 1, + .HAVE_EPOLL = 1, + .HAVE_EPOLL_CREATE1 = 1, + .HAVE_ERF = 1, + .HAVE_ERFC = 1, + .HAVE_ERRNO_H = 1, + .HAVE_EVENTFD = 1, + .HAVE_EXECV = 1, + .HAVE_EXPLICIT_BZERO = 1, + .HAVE_EXPM1 = 1, + .HAVE_FACCESSAT = 1, + .HAVE_FCHDIR = 1, + .HAVE_FCHMOD = 1, + .HAVE_FCHMODAT = 1, + .HAVE_FCHOWN = 1, + .HAVE_FCHOWNAT = 1, + .HAVE_FCNTL_H = 1, + .HAVE_FDATASYNC = 1, + .HAVE_FDOPENDIR = 1, + .HAVE_FEXECVE = 1, + .HAVE_FLOCK = 1, + .HAVE_FORK = 1, + .HAVE_FORKPTY = 1, + .HAVE_FPATHCONF = 1, + .HAVE_FSEEKO = 1, + .HAVE_FSTATAT = 1, + .HAVE_FSTATVFS = 1, + .HAVE_FSYNC = 1, + .HAVE_FTELLO = 1, + .HAVE_FTIME = 1, + .HAVE_FTRUNCATE = 1, + .HAVE_FUTIMENS = 1, + .HAVE_FUTIMES = 1, + .HAVE_FUTIMESAT = 1, + .HAVE_GAI_STRERROR = 1, + .HAVE_GCC_ASM_FOR_X64 = 1, + .HAVE_GCC_ASM_FOR_X87 = 1, + .HAVE_GCC_UINT128_T = 1, + .HAVE_GETADDRINFO = 1, + .HAVE_GETC_UNLOCKED = 1, + .HAVE_GETEGID = 1, + .HAVE_GETENTROPY = 1, + .HAVE_GETEUID = 1, + .HAVE_GETGID = 1, + .HAVE_GETGRGID = 1, + .HAVE_GETGRGID_R = 1, + .HAVE_GETGRNAM_R = 1, + .HAVE_GETGROUPLIST = 1, + .HAVE_GETGROUPS = 1, + .HAVE_GETHOSTBYADDR = 1, + .HAVE_GETHOSTBYNAME = 1, + .HAVE_GETHOSTBYNAME_R = 1, + .HAVE_GETHOSTBYNAME_R_6_ARG = 1, + .HAVE_GETHOSTNAME = 1, + .HAVE_GETITIMER = 1, + .HAVE_GETLOADAVG = 1, + .HAVE_GETLOGIN = 1, + .HAVE_GETNAMEINFO = 1, + .HAVE_GETPAGESIZE = 1, + .HAVE_GETPEERNAME = 1, + .HAVE_GETPGID = 1, + .HAVE_GETPGRP = 1, + .HAVE_GETPID = 1, + .HAVE_GETPPID = 1, + .HAVE_GETPRIORITY = 1, + .HAVE_GETPROTOBYNAME = 1, + .HAVE_GETPWENT = 1, + .HAVE_GETPWNAM_R = 1, + .HAVE_GETPWUID = 1, + .HAVE_GETPWUID_R = 1, + .HAVE_GETRANDOM = 1, + .HAVE_GETRANDOM_SYSCALL = 1, + .HAVE_GETRESGID = 1, + .HAVE_GETRESUID = 1, + .HAVE_GETRUSAGE = 1, + .HAVE_GETSERVBYNAME = 1, + .HAVE_GETSERVBYPORT = 1, + .HAVE_GETSID = 1, + .HAVE_GETSOCKNAME = 1, + .HAVE_GETSPENT = 1, + .HAVE_GETSPNAM = 1, + .HAVE_GETUID = 1, + .HAVE_GETWD = 1, + .HAVE_GRP_H = 1, + .HAVE_HSTRERROR = 1, + .HAVE_HTOLE64 = 1, + .HAVE_IF_NAMEINDEX = 1, + .HAVE_INET_ATON = 1, + .HAVE_INET_NTOA = 1, + .HAVE_INET_PTON = 1, + .HAVE_INITGROUPS = 1, + .HAVE_INTTYPES_H = 1, + .HAVE_KILL = 1, + .HAVE_KILLPG = 1, + .HAVE_LANGINFO_H = 1, + .HAVE_LCHOWN = 1, + .HAVE_LIBDL = 1, + .HAVE_LIBINTL_H = 1, + .HAVE_LINK = 1, + .HAVE_LINKAT = 1, + .HAVE_LINUX_AUXVEC_H = 1, + .HAVE_LINUX_CAN_BCM_H = 1, + .HAVE_LINUX_CAN_H = 1, + .HAVE_LINUX_CAN_J1939_H = 1, + .HAVE_LINUX_CAN_RAW_FD_FRAMES = 1, + .HAVE_LINUX_CAN_RAW_H = 1, + .HAVE_LINUX_CAN_RAW_JOIN_FILTERS = 1, + .HAVE_LINUX_MEMFD_H = 1, + .HAVE_LINUX_NETLINK_H = 1, + .HAVE_LINUX_QRTR_H = 1, + .HAVE_LINUX_RANDOM_H = 1, + .HAVE_LINUX_SOUNDCARD_H = 1, + .HAVE_LINUX_TIPC_H = 1, + .HAVE_LINUX_VM_SOCKETS_H = 1, + .HAVE_LINUX_WAIT_H = 1, + .HAVE_LISTEN = 1, + .HAVE_LOCKF = 1, + .HAVE_LOG1P = 1, + .HAVE_LOG2 = 1, + .HAVE_LOGIN_TTY = 1, + .HAVE_LONG_DOUBLE = 1, + .HAVE_LSTAT = 1, + .HAVE_LUTIMES = 1, + .HAVE_MADVISE = 1, + .HAVE_MAKEDEV = 1, + .HAVE_MBRTOWC = 1, + .HAVE_MEMFD_CREATE = 1, + .HAVE_MEMRCHR = 1, + .HAVE_MKDIRAT = 1, + .HAVE_MKFIFO = 1, + .HAVE_MKFIFOAT = 1, + .HAVE_MKNOD = 1, + .HAVE_MKNODAT = 1, + .HAVE_MKTIME = 1, + .HAVE_MMAP = 1, + .HAVE_MREMAP = 1, + .HAVE_NANOSLEEP = 1, + .HAVE_NETDB_H = 1, + .HAVE_NETINET_IN_H = 1, + .HAVE_NETPACKET_PACKET_H = 1, + .HAVE_NET_IF_H = 1, + .HAVE_NICE = 1, + .HAVE_OPENAT = 1, + .HAVE_OPENDIR = 1, + .HAVE_OPENPTY = 1, + .HAVE_PATHCONF = 1, + .HAVE_PAUSE = 1, + .HAVE_PIPE = 1, + .HAVE_PIPE2 = 1, + .HAVE_POLL = 1, + .HAVE_POLL_H = 1, + .HAVE_POSIX_FADVISE = 1, + .HAVE_POSIX_FALLOCATE = 1, + .HAVE_POSIX_SPAWN = 1, + .HAVE_POSIX_SPAWNP = 1, + .HAVE_PREAD = 1, + .HAVE_PREADV = 1, + .HAVE_PREADV2 = have(t.isGnuLibC()), + .HAVE_PRLIMIT = 1, + .HAVE_PROTOTYPES = 1, + .HAVE_PTHREAD_CONDATTR_SETCLOCK = 1, + .HAVE_PTHREAD_GETCPUCLOCKID = 1, + .HAVE_PTHREAD_H = 1, + .HAVE_PTHREAD_KILL = 1, + .HAVE_PTHREAD_SIGMASK = 1, + .HAVE_PTY_H = 1, + .HAVE_PWRITE = 1, + .HAVE_PWRITEV = 1, + .HAVE_PWRITEV2 = have(t.isGnuLibC()), + .HAVE_READLINK = 1, + .HAVE_READLINKAT = 1, + .HAVE_READV = 1, + .HAVE_REALPATH = 1, + .HAVE_RECVFROM = 1, + .HAVE_RENAMEAT = 1, + .HAVE_SCHED_GET_PRIORITY_MAX = 1, + .HAVE_SCHED_H = 1, + .HAVE_SCHED_RR_GET_INTERVAL = 1, + .HAVE_SCHED_SETAFFINITY = 1, + .HAVE_SCHED_SETPARAM = 1, + .HAVE_SCHED_SETSCHEDULER = 1, + .HAVE_SEM_CLOCKWAIT = have(t.isGnuLibC()), + .HAVE_SEM_GETVALUE = 1, + .HAVE_SEM_OPEN = 1, + .HAVE_SEM_TIMEDWAIT = 1, + .HAVE_SEM_UNLINK = 1, + .HAVE_SENDFILE = 1, + .HAVE_SENDTO = 1, + .HAVE_SETEGID = 1, + .HAVE_SETEUID = 1, + .HAVE_SETGID = 1, + .HAVE_SETGROUPS = 1, + .HAVE_SETHOSTNAME = 1, + .HAVE_SETITIMER = 1, + .HAVE_SETJMP_H = 1, + .HAVE_SETLOCALE = 1, + .HAVE_SETPGID = 1, + .HAVE_SETPGRP = 1, + .HAVE_SETPRIORITY = 1, + .HAVE_SETREGID = 1, + .HAVE_SETRESGID = 1, + .HAVE_SETRESUID = 1, + .HAVE_SETREUID = 1, + .HAVE_SETSID = 1, + .HAVE_SETSOCKOPT = 1, + .HAVE_SETUID = 1, + .HAVE_SETVBUF = 1, + .HAVE_SHADOW_H = 1, + .HAVE_SHM_OPEN = 1, + .HAVE_SHM_UNLINK = 1, + .HAVE_SHUTDOWN = 1, + .HAVE_SIGACTION = 1, + .HAVE_SIGALTSTACK = 1, + .HAVE_SIGFILLSET = 1, + .HAVE_SIGINFO_T_SI_BAND = 1, + .HAVE_SIGINTERRUPT = 1, + .HAVE_SIGNAL_H = 1, + .HAVE_SIGPENDING = 1, + .HAVE_SIGRELSE = 1, + .HAVE_SIGTIMEDWAIT = 1, + .HAVE_SIGWAIT = 1, + .HAVE_SIGWAITINFO = 1, + .HAVE_SNPRINTF = 1, + .HAVE_SOCKADDR_ALG = 1, + .HAVE_SOCKADDR_STORAGE = 1, + .HAVE_SOCKET = 1, + .HAVE_SOCKETPAIR = 1, + .HAVE_SPAWN_H = 1, + .HAVE_SPLICE = 1, + .HAVE_SSIZE_T = 1, + .HAVE_STATVFS = 1, + .HAVE_STAT_TV_NSEC = 1, + .HAVE_STDARG_PROTOTYPES = 1, + .HAVE_STDINT_H = 1, + .HAVE_STDLIB_H = 1, + .HAVE_STD_ATOMIC = 1, + .HAVE_STRFTIME = 1, + .HAVE_STRINGS_H = 1, + .HAVE_STRING_H = 1, + .HAVE_STRSIGNAL = 1, + .HAVE_STRUCT_PASSWD_PW_GECOS = 1, + .HAVE_STRUCT_PASSWD_PW_PASSWD = 1, + .HAVE_STRUCT_STAT_ST_BLKSIZE = 1, + .HAVE_STRUCT_STAT_ST_BLOCKS = 1, + .HAVE_STRUCT_STAT_ST_RDEV = 1, + .HAVE_STRUCT_TM_TM_ZONE = 1, + .HAVE_SYMLINK = 1, + .HAVE_SYMLINKAT = 1, + .HAVE_SYNC = 1, + .HAVE_SYSCONF = 1, + .HAVE_SYSEXITS_H = 1, + .HAVE_SYSLOG_H = 1, + .HAVE_SYSTEM = 1, + .HAVE_SYS_AUXV_H = 1, + .HAVE_SYS_EPOLL_H = 1, + .HAVE_SYS_EVENTFD_H = 1, + .HAVE_SYS_FILE_H = 1, + .HAVE_SYS_IOCTL_H = 1, + .HAVE_SYS_MMAN_H = 1, + .HAVE_SYS_PARAM_H = 1, + .HAVE_SYS_POLL_H = 1, + .HAVE_SYS_RANDOM_H = 1, + .HAVE_SYS_RESOURCE_H = 1, + .HAVE_SYS_SELECT_H = 1, + .HAVE_SYS_SENDFILE_H = 1, + .HAVE_SYS_SOCKET_H = 1, + .HAVE_SYS_SOUNDCARD_H = 1, + .HAVE_SYS_STATVFS_H = 1, + .HAVE_SYS_STAT_H = 1, + .HAVE_SYS_SYSCALL_H = 1, + .HAVE_SYS_SYSMACROS_H = 1, + .HAVE_SYS_TIMES_H = 1, + .HAVE_SYS_TIME_H = 1, + .HAVE_SYS_TYPES_H = 1, + .HAVE_SYS_UIO_H = 1, + .HAVE_SYS_UN_H = 1, + .HAVE_SYS_UTSNAME_H = 1, + .HAVE_SYS_WAIT_H = 1, + .HAVE_SYS_XATTR_H = 1, + .HAVE_TCGETPGRP = 1, + .HAVE_TCSETPGRP = 1, + .HAVE_TEMPNAM = 1, + .HAVE_TERMIOS_H = 1, + .HAVE_TIMEGM = 1, + .HAVE_TIMES = 1, + .HAVE_TMPFILE = 1, + .HAVE_TMPNAM = 1, + .HAVE_TMPNAM_R = 1, + .HAVE_TM_ZONE = 1, + .HAVE_TRUNCATE = 1, + .HAVE_TTYNAME = 1, + .HAVE_UMASK = 1, + .HAVE_UNAME = 1, + .HAVE_UNISTD_H = 1, + .HAVE_UNLINKAT = 1, + .HAVE_UTIMENSAT = 1, + .HAVE_UTIMES = 1, + .HAVE_UTIME_H = 1, + .HAVE_UTMP_H = 1, + .HAVE_VFORK = 1, + .HAVE_WAIT = 1, + .HAVE_WAIT3 = 1, + .HAVE_WAIT4 = 1, + .HAVE_WAITID = 1, + .HAVE_WAITPID = 1, + .HAVE_WCHAR_H = 1, + .HAVE_WCSCOLL = 1, + .HAVE_WCSFTIME = 1, + .HAVE_WCSXFRM = 1, + .HAVE_WMEMCMP = 1, + .HAVE_WORKING_TZSET = 1, + .HAVE_WRITEV = 1, + .MAJOR_IN_SYSMACROS = 1, + .PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT = 1, + .PTHREAD_SYSTEM_SCHED_SUPPORTED = 1, + .PY_BUILTIN_HASHLIB_HASHES = "md5,sha1,sha256,sha512,sha3,blake2", + .PY_COERCE_C_LOCALE = 1, + .PY_FORMAT_SIZE_T = "z", + .PY_SSL_DEFAULT_CIPHERS = 1, + .PY_SUPPORT_TIER = 2, + .RETSIGTYPE = .void, + .SIZEOF_DOUBLE = 8, + .SIZEOF_FLOAT = 4, + .SIZEOF_FPOS_T = 16, + .SIZEOF_INT = 4, + .SIZEOF_LONG = 8, + .SIZEOF_LONG_DOUBLE = 16, + .SIZEOF_LONG_LONG = 8, + .SIZEOF_OFF_T = 8, + .SIZEOF_PID_T = 4, + .SIZEOF_PTHREAD_KEY_T = 4, + .SIZEOF_PTHREAD_T = 8, + .SIZEOF_SHORT = 2, + .SIZEOF_SIZE_T = 8, + .SIZEOF_TIME_T = 8, + .SIZEOF_UINTPTR_T = 8, + .SIZEOF_VOID_P = 8, + .SIZEOF_WCHAR_T = 4, + .SIZEOF__BOOL = 1, + .STDC_HEADERS = 1, + .SYS_SELECT_WITH_SYS_TIME = 1, + .TIME_WITH_SYS_TIME = 1, + .WITH_DECIMAL_CONTEXTVAR = 1, + .WITH_DOC_STRINGS = 1, + .WITH_FREELISTS = 1, + .WITH_PYMALLOC = 1, + ._DARWIN_C_SOURCE = 1, + ._FILE_OFFSET_BITS = 64, + ._LARGEFILE_SOURCE = 1, + ._NETBSD_SOURCE = 1, + ._POSIX_C_SOURCE = .@"200809L", + ._PYTHONFRAMEWORK = "", + ._REENTRANT = 1, + ._XOPEN_SOURCE = 700, + ._XOPEN_SOURCE_EXTENDED = 1, + .__BSD_VISIBLE = 1, + ._ALL_SOURCE = 1, + ._GNU_SOURCE = 1, + ._POSIX_PTHREAD_SEMANTICS = 1, + ._TANDEM_SOURCE = 1, + .__EXTENSIONS__ = 1, + + .AC_APPLE_UNIVERSAL_BUILD = null, + .AIX_BUILDDATE = null, + .AIX_GENUINE_CPLUSPLUS = null, + .ALT_SOABI = null, + .ANDROID_API_LEVEL = null, + .DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754 = null, + .DOUBLE_IS_BIG_ENDIAN_IEEE754 = null, + .FLOAT_WORDS_BIGENDIAN = null, + .GETPGRP_HAVE_ARG = null, + .HAVE_ALIGNED_REQUIRED = null, + .HAVE_ALTZONE = null, + .HAVE_BLUETOOTH_BLUETOOTH_H = null, + .HAVE_BLUETOOTH_H = null, + .HAVE_BROKEN_MBSTOWCS = null, + .HAVE_BROKEN_NICE = null, + .HAVE_BROKEN_PIPE_BUF = null, + .HAVE_BROKEN_POLL = null, + .HAVE_BROKEN_POSIX_SEMAPHORES = null, + .HAVE_BROKEN_PTHREAD_SIGMASK = null, + .HAVE_BROKEN_SEM_GETVALUE = null, + .HAVE_BROKEN_UNSETENV = null, + .HAVE_BZLIB_H = null, + .HAVE_CHFLAGS = null, + .HAVE_CONIO_H = null, + .HAVE_CRYPT_H = null, + .HAVE_CRYPT_R = null, + .HAVE_CTERMID_R = null, + .HAVE_CURSES_FILTER = null, + .HAVE_CURSES_H = null, + .HAVE_CURSES_HAS_KEY = null, + .HAVE_CURSES_IMMEDOK = null, + .HAVE_CURSES_IS_PAD = null, + .HAVE_CURSES_IS_TERM_RESIZED = null, + .HAVE_CURSES_RESIZETERM = null, + .HAVE_CURSES_RESIZE_TERM = null, + .HAVE_CURSES_SYNCOK = null, + .HAVE_CURSES_TYPEAHEAD = null, + .HAVE_CURSES_USE_ENV = null, + .HAVE_CURSES_WCHGAT = null, + .HAVE_DB_H = null, + .HAVE_DECL_TZNAME = null, + .HAVE_DEV_PTC = null, + .HAVE_DIRECT_H = null, + .HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH = null, + .HAVE_EXPLICIT_MEMSET = null, + .HAVE_FDWALK = null, + .HAVE_FORK1 = null, + .HAVE_FSEEK64 = null, + .HAVE_FTELL64 = null, + .HAVE_GCC_ASM_FOR_MC68881 = null, + .HAVE_GDBM_DASH_NDBM_H = null, + .HAVE_GDBM_H = null, + .HAVE_GDBM_NDBM_H = null, + .HAVE_GETHOSTBYNAME_R_3_ARG = null, + .HAVE_GETHOSTBYNAME_R_5_ARG = null, + .HAVE_GLIBC_MEMMOVE_BUG = null, + .HAVE_IEEEFP_H = null, + .HAVE_IO_H = null, + .HAVE_IPA_PURE_CONST_BUG = null, + .HAVE_KQUEUE = null, + .HAVE_LARGEFILE_SUPPORT = null, + .HAVE_LCHFLAGS = null, + .HAVE_LCHMOD = null, + .HAVE_LIBB2 = null, + .HAVE_LIBDB = null, + .HAVE_LIBDLD = null, + .HAVE_LIBGDBM_COMPAT = null, + .HAVE_LIBIEEE = null, + .HAVE_LIBNDBM = null, + .HAVE_LIBREADLINE = null, + .HAVE_LIBRESOLV = null, + .HAVE_LIBSENDFILE = null, + .HAVE_LIBSQLITE3 = null, + .HAVE_LIBUTIL_H = null, + .HAVE_LZMA_H = null, + .HAVE_MEMORY_H = null, + .HAVE_NCURSES_H = null, + .HAVE_NDBM_H = null, + .HAVE_NDIR_H = null, + .HAVE_NETCAN_CAN_H = null, + .HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION = null, + .HAVE_PLOCK = null, + .HAVE_PROCESS_H = null, + .HAVE_PTHREAD_DESTRUCTOR = null, + .HAVE_PTHREAD_INIT = null, + .HAVE_PTHREAD_STUBS = null, + .HAVE_RL_APPEND_HISTORY = null, + .HAVE_RL_CATCH_SIGNAL = null, + .HAVE_RL_COMPLETION_APPEND_CHARACTER = null, + .HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK = null, + .HAVE_RL_COMPLETION_MATCHES = null, + .HAVE_RL_COMPLETION_SUPPRESS_APPEND = null, + .HAVE_RL_PRE_INPUT_HOOK = null, + .HAVE_RL_RESIZE_TERMINAL = null, + .HAVE_RPC_RPC_H = null, + .HAVE_RTPSPAWN = null, + .HAVE_SOCKADDR_SA_LEN = null, + .HAVE_STAT_TV_NSEC2 = null, + .HAVE_STRLCPY = null, + .HAVE_STROPTS_H = null, + .HAVE_STRUCT_STAT_ST_BIRTHTIME = null, + .HAVE_STRUCT_STAT_ST_FLAGS = null, + .HAVE_STRUCT_STAT_ST_GEN = null, + .HAVE_SYS_AUDIOIO_H = null, + .HAVE_SYS_BSDTTY_H = null, + .HAVE_SYS_DEVPOLL_H = null, + .HAVE_SYS_DIR_H = null, + .HAVE_SYS_ENDIAN_H = null, + .HAVE_SYS_EVENT_H = null, + .HAVE_SYS_KERN_CONTROL_H = null, + .HAVE_SYS_LOADAVG_H = null, + .HAVE_SYS_LOCK_H = null, + .HAVE_SYS_MEMFD_H = null, + .HAVE_SYS_MKDEV_H = null, + .HAVE_SYS_MODEM_H = null, + .HAVE_SYS_NDIR_H = null, + .HAVE_SYS_SYS_DOMAIN_H = null, + .HAVE_SYS_TERMIO_H = null, + .HAVE_TERM_H = null, + .HAVE_TZNAME = null, + .HAVE_USABLE_WCHAR_T = null, + .HAVE_UTIL_H = null, + .HAVE_UUID_CREATE = null, + .HAVE_UUID_ENC_BE = null, + .HAVE_UUID_GENERATE_TIME_SAFE = null, + .HAVE_UUID_H = null, + .HAVE_UUID_UUID_H = null, + .HAVE_ZLIB_COPY = 1, + .HAVE_ZLIB_H = 1, + .HAVE__GETPTY = null, + .MAJOR_IN_MKDEV = null, + .MVWDELCH_IS_EXPRESSION = null, + .PACKAGE_BUGREPORT = null, + .PACKAGE_NAME = null, + .PACKAGE_STRING = null, + .PACKAGE_TARNAME = null, + .PACKAGE_URL = null, + .PACKAGE_VERSION = null, + .POSIX_SEMAPHORES_NOT_ENABLED = null, + .PYLONG_BITS_IN_DIGIT = null, + .PY_SQLITE_ENABLE_LOAD_EXTENSION = null, + .PY_SQLITE_HAVE_SERIALIZE = null, + .PY_SSL_DEFAULT_CIPHER_STRING = null, + .Py_DEBUG = null, + .Py_ENABLE_SHARED = null, + .Py_HASH_ALGORITHM = null, + .Py_STATS = null, + .Py_TRACE_REFS = null, + .SETPGRP_HAVE_ARG = null, + .SIGNED_RIGHT_SHIFT_ZERO_FILLS = null, + .THREAD_STACK_SIZE = null, + .TIMEMODULE_LIB = null, + .TM_IN_SYS_TIME = null, + .USE_COMPUTED_GOTOS = null, + .WINDOW_HAS_FLAGS = null, + .WITH_DTRACE = null, + .WITH_DYLD = null, + .WITH_EDITLINE = null, + .WITH_LIBINTL = null, + .WITH_NEXT_FRAMEWORK = null, + .WITH_VALGRIND = null, + .X87_DOUBLE_ROUNDING = null, + ._BSD_SOURCE = null, + ._INCLUDE__STDC_A1_SOURCE = null, + ._LARGE_FILES = null, + ._MINIX = null, + ._POSIX_1_SOURCE = null, + ._POSIX_SOURCE = null, + ._POSIX_THREADS = null, + ._WASI_EMULATED_GETPID = null, + ._WASI_EMULATED_PROCESS_CLOCKS = null, + ._WASI_EMULATED_SIGNAL = null, + .clock_t = null, + .@"const" = null, + .gid_t = null, + .mode_t = null, + .off_t = null, + .pid_t = null, + .signed = null, + .size_t = null, + .socklen_t = null, + .uid_t = null, + .WORDS_BIGENDIAN = null, + }, + ); +} + +const core_files = &.{ + "Modules/_abc.c", + "Modules/_codecsmodule.c", + "Modules/_collectionsmodule.c", + "Modules/_contextvarsmodule.c", + "Modules/_functoolsmodule.c", + "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", + "Modules/_localemodule.c", + "Modules/_operator.c", + "Modules/_posixsubprocess.c", + "Modules/_randommodule.c", + "Modules/_sre/sre.c", + "Modules/_ssl.c", + "Modules/_stat.c", + "Modules/_struct.c", + "Modules/_threadmodule.c", + "Modules/_tracemalloc.c", + "Modules/_weakref.c", + "Modules/arraymodule.c", + "Modules/atexitmodule.c", + "Modules/binascii.c", + "Modules/config.c", + "Modules/errnomodule.c", + "Modules/faulthandler.c", + "Modules/gcmodule.c", + "Modules/getbuildinfo.c", + "Modules/itertoolsmodule.c", + "Modules/main.c", + "Modules/mathmodule.c", + "Modules/md5module.c", + "Modules/sha1module.c", + "Modules/sha256module.c", + + "Modules/_blake2/blake2module.c", + "Modules/_blake2/blake2b_impl.c", + "Modules/_blake2/blake2s_impl.c", + + "Modules/_sha3/sha3module.c", + "Modules/_sha3/sha3.c", + + "Modules/posixmodule.c", + "Modules/pwdmodule.c", + "Modules/selectmodule.c", + "Modules/sha512module.c", + "Modules/signalmodule.c", + "Modules/socketmodule.c", + "Modules/symtablemodule.c", + "Modules/timemodule.c", + "Modules/unicodedata.c", + "Modules/xxsubtype.c", + "Modules/zlibmodule.c", + "Objects/abstract.c", + "Objects/accu.c", + "Objects/boolobject.c", + "Objects/bytearrayobject.c", + "Objects/bytes_methods.c", + "Objects/bytesobject.c", + "Objects/call.c", + "Objects/capsule.c", + "Objects/cellobject.c", + "Objects/classobject.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/genericaliasobject.c", + "Objects/genobject.c", + "Objects/interpreteridobject.c", + "Objects/iterobject.c", + "Objects/listobject.c", + "Objects/longobject.c", + "Objects/memoryobject.c", + "Objects/methodobject.c", + "Objects/moduleobject.c", + "Objects/namespaceobject.c", + "Objects/object.c", + "Objects/obmalloc.c", + "Objects/odictobject.c", + "Objects/picklebufobject.c", + "Objects/rangeobject.c", + "Objects/setobject.c", + "Objects/sliceobject.c", + "Objects/structseq.c", + "Objects/tupleobject.c", + "Objects/typeobject.c", + "Objects/unicodectype.c", + "Objects/unicodeobject.c", + "Objects/unionobject.c", + "Objects/weakrefobject.c", + "Parser/action_helpers.c", + "Parser/myreadline.c", + "Parser/parser.c", + "Parser/peg_api.c", + "Parser/pegen.c", + "Parser/pegen_errors.c", + "Parser/string_parser.c", + "Parser/token.c", + "Parser/tokenizer.c", + "Python/Python-ast.c", + "Python/Python-tokenize.c", + "Python/_warnings.c", + "Python/asdl.c", + "Python/ast.c", + "Python/ast_opt.c", + "Python/ast_unparse.c", + "Python/bltinmodule.c", + "Python/bootstrap_hash.c", + "Python/ceval.c", + "Python/codecs.c", + "Python/compile.c", + "Python/context.c", + "Python/deepfreeze/deepfreeze.c", + "Python/dtoa.c", + "Python/dynamic_annotations.c", + "Python/errors.c", + "Python/fileutils.c", + "Python/formatter_unicode.c", + "Python/frame.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/hamt.c", + "Python/hashtable.c", + "Python/import.c", + "Python/importdl.c", + "Python/initconfig.c", + "Python/marshal.c", + "Python/modsupport.c", + "Python/mysnprintf.c", + "Python/mystrtoul.c", + "Python/pathconfig.c", + "Python/preconfig.c", + "Python/pyarena.c", + "Python/pyctype.c", + "Python/pyfpe.c", + "Python/pyhash.c", + "Python/pylifecycle.c", + "Python/pymath.c", + "Python/pystate.c", + "Python/pystrcmp.c", + "Python/pystrhex.c", + "Python/pystrtod.c", + "Python/pythonrun.c", + "Python/pytime.c", + "Python/specialize.c", + "Python/structmember.c", + "Python/suggestions.c", + "Python/symtable.c", + "Python/sysmodule.c", + "Python/thread.c", + "Python/traceback.c", +}; From f2520a0afdea3cf75c31a3a060b2519f3ffe80ff Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Mon, 26 Aug 2024 22:50:41 -0400 Subject: [PATCH 02/16] wip: try to set some config options more dynamically --- build.zig | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/build.zig b/build.zig index dafebfb14aed66..ec5f0c37f8abbc 100644 --- a/build.zig +++ b/build.zig @@ -67,8 +67,8 @@ fn buildLibPython( libpython.addIncludePath(b.path("Include/internal")); libpython.addIncludePath(b.path(".")); libpython.addIncludePath(b.path("Include")); - libpython.addConfigHeader(config_header); + libpython.addCSourceFiles(.{ .files = core_files, .flags = &.{ @@ -128,7 +128,7 @@ fn getConfigHeader(b: *std.Build, t: std.Target) *Step.ConfigHeader { .include_path = "pyconfig.h", }, .{ - .ALIGNOF_LONG = 8, + .ALIGNOF_LONG = t.c_type_alignment(.long), .ALIGNOF_SIZE_T = 8, .DOUBLE_IS_LITTLE_ENDIAN_IEEE754 = 1, .ENABLE_IPV6 = 1, @@ -498,13 +498,13 @@ fn getConfigHeader(b: *std.Build, t: std.Target) *Step.ConfigHeader { .PY_SSL_DEFAULT_CIPHERS = 1, .PY_SUPPORT_TIER = 2, .RETSIGTYPE = .void, - .SIZEOF_DOUBLE = 8, - .SIZEOF_FLOAT = 4, + .SIZEOF_DOUBLE = t.c_type_byte_size(.double), + .SIZEOF_FLOAT = t.c_type_byte_size(.float), .SIZEOF_FPOS_T = 16, - .SIZEOF_INT = 4, - .SIZEOF_LONG = 8, - .SIZEOF_LONG_DOUBLE = 16, - .SIZEOF_LONG_LONG = 8, + .SIZEOF_INT = t.c_type_byte_size(.int), + .SIZEOF_LONG = t.c_type_byte_size(.long), + .SIZEOF_LONG_DOUBLE = t.c_type_byte_size(.longdouble), + .SIZEOF_LONG_LONG = t.c_type_byte_size(.longlong), .SIZEOF_OFF_T = 8, .SIZEOF_PID_T = 4, .SIZEOF_PTHREAD_KEY_T = 4, @@ -534,7 +534,7 @@ fn getConfigHeader(b: *std.Build, t: std.Target) *Step.ConfigHeader { ._XOPEN_SOURCE_EXTENDED = 1, .__BSD_VISIBLE = 1, ._ALL_SOURCE = 1, - ._GNU_SOURCE = 1, + ._GNU_SOURCE = 1, // TODO: disable based on target? ._POSIX_PTHREAD_SEMANTICS = 1, ._TANDEM_SOURCE = 1, .__EXTENSIONS__ = 1, From 4c2a9452042f3a3d4e3fa32e8af69f4e563fc42c Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Mon, 26 Aug 2024 23:01:29 -0400 Subject: [PATCH 03/16] it compiles! --- build.zig | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/build.zig b/build.zig index ec5f0c37f8abbc..512dab6c5ac9a3 100644 --- a/build.zig +++ b/build.zig @@ -25,18 +25,30 @@ pub fn build(b: *std.Build) !void { b.installArtifact(libpython); - const cpython = try buildCpython(b, target, optimize, libpython); + const cpython = try buildCpython(b, target, optimize, libpython, config_header); cpython.linkLibrary(libz_dep.artifact("z")); cpython.linkLibrary(openssl_dep.artifact("openssl")); cpython.rdynamic = true; b.installArtifact(cpython); } +fn addIncludes( + b: *std.Build, + step: *Step.Compile, + config_header: *std.Build.Step.ConfigHeader, +) void { + step.addIncludePath(b.path("Include/internal")); + step.addIncludePath(b.path(".")); + step.addIncludePath(b.path("Include")); + step.addConfigHeader(config_header); +} + fn buildCpython( b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, libpython: *std.Build.Step.Compile, + config_header: *std.Build.Step.ConfigHeader, ) !*std.Build.Step.Compile { const cpython = b.addExecutable(.{ .name = "cpython", @@ -44,7 +56,18 @@ fn buildCpython( .optimize = optimize, }); cpython.linkLibrary(libpython); - cpython.addCSourceFile(.{ .file = b.path("Programs/python.c") }); + + addIncludes(b, cpython, config_header); + + cpython.addCSourceFile(.{ + .file = b.path("Programs/python.c"), + .flags = &.{ + "-fwrapv", + "-std=c11", + "-fvisibility=hidden", + "-DPy_BUILD_CORE", + }, + }); return cpython; } @@ -64,10 +87,7 @@ fn buildLibPython( .optimize = optimize, }); libpython.linkLibC(); - libpython.addIncludePath(b.path("Include/internal")); - libpython.addIncludePath(b.path(".")); - libpython.addIncludePath(b.path("Include")); - libpython.addConfigHeader(config_header); + addIncludes(b, libpython, config_header); libpython.addCSourceFiles(.{ .files = core_files, From 52ec83b9d43d1be70d2ccdcbafd57b3ad19964ae Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Wed, 28 Aug 2024 20:01:29 -0400 Subject: [PATCH 04/16] rename lib to lib-python --- build.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 512dab6c5ac9a3..7f021251b2bb6e 100644 --- a/build.zig +++ b/build.zig @@ -82,7 +82,7 @@ fn buildLibPython( const t = target.result; const libpython = b.addStaticLibrary(.{ - .name = "python", + .name = "lib-python", .target = target, .optimize = optimize, }); From 325154cc284b66decc9974f0585d4f9c09e5f130 Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Wed, 28 Aug 2024 22:20:39 -0400 Subject: [PATCH 05/16] Copy the zig wrapper code from https://github.com/Rexicon226/zig-cpython/tree/master --- build.zig | 2 +- src/constants.zig | 2 + src/externs.zig | 39 ++++++++++++++++++++ src/helpers.zig | 32 ++++++++++++++++ src/root.zig | 4 ++ src/types.zig | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 src/constants.zig create mode 100644 src/externs.zig create mode 100644 src/helpers.zig create mode 100644 src/root.zig create mode 100644 src/types.zig diff --git a/build.zig b/build.zig index 7f021251b2bb6e..512dab6c5ac9a3 100644 --- a/build.zig +++ b/build.zig @@ -82,7 +82,7 @@ fn buildLibPython( const t = target.result; const libpython = b.addStaticLibrary(.{ - .name = "lib-python", + .name = "python", .target = target, .optimize = optimize, }); diff --git a/src/constants.zig b/src/constants.zig new file mode 100644 index 00000000000000..54dffab6815e3b --- /dev/null +++ b/src/constants.zig @@ -0,0 +1,2 @@ +pub const Py_file_input: c_int = 257; +pub const Py_MARSHAL_VERSION: c_int = 4; diff --git a/src/externs.zig b/src/externs.zig new file mode 100644 index 00000000000000..e82601b0c3fc90 --- /dev/null +++ b/src/externs.zig @@ -0,0 +1,39 @@ +const types = @import("types.zig"); + +const PyConfig = types.PyConfig; +const PyPreConfig = types.PyPreConfig; +const PyStatus = types.PyStatus; +const PyWideStringList = types.PyWideStringList; +const wchar = types.wchar; + +pub extern fn Py_PreInitialize(*const PyPreConfig) PyStatus; +pub extern fn PyPreConfig_InitPythonConfig(*PyPreConfig) void; +pub extern fn PyStatus_Exception(PyStatus) bool; +pub extern fn Py_ExitStatusException(PyStatus) noreturn; + +pub extern fn Py_Initialize() void; +pub extern fn Py_Finalize() void; + +pub extern fn PySys_SetPath([*:0]const wchar) void; + +pub extern fn Py_DecRef(?*anyopaque) void; + +pub extern fn Py_DecodeLocale([*:0]const u8, *usize) ?[*:0]u8; +pub extern fn PyConfig_SetBytesString(*PyConfig, *const [*:0]wchar, [*:0]const u8) PyStatus; +pub extern fn Py_SetProgramName([*:0]const u8) void; + +pub extern fn Py_CompileString([*:0]const u8, [*:0]const u8, c_int) ?*anyopaque; +pub extern fn PyMarshal_WriteObjectToString(?*anyopaque, c_int) ?*anyopaque; +pub extern fn PyBytes_Size(?*anyopaque) usize; +pub extern fn PyBytes_AsString(?*anyopaque) ?[*:0]u8; + +pub extern fn PyErr_Print() void; +pub extern fn PyErr_Fetch(?*anyopaque, ?*anyopaque, ?*anyopaque) void; +pub extern fn PyErr_NormalizeException(?*anyopaque, ?*anyopaque, ?*anyopaque) void; + +pub extern fn PyConfig_InitPythonConfig(*PyConfig) void; +pub extern fn PyConfig_Clear(*PyConfig) void; +pub extern fn PyConfig_Read(*PyConfig) PyStatus; +pub extern fn Py_InitializeFromConfig(*PyConfig) PyStatus; + +pub extern fn PyWideStringList_Append(*PyWideStringList, [*:0]const u32) PyStatus; diff --git a/src/helpers.zig b/src/helpers.zig new file mode 100644 index 00000000000000..50809167e3534a --- /dev/null +++ b/src/helpers.zig @@ -0,0 +1,32 @@ +const externs = @import("externs.zig"); +const constants = @import("constants.zig"); + +pub fn Sys_SetPath(path: [:0]const u16) void { + externs.PySys_SetPath(path.ptr); +} + +pub fn SetProgramName(name: [:0]const u8) void { + externs.Py_SetProgramName(name.ptr); +} + +pub fn CompileString(source: [:0]const u8, filename: [:0]const u8) ?*anyopaque { + return externs.Py_CompileString(source.ptr, filename.ptr, constants.Py_file_input); +} + +pub fn Marshal_WriteObjectToString(code: ?*anyopaque) ?*anyopaque { + return externs.PyMarshal_WriteObjectToString(code, constants.Py_MARSHAL_VERSION); +} + +pub fn Bytes_Size(code: ?*anyopaque) usize { + return externs.PyBytes_Size(code); +} + +pub fn Bytes_AsString(code: ?*anyopaque) ?[*:0]u8 { + return externs.PyBytes_AsString(code); +} + +pub fn PrintError() void { + externs.PyErr_Print(); + + // TODO: fetch and normalize here +} diff --git a/src/root.zig b/src/root.zig new file mode 100644 index 00000000000000..e4b7b25e0ca031 --- /dev/null +++ b/src/root.zig @@ -0,0 +1,4 @@ +pub const types = @import("types.zig"); +pub const externs = @import("externs.zig"); +pub const helpers = @import("helpers.zig"); +pub const constants = @import("constants.zig"); diff --git a/src/types.zig b/src/types.zig new file mode 100644 index 00000000000000..ce6189d0d6c8e2 --- /dev/null +++ b/src/types.zig @@ -0,0 +1,94 @@ +pub const PyConfig = extern struct { + _config_init: c_int, + + isolated: c_int, + use_environment: c_int, + dev_mode: c_int, + install_signal_handlers: c_int, + use_hash_seed: c_int, + hash_seed: u64, + faulthandler: c_int, + tracemalloc: c_int, + import_time: c_int, + show_ref_count: c_int, + dump_refs: c_int, + malloc_stats: c_int, + filesystem_encoding: [*:0]wchar, + filesystem_errors: [*:0]wchar, + pycache_prefix: [*:0]wchar, + parse_argv: c_int, + orig_argv: PyWideStringList, + argv: PyWideStringList, + xoptions: PyWideStringList, + warnoptions: PyWideStringList, + site_import: c_int, + bytes_warning: c_int, + warn_default_encoding: c_int, + inspect: c_int, + interactive: c_int, + optimization_level: c_int, + parser_debug: c_int, + write_bytecode: c_int, + verbose: c_int, + quiet: c_int, + user_site_directory: c_int, + configure_c_stdio: c_int, + buffered_stdio: c_int, + stdio_encoding: [*:0]wchar, + stdio_errors: [*:0]wchar, + check_hash_pycs_mode: [*:0]wchar, + + // --- Path configuration inputs ------------ + pathconfig_warnings: c_int, + program_name: [*:0]wchar, + pythonpath_env: [*:0]wchar, + home: [*:0]wchar, + platlibdir: [*:0]wchar, + + // --- Path configuration outputs ----------- + module_search_paths_set: c_int, + module_search_paths: PyWideStringList, + executable: [*:0]wchar, + base_executable: [*:0]wchar, + prefix: [*:0]wchar, + base_prefix: [*:0]wchar, + exec_prefix: [*:0]wchar, + base_exec_prefix: [*:0]wchar, + + // --- Py_Main() --- + skip_source_first_line: c_int, + run_command: [*:0]wchar, + run_module: [*:0]wchar, + run_filename: [*:0]wchar, + + _install_importlib: c_int, + _init_main: c_int, + _isolated_interpreter: c_int, +}; + +pub const PyWideStringList = extern struct { + length: isize, + items: [*][*]wchar, +}; + +pub const PyPreConfig = extern struct { + _config_init: c_int, + parse_argv: c_int, + isolated: c_int, + use_environment: c_int, + configure_locale: c_int, + coerce_c_locale: c_int, + coerce_c_locale_warn: c_int, + utf8_mode: c_int, + dev_mode: c_int, + allocator: c_int, +}; + +pub const PyStatus = extern struct { + exitcode: c_int, + err_msg: [*:0]const u8, + func: [*:0]const u8, +}; + +pub const wchar = if (builtin.os.tag == .windows) u16 else u32; +const builtin = @import("builtin"); From 4c64536b06db7a85424c73d3d442fddd3270d10b Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Wed, 28 Aug 2024 22:22:32 -0400 Subject: [PATCH 06/16] add binding module --- build.zig | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/build.zig b/build.zig index 512dab6c5ac9a3..2b33f7bc6746fc 100644 --- a/build.zig +++ b/build.zig @@ -25,6 +25,13 @@ pub fn build(b: *std.Build) !void { b.installArtifact(libpython); + const bindings = b.addModule("python", .{ + .root_source_file = b.path("src/root.zig"), + .target = target, + .optimize = optimize, + }); + bindings.linkLibrary(libpython); + const cpython = try buildCpython(b, target, optimize, libpython, config_header); cpython.linkLibrary(libz_dep.artifact("z")); cpython.linkLibrary(openssl_dep.artifact("openssl")); From d7173ae0bb838082cd518cee0718b1a91ca883b4 Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Wed, 28 Aug 2024 22:29:22 -0400 Subject: [PATCH 07/16] add src to paths --- build.zig.zon | 1 + 1 file changed, 1 insertion(+) diff --git a/build.zig.zon b/build.zig.zon index 7e6433c4286092..fa83283d72f019 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -27,6 +27,7 @@ "README.md", "build.zig", "build.zig.zon", + "src", "pyconfig.h.in", }, } From 02d74943dc2c8d7610129de4c21296485e1b5e3e Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Wed, 28 Aug 2024 22:40:52 -0400 Subject: [PATCH 08/16] add PyConfig_InitIsolatedConfig --- src/externs.zig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/externs.zig b/src/externs.zig index e82601b0c3fc90..3271682b2c89e3 100644 --- a/src/externs.zig +++ b/src/externs.zig @@ -32,8 +32,11 @@ pub extern fn PyErr_Fetch(?*anyopaque, ?*anyopaque, ?*anyopaque) void; pub extern fn PyErr_NormalizeException(?*anyopaque, ?*anyopaque, ?*anyopaque) void; pub extern fn PyConfig_InitPythonConfig(*PyConfig) void; +pub extern fn PyConfig_InitIsolatedConfig(*PyConfig) void; pub extern fn PyConfig_Clear(*PyConfig) void; pub extern fn PyConfig_Read(*PyConfig) PyStatus; + +/// https://docs.python.org/3/c-api/init_config.html#c.Py_InitializeFromConfig pub extern fn Py_InitializeFromConfig(*PyConfig) PyStatus; pub extern fn PyWideStringList_Append(*PyWideStringList, [*:0]const u32) PyStatus; From bb743577d23e49b47ff3c00308945910e9433d79 Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Thu, 29 Aug 2024 22:05:32 -0400 Subject: [PATCH 09/16] example works but only if the status is printed? --- build.zig | 65 ++++++++++++++++++++--- examples/simple_string.zig | 105 +++++++++++++++++++++++++++++++++++++ src/constants.zig | 4 ++ src/externs.zig | 4 ++ src/types.zig | 10 +++- 5 files changed, 180 insertions(+), 8 deletions(-) create mode 100644 examples/simple_string.zig diff --git a/build.zig b/build.zig index 2b33f7bc6746fc..1e6f5806750efe 100644 --- a/build.zig +++ b/build.zig @@ -7,6 +7,9 @@ pub fn build(b: *std.Build) !void { const optimize = b.standardOptimizeOption(.{}); const t = target.result; + const lib_options = b.addOptions(); + lib_options.addOption([]const u8, "lib_path", b.fmt("{s}/python/Lib", .{b.install_path})); + const libz_dep = b.dependency("libz", .{ .target = target, .optimize = optimize, @@ -19,26 +22,76 @@ pub fn build(b: *std.Build) !void { const config_header = getConfigHeader(b, t); + // zig build of python const libpython = try buildLibPython(b, target, optimize, config_header); libpython.linkLibrary(libz_dep.artifact("z")); libpython.linkLibrary(openssl_dep.artifact("openssl")); + // actual python code lib + const python_lib = b.addInstallDirectory(.{ + .source_dir = b.path("Lib"), + .install_dir = .{ .custom = "python" }, + .install_subdir = "Lib", + }); + libpython.step.dependOn(&python_lib.step); + b.installArtifact(libpython); - const bindings = b.addModule("python", .{ - .root_source_file = b.path("src/root.zig"), - .target = target, - .optimize = optimize, - }); - bindings.linkLibrary(libpython); + const python_zig_module = getModule(b, target, optimize); + python_zig_module.linkLibrary(libpython); + python_zig_module.addOptions("build_options", lib_options); const cpython = try buildCpython(b, target, optimize, libpython, config_header); cpython.linkLibrary(libz_dep.artifact("z")); cpython.linkLibrary(openssl_dep.artifact("openssl")); cpython.rdynamic = true; b.installArtifact(cpython); + + const examples_step = b.step("examples", "Builds all the examples"); + for (examples) |ex| { + const exe = b.addExecutable(.{ + .name = ex.name, + .root_source_file = b.path(ex.path), + .optimize = optimize, + .target = target, + }); + exe.linkLibrary(libpython); + exe.root_module.addImport("python", python_zig_module); + + addIncludes(b, exe, config_header); + + const run_cmd = b.addRunArtifact(exe); + const run_step = b.step(ex.name, ex.desc); + + run_step.dependOn(&run_cmd.step); + examples_step.dependOn(&exe.step); + } +} + +fn getModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.Mode) *std.Build.Module { + if (b.modules.contains("python")) { + return b.modules.get("python").?; + } + return b.addModule("python", .{ + .root_source_file = b.path("src/root.zig"), + .target = target, + .optimize = optimize, + }); } +const Example = struct { + name: []const u8, + path: []const u8, + desc: []const u8, +}; + +const examples = [_]Example{ + .{ + .name = "simple_string", + .path = "examples/simple_string.zig", + .desc = "Basic eval of python program in a string", + }, +}; fn addIncludes( b: *std.Build, step: *Step.Compile, diff --git a/examples/simple_string.zig b/examples/simple_string.zig new file mode 100644 index 00000000000000..02addbffd3e996 --- /dev/null +++ b/examples/simple_string.zig @@ -0,0 +1,105 @@ +const std = @import("std"); +const py = @import("python"); + +pub fn utf8ToUtf32Z( + in: []const u8, + allocator: std.mem.Allocator, +) ![:0]const u32 { + var buffer = std.ArrayList(u32).init(allocator); + for (in) |char| { + try buffer.append(char); + } + return buffer.toOwnedSliceSentinel(0); +} + +// https://github.com/Rexicon226/osmium/blob/e83ac667e006cf3a233c1868f76e57b155ba1739/src/frontend/Python.zig#L72 +pub fn Initialize( + allocator: std.mem.Allocator, +) !void { + // _ = allocator; + var config: py.types.PyConfig = undefined; + py.externs.PyConfig_InitIsolatedConfig(&config); + defer py.externs.PyConfig_Clear(&config); + + // py.externs.Py_SetProgramName("test"); + + // mute some silly errors that probably do infact matter + // config.pathconfig_warnings = 0; + + // config.program_name = @constCast(try utf8ToUtf32Z("test", allocator)); + + var status = py.externs.PyConfig_SetBytesString( + &config, + &config.program_name, + "./test", + ); + + if (py.externs.PyStatus_Exception(status)) { + py.externs.Py_ExitStatusException(status); + } + + const lib_path = try allocator.dupeZ(u8, py.constants.LIB_PATH); + + status = py.externs.PyConfig_SetBytesString( + &config, + &config.home, + lib_path, + ); + + if (py.externs.PyStatus_Exception(status)) { + py.externs.Py_ExitStatusException(status); + } + + // status = py.externs.PyConfig_SetBytesString( + // &config, + // &config.pythonpath_env, + // lib_path, + // ); + + // if (py.externs.PyStatus_Exception(status)) { + // py.externs.Py_ExitStatusException(status); + // } + + // status = py.externs.PyConfig_Read(&config); + // if (py.externs.PyStatus_Exception(status)) { + // py.externs.Py_ExitStatusException(status); + // } + + std.debug.print("py.constants.LIB_PATH: {s}\n", .{py.constants.LIB_PATH}); + + const utf32_path = try utf8ToUtf32Z( + py.constants.LIB_PATH, + allocator, + ); + + config.module_search_paths_set = 1; + _ = py.externs.PyWideStringList_Append( + &config.module_search_paths, + utf32_path.ptr, + ); + + std.debug.print("config: {}\n", .{config}); + status = py.externs.Py_InitializeFromConfig(&config); + + std.debug.print("END STATUS: {}\n", .{status}); + + if (py.externs.PyStatus_Exception(status)) { + py.externs.Py_ExitStatusException(status); + } + // // needs to be a pointer discard because the stack protector gets overrun? + // _ = &status; +} + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const allocator = gpa.allocator(); + try Initialize(allocator); + defer std.debug.print("exit: {}\n", .{py.externs.Py_Finalize()}); + + // PyRun_SimpleString( + // \\ from time import time,ctime\n + // \\ print('Today is', ctime(time()))\n + // ); + + py.externs.PyRun_SimpleString("print('hi')"); +} diff --git a/src/constants.zig b/src/constants.zig index 54dffab6815e3b..9afaee26985810 100644 --- a/src/constants.zig +++ b/src/constants.zig @@ -1,2 +1,6 @@ pub const Py_file_input: c_int = 257; pub const Py_MARSHAL_VERSION: c_int = 4; + +const build_options = @import("build_options"); + +pub const LIB_PATH = build_options.lib_path; diff --git a/src/externs.zig b/src/externs.zig index 3271682b2c89e3..bf2c2cdc8a4238 100644 --- a/src/externs.zig +++ b/src/externs.zig @@ -6,6 +6,8 @@ const PyStatus = types.PyStatus; const PyWideStringList = types.PyWideStringList; const wchar = types.wchar; +const c_str = [*c]const u8; + pub extern fn Py_PreInitialize(*const PyPreConfig) PyStatus; pub extern fn PyPreConfig_InitPythonConfig(*PyPreConfig) void; pub extern fn PyStatus_Exception(PyStatus) bool; @@ -18,6 +20,8 @@ pub extern fn PySys_SetPath([*:0]const wchar) void; pub extern fn Py_DecRef(?*anyopaque) void; +pub extern fn PyRun_SimpleString(c_str) void; + pub extern fn Py_DecodeLocale([*:0]const u8, *usize) ?[*:0]u8; pub extern fn PyConfig_SetBytesString(*PyConfig, *const [*:0]wchar, [*:0]const u8) PyStatus; pub extern fn Py_SetProgramName([*:0]const u8) void; diff --git a/src/types.zig b/src/types.zig index ce6189d0d6c8e2..8258c5f52e845a 100644 --- a/src/types.zig +++ b/src/types.zig @@ -6,12 +6,14 @@ pub const PyConfig = extern struct { dev_mode: c_int, install_signal_handlers: c_int, use_hash_seed: c_int, - hash_seed: u64, + hash_seed: c_ulong, faulthandler: c_int, tracemalloc: c_int, import_time: c_int, + code_debug_ranges: c_int, show_ref_count: c_int, dump_refs: c_int, + dump_refs_file: [*:0]wchar, malloc_stats: c_int, filesystem_encoding: [*:0]wchar, filesystem_errors: [*:0]wchar, @@ -37,17 +39,20 @@ pub const PyConfig = extern struct { stdio_encoding: [*:0]wchar, stdio_errors: [*:0]wchar, check_hash_pycs_mode: [*:0]wchar, + use_frozen_modules: c_int, + safe_path: c_int, // --- Path configuration inputs ------------ pathconfig_warnings: c_int, program_name: [*:0]wchar, - pythonpath_env: [*:0]wchar, + pythonpath_env: ?[*:0]wchar, home: [*:0]wchar, platlibdir: [*:0]wchar, // --- Path configuration outputs ----------- module_search_paths_set: c_int, module_search_paths: PyWideStringList, + stdlib_dir: [*:0]wchar, executable: [*:0]wchar, base_executable: [*:0]wchar, prefix: [*:0]wchar, @@ -64,6 +69,7 @@ pub const PyConfig = extern struct { _install_importlib: c_int, _init_main: c_int, _isolated_interpreter: c_int, + _is_python_build: c_int, }; pub const PyWideStringList = extern struct { From db7f772a9b67e7d63b19b9db7868d3452eeefe5a Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Thu, 29 Aug 2024 22:25:04 -0400 Subject: [PATCH 10/16] add _type field to pystatus --- examples/simple_string.zig | 41 ++++++++++++++++++++++---------------- src/types.zig | 5 +++++ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/examples/simple_string.zig b/examples/simple_string.zig index 02addbffd3e996..24748352a40f5a 100644 --- a/examples/simple_string.zig +++ b/examples/simple_string.zig @@ -38,17 +38,17 @@ pub fn Initialize( py.externs.Py_ExitStatusException(status); } - const lib_path = try allocator.dupeZ(u8, py.constants.LIB_PATH); + // const lib_path = try allocator.dupeZ(u8, py.constants.LIB_PATH); - status = py.externs.PyConfig_SetBytesString( - &config, - &config.home, - lib_path, - ); + // status = py.externs.PyConfig_SetBytesString( + // &config, + // &config.home, + // lib_path, + // ); - if (py.externs.PyStatus_Exception(status)) { - py.externs.Py_ExitStatusException(status); - } + // if (py.externs.PyStatus_Exception(status)) { + // py.externs.Py_ExitStatusException(status); + // } // status = py.externs.PyConfig_SetBytesString( // &config, @@ -73,11 +73,15 @@ pub fn Initialize( ); config.module_search_paths_set = 1; - _ = py.externs.PyWideStringList_Append( + status = py.externs.PyWideStringList_Append( &config.module_search_paths, utf32_path.ptr, ); + if (py.externs.PyStatus_Exception(status)) { + py.externs.Py_ExitStatusException(status); + } + std.debug.print("config: {}\n", .{config}); status = py.externs.Py_InitializeFromConfig(&config); @@ -87,19 +91,22 @@ pub fn Initialize( py.externs.Py_ExitStatusException(status); } // // needs to be a pointer discard because the stack protector gets overrun? - // _ = &status; + _ = &status; + std.debug.print("END\n", .{}); } pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); try Initialize(allocator); - defer std.debug.print("exit: {}\n", .{py.externs.Py_Finalize()}); + defer py.externs.Py_Finalize(); - // PyRun_SimpleString( - // \\ from time import time,ctime\n - // \\ print('Today is', ctime(time()))\n - // ); + std.debug.print("AFTER INIT\n", .{}); + + py.externs.PyRun_SimpleString( + \\from time import time,ctime + \\print('Today is', ctime(time())) + ); - py.externs.PyRun_SimpleString("print('hi')"); + // py.externs.PyRun_SimpleString("print('hi')"); } diff --git a/src/types.zig b/src/types.zig index 8258c5f52e845a..671cd8d48f1587 100644 --- a/src/types.zig +++ b/src/types.zig @@ -91,6 +91,11 @@ pub const PyPreConfig = extern struct { }; pub const PyStatus = extern struct { + _type: enum(c_int) { + _PyStatus_TYPE_OK = 0, + _PyStatus_TYPE_ERROR = 1, + _PyStatus_TYPE_EXIT = 2, + }, exitcode: c_int, err_msg: [*:0]const u8, func: [*:0]const u8, From 05fd23dbfee80a9582f1a6143e7260d717e7b196 Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Thu, 29 Aug 2024 22:44:31 -0400 Subject: [PATCH 11/16] issue was field ordering being different --- examples/simple_string.zig | 28 +++++++++++++++++++++------- src/types.zig | 4 ++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/examples/simple_string.zig b/examples/simple_string.zig index 24748352a40f5a..48956021cbe593 100644 --- a/examples/simple_string.zig +++ b/examples/simple_string.zig @@ -12,6 +12,17 @@ pub fn utf8ToUtf32Z( return buffer.toOwnedSliceSentinel(0); } +fn init_from_config(config: *py.types.PyConfig) !void { + const status = py.externs.Py_InitializeFromConfig(config); + + // std.debug.print("END STATUS: {}\n", .{&status}); + + if (py.externs.PyStatus_Exception(status)) { + return error.Expection; + // py.externs.Py_ExitStatusException(status); + } +} + // https://github.com/Rexicon226/osmium/blob/e83ac667e006cf3a233c1868f76e57b155ba1739/src/frontend/Python.zig#L72 pub fn Initialize( allocator: std.mem.Allocator, @@ -83,16 +94,19 @@ pub fn Initialize( } std.debug.print("config: {}\n", .{config}); - status = py.externs.Py_InitializeFromConfig(&config); + try init_from_config(&config); + // status = py.externs.Py_InitializeFromConfig(&config); - std.debug.print("END STATUS: {}\n", .{status}); + // // std.debug.print("END STATUS: {}\n", .{&status}); - if (py.externs.PyStatus_Exception(status)) { - py.externs.Py_ExitStatusException(status); - } - // // needs to be a pointer discard because the stack protector gets overrun? - _ = &status; + // if (py.externs.PyStatus_Exception(status)) { + // py.externs.Py_ExitStatusException(status); + // } + // needs to be a pointer discard because the stack protector gets overrun? + // _ = &status; std.debug.print("END\n", .{}); + + py.externs.PyConfig_Clear(&config); } pub fn main() !void { diff --git a/src/types.zig b/src/types.zig index 671cd8d48f1587..021d0edf2383e9 100644 --- a/src/types.zig +++ b/src/types.zig @@ -96,9 +96,9 @@ pub const PyStatus = extern struct { _PyStatus_TYPE_ERROR = 1, _PyStatus_TYPE_EXIT = 2, }, - exitcode: c_int, - err_msg: [*:0]const u8, func: [*:0]const u8, + err_msg: [*:0]const u8, + exitcode: c_int, }; pub const wchar = if (builtin.os.tag == .windows) u16 else u32; From eed878f4ab48a2aa1cea93d6640c1f1f0a04b355 Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Thu, 29 Aug 2024 22:46:06 -0400 Subject: [PATCH 12/16] cleanup example --- examples/simple_string.zig | 51 -------------------------------------- 1 file changed, 51 deletions(-) diff --git a/examples/simple_string.zig b/examples/simple_string.zig index 48956021cbe593..99a394537692bd 100644 --- a/examples/simple_string.zig +++ b/examples/simple_string.zig @@ -32,13 +32,6 @@ pub fn Initialize( py.externs.PyConfig_InitIsolatedConfig(&config); defer py.externs.PyConfig_Clear(&config); - // py.externs.Py_SetProgramName("test"); - - // mute some silly errors that probably do infact matter - // config.pathconfig_warnings = 0; - - // config.program_name = @constCast(try utf8ToUtf32Z("test", allocator)); - var status = py.externs.PyConfig_SetBytesString( &config, &config.program_name, @@ -49,35 +42,6 @@ pub fn Initialize( py.externs.Py_ExitStatusException(status); } - // const lib_path = try allocator.dupeZ(u8, py.constants.LIB_PATH); - - // status = py.externs.PyConfig_SetBytesString( - // &config, - // &config.home, - // lib_path, - // ); - - // if (py.externs.PyStatus_Exception(status)) { - // py.externs.Py_ExitStatusException(status); - // } - - // status = py.externs.PyConfig_SetBytesString( - // &config, - // &config.pythonpath_env, - // lib_path, - // ); - - // if (py.externs.PyStatus_Exception(status)) { - // py.externs.Py_ExitStatusException(status); - // } - - // status = py.externs.PyConfig_Read(&config); - // if (py.externs.PyStatus_Exception(status)) { - // py.externs.Py_ExitStatusException(status); - // } - - std.debug.print("py.constants.LIB_PATH: {s}\n", .{py.constants.LIB_PATH}); - const utf32_path = try utf8ToUtf32Z( py.constants.LIB_PATH, allocator, @@ -93,18 +57,7 @@ pub fn Initialize( py.externs.Py_ExitStatusException(status); } - std.debug.print("config: {}\n", .{config}); try init_from_config(&config); - // status = py.externs.Py_InitializeFromConfig(&config); - - // // std.debug.print("END STATUS: {}\n", .{&status}); - - // if (py.externs.PyStatus_Exception(status)) { - // py.externs.Py_ExitStatusException(status); - // } - // needs to be a pointer discard because the stack protector gets overrun? - // _ = &status; - std.debug.print("END\n", .{}); py.externs.PyConfig_Clear(&config); } @@ -115,12 +68,8 @@ pub fn main() !void { try Initialize(allocator); defer py.externs.Py_Finalize(); - std.debug.print("AFTER INIT\n", .{}); - py.externs.PyRun_SimpleString( \\from time import time,ctime \\print('Today is', ctime(time())) ); - - // py.externs.PyRun_SimpleString("print('hi')"); } From a45daea739b3a329523f7870fe681c102f8c29a4 Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Thu, 29 Aug 2024 22:49:02 -0400 Subject: [PATCH 13/16] add a comment --- examples/simple_string.zig | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/simple_string.zig b/examples/simple_string.zig index 99a394537692bd..e75e629e254654 100644 --- a/examples/simple_string.zig +++ b/examples/simple_string.zig @@ -47,6 +47,8 @@ pub fn Initialize( allocator, ); + // need to set the search path to the python "Lib" folder + // https://docs.python.org/3/c-api/init_config.html#python-path-configuration config.module_search_paths_set = 1; status = py.externs.PyWideStringList_Append( &config.module_search_paths, From dd6239da50e5af44da03959fabcc6f58f0bd1ffd Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Fri, 30 Aug 2024 00:37:34 -0400 Subject: [PATCH 14/16] move logic to helpers --- build.zig | 35 ++++++++++++++++++ examples/simple_string.zig | 65 +------------------------------- src/helpers.zig | 76 +++++++++++++++++++++++++------------- 3 files changed, 86 insertions(+), 90 deletions(-) diff --git a/build.zig b/build.zig index 1e6f5806750efe..4c2c23bfae9c72 100644 --- a/build.zig +++ b/build.zig @@ -21,6 +21,7 @@ pub fn build(b: *std.Build) !void { }); const config_header = getConfigHeader(b, t); + // b.addInstallHeaderFile(source: LazyPath, dest_rel_path: []const u8) // zig build of python const libpython = try buildLibPython(b, target, optimize, config_header); @@ -66,6 +67,17 @@ pub fn build(b: *std.Build) !void { run_step.dependOn(&run_cmd.step); examples_step.dependOn(&exe.step); } + + const translate_step = b.step("translate", "Translates the c files"); + + const config_translate = translateFile(b, b.path("Include/cpython/initconfig.h"), target, optimize, config_header); + const translate_install = b.addInstallDirectory(.{ + .source_dir = config_translate.getOutput(), + .install_dir = .{ .custom = "translate" }, + .install_subdir = "headers", + }); + + translate_step.dependOn(&translate_install.step); } fn getModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.Mode) *std.Build.Module { @@ -103,6 +115,29 @@ fn addIncludes( step.addConfigHeader(config_header); } +fn translateFile( + b: *std.Build, + file: std.Build.LazyPath, + target: std.Build.ResolvedTarget, + optimize: std.builtin.OptimizeMode, + config_header: *std.Build.Step.ConfigHeader, +) *Step.TranslateC { + const step = b.addTranslateC(.{ + .root_source_file = file, + .target = target, + .optimize = optimize, + }); + + step.step.dependOn(&config_header.step); + + step.addIncludeDir(b.path("Include/internal").getPath(b)); + step.addIncludeDir(b.path(".").getPath(b)); + step.addIncludeDir(b.path("Include").getPath(b)); + // step.addIncludeDir(config_header.output_file.getPath()); + + return step; +} + fn buildCpython( b: *std.Build, target: std.Build.ResolvedTarget, diff --git a/examples/simple_string.zig b/examples/simple_string.zig index e75e629e254654..168f30f1c85f63 100644 --- a/examples/simple_string.zig +++ b/examples/simple_string.zig @@ -1,73 +1,10 @@ const std = @import("std"); const py = @import("python"); -pub fn utf8ToUtf32Z( - in: []const u8, - allocator: std.mem.Allocator, -) ![:0]const u32 { - var buffer = std.ArrayList(u32).init(allocator); - for (in) |char| { - try buffer.append(char); - } - return buffer.toOwnedSliceSentinel(0); -} - -fn init_from_config(config: *py.types.PyConfig) !void { - const status = py.externs.Py_InitializeFromConfig(config); - - // std.debug.print("END STATUS: {}\n", .{&status}); - - if (py.externs.PyStatus_Exception(status)) { - return error.Expection; - // py.externs.Py_ExitStatusException(status); - } -} - -// https://github.com/Rexicon226/osmium/blob/e83ac667e006cf3a233c1868f76e57b155ba1739/src/frontend/Python.zig#L72 -pub fn Initialize( - allocator: std.mem.Allocator, -) !void { - // _ = allocator; - var config: py.types.PyConfig = undefined; - py.externs.PyConfig_InitIsolatedConfig(&config); - defer py.externs.PyConfig_Clear(&config); - - var status = py.externs.PyConfig_SetBytesString( - &config, - &config.program_name, - "./test", - ); - - if (py.externs.PyStatus_Exception(status)) { - py.externs.Py_ExitStatusException(status); - } - - const utf32_path = try utf8ToUtf32Z( - py.constants.LIB_PATH, - allocator, - ); - - // need to set the search path to the python "Lib" folder - // https://docs.python.org/3/c-api/init_config.html#python-path-configuration - config.module_search_paths_set = 1; - status = py.externs.PyWideStringList_Append( - &config.module_search_paths, - utf32_path.ptr, - ); - - if (py.externs.PyStatus_Exception(status)) { - py.externs.Py_ExitStatusException(status); - } - - try init_from_config(&config); - - py.externs.PyConfig_Clear(&config); -} - pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); - try Initialize(allocator); + try py.helpers.Initialize(allocator); defer py.externs.Py_Finalize(); py.externs.PyRun_SimpleString( diff --git a/src/helpers.zig b/src/helpers.zig index 50809167e3534a..61aa56a76de2ed 100644 --- a/src/helpers.zig +++ b/src/helpers.zig @@ -1,32 +1,56 @@ const externs = @import("externs.zig"); +const types = @import("types.zig"); const constants = @import("constants.zig"); -pub fn Sys_SetPath(path: [:0]const u16) void { - externs.PySys_SetPath(path.ptr); +const std = @import("std"); + +pub fn utf8ToUtf32Z( + in: []const u8, + allocator: std.mem.Allocator, +) ![:0]const u32 { + var buffer = std.ArrayList(u32).init(allocator); + for (in) |char| { + try buffer.append(char); + } + return buffer.toOwnedSliceSentinel(0); } -pub fn SetProgramName(name: [:0]const u8) void { - externs.Py_SetProgramName(name.ptr); -} - -pub fn CompileString(source: [:0]const u8, filename: [:0]const u8) ?*anyopaque { - return externs.Py_CompileString(source.ptr, filename.ptr, constants.Py_file_input); -} - -pub fn Marshal_WriteObjectToString(code: ?*anyopaque) ?*anyopaque { - return externs.PyMarshal_WriteObjectToString(code, constants.Py_MARSHAL_VERSION); -} - -pub fn Bytes_Size(code: ?*anyopaque) usize { - return externs.PyBytes_Size(code); -} - -pub fn Bytes_AsString(code: ?*anyopaque) ?[*:0]u8 { - return externs.PyBytes_AsString(code); -} - -pub fn PrintError() void { - externs.PyErr_Print(); - - // TODO: fetch and normalize here +pub fn Initialize( + allocator: std.mem.Allocator, +) !void { + var config: types.PyConfig = undefined; + externs.PyConfig_InitIsolatedConfig(&config); + defer externs.PyConfig_Clear(&config); + + var status = externs.PyConfig_SetBytesString( + &config, + &config.program_name, + "./test", + ); + + if (externs.PyStatus_Exception(status)) { + externs.Py_ExitStatusException(status); + } + + const utf32_path = try utf8ToUtf32Z( + constants.LIB_PATH, + allocator, + ); + + // need to set the search path to the python "Lib" folder + // https://docs.python.org/3/c-api/init_config.html#python-path-configuration + config.module_search_paths_set = 1; + status = externs.PyWideStringList_Append( + &config.module_search_paths, + utf32_path.ptr, + ); + + if (externs.PyStatus_Exception(status)) { + externs.Py_ExitStatusException(status); + } + + status = externs.Py_InitializeFromConfig(&config); + if (externs.PyStatus_Exception(status)) { + externs.Py_ExitStatusException(status); + } } From dd3b55aed14a6fa743c92f02fc129cea3684c62b Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Fri, 30 Aug 2024 21:58:37 -0400 Subject: [PATCH 15/16] small cleanup --- build.zig | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/build.zig b/build.zig index 4c2c23bfae9c72..1e6f5806750efe 100644 --- a/build.zig +++ b/build.zig @@ -21,7 +21,6 @@ pub fn build(b: *std.Build) !void { }); const config_header = getConfigHeader(b, t); - // b.addInstallHeaderFile(source: LazyPath, dest_rel_path: []const u8) // zig build of python const libpython = try buildLibPython(b, target, optimize, config_header); @@ -67,17 +66,6 @@ pub fn build(b: *std.Build) !void { run_step.dependOn(&run_cmd.step); examples_step.dependOn(&exe.step); } - - const translate_step = b.step("translate", "Translates the c files"); - - const config_translate = translateFile(b, b.path("Include/cpython/initconfig.h"), target, optimize, config_header); - const translate_install = b.addInstallDirectory(.{ - .source_dir = config_translate.getOutput(), - .install_dir = .{ .custom = "translate" }, - .install_subdir = "headers", - }); - - translate_step.dependOn(&translate_install.step); } fn getModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.Mode) *std.Build.Module { @@ -115,29 +103,6 @@ fn addIncludes( step.addConfigHeader(config_header); } -fn translateFile( - b: *std.Build, - file: std.Build.LazyPath, - target: std.Build.ResolvedTarget, - optimize: std.builtin.OptimizeMode, - config_header: *std.Build.Step.ConfigHeader, -) *Step.TranslateC { - const step = b.addTranslateC(.{ - .root_source_file = file, - .target = target, - .optimize = optimize, - }); - - step.step.dependOn(&config_header.step); - - step.addIncludeDir(b.path("Include/internal").getPath(b)); - step.addIncludeDir(b.path(".").getPath(b)); - step.addIncludeDir(b.path("Include").getPath(b)); - // step.addIncludeDir(config_header.output_file.getPath()); - - return step; -} - fn buildCpython( b: *std.Build, target: std.Build.ResolvedTarget, From 2785263496860a0458e6f99a72d5e60ac09a7924 Mon Sep 17 00:00:00 2001 From: John Murray <5672686+JRMurr@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:04:06 -0400 Subject: [PATCH 16/16] add some comments --- build.zig | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/build.zig b/build.zig index 1e6f5806750efe..8825d5b8a3b148 100644 --- a/build.zig +++ b/build.zig @@ -22,7 +22,6 @@ pub fn build(b: *std.Build) !void { const config_header = getConfigHeader(b, t); - // zig build of python const libpython = try buildLibPython(b, target, optimize, config_header); libpython.linkLibrary(libz_dep.artifact("z")); libpython.linkLibrary(openssl_dep.artifact("openssl")); @@ -92,6 +91,7 @@ const examples = [_]Example{ .desc = "Basic eval of python program in a string", }, }; + fn addIncludes( b: *std.Build, step: *Step.Compile, @@ -103,6 +103,7 @@ fn addIncludes( step.addConfigHeader(config_header); } +/// Build standalone python exectuable fn buildCpython( b: *std.Build, target: std.Build.ResolvedTarget, @@ -132,7 +133,7 @@ fn buildCpython( return cpython; } -// https://github.com/Rexicon226/zig-cpython/blob/master/build.zig +/// Build python as static lib fn buildLibPython( b: *std.Build, target: std.Build.ResolvedTarget,