From dd47087afd7c96a97e2f622e221d97b2ede9364e Mon Sep 17 00:00:00 2001
From: Syping <schiedelrafael@keppe.org>
Date: Fri, 27 Aug 2021 21:48:33 +0200
Subject: [PATCH] add pkgconfig, improve Qt example

---
 CMakeLists.txt                              | 16 +++++++---------
 examples/ragephoto-gtkviewer/CMakeLists.txt | 21 ++++++++++++---------
 examples/ragephoto-qtviewer/CMakeLists.txt  | 18 +++++++++++++-----
 examples/ragephoto-qtviewer/src/main.cpp    |  2 ++
 src/ragephoto.pc.in                         |  9 +++++++++
 5 files changed, 43 insertions(+), 23 deletions(-)
 create mode 100644 src/ragephoto.pc.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index cdd589b..9c40907 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,5 @@
 cmake_minimum_required(VERSION 3.7)
-project(ragephoto LANGUAGES CXX)
+project(ragephoto VERSION 0.1 LANGUAGES CXX)
 
 set(CMAKE_INCLUDE_CURRENT_DIR ON)
 set(CMAKE_CXX_STANDARD 11)
@@ -27,16 +27,14 @@ else()
     message("-- Testing iconv - no")
 endif()
 
-project(ragephoto LANGUAGES CXX)
-
-set(RAGEPHOTO_SOURCES
-    src/RagePhoto.cpp
-    src/RagePhoto.h
-)
 set(RAGEPHOTO_HEADERS
     src/libragephoto_global.h
     src/RagePhoto.h
 )
+set(RAGEPHOTO_SOURCES
+    src/RagePhoto.cpp
+)
+configure_file(src/ragephoto.pc.in pkgconfig/ragephoto.pc @ONLY)
 
 option(BUILD_SHARED "Build libragephoto as shared library" ON)
 if (BUILD_SHARED)
@@ -59,6 +57,7 @@ target_compile_definitions(ragephoto PRIVATE
 )
 install(TARGETS ragephoto DESTINATION lib)
 install(FILES ${RAGEPHOTO_HEADERS} DESTINATION include)
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/ragephoto.pc DESTINATION lib/pkgconfig)
 
 option(WITH_GTK_EXAMPLE "Build libragephoto with GTK Photo Viewer" OFF)
 if (WITH_GTK_EXAMPLE)
@@ -72,11 +71,10 @@ endif()
 
 option(WITH_EXTRACT "Build libragephoto with ragephoto-extract" ON)
 if (WITH_EXTRACT)
-    project(ragephoto-extract LANGUAGES CXX)
     set(EXTRACT_SOURCES
         src/RagePhoto-Extract.cpp
     )
     add_executable(ragephoto-extract ${RAGEPHOTO_HEADERS} ${EXTRACT_SOURCES})
-    target_link_libraries(ragephoto-extract ragephoto)
+    target_link_libraries(ragephoto-extract PRIVATE ragephoto)
     install(TARGETS ragephoto-extract DESTINATION bin)
 endif()
diff --git a/examples/ragephoto-gtkviewer/CMakeLists.txt b/examples/ragephoto-gtkviewer/CMakeLists.txt
index 5e7c109..3f61c81 100644
--- a/examples/ragephoto-gtkviewer/CMakeLists.txt
+++ b/examples/ragephoto-gtkviewer/CMakeLists.txt
@@ -8,8 +8,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
 find_package(PkgConfig REQUIRED)
 pkg_check_modules(GTKMM REQUIRED gtkmm-3.0)
 
-project(ragephoto-gtkviewer LANGUAGES CXX)
-
 set(GTKVIEWER_HEADERS
     src/PhotoViewer.h
 )
@@ -19,13 +17,18 @@ set(GTKVIEWER_SOURCES
     src/PhotoViewer.cpp
 )
 
-if(TARGET ragephoto)
+if (TARGET ragephoto)
     set(RAGEPHOTO_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src)
-    add_executable(ragephoto-gtkviewer ${GTKVIEWER_HEADERS} ${GTKVIEWER_SOURCES})
-    target_link_libraries(ragephoto-gtkviewer ${GTKMM_LIBRARIES} ragephoto)
-    target_link_directories(ragephoto-gtkviewer PRIVATE ${GTKMM_LIBRARY_DIRS})
-    target_include_directories(ragephoto-gtkviewer PRIVATE ${GTKMM_INCLUDE_DIRS} ${RAGEPHOTO_INCLUDE_DIRS})
-    install(TARGETS ragephoto-gtkviewer DESTINATION bin)
+    set(RAGEPHOTO_LIBRARIES ragephoto)
 else()
-    message("ragephoto-gtkviewer need to be build with libragephoto together")
+    pkg_check_modules(RAGEPHOTO REQUIRED ragephoto)
 endif()
+
+add_executable(ragephoto-gtkviewer ${GTKVIEWER_HEADERS} ${GTKVIEWER_SOURCES})
+target_compile_options(ragephoto-gtkviewer PRIVATE ${GTKMM_CFLAGS} ${RAGEPHOTO_CFLAGS})
+target_link_libraries(ragephoto-gtkviewer PRIVATE ${GTKMM_LIBRARIES} ${RAGEPHOTO_LIBRARIES})
+if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.13.0")
+    target_link_directories(ragephoto-gtkviewer PRIVATE ${GTKMM_LIBRARY_DIRS} ${RAGEPHOTO_LIBRARY_DIRS})
+endif()
+target_include_directories(ragephoto-gtkviewer PRIVATE ${GTKMM_INCLUDE_DIRS} ${RAGEPHOTO_INCLUDE_DIRS})
+install(TARGETS ragephoto-gtkviewer DESTINATION bin)
diff --git a/examples/ragephoto-qtviewer/CMakeLists.txt b/examples/ragephoto-qtviewer/CMakeLists.txt
index 5a2e193..407c0fb 100644
--- a/examples/ragephoto-qtviewer/CMakeLists.txt
+++ b/examples/ragephoto-qtviewer/CMakeLists.txt
@@ -17,10 +17,18 @@ set(QTVIEWER_SOURCES
     src/main.cpp
 )
 
-if(TARGET ragephoto)
-    add_executable(ragephoto-qtviewer ${QTVIEWER_SOURCES})
-    target_link_libraries(ragephoto-qtviewer Qt${QT_VERSION_MAJOR}::Widgets ragephoto)
-    target_include_directories(ragephoto-qtviewer PRIVATE ${RAGEPHOTO_INCLUDE_DIRS})
+if (TARGET ragephoto)
+    set(RAGEPHOTO_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src)
+    set(RAGEPHOTO_LIBRARIES ragephoto)
 else()
-    message("ragephoto-qtviewer need to be build with libragephoto together")
+    find_package(PkgConfig REQUIRED)
+    pkg_check_modules(RAGEPHOTO REQUIRED ragephoto)
 endif()
+
+add_executable(ragephoto-qtviewer ${QTVIEWER_SOURCES})
+target_compile_options(ragephoto-qtviewer PRIVATE ${RAGEPHOTO_CFLAGS})
+target_link_libraries(ragephoto-qtviewer PRIVATE Qt${QT_VERSION_MAJOR}::Widgets ${RAGEPHOTO_LIBRARIES})
+if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.13.0")
+    target_link_directories(ragephoto-qtviewer PRIVATE ${RAGEPHOTO_LIBRARY_DIRS})
+endif()
+target_include_directories(ragephoto-qtviewer PRIVATE ${RAGEPHOTO_INCLUDE_DIRS})
diff --git a/examples/ragephoto-qtviewer/src/main.cpp b/examples/ragephoto-qtviewer/src/main.cpp
index a1f57bb..50f49b8 100644
--- a/examples/ragephoto-qtviewer/src/main.cpp
+++ b/examples/ragephoto-qtviewer/src/main.cpp
@@ -89,6 +89,8 @@ int main(int argc, char *argv[])
     QPushButton openButton("Open", &mainWindow);
     QObject::connect(&openButton, &QPushButton::clicked, &mainWindow, [&](){
         const QString filename = QFileDialog::getOpenFileName(&mainWindow, "Open Photo...", QString(), "GTA V Photo (PGTA5*)");
+        if (filename.isEmpty())
+            return;
         if (readPhotoFile(filename, &mainWindow, &photoLabel)) {
             QTimer::singleShot(0, &mainWindow, [&](){
                 mainWindow.setFixedSize(mainWindow.sizeHint());
diff --git a/src/ragephoto.pc.in b/src/ragephoto.pc.in
new file mode 100644
index 0000000..965fe75
--- /dev/null
+++ b/src/ragephoto.pc.in
@@ -0,0 +1,9 @@
+prefix=@CMAKE_INSTALL_PREFIX@
+libdir=${prefix}/lib
+includedir=${prefix}/include
+
+Name: libragephoto
+Description: Open Source RAGE Photo Parser for GTA V
+Version: @PROJECT_VERSION@
+Libs: -L${libdir} -lragephoto
+Cflags: -I${includedir}