Skip to content

Commit

Permalink
emboss: Add CMake support for emboss
Browse files Browse the repository at this point in the history
Adds support for generating emboss headers from CMake and building
libraries against the emboss runtime.

Tests:

cmake -B out/cmake_host -S "$PW_ROOT" -G Ninja \
  -DCMAKE_TOOLCHAIN_FILE=$PW_ROOT/pw_toolchain/host_clang/toolchain.cmake \
  -Ddir_pw_third_party_emboss=${PW_ROOT}/third_party/emboss/src
ninja -C out/cmake_host pw_run_tests.pw_bluetooth

cmake -B out/cmake_host -S "$PW_ROOT" -G Ninja \
  -DCMAKE_TOOLCHAIN_FILE=$PW_ROOT/pw_toolchain/host_clang/toolchain.cmake \
  -Ddir_pw_third_party_emboss=
ninja -C out/cmake_host pw_run_tests.pw_bluetooth

Bug: 326500136
Bug: 326499587

Change-Id: I9c685ecfffb28d8118f1f1b4139a59dd184a59d7
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/194400
Reviewed-by: Wyatt Hepler <[email protected]>
Pigweed-Auto-Submit: Eric Rahm <[email protected]>
  • Loading branch information
EricRahm committed Mar 4, 2024
1 parent 7b799c2 commit 89f5a3a
Show file tree
Hide file tree
Showing 16 changed files with 409 additions and 55 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ add_subdirectory(pw_varint EXCLUDE_FROM_ALL)
add_subdirectory(pw_work_queue EXCLUDE_FROM_ALL)

add_subdirectory(third_party/nanopb EXCLUDE_FROM_ALL)
add_subdirectory(third_party/emboss EXCLUDE_FROM_ALL)
add_subdirectory(third_party/freertos EXCLUDE_FROM_ALL)
add_subdirectory(third_party/fuchsia EXCLUDE_FROM_ALL)
add_subdirectory(third_party/fuzztest EXCLUDE_FROM_ALL)
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ git_repository(

git_repository(
name = "com_google_emboss",
commit = "69bb1372053fc3cb8a16180497970465ae2ed66d",
commit = "35e21b10019ded9ae14041af9b8e49659d9b327a",
remote = "https://pigweed.googlesource.com/third_party/github/google/emboss",
)

Expand Down
4 changes: 4 additions & 0 deletions pw_bluetooth/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ cc_library(
emboss_cc_library(
name = "_emboss_hci_commands",
srcs = ["public/pw_bluetooth/hci_commands.emb"],
import_dirs = ["public"],
visibility = ["//visibility:private"],
deps = [":_emboss_hci_common"],
)
Expand All @@ -142,6 +143,7 @@ cc_library(
emboss_cc_library(
name = "_emboss_hci_events",
srcs = ["public/pw_bluetooth/hci_events.emb"],
import_dirs = ["public"],
visibility = ["//visibility:private"],
deps = [":_emboss_hci_common"],
)
Expand All @@ -155,6 +157,7 @@ cc_library(
emboss_cc_library(
name = "_emboss_hci_vendor",
srcs = ["public/pw_bluetooth/hci_vendor.emb"],
import_dirs = ["public"],
visibility = ["//visibility:private"],
deps = [":_emboss_hci_common"],
)
Expand All @@ -168,6 +171,7 @@ cc_library(
emboss_cc_library(
name = "_emboss_hci_test",
srcs = ["public/pw_bluetooth/hci_test.emb"],
import_dirs = ["public"],
visibility = ["//visibility:private"],
deps = [":_emboss_hci_common"],
)
Expand Down
4 changes: 4 additions & 0 deletions pw_bluetooth/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ if (dir_pw_third_party_emboss != "") {
public_configs = [ ":emboss_include_path" ]
source = "public/pw_bluetooth/hci_commands.emb"
imports = [ "public/pw_bluetooth/hci_common.emb" ]
import_dirs = [ "public" ]
deps = [ ":emboss_hci_common" ]
}

Expand All @@ -108,20 +109,23 @@ if (dir_pw_third_party_emboss != "") {
public_configs = [ ":emboss_include_path" ]
source = "public/pw_bluetooth/hci_events.emb"
imports = [ "public/pw_bluetooth/hci_common.emb" ]
import_dirs = [ "public" ]
deps = [ ":emboss_hci_common" ]
}

emboss_cc_library("emboss_hci_vendor") {
public_configs = [ ":emboss_include_path" ]
source = "public/pw_bluetooth/hci_vendor.emb"
imports = [ "public/pw_bluetooth/hci_common.emb" ]
import_dirs = [ "public" ]
deps = [ ":emboss_hci_common" ]
}

emboss_cc_library("emboss_hci_test") {
public_configs = [ ":emboss_include_path" ]
source = "public/pw_bluetooth/hci_test.emb"
imports = [ "public/pw_bluetooth/hci_common.emb" ]
import_dirs = [ "public" ]
deps = [ ":emboss_hci_common" ]
}

Expand Down
83 changes: 83 additions & 0 deletions pw_bluetooth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,86 @@ pw_add_test(pw_bluetooth.uuid_test
GROUPS
pw_bluetooth
)

###############################################################################
## Everything below here is intended to be emboss only ##
## and will be skipped if emboss isn't enabled. ##
###############################################################################
if("${dir_pw_third_party_emboss}" STREQUAL "")
# Skip emboss defs if it's not configured
return()
endif()

include($ENV{PW_ROOT}/third_party/emboss/emboss.cmake)

emboss_cc_library(pw_bluetooth.hci_common
SOURCES
public/pw_bluetooth/hci_common.emb
)

emboss_cc_library(pw_bluetooth.hci_commands
SOURCES
public/pw_bluetooth/hci_commands.emb
IMPORT_DIRS
public
DEPS
pw_bluetooth.hci_common
)

emboss_cc_library(pw_bluetooth.hci_events
SOURCES
public/pw_bluetooth/hci_events.emb
IMPORT_DIRS
public
DEPS
pw_bluetooth.hci_common
)

emboss_cc_library(pw_bluetooth.hci_test
SOURCES
public/pw_bluetooth/hci_test.emb
IMPORT_DIRS
public
DEPS
pw_bluetooth.hci_common
pw_bluetooth.l2cap_frames
)

emboss_cc_library(pw_bluetooth.hci_vendor
SOURCES
public/pw_bluetooth/hci_vendor.emb
IMPORT_DIRS
public
DEPS
pw_bluetooth.hci_common
)

emboss_cc_library(pw_bluetooth.hci_data
SOURCES
public/pw_bluetooth/hci_data.emb
)

emboss_cc_library(pw_bluetooth.l2cap_frames
SOURCES
public/pw_bluetooth/l2cap_frames.emb
)

pw_target_link_targets("pw_bluetooth._public_config"
INTERFACE
pw_bluetooth.hci_common
pw_bluetooth.hci_commands
pw_bluetooth.hci_data
pw_bluetooth.hci_events
pw_bluetooth.hci_vendor
pw_bluetooth.l2cap_frames
)

pw_add_test(pw_bluetooth.emboss_test
SOURCES
emboss_test.cc
PRIVATE_DEPS
pw_bluetooth.hci_test
pw_bluetooth
GROUPS
pw_bluetooth
)
2 changes: 1 addition & 1 deletion pw_bluetooth/public/pw_bluetooth/hci_commands.emb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# and types found in the Bluetooth Core Specification. The Emboss compiler is
# used to generate a C++ header from this file.

import "pw_bluetooth/public/pw_bluetooth/hci_common.emb" as hci
import "pw_bluetooth/hci_common.emb" as hci

[$default byte_order: "LittleEndian"]
[(cpp) namespace: "pw::bluetooth::emboss"]
Expand Down
2 changes: 1 addition & 1 deletion pw_bluetooth/public/pw_bluetooth/hci_events.emb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# and types found in the Bluetooth Core Specification. The Emboss compiler is
# used to generate a C++ header from this file.

import "pw_bluetooth/public/pw_bluetooth/hci_common.emb" as hci
import "pw_bluetooth/hci_common.emb" as hci

[$default byte_order: "LittleEndian"]
[(cpp) namespace: "pw::bluetooth::emboss"]
Expand Down
2 changes: 1 addition & 1 deletion pw_bluetooth/public/pw_bluetooth/hci_test.emb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# and types found in the Bluetooth Core Specification. The Emboss compiler is
# used to generate a C++ header from this file.

import "pw_bluetooth/public/pw_bluetooth/hci_common.emb" as hci
import "pw_bluetooth/hci_common.emb" as hci

[$default byte_order: "LittleEndian"]
[(cpp) namespace: "pw::bluetooth::emboss"]
Expand Down
2 changes: 1 addition & 1 deletion pw_bluetooth/public/pw_bluetooth/hci_vendor.emb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# NOTE: The definitions below are incomplete. They get added as needed.
# This list will grow as we support more vendor features.

import "pw_bluetooth/public/pw_bluetooth/hci_common.emb" as hci
import "pw_bluetooth/hci_common.emb" as hci

[$default byte_order: "LittleEndian"]
[(cpp) namespace: "pw::bluetooth::vendor::android_hci"]
Expand Down
2 changes: 2 additions & 0 deletions pw_build/cmake.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ CMake convenience functions are defined in ``pw_build/pigweed.cmake``.
messages cannot be used to catch problems during the CMake configuration
phase.
* ``pw_parse_arguments`` -- Helper to parse CMake function arguments.
* ``pw_rebase_paths`` -- Helper to update a set of file paths to be rooted on a
new directory.

See ``pw_build/pigweed.cmake`` for the complete documentation of these
functions.
Expand Down
50 changes: 50 additions & 0 deletions pw_build/pigweed.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -876,3 +876,53 @@ function(pw_add_error_target NAME)
)
add_dependencies("${NAME}" "${NAME}._error_message")
endfunction(pw_add_error_target)

# Rebases a set of files to a new root path and optionally appends extensions
# to them. This is particularly useful for file generators.
#
# Required Arguments:
#
# <var> - Variable to store the rebased file list in.
# <new_root> - The new root to rebase file paths onto.
# <root> - The current root to rebase off of.
# <files> - The list of files to rebase.
# <extensions> - List of extensions to replace the existing file extensions
# with.
#
# Examples:
#
# list(APPEND files "public/proj/foo.def" "public/proj/bar.def")
#
# pw_rebase_paths(out_files "/tmp" "${CMAKE_CURRENT_SOURCE_DIR}/public"
# ${files} "")
# out_files => [ "/tmp/proj/foo.def", "/tmp/proj/bar.def" ]
#
# pw_rebase_paths(out_files "/tmp" "${CMAKE_CURRENT_SOURCE_DIR}/public"
# ${files} ".h")
# out_files => [ "/tmp/proj/foo.h", "/tmp/proj/bar.h" ]
#
# list (APPEND exts ".h" ".cc")
# pw_rebase_paths(out_files "/tmp" "${CMAKE_CURRENT_SOURCE_DIR}/public"
# ${files} ${exts})
# out_files => [ "/tmp/proj/foo.h", "/tmp/proj/bar.h",
# "/tmp/proj/foo.cc", "/tmp/proj/bar.cc" ]
function(pw_rebase_paths VAR NEW_ROOT ROOT FILES EXTENSIONS)
foreach(file IN LISTS FILES)
get_filename_component(file "${file}" ABSOLUTE)
file(RELATIVE_PATH file "${ROOT}" "${file}")

if("${EXTENSIONS}" STREQUAL "")
list(APPEND mirrored_files "${NEW_ROOT}/${file}")
else()
foreach(ext IN LISTS EXTENSIONS)
get_filename_component(dir "${file}" DIRECTORY)
get_filename_component(name "${file}" NAME_WE)
list(APPEND mirrored_files "${NEW_ROOT}/${dir}/${name}${ext}")
endforeach()
endif()
endforeach()

set("${VAR}"
"${mirrored_files}"
PARENT_SCOPE)
endfunction(pw_rebase_paths)
9 changes: 8 additions & 1 deletion pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ def docs_build(ctx: PresubmitContext) -> None:

def _run_cmake(ctx: PresubmitContext, toolchain='host_clang') -> None:
build.install_package(ctx, 'nanopb')
build.install_package(ctx, 'emboss')

env = None
if 'clang' in toolchain:
Expand All @@ -647,6 +648,7 @@ def _run_cmake(ctx: PresubmitContext, toolchain='host_clang') -> None:
'-DCMAKE_EXPORT_COMPILE_COMMANDS=1',
f'-Ddir_pw_third_party_nanopb={ctx.package_root / "nanopb"}',
'-Dpw_third_party_nanopb_ADD_SUBDIRECTORY=ON',
f'-Ddir_pw_third_party_emboss={ctx.package_root / "emboss"}',
env=env,
)

Expand All @@ -656,7 +658,12 @@ def _run_cmake(ctx: PresubmitContext, toolchain='host_clang') -> None:
)
def cmake_clang(ctx: PresubmitContext):
_run_cmake(ctx, toolchain='host_clang')
build.ninja(ctx, 'pw_apps', 'pw_run_tests.modules')
build.ninja(
ctx,
'pw_apps',
'pw_run_tests.modules',
'pw_run_tests.pw_bluetooth',
)
build.gn_check(ctx)


Expand Down
25 changes: 3 additions & 22 deletions pw_protobuf_compiler/proto.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ function(pw_proto_library NAME)
endforeach()

# Mirror the sources to the output directory with the specified prefix.
_pw_rebase_paths(
pw_rebase_paths(
sources "${out_dir}/sources/${arg_PREFIX}" "${arg_STRIP_PREFIX}"
"${arg_SOURCES}" "")
_pw_rebase_paths(
pw_rebase_paths(
inputs "${out_dir}/sources/${arg_PREFIX}" "${arg_STRIP_PREFIX}"
"${arg_INPUTS}" "")

Expand Down Expand Up @@ -177,25 +177,6 @@ function(pw_proto_library NAME)
)
endfunction(pw_proto_library)

function(_pw_rebase_paths VAR OUT_DIR ROOT FILES EXTENSIONS)
foreach(file IN LISTS FILES)
get_filename_component(file "${file}" ABSOLUTE)
file(RELATIVE_PATH file "${ROOT}" "${file}")

if ("${EXTENSIONS}" STREQUAL "")
list(APPEND mirrored_files "${OUT_DIR}/${file}")
else()
foreach(ext IN LISTS EXTENSIONS)
get_filename_component(dir "${file}" DIRECTORY)
get_filename_component(name "${file}" NAME_WE)
list(APPEND mirrored_files "${OUT_DIR}/${dir}/${name}${ext}")
endforeach()
endif()
endforeach()

set("${VAR}" "${mirrored_files}" PARENT_SCOPE)
endfunction(_pw_rebase_paths)

# Internal function that invokes protoc through generate_protos.py.
function(_pw_generate_protos TARGET LANGUAGE)
pw_parse_arguments(
Expand All @@ -213,7 +194,7 @@ function(_pw_generate_protos TARGET LANGUAGE)
)

# Determine the names of the compiled output files.
_pw_rebase_paths(outputs
pw_rebase_paths(outputs
"${arg_OUT_DIR}/${LANGUAGE}" "${arg_OUT_DIR}/sources" "${arg_SOURCES}"
"${arg_OUTPUT_EXTS}")

Expand Down
Loading

0 comments on commit 89f5a3a

Please sign in to comment.