Skip to content

Commit

Permalink
Add cmake support with cmake-config file
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
NeroBurner committed Jan 17, 2019
1 parent 472d085 commit eb0e9e6
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
104 changes: 104 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
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
$<BUILD_INTERFACE:
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:
-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 $<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>)
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 '<PROJECT-NAME>-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"
)
5 changes: 5 additions & 0 deletions cmake/config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
check_required_components("@CMAKE_PROJECT_NAME@")

37 changes: 37 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 $<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>)
target_link_libraries(example_${example} PRIVATE ${SDL2_LIBRARIES})
endif()
endforeach()

0 comments on commit eb0e9e6

Please sign in to comment.