Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ project(libbase VERSION 1.0
LANGUAGES C)

# Requires out-of-source builds.
file(TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" LOCATION_PATH)
if(EXISTS "${LOCATION_PATH}")
file(TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" location_path)
if(EXISTS "${location_path}")
message(
FATAL_ERROR
"You cannot build into a source directory (containing a CMakeLists.txt file).\n"
Expand Down Expand Up @@ -51,29 +51,31 @@ set(IWYU_MODE OFF CACHE STRING "Whether to run `iwyu`. OFF, WARN or FAIL.")
set_property(CACHE IWYU_MODE PROPERTY STRINGS "OFF" "WARN" "FAIL")

# Toplevel compile options, for Clang and GCC.
add_library(libbase_compiler_flags INTERFACE)

if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
if(config_DEBUG)
add_compile_options(-ggdb -DDEBUG)
target_compile_options(libbase_compiler_flags INTERFACE -ggdb -DDEBUG)
endif()

if(config_OPTIM)
add_compile_options(-O2)
target_compile_options(libbase_compiler_flags INTERFACE -O2)
else()
add_compile_options(-O0)
target_compile_options(libbase_compiler_flags INTERFACE -O0)
endif()
add_compile_options(-Wall -Wextra)
target_compile_options(libbase_compiler_flags INTERFACE -Wall -Wextra)

if(config_WERROR)
add_compile_options(-Werror)
target_compile_options(libbase_compiler_flags INTERFACE -Werror)
endif()

# CMake provides absolute paths to GCC, hence the __FILE__ macro includes the
# full path. This option resets it to a path relative to project source.
add_compile_options(-fmacro-prefix-map=${PROJECT_SOURCE_DIR}/src=.)
target_compile_options(libbase_compiler_flags INTERFACE -fmacro-prefix-map=${PROJECT_SOURCE_DIR}/src=.)

# Target system.
if(LINUX)
add_compile_options(-D__LIBBASE_LINUX)
target_compile_options(libbase_compiler_flags INTERFACE -D__LIBBASE_LINUX)
endif()

# Run iwyu when available.
Expand Down Expand Up @@ -129,15 +131,15 @@ endif()
find_package(Doxygen)
if(DOXYGEN_FOUND)
# Set input and output files.
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
set(doxygen_in ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
set(doxygen_out ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)

# Configure the file.
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
configure_file(${doxygen_in} ${doxygen_out} @ONLY)
add_custom_target(
libbase_doc
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
DEPENDS ${DOXYGEN_OUT}
COMMAND ${DOXYGEN_EXECUTABLE} ${doxygen_out}
DEPENDS ${doxygen_out}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen."
VERBATIM)
Expand All @@ -158,8 +160,8 @@ endif()
if(config_COVERAGE)
find_program(GCOVR_FOUND gcovr OPTIONAL)
if(GCOVR_FOUND)
add_compile_options(--coverage)
add_link_options(--coverage)
target_compile_options(libbase_compiler_flags INTERFACE --coverage)
target_link_options(libbase_compiler_flags INTERFACE --coverage)

message(NOTICE "Coverage configured, adding 'coverage' target.")
add_custom_target(coverage
Expand Down
54 changes: 22 additions & 32 deletions STYLE.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
# Style Reference
# CMake Style Guidelines

## CMake Style Guidelines
This document outlines the style conventions used in the CMake configuration files for this project.

(c) Copyright 2015-2026 Zephyr Project members and individual contributors.
## 1. Command and Function Casing
* **Lowercase commands:** All built-in CMake commands and functions are written in lowercase (e.g., `cmake_minimum_required`, `set`, `add_executable`, `target_link_libraries`, `install`).

### General Formatting
## 2. Indentation, Control Flow, and Line Wrapping
* **Indentation:** 2-space indentation is used for commands inside control blocks (like `if` / `endif`).
* **Control Flow:** Closing commands (e.g., `endif()`, `endforeach()`, `endmacro()`, `endfunction()`) must not repeat the condition or arguments of the opening command (e.g., use `endif()` instead of `endif(config_DEBUG)`).
* **Argument Wrapping:**
* Short, simple commands are kept on a single line (e.g., `set(CMAKE_C_STANDARD 11)`).
* Long commands or those with multiple arguments/keywords are wrapped, with each argument placed on a new line and indented by 2 spaces relative to the command (e.g., `add_executable`, `target_include_directories`).
* The closing parenthesis `)` is either placed on the same line as the last argument or on its own line aligned with the command name (usually in block-style commands like `install` or `set_target_properties`).

* **Indentation**: Use 2 spaces for indentation. Avoid tabs to ensure consistency across different environments.
* **Line Length**: Limit line length to 80 characters where possible.
* **Empty Lines**: Use empty lines to separate logically distinct sections within a CMake file.
* **No Space Before Opening Brackets**: Do not add a space between a command and the opening parenthesis. Use `if(...)` instead of `if (...)`.
## 3. Naming Conventions
* **Local and private variables:** Written in uppercase in legacy project code (e.g., `PUBLIC_HEADER_FILES`, `SOURCES`, `DOXYGEN_IN`), but official guidance recommends using **lowercase** or **snake_case** (e.g., `public_header_files`, `sources`) to distinguish them from CMake's built-in variables.
* **Options/Cache variables:** Formatted using either a lowercase/mixed-case prefix (e.g., `config_DEBUG`, `config_WERROR`) or full uppercase (e.g., `IWYU_MODE`).

### Commands and Syntax
## 4. Quoting
* **Variable expansion and paths:** Double quotes are used for file paths, generator expressions, and strings containing variable expansion (e.g., `"${CMAKE_CURRENT_BINARY_DIR}/analyzer.c"`, `"${CURSES_INCLUDE_DIRS}"`).
* **Identifiers and keywords:** Unquoted for target names, libraries, command keywords, and constant literals (e.g., `libbase`, `STATIC`, `PUBLIC`, `FATAL_ERROR`).

* **Lowercase Commands**: Always use lowercase CMake commands (e.g., `add_executable`, `find_package`). This improves readability and consistency.
* **One File Argument per Line**: Break the file arguments across multiple lines to make it easier to scan and identify each source file or item.
## 5. File Header and Comments
* **Headers:** Every CMake file begins with a standardized Apache 2.0 license and copyright header.
* **Comments:** Standard `#` comments are placed above the relevant block or line. Inline comments are generally lowercase-first or capitalized, providing short descriptions.

### Variable Naming
## 6. Target-Based Configuration
* **Prefer target-based configuration:** Define compile options, compile definitions, include directories, and link libraries on specific targets using `target_*` commands (e.g., `target_include_directories`, `target_compile_options`, `target_compile_definitions`, `target_link_libraries`) rather than modifying global/directory-wide settings (e.g., `add_compile_options`).

* **Use Uppercase for Cache Variables or variables shared across CMake files**: When defining cache variables using `option` or `set(... CACHE ...)`, use UPPERCASE names.
* **Use Lowercase for Local Variables**: For local variables within CMake files, use *lowercase* or *snake_case*.
* **Consistent Prefixing**: Use consistent prefixes for variables to avoid name conflicts, especially in large projects.

### Quoting

* **Quote Strings and Variables**: Always quote string literals and variables to prevent unintended behavior, especially when dealing with paths or arguments that may contain spaces.
* **Do Not Quote Boolean Values**: For boolean values (`ON`, `OFF`, `TRUE`, `FALSE`), avoid quoting them.

### Avoid Hardcoding Paths

* Use CMake variables (`CMAKE_SOURCE_DIR`, `CMAKE_BINARY_DIR`, `CMAKE_CURRENT_SOURCE_DIR`) instead of hardcoding paths.

### Conditional Logic

* Use `if`, `elseif`, and `else` with proper indentation, and close with `endif()`.

### Documentation

* Use comments to document complex logic in the CMake files.
10 changes: 5 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set(PUBLIC_HEADER_FILES
set(public_header_files
arg.h
assert.h
atomic.h
Expand All @@ -39,7 +39,7 @@ set(PUBLIC_HEADER_FILES
time.h
vector.h)

set(SOURCES
set(sources
arg.c
atomic.c
avltree.c
Expand All @@ -62,18 +62,18 @@ set(SOURCES
time.c)

add_library(libbase STATIC)
target_sources(libbase PRIVATE ${SOURCES})
target_sources(libbase PRIVATE ${sources})
target_include_directories(
libbase
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>)
target_include_directories(libbase PRIVATE "${CURSES_INCLUDE_DIRS}")
target_link_libraries(libbase PRIVATE "${CURSES_LIBRARIES}")
target_link_libraries(libbase PRIVATE "${CURSES_LIBRARIES}" libbase_compiler_flags)
set_target_properties(
libbase PROPERTIES
VERSION 1.0
PUBLIC_HEADER "${PUBLIC_HEADER_FILES}"
PUBLIC_HEADER "${public_header_files}"
)

if(iwyu_path_and_options)
Expand Down
6 changes: 3 additions & 3 deletions src/plist/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bison_target(

add_flex_bison_dependency(analyzer grammar)

set(PUBLIC_HEADER_FILES
set(public_header_files
"${PROJECT_SOURCE_DIR}/include/libbase/plist/decode.h"
"${PROJECT_SOURCE_DIR}/include/libbase/plist/model.h"
"${PROJECT_SOURCE_DIR}/include/libbase/plist/parse.h")
Expand All @@ -50,11 +50,11 @@ target_include_directories(
# TODO(kaeser@gubbe.ch):Remove, once updating post bison 3.8.2 (Aug 2022).
# See: https://github.com/phkaeser/wlmaker/issues/53
target_compile_options(libbase_plist PRIVATE -Wno-unused-but-set-variable)
target_link_libraries(libbase_plist libbase)
target_link_libraries(libbase_plist PRIVATE libbase libbase_compiler_flags)
set_target_properties(
libbase_plist
PROPERTIES VERSION 1.0
PUBLIC_HEADER "${PUBLIC_HEADER_FILES}")
PUBLIC_HEADER "${public_header_files}")
if(iwyu_path_and_options)
set_target_properties(
libbase_plist PROPERTIES
Expand Down
43 changes: 37 additions & 6 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,75 @@ add_executable(
libbase_test.c)
target_link_libraries(
libbase_test
libbase)
PRIVATE
libbase
libbase_compiler_flags)
# Refer to the source path for relative testdata.
target_compile_definitions(
libbase_test PUBLIC BS_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
libbase_test
PUBLIC
BS_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}")

target_compile_definitions(
libbase_test PUBLIC BS_TEST_BINARY_DIR="${CMAKE_CURRENT_BINARY_DIR}")
libbase_test
PUBLIC
BS_TEST_BINARY_DIR="${CMAKE_CURRENT_BINARY_DIR}")

add_executable(
libbase_benchmark
libbase_benchmark.c)
target_link_libraries(
libbase_benchmark
libbase)
PRIVATE
libbase
libbase_compiler_flags)

add_executable(
subprocess_test_hang
subprocess_test_hang.c)
target_link_libraries(
subprocess_test_hang
PRIVATE
libbase_compiler_flags)

add_executable(
subprocess_test_failure
subprocess_test_failure.c)
target_link_libraries(
subprocess_test_failure
PRIVATE
libbase_compiler_flags)

add_executable(
subprocess_test_sigpipe
subprocess_test_sigpipe.c)
target_link_libraries(
subprocess_test_sigpipe
PRIVATE
libbase_compiler_flags)

add_executable(
subprocess_test_success
subprocess_test_success.c)
target_link_libraries(
subprocess_test_success
PRIVATE
libbase_compiler_flags)

add_executable(
plist_test
plist_test.c)
target_link_libraries(
plist_test
PRIVATE
libbase
libbase_plist)
libbase_plist
libbase_compiler_flags)
target_include_directories(plist_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/")
target_compile_definitions(
plist_test PUBLIC TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data")
plist_test
PUBLIC
TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data")

add_test(NAME libbase_test COMMAND libbase_test)
add_test(NAME plist_test COMMAND plist_test)
Expand Down
4 changes: 3 additions & 1 deletion tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ add_executable(
plist_parse.c)
target_link_libraries(
plist_parse
PRIVATE
libbase
libbase_plist)
libbase_plist
libbase_compiler_flags)
target_include_directories(plist_parse PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/")

if(iwyu_path_and_options)
Expand Down
Loading