Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added nanobind_setup_libname helper function #858

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vfdev-5
Copy link
Contributor

@vfdev-5 vfdev-5 commented Jan 14, 2025

@wjakob
Copy link
Owner

wjakob commented Jan 15, 2025

How about giving this function the same signature as nanobind_add_module? Then the arguments can easily be forwarded, which I think is convenient. Let's call it nanobind_libname

@vfdev-5
Copy link
Contributor Author

vfdev-5 commented Jan 15, 2025

How about giving this function the same signature as nanobind_add_module? Then the arguments can easily be forwarded, which I think is convenient. Let's call it nanobind_libname

nanobind_add_module signature is just name and all other arguments are parsed with cmake_parse_arguments.
I'm not totally sure how can we forward the arguments ("STABLE_ABI;FREE_THREADED;NB_STATIC;NB_SHARED;PROTECT_STACK;LTO;NOMINSIZE;NOSTRIP;MUSL_DYNAMIC_LIBCPP") which could be parsed first by nanobind_add_module and then also by nanobind_libname.

Also parsed arguments like ARG_STABLE_ABI, ARG_NB_STATIC etc are then set to some default value depending on their mutual values:

if (ARG_NB_SHARED AND ARG_NB_STATIC)
message(FATAL_ERROR "NB_SHARED and NB_STATIC cannot be specified at the same time!")
elseif (NOT ARG_NB_SHARED)
set(ARG_NB_STATIC TRUE)
endif()
# Stable ABI builds require CPython >= 3.12 and Python::SABIModule
if ((Python_VERSION VERSION_LESS 3.12) OR
(NOT Python_INTERPRETER_ID STREQUAL "Python") OR
(NOT TARGET Python::SABIModule))
set(ARG_STABLE_ABI FALSE)
endif()
if (NB_ABI MATCHES "t")
set(ARG_STABLE_ABI FALSE)
else(ARG_STABLE_ABI)
set(ARG_FREE_THREADED FALSE)
endif()

If you have few more details on the idea how to move forward I'm all ears.

EDIT: we can otherwise do this for a bit more simple args forwarding:

function (f2 name)
    cmake_parse_arguments(PARSE_ARGV 1 ARG2 "A;B;C" "" "")

    message("2 - A: ${ARG2_A}")
    message("2 - B: ${ARG2_B}")
    message("2 - C: ${ARG2_C}")
endfunction()


function (f1 name)
    cmake_parse_arguments(PARSE_ARGV 1 ARG1 "A;B;C" "" "")

    message("1 - A: ${ARG1_A}")
    message("1 - B: ${ARG1_B}")
    message("1 - C: ${ARG1_C}")

    set(args ${name})
    if (ARG1_A)
        list(APPEND args A)
    endif()
    if (ARG1_B)
        list(APPEND args B)
    endif()
    if (ARG1_C)
        list(APPEND args C)
    endif()

    f2(${args})

endfunction()


f1("test" A B)
f2("t2" A C)

Output:

1 - A: TRUE
1 - B: TRUE
1 - C: FALSE
2 - A: TRUE
2 - B: TRUE
2 - C: FALSE
2 - A: TRUE
2 - B: FALSE
2 - C: TRUE

@wjakob wjakob force-pushed the master branch 2 times, most recently from 98def19 to 92d9cb3 Compare January 27, 2025 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants