From ebfa9395a12f3a137b4be1ced4bffd77594d76bd Mon Sep 17 00:00:00 2001 From: Bryan Bernhart Date: Mon, 13 Jun 2022 09:44:39 -0700 Subject: [PATCH] Add shared library build support for CMake. Add support for building tests through CMakelist #386 --- .github/workflows/win_msvc_dbg_x64_cmake.yml | 12 ++++ CMakeLists.txt | 7 +-- README.md | 61 +++++++++++++++----- 3 files changed, 61 insertions(+), 19 deletions(-) diff --git a/.github/workflows/win_msvc_dbg_x64_cmake.yml b/.github/workflows/win_msvc_dbg_x64_cmake.yml index e7cf52aaf..790f1ccae 100644 --- a/.github/workflows/win_msvc_dbg_x64_cmake.yml +++ b/.github/workflows/win_msvc_dbg_x64_cmake.yml @@ -56,6 +56,18 @@ jobs: copy scripts\standalone.gclient .gclient gclient sync + - name: Generate shared library for main branch (with patch) + shell: cmd + run: | + cd test + cmake . -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=ON -DCMAKE_TOOLCHAIN_FILE=..\vcpkg\scripts\buildsystems\vcpkg.cmake + + - name: Build shared library for main branch (with patch) + shell: cmd + run: | + cd test + cmake --build . + - name: Generate project for main branch (with patch) shell: cmd run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index fa4bb5431..704512381 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,9 +91,6 @@ if (APPLE OR (UNIX AND NOT ANDROID)) endif() endif() -# TODO: Enable shared library. -set(BUILD_SHARED_LIBS 0) - option_if_not_defined(GPGMM_ENABLE_TESTS "Enables compilation of tests" ON) option_if_not_defined(GPGMM_ENABLE_D3D12 "Enable compilation of the D3D12 backend" ${ENABLE_D3D12}) option_if_not_defined(GPGMM_ENABLE_VK "Enable compilation of the Vulkan backend" ${ENABLE_VK}) @@ -154,7 +151,9 @@ set(CMAKE_CXX_STANDARD "14") add_subdirectory(third_party) add_subdirectory(src/gpgmm) -if (GPGMM_ENABLE_TESTS) +# Tests use GPGMM internals (eg. gpgmm_common, gpgmm_utils, etc) which are never +# exported and must be statically built. +if (GPGMM_ENABLE_TESTS AND NOT BUILD_SHARED_LIBS) add_subdirectory(src/tests) endif() diff --git a/README.md b/README.md index 0929a08b4..171228c41 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ First create an allocator then create allocations from it: ### D3D12 ```cpp +#include + // Required gpgmm::d3d12::ALLOCATOR_DESC allocatorDesc = {}; allocatorDesc.Device = Device; @@ -75,6 +77,8 @@ To clean-up, simply call `Release()` once the is GPU is finished. ### Vulkan ```cpp +#include + gpgmm::vk::GpCreateAllocatorInfo allocatorInfo = {}; gpgmm::vk::GpResourceAllocator resourceAllocator; @@ -97,14 +101,16 @@ gpgmm::vk::gpDestroyBuffer(resourceAllocator, buffer, allocation); // Make sure gpgmm::vk::gpDestroyResourceAllocator(resourceAllocator); ``` +# Project integration -## Project integration +It is recommended to use the built-in GN or CMake build targets. Otherwise, a shared library can be used for other build systems. -It is recommended to use the built-in GN or CMake build targets. +## Target-based builds ### GN BUILD.gn + ```gn source_set("proj") { deps = [ "${gpgmm_dir}:gpgmm" ] @@ -115,21 +121,26 @@ Create `build_overrides/gpgmm.gni` file in root directory. ### CMake CMakeLists.txt + ```cmake add_subdirectory(gpgmm) target_include_directories(proj PRIVATE gpgmm/src/include gpgmm/src) target_link_libraries(proj PRIVATE gpgmm ...) ``` -### Shared library +## Shared library -Alternatively, GPGMM can be built as a shared library which can be distributed through the application. +### GN +Use `is_clang=false gpgmm_shared_library=true` when [Setting up the build](#Setting-up-the-build). +Then use `ninja -C out/Release gpgmm` or `ninja -C out/Debug gpgmm` to build the shared library. -#### Dynamic Linked Library (DLL) +### CMake -Use `is_clang=false gpgmm_shared_library=true` when [Setting up the build](#Setting-up-the-build). -Then use `ninja -C out/Release gpgmm` or `ninja -C out/Debug gpgmm` to build the shared library (DLL). +```cmake +cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Debug . +cmake --build . +``` Copy the DLL into the `$(OutputPath)` folder and configure the VS build: 1. Highlight project in the **Solution Explorer**, and then select **Project > Properties**. @@ -137,14 +148,9 @@ Copy the DLL into the `$(OutputPath)` folder and configure the VS build: 3. Under **Configuration Properties > Linker > Input**, add ``gpgmm.dll.lib`` to **Additional Dependencies**. 4. Under **Configuration Properties > Linker > General**, add the folder path to `out\Release` to **Additional Library Directories**. -Then include the public header: -```cpp -#include // Ex. gpgmm_d3d12.h -``` - -## Build and Run +# Build and Run -For development, you must use GN to generate the build. CMake is only supported for production builds (ie. no tests). +## GN ### Install `depot_tools` @@ -170,7 +176,7 @@ Get the source code as follows: > gclient sync ``` -### Setting up the build +### Configure the build Generate build files using `gn args out/Debug` or `gn args out/Release`. A text editor will appear asking build arguments, the most common argument is `is_debug=true/false`; otherwise `gn args out/Release --list` shows all the possible options. @@ -186,6 +192,31 @@ To build with a backend, please set the corresponding argument from following ta Then use `ninja -C out/Release` or `ninja -C out/Debug` to build. +## CMake + +### Install `CMake` + +CMake 3.14 or higher is required: https://cmake.org/install/. + +### Configure the build + +Generate build files using `cmake . -DCMAKE_BUILD_TYPE=Debug` or `cmake . -DCMAKE_BUILD_TYPE=Release`. + +To build with a backend, please specify the corresponding argument from following table. + +| Backend | Build argument | +|---------|--------------| +| DirectX 12 | `GPGMM_ENABLE_D3D12=ON` | +| Vulkan | `GPGMM_ENABLE_VK=ON` | + +For example, `cmake . -DGPGMM_ENABLE_VK=OFF` builds without Vulkan. + +### Build + +```cmake +cmake --build . +``` + ## Testing Verify functionality by generating deterministic or random memory patterns then running them against GPUs.