Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare ORTools for static compilation in Rust #428

Draft
wants to merge 7 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
common --enable_platform_specific_config
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We set different build flags per-host.
This is because OR-tools compilation requires C++20, and the flag to set this is different for windows MSVC compilers and non-MSVC compilers.

Note that this flag is another barrier to cross-compilation, if that ever comes back onto the table.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command basically is a note to bazel that it should should select platform-specific configurations according to the host platform


build --incompatible_strict_action_env
run --incompatible_strict_action_env
test --incompatible_strict_action_env
build:linux --cxxopt=-std=c++20
build:macos --cxxopt=-std=c++20
build:windows --cxxopt=-std:c++20

run --incompatible_strict_action_env --cxxopt=-std=c++20
test --incompatible_strict_action_env --cxxopt=-std=c++20
8 changes: 4 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ google_or_tools_mac()
google_or_tools_linux()
google_or_tools_windows()

load("//library/ortools/cc:deps.bzl", "google_or_tools_mac", "google_or_tools_linux", "google_or_tools_windows")
google_or_tools_mac()
google_or_tools_linux()
google_or_tools_windows()
load("//library/ortools/cc:deps.bzl", "google_or_tools")
google_or_tools()
load("@com_google_protobuf//:protobuf_deps.bzl", google_or_tools_protobuf_deps = "protobuf_deps")
google_or_tools_protobuf_deps()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or tools has its own protobuf dependencies


# Load //tool/common
load("//tool/common:deps.bzl", "vaticle_dependencies_ci_pip", vaticle_dependencies_tool_maven_artifacts = "maven_artifacts")
Expand Down
114 changes: 75 additions & 39 deletions library/ortools/cc/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,87 @@
#

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_repository")

def build_file_content(lib):
return """
cc_import(
name = "lib",
shared_library = "lib/{0}",
visibility = ["//visibility:public"]
)
cc_library(
name = "incl",
hdrs = glob([
"include/ortools/**/*.h",
"include/absl/**/*.h",
"include/absl/**/*.inc",
"include/google/protobuf/**/*.inc",
"include/google/protobuf/**/*.h",
]),
strip_include_prefix = "include/",
visibility = ["//visibility:public"]
)
""".format(lib)
def google_or_tools():
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or_tools and all of its dependencies. We get this from the template OR-tools bazel project: https://github.com/or-tools/bazel_or-tools

# Abseil-cpp
git_repository(
name = "com_google_absl",
tag = "20211102.0",
remote = "https://github.com/abseil/abseil-cpp.git",
)

def google_or_tools_linux():
http_archive(
name = "or_tools_linux",
urls = ["https://github.com/google/or-tools/releases/download/v9.0/or-tools_debian-10_v9.0.9048.tar.gz"],
strip_prefix = "or-tools_Debian-10-64bit_v9.0.9048/",
sha256 = "063fb1d8765ae23b0bb25b9c561e904532713416fe0458f7db45a0f72190eb50",
build_file_content = build_file_content("libortools.so")
# Protobuf
git_repository(
name = "com_google_protobuf",
tag = "v3.19.4",
remote = "https://github.com/protocolbuffers/protobuf.git",
)

# ZLIB
new_git_repository(
name = "zlib",
build_file = "@com_google_protobuf//:third_party/zlib.BUILD",
tag = "v1.2.11",
remote = "https://github.com/madler/zlib.git",
)

git_repository(
name = "com_google_re2",
patches = ["@com_google_ortools//bazel:re2.patch"],
tag = "2022-02-01",
remote = "https://github.com/google/re2.git",
)

git_repository(
name = "com_google_googletest",
tag = "release-1.11.0",
remote = "https://github.com/google/googletest.git",
)

def google_or_tools_mac():
http_archive(
name = "or_tools_mac",
urls = ["https://github.com/google/or-tools/releases/download/v9.0/or-tools_MacOsX-11.2.3_v9.0.9048.tar.gz"],
strip_prefix = "or-tools_MacOsX-11.2.3_v9.0.9048/",
sha256 = "adf73a00d4ec49558b67be5ce3cfc8f30268da2253b35feb11d0d40700550bf6",
build_file_content = build_file_content("libortools.dylib")
name = "glpk",
build_file = "//bazel:glpk.BUILD",
sha256 = "4a1013eebb50f728fc601bdd833b0b2870333c3b3e5a816eeba921d95bec6f15",
url = "http://ftp.gnu.org/gnu/glpk/glpk-5.0.tar.gz",
)

def google_or_tools_windows():
http_archive(
name = "or_tools_windows",
urls = ["https://github.com/google/or-tools/releases/download/v9.0/or-tools_VisualStudio2019-64bit_v9.0.9048.zip"],
strip_prefix = "or-tools_VisualStudio2019-64bit_v9.0.9048/",
sha256 = "1be7286e082ba346f8729a873c5fd85418ac2dc95b847d9baa5381c5ac5f5fd9",
build_file_content = build_file_content("ortools.lib")
name = "bliss",
build_file = "@com_google_ortools//bazel:bliss.BUILD",
patches = ["@com_google_ortools//bazel:bliss-0.73.patch"],
sha256 = "f57bf32804140cad58b1240b804e0dbd68f7e6bf67eba8e0c0fa3a62fd7f0f84",
url = "http://www.tcs.hut.fi/Software/bliss/bliss-0.73.zip",
)

new_git_repository(
name = "scip",
build_file = "@com_google_ortools//bazel:scip.BUILD",
patches = ["@com_google_ortools//bazel:scip.patch"],
patch_args = ["-p1"],
tag = "v800",
remote = "https://github.com/scipopt/scip.git",
)

# Eigen has no Bazel build.
new_git_repository(
name = "eigen",
tag = "3.4.0",
remote = "https://gitlab.com/libeigen/eigen.git",
build_file_content =
"""
cc_library(
name = 'eigen3',
srcs = [],
includes = ['.'],
hdrs = glob(['Eigen/**']),
visibility = ['//visibility:public'],
)
"""
)

git_repository(
name = "com_google_ortools",
remote = "https://github.com/google/or-tools.git",
tag = "v9.3",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use a tag instead of commit + shallow_since to reduce the size of the initial git clone

)
44 changes: 14 additions & 30 deletions library/ortools/rust/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,21 @@ rust_library(
rust_cxx_bridge(
name = "ortools_bridge",
src = "ortools.rs",
deps = select({
"@vaticle_dependencies//util/platform:is_mac": [
"@or_tools_mac//:lib",
"@or_tools_mac//:incl",
],
"@vaticle_dependencies//util/platform:is_linux": [
"@or_tools_linux//:lib",
"@or_tools_linux//:incl",
],
"@vaticle_dependencies//util/platform:is_windows": [
"@or_tools_windows//:lib",
"@or_tools_windows//:incl",
],
}) + [":ortools_bridge_wrapper"],
deps = [
":ortools-wrapper",
"@com_google_ortools//ortools/base",
"@com_google_ortools//ortools/linear_solver",
"@com_google_ortools//ortools/linear_solver:linear_solver_cc_proto",
]
)

cc_library(
name = "ortools_bridge_wrapper",
hdrs = ["OrToolsWrapper.h"],
deps = select({
"@vaticle_dependencies//util/platform:is_mac": [
"@or_tools_mac//:lib",
"@or_tools_mac//:incl",
],
"@vaticle_dependencies//util/platform:is_linux": [
"@or_tools_linux//:lib",
"@or_tools_linux//:incl",
],
"@vaticle_dependencies//util/platform:is_windows": [
"@or_tools_windows//:lib",
"@or_tools_windows//:incl",
],
})
name = "ortools-wrapper",
srcs = [":OrToolsWrapper.h"],
deps = [
"@com_google_ortools//ortools/base",
"@com_google_ortools//ortools/linear_solver",
"@com_google_ortools//ortools/linear_solver:linear_solver_cc_proto",
],
)

6 changes: 5 additions & 1 deletion library/ortools/rust/OrToolsWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ using namespace std;
namespace operations_research {
using MPSolverResultStatus = MPSolver::ResultStatus;

std::unique_ptr<MPSolver> new_mpsolver() {
std::unique_ptr<MPSolver> new_mpsolver_sat() {
return std::unique_ptr<MPSolver>(MPSolver::CreateSolver("SAT"));
Comment on lines +23 to +24
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expose the SAT interface explicitly

}

std::unique_ptr<MPSolver> new_mpsolver_scip() {
return std::unique_ptr<MPSolver>(MPSolver::CreateSolver("SCIP"));
}
}
4 changes: 3 additions & 1 deletion library/ortools/rust/ortools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ pub mod ffi {
type MPConstraint;
type MPObjective;

fn new_mpsolver() -> UniquePtr<MPSolver>;
fn new_mpsolver_sat() -> UniquePtr<MPSolver>;
fn new_mpsolver_scip() -> UniquePtr<MPSolver>;

fn MakeIntVar(self: Pin<&mut MPSolver>, lb: f64, ub: f64, name: &CxxString) -> *mut MPVariable;

fn solution_value(self: &MPVariable) -> f64;
Expand Down