# CMakeLists for AppStream database shared lib

find_package(GLIB2 2.46 REQUIRED)
pkg_check_modules(GIO2 REQUIRED gio-2.0)
find_package(GI REQUIRED)
find_package(LibXml2 REQUIRED)
find_package(Xapian 1.2 REQUIRED)
find_package(Protobuf REQUIRED)
find_library(M_LIB m)
pkg_check_modules(YAML REQUIRED yaml-0.1)

include(GObjectIntrospectionMacros)
set(INTROSPECTION_GIRS)
set(INTROSPECTION_SCANNER_ARGS "--identifier-prefix=As" "--symbol-prefix=as" "--warn-all" "--add-include-path=${CMAKE_CURRENT_SOURCE_DIR}")
set(INTROSPECTION_COMPILER_ARGS "--includedir=${CMAKE_CURRENT_SOURCE_DIR}")

# check if the git tools are really found.
if (NOT INTROSPECTION_SCANNER)
	message (FATAL_ERROR "GObject introspection scanner was not found! Please install it to continue.")
endif ()
if (NOT INTROSPECTION_COMPILER)
	message (FATAL_ERROR "GObject introspection compiler was not found! Please install it to continue.")
endif ()

set (srcdir ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "Source dir")
set(AS_VERSION_BASE "${AS_VERSION_MAJOR}.${AS_VERSION_MINOR}" CACHE INTERNAL "LibAppStream combined version number")

if (NOT PROTOBUF_PROTOC_EXECUTABLE)
	message (FATAL_ERROR "ProtoBuf compiler (protoc) was not found!")
endif ()

protobuf_generate_cpp(AS_PROTO_SRCS AS_PROTO_HDRS xapian/asxentries.proto)

set(ASLIB_XAP_SOURCES ${AS_PROTO_SRCS} ${AS_PROTO_HDRS}
		xapian/database-schema.hpp
		xapian/database-read.hpp
		xapian/database-read.cpp
		xapian/database-write.hpp
		xapian/database-write.cpp
		xapian/database-cwrap.hpp
		xapian/database-cwrap.cpp
)
set_source_files_properties(${ASLIB_XAP_SOURCES} PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")

set(APPSTREAM_LIB_SRC
		as-utils.c
		# internal stuff to build the database
		as-cache-builder.c
		as-xmldata.c
		as-yamldata.c
		# (mostly) public API
		as-metadata.c
		as-component.c
		as-enums.c
		as-provided.c
		as-data-pool.c
		as-database.c
		as-category.c
		as-menu-parser.c
		as-distro-details.c
		as-screenshot.c
		as-image.c
		as-release.c
		as-validator.c
		as-validator-issue.c
		as-icon.c
		as-translation.c
)

set(APPSTREAM_LIB_PUBLIC_HEADERS
		appstream.h
		as-utils.h
		as-metadata.h
		as-component.h
		as-data-pool.h
		as-enums.h
		as-provided.h
		as-database.h
		as-category.h
		as-menu-parser.h
		as-distro-details.h
		as-screenshot.h
		as-image.h
		as-release.h
		as-validator.h
		as-validator-issue.h
		as-icon.h
		as-translation.h
)

set(APPSTREAM_LIB_PRIVATE_HEADERS
		as-utils-private.h
		as-xmldata.h
		as-yamldata.h
		as-component-private.h
		as-screenshot-private.h
		as-cache-builder.h
)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/xapian
		    ${CMAKE_BINARY_DIR}
		    ${CMAKE_BINARY_DIR}/src
		    ${GLIB2_INCLUDE_DIR}
		    ${GIO2_INCLUDE_DIR}
		    ${LIBXML2_INCLUDE_DIR}
		    ${YAML_INCLUDE_DIR}
		    ${XAPIAN_INCLUDE_DIR}
		    ${PROTOBUF_INCLUDE_DIRS}
)

add_definitions("-DAS_COMPILATION")

# ensure that the GI compiler has been found (apparently, the automatic tests fail on some machines)
if (INTROSPECTION_COMPILER STREQUAL "")
  message(FATAL_ERROR "GObject-Introspection was not found! Please install it to continue.")
endif()

add_library(appstream SHARED ${ASLIB_XAP_SOURCES} ${APPSTREAM_LIB_SRC} ${APPSTREAM_LIB_PUBLIC_HEADERS} ${APPSTREAM_LIB_PRIVATE_HEADERS})
set_target_properties(appstream PROPERTIES VERSION ${AS_VERSION} SOVERSION ${APPSTREAM_LIB_API_LEVEL})

target_link_libraries(appstream
		${GLIB2_LIBRARIES}
		${GIO2_LIBRARIES}
		${LIBXML2_LIBRARIES}
		${YAML_LIBRARIES}
		${XAPIAN_LIBRARIES}
		${PROTOBUF_LITE_LIBRARIES}
)

configure_file (${CMAKE_CURRENT_SOURCE_DIR}/appstream.pc.in ${CMAKE_CURRENT_BINARY_DIR}/appstream.pc @ONLY)

####
# GObject Introspection
macro(_list_prefix _outvar _listvar _prefix)
  set(${_outvar})
  foreach(_item IN LISTS ${_listvar})
    list(APPEND ${_outvar} ${_prefix}${_item})
  endforeach()
endmacro(_list_prefix)

set(introspection_files ${APPSTREAM_LIB_SRC} ${APPSTREAM_LIB_PUBLIC_HEADERS})
set(AppStream_1_0_gir "appstream")
set(AppStream_1_0_gir_INCLUDES GObject-2.0 Gio-2.0)
set(AppStream_1_0_gir_CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/" "-DAS_COMPILATION")
set(AppStream_1_0_gir_LIBS appstream)

_list_prefix(AppStream_1_0_gir_FILES introspection_files "${CMAKE_CURRENT_SOURCE_DIR}/")
set(AppStream_1_0_gir_SCANNERFLAGS --c-include appstream.h)
set(AppStream_1_0_gir_EXPORT_PACKAGES appstream)

list(APPEND INTROSPECTION_GIRS AppStream-1.0.gir)
gir_add_introspections(INTROSPECTION_GIRS)
####

install(TARGETS appstream DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${APPSTREAM_LIB_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/AppStream)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/appstream.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig/)
