Skip to content

Commit

Permalink
Add pkg-config export to CMake script
Browse files Browse the repository at this point in the history
  • Loading branch information
OPNA2608 committed Jul 23, 2024
1 parent 32b2e24 commit 1e19d1c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
cmake_minimum_required(VERSION 3.3)
project(QZXing)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

option(BUILD_SHARED "Build as shared library" OFF)

if(BUILD_SHARED)
SET(SOVERSION_MAJOR 3)
SET(SOVERSION_MINOR 3)
SET(SOVERSION_PATCH 0)
SET(SOVERSION_FULL "${SOVERSION_MAJOR}.${SOVERSION_MINOR}.${SOVERSION_PATCH}")

OPTION(QZXING_MULTIMEDIA "" OFF)
OPTION(QZXING_USE_QML "" OFF)
Expand Down Expand Up @@ -39,6 +42,8 @@ set(PUBLIC_HEADERS
QZXing_global.h
)

set(PUBLIC_DEFINES "")

set(SOURCES

CameraImageWrapper.cpp
Expand All @@ -53,7 +58,7 @@ if(QZXING_MULTIMEDIA)

LIST(APPEND SOURCES QZXingFilter.cpp QZXingFilter.h)
LIST(APPEND PUBLIC_HEADERS QZXingFilter.h)
add_definitions(-DQZXING_MULTIMEDIA)
LIST(APPEND PUBLIC_DEFINES QZXING_MULTIMEDIA)

SET(QZXING_USE_QML ON)

Expand All @@ -62,24 +67,27 @@ endif(QZXING_MULTIMEDIA)
if(QZXING_USE_QML)
LIST(APPEND SOURCES QZXingImageProvider.cpp QZXingImageProvider.h)
LIST(APPEND PUBLIC_HEADERS QZXingImageProvider.h)
add_definitions(-DQZXING_QML)
LIST(APPEND PUBLIC_DEFINES QZXING_QML)
endif(QZXING_USE_QML)

if(QZXING_USE_ENCODER)
add_definitions(-DENABLE_ENCODER_GENERIC -DENABLE_ENCODER_QR_CODE)
LIST(APPEND PUBLIC_DEFINES ENABLE_ENCODER_GENERIC ENABLE_ENCODER_QR_CODE)
endif(QZXING_USE_ENCODER)

if(BUILD_SHARED)
add_library(qzxing SHARED ${SOURCES})
set_target_properties(qzxing
PROPERTIES
VERSION ${SOVERSION_MAJOR}.${SOVERSION_MINOR}.${SOVERSION_PATCH}
VERSION ${SOVERSION_FULL}
SOVERSION ${SOVERSION_MAJOR}
)
else()
add_library(qzxing "" ${SOURCES})
endif()

# Change Global Definitions depending on how you want to use the library
list(APPEND PUBLIC_DEFINES DISABLE_LIBRARY_FEATURES)

if(WIN32)
add_subdirectory(zxing/win32)
if(MSVC)
Expand All @@ -105,20 +113,16 @@ target_link_libraries(qzxing Qt5::Core Qt5::Gui)

if(QZXING_MULTIMEDIA)
target_link_libraries(qzxing Qt5::Multimedia)
target_compile_definitions(qzxing PUBLIC -DQZXING_MULTIMEDIA)
endif(QZXING_MULTIMEDIA)

if(QZXING_USE_QML)
target_link_libraries(qzxing
Qt5::Svg
Qt5::Quick
Qt5::QuickControls2)
target_compile_definitions(qzxing PUBLIC -DQZXING_QML)
endif(QZXING_USE_QML)

if(QZXING_USE_ENCODER)
target_compile_definitions(qzxing PUBLIC -DENABLE_ENCODER_GENERIC -DENABLE_ENCODER_QR_CODE)
endif(QZXING_USE_ENCODER)
target_compile_definitions(qzxing PUBLIC ${PUBLIC_DEFINES})

if(QZXING_USE_DECODER_QR_CODE)
target_compile_definitions(qzxing PRIVATE -DENABLE_DECODER_QR_CODE)
Expand All @@ -144,11 +148,6 @@ if(QZXING_USE_DECODER_1D_BARCODES)
target_compile_definitions(qzxing PRIVATE -DENABLE_DECODER_1D_BARCODES)
endif()

if(!BUILD_SHARED)
# Change Global Definitions depending on how you want to use the library
target_compile_definitions(qzxing PUBLIC DISABLE_LIBRARY_FEATURES)
endif()

# Target includes
target_include_directories(qzxing
PRIVATE
Expand All @@ -163,6 +162,7 @@ target_include_directories(qzxing

if(BUILD_SHARED)
include (GNUInstallDirs)
include (JoinPaths)

set(QZXING_INSTALL_TARGETS qzxing)

Expand All @@ -176,4 +176,15 @@ if(BUILD_SHARED)
install (
FILES ${PUBLIC_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/qzxing"
)

join_paths(PC_LIBDIR "\${prefix}" "${CMAKE_INSTALL_LIBDIR}")
join_paths(PC_INCLUDEDIR "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}" "qzxing")

string(REPLACE ";" " -D" PC_DEFINES "${PUBLIC_DEFINES}")
if(PC_DEFINES)
set(PC_DEFINES "-D${PC_DEFINES}")
endif()

configure_file(QZXing.pc.in ${CMAKE_CURRENT_BINARY_DIR}/QZXing.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/QZXing.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endif()
10 changes: 10 additions & 0 deletions src/QZXing.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
libdir=@PC_LIBDIR@
includedir=@PC_INCLUDEDIR@

Name: Qzxing
Description: Qzxing Library
Version: @SOVERSION_FULL@
Cflags: @PC_DEFINES@ -I${includedir}
Libs: -L${libdir} -lqzxing
23 changes: 23 additions & 0 deletions src/cmake/JoinPaths.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This module provides function for joining paths
# known from most languages
#
# SPDX-License-Identifier: (MIT OR CC0-1.0)
# Copyright 2020 Jan Tojnar
# https://github.com/jtojnar/cmake-snips
#
# Modelled after Python’s os.path.join
# https://docs.python.org/3.7/library/os.path.html#os.path.join
# Windows not supported
function(join_paths joined_path first_path_segment)
set(temp_path "${first_path_segment}")
foreach(current_segment IN LISTS ARGN)
if(NOT ("${current_segment}" STREQUAL ""))
if(IS_ABSOLUTE "${current_segment}")
set(temp_path "${current_segment}")
else()
set(temp_path "${temp_path}/${current_segment}")
endif()
endif()
endforeach()
set(${joined_path} "${temp_path}" PARENT_SCOPE)
endfunction()

0 comments on commit 1e19d1c

Please sign in to comment.