diff --git a/CMakeLists.txt b/CMakeLists.txt index e20fb4d..82732f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,17 +55,23 @@ # endorsement purposes. # +# [[ +# Author(s): +# - Cameron Rutherford +#]] + cmake_minimum_required(VERSION 3.12) project(gridkit) -set(component_models - #generic_model -) +set(PACKAGE_NAME "GRIDKIT") +set(PACKAGE_STRING "GRIDKIT 0.0.6") +set(PACKAGE_TARNAME "gridkit") + +set(PACKAGE_VERSION_MAJOR "1") +set(PACKAGE_VERSION_MINOR "0") -set(solver_libs - # generic_solver -) +set(PACKAGE_VERSION "${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}") # Ipopt support is disabled by default option(GRIDKIT_ENABLE_IPOPT "Enable Ipopt support" ON) @@ -74,13 +80,32 @@ option(GRIDKIT_ENABLE_IPOPT "Enable Ipopt support" ON) option(GRIDKIT_ENABLE_SUNDIALS "Enable SUNDIALS support" ON) # Enable KLU -option(GRIDKIT_ENABLE_SUNDIALS_SPARSE "Enable SUNDIALS sparse linear solvers" OFF) - +option(GRIDKIT_ENABLE_SUNDIALS_SPARSE "Enable SUNDIALS sparse linear solvers" ON) set(CMAKE_MACOSX_RPATH 1) -set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/config) + +# https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling#always-full-rpath +# use, i.e. don't skip the full RPATH for the build tree +set(CMAKE_SKIP_BUILD_RPATH FALSE) + +# when building, don't use the install RPATH already +# (but later on when installing) +set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) + set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") -set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/config) + +# add the automatically determined parts of the RPATH +# which point to directories outside the build tree to the install RPATH +set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) + +# the RPATH to be used when installing, but only if it's not a system directory +list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir) +if("${isSystemDir}" STREQUAL "-1") + set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") +endif("${isSystemDir}" STREQUAL "-1") + # TODO: Probably beter to set a debug interface target set(CMAKE_CXX_FLAGS_DEBUG "-Wall -O0 -g") @@ -89,31 +114,61 @@ set(CMAKE_CXX_STANDARD 11) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) -option(BUILD_SHARED_LIBS:BOOL "Build shared libraries" ON) +option(GRIDKIT_BUILD_SHARED "Build shared libraries" ON) +option(GRIDKIT_BUILD_STATIC "Build static libraries" OFF) if(GRIDKIT_ENABLE_IPOPT) include(FindIpopt) - set(CMAKE_INSTALL_RPATH ${IPOPT_LIBRARY_DIR} ${CMAKE_INSTALL_RPATH}) endif() - -include(FindSUNDIALS) -set(CMAKE_INSTALL_RPATH ${SUNDIALS_LIBRARY_DIR} ${CMAKE_INSTALL_RPATH}) - +if(GRIDKIT_ENABLE_SUNDIALS) + find_package(SUNDIALS 5.5.0 REQUIRED CONFIG + PATHS ${SUNDIALS_DIR} + ${SUNDIALS_DIR}/lib/cmake/sundials) + message(STATUS "SUNDIALS configuration found: ${SUNDIALS_CONFIG}") +endif() if(GRIDKIT_ENABLE_SUNDIALS_SPARSE) include(FindSuiteSparse) - set(CMAKE_INSTALL_RPATH ${SUITESPARSE_LIBRARY_DIR} ${CMAKE_INSTALL_RPATH}) endif() - -include_directories(${SUITESPARSE_INCLUDE_DIR} ${SUNDIALS_INCLUDE_DIR} ${IPOPT_INCLUDE_DIR}) - # Set up configuration header file # configure_file(gridkit_config.h.in gridkit_config.h) +# Macro that adds libraries +include(GridkitAddLibrary) + add_subdirectory(ComponentLib) add_subdirectory(Solver) add_subdirectory(Examples) -# install(FILES gridkit_config.hpp DESTINATION include) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +export(EXPORT gridkit-targets FILE "${CMAKE_CURRENT_BINARY_DIR}/GridKitTargets.cmake") + +# Configuring exporting cmake config +install(EXPORT gridkit-targets + FILE GridKitTargets.cmake + NAMESPACE GRIDKIT:: + DESTINATION lib/cmake/gridkit) + +include(CMakePackageConfigHelpers) + +# Basic version file +write_basic_package_version_file( + GridKitConfigVersion.cmake + VERSION ${PACKAGE_VERSION} + COMPATIBILITY SameMajorVersion) + +# Generate config file that includes exports +configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/config/Config.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/GridKitConfig.cmake" + INSTALL_DESTINATION "lib/cmake/gridkit" + NO_SET_AND_CHECK_MACRO + NO_CHECK_REQUIRED_COMPONENTS_MACRO) + +# Install configuration file +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/GridKitConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/GridKitConfigVersion.cmake + DESTINATION lib/cmake/gridkit) # TESTING enable_testing() diff --git a/ComponentLib/Branch/CMakeLists.txt b/ComponentLib/Branch/CMakeLists.txt index ef558f2..bd4ff42 100644 --- a/ComponentLib/Branch/CMakeLists.txt +++ b/ComponentLib/Branch/CMakeLists.txt @@ -55,6 +55,14 @@ # endorsement purposes. # -add_library(Branch SHARED Branch.cpp) -install(TARGETS Branch LIBRARY DESTINATION lib) -# set(component_models ${component_models} Branch PARENT_SCOPE) +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +gridkit_add_library(branch + SOURCES + Branch.cpp + OUTPUT_NAME + gridkit_branch) + diff --git a/ComponentLib/Bus/CMakeLists.txt b/ComponentLib/Bus/CMakeLists.txt index f175797..c53ee67 100644 --- a/ComponentLib/Bus/CMakeLists.txt +++ b/ComponentLib/Bus/CMakeLists.txt @@ -55,6 +55,16 @@ # endorsement purposes. # -add_library(Bus SHARED BusSlack.cpp BusPQ.cpp BusPV.cpp) -install(TARGETS Bus LIBRARY DESTINATION lib) -# set(component_models ${component_models} Bus PARENT_SCOPE) +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +gridkit_add_library(bus + SOURCES + BusPQ.cpp + BusPV.cpp + BusSlack.cpp + OUTPUT_NAME + gridkit_bus) + diff --git a/ComponentLib/CMakeLists.txt b/ComponentLib/CMakeLists.txt index d7e33f4..67112e1 100644 --- a/ComponentLib/CMakeLists.txt +++ b/ComponentLib/CMakeLists.txt @@ -55,6 +55,11 @@ # endorsement purposes. # +# [[ +# Author(s): +# - Cameron Rutherford +#]] + add_subdirectory(Branch) add_subdirectory(Bus) add_subdirectory(Generator2) @@ -63,4 +68,3 @@ add_subdirectory(Generator4Governor) add_subdirectory(Generator4Param) add_subdirectory(Load) add_subdirectory(MiniGrid) -set(component_models ${component_models} PARENT_SCOPE) diff --git a/ComponentLib/Generator2/CMakeLists.txt b/ComponentLib/Generator2/CMakeLists.txt index 49b643a..a1fbc1d 100644 --- a/ComponentLib/Generator2/CMakeLists.txt +++ b/ComponentLib/Generator2/CMakeLists.txt @@ -55,6 +55,14 @@ # endorsement purposes. # -add_library(Generator2 SHARED Generator2.cpp) -install(TARGETS Generator2 LIBRARY DESTINATION lib) -# set(component_models ${component_models} Generator2 PARENT_SCOPE) +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +gridkit_add_library(generator2 + SOURCES + Generator2.cpp + OUTPUT_NAME + gridkit_generator2) + diff --git a/ComponentLib/Generator4/CMakeLists.txt b/ComponentLib/Generator4/CMakeLists.txt index b56bd64..527d4ab 100644 --- a/ComponentLib/Generator4/CMakeLists.txt +++ b/ComponentLib/Generator4/CMakeLists.txt @@ -55,6 +55,14 @@ # endorsement purposes. # -add_library(Generator4 SHARED Generator4.cpp) -install(TARGETS Generator4 LIBRARY DESTINATION lib) -# set(component_models ${component_models} Generator4 PARENT_SCOPE) +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +gridkit_add_library(generator4 + SOURCES + Generator4.cpp + OUTPUT_NAME + gridkit_generator4) + diff --git a/ComponentLib/Generator4Governor/CMakeLists.txt b/ComponentLib/Generator4Governor/CMakeLists.txt index 6fddb19..24e2d9d 100644 --- a/ComponentLib/Generator4Governor/CMakeLists.txt +++ b/ComponentLib/Generator4Governor/CMakeLists.txt @@ -55,6 +55,14 @@ # endorsement purposes. # -add_library(Generator4Governor SHARED Generator4Governor.cpp) -install(TARGETS Generator4Governor LIBRARY DESTINATION lib) -# set(component_models ${component_models} Generator4Governor PARENT_SCOPE) +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +gridkit_add_library(generator4governor + SOURCES + Generator4Governor.cpp + OUTPUT_NAME + gridkit_generator4governor) + diff --git a/ComponentLib/Generator4Param/CMakeLists.txt b/ComponentLib/Generator4Param/CMakeLists.txt index 2544c7f..457ebc4 100644 --- a/ComponentLib/Generator4Param/CMakeLists.txt +++ b/ComponentLib/Generator4Param/CMakeLists.txt @@ -55,6 +55,14 @@ # endorsement purposes. # -add_library(Generator4Param Generator4Param.cpp) -install(TARGETS Generator4Param LIBRARY DESTINATION lib) -# set(component_models ${component_models} Generator4Param PARENT_SCOPE) +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +gridkit_add_library(generator4param + SOURCES + Generator4Param.cpp + OUTPUT_NAME + gridkit_generator4param) + diff --git a/ComponentLib/Load/CMakeLists.txt b/ComponentLib/Load/CMakeLists.txt index 860d32a..46abf3a 100644 --- a/ComponentLib/Load/CMakeLists.txt +++ b/ComponentLib/Load/CMakeLists.txt @@ -55,6 +55,14 @@ # endorsement purposes. # -add_library(Load SHARED Load.cpp) -install(TARGETS Load LIBRARY DESTINATION lib) -# set(component_models ${component_models} Load PARENT_SCOPE) +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +gridkit_add_library(load + SOURCES + Load.cpp + OUTPUT_NAME + gridkit_load) + diff --git a/ComponentLib/MiniGrid/CMakeLists.txt b/ComponentLib/MiniGrid/CMakeLists.txt index 8505899..c88608b 100644 --- a/ComponentLib/MiniGrid/CMakeLists.txt +++ b/ComponentLib/MiniGrid/CMakeLists.txt @@ -55,6 +55,14 @@ # endorsement purposes. # -add_library(MiniGrid SHARED MiniGrid.cpp) -install(TARGETS MiniGrid LIBRARY DESTINATION lib) -# set(component_models ${component_models} MiniGrid PARENT_SCOPE) +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +gridkit_add_library(minigrid + SOURCES + MiniGrid.cpp + OUTPUT_NAME + gridkit_minigrid) + diff --git a/Examples/AdjointSensitivity/CMakeLists.txt b/Examples/AdjointSensitivity/CMakeLists.txt index 90f0c23..7663c1c 100644 --- a/Examples/AdjointSensitivity/CMakeLists.txt +++ b/Examples/AdjointSensitivity/CMakeLists.txt @@ -55,6 +55,12 @@ # endorsement purposes. # +# [[ +# Author(s): +# - Cameron Rutherford +#]] + add_executable(adjoint AdjointSensitivity.cpp) -target_link_libraries(adjoint Bus Generator4 ${solver_libs}) -install(TARGETS adjoint RUNTIME DESTINATION bin) +target_link_libraries(adjoint GRIDKIT::bus GRIDKIT::generator4 GRIDKIT::solvers_dyn) +install(TARGETS adjoint DESTINATION bin) + diff --git a/Examples/DynamicConstrainedOpt/CMakeLists.txt b/Examples/DynamicConstrainedOpt/CMakeLists.txt index 7a214e1..18b2894 100644 --- a/Examples/DynamicConstrainedOpt/CMakeLists.txt +++ b/Examples/DynamicConstrainedOpt/CMakeLists.txt @@ -55,6 +55,12 @@ # endorsement purposes. # +# [[ +# Author(s): +# - Cameron Rutherford +#]] + add_executable(dynconopt DynamicConstrainedOpt.cpp) -target_link_libraries(dynconopt Bus Generator4 Generator2 ${solver_libs}) -install(TARGETS dynconopt RUNTIME DESTINATION bin) +target_link_libraries(dynconopt GRIDKIT::generator4 GRIDKIT::generator2 GRIDKIT::bus GRIDKIT::solvers_dyn GRIDKIT::solvers_opt) +install(TARGETS dynconopt DESTINATION bin) + diff --git a/Examples/GenConstLoad/CMakeLists.txt b/Examples/GenConstLoad/CMakeLists.txt index 7ff607e..6d4aa8f 100644 --- a/Examples/GenConstLoad/CMakeLists.txt +++ b/Examples/GenConstLoad/CMakeLists.txt @@ -55,6 +55,11 @@ # endorsement purposes. # +# [[ +# Author(s): +# - Cameron Rutherford +#]] + add_executable(genconstload GenConstLoad.cpp) -target_link_libraries(genconstload Generator4Governor Bus Load ${solver_libs}) -install(TARGETS genconstload RUNTIME DESTINATION bin) +target_link_libraries(genconstload GRIDKIT::generator4governor GRIDKIT::bus GRIDKIT::load GRIDKIT::solvers_dyn GRIDKIT::solvers_opt) +install(TARGETS genconstload DESTINATION bin) diff --git a/Examples/GenInfiniteBus/CMakeLists.txt b/Examples/GenInfiniteBus/CMakeLists.txt index 12accac..d825810 100644 --- a/Examples/GenInfiniteBus/CMakeLists.txt +++ b/Examples/GenInfiniteBus/CMakeLists.txt @@ -55,6 +55,12 @@ # endorsement purposes. # +# [[ +# Author(s): +# - Cameron Rutherford +#]] + add_executable(geninfbus GenInfiniteBus.cpp) -target_link_libraries(geninfbus Bus Generator4 ${solver_libs}) -install(TARGETS geninfbus RUNTIME DESTINATION bin) +target_link_libraries(geninfbus GRIDKIT::bus GRIDKIT::generator4 GRIDKIT::solvers_opt GRIDKIT::solvers_dyn) +install(TARGETS geninfbus DESTINATION bin) + diff --git a/Examples/Grid3Bus/CMakeLists.txt b/Examples/Grid3Bus/CMakeLists.txt index 46296e2..3b2d6ff 100644 --- a/Examples/Grid3Bus/CMakeLists.txt +++ b/Examples/Grid3Bus/CMakeLists.txt @@ -55,10 +55,15 @@ # endorsement purposes. # +# [[ +# Author(s): +# - Cameron Rutherford +#]] + # add_executable(grid3bus Grid3Bus.cpp) -# target_link_libraries(grid3bus MiniGrid ${solver_libs}) +# target_link_libraries(grid3bus GRIDKIT::minigrid GRIDKIT::solvers_steady) # install(TARGETS grid3bus RUNTIME DESTINATION bin) add_executable(grid3bus Grid3BusSys.cpp) -target_link_libraries(grid3bus MiniGrid Bus Branch Load ${solver_libs}) +target_link_libraries(grid3bus GRIDKIT::minigrid GRIDKIT::bus GRIDKIT::branch GRIDKIT::load GRIDKIT::solvers_steady) install(TARGETS grid3bus RUNTIME DESTINATION bin) diff --git a/Examples/ParameterEstimation/CMakeLists.txt b/Examples/ParameterEstimation/CMakeLists.txt index c40a5a2..44ae520 100644 --- a/Examples/ParameterEstimation/CMakeLists.txt +++ b/Examples/ParameterEstimation/CMakeLists.txt @@ -55,7 +55,12 @@ # endorsement purposes. # +# [[ +# Author(s): +# - Cameron Rutherford +#]] + add_executable(paramest ParameterEstimation.cpp) -target_link_libraries(paramest Bus Generator4Param ${solver_libs}) +target_link_libraries(paramest GRIDKIT::bus GRIDKIT::generator4param GRIDKIT::solvers_opt GRIDKIT::solvers_dyn) install(TARGETS paramest RUNTIME DESTINATION bin) install(FILES lookup_table.dat DESTINATION bin) diff --git a/README.md b/README.md index 31dc1aa..7750954 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Before installing GridKit™ make sure you have all needed dependencies. ### Dependencies You should have all of the following installed before installing GridKit™ - A version of - - [SUNDIALS](https://github.com/LLNL/sundials) >= 4.x + - [SUNDIALS](https://github.com/LLNL/sundials) >= 5.5 - [Suitesparse](https://github.com/DrTimothyAldenDavis/SuiteSparse) >= 5.x (optional) - [Ipopt](https://github.com/coin-or/Ipopt) >= 3.x (optional) - [CMake](https://cmake.org/) >= 3.12 diff --git a/Solver/CMakeLists.txt b/Solver/CMakeLists.txt index c9e30cc..eb602ce 100644 --- a/Solver/CMakeLists.txt +++ b/Solver/CMakeLists.txt @@ -55,10 +55,13 @@ # endorsement purposes. # +# [[ +# Author(s): +# - Cameron Rutherford +#]] + add_subdirectory(Dynamic) add_subdirectory(SteadyState) if(GRIDKIT_ENABLE_IPOPT) add_subdirectory(Optimization) endif() -set(solver_libs ${solver_libs} PARENT_SCOPE) - diff --git a/Solver/Dynamic/CMakeLists.txt b/Solver/Dynamic/CMakeLists.txt index c75e7ec..9e45c2e 100644 --- a/Solver/Dynamic/CMakeLists.txt +++ b/Solver/Dynamic/CMakeLists.txt @@ -55,8 +55,19 @@ # endorsement purposes. # -include_directories(${SUITESPARSE_INCLUDE_DIR} ${SUNDIALS_INCLUDE_DIR}) -add_library(Ida SHARED Ida.cpp) -target_link_libraries(Ida ${SUITESPARSE_LIBRARY} ${SUNDIALS_LIBRARY}) -set(solver_libs ${solver_libs} Ida PARENT_SCOPE) -install(TARGETS Ida LIBRARY DESTINATION lib) +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +gridkit_add_library(solvers_dyn + SOURCES + Ida.cpp + LINK_LIBRARIES + PUBLIC SUNDIALS::nvecserial + PUBLIC SUNDIALS::idas + PUBLIC SUNDIALS::kinsol + PUBLIC IPOPT + OUTPUT_NAME + gridkit_solvers_dyn) + diff --git a/Solver/Optimization/CMakeLists.txt b/Solver/Optimization/CMakeLists.txt index 9e6b3fe..7be4514 100644 --- a/Solver/Optimization/CMakeLists.txt +++ b/Solver/Optimization/CMakeLists.txt @@ -55,8 +55,20 @@ # endorsement purposes. # -include_directories(${IPOPT_INCLUDE_DIR}) -add_library(DynamicOpt SHARED DynamicObjective.cpp DynamicConstraint.cpp) -target_link_libraries(DynamicOpt Ida ${IPOPT_LIBRARY} ${LAPACK_LIBRARIES}) -set(solver_libs ${solver_libs} DynamicOpt PARENT_SCOPE) -install(TARGETS DynamicOpt LIBRARY DESTINATION lib) +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +gridkit_add_library(solvers_opt + SOURCES + DynamicObjective.cpp + DynamicConstraint.cpp + LINK_LIBRARIES + PUBLIC SUNDIALS::nvecserial + PUBLIC SUNDIALS::idas + PUBLIC IPOPT + PUBLIC GRIDKIT::solvers_dyn + OUTPUT_NAME + gridkit_solvers_opt) + diff --git a/Solver/SteadyState/CMakeLists.txt b/Solver/SteadyState/CMakeLists.txt index d216262..3d71c5a 100644 --- a/Solver/SteadyState/CMakeLists.txt +++ b/Solver/SteadyState/CMakeLists.txt @@ -55,8 +55,16 @@ # endorsement purposes. # -include_directories(${SUITESPARSE_INCLUDE_DIR} ${SUNDIALS_INCLUDE_DIR}) -add_library(Kinsol SHARED Kinsol.cpp) -target_link_libraries(Kinsol ${SUITESPARSE_LIBRARY} ${SUNDIALS_LIBRARY}) -set(solver_libs ${solver_libs} Kinsol PARENT_SCOPE) -install(TARGETS Kinsol LIBRARY DESTINATION lib) +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +gridkit_add_library(solvers_steady + SOURCES + Kinsol.cpp + LINK_LIBRARIES + PUBLIC SUNDIALS::kinsol + OUTPUT_NAME + gridkit_solvers_steady) + diff --git a/config/Config.cmake.in b/config/Config.cmake.in new file mode 100644 index 0000000..5905f3f --- /dev/null +++ b/config/Config.cmake.in @@ -0,0 +1,8 @@ +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +@PACKAGE_INIT@ + +include ( "${CMAKE_CURRENT_LIST_DIR}/GridKitTargets.cmake" ) diff --git a/config/FindIpopt.cmake b/config/FindIpopt.cmake index 3ba2f00..f7515c5 100644 --- a/config/FindIpopt.cmake +++ b/config/FindIpopt.cmake @@ -62,13 +62,16 @@ Finds Ipopt include directory and libraries and exports target `Ipopt` User may set: - IPOPT_ROOT_DIR +Author(s): +- Cameron Rutherford + ]] find_library(IPOPT_LIBRARY NAMES ipopt PATHS - ${IPOPT_DIR} $ENV{IPOPT_DIR} ${IPOPT_ROOT_DIR} + ${IPOPT_DIR} $ENV{IPOPT_DIR} ${IPOPT_ROOT_DIR} ${IPOPT_LIBRARY_DIR} ENV LD_LIBRARY_PATH ENV DYLD_LIBRARY_PATH PATH_SUFFIXES lib64 lib) @@ -76,8 +79,8 @@ find_library(IPOPT_LIBRARY if(IPOPT_LIBRARY) set(IPOPT_LIBRARY CACHE FILEPATH "Path to Ipopt library") message(STATUS "Found Ipopt library: " ${IPOPT_LIBRARY}) - get_filename_component(IPOPT_LIBRARY_DIR ${IPOPT_LIBRARY} DIRECTORY CACHE) - set(IPOPT_LIBRARY_DIR CACHE PATH "Path to Ipopt library") + get_filename_component(IPOPT_LIBRARY_DIR ${IPOPT_LIBRARY} DIRECTORY CACHE "Ipopt library directory") + mark_as_advanced(IPOPT_LIBRARY IPOPT_LIBRARY_DIR) if(NOT IPOPT_DIR) get_filename_component(IPOPT_DIR ${IPOPT_LIBRARY_DIR} DIRECTORY CACHE) endif() @@ -94,18 +97,19 @@ find_path(IPOPT_INCLUDE_DIR include/coin-or include/coinor) -if(IPOPT_LIBRARY AND IPOPT_INCLUDE_DIR) - set(IPOPT_INCLUDE_DIR CACHE PATH "Path to Ipopt header files") - message(STATUS "Found Ipopt include directory: " ${IPOPT_INCLUDE_DIR}) - add_library(Ipopt INTERFACE) - target_link_libraries(Ipopt INTERFACE ${IPOPT_LIBRARY}) - target_include_directories(Ipopt INTERFACE ${IPOPT_INCLUDE_DIR}) +if(IPOPT_LIBRARY) + message(STATUS "Found Ipopt include: ${IPOPT_INCLUDE_DIR}") + mark_as_advanced(IPOPT_INCLUDE_DIR) + add_library(IPOPT INTERFACE IMPORTED) + target_link_libraries(IPOPT INTERFACE ${IPOPT_LIBRARY}) + target_include_directories(IPOPT INTERFACE ${IPOPT_INCLUDE_DIR}) else() if(NOT IPOPT_ROOT_DIR) message(STATUS "Ipopt dir not found! Please provide correct filepath.") - set(IPOPT_DIR CACHE PATH "Path to Ipopt installation root.") + set(IPOPT_DIR ${IPOPT_DIR} CACHE PATH "Path to Ipopt installation root.") unset(IPOPT_INCLUDE_DIR CACHE) unset(IPOPT_LIBRARY CACHE) + unset(IPOPT_LIBRARY_DIR CACHE) elseif(NOT IPOPT_LIB) message(STATUS "Ipopt library not found! Please provide correct filepath.") endif() @@ -113,4 +117,3 @@ else() message(STATUS "Ipopt include directory not found! Please provide correct path.") endif() endif() - diff --git a/config/FindSUNDIALS.cmake b/config/FindSUNDIALS.cmake deleted file mode 100644 index 95f26f6..0000000 --- a/config/FindSUNDIALS.cmake +++ /dev/null @@ -1,134 +0,0 @@ -# -# Copyright (c) 2017, Lawrence Livermore National Security, LLC. -# Produced at the Lawrence Livermore National Laboratory. -# Written by Slaven Peles . -# LLNL-CODE-718378. -# All rights reserved. -# -# This file is part of GridKit™. For details, see github.com/LLNL/GridKit -# Please also read the LICENSE file. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# - Redistributions of source code must retain the above copyright notice, -# this list of conditions and the disclaimer below. -# - Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the disclaimer (as noted below) in the -# documentation and/or other materials provided with the distribution. -# - Neither the name of the LLNS/LLNL nor the names of its contributors may -# be used to endorse or promote products derived from this software without -# specific prior written permission. -# -# 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 LAWRENCE LIVERMORE NATIONAL -# SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY 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) ARISINGIN ANY -# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF -# THE POSSIBILITY OF SUCH DAMAGE. -# -# Lawrence Livermore National Laboratory is operated by Lawrence Livermore -# National Security, LLC, for the U.S. Department of Energy, National -# Nuclear Security Administration under Contract DE-AC52-07NA27344. -# -# This document was prepared as an account of work sponsored by an agency -# of the United States government. Neither the United States government nor -# Lawrence Livermore National Security, LLC, nor any of their employees -# makes any warranty, expressed or implied, or assumes any legal liability -# or responsibility for the accuracy, completeness, or usefulness of any -# information, apparatus, product, or process disclosed, or represents that -# its use would not infringe privately owned rights. Reference herein to -# any specific commercial product, process, or service by trade name, -# trademark, manufacturer, or otherwise does not necessarily constitute or -# imply its endorsement, recommendation, or favoring by the United States -# government or Lawrence Livermore National Security, LLC. The views and -# opinions of authors expressed herein do not necessarily state or reflect -# those of the United States government or Lawrence Livermore National -# Security, LLC, and shall not be used for advertising or product -# endorsement purposes. -# - -#[[ - -Finds Sundials include directory and libraries and exports target `SUNDIALS` - -User may set: -- SUNDIALS_ROOT_DIR - -]] - -# SUNDIALS modules needed for the build -# The order matters in case of static build! -set(SUNDIALS_MODULES - sundials_idas - sundials_kinsol - sundials_nvecserial -) - -find_library(SUNDIALS_LIBRARY - NAMES - ${SUNDIALS_MODULES} - PATHS - ${SUNDIALS_ROOT_DIR} ${SUNDIALS_DIR} $ENV{SUNDIALS_DIR} - ENV LD_LIBRARY_PATH ENV DYLD_LIBRARY_PATH - PATH_SUFFIXES - lib) - -if(SUNDIALS_LIBRARY) - get_filename_component(SUNDIALS_LIBRARY_DIR ${SUNDIALS_LIBRARY} DIRECTORY CACHE) - set(SUNDIALS_LIBRARY_DIR CACHE PATH "Path to Sundials library") - if(NOT SUNDIALS_DIR) - get_filename_component(SUNDIALS_DIR ${SUNDIALS_LIBRARY_DIR} DIRECTORY CACHE) - endif() -endif() - -# Find SUNDIALS header path and ensure all needed files are there -find_path(SUNDIALS_INCLUDE_DIR - NAMES - nvector/nvector_serial.h - sundials/sundials_dense.h - sundials/sundials_sparse.h - sundials/sundials_types.h - idas/idas.h - idas/idas_ls.h - PATHS - ${SUNDIALS_ROOT_DIR} ${SUNDIALS_DIR} - PATH_SUFFIXES - include -) - -if(SUNDIALS_LIBRARY AND SUNDIALS_INCLUDE_DIR) -set(SUNDIALS_INCLUDE_DIR CACHE PATH "Path to Sundials Include dir") - # Find each SUNDIALS module and add it to the list of libraries to link - unset(SUNDIALS_LIBRARY CACHE) - foreach(mod ${SUNDIALS_MODULES}) - find_library(SUNDIALS_${mod} - NAMES ${mod} - HINTS ${SUNDIALS_LIBRARY_DIR} - ) - if(SUNDIALS_${mod}) - set(SUNDIALS_LIBRARY ${SUNDIALS_LIBRARY} ${SUNDIALS_${mod}}) - else() - # unset ${SUNDIALS_LIBRARY_DIR} and ask user to supply it - endif() - endforeach() - message (STATUS "Found Sundials libraries ${SUNDIALS_LIBRARY}") -else() - if(NOT SUNDIALS_ROOT_DIR) - message(STATUS "Sundials dir not found! Please provide correct filepath.") - set(SUNDIALS_DIR CACHE PATH "Path to SUNDIALS installation root.") - unset(SUNDIALS_LIBRARY CACHE) - unset(SUNDIALS_INCLUDE_DIR CACHE) - elseif(NOT SUNDIALS_LIBRARY) - message(STATUS "Sundials library not found! Please provide correct filepath.") - elseif(NOT SUNDIALS_INCLUDE_DIR) - message(STATUS "Sundials include dir not found! Please provide correct filepath.") - endif() -endif() diff --git a/config/FindSuiteSparse.cmake b/config/FindSuiteSparse.cmake index 8f67b55..2d73feb 100644 --- a/config/FindSuiteSparse.cmake +++ b/config/FindSuiteSparse.cmake @@ -62,19 +62,30 @@ Finds Sutiesparse include directory and libraries and exports target `Suitespars User may set: - SUITESPARSE_ROOT_DIR +Author(s): +- Cameron Rutherford + ]] +set(SUITESPARSE_MODULES + amd + colamd + klu) find_library(SUITESPARSE_LIBRARY NAMES suitesparseconfig + ${SUITESPARSE_MODULES} PATHS ${SUITESPARSE_DIR} $ENV{SUITESPARSE_DIR} ${SUITESPARSE_ROOT_DIR} ENV LD_LIBRARY_PATH ENV DYLD_LIBRARY_PATH PATH_SUFFIXES lib64 lib) + if(SUITESPARSE_LIBRARY) - get_filename_component(SUITESPARSE_LIBRARY_DIR ${SUITESPARSE_LIBRARY} DIRECTORY CACHE) - set(SUITESPARSE_LIBRARY_DIR CACHE PATH "Path to Suitesparse library") + set(SUITESPARSE_LIBRARY CACHE FILEPATH "Path to Suitesparse library") + get_filename_component(SUITESPARSE_LIBRARY_DIR ${SUITESPARSE_LIBRARY} DIRECTORY CACHE "Suitesparse library directory") + message(STATUS "Found Suitesparse libraries in: " ${SUITESPARSE_LIBRARY_DIR}) + mark_as_advanced(SUITESPARSE_LIBRARY SUITESPARSE_LIBRARY_DIR) if(NOT SUITESPARSE_DIR) get_filename_component(SUITESPARSE_DIR ${SUITESPARSE_LIBRARY_DIR} DIRECTORY CACHE) endif() @@ -87,36 +98,35 @@ find_path(SUITESPARSE_INCLUDE_DIR colamd.h klu.h PATHS - ${SUITESPARSE_DIR} ${SUITESPARSE_ROOT_DIR} $ENV{SUITESPARSE_DIR} ${SUITESPARSE_LIBRARY_DIR}/.. + ${SUITESPARSE_DIR} $ENV{SUITESPARSE_DIR} ${SUITESPARSE_ROOT_DIR} ${SUITESPARSE_LIBRARY_DIR}/.. PATH_SUFFIXES include) -if(SUITESPARSE_LIBRARY AND SUITESPARSE_INCLUDE_DIR) - set(SUITESPARSE_INCLUDE_DIR CACHE PATH "Path to Suitesparse header files") - # SUITESPARSE modules needed for the build - # The order matters in case of static build! - set(SUITESPARSE_MODULES - amd - colamd - klu) - unset(SUITESPARSE_LIBRARY CACHE) +if(SUITESPARSE_LIBRARY) + message(STATUS "Found Suitesparse include: ${SUITESPARSE_INCLUDE_DIR}") + mark_as_advanced(SUITESPARSE_INCLUDE_DIR) + unset(SUITESPARSE_LIBRARY) + add_library(SUITESPARSE INTERFACE IMPORTED) + target_include_directories(SUITESPARSE INTERFACE ${SUITESPARSE_INCLUDE_DIR}) foreach(mod ${SUITESPARSE_MODULES}) - find_library(SUITESPARSE_${mod} + find_library(suitesparse_${mod} NAMES ${mod} HINTS ${SUITESPARSE_LIBRARY_DIR}) - if(SUITESPARSE_${mod}) - set(SUITESPARSE_LIBRARY ${SUITESPARSE_LIBRARY} ${SUITESPARSE_${mod}}) + if(suitesparse_${mod}) + message(STATUS "Found suitesparse internal library " ${mod}) + target_link_libraries(SUITESPARSE INTERFACE ${suitesparse_${mod}}) + mark_as_advanced(suitesparse_${mod}) else() - # unset ${SUITESPARSE_LIBRARY_DIR} and ask user to supply it + message(SEND_ERROR "Suitesparse internal library " ${mod} " not found") endif() - endforeach() - message (STATUS "Found SUITESPARSE libraries in ${SUITESPARSE_LIBRARY_DIR}") + endforeach(mod) else() if(NOT SUITESPARSE_ROOT_DIR) message(STATUS "Suitesparse dir not found! Please provide correct filepath.") - set(SUITESPARSE_DIR CACHE PATH "Path to Suitesparse installation root.") + set(SUITESPARSE_DIR ${SUITESPARSE_DIR} CACHE PATH "Path to Suitesparse installation root.") unset(SUITESPARSE_LIBRARY CACHE) unset(SUITESPARSE_INCLUDE_DIR CACHE) + unset(SUITESPARSE_LIBRARY_DIR CACHE) elseif(NOT SUITESPARSE_LIBRARY) message(STATUS "Suitesparse library not found! Please provide correct filepath.") endif() @@ -124,4 +134,3 @@ else() message(STATUS "Suitesparse include dir not found! Please provide correct filepath.") endif() endif() - diff --git a/config/GridkitAddLibrary.cmake b/config/GridkitAddLibrary.cmake new file mode 100644 index 0000000..e0feb5b --- /dev/null +++ b/config/GridkitAddLibrary.cmake @@ -0,0 +1,94 @@ + +# [[ +# Author(s): +# - Cameron Rutherford +#]] + +# add_library macro loosely based on https://github.com/LLNL/sundials/blob/master/cmake/macros/SundialsAddLibrary.cmake + +macro(gridkit_add_library target) + + set(options STATIC_ONLY SHARED_ONLY) + set(oneValueArgs OUTPUT_NAME) + set(multiValueArgs SOURCES LINK_LIBRARIES INCLUDE_DIRECTORIES) + + # parse arguments + cmake_parse_arguments(gridkit_add_library + "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + # library types to create + set(_libtypes "") + if(GRIDKIT_BUILD_STATIC AND (NOT gridkit_add_library_SHARED_ONLY)) + set(_libtypes "STATIC") + endif() + if(GRIDKIT_BUILD_SHARED AND (NOT gridkit_add_library_STATIC_ONLY)) + set(_libtypes "${_libtypes};SHARED") + endif() + + # build libraries + foreach(_libtype ${_libtypes}) + # library suffix + if(${_libtype} MATCHES "STATIC") + set(_lib_suffix "_static") + else() + set(_lib_suffix "_shared") + endif() + + # source files + set(sources ${gridkit_add_library_SOURCES}) + + # obj target needs a unique name + set(obj_target ${target}_obj${_lib_suffix}) + + # -- Create object library -- + + add_library(${obj_target} OBJECT ${sources}) + + if(gridkit_add_library_LINK_LIBRARIES) + if(${_lib_type} MATCHES "STATIC") + append_static_suffix(gridkit_add_library_LINK_LIBRARIES _all_libs) + else() + set(_all_libs ${gridkit_add_library_LINK_LIBRARIES}) + endif() + target_link_libraries(${obj_target} ${_all_libs}) + endif() + + # object files going into shared libs need PIC code + set_target_properties(${obj_target} PROPERTIES POSITION_INDEPENDENT_CODE TRUE) + + # set target name + set(_actual_target_name ${target}${_lib_suffix}) + + add_library(${_actual_target_name} ${_libtype} $) + + if(gridkit_add_library_LINK_LIBRARIES) + target_link_libraries(${_actual_target_name} ${gridkit_add_library_LINK_LIBRARIES}) + endif() + + add_library(GRIDKIT::${target} ALIAS ${_actual_target_name}) + + if(gridkit_add_library_OUTPUT_NAME) + set_target_properties(${_actual_target_name} PROPERTIES + OUTPUT_NAME ${gridkit_add_library_OUTPUT_NAME} + CLEAN_DIRECT_OUTPUT 1) + else() + set_target_properties(${_actual_target_name} PROPERTIES + OUTPUT_NAME ${target} + CLEAN_DIRECT_OUTPUT 1) + endif() + install(TARGETS ${_actual_target_name} DESTINATION lib EXPORT gridkit-targets) + endforeach() +endmacro() + + +macro(append_static_suffix libs_in libs_out) + set(${libs_out} "") + set(_STATIC_LIB_SUFFIX "_static") + foreach(_lib ${${libs_in}}) + if(TARGET ${_lib}${_STATIC_LIB_SUFFIX}) + list(APPEND ${libs_out} ${_lib}${_STATIC_LIB_SUFFIX}) + else() + list(APPEND ${libs_out} ${_lib}) + endif() + endforeach() +endmacro()