-
Notifications
You must be signed in to change notification settings - Fork 3
/
CMakeLists.txt
147 lines (137 loc) · 4.81 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
cmake_minimum_required(VERSION 3.18)
include_guard(GLOBAL)
set (CMAKE_CXX_STANDARD 17)
# extism-cpp library
project(extism-cpp VERSION 1.0.0 DESCRIPTION "C++ bindings for libextism")
set(extism-cpp-srcs src/manifest.cpp src/current_plugin.cpp src/plugin.cpp src/function.cpp src/extism.cpp)
option(EXTISM_CPP_BUILD_IN_TREE "Set to ON to build with submodule deps" OFF)
option(EXTISM_CPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON)
if(EXTISM_CPP_BUILD_IN_TREE)
message("EXTISM_CPP_BUILD_IN_TREE: using deps from parent directory")
add_subdirectory(../extism/libextism extism)
add_subdirectory(../jsoncpp jsoncpp)
else()
include(FetchContent)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)
find_package(extism)
if(extism_FOUND)
message("Found extism")
else()
message("Did not find extism, using FetchContent")
FetchContent_Declare(
extism
GIT_REPOSITORY https://github.com/extism/extism.git
GIT_TAG main
SOURCE_SUBDIR libextism
)
FetchContent_MakeAvailable(extism)
endif()
find_package(jsoncpp)
if(jsoncpp_FOUND)
message("Found jsoncpp")
else()
message("Did not find jsoncpp, using FetchContent")
FetchContent_Declare(
jsoncpp
GIT_REPOSITORY https://github.com/open-source-parsers/jsoncpp.git
GIT_TAG master
)
FetchContent_MakeAvailable(jsoncpp)
endif()
endif()
# SHARED
add_library(extism-cpp SHARED ${extism-cpp-srcs})
set_target_properties(extism-cpp PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties(extism-cpp PROPERTIES PUBLIC_HEADER src/extism.hpp)
target_include_directories(extism-cpp PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
)
target_link_libraries(extism-cpp PUBLIC extism-shared)
target_link_libraries(extism-cpp PRIVATE jsoncpp_lib)
set_target_properties(extism-cpp
PROPERTIES NO_SONAME 1
)
configure_file(extism-cpp.pc.in extism-cpp.pc @ONLY)
list(APPEND CMAKE_TARGETS extism-cpp)
# STATIC
add_library(extism-cpp-static STATIC ${extism-cpp-srcs})
set_target_properties(extism-cpp-static PROPERTIES OUTPUT_NAME extism-cpp)
set_target_properties(extism-cpp-static PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties(extism-cpp-static PROPERTIES PUBLIC_HEADER src/extism.hpp)
target_include_directories(extism-cpp-static PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
)
target_link_libraries(extism-cpp-static PUBLIC extism-static)
if(TARGET jsoncpp_static)
target_link_libraries(extism-cpp-static PRIVATE jsoncpp_static)
else()
message(WARNING "jsoncpp_static not found, linking jsoncpp_lib instead")
target_link_libraries(extism-cpp-static PRIVATE jsoncpp_lib)
endif()
configure_file(extism-cpp-static.pc.in extism-cpp-static.pc @ONLY)
list(APPEND CMAKE_TARGETS extism-cpp-static)
if(EXTISM_CPP_WITH_CMAKE_PACKAGE)
set(INSTALL_EXPORT EXPORT extism-cpp)
else()
set(INSTALL_EXPORT)
endif()
include(GNUInstallDirs)
install(TARGETS ${CMAKE_TARGETS} ${INSTALL_EXPORT}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/extism-cpp.pc ${CMAKE_CURRENT_BINARY_DIR}/extism-cpp-static.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
if(EXTISM_CPP_WITH_CMAKE_PACKAGE)
include(CMakePackageConfigHelpers)
install(EXPORT extism-cpp
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/extism-cpp
FILE extism-cpp-targets.cmake)
configure_package_config_file(extism-cppConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/extism-cppConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/extism-cpp)
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/extism-cppConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/extism-cppConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/extism-cppConfigVersion.cmake
${CMAKE_CURRENT_SOURCE_DIR}/extism-cpp-namespaced-targets.cmake
${CMAKE_CURRENT_SOURCE_DIR}/cmake/Findextism.cmake
${CMAKE_CURRENT_SOURCE_DIR}/cmake/Findjsoncpp.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/extism-cpp)
endif()
# Example
add_executable(
example
example.cpp
)
target_link_libraries(
example
extism-cpp
)
# Static-ish example
add_executable(
example-static
example.cpp
)
target_link_libraries(
example-static
extism-cpp-static
)
# Tests
find_package(GTest)
if(GTest_FOUND)
enable_testing()
add_executable(
extism-test
test/test.cpp
)
target_link_libraries(
extism-test
GTest::gtest
extism-cpp
)
include(GoogleTest)
add_test(NAME Test COMMAND extism-test)
endif()