From 727d7b343f5754fc2ed27c2125fa83789eb1d91f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 3 Feb 2019 20:05:44 +0100 Subject: [PATCH] cmake: initial support for building wayland-only, or x11-only --- CMakeLists.txt | 148 ++++++++++++++++++++++--------------- bar/bar.c | 21 +++++- decorations/CMakeLists.txt | 5 +- modules/CMakeLists.txt | 48 +++++++----- modules/i3.c | 21 +++++- particles/CMakeLists.txt | 5 +- plugin.c | 4 + 7 files changed, 162 insertions(+), 90 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a5a32d..edadd6b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,68 +23,87 @@ set(CMAKE_C_FLAGS "-Wall -Werror ${CMAKE_C_FLAGS}") find_package(Threads REQUIRED) find_package(PkgConfig REQUIRED) -pkg_check_modules(xcb REQUIRED IMPORTED_TARGET - xcb xcb-aux xcb-cursor xcb-event xcb-ewmh xcb-randr xcb-render) -pkg_check_modules(xcb-errors IMPORTED_TARGET xcb-errors) -pkg_check_modules(wayland REQUIRED IMPORTED_TARGET - wayland-client wayland-cursor wlroots) pkg_check_modules(fontconfig REQUIRED IMPORTED_TARGET fontconfig) -pkg_check_modules(cairo REQUIRED IMPORTED_TARGET cairo cairo-xcb cairo-ft) +pkg_check_modules(cairo REQUIRED IMPORTED_TARGET cairo cairo-ft) pkg_check_modules(yaml REQUIRED IMPORTED_TARGET yaml-0.1) -add_library(xcb-stuff STATIC EXCLUDE_FROM_ALL xcb.c xcb.h) -if (XCB_ERRORS_FOUND) - target_compile_definitions(xcb-stuff PRIVATE HAVE_XCB_ERRORS) +set(ENABLE_X11 1 CACHE BOOL "Enables support for the XCB (X11) backend") +set_property(DIRECTORY . APPEND PROPERTY + COMPILE_DEFINITIONS $<$:ENABLE_X11>) + +if (ENABLE_X11) + pkg_check_modules(xcb REQUIRED IMPORTED_TARGET + xcb xcb-aux xcb-cursor xcb-event xcb-ewmh xcb-randr xcb-render cairo-xcb) + pkg_check_modules(xcb-errors IMPORTED_TARGET xcb-errors) endif () -add_library(bar-xcb STATIC EXCLUDE_FROM_ALL bar/xcb.c bar/xcb.h) -target_link_libraries(bar-xcb - xcb-stuff PkgConfig::xcb PkgConfig::xcb-errors PkgConfig::cairo) +set(ENABLE_WAYLAND 1 CACHE BOOL "Enables support for the wayland backend") +set_property(DIRECTORY . APPEND PROPERTY + COMPILE_DEFINITIONS $<$:ENABLE_WAYLAND>) -add_custom_command( - OUTPUT wlr-layer-shell-unstable-v1.c - COMMAND wayland-scanner private-code < ${PROJECT_SOURCE_DIR}/external/wlroots/protocol/wlr-layer-shell-unstable-v1.xml > wlr-layer-shell-unstable-v1.c - VERBATIM - MAIN_DEPENDENCY ${PROJECT_SOURCE_DIR}/external/wlroots/protocol/wlr-layer-shell-unstable-v1.xml - ) -add_custom_command( - OUTPUT wlr-layer-shell-unstable-v1-client.h - COMMAND wayland-scanner client-header < /home/daniel/AUR/wlroots-git/src/wlroots-git/protocol/wlr-layer-shell-unstable-v1.xml > wlr-layer-shell-unstable-v1-client.h - VERBATIM - MAIN_DEPENDENCY /home/daniel/AUR/wlroots-git/src/wlroots-git/protocol/wlr-layer-shell-unstable-v1.xml - ) -add_custom_command( - OUTPUT xdg-shell.c - COMMAND wayland-scanner private-code < /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml > xdg-shell.c - VERBATIM - MAIN_DEPENDENCY /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml - ) -add_custom_command( - OUTPUT xdg-output-unstable-v1-client.h - COMMAND wayland-scanner client-header < /usr/share/wayland-protocols/unstable/xdg-output/xdg-output-unstable-v1.xml > xdg-output-unstable-v1-client.h - VERBATIM - MAIN_DEPENDENCY /usr/share/wayland-protocols/unstable/xdg-output/xdg-output-unstable-v1.xml - ) -add_custom_command( - OUTPUT xdg-output-unstable-v1.c - COMMAND wayland-scanner private-code < /usr/share/wayland-protocols/unstable/xdg-output/xdg-output-unstable-v1.xml > xdg-output-unstable-v1.c - VERBATIM - MAIN_DEPENDENCY /usr/share/wayland-protocols/unstable/xdg-output/xdg-output-unstable-v1.xml - ) +if (ENABLE_WAYLAND) + pkg_check_modules(wayland REQUIRED IMPORTED_TARGET + wayland-client wayland-cursor wlroots) +endif () -add_library(wayland-protocols STATIC EXCLUDE_FROM_ALL - wlr-layer-shell-unstable-v1-client.h - wlr-layer-shell-unstable-v1.c - xdg-shell.c - xdg-output-unstable-v1-client.h - xdg-output-unstable-v1.c - ) -target_include_directories(wayland-protocols PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) +if (ENABLE_X11) + add_library(xcb-stuff STATIC EXCLUDE_FROM_ALL xcb.c xcb.h) + if (XCB_ERRORS_FOUND) + target_compile_definitions(xcb-stuff PRIVATE HAVE_XCB_ERRORS) + endif () + target_link_libraries(xcb-stuff PkgConfig::xcb PkgConfig::xcb-errors) -add_library(bar-wayland STATIC EXCLUDE_FROM_ALL bar/wayland.c bar/wayland.h) -target_compile_definitions(bar-wayland PRIVATE _GNU_SOURCE) -target_link_libraries( - bar-wayland wayland-protocols PkgConfig::wayland PkgConfig::cairo) + add_library(bar-xcb STATIC EXCLUDE_FROM_ALL bar/xcb.c bar/xcb.h) + target_link_libraries(bar-xcb + xcb-stuff PkgConfig::cairo) +endif () + +if (ENABLE_WAYLAND) + add_custom_command( + OUTPUT wlr-layer-shell-unstable-v1.c + COMMAND wayland-scanner private-code < ${PROJECT_SOURCE_DIR}/external/wlroots/protocol/wlr-layer-shell-unstable-v1.xml > wlr-layer-shell-unstable-v1.c + VERBATIM + MAIN_DEPENDENCY ${PROJECT_SOURCE_DIR}/external/wlroots/protocol/wlr-layer-shell-unstable-v1.xml + ) + add_custom_command( + OUTPUT wlr-layer-shell-unstable-v1-client.h + COMMAND wayland-scanner client-header < /home/daniel/AUR/wlroots-git/src/wlroots-git/protocol/wlr-layer-shell-unstable-v1.xml > wlr-layer-shell-unstable-v1-client.h + VERBATIM + MAIN_DEPENDENCY /home/daniel/AUR/wlroots-git/src/wlroots-git/protocol/wlr-layer-shell-unstable-v1.xml + ) + add_custom_command( + OUTPUT xdg-shell.c + COMMAND wayland-scanner private-code < /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml > xdg-shell.c + VERBATIM + MAIN_DEPENDENCY /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml + ) + add_custom_command( + OUTPUT xdg-output-unstable-v1-client.h + COMMAND wayland-scanner client-header < /usr/share/wayland-protocols/unstable/xdg-output/xdg-output-unstable-v1.xml > xdg-output-unstable-v1-client.h + VERBATIM + MAIN_DEPENDENCY /usr/share/wayland-protocols/unstable/xdg-output/xdg-output-unstable-v1.xml + ) + add_custom_command( + OUTPUT xdg-output-unstable-v1.c + COMMAND wayland-scanner private-code < /usr/share/wayland-protocols/unstable/xdg-output/xdg-output-unstable-v1.xml > xdg-output-unstable-v1.c + VERBATIM + MAIN_DEPENDENCY /usr/share/wayland-protocols/unstable/xdg-output/xdg-output-unstable-v1.xml + ) + + add_library(wayland-protocols STATIC EXCLUDE_FROM_ALL + wlr-layer-shell-unstable-v1-client.h + wlr-layer-shell-unstable-v1.c + xdg-shell.c + xdg-output-unstable-v1-client.h + xdg-output-unstable-v1.c + ) + target_include_directories(wayland-protocols PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) + + add_library(bar-wayland STATIC EXCLUDE_FROM_ALL bar/wayland.c bar/wayland.h) + target_compile_definitions(bar-wayland PRIVATE _GNU_SOURCE) + target_link_libraries( + bar-wayland wayland-protocols PkgConfig::wayland PkgConfig::cairo) +endif () add_executable(f00bar config.c config.h @@ -104,7 +123,8 @@ add_executable(f00bar ) target_link_libraries(f00bar - bar-xcb bar-wayland + $<$:bar-xcb> + $<$:bar-wayland> PkgConfig::cairo PkgConfig::fontconfig PkgConfig::yaml ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS}) @@ -117,13 +137,23 @@ set_property(TARGET f00bar PROPERTY install(TARGETS f00bar DESTINATION bin) +set(enabled_modules "") +set(enabled_particles "") +set(enabled_decorations "") + add_subdirectory(modules) add_subdirectory(particles) add_subdirectory(decorations) + + if (NOT CORE_PLUGINS_AS_SHARED_LIBRARIES) - target_link_libraries(f00bar background stack underline) - target_link_libraries(f00bar dynlist empty list map progress-bar ramp string) - target_link_libraries(f00bar alsa backlight battery clock i3 label mpd network - removables xkb xwindow) + target_link_libraries(f00bar ${enabled_decorations}) + target_link_libraries(f00bar ${enabled_particles}) + target_link_libraries(f00bar ${enabled_modules}) + + foreach (plug ${enabled_decorations} ${enabled_particles} ${enabled_modules}) + string(REPLACE "-" "_" fixed "${plug}") + target_compile_definitions(f00bar PRIVATE "HAVE_PLUGIN_${fixed}") + endforeach () endif () diff --git a/bar/bar.c b/bar/bar.c index dc0b5bd..aef9ac1 100644 --- a/bar/bar.c +++ b/bar/bar.c @@ -14,8 +14,13 @@ #define LOG_ENABLE_DBG 0 #include "../log.h" -#include "xcb.h" -#include "wayland.h" +#if defined(ENABLE_X11) + #include "xcb.h" +#endif + +#if defined(ENABLE_WAYLAND) + #include "wayland.h" +#endif /* * Calculate total width of left/center/rigth groups. @@ -382,12 +387,22 @@ bar_new(const struct bar_config *config) priv->right.count = config->right.count; priv->cursor_name = NULL; -#if 0 +#if defined(ENABLE_X11) && !defined(ENABLE_WAYLAND) priv->backend.data = bar_backend_xcb_new(); priv->backend.iface = &xcb_backend_iface; #else +#if !defined(ENABLE_X11) && defined(ENABLE_WAYLAND) priv->backend.data = bar_backend_wayland_new(); priv->backend.iface = &wayland_backend_iface; +#else + if (getenv("WAYLAND_DISPLAY") != NULL) { + priv->backend.data = bar_backend_wayland_new(); + priv->backend.iface = &wayland_backend_iface; + } else { + priv->backend.data = bar_backend_xcb_new(); + priv->backend.iface = &xcb_backend_iface; + } +#endif #endif for (size_t i = 0; i < priv->left.count; i++) { diff --git a/decorations/CMakeLists.txt b/decorations/CMakeLists.txt index 3702be6..43e1713 100644 --- a/decorations/CMakeLists.txt +++ b/decorations/CMakeLists.txt @@ -1,8 +1,7 @@ cmake_minimum_required(VERSION 3.7) add_library(decoration-sdk INTERFACE) -target_compile_options(decoration-sdk INTERFACE ${CAIRO_CFLAGS_OTHER}) -target_include_directories(decoration-sdk INTERFACE ${CAIRO_INCLUDE_DIRS}) +target_link_libraries(decoration-sdk INTERFACE PkgConfig::cairo) set(CMAKE_SHARED_MODULE_PREFIX decoration_) @@ -22,3 +21,5 @@ endforeach () if (CORE_PLUGINS_AS_SHARED_LIBRARIES) install(TARGETS ${decorations} DESTINATION lib/f00bar) endif () + +set(enabled_decorations ${decorations} PARENT_SCOPE) diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index 0a92ac9..d449824 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -12,19 +12,25 @@ endif () set(CMAKE_SHARED_${lib_type}_PREFIX module_) +set(enabled "") + pkg_check_modules(alsa REQUIRED IMPORTED_TARGET alsa) add_library(alsa ${lib_type} alsa.c) target_link_libraries(alsa module-sdk PkgConfig::alsa) +list(APPEND enabled alsa) pkg_check_modules(udev REQUIRED IMPORTED_TARGET libudev) add_library(backlight ${lib_type} backlight.c) target_link_libraries(backlight module-sdk PkgConfig::udev) +list(APPEND enabled backlight) add_library(battery ${lib_type} battery.c) target_link_libraries(battery module-sdk PkgConfig::udev) +list(APPEND enabled battery) add_library(clock ${lib_type} clock.c) target_link_libraries(clock module-sdk) +list(APPEND enabled clock) pkg_check_modules(json REQUIRED IMPORTED_TARGET json-c) find_file(I3_IPC_H i3/ipc.h) @@ -33,41 +39,43 @@ if (NOT I3_IPC_H) endif () add_library(i3 ${lib_type} i3.c) target_link_libraries(i3 module-sdk dynlist PkgConfig::json) +if (ENABLE_X11) + target_link_libraries(i3 xcb-stuff) +endif () +list(APPEND enabled i3) add_library(label ${lib_type} label.c) target_link_libraries(label module-sdk) +list(APPEND enabled label) pkg_check_modules(mpd REQUIRED IMPORTED_TARGET libmpdclient) add_library(mpd ${lib_type} mpd.c) target_link_libraries(mpd module-sdk PkgConfig::mpd) +list(APPEND enabled mpd) add_library(network ${lib_type} network.c) target_link_libraries(network module-sdk) +list(APPEND enabled network) add_library(removables ${lib_type} removables.c) target_link_libraries(removables module-sdk dynlist PkgConfig::udev) +list(APPEND enabled removables) -pkg_check_modules(xkb REQUIRED IMPORTED_TARGET xcb-xkb) -add_library(xkb ${lib_type} xkb.c) -target_link_libraries(xkb module-sdk PkgConfig::xkb) +if (ENABLE_X11) + pkg_check_modules(xkb REQUIRED IMPORTED_TARGET xcb-xkb) + add_library(xkb ${lib_type} xkb.c) + target_link_libraries(xkb module-sdk PkgConfig::xcb PkgConfig::xkb) + list(APPEND enabled xkb) +endif () -add_library(xwindow ${lib_type} xwindow.c) -target_link_libraries(xwindow module-sdk) +if (ENABLE_X11) + add_library(xwindow ${lib_type} xwindow.c) + target_link_libraries(xwindow module-sdk xcb-stuff) + list(APPEND enabled xwindow) +endif () if (CORE_PLUGINS_AS_SHARED_LIBRARIES) - install( - TARGETS - alsa - backlight - battery - clock - i3 - label - mpd - network - removables - xkb - xwindow - - DESTINATION lib/f00bar) + install(TARGETS ${enabled} DESTINATION lib/f00bar) endif () + +set(enabled_modules ${enabled} PARENT_SCOPE) diff --git a/modules/i3.c b/modules/i3.c index 133ecae..cce5ca3 100644 --- a/modules/i3.c +++ b/modules/i3.c @@ -10,8 +10,11 @@ #include #include -#include -#include +#if defined(ENABLE_X11) + #include + #include +#endif + #include #include @@ -26,7 +29,10 @@ #include "../config-verify.h" #include "../particles/dynlist.h" #include "../plugin.h" -#include "../xcb.h" + +#if defined(ENABLE_X11) + #include "../xcb.h" +#endif struct ws_content { char *name; @@ -371,6 +377,7 @@ handle_workspace_event(struct private *m, const struct json_object *json) return true; } +#if defined(ENABLE_X11) static bool get_socket_address_x11(struct sockaddr_un *addr) { @@ -419,6 +426,7 @@ get_socket_address_x11(struct sockaddr_un *addr) xcb_disconnect(conn); return true; } +#endif static bool get_socket_address(struct sockaddr_un *addr) @@ -426,8 +434,13 @@ get_socket_address(struct sockaddr_un *addr) *addr = (struct sockaddr_un){.sun_family = AF_UNIX}; const char *sway_sock = getenv("SWAYSOCK"); - if (sway_sock == NULL) + if (sway_sock == NULL) { +#if defined(ENABLE_X11) return get_socket_address_x11(addr); +#else + return false; +#endif + } strncpy(addr->sun_path, sway_sock, sizeof(addr->sun_path)); return true; diff --git a/particles/CMakeLists.txt b/particles/CMakeLists.txt index 0ca7674..f80e3df 100644 --- a/particles/CMakeLists.txt +++ b/particles/CMakeLists.txt @@ -1,8 +1,7 @@ cmake_minimum_required(VERSION 3.7) add_library(particle-sdk INTERFACE) -target_compile_options(particle-sdk INTERFACE ${CAIRO_CFLAGS_OTHER}) -target_include_directories(particle-sdk INTERFACE ${CAIRO_INCLUDE_DIRS}) +target_link_libraries(particle-sdk INTERFACE PkgConfig::cairo) if (CORE_PLUGINS_AS_SHARED_LIBRARIES) set(lib_type MODULE) @@ -30,3 +29,5 @@ target_link_libraries(string ${CAIRO_LIBRARIES}) if (CORE_PLUGINS_AS_SHARED_LIBRARIES) install(TARGETS ${particles} dynlist DESTINATION lib/f00bar) endif () + +set(enabled_particles "dynlist;${particles}" PARENT_SCOPE) diff --git a/plugin.c b/plugin.c index 5731f40..8935317 100644 --- a/plugin.c +++ b/plugin.c @@ -112,8 +112,12 @@ init(void) REGISTER_CORE_MODULE(mpd, mpd); REGISTER_CORE_MODULE(network, network); REGISTER_CORE_MODULE(removables, removables); +#if defined(HAVE_MODULE_xkb) REGISTER_CORE_MODULE(xkb, xkb); +#endif +#if defined(HAVE_MODULE_xwindow) REGISTER_CORE_MODULE(xwindow, xwindow); +#endif REGISTER_CORE_PARTICLE(empty, empty); REGISTER_CORE_PARTICLE(list, list);