Skip to content

Commit 821ce39

Browse files
committed
move proto custom wrapper generator to util/python
1 parent cc64770 commit 821ce39

15 files changed

+352
-57
lines changed

ortools/routing/python/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ cc_binary(
3939
"//ortools/routing:heuristic_parameters_cc_proto",
4040
"//ortools/routing:ils_cc_proto",
4141
"//ortools/routing:parameters_cc_proto",
42-
"//ortools/sat/python:wrappers",
42+
"//ortools/util/python:wrappers",
4343
"@abseil-cpp//absl/flags:parse",
4444
"@abseil-cpp//absl/flags:usage",
4545
"@abseil-cpp//absl/log:die_if_null",

ortools/routing/python/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ target_link_libraries(routing_gen_proto_builder_pybind11 PRIVATE
2525
protobuf::libprotobuf
2626
${PROJECT_NAMESPACE}::ortools_proto
2727
${PROJECT_NAMESPACE}::routing_proto
28-
${PROJECT_NAMESPACE}::sat_python_wrappers)
28+
${PROJECT_NAMESPACE}::util_python_wrappers)
2929

3030
include(GNUInstallDirs)
3131
if(APPLE)

ortools/routing/python/gen_proto_builder_pybind11.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "ortools/routing/heuristic_parameters.pb.h"
2323
#include "ortools/routing/ils.pb.h"
2424
#include "ortools/routing/parameters.pb.h"
25-
#include "ortools/sat/python/wrappers.h"
25+
#include "ortools/util/python/wrappers.h"
2626

2727
namespace operations_research::routing::python {
2828

@@ -35,7 +35,7 @@ void ParseAndGenerate() {
3535
%s
3636
#endif // defined(IMPORT_PROTO_WRAPPER_CODE)
3737
)",
38-
sat::python::GeneratePybindCode(
38+
operations_research::util::python::GeneratePybindCode(
3939
{ABSL_DIE_IF_NULL(RoutingModelParameters::descriptor()),
4040
ABSL_DIE_IF_NULL(RoutingSearchParameters::descriptor()),
4141
ABSL_DIE_IF_NULL(FirstSolutionStrategy::descriptor()),

ortools/sat/python/BUILD.bazel

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,14 @@ cc_library(
4444
],
4545
)
4646

47-
cc_library(
48-
name = "wrappers",
49-
srcs = ["wrappers.cc"],
50-
hdrs = ["wrappers.h"],
51-
visibility = ["//visibility:public"],
52-
deps = [
53-
"@abseil-cpp//absl/algorithm:container",
54-
"@abseil-cpp//absl/base:nullability",
55-
"@abseil-cpp//absl/container:flat_hash_map",
56-
"@abseil-cpp//absl/container:flat_hash_set",
57-
"@abseil-cpp//absl/log",
58-
"@abseil-cpp//absl/log:check",
59-
"@abseil-cpp//absl/log:die_if_null",
60-
"@abseil-cpp//absl/strings",
61-
"@abseil-cpp//absl/types:span",
62-
"@protobuf",
63-
],
64-
)
65-
6647
cc_binary(
6748
name = "gen_proto_builder_pybind11",
6849
srcs = ["gen_proto_builder_pybind11.cc"],
6950
deps = [
70-
":wrappers",
7151
"//ortools/base",
7252
"//ortools/sat:cp_model_cc_proto",
7353
"//ortools/sat:sat_parameters_cc_proto",
54+
"//ortools/util/python:wrappers",
7455
"@abseil-cpp//absl/flags:parse",
7556
"@abseil-cpp//absl/flags:usage",
7657
"@abseil-cpp//absl/log:die_if_null",

ortools/sat/python/CMakeLists.txt

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,6 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14-
set(WRAPPERS_NAME sat_python_wrappers)
15-
16-
add_library(${WRAPPERS_NAME} OBJECT wrappers.h wrappers.cc)
17-
set_target_properties(${WRAPPERS_NAME} PROPERTIES
18-
POSITION_INDEPENDENT_CODE ON)
19-
target_include_directories(${WRAPPERS_NAME} PUBLIC
20-
${PROJECT_SOURCE_DIR}
21-
${PROJECT_BINARY_DIR})
22-
target_link_libraries(${WRAPPERS_NAME} PUBLIC
23-
absl::memory
24-
absl::synchronization
25-
absl::str_format
26-
protobuf::libprotobuf)
27-
add_library(${PROJECT_NAMESPACE}::${WRAPPERS_NAME} ALIAS ${WRAPPERS_NAME})
28-
2914
# gen_proto_builder_pybind11 code generator.
3015
add_executable(gen_proto_builder_pybind11)
3116
target_sources(gen_proto_builder_pybind11 PRIVATE "gen_proto_builder_pybind11.cc")
@@ -39,7 +24,7 @@ target_link_libraries(gen_proto_builder_pybind11 PRIVATE
3924
absl::str_format
4025
protobuf::libprotobuf
4126
${PROJECT_NAMESPACE}::ortools_proto
42-
${PROJECT_NAMESPACE}::${WRAPPERS_NAME})
27+
${PROJECT_NAMESPACE}::util_python_wrappers)
4328

4429
include(GNUInstallDirs)
4530
if(APPLE)

ortools/sat/python/gen_proto_builder_pybind11.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#include "absl/log/initialize.h"
1818
#include "absl/strings/str_format.h"
1919
#include "ortools/sat/cp_model.pb.h"
20-
#include "ortools/sat/python/wrappers.h"
2120
#include "ortools/sat/sat_parameters.pb.h"
21+
#include "ortools/util/python/wrappers.h"
2222

2323
namespace operations_research::sat::python {
2424

@@ -31,9 +31,10 @@ void ParseAndGenerate() {
3131
%s
3232
#endif // defined(IMPORT_PROTO_WRAPPER_CODE)
3333
)",
34-
GeneratePybindCode({ABSL_DIE_IF_NULL(CpModelProto::descriptor()),
35-
ABSL_DIE_IF_NULL(CpSolverResponse::descriptor()),
36-
ABSL_DIE_IF_NULL(SatParameters::descriptor())}));
34+
operations_research::util::python::GeneratePybindCode(
35+
{ABSL_DIE_IF_NULL(CpModelProto::descriptor()),
36+
ABSL_DIE_IF_NULL(CpSolverResponse::descriptor()),
37+
ABSL_DIE_IF_NULL(SatParameters::descriptor())}));
3738
}
3839

3940
} // namespace operations_research::sat::python

ortools/util/python/BUILD.bazel

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
load("@pip_deps//:requirements.bzl", "requirement")
1818
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
19+
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
1920
load("@rules_cc//cc:cc_library.bzl", "cc_library")
2021
load("@rules_python//python:py_library.bzl", "py_library")
2122
load("@rules_python//python:py_test.bzl", "py_test")
@@ -133,3 +134,71 @@ py_test(
133134
requirement("absl-py"),
134135
],
135136
)
137+
138+
cc_library(
139+
name = "wrappers",
140+
srcs = ["wrappers.cc"],
141+
hdrs = ["wrappers.h"],
142+
visibility = ["//visibility:public"],
143+
deps = [
144+
"@abseil-cpp//absl/algorithm:container",
145+
"@abseil-cpp//absl/base:nullability",
146+
"@abseil-cpp//absl/container:flat_hash_map",
147+
"@abseil-cpp//absl/container:flat_hash_set",
148+
"@abseil-cpp//absl/log",
149+
"@abseil-cpp//absl/log:check",
150+
"@abseil-cpp//absl/log:die_if_null",
151+
"@abseil-cpp//absl/strings",
152+
"@abseil-cpp//absl/types:span",
153+
"@protobuf",
154+
],
155+
)
156+
157+
cc_binary(
158+
name = "gen_wrappers_test_pybind11",
159+
srcs = ["gen_wrappers_test_pybind11.cc"],
160+
deps = [
161+
":wrappers",
162+
"//ortools/base",
163+
"//ortools/util/testdata:wrappers_test_cc_proto",
164+
"@abseil-cpp//absl/flags:parse",
165+
"@abseil-cpp//absl/flags:usage",
166+
"@abseil-cpp//absl/log:die_if_null",
167+
"@abseil-cpp//absl/log:initialize",
168+
"@abseil-cpp//absl/strings", # IWYU pragma: keep
169+
"@abseil-cpp//absl/strings:str_format",
170+
],
171+
)
172+
173+
genrule(
174+
name = "run_gen_wrappers_test_pybind11",
175+
outs = ["wrappers_test_pybind11.h"],
176+
cmd = "$(location :gen_wrappers_test_pybind11) > $@",
177+
tools = [":gen_wrappers_test_pybind11"],
178+
)
179+
180+
cc_library(
181+
name = "wrappers_test_pybind11",
182+
hdrs = ["wrappers_test_pybind11.h"],
183+
)
184+
185+
pybind_extension(
186+
name = "wrappers_test_extension",
187+
srcs = ["wrappers_test_extension.cc"],
188+
visibility = ["//visibility:public"],
189+
deps = [
190+
":wrappers_test_pybind11",
191+
"//ortools/port:proto_utils",
192+
"//ortools/util/testdata:wrappers_test_cc_proto",
193+
"@abseil-cpp//absl/strings",
194+
],
195+
)
196+
197+
py_test(
198+
name = "wrappers_test",
199+
srcs = ["wrappers_test.py"],
200+
deps = [
201+
":wrappers_test_extension",
202+
requirement("absl-py"),
203+
],
204+
)

ortools/util/python/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ endif()
6767
target_link_libraries(solve_interrupter_pybind11 PRIVATE ${PROJECT_NAMESPACE}::ortools)
6868
add_library(${PROJECT_NAMESPACE}::solve_interrupter_pybind11 ALIAS solve_interrupter_pybind11)
6969

70+
set(WRAPPERS_NAME util_python_wrappers)
71+
72+
add_library(${WRAPPERS_NAME} OBJECT wrappers.h wrappers.cc)
73+
set_target_properties(${WRAPPERS_NAME} PROPERTIES
74+
POSITION_INDEPENDENT_CODE ON)
75+
target_include_directories(${WRAPPERS_NAME} PUBLIC
76+
${PROJECT_SOURCE_DIR}
77+
${PROJECT_BINARY_DIR})
78+
target_link_libraries(${WRAPPERS_NAME} PUBLIC
79+
absl::memory
80+
absl::synchronization
81+
absl::str_format
82+
protobuf::libprotobuf)
83+
add_library(${PROJECT_NAMESPACE}::${WRAPPERS_NAME} ALIAS ${WRAPPERS_NAME})
7084

7185
if(BUILD_TESTING)
7286
add_library(py_solve_interrupter_testing OBJECT
@@ -97,6 +111,9 @@ if(BUILD_TESTING)
97111

98112
file(GLOB PYTHON_SRCS "*_test.py")
99113
foreach(FILE_NAME IN LISTS PYTHON_SRCS)
114+
if("${FILE_NAME}" MATCHES ".*wrappers_test.py")
115+
continue() # Skip to the next iteration of the loop
116+
endif()
100117
add_python_test(FILE_NAME ${FILE_NAME})
101118
endforeach()
102119
endif()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2010-2025 Google LLC
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
#include "absl/flags/parse.h"
15+
#include "absl/flags/usage.h"
16+
#include "absl/log/die_if_null.h"
17+
#include "absl/log/initialize.h"
18+
#include "absl/strings/str_format.h"
19+
#include "absl/strings/str_join.h" // IWYU pragma: keep
20+
#include "ortools/util/python/wrappers.h"
21+
#include "ortools/util/testdata/wrappers_test.pb.h"
22+
23+
namespace operations_research::util::python {
24+
25+
void ParseAndGenerate() {
26+
absl::PrintF(
27+
R"(
28+
29+
// This is a generated file, do not edit.
30+
#if defined(IMPORT_PROTO_WRAPPER_CODE)
31+
%s
32+
#endif // defined(IMPORT_PROTO_WRAPPER_CODE)
33+
)",
34+
GeneratePybindCode(
35+
{ABSL_DIE_IF_NULL(operations_research::util::python::
36+
WrappersTestMessage::descriptor())}));
37+
}
38+
39+
} // namespace operations_research::util::python
40+
41+
int main(int argc, char* argv[]) {
42+
absl::InitializeLog();
43+
absl::SetProgramUsageMessage(argv[0]);
44+
absl::ParseCommandLine(argc, argv);
45+
operations_research::util::python::ParseAndGenerate();
46+
return 0;
47+
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
#include "ortools/sat/python/wrappers.h"
14+
#include "ortools/util/python/wrappers.h"
1515

1616
#include <string>
1717
#include <utility>
@@ -31,7 +31,7 @@
3131
#include "absl/types/span.h"
3232
#include "google/protobuf/descriptor.h"
3333

34-
namespace operations_research::sat::python {
34+
namespace operations_research::util::python {
3535

3636
// A class that generates pybind11 code for a proto message.
3737
class Generator {
@@ -150,15 +150,15 @@ class Generator {
150150
.def(py::init<>())
151151
.def("copy_from",
152152
[](std::shared_ptr<$0> self, std::shared_ptr<$0> other) {
153-
self->CopyFrom(*other);
153+
self->CopyFrom(*other);
154154
})
155155
.def("merge_from",
156156
[](std::shared_ptr<$0> self, std::shared_ptr<$0> other) {
157-
self->MergeFrom(*other);
157+
self->MergeFrom(*other);
158158
})
159159
.def("clear",
160160
[](std::shared_ptr<$0> self) {
161-
self->Clear();
161+
self->Clear();
162162
})
163163
.def("merge_text_format",
164164
[](std::shared_ptr<$0> self, const std::string& text) {
@@ -217,11 +217,11 @@ class Generator {
217217
})
218218
.def("__copy__",
219219
[]($0 self) {
220-
return $0(self);
220+
return $0(self);
221221
})
222222
.def("__deepcopy__",
223223
[]($0 self, py::dict) {
224-
return $0(self);
224+
return $0(self);
225225
})
226226
.def("__str__",
227227
[]($0 self) {
@@ -485,4 +485,4 @@ std::string GeneratePybindCode(
485485
return Generator(roots, enums).Result();
486486
}
487487

488-
} // namespace operations_research::sat::python
488+
} // namespace operations_research::util::python

0 commit comments

Comments
 (0)