# bbowdapp - C++ binding for bbowda
# Copyright (C) 2025 DAGOPT Optimization Technologies GmbH (www.dagopt.com)
#                    written by Kevin Kofler <kofler@dagopt.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. A copy of the GNU General Public
# License version 3 can be found in the file gpl-3.0.txt.
#
# Linking bbowda statically or dynamically (directly or indirectly) with
# other modules is making a combined work based on bbowda. Thus, the terms
# and conditions of the GNU General Public License cover the whole
# combination.
#
# In addition, as a special exception, the copyright holder of bbowda gives
# you permission to combine the bbowda program:
# * with free software programs or libraries that are released under the
#   GNU Library or Lesser General Public License (LGPL), either version 2
#   of the License, or (at your option) any later version,
# * with free software programs or libraries that are released under the
#   IBM Common Public License (CPL), either version 1.0 of the License, or
#   (at your option) any later version,
# * with free software programs or libraries that are released under the
#   eclipse.org Eclipse Public License (EPL), either version 1.0 of the
#   License, or (at your option) any later version,
# * with free software programs or libraries that are released under the
#   CeCILL-C Free Software License Agreement, either version 1 of the License,
#   or (at your option) any later version,
# * with code included in the standard release of MUMPS under the old MUMPS
#   Conditions of Use as reproduced in licenses.txt (or modified versions
#   of such code, with unchanged license; variants of the license where only
#   the list of contributors and/or the list of suggested citations changed
#   shall be considered the same license) and
# * if you qualify for a free of charge license of DONLP2, with code
#   included in the standard release of DONLP2 under the DONLP2 Conditions
#   of Use as reproduced in licenses.txt (or modified versions of such code,
#   with unchanged license).
# (For avoidance of doubt, this implies that it is permitted, e.g., to combine
# the bbowda program with current versions of Ipopt released under the EPL
# version 2.0, because 2.0 is >= 1.0. Its dependency MUMPS is released under
# the CeCILL-C version 1, which is also listed above.)
#
# You may copy and distribute such a system following the terms of the GNU
# GPL for bbowda and the licenses of the other code concerned, provided that
# you include the source code of that other code when and as the GNU GPL
# requires distribution of source code.
#
# Note that people who make modified versions of bbowda are not obligated
# to grant this special exception for their modified versions; it is their
# choice whether to do so. The GNU General Public License gives permission
# to release a modified version without this exception; this exception also
# makes it possible to release a modified version which carries forward
# this exception.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

cmake_minimum_required(VERSION 3.12)

project(bbowdapp LANGUAGES CXX)

include(GenerateExportHeader)
include(GNUInstallDirs)

option(BBOWDAPP_SHARED "Build libbbowdapp as a shared library." ON)
option(WITH_PYTHON "Build the SWIG Python 3 binding for bbowdapp." ON)
option(WITH_JAVA "Build the SWIG Java binding for bbowdapp." ON)

set(WITH_SWIG "(WITH_PYTHON OR WITH_JAVA)")
set(CMAKE_CXX_FLAGS "-fomit-frame-pointer -O3 -ftree-vectorize -Wall -Wextra")
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)

if(NOT TARGET bbowda)
  message(FATAL_ERROR "bbowdapp cannot be built standalone, only as a part of "
          "bbowda. Please point CMake to the parent bbowda directory, not the "
          "bbowda/bbowdapp subdirectory.")
endif()

include_directories("${CMAKE_CURRENT_BINARY_DIR}")

set(SOURCES bbowdapp.cc)

if(BBOWDAPP_SHARED)
  set(BBOWDAPP_LIB_TYPE SHARED)
else()
  set(BBOWDAPP_LIB_TYPE STATIC)
endif()
message(STATUS "Building a ${BBOWDAPP_LIB_TYPE} libbbowdapp")
add_library(bbowdapp ${BBOWDAPP_LIB_TYPE} ${SOURCES})
generate_export_header(bbowdapp)
# force PIC so that the static library can be linked into shared libraries
set_target_properties(bbowdapp PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(bbowdapp bbowda)

install(TARGETS bbowdapp
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

add_executable(bbowdapp_example example.cc)
target_link_libraries(bbowdapp_example bbowdapp)

install(TARGETS bbowdapp_example RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

if(WITH_SWIG)
  add_subdirectory(swig)
endif()
