-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
CMakeLists.txt
475 lines (380 loc) · 15.1 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
cmake_minimum_required(VERSION 3.16.0 FATAL_ERROR)
project(jpegoptim C)
# LIBJPEG_INCLUDE_DIR and LIBJPEG_LIBRARY must both be specified if a custom libjpeg implementation is desired.
option(WITH_ARITH "Enable arithmetic coding (if supported by the libjpeg implementation)" 1)
option(USE_MOZJPEG "Download, build, and link with MozJPEG rather than the system libjpeg. Build with NASM installed for SIMD support." 1)
set(LIBJPEG_INCLUDE_DIR "" CACHE PATH "Custom libjpeg header directory")
set(LIBJPEG_LIBRARY "" CACHE FILEPATH "Custom libjpeg library binary")
if(MSVC)
option(BUILD_NO_SUBFOLDERS "Flatten the compiled program's output path")
endif()
# If LIBJPEG_INCLUDE_DIR and LIBJPEG_LIBRARY are set, USE_MOZJPEG is disabled.
if(LIBJPEG_INCLUDE_DIR AND LIBJPEG_LIBRARY)
set(USE_MOZJPEG 0)
endif()
# Set target architecture if empty. CMake's Visual Studio generator provides it, but others may not.
if(MSVC)
if(NOT CMAKE_VS_PLATFORM_NAME)
set(CMAKE_VS_PLATFORM_NAME "x64")
endif()
message("${CMAKE_VS_PLATFORM_NAME} architecture in use")
else()
add_compile_definitions(HOST_TYPE="${CMAKE_HOST_SYSTEM_NAME}")
endif()
# Global configuration types
set(CMAKE_CONFIGURATION_TYPES
"Debug"
"Release"
CACHE STRING "" FORCE
)
# Global compiler options
if(MSVC)
# remove default compiler flags provided with CMake for MSVC
set(CMAKE_C_FLAGS "")
set(CMAKE_C_FLAGS_DEBUG "")
set(CMAKE_C_FLAGS_RELEASE "")
endif()
# Global linker options
if(MSVC)
# remove default linker flags provided with CMake for MSVC
set(CMAKE_EXE_LINKER_FLAGS "")
set(CMAKE_MODULE_LINKER_FLAGS "")
set(CMAKE_SHARED_LINKER_FLAGS "")
set(CMAKE_STATIC_LINKER_FLAGS "")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_STATIC_LINKER_FLAGS_DEBUG "${CMAKE_STATIC_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS}")
endif()
# Common utils
include(CMake/Utils.cmake)
# Additional Global Settings (add specific info there)
include(CMake/GlobalSettingsInclude.cmake OPTIONAL)
# Use solution folders feature
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# Source groups
set(SOURCE_FILES
jpegoptim.c
jpegsrc.c
jpegdest.c
jpegmarker.c
misc.c
)
source_group("Source Files" FILES ${SOURCE_FILES})
# Target
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
if(MSVC)
use_props(${PROJECT_NAME} "${CMAKE_CONFIGURATION_TYPES}" "${DEFAULT_CXX_PROPS}")
set_target_properties(${PROJECT_NAME} PROPERTIES
VS_GLOBAL_KEYWORD "Win32Proj"
)
endif()
# Output directory
if(MSVC)
if(BUILD_NO_SUBFOLDERS)
set(BINARY_OUTPUT_PATH ".")
else()
set(BINARY_OUTPUT_PATH "$<CONFIG>/${CMAKE_VS_PLATFORM_NAME}")
endif()
set_target_properties(${PROJECT_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${BINARY_OUTPUT_PATH}
RUNTIME_OUTPUT_DIRECTORY_RELEASE ${BINARY_OUTPUT_PATH}
)
endif()
# Interprocedural optimization (LTCG)
set_target_properties(${PROJECT_NAME} PROPERTIES
INTERPROCEDURAL_OPTIMIZATION_RELEASE "TRUE"
)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(${PROJECT_NAME} PUBLIC
-fuse-linker-plugin
)
endif()
# MSVC runtime library
if(MSVC)
get_property(MSVC_RUNTIME_LIBRARY_DEFAULT TARGET ${PROJECT_NAME} PROPERTY MSVC_RUNTIME_LIBRARY)
string(CONCAT "MSVC_RUNTIME_LIBRARY_STR"
$<$<CONFIG:Debug>:
MultiThreadedDebug
>
$<$<CONFIG:Release>:
MultiThreaded
>
$<$<NOT:$<OR:$<CONFIG:Debug>,$<CONFIG:Release>>>:${MSVC_RUNTIME_LIBRARY_DEFAULT}>
)
set_target_properties(${PROJECT_NAME} PROPERTIES MSVC_RUNTIME_LIBRARY ${MSVC_RUNTIME_LIBRARY_STR})
endif()
# Include directories
#target_include_directories(${PROJECT_NAME} PUBLIC
# "${CMAKE_CURRENT_SOURCE_DIR}"
#)
# Compile definitions
target_compile_definitions(${PROJECT_NAME} PRIVATE
"$<$<CONFIG:Debug>:"
"_DEBUG;"
"DEBUG"
">"
"$<$<CONFIG:Release>:"
"NDEBUG"
">"
)
if(MSVC)
target_compile_definitions(${PROJECT_NAME} PRIVATE
"WIN32;"
"_WIN64;"
"WIN64;"
"_WINDOWS;"
"UNICODE;"
"_UNICODE"
)
endif()
# Compile and link options
if(MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE
$<$<CONFIG:Debug>:
/Od; # Disable optimization
/RTC1 # Enable stack frame run-time error checking and reporting when a variable is used without having been initialized.
>
$<$<CONFIG:Release>:
/MP; # Build with multiple processes
/O2; # Optimize for speed
/GF # Enable string pooling
>
/Gy; # Link per-function
/W3; # Warning level
/Zi; # Emit debug info in a separate PDB
/TC; # Compile all source files as C source code regardless of extension
/wd4996; # Suppress deprecation warnings
${DEFAULT_CXX_EXCEPTION_HANDLING};
/GS; # Enable security checks against buffer overruns
/Y- # Disable precompiled headers
)
target_link_options(${PROJECT_NAME} PRIVATE
$<$<CONFIG:Debug>:
/INCREMENTAL # Enable incremental linking (faster builds, larger filesize)
>
$<$<CONFIG:Release>:
/OPT:REF; # Don't link unused functions
/OPT:ICF; # Remove duplicate function definitions
/INCREMENTAL:NO # Disable incremental linking
>
/MANIFEST; # Generate a manifest file
/DEBUG:FULL; # Generate debugging symbols (in a separate PDB file)
/MACHINE:${CMAKE_VS_PLATFORM_NAME};
/SUBSYSTEM:CONSOLE; # Not a driver or GUI program
/NXCOMPAT; # Support Windows Data Execution Prevention
/DYNAMICBASE # Use address space layout randomization
)
# Link with setargv for command line wildcard support
# See https://learn.microsoft.com/en-us/cpp/c-language/expanding-wildcard-arguments
target_link_options(${PROJECT_NAME} PRIVATE
setargv.obj
)
endif()
# Header and function checks
include(CheckIncludeFile)
check_include_file(config.h HAVE_CONFIG_H)
check_include_file(unistd.h HAVE_UNISTD_H)
check_include_file(getopt.h HAVE_GETOPT_H)
check_include_file(string.h HAVE_STRING_H)
check_include_file(libgen.h HAVE_LIBGEN_H)
check_include_file(math.h HAVE_MATH_H)
check_include_file(fcntl.h HAVE_FCNTL_H)
check_include_file(dirent.h HAVE_DIRENT_H)
check_include_file(sys/stat.h HAVE_SYS_STAT_H)
check_include_file(sys/types.h HAVE_SYS_TYPES_H)
check_include_file(sys/wait.h HAVE_SYS_WAIT_H)
include(CheckSymbolExists)
check_symbol_exists(mkstemps "stdlib.h" HAVE_MKSTEMPS)
check_symbol_exists(labs "stdlib.h" HAVE_LABS)
check_symbol_exists(fileno "stdio.h" HAVE_FILENO)
check_symbol_exists(utimensat "sys/stat.h" HAVE_UTIMENSAT)
check_symbol_exists(fork "unistd.h" HAVE_FORK)
check_symbol_exists(wait "sys/wait.h" HAVE_WAIT)
check_symbol_exists(getopt "unistd.h" HAVE_GETOPT)
check_symbol_exists(getopt_long "getopt.h" HAVE_GETOPT_LONG)
include(CheckStructHasMember)
if(HAVE_SYS_STAT_H)
check_struct_has_member(
"struct stat" st_mtim "sys/stat.h" HAVE_STRUCT_STAT_ST_MTIM LANGUAGE C
)
endif()
target_compile_definitions(${PROJECT_NAME} PRIVATE
$<$<BOOL:${HAVE_CONFIG_H}>:HAVE_CONFIG_H>
$<$<BOOL:${HAVE_UNISTD_H}>:HAVE_UNISTD_H>
$<$<BOOL:${HAVE_GETOPT_H}>:HAVE_GETOPT_H>
$<$<BOOL:${HAVE_STRING_H}>:HAVE_STRING_H>
$<$<BOOL:${HAVE_LIBGEN_H}>:HAVE_LIBGEN_H>
$<$<BOOL:${HAVE_MATH_H}>:HAVE_MATH_H>
$<$<BOOL:${HAVE_FCNTL_H}>:HAVE_FCNTL_H>
$<$<BOOL:${HAVE_DIRENT_H}>:HAVE_DIRENT_H>
$<$<BOOL:${HAVE_SYS_STAT_H}>:HAVE_SYS_STAT_H>
$<$<BOOL:${HAVE_SYS_TYPES_H}>:HAVE_SYS_TYPES_H>
$<$<BOOL:${HAVE_SYS_WAIT_H}>:HAVE_SYS_WAIT_H>
$<$<BOOL:${HAVE_MKSTEMPS}>:HAVE_MKSTEMPS>
$<$<BOOL:${HAVE_LABS}>:HAVE_LABS>
$<$<BOOL:${HAVE_FILENO}>:HAVE_FILENO>
$<$<BOOL:${HAVE_UTIMENSAT}>:HAVE_UTIMENSAT>
$<$<BOOL:${HAVE_FORK}>:HAVE_FORK>
$<$<BOOL:${HAVE_WAIT}>:HAVE_WAIT>
$<$<BOOL:${HAVE_GETOPT}>:HAVE_GETOPT>
$<$<BOOL:${HAVE_GETOPT_LONG}>:HAVE_GETOPT_LONG>
$<$<BOOL:${HAVE_STRUCT_STAT_ST_MTIM}>:HAVE_STRUCT_STAT_ST_MTIM>
)
# Include getopt only if no native implementation found.
if(NOT HAVE_GETOPT_LONG)
target_sources(${PROJECT_NAME} PRIVATE getopt.c getopt1.c)
endif()
# Attach a manifest file to support UTF-8 on compatible Windows systems (see https://learn.microsoft.com/en-us/windows/apps/design/globalizing/use-utf8-code-page)
if(MSVC)
add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND "mt.exe" -nologo -manifest \"${CMAKE_CURRENT_SOURCE_DIR}/jpegoptim.manifest\" -outputresource:"${CMAKE_CURRENT_BINARY_DIR}/${BINARY_OUTPUT_PATH}/jpegoptim.exe"\;\#1
COMMENT "Adding manifest..."
)
endif()
# Dependencies
if(USE_MOZJPEG)
# Link with mozjpeg.
# Version tree: https://github.com/mozilla/mozjpeg/tree/fd569212597dcc249752bd38ea58a4e2072da24f
include(ExternalProject)
if(WITH_ARITH)
set(ARITH_FLAGS -DWITH_ARITH_DEC=1 -DWITH_ARITH_ENC=1)
set(JPEGLIB_SUPPORTS_ARITH_CODE 1)
endif()
ExternalProject_Add(mozjpeg_lib
GIT_REPOSITORY https://github.com/mozilla/mozjpeg.git
GIT_TAG fd569212597dcc249752bd38ea58a4e2072da24f
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/mozjpeg
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/mozjpeg -DPNG_SUPPORTED=0 -DWITH_TURBOJPEG=0 -DENABLE_SHARED=0 ${ARITH_FLAGS}
)
# Building and linking mozjpeg as a library, as explained here https://mirkokiefer.com/cmake-by-example-f95eb47d45b1
ExternalProject_Get_Property(mozjpeg_lib install_dir)
add_library(mozjpeg STATIC IMPORTED)
if(MSVC)
set_property(TARGET mozjpeg PROPERTY IMPORTED_LOCATION ${install_dir}/lib/jpeg-static.lib)
else()
target_link_libraries(mozjpeg INTERFACE m)
set_property(TARGET mozjpeg PROPERTY IMPORTED_LOCATION ${install_dir}/lib/libjpeg.a)
endif()
add_dependencies(mozjpeg mozjpeg_lib)
target_include_directories(${PROJECT_NAME} BEFORE PRIVATE ${install_dir}/include)
target_link_libraries(${PROJECT_NAME} mozjpeg)
add_dependencies(${PROJECT_NAME} mozjpeg)
# Note: check_include_file, check_symbol_exists, check_struct_has_member, and check_c_source_compiles
# cannot be used on ExternalProject dependencies because they are not compiled until build time, while check_*
# functions run during the initial CMake configuration step.
# Since the version is hardcoded above, feature checks may be set as constants as a workaround.
set(HAVE_JINT_DC_SCAN_OPT_MODE 1)
else()
if(LIBJPEG_INCLUDE_DIR AND LIBJPEG_LIBRARY)
# Link with custom libjpeg
add_library(libjpeg STATIC IMPORTED)
if(NOT MSVC)
target_link_libraries(libjpeg INTERFACE m)
endif()
set_target_properties(libjpeg PROPERTIES IMPORTED_LOCATION ${LIBJPEG_LIBRARY})
target_include_directories(${PROJECT_NAME} BEFORE PRIVATE ${LIBJPEG_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} libjpeg)
add_dependencies(${PROJECT_NAME} libjpeg)
else()
# Link with system libjpeg
include(FindJPEG)
if(NOT JPEG_FOUND)
message(FATAL_ERROR "Could not automatically locate libjpeg. Either specify -DUSE_MOZJPEG=1 to download and build with MozJPEG, or -DLIBJPEG_INCLUDE_DIR=... and -DLIBJPEG_LIBRARY=... to the appropriate paths to build with a custom libjpeg implementation.")
endif()
message(STATUS "Include dirs: ${JPEG_INCLUDE_DIRS}")
target_include_directories(${PROJECT_NAME} PRIVATE ${JPEG_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} JPEG::JPEG)
endif()
# Use all include directories and linked libraries as the main project for feature tests
get_target_property(CMAKE_REQUIRED_LIBRARIES ${PROJECT_NAME} LINK_LIBRARIES)
get_target_property(CMAKE_REQUIRED_INCLUDES ${PROJECT_NAME} INCLUDE_DIRECTORIES)
# check_include_file, check_symbol_exists, and check_struct_has_member cannot be used with libjpeg.h
# because libjpeg.h requires stdio.h to be included before it to not throw an unrelated compilation error.
include(CheckCSourceCompiles)
check_c_source_compiles(
"
#include <stdio.h>
#include <jpeglib.h>
int main(void)
{
return sizeof (&jpeg_read_header);
}
"
HAVE_APPROPRIATE_LIBJPEG_VERSION
)
if(NOT HAVE_APPROPRIATE_LIBJPEG_VERSION)
message(FATAL_ERROR "Invalid version: libjpeg version 6 or later is required.")
endif()
check_c_source_compiles(
"
#include <stdio.h>
#include <jpeglib.h>
METHODDEF(void) foo(void) {};
int main(void)
{
return 0;
}
"
WORKING_METHODDEF
)
if(NOT WORKING_METHODDEF)
target_compile_definitions(${PROJECT_NAME} PRIVATE -DBROKEN_METHODDEF)
endif()
if(WITH_ARITH)
# Check for arithmetic coding support
check_c_source_compiles(
"
#include <stdio.h>
#include <jpeglib.h>
int main(void)
{
return sizeof (((struct jpeg_compress_struct *)0)->arith_code);
}
"
JPEGLIB_SUPPORTS_ARITH_CODE
)
endif()
# Check for MozJPEG's JINT_DC_SCAN_OPT_MODE extension
check_c_source_compiles(
"
#include <stdio.h>
#include <jpeglib.h>
int main(void)
{
struct jpeg_compress_struct cinfo;
if (jpeg_c_int_param_supported(&cinfo, JINT_DC_SCAN_OPT_MODE))
jpeg_c_set_int_param(&cinfo, JINT_DC_SCAN_OPT_MODE, 1);
return 0;
}
"
HAVE_JINT_DC_SCAN_OPT_MODE
)
endif()
target_compile_definitions(${PROJECT_NAME} PRIVATE
$<$<BOOL:${HAVE_JINT_DC_SCAN_OPT_MODE}>:HAVE_JINT_DC_SCAN_OPT_MODE>
)
if(WITH_ARITH AND JPEGLIB_SUPPORTS_ARITH_CODE)
set(ARITH_ENABLED 1)
target_compile_definitions(${PROJECT_NAME} PRIVATE -DHAVE_ARITH_CODE)
endif()
if(ARITH_ENABLED)
message(STATUS "Arithmetic Coding: Enabled")
else()
message(STATUS "Arithmetic Coding: Disabled")
endif()
find_package(Python3 COMPONENTS Interpreter Development)
if (Python3_FOUND)
enable_testing()
add_test(NAME unittests
COMMAND ${Python3_EXECUTABLE} test.py
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
)
set_property(TEST unittests PROPERTY ENVIRONMENT "JPEGOPTIM=$<TARGET_FILE:jpegoptim>")
endif()