{"action":"opened","issue":{"url":"https://api.github.com/repos/pybind/pybind11/issues/748","repository_url":"https://api.github.com/repos/pybind/pybind11","labels_url":"https://api.github.com/repos/pybind/pybind11/issues/748/labels{/name}","comments_url":"https://api.github.com/repos/pybind/pybind11/issues/748/comments","events_url":"https://api.github.com/repos/pybind/pybind11/issues/748/events","html_url":"https://github.com/pybind/pybind11/issues/748","id":215454350,"number":748,"title":"Targeting multiple Python version at once","user":{"login":"thorink","id":1706821,"avatar_url":"https://avatars2.githubusercontent.com/u/1706821?v=3","gravatar_id":"","url":"https://api.github.com/users/thorink","html_url":"https://github.com/thorink","followers_url":"https://api.github.com/users/thorink/followers","following_url":"https://api.github.com/users/thorink/following{/other_user}","gists_url":"https://api.github.com/users/thorink/gists{/gist_id}","starred_url":"https://api.github.com/users/thorink/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thorink/subscriptions","organizations_url":"https://api.github.com/users/thorink/orgs","repos_url":"https://api.github.com/users/thorink/repos","events_url":"https://api.github.com/users/thorink/events{/privacy}","received_events_url":"https://api.github.com/users/thorink/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-03-20T15:19:55Z","updated_at":"2017-03-20T15:19:55Z","closed_at":null,"body":"Hi guys, I want to build a Python extension for different Python versions (2.7 and 3.5 for the moment) in one CMake run. The goal is to have multiple targets, one for each Python version.\r\n\r\nIn the main CMakeLists.txt I have the following:\r\n\r\n```cmake\r\nset(bindings_python_version 2.7)\r\nadd_subdirectory(pythonbinding pythonbindings27)\r\nset(bindings_python_version 3.5)\r\nadd_subdirectory(pythonbinding pythonbindings35)\r\n```\r\n\r\nI include my `pythonbindings` source directory two times. Inside I unset some cache variables, include pybind11 and add the extension target:\r\n\r\n```cmake\r\nunset(PYTHONINTERP_FOUND CACHE)\r\nunset(PYTHON_EXECUTABLE CACHE)\r\nunset(PYTHON_VERSION_STRING CACHE)\r\nunset(PYTHON_VERSION_MAJOR CACHE)\r\nunset(PYTHON_VERSION_MINOR CACHE)\r\nunset(PYTHON_VERSION_PATCH CACHE)\r\n\r\nfind_package(PythonInterp ${preonpy_python_version})\r\n\r\nunset(PYBIND11_INCLUDE_DIR CACHE)\r\nunset(PYTHON_INCLUDE_DIRS CACHE)\r\nunset(PYTHON_LIBRARIES CACHE)\r\nunset(PYTHON_MODULE_PREFIX CACHE)\r\nunset(PYTHON_MODULE_EXTENSION CACHE)\r\n\r\nunset(PYTHON_LIBRARY CACHE)\r\n\r\nset(PYBIND11_PYTHON_VERSION ${preonpy_python_version} CACHE STRING \"\" FORCE)\r\n\r\nadd_subdirectory(pybind11 ${CMAKE_CURRENT_BINARY_DIR}/pybind11)\r\n\r\nset(_target_name \"_pyexttarget${bindings_python_version}\")\r\n\r\nadd_library(${_target_name} MODULE main.cpp)\r\ntarget_include_directories(${_target_name} PRIVATE ${PYBIND11_INCLUDE_DIR})\r\ntarget_include_directories(${_target_name} PRIVATE ${PYTHON_INCLUDE_DIRS})\r\nset_target_properties(${_target_name} PROPERTIES PREFIX \"${PYTHON_MODULE_PREFIX}\"\r\n                                                 SUFFIX \"${PYTHON_MODULE_EXTENSION}\"\r\n                                                 OUTPUT_NAME \"_pyexttarget\")\r\n```\r\n\r\nFor this to work I changed some code in the CMakeLists.txt of pybind11:\r\n\r\n```cmake\r\nif (NOT (CMAKE_VERSION VERSION_LESS 3.0))  # CMake >= 3.0\r\n    set(target_name \"module${PYBIND11_PYTHON_VERSION}\")\r\n    # Build an interface library target:\r\n    add_library(${target_name} INTERFACE)\r\n    target_include_directories(${target_name} INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}>\r\n            $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>\r\n            $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)\r\n    if (WIN32 OR CYGWIN)\r\n        target_link_libraries(${target_name} INTERFACE $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)\r\n    elseif (APPLE)\r\n        target_link_libraries(${target_name} INTERFACE \"-undefined dynamic_lookup\")\r\n    endif ()\r\n    target_compile_options(${target_name} INTERFACE $<BUILD_INTERFACE:${PYBIND11_CPP_STANDARD}>)\r\n\r\n    add_library(pybind11::module ALIAS ${target_name})  # to match exported target\r\nendif ()\r\n```\r\n\r\nInstead of creating a target named `module` I name it `module2.7` or `module3.5`, depending on the targeted Python version.\r\n\r\nThis allows me to build my extension two times with a single `make`.\r\n\r\nIt now works for me, but actually I don't like it very much. I would like to have your opinions on this. Do you know some better ideas to archive the same? Would you like to have support for this use case in pybind11? \r\n\r\n"}}
{"actor":{"display_login":"thorink"}}