Browse Source

Generate a CMake config file when the project is installed (#1140)

Allows for other projects to import Jolt using the find_package() functionality

This makes the project install a "JoltConfig.cmake" into ${CMAKE_INSTALL_PREFIX}/lib/cmake/Jolt/ when the appropriate "install" command is called (i.e. make install or ninja install)

This allows for other CMake projects to do something like:

find_package(Jolt)

# snip

add_executable(Generic main.cpp)
target_link_libraries(Generic PRIVATE Jolt::Jolt)
Ryan Rhee 1 year ago
parent
commit
702a16b86b
2 changed files with 16 additions and 2 deletions
  1. 12 1
      Build/CMakeLists.txt
  2. 4 1
      Jolt/Jolt.cmake

+ 12 - 1
Build/CMakeLists.txt

@@ -243,7 +243,9 @@ if (XCODE)
 endif()
 endif()
 
 
 # Install Jolt library and includes
 # Install Jolt library and includes
-install(TARGETS Jolt DESTINATION lib)
+install(TARGETS Jolt
+	EXPORT JoltExport
+	DESTINATION lib)
 foreach(SRC_FILE ${JOLT_PHYSICS_SRC_FILES})
 foreach(SRC_FILE ${JOLT_PHYSICS_SRC_FILES})
 	string(REPLACE ${PHYSICS_REPO_ROOT} "" RELATIVE_SRC_FILE ${SRC_FILE})
 	string(REPLACE ${PHYSICS_REPO_ROOT} "" RELATIVE_SRC_FILE ${SRC_FILE})
 	get_filename_component(DESTINATION_PATH ${RELATIVE_SRC_FILE} DIRECTORY)
 	get_filename_component(DESTINATION_PATH ${RELATIVE_SRC_FILE} DIRECTORY)
@@ -252,6 +254,15 @@ foreach(SRC_FILE ${JOLT_PHYSICS_SRC_FILES})
 	endif()
 	endif()
 endforeach()
 endforeach()
 
 
+# Export Jolt library
+export(TARGETS Jolt
+	NAMESPACE Jolt::
+	FILE JoltConfig.cmake)
+install(EXPORT JoltExport
+	DESTINATION lib/cmake/Jolt/
+	NAMESPACE Jolt::
+	FILE JoltConfig.cmake)
+
 # Check if we're the root CMakeLists.txt, if not we are included by another CMake file and we should disable everything except for the main library
 # Check if we're the root CMakeLists.txt, if not we are included by another CMake file and we should disable everything except for the main library
 if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
 if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
 	# Ability to turn ON/OFF individual applications
 	# Ability to turn ON/OFF individual applications

+ 4 - 1
Jolt/Jolt.cmake

@@ -495,7 +495,10 @@ if (BUILD_SHARED_LIBS)
 	target_compile_definitions(Jolt PRIVATE JPH_BUILD_SHARED_LIBRARY)
 	target_compile_definitions(Jolt PRIVATE JPH_BUILD_SHARED_LIBRARY)
 endif()
 endif()
 
 
-target_include_directories(Jolt PUBLIC ${PHYSICS_REPO_ROOT})
+# Use repository as include directory when building, install directory when installing
+target_include_directories(Jolt PUBLIC
+	$<BUILD_INTERFACE:${PHYSICS_REPO_ROOT}>
+	$<INSTALL_INTERFACE:include/>)
 target_precompile_headers(Jolt PRIVATE ${JOLT_PHYSICS_ROOT}/Jolt.h)
 target_precompile_headers(Jolt PRIVATE ${JOLT_PHYSICS_ROOT}/Jolt.h)
 
 
 # Set the debug/non-debug build flags
 # Set the debug/non-debug build flags