Make osqp build

Prep for using it in the catapult.

Change-Id: Ic642b1c04469563de13a8a6227407aacb75eff6a
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/third_party/osqp-cpp/BUILD b/third_party/osqp-cpp/BUILD
new file mode 100644
index 0000000..e419d2b
--- /dev/null
+++ b/third_party/osqp-cpp/BUILD
@@ -0,0 +1,32 @@
+cc_library(
+    name = "osqp-cpp",
+    srcs = [
+        "src/osqp++.cc",
+    ],
+    hdrs = [
+        "include/osqp++.h",
+    ],
+    includes = [
+        "include",
+    ],
+    target_compatible_with = ["@platforms//os:linux"],
+    visibility = ["//visibility:public"],
+    deps = [
+        "//third_party/osqp",
+        "@com_google_absl//absl/status",
+        "@com_google_absl//absl/status:statusor",
+        "@org_tuxfamily_eigen//:eigen",
+    ],
+)
+
+cc_test(
+    name = "osqp-cpp_test",
+    srcs = [
+        "test/osqp++_test.cc",
+    ],
+    deps = [
+        ":osqp-cpp",
+        "//aos/testing:googletest",
+        "@com_google_googletest//:gtest",
+    ],
+)
diff --git a/third_party/osqp-cpp/include/osqp++.h b/third_party/osqp-cpp/include/osqp++.h
index 3f79fa9..86c9ea7 100644
--- a/third_party/osqp-cpp/include/osqp++.h
+++ b/third_party/osqp-cpp/include/osqp++.h
@@ -30,7 +30,7 @@
 
 // Must match the typedef in osqp/include/glob_opts.h (if not, it will trigger
 // a static_assert failure in osqp++.cc).
-using c_int = long long;  // NOLINT
+using c_int = int;  // NOLINT
 
 // A memory-safe mirror of the OSQPData struct defined in osqp/include/types.h.
 // The number of variables and constraints is implied by the shape of
diff --git a/third_party/osqp-cpp/src/osqp++.cc b/third_party/osqp-cpp/src/osqp++.cc
index 5fa119e..596339c 100644
--- a/third_party/osqp-cpp/src/osqp++.cc
+++ b/third_party/osqp-cpp/src/osqp++.cc
@@ -36,7 +36,7 @@
 static_assert(std::is_same_v<c_float, double>,
               "OSQP's c_float typedef is unexpectedly not the same as double");
 
-static_assert(sizeof(OSQPSettings) == 176,
+static_assert(sizeof(OSQPSettings) == 144,
               "The size of OSQPSettings has changed unexpectedly. Make sure "
               "that the map between ::OSQPSettings and osqp::OsqpSettings "
               "remains up to date.");
@@ -303,7 +303,7 @@
     }
   }
 
-  for (size_t i = 0; i < new_matrix.nonZeros(); ++i) {
+  for (Eigen::Index i = 0; i < new_matrix.nonZeros(); ++i) {
     if (ref_matrix->i[i] != new_matrix.innerIndexPtr()[i]) {
       return absl::InvalidArgumentError(
           "Sparsity of the new matrix differs from the previously "
@@ -541,6 +541,7 @@
 Map<const VectorXd> OsqpSolver::primal_infeasibility_certificate() const {
   OSQP_CHECK(IsInitialized());
   const OsqpExitCode exit_code = StatusToExitCode(workspace_->info->status_val);
+  (void)exit_code;
   OSQP_CHECK(exit_code == OsqpExitCode::kPrimalInfeasible ||
              exit_code == OsqpExitCode::kPrimalInfeasibleInaccurate);
   return Map<const VectorXd>(workspace_->delta_y, workspace_->data->m);
diff --git a/third_party/osqp/BUILD b/third_party/osqp/BUILD
new file mode 100644
index 0000000..8276eff
--- /dev/null
+++ b/third_party/osqp/BUILD
@@ -0,0 +1,146 @@
+genrule(
+    name = "make_osqp_configure",
+    outs = ["include/osqp_configure.h"],
+    cmd = "echo > $@",
+)
+
+genrule(
+    name = "make_qdldl_version",
+    outs = ["lin_sys/direct/qdldl/qdldl_sources/include/qdldl_version.h"],
+    cmd = "echo > $@",
+)
+
+genrule(
+    name = "make_qdldl_types",
+    outs = ["lin_sys/direct/qdldl/qdldl_sources/include/qdldl_types.h"],
+    cmd = """cat << EOF > $@
+#ifndef QDLDL_TYPES_H
+# define QDLDL_TYPES_H
+
+# ifdef __cplusplus
+extern "C" {
+# endif /* ifdef __cplusplus */
+
+#include <limits.h> //for the QDLDL_INT_TYPE_MAX
+
+// QDLDL integer and float types
+
+typedef int    QDLDL_int;   /* for indices */
+typedef double  QDLDL_float; /* for numerical values  */
+typedef unsigned char   QDLDL_bool;  /* for boolean values  */
+
+//Maximum value of the signed type QDLDL_int.
+#define QDLDL_INT_MAX LLONG_MAX
+
+# ifdef __cplusplus
+}
+# endif /* ifdef __cplusplus */
+
+#endif /* ifndef QDLDL_TYPES_H */
+EOF""",
+)
+
+cc_library(
+    name = "osqp_includes",
+    hdrs = [
+        "include/auxil.h",
+        "include/constants.h",
+        "include/cs.h",
+        "include/ctrlc.h",
+        "include/glob_opts.h",
+        "include/kkt.h",
+        "include/lin_alg.h",
+        "include/lin_sys.h",
+        "include/osqp.h",
+        "include/osqp_configure.h",
+        "include/osqp_error.h",
+        "include/polish.h",
+        "include/proj.h",
+        "include/scaling.h",
+        "include/types.h",
+        "include/util.h",
+        "include/version.h",
+        "lin_sys/lib_handler.h",
+    ],
+    defines = [
+        "IS_LINUX",
+        #"PRINTING",
+        "PROFILING",
+        "CTRLC",
+        # TODO(austin): Use floats instead
+        #"DFLOAT",
+        #"DLONG",
+        "ENABLE_MKL_PARDISO",
+    ],
+    includes = [
+        "include",
+        "lin_sys",
+    ],
+)
+
+cc_library(
+    name = "osqp",
+    srcs = [
+        "lin_sys/direct/pardiso/pardiso_interface.c",
+        "lin_sys/direct/pardiso/pardiso_loader.c",
+        "lin_sys/direct/qdldl/amd/src/SuiteSparse_config.c",
+        "lin_sys/direct/qdldl/amd/src/amd_1.c",
+        "lin_sys/direct/qdldl/amd/src/amd_2.c",
+        "lin_sys/direct/qdldl/amd/src/amd_aat.c",
+        "lin_sys/direct/qdldl/amd/src/amd_control.c",
+        "lin_sys/direct/qdldl/amd/src/amd_defaults.c",
+        "lin_sys/direct/qdldl/amd/src/amd_info.c",
+        "lin_sys/direct/qdldl/amd/src/amd_order.c",
+        "lin_sys/direct/qdldl/amd/src/amd_post_tree.c",
+        "lin_sys/direct/qdldl/amd/src/amd_postorder.c",
+        "lin_sys/direct/qdldl/amd/src/amd_preprocess.c",
+        "lin_sys/direct/qdldl/amd/src/amd_valid.c",
+        "lin_sys/direct/qdldl/qdldl_interface.c",
+        "lin_sys/direct/qdldl/qdldl_sources/src/qdldl.c",
+        "lin_sys/lib_handler.c",
+        "src/auxil.c",
+        "src/cs.c",
+        "src/ctrlc.c",
+        "src/error.c",
+        "src/kkt.c",
+        "src/lin_alg.c",
+        "src/lin_sys.c",
+        "src/osqp.c",
+        "src/polish.c",
+        "src/proj.c",
+        "src/scaling.c",
+        "src/util.c",
+    ],
+    hdrs = [
+        "lin_sys/direct/pardiso/pardiso_interface.h",
+        "lin_sys/direct/pardiso/pardiso_loader.h",
+        "lin_sys/direct/qdldl/amd/include/SuiteSparse_config.h",
+        "lin_sys/direct/qdldl/amd/include/amd.h",
+        "lin_sys/direct/qdldl/amd/include/amd_internal.h",
+        "lin_sys/direct/qdldl/qdldl_interface.h",
+        "lin_sys/direct/qdldl/qdldl_sources/include/qdldl.h",
+        "lin_sys/direct/qdldl/qdldl_sources/include/qdldl_types.h",
+        "lin_sys/direct/qdldl/qdldl_sources/include/qdldl_version.h",
+    ],
+    copts = [
+        "-Wno-cast-qual",
+    ],
+    defines = [
+        'QDLDL_VERSION_MAJOR="0"',
+        'QDLDL_VERSION_MINOR="1"',
+        'QDLDL_VERSION_PATCH="6"',
+        'QDLDL_VERSION="0.1.6"',
+    ],
+    includes = [
+        "lin_sys/direct/pardiso",
+        "lin_sys/direct/qdldl",
+        "lin_sys/direct/qdldl/amd/include",
+        "lin_sys/direct/qdldl/qdldl_sources/include",
+    ],
+    linkopts = ["-ldl"],
+    target_compatible_with = ["@platforms//os:linux"],
+    visibility = ["//visibility:public"],
+    deps = [
+        ":osqp_includes",
+    ],
+)
diff --git a/third_party/osqp/include/CMakeLists.txt b/third_party/osqp/include/CMakeLists.txt
index a543f70..51e4e17 100644
--- a/third_party/osqp/include/CMakeLists.txt
+++ b/third_party/osqp/include/CMakeLists.txt
@@ -4,7 +4,7 @@
     "${CMAKE_CURRENT_SOURCE_DIR}/version.h"
     "${CMAKE_CURRENT_SOURCE_DIR}/auxil.h"
     "${CMAKE_CURRENT_SOURCE_DIR}/constants.h"
-    "${CMAKE_CURRENT_SOURCE_DIR}/error.h"
+    "${CMAKE_CURRENT_SOURCE_DIR}/osqp_error.h"
     "${CMAKE_CURRENT_SOURCE_DIR}/glob_opts.h"
     "${CMAKE_CURRENT_SOURCE_DIR}/lin_alg.h"
     "${CMAKE_CURRENT_SOURCE_DIR}/osqp.h"
diff --git a/third_party/osqp/include/error.h b/third_party/osqp/include/osqp_error.h
similarity index 100%
rename from third_party/osqp/include/error.h
rename to third_party/osqp/include/osqp_error.h
diff --git a/third_party/osqp/src/error.c b/third_party/osqp/src/error.c
index e0c069e..767bb17 100644
--- a/third_party/osqp/src/error.c
+++ b/third_party/osqp/src/error.c
@@ -1,4 +1,4 @@
-#include "error.h"
+#include "osqp_error.h"
 
 const char *OSQP_ERROR_MESSAGE[] = {
   "Problem data validation.",
@@ -15,6 +15,8 @@
 		 const char * function_name) {
 # ifdef PRINTING
   c_print("ERROR in %s: %s\n", function_name, OSQP_ERROR_MESSAGE[error_code-1]);
+# else
+  (void)function_name;
 # endif
   return (c_int)error_code;
 }
diff --git a/third_party/osqp/src/osqp.c b/third_party/osqp/src/osqp.c
index 20cb1af..94819b3 100644
--- a/third_party/osqp/src/osqp.c
+++ b/third_party/osqp/src/osqp.c
@@ -3,7 +3,7 @@
 #include "util.h"
 #include "scaling.h"
 #include "glob_opts.h"
-#include "error.h"
+#include "osqp_error.h"
 
 
 #ifndef EMBEDDED
diff --git a/third_party/osqp/src/polish.c b/third_party/osqp/src/polish.c
index 55df72d..1d23788 100644
--- a/third_party/osqp/src/polish.c
+++ b/third_party/osqp/src/polish.c
@@ -5,7 +5,7 @@
 #include "lin_sys.h"
 #include "kkt.h"
 #include "proj.h"
-#include "error.h"
+#include "osqp_error.h"
 
 /**
  * Form reduced matrix A that contains only rows that are active at the