Skip to content

Commit

Permalink
backport main/
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Oct 14, 2024
1 parent 960d970 commit 8a996d7
Show file tree
Hide file tree
Showing 36 changed files with 1,026 additions and 738 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
cmake_minimum_required(VERSION 3.20)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# Enable output of compile commands during generation.
option(CMAKE_EXPORT_COMPILE_COMMANDS "Export compile command" ON)

include(utils)
set_version(VERSION)

Expand Down
75 changes: 39 additions & 36 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ git_repository(

git_repository(
name = "rules_python",
tag = "0.34.0",
tag = "0.36.0",
remote = "https://github.com/bazelbuild/rules_python.git",
)

Expand Down Expand Up @@ -105,6 +105,43 @@ git_repository(
repo_mapping = {"@abseil-cpp": "@com_google_absl"},
)

## Python
load("@rules_python//python:repositories.bzl", "py_repositories")
py_repositories()

load("@rules_python//python:repositories.bzl", "python_register_toolchains")
DEFAULT_PYTHON = "3.11"
python_register_toolchains(
name = "python3_11",
python_version = DEFAULT_PYTHON,
ignore_root_user_error=True,
)
load("@python3_11//:defs.bzl", "interpreter")

# Create a central external repo, @pip_deps, that contains Bazel targets for all the
# third-party packages specified in the bazel/requirements.txt file.
load("@rules_python//python:pip.bzl", "pip_parse")
pip_parse(
name = "pip_deps",
python_interpreter_target = interpreter,
requirements_lock = "//bazel:ortools_requirements.txt",
)

load("@pip_deps//:requirements.bzl",
install_pip_deps="install_deps")
install_pip_deps()

# Add a second repo @ortools_notebook_deps for jupyter notebooks.
pip_parse(
name = "ortools_notebook_deps",
python_interpreter_target = interpreter,
requirements_lock = "//bazel:notebook_requirements.txt",
)

load("@ortools_notebook_deps//:requirements.bzl",
install_notebook_deps="install_deps")
install_notebook_deps()

## Protobuf
# proto_library, cc_proto_library, and java_proto_library rules implicitly
# depend on @com_google_protobuf for protoc and proto runtimes.
Expand Down Expand Up @@ -205,40 +242,6 @@ new_git_repository(
remote = "https://github.com/swig/swig.git",
)

## Python
load("@rules_python//python:repositories.bzl", "py_repositories")
py_repositories()

load("@rules_python//python:repositories.bzl", "python_register_toolchains")
DEFAULT_PYTHON = "3.12"
python_register_toolchains(
name = "python3_12",
python_version = DEFAULT_PYTHON,
ignore_root_user_error=True,
)

# Create a central external repo, @pip_deps, that contains Bazel targets for all the
# third-party packages specified in the bazel/requirements.txt file.
load("@rules_python//python:pip.bzl", "pip_parse")
pip_parse(
name = "pip_deps",
requirements_lock = "//bazel:ortools_requirements.txt",
)

load("@pip_deps//:requirements.bzl",
install_pip_deps="install_deps")
install_pip_deps()

# Add a second repo @ortools_notebook_deps for jupyter notebooks.
pip_parse(
name = "ortools_notebook_deps",
requirements_lock = "//bazel:notebook_requirements.txt",
)

load("@ortools_notebook_deps//:requirements.bzl",
install_notebook_deps="install_deps")
install_notebook_deps()

# Protobuf
load("@com_google_protobuf//bazel:system_python.bzl", "system_python")
system_python(
Expand Down Expand Up @@ -283,7 +286,7 @@ new_git_repository(

new_git_repository(
name = "pybind11_protobuf",
commit = "84653a591aea5df482dc2bde42c19efafbd53a57", # 2024/06/28
commit = "ed430af1814a97e4017f2f808d3ba28cc10802f1", # 2024/10/02
remote = "https://github.com/pybind/pybind11_protobuf.git",
)

Expand Down
10 changes: 5 additions & 5 deletions cmake/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ TOOLCHAIN_STAGES := env devel toolchain build test
define toolchain-stage-target =
#$$(info STAGE: $1)
#$$(info Create targets: toolchain_$1 $(addprefix toolchain_, $(addsuffix _$1, $(TOOLCHAIN_TARGETS))).)
targets_toolchain_$1 = $(addprefix toolchain_, $(addsuffix _$1, $(TOOLCHAIN_TARGETS)))
targets_toolchain_$1 := $(addprefix toolchain_, $(addsuffix _$1, $(TOOLCHAIN_TARGETS)))
.PHONY: toolchain_$1 $$(targets_toolchain_$1)
toolchain_$1: $$(targets_toolchain_$1)
$$(targets_toolchain_$1): toolchain_%_$1: docker/toolchain/Dockerfile
Expand All @@ -645,7 +645,7 @@ $$(targets_toolchain_$1): toolchain_%_$1: docker/toolchain/Dockerfile
..

#$$(info Create targets: save_toolchain_$1 $(addprefix save_toolchain_, $(addsuffix _$1, $(TOOLCHAIN_TARGETS))) (debug).)
save_targets_toolchain_$1 = $(addprefix save_toolchain_, $(addsuffix _$1, $(TOOLCHAIN_TARGETS)))
save_targets_toolchain_$1 := $(addprefix save_toolchain_, $(addsuffix _$1, $(TOOLCHAIN_TARGETS)))
.PHONY: save_toolchain_$1 $$(save_targets_toolchain_$1)
save_toolchain_$1: $$(save_targets_toolchain_$1)
$$(save_targets_toolchain_$1): save_toolchain_%_$1: cache/%/docker_$1.tar
Expand Down Expand Up @@ -727,7 +727,7 @@ VAGRANT_VMS := \
define make-vagrant-target =
#$$(info VMS: $1)
#$$(info Create target: $1_<LANG>.)
$1_targets = $(addprefix $1_, $(LANGUAGES))
$1_targets := $(addprefix $1_, $(LANGUAGES))
.PHONY: $1 $$($1_targets)
$1: $$($1_targets)
$$($1_targets): $1_%: vagrant/$1/%/Vagrantfile
Expand All @@ -736,14 +736,14 @@ $$($1_targets): $1_%: vagrant/$1/%/Vagrantfile
cd vagrant/$1/$$* && vagrant up

#$$(info Create targets: sh_$1_<lang> vagrant machine (debug).)
sh_$1_targets = $(addprefix sh_$1_, $(LANGUAGES))
sh_$1_targets := $(addprefix sh_$1_, $(LANGUAGES))
.PHONY: $$(sh_$1_targets)
$$(sh_$1_targets): sh_$1_%:
cd vagrant/$1/$$* && vagrant up
cd vagrant/$1/$$* && vagrant ssh

#$$(info Create targets: clean_$1)
clean_$1_targets = $(addprefix clean_$1_, $(LANGUAGES))
clean_$1_targets := $(addprefix clean_$1_, $(LANGUAGES))
.PHONY: clean_$1 $(clean_$1_targets)
clean_$1: $$(clean_$1_targets)
$$(clean_$1_targets): clean_$1_%:
Expand Down
28 changes: 26 additions & 2 deletions cmake/check_deps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,36 @@ if(USE_COINOR)
set(COINOR_DEPS Coin::CbcSolver Coin::OsiCbc Coin::ClpSolver Coin::OsiClp)
endif()

if(USE_CPLEX)
if(NOT TARGET CPLEX::CPLEX)
message(FATAL_ERROR "Target CPLEX::CPLEX not available.")
endif()
set(CPLEX_DEPS CPLEX::CPLEX)
endif()

if(USE_GLPK)
if(NOT TARGET GLPK::GLPK)
message(FATAL_ERROR "Target GLPK::GLPK not available.")
endif()
set(GLPK_DEPS GLPK::GLPK)
endif()

if(USE_HIGHS)
if(NOT TARGET highs::highs)
message(FATAL_ERROR "Target highs::highs not available.")
endif()
set(HIGHS_DEPS highs::highs)
endif()

if(USE_PDLP AND BUILD_PDLP)
set(PDLP_DEPS Eigen3::Eigen)
endif()

if(USE_SCIP AND NOT TARGET libscip)
message(FATAL_ERROR "Target libscip not available.")
if(USE_SCIP)
if(NOT TARGET libscip)
message(FATAL_ERROR "Target libscip not available.")
endif()
set(SCIP_DEPS libscip)
endif()

# Check optional Dependencies
Expand Down
151 changes: 117 additions & 34 deletions cmake/cpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -147,70 +147,153 @@ endif()
# ortools_cxx_test()
# CMake function to generate and build C++ test.
# Parameters:
# FILE_NAME: the C++ filename
# COMPONENT_NAME: name of the ortools/ subdir where the test is located
# note: automatically determined if located in ortools/<component>/
# NAME: CMake target name
# SOURCES: List of source files
# [COMPILE_DEFINITIONS]: List of private compile definitions
# [COMPILE_OPTIONS]: List of private compile options
# [LINK_LIBRARIES]: List of private libraries to use when linking
# note: ortools::ortools is always linked to the target
# [LINK_OPTIONS]: List of private link options
# e.g.:
# ortools_cxx_test(
# FILE_NAME
# ${PROJECT_SOURCE_DIR}/ortools/foo/foo_test.cc
# COMPONENT_NAME
# foo
# DEPS
# NAME
# foo_bar_test
# SOURCES
# bar_test.cc
# ${PROJECT_SOURCE_DIR}/ortools/foo/bar_test.cc
# LINK_LIBRARIES
# GTest::gmock
# GTest::gtest_main
# )
function(ortools_cxx_test)
set(options "")
set(oneValueArgs "FILE_NAME;COMPONENT_NAME")
set(multiValueArgs "DEPS")
set(oneValueArgs "NAME")
set(multiValueArgs
"SOURCES;COMPILE_DEFINITIONS;COMPILE_OPTIONS;LINK_LIBRARIES;LINK_OPTIONS")
cmake_parse_arguments(TEST
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if(NOT TEST_FILE_NAME)
message(FATAL_ERROR "no FILE_NAME provided")
if(NOT TEST_NAME)
message(FATAL_ERROR "no NAME provided")
endif()
get_filename_component(TEST_NAME ${TEST_FILE_NAME} NAME_WE)

message(STATUS "Configuring test ${TEST_FILE_NAME} ...")

if(NOT TEST_COMPONENT_NAME)
# test is located in ortools/<component_name>/
get_filename_component(COMPONENT_DIR ${TEST_FILE_NAME} DIRECTORY)
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)
else()
set(COMPONENT_NAME ${TEST_COMPONENT_NAME})
if(NOT TEST_SOURCES)
message(FATAL_ERROR "no SOURCES provided")
endif()
message(STATUS "Configuring test ${TEST_NAME} ...")

add_executable(${TEST_NAME} ${TEST_FILE_NAME})
add_executable(${TEST_NAME} "")
target_sources(${TEST_NAME} PRIVATE ${TEST_SOURCES})
target_include_directories(${TEST_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_definitions(${TEST_NAME} PRIVATE ${TEST_COMPILE_DEFINITIONS})
target_compile_features(${TEST_NAME} PRIVATE cxx_std_17)
target_compile_options(${TEST_NAME} PRIVATE ${TEST_COMPILE_OPTIONS})
target_link_libraries(${TEST_NAME} PRIVATE
${PROJECT_NAMESPACE}::ortools
${TEST_DEPS}
${TEST_LINK_LIBRARIES}
)
target_link_options(${TEST_NAME} PRIVATE ${TEST_LINK_OPTIONS})

include(GNUInstallDirs)
if(APPLE)
set_target_properties(${TEST_NAME} PROPERTIES INSTALL_RPATH
"@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path")
set_target_properties(${TEST_NAME} PROPERTIES
INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path")
elseif(UNIX)
cmake_path(RELATIVE_PATH CMAKE_INSTALL_FULL_LIBDIR
BASE_DIRECTORY ${CMAKE_INSTALL_FULL_BINDIR}
OUTPUT_VARIABLE libdir_relative_path)
set_target_properties(${TEST_NAME} PROPERTIES
INSTALL_RPATH "$ORIGIN/${libdir_relative_path}")
INSTALL_RPATH "$ORIGIN/${libdir_relative_path}:$ORIGIN")
endif()

if(BUILD_TESTING)
add_test(
NAME cxx_${COMPONENT_NAME}_${TEST_NAME}
COMMAND ${TEST_NAME})
NAME cxx_${TEST_NAME}
COMMAND ${TEST_NAME}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)
endif()
message(STATUS "Configuring test ${TEST_NAME} ...DONE")
endfunction()

###################
## C++ Library ##
###################
# ortools_cxx_library()
# CMake function to generate and build C++ library.
# Parameters:
# NAME: CMake target name
# SOURCES: List of source files
# [TYPE]: SHARED or STATIC
# [COMPILE_DEFINITIONS]: List of private compile definitions
# [COMPILE_OPTIONS]: List of private compile options
# [LINK_LIBRARIES]: List of **public** libraries to use when linking
# note: ortools::ortools is always linked to the target
# [LINK_OPTIONS]: List of private link options
# e.g.:
# ortools_cxx_library(
# NAME
# foo_bar_library
# SOURCES
# bar_library.cc
# ${PROJECT_SOURCE_DIR}/ortools/foo/bar_library.cc
# TYPE
# SHARED
# LINK_LIBRARIES
# GTest::gmock
# GTest::gtest_main
# TESTING
# )
function(ortools_cxx_library)
set(options "TESTING")
set(oneValueArgs "NAME;TYPE")
set(multiValueArgs
"SOURCES;COMPILE_DEFINITIONS;COMPILE_OPTIONS;LINK_LIBRARIES;LINK_OPTIONS")
cmake_parse_arguments(LIBRARY
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if(LIBRARY_TESTING AND NOT BUILD_TESTING)
return()
endif()

if(NOT LIBRARY_NAME)
message(FATAL_ERROR "no NAME provided")
endif()
if(NOT LIBRARY_SOURCES)
message(FATAL_ERROR "no SOURCES provided")
endif()
message(STATUS "Configuring library ${LIBRARY_NAME} ...")

add_library(${LIBRARY_NAME} ${LIBRARY_TYPE} "")
target_sources(${LIBRARY_NAME} PRIVATE ${LIBRARY_SOURCES})
target_include_directories(${LIBRARY_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_definitions(${LIBRARY_NAME} PRIVATE ${LIBRARY_COMPILE_DEFINITIONS})
target_compile_features(${LIBRARY_NAME} PRIVATE cxx_std_17)
target_compile_options(${LIBRARY_NAME} PRIVATE ${LIBRARY_COMPILE_OPTIONS})
target_link_libraries(${LIBRARY_NAME} PUBLIC
${PROJECT_NAMESPACE}::ortools
${LIBRARY_LINK_LIBRARIES}
)
target_link_options(${LIBRARY_NAME} PRIVATE ${LIBRARY_LINK_OPTIONS})

include(GNUInstallDirs)
if(APPLE)
set_target_properties(${LIBRARY_NAME} PROPERTIES
INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path")
elseif(UNIX)
cmake_path(RELATIVE_PATH CMAKE_INSTALL_FULL_LIBDIR
BASE_DIRECTORY ${CMAKE_INSTALL_FULL_BINDIR}
OUTPUT_VARIABLE libdir_relative_path)
set_target_properties(${LIBRARY_NAME} PROPERTIES
INSTALL_RPATH "$ORIGIN/${libdir_relative_path}:$ORIGIN")
endif()
message(STATUS "Configuring test ${TEST_FILE_NAME} ...DONE")
add_library(${PROJECT_NAMESPACE}::${LIBRARY_NAME} ALIAS ${LIBRARY_NAME})
message(STATUS "Configuring library ${LIBRARY_NAME} ...DONE")
endfunction()

##################
Expand Down Expand Up @@ -460,11 +543,11 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
protobuf::libprotobuf
${RE2_DEPS}
${COINOR_DEPS}
$<$<BOOL:${USE_CPLEX}>:CPLEX::CPLEX>
$<$<BOOL:${USE_GLPK}>:GLPK::GLPK>
$<$<BOOL:${USE_HIGHS}>:highs::highs>
${CPLEX_DEPS}
${GLPK_DEPS}
${HIGHS_DEPS}
${PDLP_DEPS}
$<$<BOOL:${USE_SCIP}>:libscip>
${SCIP_DEPS}
Threads::Threads)
if(WIN32)
target_link_libraries(${PROJECT_NAME} PUBLIC psapi.lib ws2_32.lib)
Expand Down
Loading

0 comments on commit 8a996d7

Please sign in to comment.