-
Notifications
You must be signed in to change notification settings - Fork 427
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can move this block to the |
||
# 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}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As for the tests move this to the |
||
# SDL2 Dependency | ||
find_package(SDL2 CONFIG REQUIRED) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know what |
||
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" | ||
) |
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@") | ||
|
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
endif() | ||
foreach(example | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
#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() |
There was a problem hiding this comment.
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