Integrating Dawn in CMake project

591 views
Skip to first unread message

Guillaume Racicot

unread,
Aug 1, 2022, 12:42:20 PM8/1/22
to Dawn Graphics
Hello everyone!

As the title says, I'm trying to find a way to integrate Dawn into my CMake project. There's a couple of reasons why:

1. Dawn is not packaged in vcpkg or Conan, which are the two most popular C++ package managers
2. Dawn own CMake scripts don't export its targets and has no installation scripts, making it impossible to simply install the libs and find them
3. Dawn has many dependencies, and without exported targets a CMake script has to find all the link dependencies of Dawn and manually link them

My solution was to use Dawn with add_subdirectory or FetchContent, since I would have access to Dawn's CMake targets which allows a much simpler integration into my project.

On MacOS it was quite easy, I simply had to trick Dawn's CMake script to think all dependencies are present and disable Vulkan build. On linux on the other hand, I have to enable vulkan and my trick don't seem to work just as well.

Here's how I integrated Dawn into my buildsystem. I start with a Finddawn.cmake file:

if(NOT dawn_POPULATED)
    # I don't want to run the tests
    set(TINT_BUILD_TESTS OFF CACHE BOOL INTERNAL)
    set(TINT_LIB_FUZZING_ENGINE_LINK_OPTIONS OFF CACHE STRING INTERNAL)
    # I'll only use wgsl
    set(TINT_BUILD_SPV_READER OFF CACHE BOOL INTERNAL)
    # I want to statically link
    set(BUILD_SHARED_LIBS OFF CACHE STRING INTERNAL)
    # No need to build samples to use dawn
    set(BUILD_SAMPLES OFF CACHE BOOL INTERNAL)

    # dawn dependencies
    find_package(glslang CONFIG REQUIRED)
    find_package(SPIRV-Tools REQUIRED)
    find_package(SPIRV-Tools-opt CONFIG REQUIRED)
    add_library(SPIRV-Tools ALIAS SPIRV-Tools-static)
    find_path(SPIRV_HEADERS_INCLUDE_DIRS "spirv/1.0/GLSL.std.450.h")
    add_library(SPIRV-Headers INTERFACE IMPORTED GLOBAL)
    target_include_directories(SPIRV-Headers INTERFACE "${SPIRV_HEADERS_INCLUDE_DIRS}")

    find_path(VULKAN_HEADERS_INCLUDE_DIRS "vk_video/vulkan_video_codec_h264std.h")
    add_library(Vulkan-Headers INTERFACE IMPORTED GLOBAL)
    target_include_directories(Vulkan-Headers INTERFACE "${VULKAN_HEADERS_INCLUDE_DIRS}")

    find_package(absl CONFIG REQUIRED)
    add_library(libabsl ALIAS absl::strings)
    add_library(absl_strings ALIAS absl::strings)
    add_library(absl_str_format_internal ALIAS absl::str_format_internal)

    set(multiValueArgs "")
    FetchContent_MakeAvailable(dawn)
    unset(multiValueArgs)
endif()
set(dawn_FOUND TRUE)

I have the dependencies fetched using vcpkg:

{
  "name": "my-project",
  "version-string": "0.1.0",
  "dependencies": [
    {
      "name": "glfw3",
      "platform": "!wasm32"
    },
    {
      "name": "abseil",
      "platform": "!wasm32"
    },
    {
      "name": "glslang",
      "platform": "!wasm32"
    },
    {
      "name": "spirv-cross",
      "platform": "!wasm32"
    },
    {
      "name": "spirv-headers",
      "platform": "!wasm32"
    },
    {
      "name": "spirv-tools",
      "platform": "!wasm32"
    },
    {
      "name": "vulkan-headers",
      "platform": "!wasm32"
    }
  ]
}

Have this FetchContent command somewhere in my scripts:

FetchContent_Declare(
    dawn
    GIT_REPOSITORY https://dawn.googlesource.com/dawn
    GIT_TAG b779b2b13d1ebcd9e552d4ecf2b479dbf8801b42
)

Then finally, find the package and link to it:

find_package(dawn REQUIRED)
target_link_libraries(my-executable PRIVATE
    glfw
    dawn_utils
    dawn_internal_config
    dawn_native
    dawn_proc
    dawncpp
)

This trick worked pretty well on MacOS, but on linux I'm not sure how to adapt my Finddawn.cmake so that it works with dawn? Is there any other way I can integrate dawn into my project? What is the recommended way to statically link dawn to an executable? I haven't found any documented ways to actually use it.

I can provide any other information if needed. I can create a sample project too if that's what you need to help me. Thank you for your help.

Corentin Wallez

unread,
Aug 3, 2022, 8:47:37 AM8/3/22
to Guillaume Racicot, Dawn Graphics
Hey Guillaume,

I'm not exactly sure why you need to make a FindDawn.cmake. The idea is that you should be able to add_subdirectory(path to dawn) and have things work if you set all the CMake variables for Dawn correctly. That said, as discussed on Matrix, we're not CMake packaging experts, so if you know a better way to package Dawn or resources that explain how to do it, we could change things. In the meantime do you mind also sharing the error output you get? We might be able to help explain what's going on.

Cheers,

Corentin

--
You received this message because you are subscribed to the Google Groups "Dawn Graphics" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dawn-graphic...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dawn-graphics/6f247190-af93-4124-828b-03290f20a83en%40googlegroups.com.

Dominic Cerisano

unread,
Aug 3, 2022, 2:27:17 PM8/3/22
to Guillaume Racicot, Dawn Graphics
Hey all,

I worked on a project recently that integrated Dawn directly into V8 via NAPI and even integrated CTS for testing.
Since there were several build frameworks involved (GN, CMake, npm, etc) it made sense to use a GIT "superbuild" with each project included as a submodule. This helped separate concerns.

Also had the advantage of being able to pin each submodule at a specific commit for version control which was very helpful given the ongoing rolling nature of these projects.

Here is an older repo of the superbuild which hopefully will be informative. There is a newer version that is toward using WEB-IDL and NAPI to model and generate the Dawn web api (WebGPU) whose complexity is out of scope for this issue.

Hope that helps.


Dominic Cerisano

dcerisano@skype

NOTICE: Confidential message which may be privileged. Unauthorized use/disclosure prohibited. 
AVIS : Message confidentiel dont le contenu peut être privilégié. Utilisation/divulgation interdites sans permission. 


Reply all
Reply to author
Forward
0 new messages