Skip to content

Commit

Permalink
remove the pywrap prefix from pybind11 modules; add _pybind to the cm…
Browse files Browse the repository at this point in the history
…ake target of the same modules
  • Loading branch information
lperron committed Jul 3, 2023
1 parent 32175c9 commit 346642b
Show file tree
Hide file tree
Showing 19 changed files with 219 additions and 194 deletions.
28 changes: 14 additions & 14 deletions cmake/python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ file(COPY
DESTINATION ${PYTHON_PROJECT_DIR}/linear_solver)
file(COPY
ortools/linear_solver/python/model_builder.py
ortools/linear_solver/python/model_builder_helper.py
ortools/linear_solver/python/pandas_model.py
ortools/linear_solver/python/model_builder_numbers.py
ortools/linear_solver/python/pandas_model.py
DESTINATION ${PYTHON_PROJECT_DIR}/linear_solver/python)
file(COPY
ortools/sat/python/cp_model.py
Expand Down Expand Up @@ -318,18 +318,18 @@ add_custom_command(
COMMAND ${CMAKE_COMMAND} -E $<IF:$<STREQUAL:$<TARGET_PROPERTY:ortools,TYPE>,SHARED_LIBRARY>,copy,true>
$<$<STREQUAL:$<TARGET_PROPERTY:ortools,TYPE>,SHARED_LIBRARY>:$<TARGET_SONAME_FILE:ortools>>
${PYTHON_PROJECT}/.libs
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:init> ${PYTHON_PROJECT}/init/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:knapsack_solver> ${PYTHON_PROJECT}/algorithms/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:init_pybind11> ${PYTHON_PROJECT}/init/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:knapsack_solver_pybind11> ${PYTHON_PROJECT}/algorithms/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:linear_sum_assignment_pybind11> ${PYTHON_PROJECT}/graph/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:max_flow_pybind11> ${PYTHON_PROJECT}/graph/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:min_cost_flow_pybind11> ${PYTHON_PROJECT}/graph/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pywrapcp> ${PYTHON_PROJECT}/constraint_solver
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pywraplp> ${PYTHON_PROJECT}/linear_solver
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pywrap_model_builder_helper> ${PYTHON_PROJECT}/linear_solver/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:model_builder_helper_pybind11> ${PYTHON_PROJECT}/linear_solver/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pywrap_pdlp_pybind11> ${PYTHON_PROJECT}/pdlp/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:swig_helper> ${PYTHON_PROJECT}/sat/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:rcpsp> ${PYTHON_PROJECT}/scheduling/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:sorted_interval_list> ${PYTHON_PROJECT}/util/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:swig_helper_pybind11> ${PYTHON_PROJECT}/sat/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:rcpsp_pybind11> ${PYTHON_PROJECT}/scheduling/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:sorted_interval_list_pybind11> ${PYTHON_PROJECT}/util/python
#COMMAND ${Python3_EXECUTABLE} setup.py bdist_egg bdist_wheel
COMMAND ${Python3_EXECUTABLE} setup.py bdist_wheel
COMMAND ${CMAKE_COMMAND} -E touch ${PROJECT_BINARY_DIR}/python/dist/timestamp
Expand All @@ -339,18 +339,18 @@ add_custom_command(
python/setup.py
Py${PROJECT_NAME}_proto
${PROJECT_NAMESPACE}::ortools
init
knapsack_solver
init_pybind11
knapsack_solver_pybind11
linear_sum_assignment_pybind11
max_flow_pybind11
min_cost_flow_pybind11
pywrapcp
pywraplp
pywrap_model_builder_helper
model_builder_helper_pybind11
pywrap_pdlp_pybind11
swig_helper
rcpsp
sorted_interval_list
swig_helper_pybind11
rcpsp_pybind11
sorted_interval_list_pybind11
BYPRODUCTS
python/${PYTHON_PROJECT}
python/${PYTHON_PROJECT}.egg-info
Expand Down
16 changes: 10 additions & 6 deletions ortools/algorithms/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@
# See the License for the specific language governing permissions and
# limitations under the License.

pybind11_add_module(knapsack_solver MODULE knapsack_solver.cc)
pybind11_add_module(knapsack_solver_pybind11 MODULE knapsack_solver.cc)
set_target_properties(knapsack_solver_pybind11 PROPERTIES
LIBRARY_OUTPUT_NAME "knapsack_solver")

# note: macOS is APPLE and also UNIX !
if(APPLE)
set_target_properties(knapsack_solver PROPERTIES
set_target_properties(knapsack_solver_pybind11 PROPERTIES
SUFFIX ".so"
INSTALL_RPATH "@loader_path;@loader_path/../../../${PYTHON_PROJECT}/.libs"
)
set_property(TARGET knapsack_solver APPEND PROPERTY
set_property(TARGET knapsack_solver_pybind11 APPEND PROPERTY
LINK_FLAGS "-flat_namespace -undefined suppress"
)
elseif(UNIX)
set_target_properties(knapsack_solver PROPERTIES
set_target_properties(knapsack_solver_pybind11 PROPERTIES
INSTALL_RPATH "$ORIGIN:$ORIGIN/../../../${PYTHON_PROJECT}/.libs"
)
endif()
target_link_libraries(knapsack_solver PRIVATE ${PROJECT_NAMESPACE}::ortools)
add_library(${PROJECT_NAMESPACE}::knapsack_solver ALIAS knapsack_solver)

target_link_libraries(knapsack_solver_pybind11 PRIVATE ${PROJECT_NAMESPACE}::ortools)
add_library(${PROJECT_NAMESPACE}::knapsack_solver_pybind11 ALIAS knapsack_solver_pybind11)

if(BUILD_TESTING)
file(GLOB PYTHON_SRCS "*_test.py")
Expand Down
6 changes: 3 additions & 3 deletions ortools/algorithms/python/knapsack_solver_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef OR_TOOLS_ALGORITHMS_PYTHON_knapsack_SOLVER_DOC_H_
#define OR_TOOLS_ALGORITHMS_PYTHON_knapsack_SOLVER_DOC_H_
#ifndef OR_TOOLS_ALGORITHMS_PYTHON_KNAPSACK_SOLVER_DOC_H_
#define OR_TOOLS_ALGORITHMS_PYTHON_KNAPSACK_SOLVER_DOC_H_

/*
This file contains docstrings for use in the Python bindings.
Expand Down Expand Up @@ -292,4 +292,4 @@ static const char* __doc_operations_research_KnapsackState_is_in_2 =
#pragma GCC diagnostic pop
#endif

#endif // OR_TOOLS_ALGORITHMS_PYTHON_knapsack_SOLVER_DOC_H_
#endif // OR_TOOLS_ALGORITHMS_PYTHON_KNAPSACK_SOLVER_DOC_H_
8 changes: 3 additions & 5 deletions ortools/algorithms/python/knapsack_solver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
"""knapsack_solver unittest file."""

from absl import app

import unittest

from absl.testing import absltest
from ortools.algorithms.python import knapsack_solver


class PyWrapAlgorithmsKnapsackSolverTest(unittest.TestCase):
class PyWrapAlgorithmsKnapsackSolverTest(absltest.TestCase):
def RealSolve(self, profits, weights, capacities, solver_type, use_reduction):
solver = knapsack_solver.KnapsackSolver(solver_type, "solver")
solver.set_use_reduction(use_reduction)
Expand Down Expand Up @@ -262,7 +260,7 @@ def testSolveBigOneDimension(self):


def main(_):
unittest.main()
absltest.main()


if __name__ == "__main__":
Expand Down
16 changes: 10 additions & 6 deletions ortools/init/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@
# See the License for the specific language governing permissions and
# limitations under the License.

pybind11_add_module(init MODULE init.cc)
pybind11_add_module(init_pybind11 MODULE init.cc)
set_target_properties(init_pybind11 PROPERTIES
LIBRARY_OUTPUT_NAME "init")

# note: macOS is APPLE and also UNIX !
if(APPLE)
set_target_properties(init PROPERTIES
set_target_properties(init_pybind11 PROPERTIES
SUFFIX ".so"
INSTALL_RPATH "@loader_path;@loader_path/../../../${PYTHON_PROJECT}/.libs"
)
set_property(TARGET init APPEND PROPERTY
set_property(TARGET init_pybind11 APPEND PROPERTY
LINK_FLAGS "-flat_namespace -undefined suppress"
)
elseif(UNIX)
set_target_properties(init PROPERTIES
set_target_properties(init_pybind11 PROPERTIES
INSTALL_RPATH "$ORIGIN:$ORIGIN/../../../${PYTHON_PROJECT}/.libs"
)
endif()
target_link_libraries(init PRIVATE ${PROJECT_NAMESPACE}::ortools)
add_library(${PROJECT_NAMESPACE}::init ALIAS init)

target_link_libraries(init_pybind11 PRIVATE ${PROJECT_NAMESPACE}::ortools)
add_library(${PROJECT_NAMESPACE}::init_pybind11 ALIAS init_pybind11)

if(BUILD_TESTING)
file(GLOB PYTHON_SRCS "*_test.py")
Expand Down
6 changes: 3 additions & 3 deletions ortools/init/python/init_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef OR_TOOLS_OPEN_SOURCE_INIT_PYTHON_PYWRAPINIT_DOC_H_
#define OR_TOOLS_OPEN_SOURCE_INIT_PYTHON_PYWRAPINIT_DOC_H_
#ifndef OR_TOOLS_OPEN_SOURCE_INIT_PYTHON_INIT_DOC_H_
#define OR_TOOLS_OPEN_SOURCE_INIT_PYTHON_INIT_DOC_H_

/*
This file contains docstrings for use in the Python bindings.
Expand Down Expand Up @@ -141,4 +141,4 @@ static const char* __doc_operations_research_OrToolsVersion_VersionString =
#pragma GCC diagnostic pop
#endif

#endif // OR_TOOLS_OPEN_SOURCE_INIT_PYTHON_PYWRAPINIT_DOC_H_
#endif // OR_TOOLS_OPEN_SOURCE_INIT_PYTHON_INIT_DOC_H_
36 changes: 18 additions & 18 deletions ortools/linear_solver/python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
load("@rules_python//python:defs.bzl", "py_library", "py_test")

pybind_extension(
name = "pywrap_model_builder_helper",
srcs = ["pywrap_model_builder_helper.cc"],
name = "model_builder_helper",
srcs = ["model_builder_helper.cc"],
visibility = ["//visibility:public"],
deps = [
"//ortools/linear_solver:linear_solver_cc_proto",
Expand All @@ -30,17 +30,6 @@ pybind_extension(
],
)

py_library(
name = "model_builder_helper",
srcs = ["model_builder_helper.py"],
visibility = ["//visibility:public"],
deps = [
":pywrap_model_builder_helper",
"//ortools/linear_solver:linear_solver_py_pb2",
requirement("numpy"),
],
)

py_test(
name = "model_builder_helper_test",
srcs = ["model_builder_helper_test.py"],
Expand All @@ -51,22 +40,32 @@ py_test(
python_version = "PY3",
deps = [
":model_builder_helper",
":pywrap_model_builder_helper",
":model_builder_numbers",
"//ortools/linear_solver:linear_solver_py_pb2",
requirement("absl-py"),
requirement("numpy"),
requirement("scipy"),
],
)

py_library(
name = "model_builder_numbers",
srcs = ["model_builder_numbers.py"],
visibility = ["//visibility:public"],
deps = [
requirement("numpy"),
],
)

py_library(
name = "model_builder",
srcs = ["model_builder.py"],
visibility = ["//visibility:public"],
deps = [
":model_builder_helper",
":pywrap_model_builder_helper",
":model_builder_numbers",
requirement("numpy"),
"//ortools/linear_solver:linear_solver_py_pb2",
"@com_google_protobuf//:protobuf_python",
],
)

Expand All @@ -81,7 +80,8 @@ py_test(
deps = [
":model_builder",
":model_builder_helper",
":pywrap_model_builder_helper",
":model_builder_numbers",
requirement("absl-py"),
requirement("numpy"),
],
)
Expand All @@ -90,7 +90,7 @@ py_library(
name = "pandas_model",
srcs = ["pandas_model.py"],
deps = [
":pywrap_model_builder_helper",
":model_builder_helper",
requirement("numpy"),
requirement("pandas"),
"//ortools/linear_solver:linear_solver_py_pb2",
Expand Down
17 changes: 9 additions & 8 deletions ortools/linear_solver/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,33 @@ if(MSVC)
target_link_libraries(pywraplp PRIVATE ${Python3_LIBRARIES})
endif()

pybind11_add_module(pywrap_model_builder_helper MODULE pywrap_model_builder_helper.cc)
pybind11_add_module(model_builder_helper_pybind11 MODULE model_builder_helper.cc)
set_target_properties(model_builder_helper_pybind11 PROPERTIES
LIBRARY_OUTPUT_NAME "model_builder_helper")

target_include_directories(pywrap_model_builder_helper PRIVATE
target_include_directories(model_builder_helper_pybind11 PRIVATE
${protobuf_SOURCE_DIR})

# note: macOS is APPLE and also UNIX !
if(APPLE)
set_target_properties(pywrap_model_builder_helper PROPERTIES
set_target_properties(model_builder_helper_pybind11 PROPERTIES
SUFFIX ".so"
INSTALL_RPATH "@loader_path;@loader_path/../../../${PYTHON_PROJECT}/.libs"
)
set_property(TARGET pywrap_model_builder_helper APPEND PROPERTY
set_property(TARGET model_builder_helper_pybind11 APPEND PROPERTY
LINK_FLAGS "-flat_namespace -undefined suppress"
)
elseif(UNIX)
set_target_properties(pywrap_model_builder_helper PROPERTIES
set_target_properties(model_builder_helper_pybind11 PROPERTIES
INSTALL_RPATH "$ORIGIN:$ORIGIN/../../../${PYTHON_PROJECT}/.libs"
)
endif()

target_link_libraries(pywrap_model_builder_helper PRIVATE
target_link_libraries(model_builder_helper_pybind11 PRIVATE
${PROJECT_NAMESPACE}::ortools
pybind11_native_proto_caster
)

add_library(${PROJECT_NAMESPACE}::pywrap_model_builder_helper ALIAS pywrap_model_builder_helper)
add_library(${PROJECT_NAMESPACE}::model_builder_helper_pybind11 ALIAS model_builder_helper_pybind11)

if(BUILD_TESTING)
file(GLOB PYTHON_SRCS "*_test.py")
Expand Down
Loading

0 comments on commit 346642b

Please sign in to comment.