|
| 1 | +cmake_minimum_required(VERSION 3.16) |
| 2 | +# TODO: Get version of wgpu-native and set that up correctly here. |
| 3 | +project(wgpu_native LANGUAGES C) |
| 4 | + |
| 5 | +set(WGPU_NATIVE_USER_CARGO_BUILD_OPTIONS "" CACHE STRING "Additional cargo flags (such as --features) to apply to the build command") |
| 6 | +option(BUILD_SHARED_LIBS "Build using shared libraries" OFF) |
| 7 | +option(WGPU_NATIVE_ALWAYS_BUILD "If cmake should always invoke cargo to build wgpu_native" ON) |
| 8 | + |
| 9 | +# TODO: What to do about RelWithDebInfo? |
| 10 | +if(CMAKE_BUILD_TYPE STREQUAL "Release") |
| 11 | + set(WGPU_NATIVE_BUILD_TYPE_FLAG "--release") |
| 12 | + set(WGPU_NATIVE_BUILD_TYPE "release") |
| 13 | +else() |
| 14 | + set(WGPU_NATIVE_BUILD_TYPE "debug") |
| 15 | +endif() |
| 16 | + |
| 17 | +if(DEFINED ENV{CARGO_BUILD_TARGET}) |
| 18 | + set(WGPU_NATIVE_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/target/$ENV{CARGO_BUILD_TARGET}/${WGPU_NATIVE_BUILD_TYPE}) |
| 19 | +else() |
| 20 | + set(WGPU_NATIVE_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/target/${WGPU_NATIVE_BUILD_TYPE}) |
| 21 | +endif() |
| 22 | + |
| 23 | +if(ANDROID) |
| 24 | + if(ANDROID_ABI STREQUAL "armeabi-v7a") |
| 25 | + set(ANDROID_TARGET "armv7-linux-androideabi") |
| 26 | + set(ANDROID_ARCH_SHORT "arm") |
| 27 | + elseif(ANDROID_ABI STREQUAL "arm64-v8a") |
| 28 | + set(ANDROID_TARGET "aarch64-linux-android") |
| 29 | + set(ANDROID_ARCH_SHORT "aarch64") |
| 30 | + elseif(ANDROID_ABI STREQUAL "x86") |
| 31 | + set(ANDROID_TARGET "i686-linux-android") |
| 32 | + set(ANDROID_ARCH_SHORT "i386") |
| 33 | + elseif(ANDROID_ABI STREQUAL "x86_64") |
| 34 | + set(ANDROID_TARGET "x86_64-linux-android") |
| 35 | + set(ANDROID_ARCH_SHORT "x86_64") |
| 36 | + endif() |
| 37 | + |
| 38 | + set(WGPU_NATIVE_BUILD_TARGET "--target=${ANDROID_TARGET}") |
| 39 | + # TODO: Is this needed given CARGO_BUILD_TARGET above? |
| 40 | + set(WGPU_NATIVE_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/target/${ANDROID_TARGET}/${WGPU_NATIVE_BUILD_TYPE}) |
| 41 | + |
| 42 | + if(BUILD_SHARED_LIBS) |
| 43 | + # TODO: Is this actually right? |
| 44 | + message(FATAL_ERROR "Wasmtime cannot be built with BUILD_SHARED_LIBS on Android") |
| 45 | + endif() |
| 46 | +endif() |
| 47 | + |
| 48 | +# TODO: Add mapping for iOS / etc builds to the right rust targets as well. |
| 49 | + |
| 50 | +if(BUILD_SHARED_LIBS) |
| 51 | + if(WIN32) |
| 52 | + # TODO: Is this actually right? |
| 53 | + set(WGPU_NATIVE_BUILD_PRODUCT ${WGPU_NATIVE_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}wgpu_native.dll.lib) |
| 54 | + else() |
| 55 | + set(WGPU_NATIVE_BUILD_PRODUCT ${WGPU_NATIVE_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}wgpu_native${CMAKE_SHARED_LIBRARY_SUFFIX}) |
| 56 | + endif() |
| 57 | +else() |
| 58 | + set(WGPU_NATIVE_BUILD_PRODUCT ${WGPU_NATIVE_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}wgpu_native${CMAKE_STATIC_LIBRARY_SUFFIX}) |
| 59 | +endif() |
| 60 | + |
| 61 | +if (WIN32) |
| 62 | + set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32) |
| 63 | +elseif(UNIX AND NOT APPLE) |
| 64 | + set(OS_LIBRARIES "-lm -ldl") |
| 65 | +elseif(APPLE) |
| 66 | + set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal") |
| 67 | +endif() |
| 68 | + |
| 69 | +find_program(WGPU_NATIVE_CARGO_BINARY cargo) |
| 70 | + |
| 71 | +# TODO: Use Corrosion instead? |
| 72 | +include(ExternalProject) |
| 73 | +ExternalProject_Add( |
| 74 | + wgpu_native-crate |
| 75 | + DOWNLOAD_COMMAND "" |
| 76 | + CONFIGURE_COMMAND "" |
| 77 | + BUILD_COMMAND ${WGPU_NATIVE_PREBUILD_COMMAND} ${WGPU_NATIVE_CARGO_BINARY} build ${WGPU_NATIVE_BUILD_TYPE_FLAG} ${WGPU_NATIVE_USER_CARGO_BUILD_OPTIONS} ${WGPU_NATIVE_BUILD_TARGET} |
| 78 | + INSTALL_COMMAND "" |
| 79 | + BUILD_ALWAYS ${WGPU_NATIVE_ALWAYS_BUILD} |
| 80 | + BUILD_BYPRODUCTS ${WGPU_NATIVE_BUILD_PRODUCT}) |
| 81 | + |
| 82 | +add_library(wgpu_native INTERFACE) |
| 83 | +# TODO: How should we really name these? I don't know that the _native suffix adds any value for anyone. |
| 84 | +add_library(wgpu::wgpu ALIAS wgpu_native) |
| 85 | +add_dependencies(wgpu_native wgpu_native-crate) |
| 86 | +target_link_libraries(wgpu_native INTERFACE ${WGPU_NATIVE_BUILD_PRODUCT} ${OS_LIBRARIES}) |
| 87 | + |
| 88 | +if(BUILD_SHARED_LIBS) |
| 89 | + target_compile_definitions(wgpu_native INTERFACE WGPU_SHARED_LIBRARY) |
| 90 | +endif() |
| 91 | + |
| 92 | +target_include_directories(wgpu_native INTERFACE |
| 93 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ffi> |
| 94 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ffi/webgpu-headers> |
| 95 | + $<INSTALL_INTERFACE:include/webgpu>) |
| 96 | + |
| 97 | +export(TARGETS wgpu_native NAMESPACE wgpu:: |
| 98 | + FILE "${CMAKE_BINARY_DIR}/lib/cmake/wgpu_native/WgpuNativeTargets.cmake") |
| 99 | + |
| 100 | +# TODO: install(EXPORTS ...) |
| 101 | +include(GNUInstallDirs) |
| 102 | +install( |
| 103 | + FILES |
| 104 | + ${CMAKE_CURRENT_SOURCE_DIR}/ffi/wgpu.h |
| 105 | + ${CMAKE_CURRENT_SOURCE_DIR}/ffi/webgpu-headers/webgpu.h |
| 106 | + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/webgpu) |
| 107 | +install(FILES ${WGPU_NATIVE_BUILD_PRODUCT} |
| 108 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}) |
0 commit comments