且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在CMake中使用glib-compile-resources

更新时间:2022-06-13 01:15:43

一个解决方案是使用 add_custom_command()编译您的gresources.但是首先,这是您对CMake脚本的需求的细分:

A solution to this is using add_custom_command() to compile your gresources. But first here's a breakdown of what you need for your CMake script:

  1. glib-compile-resources 作为可执行程序- find_program()
  2. 定义如何编译您的gresource- add_custom_command()
  3. 然后定义您的自定义目标- add_custom_target()
  4. 告诉我该资源是一个生成的文件- set_source_files_properties()
  5. 最后,将自定义目标作为依赖项添加到项目目标中- add_dependencies()
  1. Pull in glib-compile-resources as executable program - find_program()
  2. Define how to compile your gresource - add_custom_command()
  3. Then define your custom target - add_custom_target()
  4. Tell CMake that resource is a generated file - set_source_files_properties()
  5. Finally, add your custom target to your project target as a dependency - add_dependencies()

这是一个示例CMake脚本:

Here's a sample CMake script:

cmake_minimum_required(VERSION 3.15)

project(dummy)

# Step 1:
find_program(GLIB_COMPILE_RESOURCES NAMES glib-compile-resources REQUIRED)

set(GRESOURCE_C   test.gresource.c)
set(GRESOURCE_XML test.gresource.xml)

# Step 2:
add_custom_command(
    OUTPUT ${GRESOURCE_C}
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMAND ${GLIB_COMPILE_RESOURCES}
    ARGS
        --target=${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}
        ${GRESOURCE_XML}
    VERBATIM
    MAIN_DEPENDENCY ${GRESOURCE_XML}
    DEPENDS
        for.glade
        bar.glade
)

# Step 3:
add_custom_target(
    dummy-resource
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}
)

# Step 4:
add_executable(${PROJECT_NAME} dummy.c ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C})
set_source_files_properties(
    ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}
    PROPERTIES GENERATED TRUE
)

# Step 5:
add_dependencies(${PROJECT_NAME} dummy-resource)

简要说明

add_custom_command()

  • 输出-这是您生成的资源文件
  • WORKING_DIRECTORY -XML和Glade文件所在的位置
  • VERBATIM -确保我们的 COMMAND 收到的 ARGS 不变
  • MAIN_DEPENDENCY -用于glib-compile-resources< input-file>
  • DEPENDS -您的林间空地文件.如果文件有任何更改,则触发您的目标构建:)
  • Brief explanation

    add_custom_command()

    • OUTPUT - This is your generated resource file
    • WORKING_DIRECTORY - Where your XML and glade files are located
    • VERBATIM - Makes sure our COMMAND receives ARGS unchanged
    • MAIN_DEPENDENCY - for glib-compile-resources <input-file>
    • DEPENDS - Your glade file(s). If any of the file changes then your target build is triggered :)
      • 虚拟资源-这是您的自定义目标名称
      • DEPENDS -自定义目标触发触发自定义命令所需的输出
      • dummy-resource - That's your custom target name
      • DEPENDS - The output your custom target needs in order to trigger your custom command

      首次使用 cmake 命令生成构建文件时,尚未生成资源文件.因此CMake会出错,因为它不知道您的资源文件在哪里或从哪里来.我们需要告诉CMake不要失败,我们的资源文件会在以后生成".

      When you first generate your build files using cmake command, your resource file isn't generated yet. So CMake will run into error because it doesn't know where your resource file is or where it's coming from. We need to tell CMake "Don't fail, our resource file is generated later"

      现在您可能会注意到我们正在重复工作,即,当我们添加新的空地文件或删除现有的空地文件(或其他任何资源,例如图标,声音,css文件等)时,我们必须同时编辑XML和CMake脚本文件. glib-compile-resources 已经提供了依赖项生成,因此我们可以在CMake脚本中使用它并使其变得聪明.

      Now you might notice we are duplicating our effort ie., when we add new glade files or remove existing ones (or any other resources such as icon, sounds, css files, etc) we have to edit both our XML and CMake script files. glib-compile-resources already provide dependency generation so we can use that in our CMake script and make it smart.

      诀窍是将您的.xml文件更改为.xml.in作为配置文件.因此,当该配置文件更改时,您可以使用--generate-dependencies调用glib工具,获取新的依赖项输出值,并将其发送到 add_custom_command(... DEPENDS).现在我们有了一个智能的CMake:)

      The trick is to change your .xml file to .xml.in as a configuration file. So when that configuration file changes, you call glib tool with --generate-dependencies, get new dependency output values, and send that to add_custom_command(... DEPENDS). Now we have an intelligent CMake :)

      如果您想采用这种方法,那么下面的帖子将非常有帮助:

      If you want to approach this method then the below post would be really helpful:

      使用列表作为对add_custom_command的依赖

      祝你好运:)