title: Building Python submodules from C++ extensions via cmake
body: <p>I'm trying to incorporate a c++ extension as a submodule into an existing python library via cmake. Building the C++ extension works fine and importing it as a python module works, but not as the submodule of the header library.</p>
  
  <p>I have the following directory structure:</p>
  
  <pre><code>frontend/
     foo.py
     bar.py
     backend/
        backend.cpp
  </code></pre>
  
  <p>The extension is bound to a python module via pybind:</p>
  
  <pre><code>PYBIND11_MODULE(backend, m)
  {
      m.doc() = "backend c++ implementation"; // optional module docstring
      m.def("method", &amp;method, "The method I want to call from python.");
  }
  </code></pre>
  
  <p>In the CMakeLists.txt, the relevant line is:</p>
  
  <pre><code>pybind11_add_module(backend "frontend/backend/backend.cpp")
  </code></pre>
  
  <p>I've followed the instructions form <a href="https://stackoverflow.com/questions/42048421/how-to-package-a-python-c-extension-such-that-it-is-a-submodule-of-a-normal-pyth">here</a> and <a href="https://stackoverflow.com/questions/42585210/extending-setuptools-extension-to-use-cmake-in-setup-py">here</a> to write the setup.py script. I guess the most important lines look like this:</p>
  
  <pre><code>from setuptools import setup, Extension, find_packages
  from setuptools.command.build_ext import build_ext
  from setuptools.command.test import test as TestCommand
  
  class CMakeExtension(Extension):
      def __init__(self, name, sourcedir=".", sources=[]):
          Extension.__init__(self, name, sources=[])
  
  
  class CMakeBuild(build_ext):
      def run(self):
          build_directory = os.path.abspath(self.build_temp)
          if not os.path.exists(self.build_temp):
              os.makedirs(self.build_temp)
  
          cmake_list_dir = os.path.abspath(os.path.dirname(__file__))
          print("-" * 10, "Running CMake prepare", "-" * 40)
          subprocess.check_call(
              ["cmake", cmake_list_dir], cwd=self.build_temp,
          )
  
          print("-" * 10, "Building extensions", "-" * 40)
          cmake_cmd = ["cmake", "--build", "."] + self.build_args
          subprocess.check_call(cmake_cmd, cwd=self.build_temp)
  
          # Move from build temp to final position
          for ext in self.extensions:
              self.move_output(ext)
  
      def move_output(self, ext):
          build_temp = Path(self.build_temp).resolve()
          dest_path = Path(self.get_ext_fullpath(ext.name)).resolve()
          source_path = build_temp / self.get_ext_filename(ext.name)
          dest_directory = dest_path.parents[0]
          dest_directory.mkdir(parents=True, exist_ok=True)
          self.copy_file(source_path, dest_path)
  
  
  extensions = [CMakeExtension("backend")]
  
  setup(
      name="frontend",
      packages=["frontend"],
      ext_modules=extensions,
      cmdclass=dict(build_ext=CMakeBuild),
  )
  </code></pre>
  
  <p>But this does not make <code>backend</code> a submodule of <code>frontend</code>, but instead a module on its own. So this works:</p>
  
  <pre><code>from backend import method
  </code></pre>
  
  <p>But to avoid naming issues with other libraries, what I would like to have is this:</p>
  
  <pre><code>from frontend.backend import method
  </code></pre>
  
  <p>Changing the naming in the pybinding or in the extension call to <code>extensions = [CMakeExtension("frontend.backend")]</code> does unfortunately not resolve my problem, the setup does not find the <code>backend.&lt;platform&gt;.so</code> shared library then, because it looks for <code>frontend/backend.&lt;platform&gt;.so</code>, which does not exist. How could I resolve this issue?</p>