Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using FetchContent to fetch libdawn.dylib from github when library not found #44

Merged
merged 9 commits into from
Aug 12, 2024
56 changes: 41 additions & 15 deletions cmake/gpu.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ if(EXISTS ${FILEPATH_CURRENT_DIR})
elseif(EXISTS ${FILEPATH_PROJECT_ROOT})
set(TARGET_FILE_PATH ${PROJECT_ROOT})
else()
message(FATAL_ERROR "File ${FILENAME} not found in either ${CMAKE_CURRENT_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR}/../../")
message(
kagurazaka-ayano marked this conversation as resolved.
Show resolved Hide resolved
FATAL_ERROR
"File ${FILENAME} not found in either ${CMAKE_CURRENT_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR}/../../"
)
endif()

# Define architecture and build type directories or file names
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(ARCH "x64")
set(ARCH "x64")
else()
set(ARCH "x86")
set(ARCH "x86")
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(BUILD_TYPE "Debug")
set(BUILD_TYPE "Debug")
else()
set(BUILD_TYPE "Release")
set(BUILD_TYPE "Release")
endif()

add_library(webgpulib SHARED IMPORTED)
Expand All @@ -38,22 +41,45 @@ add_dependencies(gpu webgpulib)
target_include_directories(gpu INTERFACE ${TARGET_FILE_PATH})

# Add headers webgpu.h
target_include_directories(wgpu INTERFACE ${TARGET_FILE_PATH}/third_party/headers)
target_include_directories(wgpu
INTERFACE ${TARGET_FILE_PATH}/third_party/headers)
if(WIN32)
set(DLL_PATH "${TARGET_FILE_PATH}/third_party/lib/libdawn_${ARCH}_${BUILD_TYPE}.dll")
set(DLL_PATH
"${TARGET_FILE_PATH}/third_party/lib/libdawn_${ARCH}_${BUILD_TYPE}.dll")
if(EXISTS ${DLL_PATH})
file(COPY ${DLL_PATH} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(webgpulib INTERFACE ${DLL_PATH})
file(COPY ${DLL_PATH} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(webgpulib INTERFACE ${DLL_PATH})
else()
message(FATAL_ERROR "libdawn dll not found at: ${DLL_PATH}")
endif()
message(FATAL_ERROR "libdawn dll not found at: ${DLL_PATH}")
endif()
else()
find_library(LIBDAWN dawn REQUIRED PATHS "${TARGET_FILE_PATH}/third_party/lib")
find_library(LIBDAWN dawn PATHS "${TARGET_FILE_PATH}/third_party/lib")
if(LIBDAWN)
message(STATUS "Found libdawn: ${LIBDAWN}")
# Link against libdawn
# Link against libdawn
target_link_libraries(webgpulib INTERFACE ${LIBDAWN})
# if not found, try download from release
else()
message(FATAL_ERROR "libdawn not found")
message("libdawn not found, try downloading from the release")
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(libdawn_ext "dylib")
elseif(UNIX)
Copy link
Contributor

@MichealReed MichealReed Aug 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will definitely fail on windows until we get artifacts hosted for windows and this to accommodate the naming convention. Biggest challenge is debug/release builds with pre-downloaded artifacts. @austinvhuang weren't you thinking about using those google built libs instead of hosting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inside this else statement the system cannot be Windows, I can add download step to windows (i.e. line 53)

set(libdawn_ext "so")
endif()
FetchContent_Declare(
libdawn
URL https://github.com/austinvhuang/dawn-artifacts/releases/download/prerelease/libdawn.${libdawn_ext}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if they're hosted yet but we should use the libdawn_${ARCH}_${BUILD_TYPE} convention unless @austinvhuang wants to migrate away from that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For windows yes but windows libraries are in another place. Under this url it's just libdawn.{so/dylib}. Maybe I can add one for Windows with the architecture and build type for windows.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we were going to structure all the libs with this sort of convention, could be mistaken, need Austin to clarify. Other OS will have debug/release/architecture differences too, so I think we wanted to capture all with a generalized approach.

DOWNLOAD_NO_EXTRACT TRUE
SOURCE_DIR "${TARGET_FILE_PATH}/third_party/lib")
FetchContent_MakeAvailable(libdawn)
find_library(LIBDAWN dawn REQUIRED
PATHS "${TARGET_FILE_PATH}/third_party/lib")
if(LIBDAWN)
message(STATUS "Found libdawn: ${LIBDAWN}")
# Link against libdawn
target_link_libraries(webgpulib INTERFACE ${LIBDAWN})
else()
message(FATAL_ERROR "libdawn not found")
endif()
endif()
endif()
endif()
Loading