From c698025ecdc4bfd22e7d21ceb49f29ec51f287bf Mon Sep 17 00:00:00 2001 From: NeroBurner Date: Thu, 17 Jan 2019 14:14:13 +0100 Subject: [PATCH] Add cmake support with cmake-config file Add full cmake support. The project can either be used with `add_subdirectory` or be installed into the system (or some other directory) and be found with `find_package(lodepng)`. In both cases the cmake target `lodepng::lodepng` is all that needs to be linked. Having an install target also makes packaging easier. - add libraries - loadepng - lodepng_util - add executables (also installed) - pngdetail - generate lodepng-config.cmake and install in lib/cmake/lodepng - add unittest and target `test` (enabled by flag ENABLE_TESTING) - add benchmark (enabled by flag BUILD_BENCHMARK) - add examples (enabled by flag BUILD_EXAMPLES) note: I was only able to install SDL2 from my package manager, but benchmark, example_opengl and example_sdl use SDL (1) --- CMakeLists.txt | 104 ++++++++++++++++++++++++++++++++++++++++ cmake/config.cmake.in | 5 ++ examples/CMakeLists.txt | 37 ++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 cmake/config.cmake.in create mode 100644 examples/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..db1c8178 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,104 @@ +cmake_minimum_required(VERSION 3.1) +project(lodepng) + +# define target to install and link tests against +add_library(${CMAKE_PROJECT_NAME} + lodepng.h + lodepng.cpp) +target_include_directories(${CMAKE_PROJECT_NAME} + PUBLIC $) +add_library(${CMAKE_PROJECT_NAME}_util + lodepng_util.h + lodepng_util.cpp) +target_link_libraries(${CMAKE_PROJECT_NAME}_util PUBLIC ${CMAKE_PROJECT_NAME}) +add_executable(pngdetail + pngdetail.cpp) +target_link_libraries(pngdetail PRIVATE ${CMAKE_PROJECT_NAME}_util) + +# ALIAS same as in configure file +# the ALIAS can be used to create examples which use the same syntax as a client +# application, which uses `find_package(lodepng CONFIG)` +# create the alias lodepng::lodepng +add_library(${CMAKE_PROJECT_NAME}::${CMAKE_PROJECT_NAME} ALIAS ${CMAKE_PROJECT_NAME}) +add_library(${CMAKE_PROJECT_NAME}::${CMAKE_PROJECT_NAME}_util ALIAS ${CMAKE_PROJECT_NAME}_util) + +option(ENABLE_TESTING "enable creation of unittest" ON) +if(ENABLE_TESTING) + enable_testing() + add_executable(${CMAKE_PROJECT_NAME}_unittest lodepng_unittest.cpp) + # add compiler flags for test target only for GCC and Clang + target_compile_options(${CMAKE_PROJECT_NAME}_unittest PRIVATE + $,$>: + -Werror -Wall -pedantic>> + ) + + add_test(test_${CMAKE_PROJECT_NAME}_unittest ${CMAKE_PROJECT_NAME}_unittest) + target_link_libraries(${CMAKE_PROJECT_NAME}_unittest PRIVATE ${CMAKE_PROJECT_NAME}_util) +endif() + +option(BUILD_EXAMPLES "build examples using ${CMAKE_PROJECT_NAME}" OFF) +if(BUILD_EXAMPLES) + add_subdirectory(examples) +endif() + +option(BUILD_BENCHMARK "build benchmark for ${CMAKE_PROJECT_NAME}, requires SDL2" OFF) +if(BUILD_BENCHMARK) + add_executable(${CMAKE_PROJECT_NAME}_benchmark lodepng_benchmark.cpp) + target_link_libraries(${CMAKE_PROJECT_NAME}_benchmark PRIVATE ${CMAKE_PROJECT_NAME}::${CMAKE_PROJECT_NAME}) + # SDL2 Dependency + find_package(SDL2 CONFIG REQUIRED) + if (TARGET SDL2::SDL2) + message(STATUS "using TARGET SDL2::SDL2") + target_link_libraries(${CMAKE_PROJECT_NAME}_benchmark PUBLIC SDL2::SDL2) + else() + message(STATUS "no TARGET SDL2::SDL2, or SDL2, using variables") + target_include_directories(${CMAKE_PROJECT_NAME}_benchmark PUBLIC $) + target_link_libraries(${CMAKE_PROJECT_NAME}_benchmark PUBLIC ${SDL2_LIBRARIES}) + endif() + add_custom_target(benchmark COMMAND ${CMAKE_PROJECT_NAME}_benchmark) +endif() + +# create install target +include(GNUInstallDirs) +install( + TARGETS + ${CMAKE_PROJECT_NAME} + ${CMAKE_PROJECT_NAME}_util + pngdetail + EXPORT ${CMAKE_PROJECT_NAME}-targets + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} # set include path for installed library target + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + ) +install( + FILES + lodepng.h + lodepng_util.h + DESTINATION include + ) + +# Include module for fuctions +# - 'write_basic_package_version_file' +# - 'configure_package_config_file' +include(CMakePackageConfigHelpers) + +# generate and install termcolor-config.cmake file +# Configure '-config.cmake' +configure_package_config_file( + "cmake/config.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/generated/lodepng-config.cmake" + INSTALL_DESTINATION "lib/cmake/lodepng" +) +# install config file +install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/generated/lodepng-config.cmake" + DESTINATION "lib/cmake/lodepng" +) +# install targets file +install( + EXPORT "${CMAKE_PROJECT_NAME}-targets" + NAMESPACE "${CMAKE_PROJECT_NAME}::" + DESTINATION "lib/cmake/lodepng" +) diff --git a/cmake/config.cmake.in b/cmake/config.cmake.in new file mode 100644 index 00000000..8bb8c0e8 --- /dev/null +++ b/cmake/config.cmake.in @@ -0,0 +1,5 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/@CMAKE_PROJECT_NAME@-targets.cmake") +check_required_components("@CMAKE_PROJECT_NAME@") + diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 00000000..1482c2d6 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,37 @@ + +foreach(example + 4bit_palette + bmp2png + decode + encode + gzip + #opengl + optimize_png + png2bmp + png_info + reencode + #sdl + ) + add_executable(example_${example} + example_${example}.cpp) + target_link_libraries(example_${example} PRIVATE lodepng) +endforeach() + +# SDL2 Dependency +find_package(SDL2 CONFIG REQUIRED) +if (TARGET SDL2::SDL2) + message(STATUS "using TARGET SDL2::SDL2") +else() + message(STATUS "no TARGET SDL2::SDL2, or SDL2, using variables") +endif() +foreach(example + #opengl + #sdl + ) + if (TARGET SDL2::SDL2) + target_link_libraries(example_${example} PRIVATE SDL2::SDL2) + else() + target_include_directories(example_${example} PRIVATE $) + target_link_libraries(example_${example} PRIVATE ${SDL2_LIBRARIES}) + endif() +endforeach()