blob: 16af8a804646ffdf6f9c351d160bb7646fc392e3 [file] [log] [blame]
Austin Schuha20e8c92022-02-20 17:44:06 -08001# 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
15cmake_minimum_required(VERSION 3.14)
16
17project(osqp-cpp)
18
19set(CMAKE_CXX_STANDARD 17)
20set(CMAKE_CXX_STANDARD_REQUIRED True)
21
22# Options.
23option(OSQP-CPP_BUILD_TESTS
24 "Whether to build tests." ON
25)
26
27include(FetchContent)
28
29# ABSL
30if (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)
38else()
39 message(STATUS "osqp-cpp: `absl` targets found.")
40endif()
41
42# EIGEN
43# Eigen is usually available as a system package, so we use find_package.
44if (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)
47else()
48 message(STATUS "osqp-cpp: `Eigen3` targets found.")
49endif()
50
51# OSQP
52if (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)
60else()
61 message(STATUS "osqp-cpp: `osqp` targets found.")
62endif()
63
64# Googletest
65if (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()
80endif()
81
82message(STATUS "osqp-cpp: Adding osqp-cpp library...")
83add_library(osqp-cpp src/osqp++.cc)
84target_link_libraries(osqp-cpp PUBLIC absl::strings absl::status absl::statusor Eigen3::Eigen PRIVATE osqpstatic ${CMAKE_DL_LIBS})
85target_include_directories(osqp-cpp PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>")
86message(STATUS "osqp-cpp: Added osqp-cpp library.")
87
88# Build and register tests.
89if (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.")
95endif()
96