diff --git a/Include/internal/pycore_faulthandler.h b/Include/internal/pycore_faulthandler.h index 6dd7d8d7ca9792..836c8a3fcf1ea7 100644 --- a/Include/internal/pycore_faulthandler.h +++ b/Include/internal/pycore_faulthandler.h @@ -12,6 +12,14 @@ extern "C" { # include // sigaction #endif +#ifdef __APPLE__ +# include "TargetConditionals.h" +#endif /* __APPLE__ */ + +// tvOS and watchOS don't provide a number of important POSIX functions. +#if TARGET_OS_TV || TARGET_OS_WATCH +# undef HAVE_SIGALTSTACK +#endif /* TVOS || WATCHOS */ #ifndef MS_WINDOWS /* register() is useless on Windows, because only SIGSEGV, SIGABRT and diff --git a/Misc/NEWS.d/next/Library/2024-02-05-14-55-19.gh-issue-114099.iHk3Zn.rst b/Misc/NEWS.d/next/Library/2024-02-05-14-55-19.gh-issue-114099.iHk3Zn.rst new file mode 100644 index 00000000000000..7e363e65187c7c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-02-05-14-55-19.gh-issue-114099.iHk3Zn.rst @@ -0,0 +1,2 @@ +Preprocessor directives to support compilation on iOS/tvOS/watchOS were +added. diff --git a/Misc/platform_triplet.c b/Misc/platform_triplet.c index 3307260544e8a6..b5db9e8a80db31 100644 --- a/Misc/platform_triplet.c +++ b/Misc/platform_triplet.c @@ -233,7 +233,42 @@ PLATFORM_TRIPLET=i386-gnu # error unknown platform triplet # endif #elif defined(__APPLE__) +# include "TargetConditionals.h" +# if TARGET_OS_IOS +# if TARGET_OS_SIMULATOR +# if __x86_64__ +PLATFORM_TRIPLET=iphonesimulator-x86_64 +# else +PLATFORM_TRIPLET=iphonesimulator-arm64 +# endif +# else +PLATFORM_TRIPLET=iphoneos-arm64 +# endif +# elif TARGET_OS_TV +# if TARGET_OS_SIMULATOR +# if __x86_64__ +PLATFORM_TRIPLET=appletvsimulator-x86_64 +# else +PLATFORM_TRIPLET=appletvsimulator-arm64 +# endif +# else +PLATFORM_TRIPLET=appletvos-arm64 +# endif +# elif TARGET_OS_WATCH +# if TARGET_OS_SIMULATOR +# if __x86_64__ +PLATFORM_TRIPLET=watchsimulator-x86_64 +# else +PLATFORM_TRIPLET=watchsimulator-arm64 +# endif +# else +PLATFORM_TRIPLET=watchos-arm64_32 +# endif +# elif TARGET_OS_OSX PLATFORM_TRIPLET=darwin +# else +# error unknown Apple platform +# endif #elif defined(__VXWORKS__) PLATFORM_TRIPLET=vxworks #elif defined(__wasm32__) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 40ff131b119d66..813b3f9c8012e2 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -92,6 +92,8 @@ #include +#include "TargetConditionals.h" + #if defined(__has_builtin) #if __has_builtin(__builtin_available) #define HAVE_BUILTIN_AVAILABLE 1 @@ -376,6 +378,13 @@ corresponding Unix manual entries for more information on calls."); # define fsync _commit #endif /* ! __WATCOMC__ || __QNX__ */ +// iOS/tvOS/watchOS *define* some POSIX methods, +// but raise a compiler error if they are used. +#if TARGET_OS_IPHONE +# undef HAVE_GETGROUPS +# undef HAVE_SYSTEM +#endif + /*[clinic input] # one of the few times we lie about this name! module os diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c index b7034369c4731e..d5806f6db9f484 100644 --- a/Modules/pwdmodule.c +++ b/Modules/pwdmodule.c @@ -1,6 +1,10 @@ /* UNIX password file access module */ +#ifdef __APPLE__ +# include "TargetConditionals.h" +#endif /* __APPLE__ */ + #include "Python.h" #include "posixmodule.h" @@ -183,6 +187,22 @@ pwd_getpwuid(PyObject *module, PyObject *uidobj) if (nomem == 1) { return PyErr_NoMemory(); } + +// iPhone has a "user" with UID 501, username "mobile"; but the simulator +// doesn't reflect this. Generate a simulated response. +#if TARGET_OS_SIMULATOR + if (uid == 501) { + struct passwd mp; + mp.pw_name = "mobile"; + mp.pw_passwd = "/smx7MYTQIi2M"; + mp.pw_uid = 501; + mp.pw_gid = 501; + mp.pw_gecos = "Mobile User"; + mp.pw_dir = "/var/mobile"; + mp.pw_shell = "/bin/sh"; + return mkpwent(module, &mp); + } +#endif PyObject *uid_obj = _PyLong_FromUid(uid); if (uid_obj == NULL) return NULL; @@ -266,6 +286,22 @@ pwd_getpwnam_impl(PyObject *module, PyObject *name) PyErr_NoMemory(); } else { +// iPhone has a "user" with UID 501, username "mobile"; but the simulator +// doesn't reflect this. Generate a simulated response. +#if TARGET_OS_SIMULATOR + if (strcmp(name, "mobile") == 0) { + struct passwd mp; + mp.pw_name = "mobile"; + mp.pw_passwd = "/smx7MYTQIi2M"; + mp.pw_uid = 501; + mp.pw_gid = 501; + mp.pw_gecos = "Mobile User"; + mp.pw_dir = "/var/mobile"; + mp.pw_shell = "/bin/sh"; + retval = mkpwent(module, &mp); + goto out; + } +#endif PyErr_Format(PyExc_KeyError, "getpwnam(): name not found: %R", name); } diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 2b0d3900dbddd6..81addae19c738a 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -6,6 +6,10 @@ #include "pycore_namespace.h" // _PyNamespace_New() #include "pycore_runtime.h" // _Py_ID() +#ifdef __APPLE__ +# include "TargetConditionals.h" +#endif /* __APPLE__ */ + #include // clock() #ifdef HAVE_SYS_TIMES_H # include // times() @@ -59,6 +63,10 @@ # define HAVE_CLOCK_GETTIME_RUNTIME 1 #endif +// iOS/tvOS/watchOS *define* clock_settime, but it can't be used +#if TARGET_OS_IPHONE +# undef HAVE_CLOCK_SETTIME +#endif #define SEC_TO_NS (1000 * 1000 * 1000) diff --git a/Python/bootstrap_hash.c b/Python/bootstrap_hash.c index 92f2301a012c0a..d1ab940c018699 100644 --- a/Python/bootstrap_hash.c +++ b/Python/bootstrap_hash.c @@ -40,6 +40,16 @@ #endif +#ifdef __APPLE__ +# include "TargetConditionals.h" +#endif /* __APPLE__ */ + +// iOS/tvOS/watchOS *define* some POSIX methods, +// but raise a compiler error if they are used. +#if TARGET_OS_IPHONE +# undef HAVE_GETENTROPY +#endif + #ifdef Py_DEBUG int _Py_HashSecret_Initialized = 0; #else diff --git a/Python/dynload_shlib.c b/Python/dynload_shlib.c index 5a37a83805ba78..92b632af228e50 100644 --- a/Python/dynload_shlib.c +++ b/Python/dynload_shlib.c @@ -28,6 +28,10 @@ #define LEAD_UNDERSCORE "" #endif +#ifdef __APPLE__ +# include "TargetConditionals.h" +#endif /* __APPLE__ */ + /* The .so extension module ABI tag, supplied by the Makefile via Makefile.pre.in and configure. This is used to discriminate between incompatible .so files so that extensions for different Python builds can @@ -38,12 +42,21 @@ const char *_PyImport_DynLoadFiletab[] = { #ifdef __CYGWIN__ ".dll", #else /* !__CYGWIN__ */ - "." SOABI ".so", -#ifdef ALT_SOABI - "." ALT_SOABI ".so", -#endif - ".abi" PYTHON_ABI_STRING ".so", - ".so", +# ifdef __APPLE__ +# if TARGET_OS_IPHONE +# define SHLIB_SUFFIX ".dylib" +# else +# define SHLIB_SUFFIX ".so" +# endif +# else +# define SHLIB_SUFFIX ".so" +# endif + "." SOABI SHLIB_SUFFIX, +# ifdef ALT_SOABI + "." ALT_SOABI SHLIB_SUFFIX, +# endif + ".abi" PYTHON_ABI_STRING SHLIB_SUFFIX, + SHLIB_SUFFIX, #endif /* __CYGWIN__ */ NULL, }; diff --git a/Python/marshal.c b/Python/marshal.c index daec7415b3fc7e..d36a8ff69d9cd2 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -14,6 +14,10 @@ #include "pycore_setobject.h" // _PySet_NextEntry() #include "marshal.h" // Py_MARSHAL_VERSION +#ifdef __APPLE__ +# include "TargetConditionals.h" +#endif /* __APPLE__ */ + /*[clinic input] module marshal [clinic start generated code]*/ @@ -33,11 +37,15 @@ module marshal * #if defined(MS_WINDOWS) && defined(_DEBUG) */ #if defined(MS_WINDOWS) -#define MAX_MARSHAL_STACK_DEPTH 1000 +# define MAX_MARSHAL_STACK_DEPTH 1000 #elif defined(__wasi__) -#define MAX_MARSHAL_STACK_DEPTH 1500 +# define MAX_MARSHAL_STACK_DEPTH 1500 #else -#define MAX_MARSHAL_STACK_DEPTH 2000 +# if TARGET_OS_IPHONE +# define MAX_MARSHAL_STACK_DEPTH 1500 +# else +# define MAX_MARSHAL_STACK_DEPTH 2000 +# endif #endif #define TYPE_NULL '0'