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

Conversation

NeroBurner
Copy link

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)

If you have any questions about this commit I'd be happy to clear out any misunderstandings

cmake -S . -B _build -DCMAKE_INSTALL_PREFIX="${PWD}/_install" -DCMAKE_BUILD_TYPE=Release
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/neroburner/repos/external/lodepng/_build

$ cmake --build _build --target install
Scanning dependencies of target lodepng
[ 12%] Building CXX object CMakeFiles/lodepng.dir/lodepng.cpp.o
[ 25%] Linking CXX static library liblodepng.a
[ 25%] Built target lodepng
Scanning dependencies of target lodepng_util
[ 37%] Building CXX object CMakeFiles/lodepng_util.dir/lodepng_util.cpp.o
[ 50%] Linking CXX static library liblodepng_util.a
[ 50%] Built target lodepng_util
Scanning dependencies of target lodepng_unittest
[ 62%] Building CXX object CMakeFiles/lodepng_unittest.dir/lodepng_unittest.cpp.o
[ 75%] Linking CXX executable lodepng_unittest
[ 75%] Built target lodepng_unittest
Scanning dependencies of target pngdetail
[ 87%] Building CXX object CMakeFiles/pngdetail.dir/pngdetail.cpp.o
[100%] Linking CXX executable pngdetail
[100%] Built target pngdetail
Install the project...
-- Install configuration: "Release"
-- Installing: /home/neroburner/repos/external/lodepng/_install/lib/liblodepng.a
-- Installing: /home/neroburner/repos/external/lodepng/_install/lib/liblodepng_util.a
-- Installing: /home/neroburner/repos/external/lodepng/_install/bin/pngdetail
-- Installing: /home/neroburner/repos/external/lodepng/_install/include/lodepng.h
-- Installing: /home/neroburner/repos/external/lodepng/_install/include/lodepng_util.h
-- Installing: /home/neroburner/repos/external/lodepng/_install/lib/cmake/lodepng/lodepng-config.cmake
-- Installing: /home/neroburner/repos/external/lodepng/_install/lib/cmake/lodepng/lodepng-targets.cmake
-- Installing: /home/neroburner/repos/external/lodepng/_install/lib/cmake/lodepng/lodepng-targets-release.cmake

@ekg
Copy link

ekg commented Mar 14, 2019

I just realized that there are many cmakeification PRs here. Is this project not active anymore?

@NeroBurner
Copy link
Author

not dead, @lvandeve still commits, but I think he is not interested in cmake support

@ekg
Copy link

ekg commented Mar 15, 2019

In the end, it's just a CMakeLists.txt. It's very helpful when integrating this into other projects.

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)
@NeroBurner
Copy link
Author

@aleksey-nikolaev could you review my attempt at adding CMake support for lodepng? Anything missing here?

Copy link

@aleksey-nikolaev aleksey-nikolaev left a comment

Choose a reason for hiding this comment

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

I think you need to refactoring your project first of all

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.

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

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)

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

else()
message(STATUS "no TARGET SDL2::SDL2, or SDL2, using variables")
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?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants