#
# Copyright (c) The acados authors.
#
# This file is part of acados.
#
# The 2-Clause BSD License
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.;
#

{%- if solver_options.qp_solver %}
    {%- set qp_solver = solver_options.qp_solver %}
{%- else %}
    {%- set qp_solver = "FULL_CONDENSING_HPIPM" %}
{%- endif %}

{%- if solver_options.hessian_approx %}
    {%- set hessian_approx = solver_options.hessian_approx %}
{%- elif solver_options.sens_hess %}
    {%- set hessian_approx = "EXACT" %}
{%- else %}
    {%- set hessian_approx = "GAUSS_NEWTON" %}
{%- endif %}

{%- if solver_options.model_external_shared_lib_dir %}
    {%- set model_external_shared_lib_dir = solver_options.model_external_shared_lib_dir %}
{%- endif %}

{%- if solver_options.model_external_shared_lib_name %}
    {%- set model_external_shared_lib_name = solver_options.model_external_shared_lib_name %}
{%- endif %}

{#- control operator #}
{%- if os and os == "pc" %}
    {%- set control = "&" %}
{%- else %}
    {%- set control = ";" %}
{%- endif %}

{%- if acados_link_libs and os and os == "pc" %}{# acados linking libraries and flags #}
    {%- set link_libs = acados_link_libs.qpoases ~ " " ~ acados_link_libs.hpmpc ~ " " ~ acados_link_libs.osqp ~ " " ~ acados_link_libs.daqp -%}
    {%- set openmp_flag = acados_link_libs.openmp %}
{%- else %}
    {%- set openmp_flag = " " %}
    {%- if qp_solver == "FULL_CONDENSING_QPOASES" %}
        {%- set link_libs = "-lqpOASES_e" %}
    {%- elif qp_solver == "FULL_CONDENSING_DAQP" %}
        {%- set link_libs = "-ldaqp" %}
    {%- else %}
        {%- set link_libs = "" %}
    {%- endif %}
{%- endif %}

cmake_minimum_required(VERSION 3.13)

project({{ model.name }})

# build options.
{% if problem_class != "SIM" %}
option(BUILD_ACADOS_SOLVER_LIB "Should the solver library acados_solver_{{ model.name }} be build?" OFF)
option(BUILD_ACADOS_OCP_SOLVER_LIB "Should the OCP solver library acados_ocp_solver_{{ model.name }} be build?" OFF)
option(BUILD_EXAMPLE "Should the example main_{{ model.name }} be build?" OFF)
{%- endif %}


{%- if solver_options.integrator_type != "DISCRETE" %}
option(BUILD_SIM_EXAMPLE "Should the simulation example main_sim_{{ model.name }} be build?" OFF)
option(BUILD_ACADOS_SIM_SOLVER_LIB "Should the simulation solver library acados_sim_solver_{{ model.name }} be build?" OFF)
{%- endif %}


if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_SYSTEM_NAME MATCHES "Windows")
    # MinGW: remove prefix and change suffix to match MSVC
    # (such that Matlab mex recognizes the libraries)
    set(CMAKE_SHARED_LIBRARY_PREFIX "")
    set(CMAKE_IMPORT_LIBRARY_SUFFIX ".lib")
    set(CMAKE_IMPORT_LIBRARY_PREFIX "")
    set(CMAKE_STATIC_LIBRARY_SUFFIX ".lib")
    set(CMAKE_STATIC_LIBRARY_PREFIX "")
endif()


if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE {{ code_export_directory | replace(from="\", to="/") }})
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE {{ code_export_directory | replace(from="\", to="/") }})
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE {{ code_export_directory | replace(from="\", to="/") }})
endif()


# object target names
set(MODEL_OBJ model_{{ model.name }})
set(OCP_OBJ ocp_{{ model.name }})
{%- if solver_options.integrator_type != "DISCRETE" %}
set(SIM_OBJ sim_{{ model.name }})
{%- endif %}

# model
set(MODEL_SRC
{%- for filename in external_function_files_model %}
    {{ filename }}
{%- endfor %}
)
add_library(${MODEL_OBJ} OBJECT ${MODEL_SRC} )


{% if problem_class != "SIM" %}
# optimal control problem - mostly CasADi exports
if(${BUILD_ACADOS_SOLVER_LIB} OR ${BUILD_ACADOS_OCP_SOLVER_LIB} OR ${BUILD_EXAMPLE})
    set(OCP_SRC
{% for filename in external_function_files_ocp %}
        {{ filename }}
{%- endfor %}
{%- if solver_options.custom_update_filename %}
    {%- if solver_options.custom_update_filename != "" %}
        {{ solver_options.custom_update_filename }}
    {%- endif %}
{%- endif %}
        acados_solver_{{ model.name }}.c
    )
    add_library(${OCP_OBJ} OBJECT ${OCP_SRC})
endif()
{%- endif %}

{%- if solver_options.integrator_type != "DISCRETE" %}
# for sim solver
if(${BUILD_ACADOS_SOLVER_LIB} OR ${BUILD_EXAMPLE}
    {%- if solver_options.integrator_type != "DISCRETE" %}
    OR ${BUILD_SIM_EXAMPLE} OR ${BUILD_ACADOS_SIM_SOLVER_LIB}
    {%- endif -%}
    )
    set(SIM_SRC acados_sim_solver_{{ model.name }}.c)
    add_library(${SIM_OBJ} OBJECT ${SIM_SRC})
endif()
{%- endif %}


  {%- if problem_class != "SIM" %}
# for target example
set(EX_SRC main_{{ model.name }}.c)
set(EX_EXE main_{{ model.name }})
  {%- endif %}

{%- if model_external_shared_lib_dir and model_external_shared_lib_name %}
set(EXTERNAL_DIR {{ model_external_shared_lib_dir | replace(from="\", to="/")  }})
set(EXTERNAL_LIB {{ model_external_shared_lib_name }})
{%- else %}
set(EXTERNAL_DIR)
set(EXTERNAL_LIB)
{%- endif %}

# set some search paths for preprocessor and linker
set(ACADOS_INCLUDE_PATH {{ acados_include_path | replace(from="\", to="/")  }} CACHE PATH "Define the path which contains the include directory for acados.")
set(ACADOS_LIB_PATH {{ acados_lib_path | replace(from="\", to="/") }} CACHE PATH "Define the path which contains the lib directory for acados.")

# c-compiler flags for debugging
set(CMAKE_C_FLAGS_DEBUG "-O0 -ggdb")

set(CMAKE_C_FLAGS "-fPIC -std=c99 {{ openmp_flag }} {{ solver_options.ext_fun_compile_flags | replace(from="\", to="/") }} {{ " " }}
{%- if qp_solver == "FULL_CONDENSING_QPOASES" -%}
    -DACADOS_WITH_QPOASES
{%- endif -%}
{%- if qp_solver == "FULL_CONDENSING_DAQP" -%}
    -DACADOS_WITH_DAQP
{%- endif -%}
{%- if qp_solver == "PARTIAL_CONDENSING_OSQP" -%}
    -DACADOS_WITH_OSQP
{%- endif -%}
{%- if qp_solver == "PARTIAL_CONDENSING_QPDUNES" -%}
    -DACADOS_WITH_QPDUNES
{%- endif -%}
{%- if solver_options.num_threads_in_batch_solve > 1 -%}
    -fopenmp
{%- endif -%}
")
#-fno-diagnostics-show-line-numbers -g

include_directories(
   ${ACADOS_INCLUDE_PATH}
   ${ACADOS_INCLUDE_PATH}/acados
   ${ACADOS_INCLUDE_PATH}/blasfeo/include
   ${ACADOS_INCLUDE_PATH}/hpipm/include
{%- if qp_solver == "FULL_CONDENSING_QPOASES" %}
   ${ACADOS_INCLUDE_PATH}/qpOASES_e/
{%- endif %}
{%- if qp_solver == "FULL_CONDENSING_DAQP" %}
   ${ACADOS_INCLUDE_PATH}/daqp/include
{%- endif %}
)

# linker flags
link_directories(${ACADOS_LIB_PATH})

# link to libraries
if(UNIX)
    link_libraries(acados hpipm blasfeo m {{ link_libs }})
else()
    link_libraries(acados hpipm blasfeo {{ link_libs }})
endif()

# the targets

  {%- if problem_class != "SIM" %}
# bundled_shared_lib
if(${BUILD_ACADOS_SOLVER_LIB})
    set(LIB_ACADOS_SOLVER acados_solver_{{ model.name }})
    add_library(${LIB_ACADOS_SOLVER} SHARED $<TARGET_OBJECTS:${MODEL_OBJ}> $<TARGET_OBJECTS:${OCP_OBJ}>
    {%- if solver_options.integrator_type != "DISCRETE" %}
        $<TARGET_OBJECTS:${SIM_OBJ}>
    {%- endif -%}
    )
    install(TARGETS ${LIB_ACADOS_SOLVER} DESTINATION ${CMAKE_INSTALL_PREFIX})
endif(${BUILD_ACADOS_SOLVER_LIB})

# ocp_shared_lib
if(${BUILD_ACADOS_OCP_SOLVER_LIB})
    set(LIB_ACADOS_OCP_SOLVER acados_ocp_solver_{{ model.name }})
    add_library(${LIB_ACADOS_OCP_SOLVER} SHARED $<TARGET_OBJECTS:${MODEL_OBJ}> $<TARGET_OBJECTS:${OCP_OBJ}>)
    # Specify libraries or flags to use when linking a given target and/or its dependents.
    target_link_libraries(${LIB_ACADOS_OCP_SOLVER} PRIVATE ${EXTERNAL_LIB})
    target_link_directories(${LIB_ACADOS_OCP_SOLVER} PRIVATE ${EXTERNAL_DIR})
    install(TARGETS ${LIB_ACADOS_OCP_SOLVER} DESTINATION ${CMAKE_INSTALL_PREFIX})
endif(${BUILD_ACADOS_OCP_SOLVER_LIB})

# example
if(${BUILD_EXAMPLE})
    add_executable(${EX_EXE} ${EX_SRC} $<TARGET_OBJECTS:${MODEL_OBJ}> $<TARGET_OBJECTS:${OCP_OBJ}>
    {%- if solver_options.integrator_type != "DISCRETE" %}
        $<TARGET_OBJECTS:${SIM_OBJ}>
    {%- endif -%}
    )
    install(TARGETS ${EX_EXE} DESTINATION ${CMAKE_INSTALL_PREFIX})
endif(${BUILD_EXAMPLE})
  {%- endif %}

{% if solver_options.integrator_type != "DISCRETE" -%}
# example_sim
if(${BUILD_SIM_EXAMPLE})
    set(EX_SIM_SRC main_sim_{{ model.name }}.c)
    set(EX_SIM_EXE main_sim_{{ model.name }})
    add_executable(${EX_SIM_EXE} ${EX_SIM_SRC} $<TARGET_OBJECTS:${MODEL_OBJ}> $<TARGET_OBJECTS:${SIM_OBJ}>)
    install(TARGETS ${EX_SIM_EXE} DESTINATION ${CMAKE_INSTALL_PREFIX})
endif(${BUILD_SIM_EXAMPLE})

# sim_shared_lib
if(${BUILD_ACADOS_SIM_SOLVER_LIB})
    set(LIB_ACADOS_SIM_SOLVER acados_sim_solver_{{ model.name }})
    add_library(${LIB_ACADOS_SIM_SOLVER} SHARED $<TARGET_OBJECTS:${MODEL_OBJ}> $<TARGET_OBJECTS:${SIM_OBJ}>)
    install(TARGETS ${LIB_ACADOS_SIM_SOLVER} DESTINATION ${CMAKE_INSTALL_PREFIX})
endif(${BUILD_ACADOS_SIM_SOLVER_LIB})
{%- endif %}

# unset options for clean cmake cache on subsequent cmake runs
  {%- if problem_class != "SIM" %}
unset(BUILD_ACADOS_SOLVER_LIB CACHE)
unset(BUILD_ACADOS_OCP_SOLVER_LIB CACHE)
unset(BUILD_EXAMPLE CACHE)
  {%- endif %}
{%- if solver_options.integrator_type != "DISCRETE" %}
unset(BUILD_SIM_EXAMPLE CACHE)
unset(BUILD_ACADOS_SIM_SOLVER_LIB CACHE)
{%- endif %}
