# LinuxNoteText -- the editing canvas.
#
# Builds Scintilla (the editor), Lexilla (the syntax colouring for 125
# languages) and a small C shim into one shared library that Python loads
# with ctypes. The widget itself is handed to PySide6 as a pointer, so the
# window, the menus and the rest of the program stay in Python.
#
# It links against the Qt that PySide6 already has loaded rather than a
# second copy: see SciCodec.h for why that matters.

cmake_minimum_required(VERSION 3.16)
project(lntcanvas LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_AUTOMOC ON)

if(NOT CMAKE_BUILD_TYPE)
	set(CMAKE_BUILD_TYPE Release)
endif()

find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)

set(SCI  ${CMAKE_CURRENT_SOURCE_DIR}/../src/scintilla)
set(LEX  ${CMAKE_CURRENT_SOURCE_DIR}/../src/lexilla)

file(READ ${SCI}/version.txt SCI_VERSION)
string(STRIP "${SCI_VERSION}" SCI_VERSION)

# ---- the sources, taken as they come so an upstream update needs no edit ----
file(GLOB SCI_CORE     ${SCI}/src/*.cxx)
file(GLOB SCI_QT_BASE  ${SCI}/qt/ScintillaEditBase/*.cpp)
file(GLOB SCI_QT_EDIT  ${SCI}/qt/ScintillaEdit/*.cpp)
file(GLOB LEX_LIB      ${LEX}/lexlib/*.cxx)
file(GLOB LEX_SRC      ${LEX}/src/*.cxx)
file(GLOB LEX_LEXERS   ${LEX}/lexers/*.cxx)

list(LENGTH LEX_LEXERS LEXER_COUNT)
message(STATUS "LinuxNoteText: Scintilla ${SCI_VERSION}, ${LEXER_COUNT} language lexers")

add_library(lntcanvas SHARED
	${SCI_CORE}
	${SCI_QT_BASE}
	${SCI_QT_EDIT}
	${LEX_LIB}
	${LEX_SRC}
	${LEX_LEXERS}
	SciCodec.cpp
	lnt_shim.cpp
)

target_include_directories(lntcanvas PRIVATE
	# ours first, so <QTextCodec> finds the stand-in and Scintilla's own
	# sources compile with no changes at all
	${CMAKE_CURRENT_SOURCE_DIR}/shim_include
	${CMAKE_CURRENT_SOURCE_DIR}
	${SCI}/include
	${SCI}/src
	${SCI}/qt/ScintillaEditBase
	${SCI}/qt/ScintillaEdit
	${LEX}/include
	${LEX}/lexlib
)

target_compile_definitions(lntcanvas PRIVATE
	SCINTILLA_QT=1
	MAKING_LIBRARY=1
	EXPORT_IMPORT_API=
	SCINTILLA_VERSION="${SCI_VERSION}"
)

target_compile_options(lntcanvas PRIVATE -w)

target_link_libraries(lntcanvas PRIVATE Qt6::Core Qt6::Gui Qt6::Widgets)

set_target_properties(lntcanvas PROPERTIES
	LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib
	PREFIX "lib"
)
