Debian Codegraph @

test/cmake/libfswatch_package_consumer.cmake

raw
1 #
2 # Copyright (c) 2026 Enrico M. Crisostomo
3 #
4 # This program is free software; you can redistribute it and/or modify it under
5 # the terms of the GNU General Public License as published by the Free Software
6 # Foundation; either version 3, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU General Public License along with
14 # this program. If not, see <http://www.gnu.org/licenses/>.
15 #
16
17 foreach(required_var TEST_BUILD_DIR TEST_WORK_DIR TEST_GENERATOR)
18 if (NOT DEFINED ${required_var})
19 message(FATAL_ERROR "${required_var} is required")
20 endif ()
21 endforeach()
22
23 get_filename_component(TEST_BUILD_DIR "${TEST_BUILD_DIR}" ABSOLUTE)
24 get_filename_component(TEST_WORK_DIR "${TEST_WORK_DIR}" ABSOLUTE)
25 string(FIND "${TEST_WORK_DIR}/" "${TEST_BUILD_DIR}/" work_dir_in_build_dir)
26 if (NOT work_dir_in_build_dir EQUAL 0)
27 message(FATAL_ERROR "Refusing to write outside the build tree: ${TEST_WORK_DIR}")
28 endif ()
29
30 set(install_dir "${TEST_WORK_DIR}/install")
31 set(consumer_source_dir "${TEST_WORK_DIR}/consumer")
32 set(consumer_build_dir "${TEST_WORK_DIR}/consumer-build")
33
34 file(REMOVE_RECURSE "${TEST_WORK_DIR}")
35 file(MAKE_DIRECTORY "${consumer_source_dir}" "${consumer_build_dir}")
36
37 set(config_args)
38 if (DEFINED TEST_CONFIG AND NOT TEST_CONFIG STREQUAL "")
39 list(APPEND config_args --config "${TEST_CONFIG}")
40 endif ()
41
42 execute_process(
43 COMMAND "${CMAKE_COMMAND}" --install "${TEST_BUILD_DIR}" --prefix "${install_dir}" ${config_args}
44 RESULT_VARIABLE install_result)
45 if (NOT install_result EQUAL 0)
46 message(FATAL_ERROR "Could not install libfswatch package test prefix")
47 endif ()
48
49 file(WRITE "${consumer_source_dir}/CMakeLists.txt" [=[
50 cmake_minimum_required(VERSION 3.14)
51 project(libfswatch_package_consumer LANGUAGES CXX)
52
53 find_package(libfswatch CONFIG REQUIRED
54 PATHS ${CMAKE_PREFIX_PATH}
55 NO_DEFAULT_PATH)
56
57 add_executable(consumer main.cpp)
58 target_link_libraries(consumer PRIVATE libfswatch::libfswatch)
59 ]=])
60
61 set(consumer_main [=[
62 #include <libfswatch/c/libfswatch.h>
63 #include <libfswatch/c++/event.hpp>
64 #include <libfswatch/c++/filter.hpp>
65 #include <libfswatch/c++/libfswatch_exception.hpp>
66 #include <libfswatch/c++/monitor.hpp>
67 #include <libfswatch/c++/monitor_factory.hpp>
68 #include <libfswatch/c++/path_utils.hpp>
69 #include <libfswatch/c++/poll_monitor.hpp>
70 #include <libfswatch/c++/string/string_utils.hpp>
71 ]=])
72
73 if (TEST_HAVE_FANOTIFY)
74 string(APPEND consumer_main "#include <libfswatch/c++/fanotify_monitor.hpp>\n")
75 endif ()
76 if (TEST_HAVE_FSEVENTS)
77 string(APPEND consumer_main "#include <libfswatch/c++/fsevents_monitor.hpp>\n")
78 endif ()
79 if (TEST_HAVE_INOTIFY_MONITOR)
80 string(APPEND consumer_main "#include <libfswatch/c++/inotify_monitor.hpp>\n")
81 endif ()
82 if (TEST_HAVE_PORT_H)
83 string(APPEND consumer_main "#include <libfswatch/c++/fen_monitor.hpp>\n")
84 endif ()
85 if (TEST_HAVE_SYS_EVENT_H)
86 string(APPEND consumer_main "#include <libfswatch/c++/kqueue_monitor.hpp>\n")
87 endif ()
88 if (TEST_HAVE_WINDOWS)
89 string(APPEND consumer_main "#include <libfswatch/c++/windows_monitor.hpp>\n")
90 endif ()
91
92 string(APPEND consumer_main [=[
93
94 int main()
95 {
96 FSW_HANDLE handle = fsw_init_session(poll_monitor_type);
97 if (handle == nullptr) return 1;
98
99 if (fsw_set_filter_mode(handle, filter_mode_conjunctive) != FSW_OK)
100 return 1;
101
102 if (fsw_destroy_session(handle) != FSW_OK)
103 return 1;
104
105 return fsw_init_library() == FSW_OK ? 0 : 1;
106 }
107 ]=])
108 file(WRITE "${consumer_source_dir}/main.cpp" "${consumer_main}")
109
110 set(generator_args -G "${TEST_GENERATOR}")
111 if (DEFINED TEST_GENERATOR_PLATFORM AND NOT TEST_GENERATOR_PLATFORM STREQUAL "")
112 list(APPEND generator_args -A "${TEST_GENERATOR_PLATFORM}")
113 endif ()
114 if (DEFINED TEST_GENERATOR_TOOLSET AND NOT TEST_GENERATOR_TOOLSET STREQUAL "")
115 list(APPEND generator_args -T "${TEST_GENERATOR_TOOLSET}")
116 endif ()
117
118 set(compiler_args)
119 if (DEFINED TEST_CXX_COMPILER AND NOT TEST_CXX_COMPILER STREQUAL "")
120 list(APPEND compiler_args "-DCMAKE_CXX_COMPILER=${TEST_CXX_COMPILER}")
121 endif ()
122
123 execute_process(
124 COMMAND "${CMAKE_COMMAND}"
125 -S "${consumer_source_dir}"
126 -B "${consumer_build_dir}"
127 ${generator_args}
128 ${compiler_args}
129 "-DCMAKE_PREFIX_PATH=${install_dir}"
130 RESULT_VARIABLE configure_result)
131 if (NOT configure_result EQUAL 0)
132 message(FATAL_ERROR "Could not configure libfswatch package consumer")
133 endif ()
134
135 execute_process(
136 COMMAND "${CMAKE_COMMAND}" --build "${consumer_build_dir}" ${config_args}
137 RESULT_VARIABLE build_result)
138 if (NOT build_result EQUAL 0)
139 message(FATAL_ERROR "Could not build libfswatch package consumer")
140 endif ()