gpt4 book ai didi

python - 从 Cmake 的 find_program 中保存可执行文件,以便它可以由 Python 运行

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:57 27 4
gpt4 key购买 nike

我正在尝试结合使用 C++ 和 Python,为此我正在使用 CMake。在 CMakeLists.txt 中,我试图找到一个程序:

# Check for Gmsh executable
find_program (GMSH gmsh [/Applications/Gmsh.app/Contents/MacOS/ /usr/bin/ /usr/local/bin/])

if(GMSH)
message(STATUS "Found Gmsh in: ${GMSH}")
set(HAVE_GMSH YES)
else()
set(HAVE_GMSH NO)
endif()
message(STATUS "Setting HAVE_GMSH to: ${HAVE_GMSH}")

所以这能够毫无问题地找到可执行文件。但是我必须以某种方式将它保存在某个地方,以便以后可以从 Python 中调用它。这应该在配置文件中完成吗?执行此操作的正确方法是什么?

更新

我创建了一个包含以下内容的文件 ProjectConfig.cmake.in:

# Compute paths
get_filename_component(PROJECT_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(PROJECT_INCLUDE_DIRS "@CONF_INCLUDE_DIRS@")

# Our library dependencies (contains definitions for IMPORTED targets)
include("${PROJECT_CMAKE_DIR}/ProjectTargets.cmake")

# These are IMPORTED targets created by ProjectTargets.cmake
set(PROJECT_LIBRARIES core)
set(PROJECT_EXECUTABLE bar)

#cmakedefine HAVE_GMSH @HAVE_GMSH@
#cmakedefine GMSH @GMSH@

CMakeLists.txt 文件如下所示:

cmake_minimum_required(VERSION 3.1)
project(TestProject)
enable_language(CXX)


# Options

# Offer the user the choice of overriding the installation directories
set(INSTALL_LIB_DIR lib CACHE PATH "Installation directory for libraries")
set(INSTALL_BIN_DIR bin CACHE PATH "Installation directory for executables")
set(INSTALL_INCLUDE_DIR include CACHE PATH
"Installation directory for header files")
if(WIN32 AND NOT CYGWIN)
set(DEF_INSTALL_CMAKE_DIR CMake)
else()
set(DEF_INSTALL_CMAKE_DIR lib/CMake/Project)
endif()
set(INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH
"Installation directory for CMake files")

# Make relative paths absolute (needed later on)
foreach(p LIB BIN INCLUDE CMAKE)
set(var INSTALL_${p}_DIR)
if(NOT IS_ABSOLUTE "${${var}}")
set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
endif()
endforeach()

# Check for Gmsh executable
find_program (GMSH gmsh [/Applications/Gmsh.app/Contents/MacOS/ /usr/bin/ /usr/local/bin/])

if(GMSH)
message(STATUS "Found Gmsh in: ${GMSH}")
set(HAVE_GMSH YES)
else()
set(HAVE_GMSH NO)
endif()
message(STATUS "Setting HAVE_GMSH to: ${HAVE_GMSH}")

# Python support
#

# find Python
find_package(PythonInterp)

# find SWIG
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

find_package(PythonLibs)
include_directories(${PYTHON_INCLUDE_PATH})

message(STATUS "PYTHON_INCLUDE_PATH: ${PYTHON_INCLUDE_PATH}")
message(STATUS "PYTHON_LIBRARIES: ${PYTHON_LIBRARIES}")

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/core)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/core/fem)

#set(CMAKE_SWIG_OUTDIR ${PROJECT_BINARY_DIR}/../lib/project)

SET_SOURCE_FILES_PROPERTIES(swig/core.i PROPERTIES CPLUSPLUS ON)
set_source_files_properties(swig/core.i SWIG_FLAGS "-includeall;-c++;-shadow")
swig_add_module(core python swig/core.i core/foo.cpp)
swig_link_libraries(core project_core ${PYTHON_LIBRARIES})

################################################################################
# Directories
#

# set up include-directories
include_directories(
"${PROJECT_SOURCE_DIR}" # to find core/foo.hpp
"${PROJECT_BINARY_DIR}") # to find core/config.hpp

# Add sub-directories
add_subdirectory(core)
add_subdirectory(bar)

# The interesting stuff goes here
# ===============================

# Add all targets to the build-tree export set
export(TARGETS project_core bar
FILE "${PROJECT_BINARY_DIR}/ProjectTargets.cmake")

# Export the package for use from the build-tree
# (this registers the build-tree with a global CMake-registry)
export(PACKAGE Project)

# Create the ProjectConfig.cmake and ProjectConfigVersion files
file(RELATIVE_PATH REL_INCLUDE_DIR "${INSTALL_CMAKE_DIR}"
"${INSTALL_INCLUDE_DIR}")
# ... for the build tree
set(CONF_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}" "${PROJECT_BINARY_DIR}")
configure_file(ProjectConfig.cmake.in
"${PROJECT_BINARY_DIR}/ProjectConfig.cmake" @ONLY)
# ... for the install treew
set(CONF_INCLUDE_DIRS "\${PROJECT_CMAKE_DIR}/${REL_INCLUDE_DIR}")
configure_file(ProjectConfig.cmake.in
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/ProjectConfig.cmake" @ONLY)
# ... for both
configure_file(ProjectConfigVersion.cmake.in
"${PROJECT_BINARY_DIR}/ProjectConfigVersion.cmake" @ONLY)

# Install the ProjectConfig.cmake and ProjectConfigVersion.cmake
install(FILES
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/ProjectConfig.cmake"
"${PROJECT_BINARY_DIR}/ProjectConfigVersion.cmake"
DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev)

# Install the export set for use with the install-tree
install(EXPORT ProjectTargets DESTINATION
"${INSTALL_CMAKE_DIR}" COMPONENT dev)

现在我不确定生成的配置文件的位置(在 ${PROJECT_BINARY_DIR} 中)是否是放置它的正确位置。我应该把它放在整个层次结构的根部吗?这是一个大项目,其结构如下所示:

├── AUTHORS.rst
├── CHANGELOG.rst
├── CONTRIBUTING.rst
├── LICENSE
├── MANIFEST.in
├── Makefile
├── README.rst
├── core
│   ├── CMakeLists.txt
│   ├── ProjectConfig.cmake.in
│   ├── ProjectConfigVersion.cmake.in
│   ├── Makefile
│   ├── bar
│   ├── core
│   └── swig
├── setup.cfg
├── setup.py
├── src
│   └── project
├── tests
│   └── unit
└── tox.ini

最佳答案

通常的方法是使用configure_file在其中搜索和替换 CMake 变量。例如,假设您有一个名为 foo.h.in 的头文件:

#define HAVE_GMSH ${HAVE_GMSH}

然后在 CMake 中调用

configure_file(foo.h.in ${CMAKE_BINARY_DIR}/foo.h)

然后它将替换该文件中的所有 CMake 变量。通常的表示法是使用 0 表示“否”,使用 1 表示"is"。因此,如果您设置(HAVE_GMSH 1),它将扩展为

#define HAVE_GMSH 1

然后您必须确保包含输出文件所在的目录。例如 target_include_directories(mytarget ${CMAKE_BINARY_DIR})(当然,将生成的包含文件放在子目录。但最好不要将输出文件放在源目录中)。

CMake 文档提到您还可以使用特殊的 @VARS@ 进行扩展。在这种情况下,使用 @ONLYconfigure_file。它还提供了#cmakedefine

您可以对 Python 文件使用相同的方法。许多项目在 CMake 变量中定义作者、版本等,然后提供一个 setup.py.in 文件,该文件被搜索并替换为 configure_file

关于python - 从 Cmake 的 find_program 中保存可执行文件,以便它可以由 Python 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42852549/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com