使用 CMake 构建系统的子目录中 halide_library 的可见性

The visibility of halide_library in subdirectory using CMake building system

Halide 库似乎是使用以下方法创建的:

halide_library(xxx SRCS xxx_generator.cpp)
子目录中的

in CMakeLists.txt 对其父目录或并行目录不可见。

例如,我有一个使用 CMake 的 Halide 项目,如下所示:

├── A
│   ├── a_generator.cpp
│   ├── a_run_generator.cpp
│   └── CMakeLists.txt
├── B
│   ├── b_run_generator.cpp
│   └── CMakeLists.txt
└── CMakeLists.txt

在子文件夹 A 的 CMakeLists.txt 中,我有

halide_library(a_filter SRCS a_generator.cpp)

我想 link "a_filter" 库到子文件夹 B 中的 "b_run_generator.cpp",如下所示:

add_executable(b_run_generator b_run_generator.cpp)
target_link_library(b_run_generator PRIVATE a_filter)

CMake 抱怨找不到 "a_filter"。

如果我link到"a_filter"的显式路径,就会出现很多这样的错误:

/usr/bin/ld: ../genfiles/a/a.a(a.a.o): in function `a':
halide_buffer_t.cpp:(.text.a+0x82f): undefined reference to `halide_error_buffer_argument_is_null'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0x8a3): undefined reference to `halide_error_buffer_allocation_too_large'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0x8be): undefined reference to `halide_error_buffer_allocation_too_large'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0x8d6): undefined reference to `halide_error_buffer_extents_too_large'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0x8f1): undefined reference to `halide_error_buffer_allocation_too_large'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0x90c): undefined reference to `halide_error_buffer_allocation_too_large'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0x927): undefined reference to `halide_error_buffer_extents_too_large'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0x943): undefined reference to `halide_error_device_dirty_with_no_device_support'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0x95f): undefined reference to `halide_error_host_is_null'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0x986): undefined reference to `halide_error_bad_type'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0x9a2): undefined reference to `halide_error_bad_dimensions'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0x9cc): undefined reference to `halide_error_access_out_of_bounds'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0x9e4): undefined reference to `halide_error_buffer_extents_negative'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0xa0e): undefined reference to `halide_error_access_out_of_bounds'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0xa2b): undefined reference to `halide_error_buffer_extents_negative'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0xa45): undefined reference to `halide_error_buffer_extents_negative'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0xa5f): undefined reference to `halide_error_buffer_extents_negative'
/usr/bin/ld: halide_buffer_t.cpp:(.text.a+0xa99): undefined reference to `halide_error_constraint_violated'
clang-9: error: linker command failed with exit code 1 (use -v to see invocation)

所以我的问题是,如何使在子目录中创建的卤化物库对其父目录或并行目录可见?

函数 halide_library 实际上 creates 一个 IMPORTED 库,默认情况下仅在当前范围或以下范围内可见:

# Inside 'halide_library_from_generator' function which is called from 'halide_library'
# ...
add_library("${BASENAME}" STATIC IMPORTED)

虽然正常 add_library(IMPORTED) 接受 GLOBAL 关键字以使其全局可见,但 halide_library 不接受此关键字。

您可以 "wrap" 使用 halide_library 创建的目标,因此生成的目标将全局可见:

halide_library(a_filter SRCS a_generator.cpp)
# Create globally visible wrapper target 'a_filter_global'.
add_library(a_filter_global INTERFACE)
target_link_libraries(a_filter_global INTERFACE a_filter)

然后使用包装器目标:

add_executable(b_run_generator b_run_generator.cpp)
target_link_library(b_run_generator PRIVATE a_filter_global)

从版本 3.11 开始,CMake 允许为 IMPORTED 目标设置 IMPORTED_GLOBAL 属性 以使其全局可见:

halide_library(a_filter SRCS a_generator.cpp)
# Make the target globally visible.
set_target_properties(a_filter PROPERTIES IMPORTED_GLOBAL TRUE)

请注意,设置 属性 应在使用 halide_library 创建目标的同一目录中执行。