Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cmake support with cmake-config file #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
Copy link

@aleksey-nikolaev aleksey-nikolaev Sep 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also you can use option(BUILD_SHARED_LIBS "Build ${CMAKE_PROJECT_NAME} as shared library" OFF) if you care about type of library to being build

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this block to the tests/CMakeList.txt. I think, it will be much readable if your main folder will not contain tests or any other unnecessary code.

# 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})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the tests move this to the benchmark/CMakeList.txt

# SDL2 Dependency
find_package(SDL2 CONFIG REQUIRED)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what SDL2 is, but are you shure that when system will not contain this library the next if will work as expected?

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")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can you get here? Did you expect an older version of this library without SDL2::SDL2? If so, maybe it should be better to create your own SDL2Find.cmake instead of using SDL2_INCLUDE_DIRS directly.

endif()
foreach(example

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This for do nothing, isn' it?

#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()