forked from selmf/unarr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
242 lines (216 loc) · 7.13 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(unarr VERSION 1.1.0 LANGUAGES C)
set(PROJECT_DESCRIPTION
"A decompression library for rar, tar and zip files.")
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
# Set build type to default if unset.
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
"MinSizeRel" "RelWithDebInfo")
endif()
# Build options
option(BUILD_SHARED_LIBS "Build ${PROJECT_NAME} as a shared library" ON)
option(BUILD_SAMPLES "Build unarr-test executable" OFF)
option(ENABLE_7Z "Build with 7z support" OFF)
option(USE_SYSTEM_BZ2 "Build with system bzip2 if possible" ON)
option(USE_SYSTEM_LZMA "Build with system lzma/xz if possible" ON)
option(USE_SYSTEM_ZLIB "Build with system zlib if possible" ON)
# Build target
add_library(unarr
_7z/_7z.h
_7z/_7z.c
common/allocator.h
common/unarr-imp.h
common/conv.c
common/crc32.c
#common/custalloc.c
common/stream.c
common/unarr.c
lzmasdk/7zTypes.h
lzmasdk/Compiler.h
lzmasdk/CpuArch.h
lzmasdk/Ppmd.h
lzmasdk/Ppmd7.h
lzmasdk/Ppmd8.h
lzmasdk/Precomp.h
lzmasdk/CpuArch.c
lzmasdk/Ppmd7.c
lzmasdk/Ppmd8.c
lzmasdk/Ppmd7Dec.c
lzmasdk/Ppmd8Dec.c
rar/lzss.h
rar/rar.h
rar/rarvm.h
rar/filter-rar.c
rar/uncompress-rar.c
rar/huffman-rar.c
rar/rar.c
rar/rarvm.c
rar/parse-rar.c
tar/tar.h
tar/parse-tar.c
tar/tar.c
zip/inflate.h
zip/zip.h
zip/inflate.c
zip/parse-zip.c
zip/uncompress-zip.c
zip/zip.c)
set_target_properties(unarr PROPERTIES
PUBLIC_HEADER unarr.h
C_VISIBILITY_PRESET hidden
C_STANDARD 99
C_STANDARD_REQUIRED ON
DEFINE_SYMBOL UNARR_EXPORT_SYMBOLS
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR})
# Add include directories for build and install
target_include_directories(unarr PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)
if(BUILD_SHARED_LIBS)
target_compile_definitions(unarr PUBLIC UNARR_IS_SHARED_LIBRARY)
endif()
if(USE_SYSTEM_BZ2)
find_package(BZip2)
endif()
if(BZIP2_FOUND)
if(TARGET BZip2::BZip23)
target_link_libraries(unarr PRIVATE BZip2::BZip2)
else()
target_include_directories(unarr PRIVATE ${BZIP_INCLUDE_DIRS})
target_link_libraries(unarr PRIVATE ${BZIP2_LIBRARIES})
endif()
target_compile_definitions(unarr PRIVATE -DHAVE_BZIP)
# Bzip2 upstream does not supply a .pc file. Add it to Libs.private.
set(PROJECT_LIBS_PRIVATE "-I${BZIP2_INCLUDE_DIRS} -l${BZIP2_LIBRARIES}")
set(UNARR_DEPENDS_BZip2 "find_dependency(BZip2)")
endif()
if(USE_SYSTEM_LZMA)
find_package(LibLZMA)
endif()
if(LIBLZMA_FOUND)
if(TARGET LibLZMA::LibLZMA)
target_link_libraries(unarr PRIVATE LibLZMA::LibLZMA)
else()
target_include_directories(unarr PRIVATE ${LIBLZMA_INCLUDE_DIRS})
target_link_libraries(unarr PRIVATE ${LIBLZMA_LIBRARIES})
endif()
target_compile_definitions(unarr PRIVATE -DHAVE_LIBLZMA)
set(PROJECT_REQUIRES_PRIVATE "${PROJECT_REQUIRES_PRIVATE} liblzma")
set(UNARR_DEPENDS_LibLZMA "find_dependency(LibLZMA)")
else()
target_sources(unarr PRIVATE
lzmasdk/LzmaDec.h
lzmasdk/LzmaDec.c)
endif()
if(USE_SYSTEM_ZLIB)
find_package(ZLIB)
endif()
if(ZLIB_FOUND)
if(TARGET ZLIB::ZLIB)
target_link_libraries(unarr PRIVATE ZLIB::ZLIB)
else()
target_include_directories(unarr PRIVATE ${ZLIB_INCLUDE_DIRS})
target_link_libraries(unarr PRIVATE ${ZLIB_LIBRARIES})
endif()
target_compile_definitions(unarr PRIVATE -DHAVE_ZLIB)
# Add zlib to libunarr.pc Requires.private
set(PROJECT_REQUIRES_PRIVATE "${PROJECT_REQUIRES_PRIVATE} zlib")
set(UNARR_DEPENDS_ZLIB "find_dependency(ZLIB)")
endif()
if(ENABLE_7Z)
target_sources(unarr PRIVATE
lzmasdk/7z.h
lzmasdk/7zArcIn.c
lzmasdk/7zBuf.h
lzmasdk/7zBuf.c
lzmasdk/7zDec.c
lzmasdk/7zStream.c
lzmasdk/Bcj2.h
lzmasdk/Bcj2.c
lzmasdk/Bra.c
lzmasdk/Bra.h
lzmasdk/Bra86.c
lzmasdk/BraIA64.c
lzmasdk/7zCrc.h
lzmasdk/Delta.h
lzmasdk/Delta.c
lzmasdk/Lzma2Dec.h
lzmasdk/Lzma2Dec.c)
if(LIBLZMA_FOUND)
target_sources(unarr PRIVATE
lzmasdk/LzmaDec.h
lzmasdk/LzmaDec.c)
endif()
target_compile_definitions(unarr PRIVATE -DHAVE_7Z -D_7ZIP_PPMD_SUPPPORT)
endif()
if(BUILD_SAMPLES)
add_executable(unarr-test
test/main.c)
add_dependencies(unarr-test unarr)
target_link_libraries(unarr-test unarr)
endif()
# Compiler specific settings
if(UNIX OR MINGW OR MSYS)
target_compile_options(unarr PRIVATE -Wall -Wextra -pedantic
-Wstrict-prototypes -Wmissing-prototypes
-Werror-implicit-function-declaration
$<$<CONFIG:Release>:-fomit-frame-pointer>
$<$<OR:$<C_COMPILER_ID:Clang>,$<C_COMPILER_ID:AppleClang>>:
-Wno-missing-field-initializers>
-flto)
target_compile_definitions(unarr PRIVATE -D_FILE_OFFSET_BITS=64)
# Linker flags
# Clang linker needs -flto too when doing lto
if("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
set_target_properties(unarr PROPERTIES LINK_FLAGS
"-Wl,--no-undefined -Wl,--as-needed -flto")
# Apple ld uses different syntax for undefined symbol check
elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang")
set_target_properties(unarr PROPERTIES LINK_FLAGS
"-Wl,-undefined,error -flto")
else()
set_target_properties(unarr PROPERTIES LINK_FLAGS
"-Wl,--no-undefined -Wl,--as-needed")
endif()
endif()
if(MSVC)
target_compile_options(unarr PRIVATE /W3
$<$<CONFIG:Release>:/Ox>)
target_compile_definitions(unarr PRIVATE _CRT_SECURE_NO_WARNINGS)
endif()
# Write pkg-config file
configure_file("pkg-config.pc.cmake" "lib${PROJECT_NAME}.pc" @ONLY)
# Install library and header
install(TARGETS unarr
EXPORT unarr-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# Install pkg-config file
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${PROJECT_NAME}.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
# Export and install targets
install(EXPORT unarr-targets
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/unarr
NAMESPACE unarr::)
# Make project importable from build dir
export(TARGETS unarr
FILE unarr-targets.cmake
NAMESPACE unarr::)
# Write a config file for installation
configure_package_config_file(
unarr-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/unarr-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/unarr)
# Write version file
write_basic_package_version_file(unarr-version.cmake
COMPATIBILITY AnyNewerVersion)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/unarr-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/unarr-version.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/unarr)