Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to make SPARTA easier to use as a dependency #24

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cmake_minimum_required(VERSION 3.0.2)
project("sparta")

include(CTest)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
include(Commons)

if (NOT CMAKE_BUILD_TYPE)
Expand All @@ -31,6 +31,8 @@ target_include_directories(sparta INTERFACE

target_link_libraries(sparta INTERFACE ${Boost_LIBRARIES})

add_library(sparta::sparta ALIAS sparta)

###################################################
# install and export
###################################################
Expand All @@ -46,10 +48,29 @@ install(TARGETS sparta EXPORT sparta_target
)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

install(EXPORT sparta_target DESTINATION cmake)
install(EXPORT sparta_target
FILE sparta_target.cmake
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/sparta"
NAMESPACE sparta::
)

# This makes the project importable from the build directory
export(TARGETS sparta FILE sparta_target.cmake)
export(TARGETS sparta
FILE sparta_target.cmake
NAMESPACE sparta::
)

# Generate CMake config file that can be used in other projects
include(CMakePackageConfigHelpers)
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/sparta-config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/sparta-config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/sparta"
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/sparta-config.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/sparta"
)

###################################################
# test
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ To copy the header files into `/usr/local/include/sparta` and set up a cmake lib
sudo make install
```

### CMake Setup

If you are using CMake for your project, you can also use SPARTA in the following ways:

* A system-wide installation of SPARTA will contain a CMake configuration file that exports the library as `sparta::sparta`. This enables the use of `find_package` to locate SPARTA. For example:

```cmake
find_package(Boost REQUIRED COMPONENTS thread) # required by SPARTA
find_package(sparta REQUIRED)
add_library(MyAnalysisLibrary)
target_link_libraries(MyAnalysisLibrary PUBLIC sparta::sparta)
```

* If you have cloned and "built" SPARTA locally, you can use a `find_package` call (such as the one above) and run CMake with the `-Dsparta_DIR=<your_sparta_build_dir>` to load the library:

```sh
# Assuming you are at the top-level of your project that uses SPARTA
mkdir build && cd build
cmake .. -Dsparta_DIR=path/to/your/sparta/build
```

* SPARTA can be added as a subdirectory in your project:

```cmake
# assuming you have cloned SPARTA into a folder named sparta
add_subdirectory(sparta)
add_library(MyAnalysisLibrary)
target_link_libraries(MyAnalysisLibrary PUBLIC sparta::sparta)
```

## Issues

Expand Down
8 changes: 4 additions & 4 deletions cmake_modules/Commons.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ macro(add_dependent_packages_for_sparta)
configure_file(cmake_modules/gtest.cmake.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
)
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
)
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
Expand All @@ -64,8 +64,8 @@ macro(add_dependent_packages_for_sparta)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(
${CMAKE_BINARY_DIR}/googletest-src
${CMAKE_BINARY_DIR}/googletest-build
${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL
)

Expand Down
4 changes: 2 additions & 2 deletions cmake_modules/gtest.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.8.1
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
Expand Down
11 changes: 11 additions & 0 deletions cmake_modules/sparta-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

@PACKAGE_INIT@

include(CMakeFindDependencyMacro)
find_dependency(Boost COMPONENTS thread)

include("${CMAKE_CURRENT_LIST_DIR}/sparta_target.cmake")
Loading