cmake: improve: mz.h, mz_os.h are public API, shouldn't rely on user -D (#978)

This commit is contained in:
Sergey 2026-04-20 17:26:22 -07:00 committed by GitHub
parent bf4c563d7c
commit 2f074dc2b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 52 additions and 35 deletions

3
.gitignore vendored
View file

@ -8,3 +8,6 @@ third_party/
# IDE
.vscode
# cmake generated
/mz_config.h

View file

@ -144,19 +144,6 @@ check_include_file(sys/dirent.h HAVE_SYS_DIRENT_H)
check_include_file(inttypes.h HAVE_INTTYPES_H)
check_include_file(stdint.h HAVE_STDINT_H)
if(HAVE_DIRENT_H)
list(APPEND STDLIB_DEF -DHAVE_DIRENT_H)
endif()
if(HAVE_SYS_DIRENT_H)
list(APPEND STDLIB_DEF -DHAVE_SYS_DIRENT_H)
endif()
if(HAVE_INTTYPES_H)
list(APPEND STDLIB_DEF -DHAVE_INTTYPES_H)
endif()
if(HAVE_STDINT_H)
list(APPEND STDLIB_DEF -DHAVE_STDINT_H)
endif()
# Check DIR* exists
cmake_push_check_state(RESET)
if(HAVE_DIRENT_H)
@ -167,9 +154,6 @@ cmake_push_check_state(RESET)
endif()
check_type_size(DIR* PDIR)
cmake_pop_check_state()
if(HAVE_PDIR)
list(APPEND STDLIB_DEF -DHAVE_PDIR)
endif()
# Check for large file support
check_type_size(off64_t OFF64_T)
@ -179,19 +163,12 @@ if(HAVE_OFF64_T)
endif()
# Check for fseeko support
check_function_exists(fseeko HAVE_FSEEKO)
if(NOT HAVE_FSEEKO)
list(APPEND STDLIB_DEF -DNO_FSEEKO)
endif()
# Check for fseeko symlink
check_function_exists(symlink HAVE_SYMLINK)
if(NOT HAVE_SYMLINK)
list(APPEND STDLIB_DEF -DNO_SYMLINK)
endif()
# Check for fseeko readlink
check_function_exists(readlink HAVE_READLINK)
if(NOT HAVE_READLINK)
list(APPEND STDLIB_DEF -DNO_READLINK)
endif()
configure_file(mz_config.h.cmakein ${CMAKE_CURRENT_SOURCE_DIR}/mz_config.h @ONLY NEWLINE_STYLE UNIX)
if(MZ_LIBCOMP)
if(APPLE)

6
mz.h
View file

@ -161,7 +161,9 @@
#include <string.h> /* memset, strncpy, strlen */
#include <limits.h>
#if defined(HAVE_STDINT_H)
#include "mz_config.h"
#if HAVE_STDINT_H
# include <stdint.h>
#elif defined(__has_include)
# if __has_include(<stdint.h>)
@ -194,7 +196,7 @@ typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
#endif
#if defined(HAVE_INTTYPES_H)
#if HAVE_INTTYPES_H
# include <inttypes.h>
#elif defined(__has_include)
# if __has_include(<inttypes.h>)

28
mz_config.h.cmakein Normal file
View file

@ -0,0 +1,28 @@
#ifndef MZ_CONFIG_H
#define MZ_CONFIG_H
// Define to 1 if you have the <dirent.h> header file.
#cmakedefine01 HAVE_DIRENT_H
// Define to 1 if you have the <sys/dirent.h> header file.
#cmakedefine01 HAVE_SYS_DIRENT_H
// Define to 1 if you have the <inttypes.h> header file.
#cmakedefine01 HAVE_INTTYPES_H
// Define to 1 if you have the <stdint.h> header file.
#cmakedefine01 HAVE_STDINT_H
// Define to 1 if DIR* is defined.
#cmakedefine01 HAVE_PDIR
// Define to 1 if fseeko() is defined.
#cmakedefine01 HAVE_FSEEKO
// Define to 1 if symlink() is defined.
#cmakedefine01 HAVE_SYMLINK
// Define to 1 if readlink() is defined.
#cmakedefine01 HAVE_READLINK
#endif

12
mz_os.h
View file

@ -11,10 +11,6 @@
#ifndef MZ_OS_H
#define MZ_OS_H
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
#if defined(__APPLE__)
@ -49,6 +45,8 @@ extern "C" {
/***************************************************************************/
#include "mz_config.h"
#if HAVE_DIRENT_H
# include <dirent.h>
#elif HAVE_SYS_DIRENT_H
@ -63,6 +61,12 @@ struct dirent {
typedef struct DIR DIR;
#endif
/***************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
/* Shared functions */

View file

@ -9,6 +9,7 @@
*/
#include "mz.h"
#include "mz_config.h"
#include "mz_strm.h"
#include "mz_os.h"
@ -324,7 +325,7 @@ int32_t mz_os_is_symlink(const char *path) {
}
int32_t mz_os_make_symlink(const char *path, const char *target_path) {
#if defined(NO_SYMLINK)
#if !HAVE_SYMLINK
return MZ_SUPPORT_ERROR;
#else
if (symlink(target_path, path) != 0)
@ -334,7 +335,7 @@ int32_t mz_os_make_symlink(const char *path, const char *target_path) {
}
int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_target_path) {
#if defined(NO_READLINK)
#if !HAVE_READLINK
return MZ_SUPPORT_ERROR;
#else
size_t length = 0;

View file

@ -14,6 +14,7 @@
*/
#include "mz.h"
#include "mz_config.h"
#include "mz_strm.h"
#include "mz_strm_os.h"
@ -27,7 +28,7 @@
#define fopen64 fopen
#ifndef MZ_FILE32_API
# ifndef NO_FSEEKO
# if HAVE_FSEEKO
# define ftello64 ftello
# define fseeko64 fseeko
# elif defined(_MSC_VER) && (_MSC_VER >= 1400)

View file

@ -9,6 +9,7 @@
*/
#include "mz.h"
#include "mz_config.h"
#include "mz_os.h"
#include "mz_zip.h"
@ -156,7 +157,7 @@ static void test_unzip_compat(unzFile unzip) {
}
# ifndef MZ_FILE32_API
# ifndef NO_FSEEKO
# if HAVE_FSEEKO
# define ftello64 ftello
# define fseeko64 fseeko
# elif defined(_MSC_VER) && (_MSC_VER >= 1400)