Skip to content

Commit 1f0f7d9

Browse files
billyzheli0xFireWolf
authored andcommitted
(conan-io#16469) osqp: add recipe
1 parent 9c237a9 commit 1f0f7d9

File tree

8 files changed

+233
-0
lines changed

8 files changed

+233
-0
lines changed

recipes/osqp/all/conandata.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sources:
2+
"0.6.2":
3+
url: "https://github.com/osqp/osqp/releases/download/v0.6.2/complete_sources.tar.gz"
4+
sha256: "0a7ade2fa19f13e13bc12f6ea0046ef764049023efb4997a4e72a76534f623ec"

recipes/osqp/all/conanfile.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from conan import ConanFile
2+
from conan.tools.files import get, copy, rm, rmdir
3+
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
4+
import os
5+
6+
required_conan_version = ">=1.53.0"
7+
8+
class OsqpConan(ConanFile):
9+
name = "osqp"
10+
package_type = "library"
11+
description = "The OSQP (Operator Splitting Quadratic Program) solver is a numerical optimization package."
12+
license = "Apache-2.0"
13+
url = "https://github.com/conan-io/conan-center-index"
14+
homepage = "https://osqp.org/"
15+
topics = ("machine-learning", "control", "optimization", "svm", "solver", "lasso", "portfolio-optimization",
16+
"numerical-optimization", "quadratic-programming", "convex-optimization", "model-predictive-control")
17+
settings = "os", "arch", "compiler", "build_type"
18+
options = {
19+
"shared": [True, False],
20+
"fPIC": [True, False],
21+
}
22+
default_options = {
23+
"shared": False,
24+
"fPIC": True,
25+
}
26+
27+
def config_options(self):
28+
if self.settings.os == "Windows":
29+
del self.options.fPIC
30+
31+
def configure(self):
32+
if self.options.shared:
33+
self.options.rm_safe("fPIC")
34+
self.settings.rm_safe("compiler.libcxx")
35+
self.settings.rm_safe("compiler.cppstd")
36+
37+
def layout(self):
38+
cmake_layout(self, src_folder="src")
39+
40+
def source(self):
41+
get(self, **self.conan_data["sources"][self.version], strip_root=True)
42+
43+
def generate(self):
44+
tc = CMakeToolchain(self)
45+
tc.variables['UNITTESTS'] = not self.conf.get("tools.build:skip_test", default=True, check_type=bool)
46+
tc.variables["PRINTING"] = True
47+
tc.variables["PROFILING"] = True
48+
tc.variables["CTRLC"] = True
49+
tc.variables["DFLOAT"] = False
50+
tc.variables["DLONG"] = True
51+
tc.variables["COVERAGE"] = False
52+
tc.variables["ENABLE_MKL_PARDISO"] = True
53+
tc.generate()
54+
55+
def build(self):
56+
cmake = CMake(self)
57+
cmake.configure()
58+
cmake.build()
59+
60+
def package(self):
61+
copy(self, pattern="LICENSE", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
62+
cmake = CMake(self)
63+
cmake.install()
64+
65+
if self.settings.os == "Windows":
66+
if self.options.shared:
67+
rm(self, "qdldl.dll", os.path.join(self.package_folder, "bin"))
68+
else:
69+
rmdir(self, os.path.join(self.package_folder, "bin"))
70+
else:
71+
if self.options.shared:
72+
rm(self, "*.a", os.path.join(self.package_folder, "lib"))
73+
else:
74+
rm(self, "*.so", os.path.join(self.package_folder, "lib"))
75+
rm(self, "*.dylib", os.path.join(self.package_folder, "lib"))
76+
77+
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))
78+
rmdir(self, os.path.join(self.package_folder, "include", "qdldl"))
79+
rm(self, "*qdldl.*", os.path.join(self.package_folder, "lib"))
80+
81+
def package_info(self):
82+
self.cpp_info.set_property("cmake_file_name", "osqp")
83+
self.cpp_info.set_property("cmake_target_name", "osqp::osqp")
84+
self.cpp_info.libs = ["osqp"]
85+
86+
if self.settings.os in ["Linux", "FreeBSD"]:
87+
self.cpp_info.system_libs.append("m")
88+
self.cpp_info.system_libs.append("rt")
89+
self.cpp_info.system_libs.append("dl")
90+
91+
# TODO: to remove in conan v2 once cmake_find_package_* generators removed
92+
self.cpp_info.filenames["cmake_find_package"] = "osqp"
93+
self.cpp_info.filenames["cmake_find_package_multi"] = "osqp"
94+
self.cpp_info.names["cmake_find_package"] = "osqp"
95+
self.cpp_info.names["cmake_find_package_multi"] = "osqp"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
3+
project(test_package CXX)
4+
5+
find_package(osqp REQUIRED CONFIG)
6+
7+
add_executable(${PROJECT_NAME} test_package.cpp)
8+
target_link_libraries(${PROJECT_NAME} PRIVATE osqp::osqp)
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from conan import ConanFile
2+
from conan.tools.build import can_run
3+
from conan.tools.cmake import cmake_layout, CMake
4+
import os
5+
6+
7+
# It will become the standard on Conan 2.x
8+
class TestPackageConan(ConanFile):
9+
settings = "os", "arch", "compiler", "build_type"
10+
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
11+
test_type = "explicit"
12+
13+
def requirements(self):
14+
self.requires(self.tested_reference_str)
15+
16+
def layout(self):
17+
cmake_layout(self)
18+
19+
def build(self):
20+
cmake = CMake(self)
21+
cmake.configure()
22+
cmake.build()
23+
24+
def test(self):
25+
if can_run(self):
26+
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
27+
self.run(bin_path, env="conanrun")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Below is a demo copied from an upstream example code,
3+
* It shows how to setup and solve an optimization problem.
4+
* Source code: https://github.com/osqp/osqp/blob/master/examples/osqp_demo.c
5+
* Problem definition: https://osqp.org/docs/examples/setup-and-solve.html
6+
*/
7+
#include "osqp/osqp.h"
8+
9+
10+
int main(void) {
11+
// Load problem data
12+
c_float P_x[3] = {4.0, 1.0, 2.0, };
13+
c_int P_nnz = 3;
14+
c_int P_i[3] = {0, 0, 1, };
15+
c_int P_p[3] = {0, 1, 3, };
16+
c_float q[2] = {1.0, 1.0, };
17+
c_float A_x[4] = {1.0, 1.0, 1.0, 1.0, };
18+
c_int A_nnz = 4;
19+
c_int A_i[4] = {0, 1, 0, 2, };
20+
c_int A_p[3] = {0, 2, 4, };
21+
c_float l[3] = {1.0, 0.0, 0.0, };
22+
c_float u[3] = {1.0, 0.7, 0.7, };
23+
c_int n = 2;
24+
c_int m = 3;
25+
26+
// Exitflag
27+
c_int exitflag = 0;
28+
29+
// Workspace structures
30+
OSQPWorkspace *work;
31+
OSQPSettings *settings = (OSQPSettings *)c_malloc(sizeof(OSQPSettings));
32+
OSQPData *data = (OSQPData *)c_malloc(sizeof(OSQPData));
33+
34+
// Populate data
35+
if (data) {
36+
data->n = n;
37+
data->m = m;
38+
data->P = csc_matrix(data->n, data->n, P_nnz, P_x, P_i, P_p);
39+
data->q = q;
40+
data->A = csc_matrix(data->m, data->n, A_nnz, A_x, A_i, A_p);
41+
data->l = l;
42+
data->u = u;
43+
}
44+
45+
// Define solver settings as default
46+
if (settings) {
47+
osqp_set_default_settings(settings);
48+
settings->alpha = 1.0; // Change alpha parameter
49+
}
50+
51+
// Setup workspace
52+
exitflag = osqp_setup(&work, data, settings);
53+
54+
// Solve Problem
55+
osqp_solve(work);
56+
57+
// Cleanup
58+
osqp_cleanup(work);
59+
if (data) {
60+
if (data->A) c_free(data->A);
61+
if (data->P) c_free(data->P);
62+
c_free(data);
63+
}
64+
if (settings) c_free(settings);
65+
66+
return exitflag;
67+
68+
return EXIT_SUCCESS;
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
project(test_package)
3+
4+
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
5+
conan_basic_setup(TARGETS)
6+
7+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package/
8+
${CMAKE_CURRENT_BINARY_DIR}/test_package/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from conans import ConanFile, CMake
2+
from conan.tools.build import cross_building
3+
import os
4+
5+
6+
# legacy validation with Conan 1.x
7+
class TestPackageV1Conan(ConanFile):
8+
settings = "os", "arch", "compiler", "build_type"
9+
generators = "cmake", "cmake_find_package_multi"
10+
11+
def build(self):
12+
cmake = CMake(self)
13+
cmake.configure()
14+
cmake.build()
15+
16+
def test(self):
17+
if not cross_building(self):
18+
bin_path = os.path.join("bin", "test_package")
19+
self.run(bin_path, run_environment=True)

recipes/osqp/config.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
versions:
2+
"0.6.2":
3+
folder: all

0 commit comments

Comments
 (0)