How to build an executable while installing in normal and editable mode

未關閉
#132 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
38/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
cmake, cpp, python
領域
build-system

研究方向

先閱讀 setup.py 和 CMakeLists.txt,然後使用 issue 中的命令和專案配置重現 pip install . 和 pip install -e .。比較兩種模式下可執行檔 helloworld 的產生位置,並追蹤現有的 CMake 輸出目錄設定。當兩種安裝模式都在適當的 Python 根路徑下建立該可執行檔時,即視為完成。

由索引模型根據 Issue 內容生成。

描述

Hello, I'm currently learning how to use CMake and pybind11 with this project. As part of the installation process, I need to build and install a C++ source file. To ensure that everything is clear, I've included the reproduction code below.

Description
  • My project structure: It's basically the same except there is my cpp source file helloworld.cpp in src/ folder.
$ tree -L 2
.
├── CMakeLists.txt
├── pybind11
├── pyproject.toml
├── setup.py
└── src
    ├── helloworld.cpp
    └── main.cpp
  • src/helloworld.cpp: A trivial helloworld program. $ cat src/helloworld.cpp
#include <iostream>

int main(int argc, char **argv) {
  std::cout << "Hello World!!!\n";
  return 0;
}
  • CMakeLists.txt: It's basically the same as CMakeLists.tx except add_executable(helloworld src/helloworld.cpp) is added to build executable.
$ cat CMakeLists.txt 
cmake_minimum_required(VERSION 3.22)
project(cmake_example)

add_executable(helloworld src/helloworld.cpp)

add_subdirectory(pybind11)
pybind11_add_module(cmake_example src/main.cpp)

target_compile_definitions(cmake_example
                           PRIVATE VERSION_INFO=${EXAMPLE_VERSION_INFO})
  • pyproject.toml:
$ cat pyproject.toml 
[build-system]
requires = ["setuptools>=62.0.0", "wheel", "cmake>=3.22"]
build-backend = "setuptools.build_meta"
  • pybind11 is a git submodule, and setup.py is exactly the same as this setup.py.
Build in normal mode.
  • Create virtual env and update pip: python3 -m venv venv && source venv/bin/activate && python3 -m pip install --upgrade pip
  • Install in normal mode: python3 -m pip install .
  • cmake_example library is installed correctly but my helloworld program is not. There is no helloworld program in venv.
$ ls venv/lib/python3.8/site-packages/
cmake_example-0.0.1.dist-info             
cmake_example.cpython-38-x86_64-linux-gnu.so
Build in editable mode.
  • Create virtual env and update pip: python3 -m venv venv && source venv/bin/activate && python3 -m pip install --upgrade pip
  • Install in editable mode: python3 -m pip install -e .
  • cmake_example library is also installed correctly but my helloworld program is not. There is no helloworld program created in the current folder.
$ ls
cmake_example.cpython-38-x86_64-linux-gnu.so  cmake_example.egg-info  CMakeLists.txt  pybind11  pyproject.toml  setup.py  src  venv
My experiment:

I actually tried to make it work by modifying setup.py with the following way.

  • Add code snippet in setup.py in Line:47.
        bindir = extdir / "bin"
        print(f"User's executables resides in {bindir}")
        cmake_args = [
            # My innovation. helloworld can be called in scripts.
            f"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY={bindir}{os.sep}",
            f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
            f"-DPYTHON_EXECUTABLE={sys.executable}",
            f"-DCMAKE_BUILD_TYPE={cfg}",  # not used on MSVC, but no harm
        ]

This approach kind of work. Now I can do pip install . and helloworld program appears in venv/lib/python3.8/site-packages/bin/helloworld. However, this approach doesn't work in editable model. I.e., I do pip install -e ., but helloworld program is no created anywhere in the project.

My question is how to achieve the following goal: I would like the helloworld program to be created automatically when I run pip install . and pip install -e ., and I want this program to be installed in the correct Python root path in both modes. Do you have any suggestions or advice on how I can make this happen? Thank you for your help!

System info:
OS: ubuntu20.04
Python: Python3.8

主要語言
C++
星號
671
分支
220
PR 合併指標
30 天內沒有已合併 PR

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

pybind/cuda_example 的其他 Issue

查看 pybind/cuda_example 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。