|
1 | | -cmake_minimum_required(VERSION 3.21) |
| 1 | +cmake_minimum_required(VERSION 3.24) |
2 | 2 |
|
3 | | -project(RobotCPP) |
| 3 | +project(robot-cpp |
| 4 | + VERSION 0.2.0 |
| 5 | + DESCRIPTION "Cross-platform keyboard, mouse, and screen automation" |
| 6 | + LANGUAGES CXX |
| 7 | +) |
4 | 8 |
|
| 9 | +# Honour CMAKE_CXX_STANDARD on targets and keep this a subproject-friendly build. |
5 | 10 | set(CMAKE_CXX_STANDARD 23) |
6 | 11 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
7 | 12 | set(CMAKE_CXX_EXTENSIONS OFF) |
8 | 13 |
|
9 | | -# Library name |
10 | | -set(LIB_NAME RobotCPP) |
11 | | - |
12 | | -# Option to build tests (OFF by default) |
13 | | -option(BUILD_ROBOT_TESTS "Build the RobotCPP tests" OFF) |
14 | | - |
15 | | -# Only find dependencies and add GoogleTest if tests are enabled |
16 | | -if(BUILD_ROBOT_TESTS) |
17 | | - # Add GoogleTest |
18 | | - add_subdirectory(externals/googletest) |
19 | | - enable_testing() |
| 14 | +if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY) |
| 15 | + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
| 16 | +endif() |
20 | 17 |
|
21 | | - # Find SDL2 for tests |
22 | | - set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/sdl2/") |
23 | | - find_package(SDL2 REQUIRED) |
| 18 | +# Detect whether we are the top-level project so options don't leak into parents. |
| 19 | +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) |
| 20 | + set(ROBOT_IS_TOPLEVEL ON) |
| 21 | +else() |
| 22 | + set(ROBOT_IS_TOPLEVEL OFF) |
24 | 23 | endif() |
25 | 24 |
|
26 | | -# Compiler-specific options |
| 25 | +option(ROBOT_BUILD_TESTS "Build robot-cpp tests" ${ROBOT_IS_TOPLEVEL}) |
| 26 | +option(ROBOT_BUILD_INTERACTIVE_TESTS |
| 27 | + "Build the SDL-driven interactive injection tests (needs a live display)" OFF) |
| 28 | +option(ROBOT_BUILD_EXAMPLES "Build robot-cpp examples" ${ROBOT_IS_TOPLEVEL}) |
| 29 | +option(ROBOT_WERROR "Treat warnings as errors" OFF) |
| 30 | +option(ROBOT_LINUX_ENABLE_UINPUT "Build the Linux uinput backend" ON) |
| 31 | + |
| 32 | +# ── Warnings interface ──────────────────────────────────────────────────────── |
| 33 | +# A dedicated interface target carries warning flags so they attach per-target |
| 34 | +# instead of polluting the whole directory via add_compile_options. |
| 35 | +add_library(robot_warnings INTERFACE) |
27 | 36 | if(MSVC) |
28 | | - # MSVC flags |
29 | | - add_compile_options(/W4 /MP) |
| 37 | + target_compile_options(robot_warnings INTERFACE /W4 /permissive- /MP) |
| 38 | + if(ROBOT_WERROR) |
| 39 | + target_compile_options(robot_warnings INTERFACE /WX) |
| 40 | + endif() |
30 | 41 | else() |
31 | | - # GCC/Clang flags |
32 | | - add_compile_options(-Wall -Wextra -Wpedantic) |
| 42 | + target_compile_options(robot_warnings INTERFACE |
| 43 | + -Wall -Wextra -Wpedantic -Wconversion -Wshadow) |
| 44 | + if(ROBOT_WERROR) |
| 45 | + target_compile_options(robot_warnings INTERFACE -Werror) |
| 46 | + endif() |
33 | 47 | endif() |
34 | 48 |
|
35 | | -# Common source files |
36 | | -set(COMMON_SOURCES |
37 | | - src/ActionRecorder.h |
38 | | - src/types.h |
39 | | - src/Mouse.h |
40 | | - src/Mouse.cpp |
41 | | - src/Utils.h |
42 | | - src/Utils.cpp |
43 | | - src/Keyboard.cpp |
44 | | - src/Keyboard.h |
45 | | - src/Screen.cpp |
46 | | - src/Screen.h) |
47 | | - |
48 | | -# External dependencies |
49 | | -set(SOURCES_LODEPNG |
50 | | - externals/lodepng/lodepng.cpp |
51 | | - externals/lodepng/lodepng.h) |
52 | | - |
53 | | -# Platform-specific components |
| 49 | +# ── Vendored PNG encoder (private) ──────────────────────────────────────────── |
| 50 | +add_library(robot_lodepng STATIC externals/lodepng/lodepng.cpp) |
| 51 | +target_include_directories(robot_lodepng |
| 52 | + PUBLIC |
| 53 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/externals/lodepng> |
| 54 | + $<INSTALL_INTERFACE:include/robot/external/lodepng> |
| 55 | +) |
| 56 | +set_target_properties(robot_lodepng PROPERTIES POSITION_INDEPENDENT_CODE ON) |
| 57 | + |
| 58 | +# ── Portable sources (compiled on every platform) ───────────────────────────── |
| 59 | +set(ROBOT_COMMON_SOURCES |
| 60 | + src/common/Session.cpp |
| 61 | + src/common/Keyboard.cpp |
| 62 | + src/common/Mouse.cpp |
| 63 | + src/common/Screen.cpp |
| 64 | + src/common/Image.cpp |
| 65 | + src/common/Recorder.cpp |
| 66 | + src/common/EventTap.cpp |
| 67 | +) |
| 68 | + |
| 69 | +# ── Platform selection: exactly one directory is compiled ───────────────────── |
| 70 | +# This is the single compile boundary. No consumer translation unit contains a |
| 71 | +# platform #ifdef; the platform is chosen here, once. |
54 | 72 | if(APPLE) |
55 | | - list(APPEND PLATFORM_SOURCES src/EventHookMacOS.h) |
56 | | - find_library(CARBON_LIBRARY Carbon) |
57 | | - mark_as_advanced(CARBON_LIBRARY) |
58 | | - list(APPEND PLATFORM_LIBRARIES ${CARBON_LIBRARY}) |
| 73 | + set(ROBOT_PLATFORM_SOURCES |
| 74 | + src/platform/macos/MacBackendFactory.cpp |
| 75 | + src/platform/macos/MacKeyMap.cpp |
| 76 | + src/platform/macos/MacKeyboardBackend.cpp |
| 77 | + src/platform/macos/MacMouseBackend.cpp |
| 78 | + src/platform/macos/MacScreenBackend.cpp |
| 79 | + src/platform/macos/MacEventTapBackend.cpp |
| 80 | + ) |
59 | 81 | elseif(WIN32) |
60 | | - list(APPEND PLATFORM_SOURCES src/EventHookWindows.h) |
| 82 | + set(ROBOT_PLATFORM_SOURCES |
| 83 | + src/platform/windows/WinBackendFactory.cpp |
| 84 | + src/platform/windows/WinDpi.cpp |
| 85 | + src/platform/windows/WinKeyMap.cpp |
| 86 | + src/platform/windows/WinKeyboardBackend.cpp |
| 87 | + src/platform/windows/WinMouseBackend.cpp |
| 88 | + src/platform/windows/WinScreenBackend.cpp |
| 89 | + src/platform/windows/WinEventTapBackend.cpp |
| 90 | + ) |
| 91 | +elseif(UNIX) |
| 92 | + set(ROBOT_PLATFORM_SOURCES |
| 93 | + src/platform/linux/LinuxBackendFactory.cpp |
| 94 | + src/platform/linux/LinuxPlatformBackend.cpp |
| 95 | + src/platform/linux/X11Display.cpp |
| 96 | + src/platform/linux/X11KeyMap.cpp |
| 97 | + src/platform/linux/X11KeyboardBackend.cpp |
| 98 | + src/platform/linux/X11MouseBackend.cpp |
| 99 | + src/platform/linux/X11ScreenBackend.cpp |
| 100 | + src/platform/linux/X11EventTapBackend.cpp |
| 101 | + ) |
| 102 | + if(ROBOT_LINUX_ENABLE_UINPUT) |
| 103 | + list(APPEND ROBOT_PLATFORM_SOURCES |
| 104 | + src/platform/linux/UinputBackend.cpp |
| 105 | + src/platform/linux/LinuxEvdevKeymap.cpp |
| 106 | + ) |
| 107 | + endif() |
| 108 | +else() |
| 109 | + message(FATAL_ERROR "robot-cpp: unsupported platform") |
61 | 110 | endif() |
62 | 111 |
|
63 | | -# Define the main library |
64 | | -add_library(${LIB_NAME} STATIC ${COMMON_SOURCES} ${PLATFORM_SOURCES} ${SOURCES_LODEPNG}) |
65 | | -target_include_directories(${LIB_NAME} PUBLIC src PRIVATE externals/lodepng) |
66 | | -target_link_libraries(${LIB_NAME} ${PLATFORM_LIBRARIES}) |
| 112 | +# ── Main library ────────────────────────────────────────────────────────────── |
| 113 | +add_library(robot ${ROBOT_COMMON_SOURCES} ${ROBOT_PLATFORM_SOURCES}) |
| 114 | +add_library(robot::robot ALIAS robot) |
| 115 | + |
| 116 | +target_compile_features(robot PUBLIC cxx_std_23) |
| 117 | + |
| 118 | +# Public headers under include/; implementation-only headers under src/ are |
| 119 | +# private. The generator expression keeps install and build-tree includes correct. |
| 120 | +target_include_directories(robot |
| 121 | + PUBLIC |
| 122 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 123 | + $<INSTALL_INTERFACE:include> |
| 124 | + PRIVATE |
| 125 | + ${CMAKE_CURRENT_SOURCE_DIR}/src |
| 126 | +) |
| 127 | + |
| 128 | +# lodepng and the warnings flags are implementation details: PRIVATE, so they do |
| 129 | +# not propagate to consumers of the library. |
| 130 | +target_link_libraries(robot |
| 131 | + PRIVATE |
| 132 | + robot_lodepng |
| 133 | + robot_warnings |
| 134 | +) |
67 | 135 |
|
68 | | -# Set output directory for all targets |
69 | | -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
70 | | -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin) |
71 | | -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin) |
| 136 | +set_target_properties(robot PROPERTIES POSITION_INDEPENDENT_CODE ON) |
72 | 137 |
|
73 | | -# Add the tests directory only if tests are enabled |
74 | | -if(BUILD_ROBOT_TESTS) |
| 138 | +# ── Platform dependencies, scoped per target ────────────────────────────────── |
| 139 | +if(APPLE) |
| 140 | + find_library(APPLICATION_SERVICES ApplicationServices REQUIRED) |
| 141 | + # ApplicationServices types appear only in .cpp files (behind the backend |
| 142 | + # interfaces), so the dependency is PRIVATE. Carbon is intentionally NOT used. |
| 143 | + target_link_libraries(robot PRIVATE ${APPLICATION_SERVICES}) |
| 144 | +elseif(WIN32) |
| 145 | + # user32/gdi32 for SendInput, hooks, GDI capture. Shcore and the DPI context |
| 146 | + # API are resolved at runtime (see WinDpi.cpp / WinScreenBackend.cpp), so they |
| 147 | + # are not linked here. |
| 148 | + target_link_libraries(robot PRIVATE user32 gdi32) |
| 149 | + target_compile_definitions(robot PRIVATE |
| 150 | + WIN32_LEAN_AND_MEAN NOMINMAX UNICODE _UNICODE) |
| 151 | +elseif(UNIX) |
| 152 | + find_package(X11 REQUIRED) |
| 153 | + # X11 headers are used only in platform .cpp files -> PRIVATE. XTest and |
| 154 | + # Xrandr components ship with X11; XRecord lives in libXtst as well. |
| 155 | + target_link_libraries(robot PRIVATE |
| 156 | + X11::X11 X11::Xtst X11::Xrandr) |
| 157 | +endif() |
| 158 | + |
| 159 | +# ── Tests ───────────────────────────────────────────────────────────────────── |
| 160 | +if(ROBOT_BUILD_TESTS) |
| 161 | + enable_testing() |
75 | 162 | add_subdirectory(tests) |
76 | 163 | endif() |
| 164 | + |
| 165 | +# ── Examples ────────────────────────────────────────────────────────────────── |
| 166 | +if(ROBOT_BUILD_EXAMPLES) |
| 167 | + add_subdirectory(examples) |
| 168 | +endif() |
| 169 | + |
| 170 | +# ── Install / export ────────────────────────────────────────────────────────── |
| 171 | +include(GNUInstallDirs) |
| 172 | +include(CMakePackageConfigHelpers) |
| 173 | + |
| 174 | +install(TARGETS robot robot_lodepng robot_warnings |
| 175 | + EXPORT robotTargets |
| 176 | + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 177 | + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 178 | + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} |
| 179 | +) |
| 180 | +install(DIRECTORY include/robot DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) |
| 181 | +install(FILES |
| 182 | + externals/lodepng/lodepng.h |
| 183 | + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/robot/external/lodepng |
| 184 | +) |
| 185 | + |
| 186 | +install(EXPORT robotTargets |
| 187 | + FILE robotTargets.cmake |
| 188 | + NAMESPACE robot:: |
| 189 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/robot |
| 190 | +) |
| 191 | + |
| 192 | +write_basic_package_version_file( |
| 193 | + "${CMAKE_CURRENT_BINARY_DIR}/robotConfigVersion.cmake" |
| 194 | + VERSION ${PROJECT_VERSION} |
| 195 | + COMPATIBILITY SameMajorVersion |
| 196 | +) |
| 197 | +# A minimal package config that re-finds the platform deps on the consumer side. |
| 198 | +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/robotConfig.cmake" |
| 199 | +"include(CMakeFindDependencyMacro)\n\ |
| 200 | +if(UNIX AND NOT APPLE)\n\ |
| 201 | + find_dependency(X11)\n\ |
| 202 | +endif()\n\ |
| 203 | +include(\"\${CMAKE_CURRENT_LIST_DIR}/robotTargets.cmake\")\n") |
| 204 | + |
| 205 | +install(FILES |
| 206 | + "${CMAKE_CURRENT_BINARY_DIR}/robotConfig.cmake" |
| 207 | + "${CMAKE_CURRENT_BINARY_DIR}/robotConfigVersion.cmake" |
| 208 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/robot |
| 209 | +) |
0 commit comments