Skip to content

Commit

Permalink
cmake: reorganize BUILD_WRITER options
Browse files Browse the repository at this point in the history
Build with old and/or new writer (encoder) backend (OFF/ON/OLD/NEW/BOTH)

The default value ON is currently mapped to OLD (MultiFormatWriter).

The value NEW means use libzint as the writer backend but that only takes
effect if BUILD_EXPERIMENTAL_API is ON as well.
  • Loading branch information
axxel committed Mar 6, 2024
1 parent 29fb3cf commit 850ff92
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 109 deletions.
16 changes: 6 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ cmake_minimum_required(VERSION 3.15)

project(ZXing)

option (BUILD_WRITERS "Build with writer support (encoders)" ON)
# option (BUILD_WRITERS "Build with writer support (encoders)" ON)
set(BUILD_WRITERS "ON" CACHE STRING "Build with old and/or new writer (encoder) backend (OFF/ON/OLD/NEW/BOTH)")
option (BUILD_READERS "Build with reader support (decoders)" ON)
option (BUILD_EXAMPLES "Build the example barcode reader/writer applications" ON)
option (BUILD_BLACKBOX_TESTS "Build the black box reader/writer tests" OFF)
option (BUILD_UNIT_TESTS "Build the unit tests (don't enable for production builds)" OFF)
option (BUILD_PYTHON_MODULE "Build the python module" OFF)
option (BUILD_C_API "Build the C-API" OFF)
option (BUILD_EXPERIMENTAL_API "Build with experimental API" OFF)
option (ZXING_USE_ZINT "Use libzint for barcode creation/generation" OFF)
set(BUILD_DEPENDENCIES "AUTO" CACHE STRING "Fetch from github or use locally installed (AUTO/GITHUB/LOCAL)")

if (WIN32)
Expand Down Expand Up @@ -48,14 +48,10 @@ if (BUILD_UNIT_TESTS AND (NOT BUILD_WRITERS OR NOT BUILD_READERS))
set (BUILD_READERS ON)
endif()

if (ZXING_USE_ZINT)
set (BUILD_EXPERIMENTAL_API ON)
add_definitions (-DZXING_USE_ZINT)
endif()

if (BUILD_EXPERIMENTAL_API)
set (CMAKE_CXX_STANDARD 20)
add_definitions (-DZXING_BUILD_EXPERIMENTAL_API)
set(BUILD_WRITERS_LIST OFF ON OLD NEW BOTH)
set_property(CACHE BUILD_WRITERS PROPERTY STRINGS ${BUILD_WRITERS_LIST})
if(NOT BUILD_WRITERS IN_LIST BUILD_WRITERS_LIST)
message(FATAL_ERROR "BUILD_WRITERS must be one of ${BUILD_WRITERS_LIST}")
endif()

set(BUILD_DEPENDENCIES_LIST AUTO GITHUB LOCAL)
Expand Down
59 changes: 41 additions & 18 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,23 @@ if (NOT DEFINED BUILD_READERS)
set (BUILD_READERS ON)
endif()

set (ZXING_CORE_DEFINES)
set (BUILD_WRITERS_NEW OFF)
set (BUILD_WRITERS_OLD OFF)
if (BUILD_WRITERS MATCHES "OLD|ON")
set (BUILD_WRITERS ON)
set (BUILD_WRITERS_OLD ON)
elseif (BUILD_WRITERS MATCHES "NEW")
set (BUILD_WRITERS ON)
set (BUILD_WRITERS_NEW ON)
elseif (BUILD_WRITERS MATCHES "BOTH")
set (BUILD_WRITERS ON)
set (BUILD_WRITERS_NEW ON)
set (BUILD_WRITERS_OLD ON)
endif()

set (ZXING_CORE_DEFINES
$<$<BOOL:${BUILD_EXPERIMENTAL_API}>:-DZXING_BUILD_EXPERIMENTAL_API>
)
if (WINRT)
set (ZXING_CORE_DEFINES ${ZXING_CORE_DEFINES}
-DWINRT
Expand All @@ -30,6 +46,7 @@ endif()
set (ZXING_CORE_LOCAL_DEFINES
$<$<BOOL:${BUILD_READERS}>:-DZXING_BUILD_READERS>
$<$<BOOL:${BUILD_WRITERS}>:-DZXING_BUILD_WRITERS>
$<$<BOOL:${BUILD_WRITERS_NEW}>:-DZXING_USE_ZINT>
$<$<BOOL:${BUILD_UNIT_TESTS}>:-DZXING_BUILD_FOR_TEST>
)
if (MSVC)
Expand Down Expand Up @@ -59,6 +76,8 @@ endif()
################# Source files

set (COMMON_FILES
src/Barcode.h
src/Barcode.cpp
src/BarcodeFormat.h
src/BarcodeFormat.cpp
src/BitArray.h
Expand All @@ -75,6 +94,8 @@ set (COMMON_FILES
src/CharacterSet.cpp
src/ConcentricFinder.h
src/ConcentricFinder.cpp
src/Content.h
src/Content.cpp
src/CustomData.h
src/ECI.h
src/ECI.cpp
Expand Down Expand Up @@ -108,14 +129,10 @@ set (COMMON_FILES
)
if (BUILD_READERS)
set (COMMON_FILES ${COMMON_FILES}
src/Barcode.h
src/Barcode.cpp
src/BinaryBitmap.h
src/BinaryBitmap.cpp
src/BitSource.h
src/BitSource.cpp
src/Content.h
src/Content.cpp
src/DecodeHints.h
$<$<BOOL:${BUILD_SHARED_LIBS}>:src/DecodeHints.cpp> # [[deprecated]]
src/DecoderResult.h
Expand Down Expand Up @@ -154,7 +171,15 @@ if (BUILD_READERS)
$<$<BOOL:${BUILD_C_API}>:src/ZXingC.cpp>
)
endif()

if (BUILD_WRITERS)
set (COMMON_FILES ${COMMON_FILES}
src/WriteBarcode.h
src/WriteBarcode.cpp
)
endif()

if (BUILD_WRITERS_OLD)
set (COMMON_FILES ${COMMON_FILES}
src/ByteMatrix.h
src/ReedSolomonEncoder.h
Expand All @@ -163,8 +188,6 @@ if (BUILD_WRITERS)
src/TextEncoder.cpp
src/MultiFormatWriter.h
src/MultiFormatWriter.cpp
$<$<BOOL:${BUILD_EXPERIMENTAL_API}>:src/WriteBarcode.h>
$<$<BOOL:${BUILD_EXPERIMENTAL_API}>:src/WriteBarcode.cpp>
)
endif()

Expand Down Expand Up @@ -197,7 +220,7 @@ if (BUILD_READERS)
$<$<BOOL:${BUILD_C_API}>:${CMAKE_CURRENT_SOURCE_DIR}/src/ZXingC.h>
)
endif()
if (BUILD_WRITERS)
if (BUILD_WRITERS_OLD)
set (PUBLIC_HEADERS ${PUBLIC_HEADERS}
src/BitMatrix.h
src/BitMatrixIO.h
Expand All @@ -221,7 +244,7 @@ if (BUILD_READERS)
src/aztec/AZReader.cpp
)
endif()
if (BUILD_WRITERS)
if (BUILD_WRITERS_OLD)
set (AZTEC_FILES ${AZTEC_FILES}
src/aztec/AZEncodingState.h
src/aztec/AZEncoder.h
Expand Down Expand Up @@ -254,7 +277,7 @@ if (BUILD_READERS)
src/datamatrix/DMReader.cpp
)
endif()
if (BUILD_WRITERS)
if (BUILD_WRITERS_OLD)
set (DATAMATRIX_FILES ${DATAMATRIX_FILES}
src/datamatrix/DMECEncoder.h
src/datamatrix/DMECEncoder.cpp
Expand Down Expand Up @@ -319,7 +342,7 @@ if (BUILD_READERS)
src/oned/ODRowReader.h
)
endif()
if (BUILD_WRITERS)
if (BUILD_WRITERS_OLD)
set (ONED_FILES ${ONED_FILES}
src/oned/ODCodabarWriter.h
src/oned/ODCodabarWriter.cpp
Expand Down Expand Up @@ -376,7 +399,7 @@ if (BUILD_READERS)
src/pdf417/PDFScanningDecoder.cpp
)
endif()
if (BUILD_WRITERS)
if (BUILD_WRITERS_OLD)
set (PDF417_FILES ${PDF417_FILES}
src/pdf417/PDFCompaction.h
src/pdf417/PDFEncoder.h
Expand Down Expand Up @@ -415,7 +438,7 @@ if (BUILD_READERS)
src/qrcode/QRReader.cpp
)
endif()
if (BUILD_WRITERS)
if (BUILD_WRITERS_OLD)
set (QRCODE_FILES ${QRCODE_FILES}
src/qrcode/QREncoder.h
src/qrcode/QREncoder.cpp
Expand All @@ -437,7 +460,7 @@ set (LIBZUECI_FILES
if (NOT BUILD_READERS)
set_source_files_properties(src/libzueci/zueci.c PROPERTIES COMPILE_FLAGS -DZUECI_EMBED_NO_TO_UTF)
endif()
if (NOT BUILD_WRITERS)
if (NOT BUILD_WRITERS_OLD)
set_source_files_properties(src/libzueci/zueci.c PROPERTIES COMPILE_FLAGS -DZUECI_EMBED_NO_TO_ECI)
endif()

Expand Down Expand Up @@ -479,10 +502,10 @@ target_compile_features(ZXing PUBLIC cxx_std_17)

target_link_libraries (ZXing PRIVATE Threads::Threads)

if (ZXING_USE_ZINT)
include(../zxing.cmake)
zxing_add_package(zint zint https://github.com/zint/zint.git 11b3c18aed29cf9eb2a31debd66c8fe8a1d77604)
target_link_libraries (ZXing PRIVATE zint)
if (BUILD_WRITERS_NEW)
include(../zxing.cmake)
zxing_add_package(zint zint https://github.com/zint/zint.git 55a7369cd8c4a6b58bcd62f02a3a2d486952c897)
target_link_libraries (ZXing PRIVATE zint)
endif()

add_library(ZXing::ZXing ALIAS ZXing)
Expand Down
4 changes: 3 additions & 1 deletion core/src/Barcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#ifdef ZXING_USE_ZINT
#include <zint.h>
#else
struct zint_symbol {};
#endif

#include <cmath>
Expand Down Expand Up @@ -132,7 +134,7 @@ Result& Result::setReaderOptions(const ReaderOptions& opts)
return *this;
}

#ifdef ZXING_USE_ZINT
#ifdef ZXING_BUILD_EXPERIMENTAL_API
void Result::zint(std::unique_ptr<zint_symbol>&& z)
{
_zint = std::shared_ptr(std::move(z));
Expand Down
4 changes: 0 additions & 4 deletions core/src/Barcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ class Result
#ifdef ZXING_BUILD_EXPERIMENTAL_API
void symbol(BitMatrix&& bits) { _symbol = std::make_shared<BitMatrix>(std::move(bits)); }
const BitMatrix& symbol() const { return *_symbol; }
#endif
#ifdef ZXING_USE_ZINT
void zint(std::unique_ptr<zint_symbol>&& z);
zint_symbol* zint() const { return _zint.get(); }
#endif
Expand All @@ -190,8 +188,6 @@ class Result
bool _readerInit = false;
#ifdef ZXING_BUILD_EXPERIMENTAL_API
std::shared_ptr<BitMatrix> _symbol;
#endif
#ifdef ZXING_USE_ZINT
std::shared_ptr<zint_symbol> _zint;
#endif
};
Expand Down
11 changes: 9 additions & 2 deletions core/src/WriteBarcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ struct std::default_delete<zint_symbol>
{
void operator()(zint_symbol* p) const noexcept { ZBarcode_Delete(p); }
};

#else
struct zint_symbol {};
#endif
Expand All @@ -33,6 +32,10 @@ struct CreatorOptions::Data
// structured_append (idx, cnt, ID)

mutable std::unique_ptr<zint_symbol> zint;

#if __cplusplus <= 201703L
Data(BarcodeFormat f) : format(f) {}
#endif
};

#define ZX_PROPERTY(TYPE, NAME) \
Expand Down Expand Up @@ -110,7 +113,7 @@ static constexpr BarcodeFormatZXing2Zint barcodeFormatZXing2Zint[] = {
{BarcodeFormat::DXFilmEdge, -1},
{BarcodeFormat::EAN8, BARCODE_EANX},
{BarcodeFormat::EAN13, BARCODE_EANX},
{BarcodeFormat::ITF, BARCODE_ITF14},
{BarcodeFormat::ITF, BARCODE_C25INTER},
{BarcodeFormat::MaxiCode, BARCODE_MAXICODE},
{BarcodeFormat::MicroQRCode, BARCODE_MICROQR},
{BarcodeFormat::PDF417, BARCODE_PDF417},
Expand Down Expand Up @@ -226,10 +229,12 @@ Barcode CreateBarcodeFromText(std::string_view contents, const CreatorOptions& o
return CreateBarcode(contents.data(), contents.size(), UNICODE_MODE, opts);
}

#if __cplusplus > 201703L
Barcode CreateBarcodeFromText(std::u8string_view contents, const CreatorOptions& opts)
{
return CreateBarcode(contents.data(), contents.size(), UNICODE_MODE, opts);
}
#endif

Barcode CreateBarcodeFromBytes(const void* data, int size, const CreatorOptions& opts)
{
Expand Down Expand Up @@ -328,10 +333,12 @@ Barcode CreateBarcodeFromText(std::string_view contents, const CreatorOptions& o
return CreateBarcode(writer.encode(std::string(contents), 0, IsLinearCode(opts.format()) ? 50 : 0), opts);
}

#if __cplusplus > 201703L
Barcode CreateBarcodeFromText(std::u8string_view contents, const CreatorOptions& opts)
{
return CreateBarcodeFromText({reinterpret_cast<const char*>(contents.data()), contents.size()}, opts);
}
#endif

Barcode CreateBarcodeFromBytes(const void* data, int size, const CreatorOptions& opts)
{
Expand Down
6 changes: 6 additions & 0 deletions core/src/WriteBarcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ Barcode CreateBarcodeFromBytes(const R& contents, const CreatorOptions& options)
{
return CreateBarcodeFromBytes(std::ranges::data(contents), std::ranges::size(contents), options);
}
#else
template <typename R>
Barcode CreateBarcodeFromBytes(const R& contents, const CreatorOptions& options)
{
return CreateBarcodeFromBytes(std::data(contents), std::size(contents), options);
}
#endif

// =================================================================================
Expand Down
Loading

0 comments on commit 850ff92

Please sign in to comment.