Skip to content

Commit e29f74e

Browse files
committed
Merge #98: cmake: Combine installed packages
dc9b4e6 cmake: Combine installed packages (Ryan Ofsky) 2ed1e9a cmake: CMakeLists.txt cleanup (Ryan Ofsky) Pull request description: This change combines previous installed: - `cmake/LibmultiprocessLibConfig.cmake` - `cmake/LibmultiprocessBinConfig.cmake` files into a single: - `cmake/Libmultiprocess/LibmultiprocessConfig.cmake` file, so it can be imported with `find_package(Libmultiprocess)`. The previous locations which were set in #97 were not compatible with `find_package` search behavior by default. The change also adds some documentation about using the new package to doc/install.md. Top commit has no ACKs. Tree-SHA512: 506a2f73f19de541d36cc192f3553ae583e78644f60e958234b2a29d0c5f053890bbe176f7dbd38bad0598a5fa2426a34e64b0f266bc87a01eaa21cb7ecd1226
2 parents 3f8483b + dc9b4e6 commit e29f74e

File tree

6 files changed

+149
-83
lines changed

6 files changed

+149
-83
lines changed

CMakeLists.txt

Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ project("Libmultiprocess" CXX)
88
set(CMAKE_CXX_STANDARD 17)
99
set(CMAKE_CXX_STANDARD_REQUIRED YES)
1010

11+
find_package(CapnProto REQUIRED)
12+
find_package(Threads REQUIRED)
13+
1114
option(Libmultiprocess_ENABLE_CLANG_TIDY "Run clang-tidy with the compiler." OFF)
1215
if(Libmultiprocess_ENABLE_CLANG_TIDY)
1316
find_program(CLANG_TIDY_EXECUTABLE NAMES clang-tidy)
@@ -17,61 +20,30 @@ if(Libmultiprocess_ENABLE_CLANG_TIDY)
1720
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXECUTABLE}")
1821
endif()
1922

20-
include(CMakePushCheckState)
21-
include(CheckCXXSourceCompiles)
22-
include(GNUInstallDirs)
23-
find_package(CapnProto REQUIRED)
24-
find_package(Threads REQUIRED)
25-
2623
include("cmake/capnp_compat.cmake")
24+
include("cmake/pthread_checks.cmake")
25+
include(GNUInstallDirs)
2726

28-
cmake_push_check_state()
29-
set(CMAKE_REQUIRED_LIBRARIES Threads::Threads)
30-
check_cxx_source_compiles("
31-
#include <pthread.h>
32-
int main(int argc, char** argv)
33-
{
34-
char thread_name[16];
35-
return pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name));
36-
}"
37-
HAVE_PTHREAD_GETNAME_NP)
38-
39-
check_cxx_source_compiles("
40-
#include <cstdint>
41-
#include <pthread.h>
42-
int main(int argc, char** argv)
43-
{
44-
uint64_t tid;
45-
pthread_threadid_np(NULL, &tid);
46-
return 0;
47-
}"
48-
HAVE_PTHREAD_THREADID_NP)
49-
50-
check_cxx_source_compiles("
51-
#include <pthread.h>
52-
#include <pthread_np.h>
53-
int main(int argc, char** argv)
54-
{
55-
return pthread_getthreadid_np();
56-
}"
57-
HAVE_PTHREAD_GETTHREADID_NP)
58-
cmake_pop_check_state()
27+
# Generated C++ preprocessor defines
28+
configure_file(include/mp/config.h.in "${CMAKE_CURRENT_BINARY_DIR}/include/mp/config.h")
5929

30+
# Generated C++ Capn'Proto schema files
6031
capnp_generate_cpp(MP_PROXY_SRCS MP_PROXY_HDRS include/mp/proxy.capnp)
6132

33+
# util library
6234
add_library(util OBJECT src/mp/util.cpp)
6335
target_include_directories(util PRIVATE
6436
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
6537
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
6638
${CAPNP_INCLUDE_DIRECTORY})
6739

40+
# libmultiprocess.a runtime library
6841
set(MP_PUBLIC_HEADERS
6942
${MP_PROXY_HDRS}
7043
include/mp/proxy-io.h
7144
include/mp/proxy-types.h
7245
include/mp/proxy.h
7346
include/mp/util.h)
74-
7547
add_library(multiprocess STATIC
7648
${MP_PROXY_SRCS}
7749
${MP_PUBLIC_HEADERS}
@@ -92,26 +64,8 @@ set_target_properties(multiprocess PROPERTIES
9264
install(TARGETS multiprocess EXPORT LibTargets
9365
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT lib
9466
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mp COMPONENT lib)
95-
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/libmultiprocess.pc"
96-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig COMPONENT lib)
97-
install(EXPORT LibTargets
98-
NAMESPACE Libmultiprocess::
99-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Libmultiprocess COMPONENT lib)
100-
include(CMakePackageConfigHelpers)
101-
configure_package_config_file(
102-
${PROJECT_SOURCE_DIR}/cmake/LibmultiprocessLibConfig.cmake.in
103-
LibmultiprocessLibConfig.cmake
104-
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake
105-
NO_SET_AND_CHECK_MACRO)
106-
install(
107-
FILES
108-
${CMAKE_CURRENT_BINARY_DIR}/LibmultiprocessLibConfig.cmake
109-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake COMPONENT lib)
110-
add_custom_target(install-lib
111-
COMMAND ${CMAKE_COMMAND} -DCOMPONENT=lib -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake
112-
VERBATIM)
113-
add_dependencies(install-lib multiprocess)
11467

68+
# mpgen code generator
11569
add_executable(mpgen src/mp/gen.cpp $<TARGET_OBJECTS:util>)
11670
add_executable(Libmultiprocess::mpgen ALIAS mpgen)
11771
target_include_directories(mpgen PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>)
@@ -128,32 +82,55 @@ set_target_properties(mpgen PROPERTIES
12882
install(TARGETS mpgen EXPORT BinTargets
12983
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT bin
13084
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mp COMPONENT bin)
85+
86+
# makefile include to invoke mpgen code generator, for downstream Make projects
13187
install(FILES "include/mpgen.mk"
13288
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT bin)
89+
90+
# pkg-config module to build against libmultiprocess library, for downstream autoconf projects
91+
configure_file(pkgconfig/libmultiprocess.pc.in "${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/libmultiprocess.pc" @ONLY)
92+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/libmultiprocess.pc"
93+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig COMPONENT lib)
94+
95+
# cmake include to invoke mpgen code generator, for downstream CMake projects
96+
install(
97+
FILES
98+
${CMAKE_CURRENT_SOURCE_DIR}/cmake/TargetCapnpSources.cmake
99+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Libmultiprocess COMPONENT bin)
100+
101+
# CMake target import files, for downstream CMake projects
133102
install(EXPORT BinTargets
134103
NAMESPACE Libmultiprocess::
135104
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Libmultiprocess COMPONENT bin)
105+
install(EXPORT LibTargets
106+
NAMESPACE Libmultiprocess::
107+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Libmultiprocess COMPONENT lib)
108+
109+
# CMake find_package config file, for downstream CMake projects
136110
include(CMakePackageConfigHelpers)
137111
configure_package_config_file(
138-
${PROJECT_SOURCE_DIR}/cmake/LibmultiprocessBinConfig.cmake.in
139-
LibmultiprocessBinConfig.cmake
140-
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake
112+
${PROJECT_SOURCE_DIR}/cmake/Config.cmake.in
113+
LibmultiprocessConfig.cmake
114+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Libmultiprocess
141115
NO_SET_AND_CHECK_MACRO)
142116
install(
143117
FILES
144-
${CMAKE_CURRENT_BINARY_DIR}/LibmultiprocessBinConfig.cmake
145-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake COMPONENT bin)
146-
install(
147-
FILES
148-
${CMAKE_CURRENT_SOURCE_DIR}/cmake/TargetCapnpSources.cmake
149-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Libmultiprocess COMPONENT bin)
118+
${CMAKE_CURRENT_BINARY_DIR}/LibmultiprocessConfig.cmake
119+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Libmultiprocess
120+
COMPONENT common)
121+
122+
# Makefile targets to support "make install-bin" "make install-lib"
150123
add_custom_target(install-bin
151124
COMMAND ${CMAKE_COMMAND} -DCOMPONENT=bin -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake
125+
COMMAND ${CMAKE_COMMAND} -DCOMPONENT=common -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake
152126
VERBATIM)
153127
add_dependencies(install-bin mpgen)
128+
add_custom_target(install-lib
129+
COMMAND ${CMAKE_COMMAND} -DCOMPONENT=lib -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake
130+
COMMAND ${CMAKE_COMMAND} -DCOMPONENT=common -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake
131+
VERBATIM)
132+
add_dependencies(install-lib multiprocess)
154133

155-
configure_file(include/mp/config.h.in "${CMAKE_CURRENT_BINARY_DIR}/include/mp/config.h")
156-
configure_file(pkgconfig/libmultiprocess.pc.in "${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/libmultiprocess.pc" @ONLY)
157-
134+
# Example and test subdirectories
158135
add_subdirectory(example EXCLUDE_FROM_ALL)
159136
add_subdirectory(test EXCLUDE_FROM_ALL)

cmake/Config.cmake.in

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
@PACKAGE_INIT@
2+
3+
# CMake find_package compatible package file, for downstream CMake projects
4+
#
5+
# Based on https://cmake.org/cmake/help/latest/guide/importing-exporting/index.html#adding-components
6+
7+
set(_Libmultiprocess_supported_components Bin Lib)
8+
9+
# If no components specified, include all components.
10+
list(LENGTH Libmultiprocess_FIND_COMPONENTS Libmultiprocess_FIND_COMPONENTS_len)
11+
if(Libmultiprocess_FIND_COMPONENTS_len EQUAL 0)
12+
set(Libmultiprocess_FIND_COMPONENTS ${_Libmultiprocess_supported_components})
13+
endif()
14+
15+
if ("Bin" IN_LIST Libmultiprocess_FIND_COMPONENTS)
16+
include("${CMAKE_CURRENT_LIST_DIR}/TargetCapnpSources.cmake")
17+
endif()
18+
19+
if ("Lib" IN_LIST Libmultiprocess_FIND_COMPONENTS)
20+
include(CMakeFindDependencyMacro)
21+
find_dependency(CapnProto)
22+
endif()
23+
24+
foreach(_comp ${Libmultiprocess_FIND_COMPONENTS})
25+
if (NOT _comp IN_LIST _Libmultiprocess_supported_components)
26+
set(Libmultiprocess_FOUND False)
27+
set(Libmultiprocess_NOT_FOUND_MESSAGE "Unsupported component: ${_comp}")
28+
endif()
29+
include("${CMAKE_CURRENT_LIST_DIR}/${_comp}Targets.cmake")
30+
endforeach()

cmake/LibmultiprocessBinConfig.cmake.in

Lines changed: 0 additions & 6 deletions
This file was deleted.

cmake/LibmultiprocessLibConfig.cmake.in

Lines changed: 0 additions & 8 deletions
This file was deleted.

cmake/pthread_checks.cmake

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) 2024 The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
# Define HAVE_PTHREAD_* variables depending on what pthread functions are
6+
# available.
7+
8+
include(CMakePushCheckState)
9+
include(CheckCXXSourceCompiles)
10+
11+
cmake_push_check_state()
12+
set(CMAKE_REQUIRED_LIBRARIES Threads::Threads)
13+
check_cxx_source_compiles("
14+
#include <pthread.h>
15+
int main(int argc, char** argv)
16+
{
17+
char thread_name[16];
18+
return pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name));
19+
}"
20+
HAVE_PTHREAD_GETNAME_NP)
21+
22+
check_cxx_source_compiles("
23+
#include <cstdint>
24+
#include <pthread.h>
25+
int main(int argc, char** argv)
26+
{
27+
uint64_t tid;
28+
pthread_threadid_np(NULL, &tid);
29+
return 0;
30+
}"
31+
HAVE_PTHREAD_THREADID_NP)
32+
33+
check_cxx_source_compiles("
34+
#include <pthread.h>
35+
#include <pthread_np.h>
36+
int main(int argc, char** argv)
37+
{
38+
return pthread_getthreadid_np();
39+
}"
40+
HAVE_PTHREAD_GETTHREADID_NP)
41+
cmake_pop_check_state()

doc/install.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,35 @@ make
1818
make check # Optionally build and run tests
1919
make install
2020
```
21+
22+
To build with libmultiprocess in a CMake project can specify:
23+
24+
```cmake
25+
find_package(Libmultiprocess)
26+
target_capnp_sources(mytarget ${CMAKE_CURRENT_SOURCE_DIR} myschema.capnp)
27+
```
28+
29+
Which will locate the libmultiprocess cmake package, and call the
30+
`target_capnp_sources` function to generate C++ files and link them into a
31+
library or executable target. See `example/CMakeLists.txt` for a complete
32+
example.
33+
34+
To build with libmultiprocess in a non-CMake project can use installed
35+
`<prefix>/include/mpgen.mk` Makefile rule to generate C++ files, and
36+
`<prefix>/lib/pkgconfig/libmultiprocess.pc` pkg-config definition to link
37+
against the runtime library.
38+
39+
For cross-compilation, it may be useful to build the runtime library and code
40+
generation binaries separately, which can be done with:
41+
42+
```sh
43+
make install-bin # install bin/mpgen and related files
44+
make install-lib # install lib/libmultiprocess.a and related files
45+
```
46+
47+
It is also possible to import CMake targets separately with:
48+
49+
```cmake
50+
find_package(Libmultiprocess COMPONENTS Bin)
51+
find_package(Libmultiprocess COMPONENTS Lib)
52+
```

0 commit comments

Comments
 (0)