Skip to content

Commit

Permalink
Add shared library build support for CMake.
Browse files Browse the repository at this point in the history
Add support for building tests through CMakelist #386
  • Loading branch information
bbernhar committed Jun 13, 2022
1 parent 7897942 commit ebfa939
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/win_msvc_dbg_x64_cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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()

Expand Down
61 changes: 46 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ First create an allocator then create allocations from it:

### D3D12
```cpp
#include <gpgmm_d3d12.h>

// Required
gpgmm::d3d12::ALLOCATOR_DESC allocatorDesc = {};
allocatorDesc.Device = Device;
Expand Down Expand Up @@ -75,6 +77,8 @@ To clean-up, simply call `Release()` once the is GPU is finished.
### Vulkan

```cpp
#include <gpgmm_vk.h>

gpgmm::vk::GpCreateAllocatorInfo allocatorInfo = {};

gpgmm::vk::GpResourceAllocator resourceAllocator;
Expand All @@ -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" ]
Expand All @@ -115,36 +121,36 @@ 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**.
2. Under **Configuration Properties > C/C++ > General**, add `gpgmm\src` and `gpgmm\src\include` to **Additional Include Directories**.
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 <gpgmm_*.h> // 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`

Expand All @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit ebfa939

Please sign in to comment.