From 570428542db496c9c14024f07cd78b117f463932 Mon Sep 17 00:00:00 2001 From: vsoch Date: Fri, 29 Sep 2023 13:54:47 -0600 Subject: [PATCH] build: add golang to cmake Problem: the new cmake build system needs to compile go tests for bindings Solution: use add_custom_command to do a build of the test. --- CMakeLists.txt | 5 ++++ README.md | 24 +++++++++++++++ cmake/GolangSimple.cmake | 30 +++++++++++++++++++ resource/reapi/bindings/CMakeLists.txt | 5 ++++ resource/reapi/bindings/go/CMakeLists.txt | 1 + resource/reapi/bindings/go/src/CMakeLists.txt | 1 + .../reapi/bindings/go/src/test/CMakeLists.txt | 17 +++++++++++ 7 files changed, 83 insertions(+) create mode 100644 cmake/GolangSimple.cmake create mode 100644 resource/reapi/bindings/go/CMakeLists.txt create mode 100644 resource/reapi/bindings/go/src/CMakeLists.txt create mode 100644 resource/reapi/bindings/go/src/test/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index eca213cc7..2f835e27d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -173,6 +173,11 @@ if(ENABLE_COVERAGE) SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${COVERAGE_FLAGS}" ) endif() +if(DEFINED ENV{WITH_GO}) + message(STATUS "WITH_GO detected in main CMakeLists.txt to build go bindings") + include(GolangSimple) +endif() + include_directories(.) add_subdirectory( etc ) add_subdirectory( src ) diff --git a/README.md b/README.md index 0436d5a73..072892759 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,30 @@ make check make install ``` +To build go bindings, you will need go (tested with 1.19.10) available, and then: + +```bash +export WITH_GO=yes +./configure +make +``` + +To run just one test, you can cd into t + +```bash +$ ./t9001-golang-basic.t +ok 1 - match allocate 1 slot: 1 socket: 1 core (pol=default) +ok 2 - match allocate 2 slots: 2 sockets: 5 cores 1 gpu 6 memory +# passed all 2 test(s) +1..2 +``` + +To run full tests (more robust and mimics what happens in CI) you can do: + +```bash +make check +``` + ##### Flux Instance The examples below walk through exercising functioning flux-sched modules (i.e., diff --git a/cmake/GolangSimple.cmake b/cmake/GolangSimple.cmake new file mode 100644 index 000000000..568aa6305 --- /dev/null +++ b/cmake/GolangSimple.cmake @@ -0,0 +1,30 @@ +set(CUSTOM_GO_PATH "${CMAKE_SOURCE_DIR}/resource/reapi/bindings/go") +set(GOPATH "${CMAKE_CURRENT_BINARY_DIR}/go") + +# This probably isn't necessary, although if we could build fluxcli into it maybe +file(MAKE_DIRECTORY ${GOPATH}) + +# ADD_GO_INSTALLABLE_PROGRAM builds a custom go program (primarily for testing) +function(BUILD_GO_PROGRAM NAME MAIN_SRC CGO_CFLAGS CGO_LIBRARY_FLAGS) + message(STATUS "GOPATH: ${GOPATH}") + message(STATUS "CGO_LDFLAGS: ${CGO_LIBRARY_FLAGS}") + get_filename_component(MAIN_SRC_ABS ${MAIN_SRC} ABSOLUTE) + add_custom_target(${NAME}) + + # IMPORTANT: the trick to getting *spaces* to render in COMMAND is to convert them to ";" + # string(REPLACE ...) + STRING(REPLACE " " ";" CGO_LIBRARY_FLAGS ${CGO_LIBRARY_FLAGS}) + add_custom_command(TARGET ${NAME} + COMMAND GOPATH=${GOPATH}:${CUSTOM_GO_PATH} GOOS=linux G0111MODULE=off CGO_CFLAGS="${CGO_CFLAGS}" CGO_LDFLAGS='${CGO_LIBRARY_FLAGS}' go build -ldflags '-w' + -o "${CMAKE_CURRENT_SOURCE_DIR}/${NAME}" + ${CMAKE_GO_FLAGS} ${MAIN_SRC} + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + DEPENDS ${MAIN_SRC_ABS} + COMMENT "Building Go library") + foreach(DEP ${ARGN}) + add_dependencies(${NAME} ${DEP}) + endforeach() + + add_custom_target(${NAME}_all ALL DEPENDS ${NAME}) + install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${NAME} DESTINATION bin) +endfunction(BUILD_GO_PROGRAM) diff --git a/resource/reapi/bindings/CMakeLists.txt b/resource/reapi/bindings/CMakeLists.txt index c478213aa..ad00f0264 100644 --- a/resource/reapi/bindings/CMakeLists.txt +++ b/resource/reapi/bindings/CMakeLists.txt @@ -18,3 +18,8 @@ add_library ( reapi_module STATIC target_link_libraries(reapi_module PRIVATE flux::core ) + +if(DEFINED ENV{WITH_GO}) + message(STATUS "WITH_GO is set to build go bindings") + add_subdirectory( go ) +endif() diff --git a/resource/reapi/bindings/go/CMakeLists.txt b/resource/reapi/bindings/go/CMakeLists.txt new file mode 100644 index 000000000..d4fb65825 --- /dev/null +++ b/resource/reapi/bindings/go/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory( src ) diff --git a/resource/reapi/bindings/go/src/CMakeLists.txt b/resource/reapi/bindings/go/src/CMakeLists.txt new file mode 100644 index 000000000..c2f6d9df4 --- /dev/null +++ b/resource/reapi/bindings/go/src/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory( test ) diff --git a/resource/reapi/bindings/go/src/test/CMakeLists.txt b/resource/reapi/bindings/go/src/test/CMakeLists.txt new file mode 100644 index 000000000..9c0a87b8f --- /dev/null +++ b/resource/reapi/bindings/go/src/test/CMakeLists.txt @@ -0,0 +1,17 @@ +set(TARGET main) +set(SRCS main.go) +set(CGO_CFLAGS "-I${CMAKE_BINARY_DIR}/resource/reapi/bindings/c") + +# This is currently passed but not used because when passed into add_custom_command the spaces are escaped +set(CGO_LIBRARY_FLAGS "-Wl,-R ${CMAKE_BINARY_DIR}/resource -L${CMAKE_BINARY_DIR}/resource/reapi/bindings -L${CMAKE_BINARY_DIR}/resource/libjobspec -ljobspec_conv -lreapi_cli -L${CMAKE_BINARY_DIR}/resource -lresource -lflux-idset -lstdc++ -lczmq -ljansson -lhwloc -lboost_system -lflux-hostlist -lboost_graph -lyaml-cpp") + +# This ensures the main binary is cleaned up +set_directory_properties( + PROPERTIES + ADDITIONAL_CLEAN_FILES "${CMAKE_CURRENT_BINARY_DIR}/main" + ) + +# GO_GET(go_redis github.com/hoisie/redis) +# main main.go +# We add the dependencies at the end so the build can run in parallel and we don't try to build this before they are ready! +BUILD_GO_PROGRAM(${TARGET} ${SRCS} "${CGO_CFLAGS}" "${CGO_LIBRARY_FLAGS}" jobspec_conv reapi_module reapi_cli resource flux::core)