11cmake_minimum_required (VERSION 3.20 )
22
3+ # TODO: Can this be replaced with a toolchain file?
34set (CMAKE_LINK_DEPENDS_NO_SHARED TRUE )
4- set (CMAKE_POSITION_INDEPENDENT_CODE OFF )
5- set (CMAKE_SHARED_LIBRARY_C_FLAGS "" )
65
76set (CMAKE_C_COMPILER clang)
87
98set (SDK_PATH ${CMAKE_SOURCE_DIR} /common-3.0.sdk)
10-
119set (CMAKE_LINKER ${SDK_PATH} /usr/bin/ld)
1210set (LIPO ${SDK_PATH} /usr/bin/lipo)
1311
14- set (CMAKE_C_CREATE_SHARED_LIBRARY "<CMAKE_LINKER> <LINK_FLAGS> -o <TARGET> <OBJECTS>" ) # Force CMake to use custom linker
12+ set (CMAKE_OSX_SYSROOT ${SDK_PATH} )
13+ set (CMAKE_OSX_ARCHITECTURES armv6 armv7)
14+ set (CMAKE_OSX_DEPLOYMENT_TARGET 3.0)
15+ set (CMAKE_C_COMPILER_TARGET arm-apple -ios3.0)
16+
17+ set (CMAKE_C_CREATE_SHARED_LIBRARY
18+ "<CMAKE_LINKER> <LINK_FLAGS> -o <TARGET> <OBJECTS>" ) # Force CMake to use
19+ # custom linker
1520
16- project (zlib VERSION 1.2.3 LANGUAGES C )
21+ # Setting OSX Flags breaks the test on Ubuntu. Claim it works as a workaround
22+ # (since it does)
23+ set (CMAKE_C_COMPILER_WORKS TRUE )
24+ #
25+
26+ project (
27+ zlib
28+ VERSION 1.2.3
29+ LANGUAGES C )
1730
1831set (LIB_NAME "libz.1.dylib" )
1932set (INSTALL_PATH /usr/lib)
2033set (VERSION 1.2.3)
2134set (COMPAT_VERSION 1.0.0)
2235
2336include (FetchContent )
24-
25- # Auto fetch the zlib repo
2637FetchContent_Declare (
27- zlib
28- GIT_REPOSITORY https://github.com/apple -oss-distributions/zlib
29- GIT_TAG zlib-23.0.1
30- )
38+ zlib
39+ GIT_REPOSITORY https://github.com/apple -oss-distributions/zlib
40+ GIT_TAG zlib-23.0.1)
3141FetchContent_MakeAvailable (zlib)
3242
33- file (GLOB SRCS "${zlib_SOURCE_DIR} /zlib/*.c" )
34-
35- set (CFLAGS_COMMON
36- -isysroot ${SDK_PATH}
37- -pipe
38- -std=gnu99
39- -Wno-deprecated-non-prototype
40- -Wno-incompatible-sysroot
41- -fno-stack-protector
42- -DUSE_MMAP
43- -DNDEBUG
44- --target=arm-apple -ios3.0
45- )
46-
47- set (LDFLAGS_COMMON
48- ${SDK_PATH} /usr/lib/dylib1.o
49- -L${SDK_PATH} /usr/lib
50- -lSystem.B
51- -lgcc_s.1
52- -exported_symbols_list ${zlib_SOURCE_DIR} /libz.exp
53- -install_name ${INSTALL_PATH} /${LIB_NAME}
54- -dead_strip
55- -dylib
56- -single_module
57- -dead_strip_dylibs
58- -prebind
59- -dynamic
60- -S
61- -twolevel_namespace
62- -twolevel_namespace_hints
63- -ios_version_min 3.0
43+ set (ZLIB_SOURCES
44+ ${zlib_SOURCE_DIR} /zlib/adler32.c
45+ ${zlib_SOURCE_DIR} /zlib/compress.c
46+ ${zlib_SOURCE_DIR} /zlib/crc32.c
47+ ${zlib_SOURCE_DIR} /zlib/deflate.c
48+ ${zlib_SOURCE_DIR} /zlib/gzio.c
49+ ${zlib_SOURCE_DIR} /zlib/infback.c
50+ ${zlib_SOURCE_DIR} /zlib/inffast.c
51+ ${zlib_SOURCE_DIR} /zlib/inflate.c
52+ ${zlib_SOURCE_DIR} /zlib/inftrees.c
53+ ${zlib_SOURCE_DIR} /zlib/trees.c
54+ ${zlib_SOURCE_DIR} /zlib/uncompr.c
55+ ${zlib_SOURCE_DIR} /zlib/zutil.c)
56+
57+ add_compile_options (
58+ -Wno-deprecated-non-prototype -Wno-incompatible-sysroot
59+ -Wno-expansion-to-defined -fno-stack-protector # Workardound for missing
60+ # __stack_chk_*
6461)
6562
66- # Function to create a target for each architecture
67- function (add_arch_target ARCH )
68- add_library (${ARCH} _target SHARED ${SRCS} )
69-
70- set_target_properties (${ARCH} _target PROPERTIES
71- OUTPUT_NAME "${LIB_NAME} .${ARCH} "
72- PREFIX ""
73- SUFFIX ""
74- )
75-
76- target_compile_options (${ARCH} _target PRIVATE
77- ${CFLAGS_COMMON}
78- -arch ${ARCH}
79- )
80-
81- target_link_options (${ARCH} _target PRIVATE
82- ${LDFLAGS_COMMON}
83- -arch ${ARCH}
84- -image_base 0x301f6000
85- )
86-
87- target_include_directories (${ARCH} _target PRIVATE
88- ${CMAKE_SOURCE_DIR} /zlib
89- ${SDK_PATH} /usr/include
90- )
91-
92- endfunction ()
93-
94- add_arch_target (armv6 )
95- add_arch_target (armv7 )
96-
97- add_custom_target (${LIB_NAME} ALL
98- COMMAND ${LIPO} -create
99- ${CMAKE_BINARY_DIR} /${LIB_NAME}.armv6
100- ${CMAKE_BINARY_DIR} /${LIB_NAME}.armv7
101- -output ${CMAKE_BINARY_DIR} /${LIB_NAME}
102- DEPENDS armv6_target armv7_target
103- )
63+ add_compile_definitions (USE_MMAP NDEBUG )
64+
65+ # There are more idiomatic ways of setting some of these but stuff like
66+ # target_link_libraries was not working for some reason
67+ add_link_options (
68+ ${SDK_PATH} /usr/lib/dylib1.o
69+ -L${SDK_PATH}/usr/lib
70+ -lSystem.B
71+ -lgcc_s.1
72+ -ios_version_min
73+ ${CMAKE_OSX_DEPLOYMENT_TARGET}
74+ -exported_symbols_list
75+ ${zlib_SOURCE_DIR} /libz.exp
76+ -dead_strip
77+ -dead_strip_dylibs
78+ -dylib
79+ -single_module
80+ -prebind
81+ -dynamic
82+ -twolevel_namespace
83+ -twolevel_namespace_hints
84+ -image_base
85+ 0x301f6000
86+ -install_name
87+ ${INSTALL_PATH} /${LIB_NAME} )
88+
89+ set (arch_targets "" )
90+ # Create a target for each architecture
91+ foreach (ARCH IN LISTS CMAKE_OSX_ARCHITECTURES)
92+ set (target_name libz_${ARCH} )
93+ list (APPEND arch_targets ${target_name} )
94+ add_library (${target_name} SHARED ${ZLIB_SOURCES} )
95+
96+ set_target_properties (
97+ ${target_name}
98+ PROPERTIES OUTPUT_NAME "${LIB_NAME} .${ARCH} "
99+ INSTALL_NAME_DIR "${INSTALL_PATH} "
100+ PREFIX ""
101+ SUFFIX ""
102+ POSITION_INDEPENDENTCODE OFF
103+ C_STANDARD 99 )
104+
105+ target_include_directories (${target_name} PRIVATE ${CMAKE_SOURCE_DIR} /zlib
106+ ${SDK_PATH} /usr/include )
107+
108+ target_compile_options (${target_name} PRIVATE -arch ${ARCH} )
109+
110+ target_link_options (${target_name} PRIVATE -arch ${ARCH} )
111+ endforeach ()
112+
113+ set (lipo_inputs "" )
114+ foreach (ARCH IN LISTS CMAKE_OSX_ARCHITECTURES)
115+ list (APPEND lipo_inputs "${CMAKE_BINARY_DIR} /${LIB_NAME} .${ARCH} " )
116+ endforeach ()
117+
118+ add_custom_target (
119+ ${LIB_NAME} ALL
120+ COMMAND ${LIPO} -create ${lipo_inputs} -output ${CMAKE_BINARY_DIR} /${LIB_NAME}
121+ DEPENDS ${arch_targets} )
0 commit comments