Skip to content

Commit 48608ea

Browse files
committed
Add autogenerated targets from bazel
1 parent 05fc0c9 commit 48608ea

File tree

6 files changed

+816
-2
lines changed

6 files changed

+816
-2
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ examples/dotnet/obj
107107
CMakeCache.txt
108108
CMakeFiles
109109
DartConfiguration.tcl
110-
*build*/*
111-
build/
110+
/build*/
112111

113112
# Ignore Bzlmod lock file until it is more stable
114113
MODULE.bazel.lock

cmake/cpp.cmake

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,119 @@ function(ortools_cxx_library)
309309
message(STATUS "Configuring library ${LIBRARY_NAME} ...DONE")
310310
endfunction()
311311

312+
313+
# ortools_cxx_binary()
314+
# CMake function to generate and build C++ library.
315+
# Parameters:
316+
# NAME: CMake target name
317+
# SOURCES: List of source files
318+
# [COMPILE_DEFINITIONS]: List of private compile definitions
319+
# [COMPILE_OPTIONS]: List of private compile options
320+
# [LINK_LIBRARIES]: List of **public** libraries to use when linking
321+
# note: ortools::ortools is always linked to the target
322+
# [LINK_OPTIONS]: List of private link options
323+
# e.g.:
324+
# ortools_cxx_binary(
325+
# NAME
326+
# foo_bar_binary
327+
# SOURCES
328+
# bar_binary.cc
329+
# ${PROJECT_SOURCE_DIR}/ortools/foo/bar_binary.cc
330+
# LINK_LIBRARIES
331+
# GTest::gmock
332+
# GTest::gtest_main
333+
# TESTING
334+
# )
335+
function(ortools_cxx_binary)
336+
set(options "TESTING")
337+
set(oneValueArgs "NAME")
338+
set(multiValueArgs
339+
"SOURCES;COMPILE_DEFINITIONS;COMPILE_OPTIONS;LINK_LIBRARIES;LINK_OPTIONS")
340+
cmake_parse_arguments(BINARY
341+
"${options}"
342+
"${oneValueArgs}"
343+
"${multiValueArgs}"
344+
${ARGN}
345+
)
346+
if(BINARY_TESTING AND NOT BUILD_TESTING)
347+
return()
348+
endif()
349+
350+
if(NOT BINARY_NAME)
351+
message(FATAL_ERROR "no NAME provided")
352+
endif()
353+
if(NOT BINARY_SOURCES)
354+
message(FATAL_ERROR "no SOURCES provided")
355+
endif()
356+
message(STATUS "Configuring binary ${BINARY_NAME} ...")
357+
358+
add_executable(${BINARY_NAME} ${BINARY_TYPE} "")
359+
target_sources(${BINARY_NAME} PRIVATE ${BINARY_SOURCES})
360+
target_include_directories(${BINARY_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
361+
target_compile_definitions(${BINARY_NAME} PRIVATE ${BINARY_COMPILE_DEFINITIONS})
362+
target_compile_features(${BINARY_NAME} PRIVATE cxx_std_17)
363+
target_compile_options(${BINARY_NAME} PRIVATE ${BINARY_COMPILE_OPTIONS})
364+
target_link_libraries(${BINARY_NAME} PRIVATE ${PROJECT_NAMESPACE}::ortools ${BINARY_LINK_LIBRARIES})
365+
target_link_options(${BINARY_NAME} PRIVATE ${BINARY_LINK_OPTIONS})
366+
367+
include(GNUInstallDirs)
368+
if(APPLE)
369+
set_target_properties(${BINARY_NAME} PROPERTIES
370+
INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path")
371+
elseif(UNIX)
372+
cmake_path(RELATIVE_PATH CMAKE_INSTALL_FULL_LIBDIR
373+
BASE_DIRECTORY ${CMAKE_INSTALL_FULL_BINDIR}
374+
OUTPUT_VARIABLE libdir_relative_path)
375+
set_target_properties(${BINARY_NAME} PROPERTIES
376+
INSTALL_RPATH "$ORIGIN/${libdir_relative_path}:$ORIGIN")
377+
endif()
378+
add_executable(${PROJECT_NAMESPACE}::${BINARY_NAME} ALIAS ${BINARY_NAME})
379+
message(STATUS "Configuring binary ${BINARY_NAME} ...DONE")
380+
endfunction()
381+
382+
# ortools_cxx_bintest()
383+
# CMake function to generate and build C++ test.
384+
# Parameters:
385+
# NAME: CMake target name
386+
# SCRIPT: The script to run the test.
387+
# e.g.:
388+
# ortools_cxx_bintest(
389+
# NAME
390+
# foo_bar_bintest
391+
# SCRIPT
392+
# foo_bar.bintest
393+
# ENVIRONMENT
394+
# "BINTEST_foo_bar=$<TARGET_FILE:foo_bar_binary>"
395+
# "BINTEST_foo_bar_data=$(CMAKE_CURRENT_SOURCE_DIR)/foo_bar_data.txt"
396+
# )
397+
function(ortools_cxx_bintest)
398+
set(options "")
399+
set(oneValueArgs "NAME;SCRIPT")
400+
set(multiValueArgs "ENVIRONMENT")
401+
cmake_parse_arguments(BINTEST
402+
"${options}"
403+
"${oneValueArgs}"
404+
"${multiValueArgs}"
405+
${ARGN}
406+
)
407+
if(NOT BINTEST_NAME)
408+
message(FATAL_ERROR "no NAME provided")
409+
endif()
410+
if(NOT BINTEST_SCRIPT)
411+
message(FATAL_ERROR "no SCRIPT provided")
412+
endif()
413+
message(STATUS "Configuring binary test ${BINTEST_NAME} ...")
414+
add_test(
415+
NAME ${BINTEST_NAME}
416+
COMMAND python3 -m tools.testing.bintest_script_launcher ${BINTEST_SCRIPT}
417+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
418+
)
419+
set_tests_properties(${BINTEST_NAME} PROPERTIES
420+
ENVIRONMENT "${BINTEST_ENVIRONMENT}"
421+
)
422+
message(STATUS "Configuring binary test ${BINTEST_NAME} ...DONE")
423+
endfunction()
424+
312425
##################
313426
## PROTO FILE ##
314427
##################

0 commit comments

Comments
 (0)