Skip to content

Commit

Permalink
pw_polyfill: Move std::endian polyfill to pw::endian
Browse files Browse the repository at this point in the history
- Provide the endian enum as pw::endian. Alias it to std::endian in
  the <bit> polyfill header.
- Remove automatic polyfilling for Bazel. The current setup doesn't seem
  to support having the <bit> polyfill depend on pw_bytes/bit.h. Since
  automatic polyfills are being removed anyway, it is simpler to remove
  them from Bazel now.

Bug: b/235237667
Change-Id: I20fa9c7bfca2b6858ee2a121ef755a953ce78ef0
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/97570
Commit-Queue: Wyatt Hepler <[email protected]>
Reviewed-by: Ted Pudlik <[email protected]>
  • Loading branch information
255 authored and CQ Bot Account committed Jun 9, 2022
1 parent e267413 commit ed6629a
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 140 deletions.
3 changes: 0 additions & 3 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ build:asan-libfuzzer \
--@rules_fuzzing//fuzzing:cc_engine_instrumentation=libfuzzer
build:asan-libfuzzer --@rules_fuzzing//fuzzing:cc_engine_sanitizer=asan

# Configures toolchain with polyfill headers.
build --@rules_cc_toolchain_config//:user_defined=//pw_polyfill:toolchain_polyfill_overrides

# Specifies desired output mode for running tests.
# Valid values are
# 'summary' to output only test status summary
Expand Down
9 changes: 0 additions & 9 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,6 @@ git_repository(
remote = "https://github.com/bazelembedded/bazel-embedded.git",
)

# Instantiate Pigweed configuration for embedded toolchain,
# this must be called before bazel_embedded_deps.
load(
"//pw_build:pigweed_toolchain_upstream.bzl",
"toolchain_upstream_deps",
)

toolchain_upstream_deps()

# Configure bazel_embedded toolchains and platforms.
load(
"@bazel_embedded//:bazel_embedded_deps.bzl",
Expand Down
78 changes: 0 additions & 78 deletions pw_build/pigweed_toolchain_upstream.bzl

This file was deleted.

6 changes: 6 additions & 0 deletions pw_bytes/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ pw_cc_library(
],
)

pw_cc_library(
name = "bit",
hdrs = ["public/pw_bytes/bit.h"],
includes = ["public"],
)

pw_cc_test(
name = "array_test",
srcs = ["array_test.cc"],
Expand Down
10 changes: 8 additions & 2 deletions pw_bytes/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import("$dir_pw_build/target_types.gni")
import("$dir_pw_docgen/docs.gni")
import("$dir_pw_unit_test/test.gni")

config("default_config") {
config("public_include_path") {
include_dirs = [ "public" ]
visibility = [ ":*" ]
}

pw_source_set("pw_bytes") {
public_configs = [ ":default_config" ]
public_configs = [ ":public_include_path" ]
public = [
"public/pw_bytes/array.h",
"public/pw_bytes/byte_builder.h",
Expand All @@ -41,6 +41,12 @@ pw_source_set("pw_bytes") {
]
}

pw_source_set("bit") {
public_configs = [ ":public_include_path" ]
public = [ "public/pw_bytes/bit.h" ]
remove_public_deps = [ "*" ]
}

pw_test_group("tests") {
tests = [
":array_test",
Expand Down
7 changes: 7 additions & 0 deletions pw_bytes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ if(Zephyr_FOUND AND CONFIG_PIGWEED_BYTES)
zephyr_link_libraries(pw_bytes)
endif()

pw_add_module_library(pw_bytes.bit
HEADERS
public/pw_bytes/bit.h
PUBLIC_INCLUDES
public
)

pw_add_test(pw_bytes.array_test
SOURCES
array_test.cc
Expand Down
33 changes: 22 additions & 11 deletions pw_bytes/docs.rst
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
.. _module-pw_bytes:

---------
=========
pw_bytes
---------
=========
pw_bytes is a collection of utilities for manipulating binary data.

-------------
Compatibility
=============
-------------
C++17

------------
Dependencies
============
------------
* ``pw_preprocessor``
* ``pw_status``
* ``pw_span``

--------
Features
========
--------

pw_bytes/array.h
----------------
================
Functions for working with byte arrays, primarily for building fixed-size byte
arrays at compile time.

pw_bytes/byte_builder.h
-----------------------
=======================
.. cpp:class:: ByteBuilder

``ByteBuilder`` is a class that facilitates building or reading arrays of
Expand All @@ -36,15 +39,22 @@ pw_bytes/byte_builder.h
``ByteBuilder`` with an internally allocated buffer.

Size report: using ByteBuffer
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-----------------------------
.. include:: byte_builder_size_report

pw_bytes/bit.h
================
Implementation of features provided by C++20's ``<bit>`` header. Supported
features:

* ``pw::endian`` -- Implementation of the ``std::endian`` enum.

pw_bytes/endian.h
-----------------
=================
Functions for converting the endianness of integral values.

pw_bytes/units.h
----------------
================
Constants, functions and user-defined literals for specifying a number of bytes
in powers of two, as defined by IEC 60027-2 A.2 and ISO/IEC 80000:13-2008.

Expand Down Expand Up @@ -78,7 +88,8 @@ also similar functions:
constexpr size_t kBufferSizeBytes = pw::bytes::MiB(1) + pw::bytes::KiB(42);
------
Zephyr
======
------
To enable ``pw_bytes`` for Zephyr add ``CONFIG_PIGWEED_BYTES=y`` to the
project's configuration.
44 changes: 44 additions & 0 deletions pw_bytes/public/pw_bytes/bit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// Features from the <bit> header introduced in C++20.
#pragma once

#if __has_include(<bit>)
#include <bit>
#endif // __has_include(<bit>)

namespace pw {

#ifdef __cpp_lib_endian

using std::endian;

#elif defined(__GNUC__)

enum class endian {
little = __ORDER_LITTLE_ENDIAN__,
big = __ORDER_BIG_ENDIAN__,
native = __BYTE_ORDER__,
};

#else

static_assert(false,
"The pw::endian enum is not defined for this compiler. Add a "
"definition to pw_bytes/bit.h.");

#endif // __cpp_lib_endian

} // namespace pw
33 changes: 6 additions & 27 deletions pw_polyfill/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,11 @@ load(
"pw_cc_library",
"pw_cc_test",
)
load("@rules_cc_toolchain//cc_toolchain:cc_toolchain_import.bzl", "cc_toolchain_import")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

cc_toolchain_import(
name = "toolchain_polyfill_overrides",
hdrs = [
"bit_public_overrides/bit",
"standard_library_public/pw_polyfill/standard_library/bit.h",
"standard_library_public/pw_polyfill/standard_library/namespace.h",
],
includes = [
"bit_public_overrides",
"public",
"standard_library_public",
],
)

pw_cc_library(
name = "pw_polyfill",
hdrs = [
Expand All @@ -46,16 +31,6 @@ pw_cc_library(
includes = ["public"],
)

# TODO(b/235237667): Deprecate this once all users have been migrated to targeted
# polyfill deps.
pw_cc_library(
name = "overrides",
deps = [
":bit",
":span",
],
)

# Provides <bit>'s std::endian.
pw_cc_library(
name = "bit",
Expand All @@ -64,10 +39,13 @@ pw_cc_library(
"standard_library_public/pw_polyfill/standard_library/bit.h",
],
includes = [
"public_overrides",
"bit_public_overrides",
"standard_library_public",
],
deps = [":standard_library"],
deps = [
":standard_library",
"//pw_bytes:bit",
],
)

# Provides <cstddef>'s std::byte.
Expand Down Expand Up @@ -117,6 +95,7 @@ pw_cc_test(
name = "test",
srcs = ["test.cc"],
deps = [
":bit",
":cstddef",
":iterator",
":pw_polyfill",
Expand Down
5 changes: 4 additions & 1 deletion pw_polyfill/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ pw_source_set("bit") {
":standard_library_public",
":bit_overrides_config",
]
public_deps = [ ":standard_library" ]
public_deps = [
":standard_library",
"$dir_pw_bytes:bit",
]
remove_public_deps = [ "*" ]
inputs = [ "bit_public_overrides/bit" ]
public = [ "standard_library_public/pw_polyfill/standard_library/bit.h" ]
Expand Down
1 change: 1 addition & 0 deletions pw_polyfill/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pw_add_module_library(pw_polyfill.bit
bit_public_overrides
standard_library_public
PUBLIC_DEPS
pw_bytes.bit
pw_polyfill.standard_library
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@
// the License.
#pragma once

#include "pw_bytes/bit.h"
#include "pw_polyfill/standard_library/namespace.h"

_PW_POLYFILL_BEGIN_NAMESPACE_STD

#ifndef __cpp_lib_endian
#define __cpp_lib_endian 201907L

enum class endian {
little = __ORDER_LITTLE_ENDIAN__,
big = __ORDER_BIG_ENDIAN__,
native = __BYTE_ORDER__,
};
_PW_POLYFILL_BEGIN_NAMESPACE_STD

#endif // __cpp_lib_endian
using pw::endian;

_PW_POLYFILL_END_NAMESPACE_STD

#endif // __cpp_lib_endian
Loading

0 comments on commit ed6629a

Please sign in to comment.