diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..02ab737 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,90 @@ +# Copyright (c) 2022 Bartek Fabiszewski +# http://www.fabiszewski.net +# +# This file is part of libmobi. +# Licensed under LGPL, either version 3, or any later. +# See + +cmake_minimum_required(VERSION 3.12) + +project(LIBMOBI C) + +set(CMAKE_C_STANDARD 99) + +file(STRINGS ${LIBMOBI_SOURCE_DIR}/configure.ac VERSION_LINE REGEX "AC_INIT\\(\\[libmobi\\], \\[(.*)\\]\\)") +string(REGEX MATCH "([0-9]+\\.[0-9]+)" PACKAGE_VERSION "${VERSION_LINE}") +message(STATUS "libmobi version ${PACKAGE_VERSION}") +add_definitions(-DPACKAGE_VERSION="${PACKAGE_VERSION}") +string(REPLACE "." ";" VERSION_LIST ${PACKAGE_VERSION}) +list(GET VERSION_LIST 0 PACKAGE_VERSION_MAJOR) +list(GET VERSION_LIST 1 PACKAGE_VERSION_MINOR) + +# Option to enable encryption +option(USE_ENCRYPTION "Enable encryption" ON) + +# Option to enable static tools compilation +option(TOOLS_STATIC "Enable static tools compilation" OFF) + +# Option to use libxml2 +option(USE_LIBXML2 "Use libxml2 instead of internal xmlwriter" ON) + +# Option to use zlib +option(USE_ZLIB "Use zlib" ON) + +# Option to enable XMLWRITER +option(USE_XMLWRITER "Enable xmlwriter (for opf support)" ON) + +# Option to enable debug +option(MOBI_DEBUG "Enable debug" OFF) + +# Option to enable debug alloc +option(MOBI_DEBUG_ALLOC "Enable debug alloc" OFF) + +option(BUILD_SHARED_LIBS "Build using shared libraries" ON) + +if(TOOLS_STATIC) + set(BUILD_SHARED_LIBS OFF) +endif(TOOLS_STATIC) + +if(USE_ENCRYPTION) + add_definitions(-DUSE_ENCRYPTION) +endif(USE_ENCRYPTION) + +if(USE_XMLWRITER) + add_definitions(-DUSE_XMLWRITER) + if(USE_LIBXML2) + add_definitions(-DUSE_LIBXML2) + find_package(LibXml2 REQUIRED) + include_directories(${LIBXML2_INCLUDE_DIR}) + endif(USE_LIBXML2) +endif(USE_XMLWRITER) + +if(MOBI_DEBUG) + add_definitions(-DMOBI_DEBUG) + add_compile_options(-pedantic -Wall -Wextra -Werror) +endif(MOBI_DEBUG) + +if(MOBI_DEBUG_ALLOC) + add_definitions(-DMOBI_DEBUG_ALLOC) +endif(MOBI_DEBUG_ALLOC) + +if(USE_ZLIB) + find_package(ZLIB REQUIRED) + include_directories(${ZLIB_INCLUDE_DIR}) +else() + add_definitions(-DUSE_MINIZ) +endif(USE_ZLIB) + +include(CheckIncludeFile) +include(CheckFunctionExists) +check_include_file(unistd.h HAVE_UNISTD_H) +if(HAVE_UNISTD_H) + add_definitions(-DHAVE_UNISTD_H) +endif(HAVE_UNISTD_H) +check_function_exists(getopt HAVE_GETOPT) +if(HAVE_GETOPT) + add_definitions(-DHAVE_GETOPT) +endif(HAVE_GETOPT) + +add_subdirectory(src) +add_subdirectory(tools) diff --git a/ChangeLog b/ChangeLog index 6c2a04f..fc2e5f3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ +2022-02-27: Add basic CMake support 2022-02-26: GHA: fetch tags with checkout 2022-02-26: Minor refactoring of file path manipulation function 2022-02-26: Fix memory handling issues diff --git a/README.md b/README.md index 7273e89..15c76d4 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,9 @@ The tools source may also be used as an example on how to use the library. On macOS, you can install via [Homebrew](https://brew.sh/) with `brew install libmobi`. -## Optionally provided Xcode and MSVC++ project files +## Alternative build systems +- The supported way of building project is by using autotools. +- Optionally project provides basic support for CMake, Xcode and MSVC++ systems. However these alternative configurations are not covering all options of autotools project. They are also not tested and not updated regularly. ## Usage - single include file: `#include ` diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..879898d --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,89 @@ +# Copyright (c) 2022 Bartek Fabiszewski +# http://www.fabiszewski.net +# +# This file is part of libmobi. +# Licensed under LGPL, either version 3, or any later. +# See + +set(mobi_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/buffer.c + ${CMAKE_CURRENT_SOURCE_DIR}/buffer.h + ${CMAKE_CURRENT_SOURCE_DIR}/compression.c + ${CMAKE_CURRENT_SOURCE_DIR}/compression.h + ${CMAKE_CURRENT_SOURCE_DIR}/config.h + ${CMAKE_CURRENT_SOURCE_DIR}/debug.c + ${CMAKE_CURRENT_SOURCE_DIR}/debug.h + ${CMAKE_CURRENT_SOURCE_DIR}/index.c + ${CMAKE_CURRENT_SOURCE_DIR}/index.h + ${CMAKE_CURRENT_SOURCE_DIR}/memory.c + ${CMAKE_CURRENT_SOURCE_DIR}/memory.h + ${CMAKE_CURRENT_SOURCE_DIR}/meta.c + ${CMAKE_CURRENT_SOURCE_DIR}/meta.h + ${CMAKE_CURRENT_SOURCE_DIR}/mobi.h + ${CMAKE_CURRENT_SOURCE_DIR}/parse_rawml.c + ${CMAKE_CURRENT_SOURCE_DIR}/parse_rawml.h + ${CMAKE_CURRENT_SOURCE_DIR}/read.c + ${CMAKE_CURRENT_SOURCE_DIR}/read.h + ${CMAKE_CURRENT_SOURCE_DIR}/structure.c + ${CMAKE_CURRENT_SOURCE_DIR}/structure.h + ${CMAKE_CURRENT_SOURCE_DIR}/util.c + ${CMAKE_CURRENT_SOURCE_DIR}/util.h + ${CMAKE_CURRENT_SOURCE_DIR}/write.c + ${CMAKE_CURRENT_SOURCE_DIR}/write.h + ${CMAKE_CURRENT_SOURCE_DIR}/xmlwriter.c + ${CMAKE_CURRENT_SOURCE_DIR}/xmlwriter.h +) + +if(USE_ENCRYPTION) + list(APPEND mobi_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/encryption.c + ${CMAKE_CURRENT_SOURCE_DIR}/encryption.h + ${CMAKE_CURRENT_SOURCE_DIR}/sha1.c + ${CMAKE_CURRENT_SOURCE_DIR}/sha1.h + ${CMAKE_CURRENT_SOURCE_DIR}/randombytes.c + ${CMAKE_CURRENT_SOURCE_DIR}/randombytes.h) +endif(USE_ENCRYPTION) + +if(USE_XMLWRITER) + list(APPEND mobi_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/opf.c + ${CMAKE_CURRENT_SOURCE_DIR}/opf.h) + if(NOT USE_LIBXML2) + list(APPEND mobi_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/xmlwriter.c + ${CMAKE_CURRENT_SOURCE_DIR}/xmlwriter.h) + endif(NOT USE_LIBXML2) +endif(USE_XMLWRITER) + + +add_library(mobi ${mobi_SOURCES}) + +set_target_properties(mobi PROPERTIES + OUTPUT_NAME "mobi" + SOVERSION ${PACKAGE_VERSION_MAJOR} + VERSION "${PACKAGE_VERSION}" + POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS} + C_VISIBILITY_PRESET hidden + VISIBILITY_INLINES_HIDDEN ON + MACOSX_RPATH 1) + +if(USE_MINIZ) + set(miniz_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/miniz.c + ${CMAKE_CURRENT_SOURCE_DIR}/miniz.h + ) + add_library(miniz OBJECT ${miniz_SOURCES}) + target_compile_definitions(miniz PRIVATE + MINIZ_NO_STDIO + MINIZ_NO_ZLIB_COMPATIBLE_NAMES + MINIZ_NO_TIME + MINIZ_NO_ARCHIVE_APIS + MINIZ_NO_ARCHIVE_WRITING_APIS + _POSIX_C_SOURCE=200112L) + target_link_libraries(mobi PRIVATE miniz) +endif(USE_MINIZ) + +if(USE_LIBXML2) + target_link_libraries(mobi PUBLIC LibXml2::LibXml2) +endif(USE_LIBXML2) + +if(USE_ZLIB) + target_link_libraries(mobi PUBLIC ZLIB::ZLIB) +endif(USE_ZLIB) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt new file mode 100644 index 0000000..4509cf3 --- /dev/null +++ b/tools/CMakeLists.txt @@ -0,0 +1,37 @@ +# Copyright (c) 2022 Bartek Fabiszewski +# http://www.fabiszewski.net +# +# This file is part of libmobi. +# Licensed under LGPL, either version 3, or any later. +# See + +include_directories(${LIBMOBI_SOURCE_DIR}/src) + +add_library(common OBJECT common.c) + +add_executable(mobitool mobitool.c) +target_link_libraries(mobitool PUBLIC mobi) +target_link_libraries(mobitool PRIVATE common) +add_executable(mobimeta mobimeta.c) +target_link_libraries(mobimeta PUBLIC mobi) +target_link_libraries(mobimeta PRIVATE common) +add_executable(mobidrm mobidrm.c) +target_link_libraries(mobidrm PUBLIC mobi) +target_link_libraries(mobidrm PRIVATE common) + +if(USE_XMLWRITER) +# miniz.c zip functions are needed for epub creation + set(zip_SOURCES + ${LIBMOBI_SOURCE_DIR}/src/miniz.c + ${LIBMOBI_SOURCE_DIR}/src/miniz.h + ) + add_library(zip OBJECT ${zip_SOURCES}) + target_compile_definitions(zip PRIVATE + MINIZ_NO_ZLIB_COMPATIBLE_NAMES + _POSIX_C_SOURCE=200112L) + target_link_libraries(mobitool PRIVATE zip) + target_link_libraries(mobimeta PRIVATE zip) + if (TOOLS_STATIC AND USE_MINIZ) + target_link_options(mobitool PRIVATE "-Wl,--allow-multiple-definition") + endif (TOOLS_STATIC AND USE_MINIZ) +endif(USE_XMLWRITER)