Austin Schuh | a20e8c9 | 2022-02-20 17:44:06 -0800 | [diff] [blame^] | 1 | # Copyright 2020 Google LLC |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | cmake_minimum_required(VERSION 3.14) |
| 16 | |
| 17 | project(osqp-cpp) |
| 18 | |
| 19 | set(CMAKE_CXX_STANDARD 17) |
| 20 | set(CMAKE_CXX_STANDARD_REQUIRED True) |
| 21 | |
| 22 | # Options. |
| 23 | option(OSQP-CPP_BUILD_TESTS |
| 24 | "Whether to build tests." ON |
| 25 | ) |
| 26 | |
| 27 | include(FetchContent) |
| 28 | |
| 29 | # ABSL |
| 30 | if (NOT TARGET absl::strings OR NOT TARGET absl::status OR NOT TARGET absl::span) |
| 31 | message(STATUS "osqp-cpp: `absl` targets not found. Attempting to fetch contents...") |
| 32 | FetchContent_Declare( |
| 33 | abseil-cpp |
| 34 | GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git |
| 35 | GIT_TAG origin/master |
| 36 | ) |
| 37 | FetchContent_MakeAvailable(abseil-cpp) |
| 38 | else() |
| 39 | message(STATUS "osqp-cpp: `absl` targets found.") |
| 40 | endif() |
| 41 | |
| 42 | # EIGEN |
| 43 | # Eigen is usually available as a system package, so we use find_package. |
| 44 | if (NOT TARGET Eigen3::Eigen) |
| 45 | message(STATUS "osqp-cpp: `Eigen3` targets not found. Attempting to find package...") |
| 46 | find_package(Eigen3 3.3.7 REQUIRED NO_MODULE) |
| 47 | else() |
| 48 | message(STATUS "osqp-cpp: `Eigen3` targets found.") |
| 49 | endif() |
| 50 | |
| 51 | # OSQP |
| 52 | if (NOT TARGET osqpstatic) |
| 53 | message(STATUS "osqp-cpp: `osqp` targets not found. Attempting to fetch contents...") |
| 54 | FetchContent_Declare( |
| 55 | osqp |
| 56 | GIT_REPOSITORY https://github.com/oxfordcontrol/osqp.git |
| 57 | GIT_TAG origin/master |
| 58 | ) |
| 59 | FetchContent_MakeAvailable(osqp) |
| 60 | else() |
| 61 | message(STATUS "osqp-cpp: `osqp` targets found.") |
| 62 | endif() |
| 63 | |
| 64 | # Googletest |
| 65 | if (OSQP-CPP_BUILD_TESTS) |
| 66 | enable_testing() |
| 67 | if (NOT TARGET gtest OR NOT TARGET gmock OR NOT TARGET gtest_main) |
| 68 | message(STATUS "osqp-cpp: `googletest` targets not found. Attempting to fetch contents...") |
| 69 | set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) |
| 70 | FetchContent_Declare( |
| 71 | googletest |
| 72 | GIT_REPOSITORY https://github.com/google/googletest.git |
| 73 | GIT_TAG origin/main |
| 74 | ) |
| 75 | FetchContent_MakeAvailable(googletest) |
| 76 | include(GoogleTest) |
| 77 | else() |
| 78 | message(STATUS "osqp-cpp: `googletest` targets found.") |
| 79 | endif() |
| 80 | endif() |
| 81 | |
| 82 | message(STATUS "osqp-cpp: Adding osqp-cpp library...") |
| 83 | add_library(osqp-cpp src/osqp++.cc) |
| 84 | target_link_libraries(osqp-cpp PUBLIC absl::strings absl::status absl::statusor Eigen3::Eigen PRIVATE osqpstatic ${CMAKE_DL_LIBS}) |
| 85 | target_include_directories(osqp-cpp PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>") |
| 86 | message(STATUS "osqp-cpp: Added osqp-cpp library.") |
| 87 | |
| 88 | # Build and register tests. |
| 89 | if (OSQP-CPP_BUILD_TESTS) |
| 90 | message(STATUS "osqp-cpp: Adding osqp-cpp tests...") |
| 91 | add_executable(osqp_test test/osqp++_test.cc) |
| 92 | target_link_libraries(osqp_test gtest gmock gtest_main absl::status absl::span osqp-cpp) |
| 93 | gtest_discover_tests(osqp_test) |
| 94 | message(STATUS "osqp-cpp: Added osqp-cpp tests.") |
| 95 | endif() |
| 96 | |