cmake_minimum_required(VERSION 3.8.2)
project(imgui VERSION 1.73.0 LANGUAGES CXX C)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

include(GNUInstallDirs)

set(pc_req_private)

option(IMGUI_ALLEGRO OFF)
option(IMGUI_EXAMPLES "Build ImGui examples" OFF)
option(IMGUI_FREETYPE OFF)
option(IMGUI_GLFW OFF)
option(IMGUI_GLUT OFF)
option(IMGUI_OPENGL OFF)
option(IMGUI_SDL OFF)
option(IMGUI_VULKAN OFF)

add_library (imgui_objects
	OBJECT
	imgui.cpp
	imgui_demo.cpp
	imgui_draw.cpp
	imgui_tables.cpp
	imgui_widgets.cpp
)
add_library (imgui SHARED "")
target_include_directories(imgui_objects PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/backends")
#set(IMGUI_COMMON_FLAGS "-DIMGUI_USE_WCHAR32 -DIMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION -DIMGUI_STB_RECT_PACK_FILENAME=\"<stb/stb_rect_pack.h>\"")
set(IMGUI_COMMON_FLAGS "-DIMGUI_USE_WCHAR32")

if(IMGUI_ALLEGRO)
	find_package(PkgConfig REQUIRED)
	pkg_check_modules(ALLEGRO5 REQUIRED allegro-5 allegro_main-5 allegro_primitives-5)
	target_sources(imgui_objects PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/backends/imgui_impl_allegro5.cpp")
	target_include_directories(imgui_objects PUBLIC ${ALLEGRO5_INCLUDE_DIRS})
	list(APPEND IMGUI_LIBRARIES ${ALLEGRO5_LIBRARIES})
	string(APPEND pc_req_private " allegro-5 allegro_main-5 allegro_primitives-5")
endif()
if(IMGUI_FREETYPE)
	find_package(Freetype REQUIRED)
	target_sources(imgui_objects PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/misc/freetype/imgui_freetype.cpp")
	target_include_directories(imgui_objects PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/misc/freetype" ${FREETYPE_INCLUDE_DIRS})
	list(APPEND IMGUI_LIBRARIES ${FREETYPE_LIBRARIES})
#	set(IMGUI_COMMON_FLAGS "${IMGUI_COMMON_FLAGS} -DIMGUI_ENABLE_FREETYPE -DIMGUI_ENABLE_STB_TRUETYPE -DIMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION -DIMGUI_STB_TRUETYPE_FILENAME=\"<stb/stb_truetype.h>\"")
	set(IMGUI_COMMON_FLAGS "${IMGUI_COMMON_FLAGS} -DIMGUI_ENABLE_FREETYPE -DIMGUI_ENABLE_STB_TRUETYPE")
	add_executable(binary_to_compressed_c "${CMAKE_CURRENT_SOURCE_DIR}/misc/fonts/binary_to_compressed_c.cpp")
	target_include_directories(binary_to_compressed_c PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
	target_link_libraries(binary_to_compressed_c PUBLIC imgui)
	install(TARGETS binary_to_compressed_c DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/imgui")
	string(APPEND pc_req_private " freetype2")
endif()
if(IMGUI_GLFW)
	find_package(glfw3 REQUIRED)
	target_sources(imgui_objects PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/backends/imgui_impl_glfw.cpp")
	list(APPEND IMGUI_LIBRARIES glfw)
	string(APPEND pc_req_private " glfw3")
endif()
if(IMGUI_GLUT)
	find_package(FreeGLUT REQUIRED)
	target_sources(imgui_objects PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/backends/imgui_impl_glut.cpp")
	target_include_directories(imgui_objects PUBLIC ${FreeGLUT_INCLUDE_DIRS})
	list(APPEND IMGUI_LIBRARIES FreeGLUT::freeglut)
	string(APPEND pc_req_private " glut")
endif()
if(IMGUI_OPENGL)
	find_package(OpenGL REQUIRED)
	target_sources(imgui_objects
		PUBLIC
		"${CMAKE_CURRENT_SOURCE_DIR}/backends/imgui_impl_opengl2.cpp"
		"${CMAKE_CURRENT_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp"
	)
	target_include_directories(imgui_objects PUBLIC ${OPENGL_INCLUDE_DIR})
	list(APPEND IMGUI_LIBRARIES ${OPENGL_gl_LIBRARY})
	string(APPEND pc_req_private " opengl")
endif()
if(IMGUI_SDL)
	find_package(SDL2 REQUIRED)
	target_sources(imgui_objects
		PUBLIC
		"${CMAKE_CURRENT_SOURCE_DIR}/backends/imgui_impl_sdl.cpp"
		"${CMAKE_CURRENT_SOURCE_DIR}/backends/imgui_impl_sdlrenderer.cpp"
	)
	target_include_directories(imgui_objects PUBLIC "${SDL2_INCLUDE_DIRS}")
	list(APPEND IMGUI_LIBRARIES "${SDL2_LIBRARIES}")
	string(APPEND pc_req_private " sdl2")
endif()
if(IMGUI_VULKAN)
	find_package(Vulkan REQUIRED)
	target_sources(imgui_objects PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/backends/imgui_impl_vulkan.cpp")
	set(IMGUI_COMMON_FLAGS "${IMGUI_COMMON_FLAGS} -DImTextureID=ImU64")
	target_include_directories(imgui_objects PUBLIC ${Vulkan_INCLUDE_DIRS})
	list(APPEND IMGUI_LIBRARIES Vulkan::Vulkan)
	string(APPEND pc_req_private " vulkan")
endif()

if(IMGUI_EXAMPLES)
	install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/examples" DESTINATION "${CMAKE_INSTALL_DOCDIR}")
	add_executable(example_null "${CMAKE_CURRENT_SOURCE_DIR}/examples/example_null/main.cpp")
	target_include_directories(example_null PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
	target_link_libraries(example_null PUBLIC imgui)
	install(TARGETS example_null DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/imgui/examples" )
	if(IMGUI_ALLEGRO)
		add_executable(example_allegro5 "${CMAKE_CURRENT_SOURCE_DIR}/examples/example_allegro5/main.cpp")
		target_include_directories(example_allegro5 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/backends")
		target_link_libraries(example_allegro5 PUBLIC imgui)
		install(TARGETS example_allegro5 DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/imgui/examples" )
	endif()
	if(IMGUI_GLFW AND IMGUI_OPENGL)
		add_executable(example_glfw_opengl2 "${CMAKE_CURRENT_SOURCE_DIR}/examples/example_glfw_opengl2/main.cpp")
		target_include_directories(example_glfw_opengl2 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/backends")
		target_link_libraries(example_glfw_opengl2 PUBLIC imgui)
		install(TARGETS example_allegro5 DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/imgui/examples" )
		add_executable(example_glfw_opengl3 "${CMAKE_CURRENT_SOURCE_DIR}/examples/example_glfw_opengl3/main.cpp")
		target_include_directories(example_glfw_opengl3 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/backends")
		target_link_libraries(example_glfw_opengl3 PUBLIC imgui)
		install(TARGETS example_glfw_opengl3 DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/imgui/examples" )
	endif()
	if(IMGUI_GLFW AND IMGUI_VULKAN)
		add_executable(example_glfw_vulkan "${CMAKE_CURRENT_SOURCE_DIR}/examples/example_glfw_vulkan/main.cpp")
		target_include_directories(example_glfw_vulkan PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/backends")
		target_link_libraries(example_glfw_vulkan PUBLIC imgui)
		install(TARGETS example_glfw_vulkan DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/imgui/examples" )
	endif()
	if(IMGUI_GLUT AND IMGUI_OPENGL)
		add_executable(example_glut_opengl2 "${CMAKE_CURRENT_SOURCE_DIR}/examples/example_glut_opengl2/main.cpp")
		target_include_directories(example_glut_opengl2 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/backends")
		target_link_libraries(example_glut_opengl2 PUBLIC imgui)
		install(TARGETS example_glut_opengl2 DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/imgui/examples" )
	endif()
	if(IMGUI_SDL)
		add_executable(example_sdl_sdlrenderer "${CMAKE_CURRENT_SOURCE_DIR}/examples/example_sdl_sdlrenderer/main.cpp")
		target_include_directories(example_sdl_sdlrenderer PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/backends" "${SDL2_INCLUDE_DIRS}")
		target_link_libraries(example_sdl_sdlrenderer PUBLIC imgui)
		install(TARGETS example_sdl_sdlrenderer DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/imgui/examples" )
		if(IMGUI_OPENGL)
			add_executable(example_sdl_opengl2 "${CMAKE_CURRENT_SOURCE_DIR}/examples/example_sdl_opengl2/main.cpp")
			target_include_directories(example_sdl_opengl2 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/backends" "${SDL2_INCLUDE_DIRS}")
			target_link_libraries(example_sdl_opengl2 PUBLIC imgui)
			install(TARGETS example_sdl_opengl2 DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/imgui/examples" )
			add_executable(example_sdl_opengl3 "${CMAKE_CURRENT_SOURCE_DIR}/examples/example_sdl_opengl3/main.cpp")
			target_include_directories(example_sdl_opengl3 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/backends" "${SDL2_INCLUDE_DIRS}")
			target_link_libraries(example_sdl_opengl3 PUBLIC imgui)
			install(TARGETS example_sdl_opengl2 DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/imgui/examples" )
		endif()
		if(IMGUI_VULKAN)
			add_executable(example_sdl_vulkan "${CMAKE_CURRENT_SOURCE_DIR}/examples/example_sdl_vulkan/main.cpp")
			target_include_directories(example_sdl_vulkan PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/backends" "${SDL2_INCLUDE_DIRS}")
			target_link_libraries(example_sdl_vulkan PUBLIC imgui)
			install(TARGETS example_sdl_vulkan DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/imgui/examples" )
		endif()
	endif()
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${IMGUI_COMMON_FLAGS}")
target_sources (imgui PUBLIC $<TARGET_OBJECTS:imgui_objects>)

add_library(imgui::imgui ALIAS imgui)
target_link_libraries(imgui PUBLIC ${IMGUI_LIBRARIES})

file(GLOB IMGUI_INCLUDE_FILES "*.h" "backends/*.h" "misc/freetype/*.h")
install(FILES ${IMGUI_INCLUDE_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/imgui")
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/backends/vulkan" DESTINATION "${CMAKE_INSTALL_DATADIR}/imgui/backends")
file(GLOB IMGUI_DOC_FILES "docs/*")
install(FILES ${IMGUI_DOC_FILES} DESTINATION "${CMAKE_INSTALL_DOCDIR}")
install(TARGETS imgui)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/imgui.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/imgui.pc" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/imgui.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
set(IMGUI_COMMON_FLAGS "${IMGUI_COMMON_FLAGS}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE)
