构建步骤自检#

当您遇到特定 Python 扩展模块或其他构建目标的问题时,有许多方法可以弄清楚构建系统到底在做什么。除了查看目标的 meson.build 内容外,还包括以下方法:

  1. 阅读构建目录中生成的 build.ninja 文件;

  2. 使用 meson introspect 了解有关目标的构建选项、依赖关系和使用的标志的更多信息;

  3. 阅读 <build-dir>/meson-info/*.json 以了解有关发现的依赖关系、Meson 计划将文件安装到的位置等的详细信息。

这些东西都在构建配置阶段(即 meson setup)运行后可用。通常,查看这些信息比运行构建并阅读完整的构建日志更有效。

ninja.build 文件#

例如,假设我们对 scipy.linalg._decomp_update 感兴趣。从 scipy/linalg/meson.build 中,我们了解到该扩展是用模板化的 Cython 代码编写的,除了 numpy 之外,没有使用特殊的编译标志或包含目录。所以下一步是查看 build.ninja。在编辑器中打开该文件并搜索 _decomp_update。您将找到以下适用于通用规则和特定目标的规则集(注意,此代码块中的注释不在 build.ninja 中,仅在此文档部分中添加以解释正在发生的事情)

# These rules are usually not needed to understand the problem, but can be looked up at the top of the file:
rule c_COMPILER
 command = /home/username/anaconda3/envs/scipy-dev/bin/x86_64-conda-linux-gnu-cc $ARGS -MD -MQ $out -MF $DEPFILE -o $out -c $in
 deps = gcc
 depfile = $DEPFILE_UNQUOTED
 description = Compiling C object $out

rule c_LINKER
 command = /home/username/anaconda3/envs/scipy-dev/bin/x86_64-conda-linux-gnu-cc $ARGS -o $out $in $LINK_ARGS
 description = Linking target $out

# step 1: `.pyx.in` to `.pyx` code generation with Tempita
build scipy/linalg/_decomp_update.pyx: CUSTOM_COMMAND ../scipy/linalg/_decomp_update.pyx.in | ../scipy/_build_utils/tempita.py /home/username/anaconda3/envs/scipy-dev/bin/python3.10
 COMMAND = /home/username/anaconda3/envs/scipy-dev/bin/python3.10 ../scipy/_build_utils/tempita.py ../scipy/linalg/_decomp_update.pyx.in -o scipy/linalg
 description = Generating$ scipy/linalg/_decomp_update$ with$ a$ custom$ command

# step 2: `.pyx` to `.c` compilation with Cython
build scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so.p/_decomp_update.c: CUSTOM_COMMAND scipy/linalg/_decomp_update.pyx | /home/username/code/scipy/scipy/_build_utils/cythoner.py scipy/__init__.py scipy/linalg/__init__.py scipy/linalg/cython_blas.pyx
 DESC = Generating$ 'scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so.p/_decomp_update.c'.
 COMMAND = /home/username/anaconda3/envs/scipy-dev/bin/python3.10 /home/username/code/scipy/scipy/_build_utils/cythoner.py scipy/linalg/_decomp_update.pyx scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so.p/_decomp_update.c

# step 3: use C compiler to go from `.c` to object file (`.o`)
build scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so.p/meson-generated__decomp_update.c.o: c_COMPILER scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so.p/_decomp_update.c
 DEPFILE = scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so.p/meson-generated__decomp_update.c.o.d
 DEPFILE_UNQUOTED = scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so.p/meson-generated__decomp_update.c.o.d
 ARGS = -Iscipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so.p -Iscipy/linalg -I../scipy/linalg -I/home/username/anaconda3/envs/scipy-dev/lib/python3.10/site-packages/numpy/core/include -I/home/username/anaconda3/envs/scipy-dev/include/python3.10 -fvisibility=hidden -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -std=c99 -O2 -g -Wno-unused-but-set-variable -Wno-unused-function -Wno-conversion -Wno-misleading-indentation -fPIC -Wno-cpp

# step 4: generate a symbol file (uses `meson --internal symbolextractor`); you can safely ignore this step
build scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so.p/_decomp_update.cpython-310-x86_64-linux-gnu.so.symbols: SHSYM scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so
 IMPLIB = scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so

# step 5: link the `.o` file to obtain the file extension module (`.so`)
build scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so: c_LINKER scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so.p/meson-generated__decomp_update.c.o | /home/username/anaconda3/envs/scipy-dev/x86_64-conda-linux-gnu/sysroot/lib64/libm-2.12.so /home/username/anaconda3/envs/scipy-dev/x86_64-conda-linux-gnu/sysroot/usr/lib64/libm.a
 LINK_ARGS = -L/home/username/anaconda3/envs/scipy-dev/lib -Wl,--as-needed -Wl,--allow-shlib-undefined -shared -fPIC -Wl,--start-group -lm -Wl,--end-group -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,--allow-shlib-undefined -Wl,-rpath,/home/username/anaconda3/envs/scipy-dev/lib -Wl,-rpath-link,/home/username/anaconda3/envs/scipy-dev/lib

使用 meson introspect#

如果我们想从另一个角度查看 _decomp_update,我们可以使用(例如)meson introspect --targets -i <build-dir> > targets.json 来生成可读的 JSON。在生成的文件中搜索我们感兴趣的目标,显示如下:

{
    "name": "_decomp_update",
    "id": "b4ac6f0@@_decomp_update@cus",
    "type": "custom",
    "defined_in": "/home/username/code/scipy/scipy/linalg/meson.build",
    "filename": [
        "/home/username/code/scipy/build/scipy/linalg/_decomp_update.pyx"
    ],
    "build_by_default": false,
    "target_sources": [
        {
            "language": "unknown",
            "compiler": [
                "/home/username/anaconda3/envs/scipy-dev/bin/python3.10",
                "/home/username/code/scipy/scipy/_build_utils/tempita.py",
                "@INPUT@",
                "-o",
                "@OUTDIR@"
            ],
            "parameters": [],
            "sources": [
                "/home/username/code/scipy/scipy/linalg/_decomp_update.pyx.in"
            ],
            "generated_sources": []
        }
    ],
    "extra_files": [],
    "subproject": null,
    "installed": false
},
{
    "name": "_decomp_update.cpython-310-x86_64-linux-gnu",
    "id": "b4ac6f0@@_decomp_update.cpython-310-x86_64-linux-gnu@sha",
    "type": "shared module",
    "defined_in": "/home/username/code/scipy/scipy/linalg/meson.build",
    "filename": [
        "/home/username/code/scipy/build/scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so"
    ],
    "build_by_default": true,
    "target_sources": [
        {
            "language": "c",
            "compiler": [
                "/home/username/anaconda3/envs/scipy-dev/bin/x86_64-conda-linux-gnu-cc"
            ],
            "parameters": [
                "-I/home/username/code/scipy/build/scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so.p",
                "-I/home/username/code/scipy/build/scipy/linalg",
                "-I/home/username/code/scipy/scipy/linalg",
                "-I/home/username/anaconda3/envs/scipy-dev/lib/python3.10/site-packages/numpy/core/include",
                "-I/home/username/anaconda3/envs/scipy-dev/include/python3.10",
                "-fvisibility=hidden",
                "-fdiagnostics-color=always",
                "-D_FILE_OFFSET_BITS=64",
                "-Wall",
                "-Winvalid-pch",
                "-std=c99",
                "-O2",
                "-g",
                "-Wno-unused-but-set-variable",
                "-Wno-unused-function",
                "-Wno-conversion",
                "-Wno-misleading-indentation",
                "-fPIC",
                "-Wno-cpp"
            ],
            "sources": [],
            "generated_sources": [
                "/home/username/code/scipy/build/scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so.p/_decomp_update.c"
            ]
        }
    ],
    "extra_files": [],
    "subproject": null,
    "installed": true,
    "install_filename": [
        "/home/username/code/scipy/build-install/lib/python3.10/site-packages/scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so"
    ]
},

这告诉了我们很多东西,比如将使用哪些包含目录,可以在哪里找到 Cython 生成的 C 代码,以及使用了哪些编译标志。 meson introspect --help 对完整的功能范围和如何使用它们有很好的文档。

meson-info JSON 文件#

<build-dir>/meson-info/ 中有许多不同的 JSON 文件。这些文件具有描述性名称,暗示其内容。例如,最终的 _decomp_update 扩展安装到的位置在 intro-install_plan.json 中描述(注意,这些文件没有漂亮打印,通过 JSON 格式化程序运行它们会有所帮助)

"/home/username/code/scipy/build/scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so":{
   "destination":"{py_platlib}/scipy/linalg/_decomp_update.cpython-310-x86_64-linux-gnu.so",
   "tag":"runtime"
},

我们可能也想知道配置阶段的构建发现了哪些依赖关系。因此,我们查看 intro-dependencies.json

[
   {
      "name":"python",
      "version":"3.10",
      "compile_args":[
         "-I/home/username/anaconda3/envs/scipy-dev/include/python3.10"
      ],
      "link_args":[

      ]
   },
   {
      "name":"openblas",
      "version":"0.3.20",
      "compile_args":[
         "-I/home/username/anaconda3/envs/scipy-dev/include"
      ],
      "link_args":[
         "/home/username/anaconda3/envs/scipy-dev/lib/libopenblas.so"
      ]
   },
   {
      "name":"threads",
      "version":"unknown",
      "compile_args":[
         "-pthread"
      ],
      "link_args":[
         "-pthread"
      ]
   }
]

这告诉我们我们发现了三个依赖关系。注意:numpy 和一些其他构建时依赖关系在此处缺失,因为我们(目前)没有使用内置的 dependency() Meson 命令搜索它们。