diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..7a6ad12c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,108 @@ +cmake_minimum_required(VERSION 3.16) +# TODO: Get version of wgpu-native and set that up correctly here. +project(wgpu_native LANGUAGES C) + +set(WGPU_NATIVE_USER_CARGO_BUILD_OPTIONS "" CACHE STRING "Additional cargo flags (such as --features) to apply to the build command") +option(BUILD_SHARED_LIBS "Build using shared libraries" OFF) +option(WGPU_NATIVE_ALWAYS_BUILD "If cmake should always invoke cargo to build wgpu_native" ON) + +# TODO: What to do about RelWithDebInfo? +if(CMAKE_BUILD_TYPE STREQUAL "Release") + set(WGPU_NATIVE_BUILD_TYPE_FLAG "--release") + set(WGPU_NATIVE_BUILD_TYPE "release") +else() + set(WGPU_NATIVE_BUILD_TYPE "debug") +endif() + +if(DEFINED ENV{CARGO_BUILD_TARGET}) + set(WGPU_NATIVE_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/target/$ENV{CARGO_BUILD_TARGET}/${WGPU_NATIVE_BUILD_TYPE}) +else() + set(WGPU_NATIVE_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/target/${WGPU_NATIVE_BUILD_TYPE}) +endif() + +if(ANDROID) + if(ANDROID_ABI STREQUAL "armeabi-v7a") + set(ANDROID_TARGET "armv7-linux-androideabi") + set(ANDROID_ARCH_SHORT "arm") + elseif(ANDROID_ABI STREQUAL "arm64-v8a") + set(ANDROID_TARGET "aarch64-linux-android") + set(ANDROID_ARCH_SHORT "aarch64") + elseif(ANDROID_ABI STREQUAL "x86") + set(ANDROID_TARGET "i686-linux-android") + set(ANDROID_ARCH_SHORT "i386") + elseif(ANDROID_ABI STREQUAL "x86_64") + set(ANDROID_TARGET "x86_64-linux-android") + set(ANDROID_ARCH_SHORT "x86_64") + endif() + + set(WGPU_NATIVE_BUILD_TARGET "--target=${ANDROID_TARGET}") + # TODO: Is this needed given CARGO_BUILD_TARGET above? + set(WGPU_NATIVE_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/target/${ANDROID_TARGET}/${WGPU_NATIVE_BUILD_TYPE}) + + if(BUILD_SHARED_LIBS) + # TODO: Is this actually right? + message(FATAL_ERROR "Wasmtime cannot be built with BUILD_SHARED_LIBS on Android") + endif() +endif() + +# TODO: Add mapping for iOS / etc builds to the right rust targets as well. + +if(BUILD_SHARED_LIBS) + if(WIN32) + # TODO: Is this actually right? + set(WGPU_NATIVE_BUILD_PRODUCT ${WGPU_NATIVE_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}wgpu_native.dll.lib) + else() + set(WGPU_NATIVE_BUILD_PRODUCT ${WGPU_NATIVE_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}wgpu_native${CMAKE_SHARED_LIBRARY_SUFFIX}) + endif() +else() + set(WGPU_NATIVE_BUILD_PRODUCT ${WGPU_NATIVE_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}wgpu_native${CMAKE_STATIC_LIBRARY_SUFFIX}) +endif() + +if (WIN32) + set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32) +elseif(UNIX AND NOT APPLE) + set(OS_LIBRARIES "-lm -ldl") +elseif(APPLE) + set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal") +endif() + +find_program(WGPU_NATIVE_CARGO_BINARY cargo) + +# TODO: Use Corrosion instead? +include(ExternalProject) +ExternalProject_Add( + wgpu_native-crate + DOWNLOAD_COMMAND "" + CONFIGURE_COMMAND "" + 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} + INSTALL_COMMAND "" + BUILD_ALWAYS ${WGPU_NATIVE_ALWAYS_BUILD} + BUILD_BYPRODUCTS ${WGPU_NATIVE_BUILD_PRODUCT}) + +add_library(wgpu_native INTERFACE) +# TODO: How should we really name these? I don't know that the _native suffix adds any value for anyone. +add_library(wgpu::wgpu ALIAS wgpu_native) +add_dependencies(wgpu_native wgpu_native-crate) +target_link_libraries(wgpu_native INTERFACE ${WGPU_NATIVE_BUILD_PRODUCT} ${OS_LIBRARIES}) + +if(BUILD_SHARED_LIBS) + target_compile_definitions(wgpu_native INTERFACE WGPU_SHARED_LIBRARY) +endif() + +target_include_directories(wgpu_native INTERFACE + $ + $ + $) + +export(TARGETS wgpu_native NAMESPACE wgpu:: + FILE "${CMAKE_BINARY_DIR}/lib/cmake/wgpu_native/WgpuNativeTargets.cmake") + +# TODO: install(EXPORTS ...) +include(GNUInstallDirs) +install( + FILES + ${CMAKE_CURRENT_SOURCE_DIR}/ffi/wgpu.h + ${CMAKE_CURRENT_SOURCE_DIR}/ffi/webgpu-headers/webgpu.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/webgpu) +install(FILES ${WGPU_NATIVE_BUILD_PRODUCT} + DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 421cc675..1bd53fb3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -8,24 +8,13 @@ if(APPLE) endif() cmake_path(GET CMAKE_SOURCE_DIR PARENT_PATH PROJECT_ROOT_DIR) - -if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(WGPU_NATIVE_LIB_TYPE debug) -else() - set(WGPU_NATIVE_LIB_TYPE release) -endif() - -if(DEFINED ENV{CARGO_BUILD_TARGET}) - set(WGPU_NATIVE_LIB_DIR ${PROJECT_ROOT_DIR}/target/$ENV{CARGO_BUILD_TARGET}/${WGPU_NATIVE_LIB_TYPE}) -else() - set(WGPU_NATIVE_LIB_DIR ${PROJECT_ROOT_DIR}/target/${WGPU_NATIVE_LIB_TYPE}) -endif() - -find_library( - WGPU_LIBRARY NAMES libwgpu_native.a wgpu_native.lib wgpu_native - HINTS ${WGPU_NATIVE_LIB_DIR} - REQUIRED +include(FetchContent) +FetchContent_Declare( + wgpu_native + SOURCE_DIR ${PROJECT_ROOT_DIR} ) +FetchContent_MakeAvailable(wgpu_native) +set(WGPU_LIBRARY wgpu::wgpu) add_subdirectory(framework) diff --git a/examples/capture/CMakeLists.txt b/examples/capture/CMakeLists.txt index db148c9a..b001e5f0 100644 --- a/examples/capture/CMakeLists.txt +++ b/examples/capture/CMakeLists.txt @@ -11,16 +11,6 @@ endif() add_definitions(-DSTB_IMAGE_WRITE_IMPLEMENTATION) -include_directories(${CMAKE_SOURCE_DIR}/../ffi) -include_directories(${CMAKE_SOURCE_DIR}/../ffi/webgpu-headers) include_directories(${CMAKE_SOURCE_DIR}/framework) -if (WIN32) - set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32) -elseif(UNIX AND NOT APPLE) - set(OS_LIBRARIES "-lm -ldl") -elseif(APPLE) - set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal") -endif() - -target_link_libraries(capture framework ${WGPU_LIBRARY} ${OS_LIBRARIES}) +target_link_libraries(capture framework ${WGPU_LIBRARY}) diff --git a/examples/compute/CMakeLists.txt b/examples/compute/CMakeLists.txt index e6efecfe..fb3f441d 100644 --- a/examples/compute/CMakeLists.txt +++ b/examples/compute/CMakeLists.txt @@ -9,16 +9,6 @@ else() add_compile_options(-Wall -Wextra -Wpedantic) endif() -include_directories(${CMAKE_SOURCE_DIR}/../ffi) -include_directories(${CMAKE_SOURCE_DIR}/../ffi/webgpu-headers) include_directories(${CMAKE_SOURCE_DIR}/framework) -if (WIN32) - set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32) -elseif(UNIX AND NOT APPLE) - set(OS_LIBRARIES "-lm -ldl") -elseif(APPLE) - set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal") -endif() - -target_link_libraries(compute framework ${WGPU_LIBRARY} ${OS_LIBRARIES}) +target_link_libraries(compute framework ${WGPU_LIBRARY}) diff --git a/examples/enumerate_adapters/CMakeLists.txt b/examples/enumerate_adapters/CMakeLists.txt index a939822d..35db38e9 100644 --- a/examples/enumerate_adapters/CMakeLists.txt +++ b/examples/enumerate_adapters/CMakeLists.txt @@ -9,16 +9,6 @@ else() add_compile_options(-Wall -Wextra -Wpedantic) endif() -include_directories(${CMAKE_SOURCE_DIR}/../ffi) -include_directories(${CMAKE_SOURCE_DIR}/../ffi/webgpu-headers) include_directories(${CMAKE_SOURCE_DIR}/framework) -if (WIN32) - set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32) -elseif(UNIX AND NOT APPLE) - set(OS_LIBRARIES "-lm -ldl") -elseif(APPLE) - set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal") -endif() - -target_link_libraries(enumerate_adapters framework ${WGPU_LIBRARY} ${OS_LIBRARIES}) +target_link_libraries(enumerate_adapters framework ${WGPU_LIBRARY}) diff --git a/examples/framework/CMakeLists.txt b/examples/framework/CMakeLists.txt index bdefbd36..2fee5875 100644 --- a/examples/framework/CMakeLists.txt +++ b/examples/framework/CMakeLists.txt @@ -9,15 +9,4 @@ else() add_compile_options(-Wall -Wextra -Wpedantic) endif() -include_directories(${CMAKE_SOURCE_DIR}/../ffi) -include_directories(${CMAKE_SOURCE_DIR}/../ffi/webgpu-headers) - -if (WIN32) - set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32) -elseif(UNIX AND NOT APPLE) - set(OS_LIBRARIES "-lm -ldl") -elseif(APPLE) - set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal") -endif() - -target_link_libraries(framework ${WGPU_LIBRARY} ${OS_LIBRARIES}) +target_link_libraries(framework ${WGPU_LIBRARY}) diff --git a/examples/texture_arrays/CMakeLists.txt b/examples/texture_arrays/CMakeLists.txt index b235c859..61a1311c 100644 --- a/examples/texture_arrays/CMakeLists.txt +++ b/examples/texture_arrays/CMakeLists.txt @@ -9,19 +9,14 @@ else() add_compile_options(-Wall -Wextra -Wpedantic) endif() -include_directories(${CMAKE_SOURCE_DIR}/../ffi) -include_directories(${CMAKE_SOURCE_DIR}/../ffi/webgpu-headers) include_directories(${CMAKE_SOURCE_DIR}/framework) if (WIN32) add_definitions(-DWGPU_TARGET_WINDOWS) - set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32) elseif(UNIX AND NOT APPLE) add_definitions(-DWGPU_TARGET_LINUX_X11) - set(OS_LIBRARIES "-lm -ldl") elseif(APPLE) add_definitions(-DWGPU_TARGET_MACOS) - set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal") endif() -target_link_libraries(texture_arrays framework glfw ${WGPU_LIBRARY} ${OS_LIBRARIES}) +target_link_libraries(texture_arrays framework glfw ${WGPU_LIBRARY}) diff --git a/examples/triangle/CMakeLists.txt b/examples/triangle/CMakeLists.txt index 61537239..a55edf06 100644 --- a/examples/triangle/CMakeLists.txt +++ b/examples/triangle/CMakeLists.txt @@ -9,19 +9,14 @@ else() add_compile_options(-Wall -Wextra -Wpedantic) endif() -include_directories(${CMAKE_SOURCE_DIR}/../ffi) -include_directories(${CMAKE_SOURCE_DIR}/../ffi/webgpu-headers) include_directories(${CMAKE_SOURCE_DIR}/framework) if (WIN32) add_definitions(-DWGPU_TARGET_WINDOWS) - set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32) elseif(UNIX AND NOT APPLE) add_definitions(-DWGPU_TARGET_LINUX_X11) - set(OS_LIBRARIES "-lm -ldl") elseif(APPLE) add_definitions(-DWGPU_TARGET_MACOS) - set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal") endif() -target_link_libraries(triangle framework glfw ${WGPU_LIBRARY} ${OS_LIBRARIES}) +target_link_libraries(triangle framework glfw ${WGPU_LIBRARY})