cmake_minimum_required(VERSION 3.1)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
project(cimgui)

#general settings

set(IMGUI_LIBRARIES )
set(IMGUI_COMMON_FLAGS )
set(GENERATOR_IMPL "internal")

if(IMGUI_FREETYPE)
	set(GENERATOR_IMPL "${GENERATOR_IMPL} freetype")
	add_definitions("-DCIMGUI_FREETYPE=1")
endif(IMGUI_FREETYPE)

add_subdirectory(imgui)

if(IMGUI_BINDINGS)
add_library(cimgui SHARED "")
target_link_libraries(cimgui PUBLIC imgui ${IMGUI_LIBRARIES})
target_compile_definitions(cimgui PUBLIC IMGUI_IMPL_API=extern\t\"C\"\t)
target_include_directories(cimgui PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/generator/output)
target_include_directories(cimgui PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/imgui)
set_target_properties(cimgui PROPERTIES LINKER_LANGUAGE CXX)

set(GENERATOR_ARGS )
if(IMGUI_ALLEGRO)
	list(APPEND GENERATOR_ARGS allegro5)
endif()
if(IMGUI_GLFW)
	list(APPEND GENERATOR_ARGS glfw)
endif()
if(IMGUI_GLUT)
	list(APPEND GENERATOR_ARGS glut)
endif()
if(IMGUI_OPENGL)
	list(APPEND GENERATOR_ARGS opengl2 opengl3)
endif()
if(IMGUI_SDL)
	list(APPEND GENERATOR_ARGS sdl sdlrenderer)
endif()
if(IMGUI_VULKAN)
	list(APPEND GENERATOR_ARGS vulkan)
endif()
list(APPEND GENERATOR_ARGS ${IMGUI_COMMON_FLAGS} ${CMAKE_CPP_FLAGS})
set(CIMGUI_EXTRA_STUFF
	${CMAKE_SOURCE_DIR}/generator/output/definitions.json
	${CMAKE_SOURCE_DIR}/generator/output/definitions.lua
	${CMAKE_SOURCE_DIR}/generator/output/impl_definitions.json
	${CMAKE_SOURCE_DIR}/generator/output/impl_definitions.lua
	${CMAKE_SOURCE_DIR}/generator/output/overloads.txt
	${CMAKE_SOURCE_DIR}/generator/output/structs_and_enums.json
	${CMAKE_SOURCE_DIR}/generator/output/structs_and_enums.lua
	${CMAKE_SOURCE_DIR}/generator/output/typedefs_dict.json
	${CMAKE_SOURCE_DIR}/generator/output/typedefs_dict.lua
)
set(GENERATOR_OUTPUT_FILES
	${CMAKE_SOURCE_DIR}/cimgui.cpp
	${CMAKE_SOURCE_DIR}/cimgui.h
	${CMAKE_SOURCE_DIR}/generator/output/cimgui_impl.h
	${CIMGUI_EXTRA_STUFF}
)
ADD_CUSTOM_COMMAND(VERBATIM
	OUTPUT ${GENERATOR_OUTPUT_FILES}
	COMMAND luajit ./generator.lua gcc "${GENERATOR_IMPL}" ${GENERATOR_ARGS}
	WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/generator"
)
set(CIMGUI_SOURCES "${CMAKE_SOURCE_DIR}/cimgui.cpp")
set(CIMGUI_INCLUDE_FILES
	"${CMAKE_SOURCE_DIR}/cimgui.h"
	"${CMAKE_SOURCE_DIR}/generator/output/cimgui_impl.h"
)

target_sources(cimgui
	PUBLIC
	${CIMGUI_SOURCES}
	${CIMGUI_INCLUDE_FILES}
)

include(GNUInstallDirs)

#install
install(TARGETS cimgui LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${CIMGUI_INCLUDE_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/imgui")
install(FILES ${CIMGUI_EXTRA_STUFF} DESTINATION "${CMAKE_INSTALL_DATADIR}/cimgui")

#test
set(CIMGUI_TEST "no" CACHE STRING "Enable compilation of a test unit based on imgui null")

if (CIMGUI_TEST)
  add_subdirectory(test)
endif ()
endif()

