@@ -5,6 +5,8 @@ project(jpegoptim C)
55# LIBJPEG_INCLUDE_DIR and LIBJPEG_LIBRARY must both be specified if a custom libjpeg implementation is desired.
66option (WITH_ARITH "Enable arithmetic coding (if supported by the libjpeg implementation)" 1)
77option (USE_MOZJPEG "Download, build, and link with MozJPEG rather than the system libjpeg. Build with NASM installed for SIMD support." 1)
8+ option (BUILD_FUZZERS "Build harnesses with instrumentation" 0)
9+
810set (LIBJPEG_INCLUDE_DIR "" CACHE PATH "Custom libjpeg header directory" )
911set (LIBJPEG_LIBRARY "" CACHE FILEPATH "Custom libjpeg library binary" )
1012if (MSVC )
@@ -83,7 +85,6 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
8385
8486
8587# Source groups
86-
8788set (SOURCE_FILES
8889 jpegoptim.c
8990 jpegsrc.c
@@ -93,10 +94,20 @@ set(SOURCE_FILES
9394 )
9495source_group ("Source Files" FILES ${SOURCE_FILES} )
9596
96-
9797# Target
98-
99- add_executable (${PROJECT_NAME} ${SOURCE_FILES} )
98+ if (BUILD_FUZZERS)
99+ add_compile_definitions (BUILD_FOR_OSS_FUZZ=1)
100+ add_compile_options (-Wno-implicit-function-declaration)
101+ add_library (${PROJECT_NAME} ${SOURCE_FILES} )
102+ target_include_directories (${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} )
103+
104+ # Temporarily remove fuzzing flags that would break compiler checks
105+ set (ORIG_C_FLAGS "${CMAKE_C_FLAGS} " )
106+ string (REGEX REPLACE "-fsanitize=[^ ]+" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS} " )
107+ string (REGEX REPLACE "-fsanitize=[^ ]+" "" $ENV{CFLAGS} "${CMAKE_C_FLAGS} " )
108+ else ()
109+ add_executable (${PROJECT_NAME} ${SOURCE_FILES} )
110+ endif ()
100111
101112if (MSVC )
102113 use_props(${PROJECT_NAME} "${CMAKE_CONFIGURATION_TYPES} " "${DEFAULT_CXX_PROPS} " )
@@ -308,6 +319,7 @@ endif()
308319# Dependencies
309320
310321if (USE_MOZJPEG)
322+
311323 # Link with mozjpeg.
312324 # Version tree: https://github.com/mozilla/mozjpeg/tree/fd569212597dcc249752bd38ea58a4e2072da24f
313325
@@ -318,11 +330,17 @@ if(USE_MOZJPEG)
318330 set (JPEGLIB_SUPPORTS_ARITH_CODE 1)
319331 endif ()
320332
333+ if (BUILD_FUZZERS)
334+ set (MOZJPEG_EXTENDED_CMAKE_FLAGS -DCMAKE_C_FLAGS="" )
335+ else ()
336+ set (MOZJPEG_EXTENDED_CMAKE_FLAGS "" )
337+ endif ()
338+
321339 ExternalProject_Add(mozjpeg_lib
322340 GIT_REPOSITORY https://github.com/mozilla/mozjpeg.git
323341 GIT_TAG fd569212597dcc249752bd38ea58a4e2072da24f
324342 PREFIX ${CMAKE_CURRENT_BINARY_DIR} /mozjpeg
325- CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH =${CMAKE_CURRENT_BINARY_DIR} /mozjpeg -DPNG_SUPPORTED=0 -DWITH_TURBOJPEG=0 -DENABLE_SHARED=0 ${ARITH_FLAGS}
343+ CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH =${CMAKE_CURRENT_BINARY_DIR} /mozjpeg -DPNG_SUPPORTED=0 -DWITH_TURBOJPEG=0 -DENABLE_SHARED=0 ${ARITH_FLAGS} ${MOZJPEG_EXTENDED_CMAKE_FLAGS}
326344 )
327345
328346
@@ -476,6 +494,11 @@ if (Python3_FOUND)
476494endif ()
477495
478496
497+ if (BUILD_FUZZERS AND DEFINED ENV{LIB_FUZZING_ENGINE})
498+ set (CMAKE_C_FLAGS "${ORIG_C_FLAGS} " )
499+ add_subdirectory (fuzz)
500+ endif ()
501+
479502install (TARGETS ${PROJECT_NAME} )
480503install (FILES jpegoptim.1 TYPE MAN)
481504install (FILES README COPYRIGHT LICENSE TYPE DOC )
0 commit comments