# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set(public_header_files
  arg.h
  assert.h
  atomic.h
  avltree.h
  def.h
  dequeue.h
  dllist.h
  dynbuf.h
  file.h
  gfxbuf.h
  gfxbuf_xpm.h
  libbase.h
  log.h
  log_wrappers.h
  ptr_set.h
  ptr_stack.h
  ptr_vector.h
  ref.h
  sock.h
  strutil.h
  subprocess.h
  test.h
  thread.h
  time.h
  vector.h)

set(sources
  arg.c
  atomic.c
  avltree.c
  dequeue.c
  dllist.c
  dynbuf.c
  file.c
  gfxbuf.c
  gfxbuf_xpm.c
  log.c
  ptr_set.c
  ptr_stack.c
  ptr_vector.c
  ref.c
  sock.c
  strutil.c
  subprocess.c
  test.c
  thread.c
  time.c)

add_library(libbase STATIC)
target_sources(libbase PRIVATE ${sources})
target_include_directories(
  libbase
  PUBLIC
  $<INSTALL_INTERFACE:include>
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>)
target_include_directories(libbase PRIVATE "${CURSES_INCLUDE_DIRS}")
target_link_libraries(libbase PRIVATE "${CURSES_LIBRARIES}" libbase_compiler_flags)
set_target_properties(
  libbase PROPERTIES
  VERSION 1.0
  PUBLIC_HEADER "${public_header_files}"
)

if(iwyu_path_and_options)
  set_target_properties(
    libbase PROPERTIES
    C_INCLUDE_WHAT_YOU_USE "${iwyu_path_and_options}")
endif()

if(CAIRO_FOUND)
  target_compile_definitions(libbase PUBLIC HAVE_CAIRO)
  target_include_directories(libbase PUBLIC ${CAIRO_INCLUDE_DIRS})
  target_link_libraries(libbase PUBLIC PkgConfig::CAIRO)
endif()

# Add 'install' target, but only if we're the toplevel project.
if(CMAKE_PROJECT_NAME STREQUAL libbase)
  install(
    TARGETS libbase
    LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libbase")
endif()
