Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor fixes to build on windows + enable build of command line tool with cmake #30

Merged
merged 4 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 66 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ set (VERSION "0.9.2")

option(BUILD_STATIC_LIB "Build static library" ON)
option(BUILD_SHARED_LIB "Build shared library" ON)

option(BUILD_PROGRAM "Build command line tool" OFF)

# Build configuration
# this project needs libjpeg and libexif.
# in order to build the shared library, the static or shared libraries of libjpeg
# and libexif must be found.
# if they are not in a standard location, you can specify:
# CMAKE_FIND_ROOT_PATH or JPEG_INCLUDE_DIRECTORY/EXIF_INCLUDE_DIRECTORY and
# JPEG_LIB_DIRECTORY/EXIF_LIB_DIRECTORY
################################################################################

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
Expand All @@ -16,7 +22,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Platform specific build configuration
################################################################################

if(MSVC)
if(WIN32)
message(STATUS "Building for windows")
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()
Expand All @@ -29,61 +35,91 @@ endif()
file(GLOB_RECURSE libepeg_SRC src/lib/*.c)
list(SORT libepeg_SRC)


set(libepeg_HEADER src/lib/Epeg.h)

include(CheckIncludeFile)

if(DEFINED JPEG_INCLUDE_DIRECTORY)
include_directories(BEFORE SYSTEM "${JPEG_INCLUDE_DIRECTORY}")
list(APPEND CMAKE_REQUIRED_INCLUDES ${JPEG_INCLUDE_DIRECTORY})
endif()
file(GLOB_RECURSE epeg_SRC src/bin/*.c)
list(SORT epeg_SRC)

if(DEFINED EXIF_INCLUDE_DIRECTORY)
include_directories(BEFORE SYSTEM "${EXIF_INCLUDE_DIRECTORY}")
list(APPEND CMAKE_REQUIRED_INCLUDES ${EXIF_INCLUDE_DIRECTORY})
endif()

CHECK_INCLUDE_FILE(jerror.h HAS_LIBJPEG)
if(NOT HAS_LIBJPEG)
find_path(JPEG_DIR_HEADER NAME jerror.h HINTS "${JPEG_INCLUDE_DIRECTORY}")
if (NOT JPEG_DIR_HEADER)
message(FATAL_ERROR "Error: jerror.h not found, you must first install libjpeg or specify JPEG_INCLUDE_DIRECTORY!")
endif()

message(STATUS "Found jerror.h...")
include_directories(BEFORE SYSTEM "${JPEG_DIR_HEADER}")


CHECK_INCLUDE_FILE(libexif/exif-data.h HAS_LIBEXIF)
if(NOT HAS_LIBEXIF)
find_path(EXIF_DIR_HEADER NAME libexif/exif-data.h HINTS "${EXIF_INCLUDE_DIRECTORY}")
if (NOT EXIF_DIR_HEADER)
message(FATAL_ERROR "Error: libexif/exif-data.h not found, you must first install libexif or specify EXIF_INCLUDE_DIRECTORY!")
endif()

message(STATUS "Found libexif/exif-data.h...")
include_directories(BEFORE SYSTEM "${EXIF_DIR_HEADER}")


if(CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_options(-fdata-sections -ffunction-sections)
endif()

# Build
################################################################################

# Build library
if (BUILD_SHARED_LIB)
message("* Building shared library")
if (BUILD_SHARED_LIB OR BUILD_PROGRAM)
if(NOT DEFINED JPEG_LIB_DIRECTORY)
set(JPEG_LIB_DIRECTORY ${JPEG_DIR_HEADER}/../lib)
endif()
find_library(JPEG_LIBRARY NAMES jpeg HINTS ${JPEG_LIB_DIRECTORY})
if (NOT JPEG_LIBRARY)
message(FATAL_ERROR "Error: cant find jpeg library, you must first install libjpeg or specify JPEG_LIB_DIRECTORY!")
endif()


if(NOT DEFINED EXIF_LIB_DIRECTORY)
set(EXIF_LIB_DIRECTORY ${EXIF_DIR_HEADER}/../lib)
endif()
find_library(EXIF_LIBRARY NAMES exif HINTS ${EXIF_LIB_DIRECTORY})
if (NOT EXIF_LIBRARY)
message(FATAL_ERROR "Error: cant find exif library, you must first install libexif or specify EXIF_LIB_DIRECTORY!")
endif()
endif()

if(BUILD_SHARED_LIB)
message(STATUS "Building shared library")
add_library(epeg_shared SHARED ${libepeg_SRC})
target_link_libraries(epeg_shared ${EXTRA_LIBS})
if(WIN32)
target_compile_definitions(epeg_shared PUBLIC BUILDING_DLL)
endif()
target_link_libraries(epeg_shared ${JPEG_LIBRARY} ${EXIF_LIBRARY})
set_target_properties(epeg_shared PROPERTIES OUTPUT_NAME epeg)

install(TARGETS epeg_shared DESTINATION lib)
endif()

if (BUILD_STATIC_LIB)
message("* Building static library")
if (BUILD_STATIC_LIB OR BUILD_PROGRAM)
message(STATUS "Building static library")
add_library(epeg_static STATIC ${libepeg_SRC})
target_link_libraries(epeg_static ${EXTRA_LIBS})
set_target_properties(epeg_static PROPERTIES OUTPUT_NAME epeg)
install(TARGETS epeg_static DESTINATION lib)
endif()

if (BUILD_PROGRAM)
set(dll_lib m)
add_executable(epeg_bin ${epeg_SRC})
message(STATUS "Building command line tool")
target_link_libraries(epeg_bin epeg_static ${JPEG_LIBRARY} ${EXIF_LIBRARY})
target_link_libraries(epeg_bin -Wl,--gc-sections)
target_link_libraries(epeg_bin -Wl,-Bdynamic ${dll_lib})
set_target_properties(epeg_bin PROPERTIES OUTPUT_NAME epeg)
install(TARGETS epeg_bin DESTINATION bin)
endif()

if (NOT EXISTS src/lib/config.h)
file(WRITE src/lib/config.h "")
endif()


if (BUILD_SHARED_LIB)
install(TARGETS epeg_shared DESTINATION lib)
endif()
if (BUILD_STATIC_LIB)
install(TARGETS epeg_static DESTINATION lib)
endif()

install(FILES ${libepeg_HEADER} DESTINATION include)

2 changes: 1 addition & 1 deletion src/bin/epeg_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ main(int argc, char **argv)
}

if (max_dimension > 0) {
if (w > h ^ inset_flag) {
if ((w > h) ^ inset_flag) {
thumb_width = max_dimension;
thumb_height = max_dimension * h / w;
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/Epeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#ifdef WIN32
# ifdef BUILDING_DLL
# define EAPI __declspec(dllexport)
# else
# elif defined(USE_EPEG_DLL)
# define EAPI __declspec(dllimport)
# else
# define EAPI
# endif
#else
# ifdef __GNUC__
Expand Down