set(CMAKE_VERBOSE_MAKEFILE ON)
include(../Sanitise.cmake)

option(ENABLE_TCL "Enable Tcl scripting support" OFF)

set(CMAKE_BUILD_TYPE Debug)
#add_compile_definitions("NDEBUG") # debugging
#set(CMAKE_VERBOSE_MAKEFILE TRUE)

set(CMAKE_CXX_COMPILER c++)
#set(CMAKE_CXX_COMPILER clang++)
#set(CMAKE_CXX_COMPILER g++)

add_compile_options(-Wno-psabi) # stop obsolete warnings GCC v7 on RPi
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
#set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
#set(CMAKE_CXX_MODULE_STD 1)
set(CMAKE_CXX_FLAGS_DEBUG_INIT "-Wall")
set(CMAKE_BUILD_TYPE Debug)

find_package(PkgConfig)
pkg_check_modules(LUA REQUIRED lua)
include_directories(${LUA_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}) # for all targets

set(tcl_srcs)
set(tcl_libs)
set(nosc_pc_requires "ncursesw lua")
if(ENABLE_TCL)
	pkg_check_modules(TCL REQUIRED tcl)
	include_directories(${TCL_INCLUDE_DIRS})
	include(swig.cmake)
	add_compile_definitions(ENABLE_TCL)
	set(tcl_srcs tickle.cc ${wrapper_cc})
	set(tcl_libs ${TCL_LIBRARIES})
	string(APPEND nosc_pc_requires " tcl")
endif()

# files that we need for creating nosc, testneo and a shared library
set(common_srcs basic.cc cell.cc lua_script.cc oleofile.cc parser-2019.cc regions.cc sheet.cc spans.cc utils.cc vidi.cc ${tcl_srcs})
set(common_curses  io-curses.cc  io-2019.cc     menu-2025.cc    win.cc	 )
set(common_srcs ${common_curses} ${common_srcs})

# this is the "object library" target: compiles the sources only once
add_library(objlib OBJECT ${common_srcs})
set_property(TARGET objlib PROPERTY POSITION_INDEPENDENT_CODE 1) # shared libraries need PIC
# shared and static libraries built from the same object files
add_library(ploppy SHARED $<TARGET_OBJECTS:objlib>)
target_link_libraries(ploppy ncursesw tinfo)
add_library(ploppy_static STATIC $<TARGET_OBJECTS:objlib>)
target_link_libraries(ploppy_static ncursesw tinfo)
install(TARGETS ploppy ploppy_static)



add_executable(nosc)
target_sources(nosc PUBLIC ${neo_srcs} main.cc)
target_link_libraries(nosc ploppy_static)
target_link_libraries(nosc ${LUA_LIBRARIES} ${tcl_libs})
install(TARGETS nosc)




# build testneo, which you shouldn't install, as it's for testing purposes
add_executable(testneo testneo.cc)
#add_dependencies(testneo ploppy)
#target_link_libraries(testneo ploppy)
target_link_libraries(testneo ploppy_static)
target_link_libraries(testneo ${LUA_LIBRARIES} ${tcl_libs})


# 26/3 pkkconfig file
# Users may need to do something like
# export PKG_CONFIG_PATH=/usr/lib/pkgconfig
# There's something not 100% on this. 
# https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337
# example:
# /usr/lib/x86_64-linux-gnu/pkgconfig/ncurses.pc
set(CMAKE_INSTALL_FULL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/include/nosc)
set(NOSC_LIBDIR ${CMAKE_INSTALL_PREFIX}/lib) # needed by nosc.pc

set(VERSION "${PROJECT_VERSION}")
set(PROJECT_NAME "${PROJECT_NAME}")

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/nosc.pc.in ${CMAKE_CURRENT_BINARY_DIR}/nosc.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/nosc.pc DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/pkgconfig)
set(installed_headers neotypes.h oleofile.h sheet.h)
if(ENABLE_TCL)
	list(APPEND installed_headers tickle.h)
endif()
install(FILES
	${installed_headers}
	DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR})
