Skip to content

CPM support #17

@triuk

Description

@triuk

Hi, this piece of code is precious. I really love it exists. But the setup is not that simple, because it links to other repositories when downloaded with git and if you already use imgui in the project, it means you'll end up linking all things manually and probably have target conflicts.
So to ease this integration, I made a CMakeLists.txt that uses CPM to download and build imgui-vtk. The benefit is obvious - the CPM checks for new version of imgui-vtk every time you configure your project and run cmake on it. That means it is always up to date without effort. When you add other's project to yours, you definitely do not want to do it manually. I'll be happy, if you comment or improve this idea :)

Usage: download just main.cpp from the imgui-vtk project. Add my CMakeFiles.txt code to the same folder (so the working dir has only two files: main.cpp and this CMakeFiles.txt. Configure and build the executable.

cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR)

# #####################
# # DOWNLOAD SECTION ##
# #####################
set(CPM_DOWNLOAD_VERSION 0.36.0)

if(CPM_SOURCE_CACHE)
  set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
  set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
  set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

if(NOT(EXISTS ${CPM_DOWNLOAD_LOCATION}))
  message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
  file(DOWNLOAD
    https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
    ${CPM_DOWNLOAD_LOCATION}
  )
endif()

include(${CPM_DOWNLOAD_LOCATION})

# # ImGUI VTK ########
find_package(imvtk QUIET)

if(NOT imvtk_FOUND)
  CPMAddPackage(
    NAME imvtk
    GITHUB_REPOSITORY trlsmax/imgui-vtk
    GIT_TAG origin/master
  )
endif()

# ###### END OF DOWNLOAD SECTION ########################
# #######################################################
project(imgui-vtk)

set(CMAKE_CXX_STANDARD 11)
set(EXEC_NAME imvtkCPM)
set(PROGRAM_SRC main.cpp)
add_executable(${EXEC_NAME} ${PROGRAM_SRC})

# VTK
find_package(VTK COMPONENTS
  CommonCore
  CommonColor
  CommonDataModel
  FiltersCore
  InteractionStyle
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
  QUIET
)

if(NOT VTK_FOUND)
  message(FATAL_ERROR "VTK not found!")
  return()
endif()

if(VTK_VERSION VERSION_LESS "9.0.0")
  include(${VTK_USE_FILE})
else()
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ${EXEC_NAME}
    MODULES ${VTK_LIBRARIES}
  )
endif()

target_link_libraries(${EXEC_NAME} imgui_vtk_viewer)

Successfully tried on Ubuntu 22.04 with VS Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions