Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-114099: Add preprocessor declarations to support compilation on iOS #115023

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions Include/internal/pycore_faulthandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ extern "C" {
# include <signal.h> // 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
Comment on lines +19 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

#endif /* TVOS || WATCHOS */

#ifndef MS_WINDOWS
/* register() is useless on Windows, because only SIGSEGV, SIGABRT and
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Preprocessor directives to support compilation on iOS/tvOS/watchOS were
added.
35 changes: 35 additions & 0 deletions Misc/platform_triplet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
9 changes: 9 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@

#include <mach/mach.h>

#include "TargetConditionals.h"

#if defined(__has_builtin)
#if __has_builtin(__builtin_available)
#define HAVE_BUILTIN_AVAILABLE 1
Expand Down Expand Up @@ -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
Comment on lines +381 to +384
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's a compiler error, as opposed to a runtime error, then autoconf should be able to detect the problem. The comment should explain why that wasn't sufficient.

# undef HAVE_SYSTEM
#endif

/*[clinic input]
# one of the few times we lie about this name!
module os
Expand Down
36 changes: 36 additions & 0 deletions Modules/pwdmodule.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

/* UNIX password file access module */

#ifdef __APPLE__
# include "TargetConditionals.h"
#endif /* __APPLE__ */

#include "Python.h"
#include "posixmodule.h"

Expand Down Expand Up @@ -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";
Copy link
Member

@mhsmith mhsmith Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it's easy enough to Google, but there should still be a comment explaining what this value is and where it comes from.

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;
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "pycore_namespace.h" // _PyNamespace_New()
#include "pycore_runtime.h" // _Py_ID()

#ifdef __APPLE__
# include "TargetConditionals.h"
#endif /* __APPLE__ */

#include <time.h> // clock()
#ifdef HAVE_SYS_TIMES_H
# include <sys/times.h> // times()
Expand Down Expand Up @@ -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
Comment on lines +66 to +68
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

#endif

#define SEC_TO_NS (1000 * 1000 * 1000)

Expand Down
10 changes: 10 additions & 0 deletions Python/bootstrap_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +47 to +50
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

#endif

#ifdef Py_DEBUG
int _Py_HashSecret_Initialized = 0;
#else
Expand Down
25 changes: 19 additions & 6 deletions Python/dynload_shlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
};
Expand Down
14 changes: 11 additions & 3 deletions Python/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]*/
Expand All @@ -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'
Expand Down