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

Fix platform check for macOS #895

Closed
wants to merge 1 commit into from

Conversation

zixu-w
Copy link

@zixu-w zixu-w commented Dec 12, 2023

zutils.h contains configurations that are no longer exercised for the macOS target.

  • The target OS conditional macros are misused. Specifically TARGET_OS_MAC covers all Apple targets, including iOS, and it should not be checked with #if defined as they would always be defined (to either 1 or 0) on Apple platforms.
  • The assumption that macOS does not have fdopen, or defines fdopen as a macro is no longer valid. The null definition in zutils.h would conflict with the macOS SDK and cause a compilation failure.

This problem has not been noticed until a recent extension in clang (llvm/llvm-project#74676) exposed the issue and broke zlib builds on Apple platforms.

`zutils.h` contains configurations that are no longer exercised for
the macOS target.
- The target OS conditional macros are misused. Specifically
  `TARGET_OS_MAC` covers all Apple targets, including iOS,
  and it should not be checked with `#if defined` as they would
  always be defined (to either 1 or 0) on Apple platforms.
- The assumption that macOS does not have `fdopen`, or defines `fdopen`
  as a macro is no longer valid. The null definition in `zutils.h` would
  conflict with the macOS SDK and cause a compilation failure.

This problem has not been noticed until a recent extension in clang
(llvm/llvm-project#74676) exposed the issue
and broke zlib builds on Apple platforms.
@madler
Copy link
Owner

madler commented Dec 12, 2023

This problem has not been noticed until a recent extension in clang (llvm/llvm-project#74676) exposed the issue and broke zlib builds on Apple platforms.

zlib still compiles fine on the latest clang that Apple delivers, 15.0.0. What version of clang does it break on, and exactly how does it break?

@zixu-w
Copy link
Author

zixu-w commented Dec 12, 2023

This problem has not been noticed until a recent extension in clang (llvm/llvm-project#74676) exposed the issue and broke zlib builds on Apple platforms.

zlib still compiles fine on the latest clang that Apple delivers, 15.0.0. What version of clang does it break on, and exactly how does it break?

Thanks for taking a look Mark!
The open source change will get into a future Apple clang at some point.
The build failure can be reproduced currently by adding a line

#include <TargetConditionals.h>

before the #if defined(TARGET_OS_MAC) check.

@madler
Copy link
Owner

madler commented Dec 13, 2023

Thanks. Fixed.

@madler madler closed this Dec 13, 2023
@zixu-w zixu-w deleted the zutil-target-os-mac-misuse branch December 15, 2023 01:07
@Neustradamus
Copy link

@madler: I think that some commits are missing since 2023-11-15, can you push it?
Thanks in advance.

@glandium
Copy link

Note that the #if __APPLE block further down the same file redefines OS_CODE.

@@ -137,15 +137,11 @@ extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
# endif
#endif

#if defined(MACOS) || defined(TARGET_OS_MAC)
#if TARGET_OS_OSX
# define OS_CODE 7
# ifndef Z_SOLO
# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
Copy link

@glandium glandium Jan 17, 2024

Choose a reason for hiding this comment

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

The history of this code is interesting.
zlib 0.71 (the first version in git) had:

#ifdef MACOS
#  define OS_CODE  0x07
#endif

zlib 1.0.8 added the fdopen thing, but differently:

#if defined(MACOS) || defined(TARGET_OS_MAC)
#  define OS_CODE  0x07
#  ifndef fdopen
#    define fdopen(fd,mode) NULL /* No fdopen() */
#  endif
#endif
#if defined(__MWERKS__) && !defined(fdopen)
#  if __dest_os != __be_os && __dest_os != __win32_os
#    define fdopen(fd,mode) NULL
#  endif
#endif

The corresponding changelog:

  • check for TARGET_OS_MAC in addition to MACOS (Brad Pettit)
  • do not use fdopen for Metrowerks on Mac (Brad Pettit))

zlib 1.1.3 changed it to:

#if defined(MACOS) || defined(TARGET_OS_MAC)
#  define OS_CODE  0x07
#  if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
#    include <unix.h> /* for fdopen */
#  else
#    ifndef fdopen
#      define fdopen(fd,mode) NULL /* No fdopen() */
#    endif
#  endif
#endif

I think the corresponding changelog entry is:

  • Support gzdopen on Mac with Metrowerks (Jason Linhart)

Presumably, Metrowerks was defining TARGET_OS_MAC, and probably didn't define TARGET_OS_OSX, so my assumption here is that this change breaks Metrowerks. However, it's also a long gone compiler, so maybe this part can go entirely, which leaves us with two different OS_CODE values for macOS, one of which was added in ce12c5c

zixu-w added a commit to zixu-w/libpng that referenced this pull request Jan 18, 2024
In a similar manner as zlib (madler/zlib#895),
libpng contains a header configuration that's no longer valid and
hasn't been exercised for the macOS target.

- The target OS conditional macros are misused. Specifically
  `TARGET_OS_MAC` covers all Apple targets, including iOS, and it
  should not be checked with `#if defined` as they would always be
  defined (to either 1 or 0) on Apple platforms.
- `#include <fp.h>` no longer works for the macOS target and results
  in a compilation failure. macOS ships all required functions in
  `math.h`, and clients should use `math.h` instead.

This problem has not been noticed until a recent extension in clang
(llvm/llvm-project#74676) exposed the issue
and broke libpng builds on Apple platforms. The failure can be
reproduced now by adding `#include <TargetConditionals.h>` before the
block.
ctruta pushed a commit to ctruta/libpng that referenced this pull request Jan 18, 2024
In a similar manner as zlib (madler/zlib#895),
libpng contains a header configuration that's no longer valid and
hasn't been exercised for the macOS target.

- The target OS conditional macros are misused. Specifically
  `TARGET_OS_MAC` covers all Apple targets, including iOS, and it
  should not be checked with `#if defined` as they would always be
  defined (to either 1 or 0) on Apple platforms.
- `#include <fp.h>` no longer works for the macOS target and results
  in a compilation failure. macOS ships all required functions in
  `math.h`, and clients should use `math.h` instead.

This problem has not been noticed until a recent extension in clang
(llvm/llvm-project#74676) exposed the issue
and broke libpng builds on Apple platforms. The failure can be
reproduced now by adding `#include <TargetConditionals.h>` before the
block.

Signed-off-by: Cosmin Truta <ctruta@gmail.com>
@Neustradamus
Copy link

It is good with the current devel branch?

@zixu-w
Copy link
Author

zixu-w commented Jan 23, 2024

It is good with the current devel branch?

I believe 4bd9a71 resolved the issue so yeah it should be good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants