include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

#Singletons in common.h depend on lots of things in subdirs
include_directories(
	${CMAKE_SOURCE_DIR}/src
	${CMAKE_SOURCE_DIR}/src/config
	${CMAKE_SOURCE_DIR}/src/core
	${CMAKE_SOURCE_DIR}/src/history
	${CMAKE_SOURCE_DIR}/src/intro
	${CMAKE_SOURCE_DIR}/src/logs
	${CMAKE_SOURCE_DIR}/src/portage
	${CMAKE_SOURCE_DIR}/src/queue
)

#core
set(core_sources
	core/cacheportagejob.cpp
	core/categorieslistview.cpp
	core/dependatom.cpp
	core/dependencyview.cpp
	core/emerge.cpp
	core/etcupdate.cpp
	core/global.cpp
	# After running clazy-fix-missing-qobject-macro it added Q_OBJECT to the classes in these headers.
	# Apparently that combined with not having a separate .cpp for the implementation in this list makes
	# it now required to list them in sources.
	# Hint from: https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable#comment105441153_3065154
	core/LoadPackageHardMaskJob.h
	core/LoadPackageKeywordsJob.h
	core/LoadPackageUseJob.h
	core/LoadPackageUserHardMaskJob.h
	core/LoadPackageUserUnMaskJob.h
	core/packageinspector.cpp
	core/packagelistmodel.cpp
	core/packagelistitem.cpp
	core/packageversion.cpp
	core/portagedb.cpp
	core/portagefiles.cpp
	core/progressjob.cpp
	core/scanhistoryjob.cpp
	core/scanportagejob.cpp
	core/scanupdatesjob.cpp
	core/signalist.cpp
	core/tooltip.cpp
	core/versionview.cpp )
ki18n_wrap_ui( core_sources core/inspectorbase.ui )

#history
set(history_sources
	history/history.cpp
	history/historylistview.cpp
	history/historytab.cpp
	history/mergelistview.cpp
	history/mergetab.cpp )
ki18n_wrap_ui( history_sources history/historybase.ui history/mergebase.ui )

#portage
set(portage_sources
	portage/portage.cpp
	portage/portagetab.cpp
	portage/uninstallinspector.cpp
	portage/portagelistview.cpp )
ki18n_wrap_ui( portage_sources portage/uninstallbase.ui portage/portagebase.ui )

#config
set( config_sources config/configdialog.cpp )
ki18n_wrap_ui(	config_sources
				config/options1.ui
				config/options2.ui
				config/options3.ui
				config/options4.ui )
kconfig_add_kcfg_files( config_sources config/settings.kcfgc )

#queue
set(queue_sources
	queue/queue.cpp
	queue/queuetab.cpp
	queue/queuelistmodel.cpp
	queue/queuelistview.cpp
	queue/queuelistitem.cpp
	queue/queuelistdelegate.cpp )
ki18n_wrap_ui( queue_sources queue/queuebase.ui )

#intro
set( intro_sources intro/introdlg.cpp )
ki18n_wrap_ui( intro_sources intro/intro.ui )

#logs
set( logs_sources logs/log.cpp logs/logstab.cpp )
ki18n_wrap_ui( logs_sources logs/logsbase.ui )

#kuroo
set( kuroo_sources
	kuroo.cpp
	kurooinit.cpp
	kurooview.cpp
	main.cpp
	statusbar.cpp
	systemtray.cpp )

set( all_sources
	${core_sources}
	${history_sources}
	${portage_sources}
	${config_sources}
	${queue_sources}
	${intro_sources}
	${logs_sources}
	${kuroo_sources} )

add_executable(kuroo
	${all_sources} )
kde_enable_exceptions()

#This is a rather hackish way of maintaining the normal kuroo executable build as is
#without any exports or instrumentation and
#creating a separate library which exports everything by default and is compiled with
#coverage instrumentation, which is then linked by the tests. Various attempts to
#set_property(ENABLE_EXPORTS) on the kuroo executable and link directly to it all
#failed :-(
if (BUILD_TESTING)
	add_library(libkuroo
		${all_sources} #statusbar is used by lib things
	)
	set_target_properties(libkuroo
		PROPERTIES ENABLE_EXPORTS on
			CXX_VISIBILITY_PRESET default
			VISIBILITY_INLINES_HIDDEN 0
	)

	#There's an existing option BUILD_COVERAGE which gets pulled in via KDECMakeSettings
	#which enables gcov instead of LLVM-style coverage
	option(BUILD_LLVM_COVERAGE "Build the project with llvm-cov support" OFF)
	if(BUILD_LLVM_COVERAGE)
		#This is based on https://clang.llvm.org/docs/SourceBasedCodeCoverage.html (2024-12-18)
		target_compile_options(libkuroo PRIVATE -fprofile-instr-generate -fcoverage-mapping)
		target_link_options(libkuroo PRIVATE -fprofile-instr-generate -fcoverage-mapping)
		#from the build/bin directory, run tests with:
		#'SANDBOX_WRITE="${HOME}/code/kuroo/trunk/build" SANDBOX_PREDICT="/etc/portage/package.use" sandbox ./portagefilestests'
		#you may need to remove stale coverage data 'find ../autotests/CMakeFiles/ -name '*.gc*'  -delete'
		#afterwards run llvm-profdata merge -sparse default.profraw -o default.profdata
		#and based on https://github.com/llvm/llvm-project/blob/main/llvm/utils/prepare-code-coverage-artifact.py
		#llvm-cov show liblibkuroo.so -format html -o coverage_report -instr-profile default.profdata -show-line-counts-or-regions -show-directory-coverage
	endif()
	#generate_export_header(kuroo)
	target_link_libraries( libkuroo
		Qt6::Core
		Qt6::Core5Compat	# for QRegExp
		Qt6::Gui
		Qt6::Widgets
		KF6::AuthCore
		KF6::ConfigCore
		KF6::ConfigGui
		KF6::ConfigWidgets
		KF6::CoreAddons
		KF6::Crash
		KF6::I18n
		KF6::KIOCore
		KF6::ItemViews
		KF6::StatusNotifierItem	#separate from Notifications in KF6?
		KF6::ThreadWeaver
		KF6::TextWidgets
		KF6::WidgetsAddons
		KF6::XmlGui
		sqlite3
	)
endif()

# KTorrent sets this just before target_link_libraries, but it doesn't seem to work
# to set the window icon, at least when running from build dir
set( KUROO_ICONS_PNG
	../pics/16-apps-kuroo.png
	../pics/22-apps-kuroo.png
	../pics/32-apps-kuroo.png
	../pics/48-apps-kuroo.png
)

ecm_add_app_icon( kuroo ICONS ${KUROO_ICONS_PNG} )

target_link_libraries( kuroo
	Qt6::Core
	Qt6::Core5Compat	# for QRegExp
	Qt6::Gui
	Qt6::Widgets
	KF6::AuthCore
	KF6::ConfigCore
	KF6::ConfigGui
	KF6::ConfigWidgets
	KF6::CoreAddons
	KF6::Crash
	KF6::I18n
	KF6::KIOCore
	KF6::ItemViews
	KF6::StatusNotifierItem	#separate from Notifications in KF6?
	KF6::ThreadWeaver
	KF6::TextWidgets
	KF6::WidgetsAddons
	KF6::XmlGui
	sqlite3		# TODO: use some sort of ORM maybe find_package?
	# lddtree also reports these though they aren't used directly
	# -DCMAKE_LINK_WHAT_YOU_USE=TRUE also reports these specifically are unused direct dependencies
	#KF6::SonnetUi
	#KF6::ColorScheme
	#GLX
	#OpenGL
	#Qt6::Network
	#Qt6::Dbus
	#Qt6::Xml
)

add_executable( kuroo_helper auth/kuroohelper.cpp )
target_link_libraries( kuroo_helper
	# These are all used directly and not provided by any other dependency. It "compiles and works" on the development system with just these
	KF6::KIOCore
	KF6::AuthCore
	# Builds without explicit declaration, but these make sense
	Qt6::Core
	KF6::CoreAddons	# KProcess
)
if( NOT DEFINED KAUTH_HELPER_INSTALL_DIR )
	message(FATAL_ERROR "KAUTH_HELPER_INSTALL_DIR not defined")
endif()
install( TARGETS kuroo_helper DESTINATION ${KAUTH_HELPER_INSTALL_DIR} )
kauth_install_helper_files( kuroo_helper org.gentoo.kuroo root )
kauth_install_actions( org.gentoo.kuroo auth/org.gentoo.kuroo.actions )

########### install ###############
if( NOT DEFINED KDE_INSTALL_TARGETS_DEFAULT_ARGS )
	message(FATAL_ERROR "KDE_INSTALL_TARGETS_DEFAULT_ARGS not defined")
endif()
install( TARGETS kuroo ${KDE_INSTALL_TARGETS_DEFAULT_ARGS} )
#install( FILES  DESTINATION ${DATA_INSTALL_DIR}/autostart )
if( NOT DEFINED KDE_INSTALL_KCFGDIR )
	message(FATAL_ERROR "KDE_INSTALL_KCFGDIR not defined")
endif()
install( FILES  config/kurooconfig.kcfg DESTINATION ${KDE_INSTALL_KCFGDIR} )
if( NOT DEFINED KDE_INSTALL_KXMLGUIDIR )
	message(FATAL_ERROR "KDE_INSTALL_KXMLGUIDIR not defined")
endif()
install( FILES  kurooui.rc DESTINATION ${KDE_INSTALL_KXMLGUIDIR}/kuroo )
# ktorrent uses install( PROGRAMS ) instead of install( FILES ) for the desktop file
if( NOT DEFINED KDE_INSTALL_APPDIR )
	message(FATAL_ERROR "KDE_INSTALL_APPDIR not defined")
endif()
install( PROGRAMS org.gentoo.kuroo.desktop DESTINATION ${KDE_INSTALL_APPDIR} )
