Skip to content

Commit

Permalink
refactor(cmake): Allow targets to be enabled individually
Browse files Browse the repository at this point in the history
Each major target of polybar can now be enabled/disabled while
configuring (even polybar itself).

The cmake code specific to each target will only run if the target is
enabled.

This allows us to for example just build the documentation without
having to run all the cmake code related to compilation or having the
polybar dependencies installed (other than sphinx).
  • Loading branch information
patrick96 committed Dec 24, 2020
1 parent 7711547 commit c24a699
Show file tree
Hide file tree
Showing 14 changed files with 379 additions and 349 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Expand Up @@ -21,9 +21,9 @@ jobs:
run: sudo apt-get install -y python3-sphinx
- name: Build Documentation
run: |
mkdir -p doc/build
cd doc/build
cmake ..
mkdir -p build
cd build
cmake -DDISABLE_ALL=ON -DBUILD_DOC=ON ..
make doc
build:
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Build
- Bump the minimum cmake version to 3.5
- The `BUILD_IPC_MSG` option has been renamed to `BUILD_POLYBAR_MSG`
- Building the documentation is now enabled by default and not just when
`sphinx-build` is found.
- Users can control exactly which targets should be available with the following
cmake options (together with their default value):
- `BUILD_POLYBAR=ON` - Builds the `polybar` executable
- `BUILD_POLYBAR_MSG=ON` - Builds the `polybar-msg` executable
- `BUILD_TESTS=OFF` - Builds the test suite
- `BUILD_DOC=ON` - Builds the documentation
- `DISABLE_ALL=OFF` - Disables all above targets by default. Individual
targets can still be enabled explicitly.
- The documentation can no longer be built by directly configuring the `doc`
directory.

### Added
- Warn states for the cpu, memory, fs, and battery modules.
Expand Down
33 changes: 12 additions & 21 deletions CMakeLists.txt
Expand Up @@ -4,22 +4,6 @@
cmake_minimum_required(VERSION 3.5.0 FATAL_ERROR)
project(polybar CXX)

option(ENABLE_CCACHE "Enable ccache support" ON)
if(ENABLE_CCACHE)
message(STATUS "Trying to enable ccache")
find_program(BIN_CCACHE ccache)
mark_as_advanced(BIN_CCACHE)

string(ASCII 27 esc)
if(NOT BIN_CCACHE)
message(STATUS "${esc}[33mCouldn't locate ccache, disabling ccache...${esc}[0m")
else()
# Enable only if the binary is found
message(STATUS "${esc}[32mUsing compiler cache ${BIN_CCACHE}${esc}[0m")
set(CMAKE_CXX_COMPILER_LAUNCHER ${BIN_CCACHE} CACHE STRING "")
endif()
endif()

# Extract version information from version.txt. The first line that looks like
# a version string is used, so the file supports comments
file(STRINGS version.txt version_txt REGEX "^[0-9]+\\.[0-9]+\\.[0-9]+.*$" LIMIT_COUNT 1)
Expand All @@ -46,20 +30,26 @@ set(CMAKE_MODULE_PATH

include(GNUInstallDirs)
include(utils)
include(01-core)
include(00-components)
include(02-opts)
if (HAS_CXX_COMPILATION)
include(01-core)
endif()
include(03-libs)
include(04-targets)
include(05-summary)

if(BUILD_DOC)
add_subdirectory(doc)
endif()
add_subdirectory(contrib/bash)
add_subdirectory(contrib/zsh)
add_subdirectory(include)
add_subdirectory(lib)
add_subdirectory(src bin)
if (BUILD_LIBPOLY)
add_subdirectory(include)
add_subdirectory(lib)
endif()
if (HAS_CXX_COMPILATION)
add_subdirectory(src bin)
endif()

# We need to enable testing in the root folder so that 'ctest' and 'make test'
# can be run in the build directory
Expand All @@ -68,6 +58,7 @@ if(BUILD_TESTS)
add_subdirectory(tests)
endif()

include(05-summary)

#
# Generate configuration file
Expand Down
4 changes: 2 additions & 2 deletions build.sh
Expand Up @@ -149,7 +149,7 @@ set_build_opts() {
read -r -p "$(msg "Build \"polybar-msg\" used to send ipc messages ------------------ [y/N]: ")" -n 1 p && echo
[[ "${p^^}" != "Y" ]] && ENABLE_IPC_MSG="OFF" || ENABLE_IPC_MSG="ON"
fi

if [[ -z "$JOB_COUNT" ]]; then
read -r -p "$(msg "Parallelize the build using make -j$(nproc) --------------------------- [y/N]: ")" -n 1 p && echo
[[ "${p^^}" != "Y" ]] && JOB_COUNT=1 || JOB_COUNT=$(nproc)
Expand Down Expand Up @@ -200,7 +200,7 @@ main() {
-DENABLE_MPD:BOOL="${ENABLE_MPD}" \
-DENABLE_NETWORK:BOOL="${ENABLE_NETWORK}" \
-DENABLE_CURL:BOOL="${ENABLE_CURL}" \
-DBUILD_IPC_MSG:BOOL="${ENABLE_IPC_MSG}" \
-DBUILD_POLYBAR_MSG:BOOL="${ENABLE_IPC_MSG}" \
.. || msg_err "Failed to generate build... read output to get a hint of what went wrong"

msg "Building project"
Expand Down
26 changes: 26 additions & 0 deletions cmake/00-components.cmake
@@ -0,0 +1,26 @@
option(DISABLE_ALL "Set this to ON disable all targets. Individual targets can be enabled explicitly." OFF)

# If all targets are disabled, we set the default value for options that are on
# by default to OFF
if (DISABLE_ALL)
set(DEFAULT_ON OFF)
else()
set(DEFAULT_ON ON)
endif()

option(BUILD_POLYBAR "Build the main polybar executable" ${DEFAULT_ON})
option(BUILD_POLYBAR_MSG "Build polybar-msg" ${DEFAULT_ON})
option(BUILD_TESTS "Build testsuite" OFF)
option(BUILD_DOC "Build documentation" ${DEFAULT_ON})

if (BUILD_POLYBAR OR BUILD_TESTS)
set(BUILD_LIBPOLY ON)
else()
set(BUILD_LIBPOLY OFF)
endif()

if (BUILD_LIBPOLY OR BUILD_POLYBAR_MSG)
set(HAS_CXX_COMPILATION ON)
else()
set(HAS_CXX_COMPILATION OFF)
endif()
101 changes: 54 additions & 47 deletions cmake/02-opts.cmake
@@ -1,59 +1,66 @@
#
# Build options
#
set(SPHINX_FLAGS "" CACHE STRING "Flags to pass to sphinx-build")
set(SPHINX_BUILD "sphinx-build" CACHE STRING "Name/Path of the sphinx-build executable to use.")
checklib(BUILD_DOC "binary" "${SPHINX_BUILD}")

checklib(ENABLE_ALSA "pkg-config" alsa)
checklib(ENABLE_CURL "pkg-config" libcurl)
checklib(ENABLE_I3 "binary" i3)
checklib(ENABLE_MPD "pkg-config" libmpdclient)
checklib(WITH_LIBNL "pkg-config" libnl-genl-3.0)
if(WITH_LIBNL)
checklib(ENABLE_NETWORK "pkg-config" libnl-genl-3.0)
set(WIRELESS_LIB "libnl")
else()
checklib(ENABLE_NETWORK "cmake" Libiw)
set(WIRELESS_LIB "wireless-tools")
endif()
checklib(ENABLE_PULSEAUDIO "pkg-config" libpulse)
checklib(WITH_XKB "pkg-config" xcb-xkb)
checklib(WITH_XRM "pkg-config" xcb-xrm)
checklib(WITH_XRANDR_MONITORS "pkg-config" "xcb-randr>=1.12")
checklib(WITH_XCURSOR "pkg-config" "xcb-cursor")
if (HAS_CXX_COMPILATION)
option(ENABLE_CCACHE "Enable ccache support" ON)
if(ENABLE_CCACHE)
find_program(BIN_CCACHE ccache)
mark_as_advanced(BIN_CCACHE)

if(NOT DEFINED ENABLE_CCACHE AND CMAKE_BUILD_TYPE_UPPER MATCHES DEBUG)
set(ENABLE_CCACHE ON)
endif()
if(NOT BIN_CCACHE)
message_colored(STATUS "Couldn't locate ccache, disabling ccache..." "33")
else()
# Enable only if the binary is found
message_colored(STATUS "Using compiler cache ${BIN_CCACHE}" "32")
set(CMAKE_CXX_COMPILER_LAUNCHER ${BIN_CCACHE} CACHE STRING "")
endif()
endif()

option(CXXLIB_CLANG "Link against libc++" OFF)
option(CXXLIB_GCC "Link against stdlibc++" OFF)
option(CXXLIB_CLANG "Link against libc++" OFF)
option(CXXLIB_GCC "Link against stdlibc++" OFF)
endif()

option(BUILD_IPC_MSG "Build ipc messager" ON)
option(BUILD_TESTS "Build testsuite" OFF)
option(BUILD_DOC "Build documentation" ON)
if (BUILD_LIBPOLY)
checklib(ENABLE_ALSA "pkg-config" alsa)
checklib(ENABLE_CURL "pkg-config" libcurl)
checklib(ENABLE_I3 "binary" i3)
checklib(ENABLE_MPD "pkg-config" libmpdclient)
checklib(WITH_LIBNL "pkg-config" libnl-genl-3.0)
if(WITH_LIBNL)
checklib(ENABLE_NETWORK "pkg-config" libnl-genl-3.0)
set(WIRELESS_LIB "libnl")
else()
checklib(ENABLE_NETWORK "cmake" Libiw)
set(WIRELESS_LIB "wireless-tools")
endif()
checklib(ENABLE_PULSEAUDIO "pkg-config" libpulse)
checklib(WITH_XKB "pkg-config" xcb-xkb)
checklib(WITH_XRM "pkg-config" xcb-xrm)
checklib(WITH_XRANDR_MONITORS "pkg-config" "xcb-randr>=1.12")
checklib(WITH_XCURSOR "pkg-config" "xcb-cursor")

option(ENABLE_ALSA "Enable alsa support" ON)
option(ENABLE_CURL "Enable curl support" ON)
option(ENABLE_I3 "Enable i3 support" ON)
option(ENABLE_MPD "Enable mpd support" ON)
option(WITH_LIBNL "Use netlink interface for wireless" ON)
option(ENABLE_NETWORK "Enable network support" ON)
option(ENABLE_XKEYBOARD "Enable xkeyboard support" ON)
option(ENABLE_PULSEAUDIO "Enable PulseAudio support" ON)
option(ENABLE_ALSA "Enable alsa support" ON)
option(ENABLE_CURL "Enable curl support" ON)
option(ENABLE_I3 "Enable i3 support" ON)
option(ENABLE_MPD "Enable mpd support" ON)
option(WITH_LIBNL "Use netlink interface for wireless" ON)
option(ENABLE_NETWORK "Enable network support" ON)
option(ENABLE_XKEYBOARD "Enable xkeyboard support" ON)
option(ENABLE_PULSEAUDIO "Enable PulseAudio support" ON)

option(WITH_XRANDR_MONITORS "xcb-randr monitor support" ON)
option(WITH_XKB "xcb-xkb support" ON)
option(WITH_XRM "xcb-xrm support" ON)
option(WITH_XCURSOR "xcb-cursor support" ON)
option(WITH_XRANDR_MONITORS "xcb-randr monitor support" ON)
option(WITH_XKB "xcb-xkb support" ON)
option(WITH_XRM "xcb-xrm support" ON)
option(WITH_XCURSOR "xcb-cursor support" ON)

option(DEBUG_LOGGER "Trace logging" ON)
option(DEBUG_LOGGER "Trace logging" ON)

if(CMAKE_BUILD_TYPE_UPPER MATCHES DEBUG)
option(DEBUG_LOGGER_VERBOSE "Trace logging (verbose)" OFF)
option(DEBUG_HINTS "Debug clickable areas" OFF)
option(DEBUG_WHITESPACE "Debug whitespace" OFF)
option(DEBUG_FONTCONFIG "Debug fontconfig" OFF)
if(CMAKE_BUILD_TYPE_UPPER MATCHES DEBUG)
option(DEBUG_LOGGER_VERBOSE "Trace logging (verbose)" OFF)
option(DEBUG_HINTS "Debug clickable areas" OFF)
option(DEBUG_WHITESPACE "Debug whitespace" OFF)
option(DEBUG_FONTCONFIG "Debug fontconfig" OFF)
endif()
endif()

set(SETTING_ALSA_SOUNDCARD "default"
Expand Down
104 changes: 57 additions & 47 deletions cmake/03-libs.cmake
Expand Up @@ -2,62 +2,72 @@
# Check libraries
#

find_package(Threads REQUIRED)
find_package(CairoFC REQUIRED)
if (BUILD_DOC)
find_program(BIN_SPHINX "${SPHINX_BUILD}")

if (ENABLE_ALSA)
find_package(ALSA REQUIRED)
set(ALSA_VERSION ${ALSA_VERSION_STRING})
if (NOT BIN_SPHINX)
message(FATAL_ERROR "sphinx-build executable '${SPHINX_BUILD}' not found.")
endif()
endif()

if (ENABLE_CURL)
find_package(CURL REQUIRED)
set(CURL_VERSION ${CURL_VERSION_STRING})
endif()
if (BUILD_LIBPOLY)
find_package(Threads REQUIRED)
find_package(CairoFC REQUIRED)

if (ENABLE_MPD)
find_package(LibMPDClient REQUIRED)
set(MPD_VERSION ${LibMPDClient_VERSION})
endif()
if (ENABLE_ALSA)
find_package(ALSA REQUIRED)
set(ALSA_VERSION ${ALSA_VERSION_STRING})
endif()

if (ENABLE_NETWORK)
if(WITH_LIBNL)
find_package(LibNlGenl3 REQUIRED)
set(NETWORK_LIBRARY_VERSION ${LibNlGenl3_VERSION})
else()
find_package(Libiw REQUIRED)
if (ENABLE_CURL)
find_package(CURL REQUIRED)
set(CURL_VERSION ${CURL_VERSION_STRING})
endif()
endif()

if (ENABLE_PULSEAUDIO)
find_package(LibPulse REQUIRED)
set(PULSEAUDIO_VERSION ${LibPulse_VERSION})
endif()
if (ENABLE_MPD)
find_package(LibMPDClient REQUIRED)
set(MPD_VERSION ${LibMPDClient_VERSION})
endif()

# xcomposite is required
list(APPEND XORG_EXTENSIONS COMPOSITE)
if (WITH_XKB)
list(APPEND XORG_EXTENSIONS XKB)
endif()
if (WITH_XCURSOR)
list(APPEND XORG_EXTENSIONS CURSOR)
endif()
if (WITH_XRM)
list(APPEND XORG_EXTENSIONS XRM)
endif()
if (ENABLE_NETWORK)
if(WITH_LIBNL)
find_package(LibNlGenl3 REQUIRED)
set(NETWORK_LIBRARY_VERSION ${LibNlGenl3_VERSION})
else()
find_package(Libiw REQUIRED)
endif()
endif()

# Set min xrandr version required
if (WITH_XRANDR_MONITORS)
set(XRANDR_VERSION "1.12")
else ()
set(XRANDR_VERSION "")
endif()
if (ENABLE_PULSEAUDIO)
find_package(LibPulse REQUIRED)
set(PULSEAUDIO_VERSION ${LibPulse_VERSION})
endif()

# xcomposite is required
list(APPEND XORG_EXTENSIONS COMPOSITE)
if (WITH_XKB)
list(APPEND XORG_EXTENSIONS XKB)
endif()
if (WITH_XCURSOR)
list(APPEND XORG_EXTENSIONS CURSOR)
endif()
if (WITH_XRM)
list(APPEND XORG_EXTENSIONS XRM)
endif()

# Randr is required
find_package(Xcb ${XRANDR_VERSION} REQUIRED COMPONENTS RANDR)
find_package(Xcb REQUIRED COMPONENTS ${XORG_EXTENSIONS})
# Set min xrandr version required
if (WITH_XRANDR_MONITORS)
set(XRANDR_VERSION "1.12")
else ()
set(XRANDR_VERSION "")
endif()

# FreeBSD Support
if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
find_package(LibInotify REQUIRED)
# Randr is required
find_package(Xcb ${XRANDR_VERSION} REQUIRED COMPONENTS RANDR)
find_package(Xcb REQUIRED COMPONENTS ${XORG_EXTENSIONS})

# FreeBSD Support
if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
find_package(LibInotify REQUIRED)
endif()
endif()

0 comments on commit c24a699

Please sign in to comment.