From df32770b2bf70d2ea397a7b3cfe1f55537ad9a83 Mon Sep 17 00:00:00 2001 From: Mikolaj Kaczmarek Date: Sun, 6 Sep 2020 19:17:05 +0200 Subject: [PATCH 1/2] Added post_x_bindings methods for Python, Go and Julia bindings --- src/mlpack/CMakeLists.txt | 65 ++--------------------- src/mlpack/bindings/go/CMakeLists.txt | 26 ++++++++- src/mlpack/bindings/julia/CMakeLists.txt | 10 ++++ src/mlpack/bindings/python/CMakeLists.txt | 34 ++++++++++++ 4 files changed, 72 insertions(+), 63 deletions(-) diff --git a/src/mlpack/CMakeLists.txt b/src/mlpack/CMakeLists.txt index 12ff343a34..e3efa5e836 100644 --- a/src/mlpack/CMakeLists.txt +++ b/src/mlpack/CMakeLists.txt @@ -125,68 +125,9 @@ string(REGEX REPLACE ".*#define MLPACK_VERSION_PATCH [\"]?([0-9x]+)[\"]?.*" set(PACKAGE_VERSION "${MLPACK_VERSION_MAJOR}.${MLPACK_VERSION_MINOR}.${MLPACK_VERSION_PATCH}") -# If we are building Python bindings, we have to configure setup.py but only -# after we've recursed into methods/. -if (BUILDING_PYTHON_BINDINGS) - - get_property(CYTHON_INCLUDE_DIRECTORIES DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - PROPERTY INCLUDE_DIRECTORIES) - add_custom_target(python_configure - COMMAND ${CMAKE_COMMAND} - -D SETUP_PY_IN=${CMAKE_SOURCE_DIR}/src/mlpack/bindings/python/setup.py.in - -D SETUP_PY_OUT=${CMAKE_BINARY_DIR}/src/mlpack/bindings/python/setup.py - -D PACKAGE_VERSION="${PACKAGE_VERSION}" - -D Boost_SERIALIZATION_LIBRARY="${Boost_SERIALIZATION_LIBRARY_RELEASE}" - -D Boost_LIBRARY_DIRS="${Boost_LIBRARY_DIRS}" - -D ARMADILLO_LIBRARIES="${ARMADILLO_LIBRARIES}" - -D MLPACK_LIBRARY=$ - -D MLPACK_LIBDIR=$ - -D MLPACK_PYXS="${MLPACK_PYXS}" - -D OpenMP_CXX_FLAGS="${OpenMP_CXX_FLAGS}" - -D DISABLE_CFLAGS="${DISABLE_CFLAGS}" - -D CYTHON_INCLUDE_DIRECTORIES="${CYTHON_INCLUDE_DIRECTORIES}" - -D CMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} - -D OUTPUT_DIR=${CMAKE_BINARY_DIR} - -P "${CMAKE_SOURCE_DIR}/src/mlpack/bindings/python/ConfigureSetup.cmake" - BYPRODUCTS "${CMAKE_BINARY_DIR}/src/mlpack/bindings/python/setup.py" - COMMENT "Configuring setup.py...") - add_dependencies(python_configure python_copy) - add_dependencies(python_configured python_configure) - - # Append the package version to __init__.py after all the imports are loaded. - file(APPEND ${CMAKE_BINARY_DIR}/src/mlpack/bindings/python/mlpack/__init__.py - "__version__='${PACKAGE_VERSION}'\n") -endif () - -# If we are building Julia bindings, we have to end the 'module' declaration in -# mlpack.jl -if (BUILD_JULIA_BINDINGS) - file(APPEND - "${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/mlpack.jl" - "\nend\ninclude(\"functions.jl\")\ninclude(\"serialization.jl\")\nend\n") -endif () - -# If we are building Go bindings, we have to end the 'module' declaration in -# models.go -if (BUILD_GO_BINDINGS) - file(APPEND - "${CMAKE_BINARY_DIR}/src/mlpack/bindings/go/src/mlpack.org/v1/mlpack/models.go" - "*/\n" - "import \"C\"\n\n" - "import (\n" - " \"runtime\"\n" - " \"unsafe\"\n" - ")\n\n") - - include("${CMAKE_SOURCE_DIR}/CMake/go/AppendModel.cmake") - # Read list content. - get_property(MODELS GLOBAL PROPERTY GO_MODELS) - foreach (models IN LISTS MODELS) - append_model( - "${CMAKE_BINARY_DIR}/src/mlpack/bindings/go/src/mlpack.org/v1/mlpack/models.go" - ${models}) - endforeach() -endif() +post_python_bindings() +post_julia_setup() +post_go_setup() # If we are building R-bindings, we have to run some setup after we # recurse into methods/. If not, this macro is empty. diff --git a/src/mlpack/bindings/go/CMakeLists.txt b/src/mlpack/bindings/go/CMakeLists.txt index 5433acd6eb..37d14be839 100644 --- a/src/mlpack/bindings/go/CMakeLists.txt +++ b/src/mlpack/bindings/go/CMakeLists.txt @@ -9,6 +9,30 @@ macro (not_found_return message) endif() endmacro () +macro (post_go_setup) + if (BUILD_GO_BINDINGS) + # If we are building Go bindings, we have to end the 'module' declaration in + # models.go + file(APPEND + "${CMAKE_BINARY_DIR}/src/mlpack/bindings/go/src/mlpack.org/v1/mlpack/models.go" + "*/\n" + "import \"C\"\n\n" + "import (\n" + " \"runtime\"\n" + " \"unsafe\"\n" + ")\n\n") + + include("${CMAKE_SOURCE_DIR}/CMake/go/AppendModel.cmake") + # Read list content. + get_property(MODELS GLOBAL PROPERTY GO_MODELS) + foreach (models IN LISTS MODELS) + append_model( + "${CMAKE_BINARY_DIR}/src/mlpack/bindings/go/src/mlpack.org/v1/mlpack/models.go" + ${models}) + endforeach() + endif() +endmacro () + # If we are not supposed to make Go bindings, define the macro so it does # nothing and leave this file. if (NOT BUILD_GO_BINDINGS) @@ -176,7 +200,7 @@ set_property(GLOBAL PROPERTY GO_MODELS "") macro (add_go_binding name) if (BUILD_GO_BINDINGS) - # Include all .h that define model to models.go. + # Include all .h that define model to models.go. file(READ "${CMAKE_CURRENT_SOURCE_DIR}/${name}_main.cpp" MAIN_FILE) if (MAIN_FILE MATCHES "PARAM_MODEL") file(APPEND diff --git a/src/mlpack/bindings/julia/CMakeLists.txt b/src/mlpack/bindings/julia/CMakeLists.txt index 61e55ed9a9..d5ba58ab8b 100644 --- a/src/mlpack/bindings/julia/CMakeLists.txt +++ b/src/mlpack/bindings/julia/CMakeLists.txt @@ -7,6 +7,16 @@ macro (not_found_return message) return() endmacro () +macro (post_julia_setup) + # If we are building Julia bindings, we have to end the 'module' declaration in + # mlpack.jl + if (BUILD_JULIA_BINDINGS) + file(APPEND + "${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/mlpack.jl" + "\nend\ninclude(\"functions.jl\")\ninclude(\"serialization.jl\")\nend\n") + endif () +endmacro () + # If we are not supposed to make Julia bindings, define the macro so it does # nothing and leave this file. if (NOT BUILD_JULIA_BINDINGS) diff --git a/src/mlpack/bindings/python/CMakeLists.txt b/src/mlpack/bindings/python/CMakeLists.txt index 191a5eb00d..b35bd85745 100644 --- a/src/mlpack/bindings/python/CMakeLists.txt +++ b/src/mlpack/bindings/python/CMakeLists.txt @@ -7,6 +7,40 @@ macro (not_found_return message) return() endmacro () +macro (post_python_bindings) + # If we are building Python bindings, we have to configure setup.py but only + # after we've recursed into methods/. + if (BUILDING_PYTHON_BINDINGS) + get_property(CYTHON_INCLUDE_DIRECTORIES DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + PROPERTY INCLUDE_DIRECTORIES) + add_custom_target(python_configure + COMMAND ${CMAKE_COMMAND} + -D SETUP_PY_IN=${CMAKE_SOURCE_DIR}/src/mlpack/bindings/python/setup.py.in + -D SETUP_PY_OUT=${CMAKE_BINARY_DIR}/src/mlpack/bindings/python/setup.py + -D PACKAGE_VERSION="${PACKAGE_VERSION}" + -D Boost_SERIALIZATION_LIBRARY="${Boost_SERIALIZATION_LIBRARY_RELEASE}" + -D Boost_LIBRARY_DIRS="${Boost_LIBRARY_DIRS}" + -D ARMADILLO_LIBRARIES="${ARMADILLO_LIBRARIES}" + -D MLPACK_LIBRARY=$ + -D MLPACK_LIBDIR=$ + -D MLPACK_PYXS="${MLPACK_PYXS}" + -D OpenMP_CXX_FLAGS="${OpenMP_CXX_FLAGS}" + -D DISABLE_CFLAGS="${DISABLE_CFLAGS}" + -D CYTHON_INCLUDE_DIRECTORIES="${CYTHON_INCLUDE_DIRECTORIES}" + -D CMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} + -D OUTPUT_DIR=${CMAKE_BINARY_DIR} + -P "${CMAKE_SOURCE_DIR}/src/mlpack/bindings/python/ConfigureSetup.cmake" + BYPRODUCTS "${CMAKE_BINARY_DIR}/src/mlpack/bindings/python/setup.py" + COMMENT "Configuring setup.py...") + add_dependencies(python_configure python_copy) + add_dependencies(python_configured python_configure) + + # Append the package version to __init__.py after all the imports are loaded. + file(APPEND ${CMAKE_BINARY_DIR}/src/mlpack/bindings/python/mlpack/__init__.py + "__version__='${PACKAGE_VERSION}'\n") + endif () +endmacro () + # If we are not supposed to make Python bindings, define the macro so it does # nothing and leave this file. if (NOT BUILD_PYTHON_BINDINGS) From db5a82cbc2cc1e085a34c9e076339c5bbb3b06c2 Mon Sep 17 00:00:00 2001 From: Mikolaj Kaczmarek Date: Sat, 12 Sep 2020 11:31:25 +0200 Subject: [PATCH 2/2] Fixed whitespaces and one comment --- src/mlpack/bindings/go/CMakeLists.txt | 3 +-- src/mlpack/bindings/julia/CMakeLists.txt | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mlpack/bindings/go/CMakeLists.txt b/src/mlpack/bindings/go/CMakeLists.txt index 37d14be839..8d83745bdf 100644 --- a/src/mlpack/bindings/go/CMakeLists.txt +++ b/src/mlpack/bindings/go/CMakeLists.txt @@ -11,8 +11,7 @@ endmacro () macro (post_go_setup) if (BUILD_GO_BINDINGS) - # If we are building Go bindings, we have to end the 'module' declaration in - # models.go + # Once `GO_MODELS` is populated, generate `models.go`. file(APPEND "${CMAKE_BINARY_DIR}/src/mlpack/bindings/go/src/mlpack.org/v1/mlpack/models.go" "*/\n" diff --git a/src/mlpack/bindings/julia/CMakeLists.txt b/src/mlpack/bindings/julia/CMakeLists.txt index d5ba58ab8b..10fe0b944a 100644 --- a/src/mlpack/bindings/julia/CMakeLists.txt +++ b/src/mlpack/bindings/julia/CMakeLists.txt @@ -9,7 +9,7 @@ endmacro () macro (post_julia_setup) # If we are building Julia bindings, we have to end the 'module' declaration in - # mlpack.jl + # mlpack.jl. if (BUILD_JULIA_BINDINGS) file(APPEND "${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/mlpack.jl"