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
15 changes: 13 additions & 2 deletions .github/workflows/shorebird_engine_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ jobs:
# shorebird_unittests covers shell/common/shorebird, runtime/shorebird
# and the Rust updater. flutter_linux_sources adds the Linux embedder's
# Shorebird code (fl_shorebird.cc, fl_engine.cc), and shell_unittests
# holds the Shell's Shorebird boot-flow tests.
# holds the Shell's Shorebird boot-flow tests. licenses_cpp_testrunner
# covers Shorebird's --package_licenses_path (shorebird/sbom).
- name: 🔨 Build
run: ninja -C out/host_profile_unopt shorebird_unittests shell_unittests flutter/shell/platform/linux:flutter_linux_sources
run: ninja -C out/host_profile_unopt shorebird_unittests shell_unittests flutter/shell/platform/linux:flutter_linux_sources flutter/tools/licenses_cpp:licenses_cpp_testrunner
working-directory: engine/src

# From exe.unstripped/, where run_tests.py (the release's engine tests)
Expand All @@ -220,6 +221,16 @@ jobs:
run: ./out/host_profile_unopt/exe.unstripped/shorebird_unittests
working-directory: engine/src

# Its tests commit to temporary git repos, which needs an identity.
- name: 🧪 Run licenses_cpp tests
run: ./out/host_profile_unopt/licenses_cpp_testrunner
working-directory: engine/src
env:
GIT_AUTHOR_NAME: licenses_cpp test
GIT_AUTHOR_EMAIL: test@example.com
GIT_COMMITTER_NAME: licenses_cpp test
GIT_COMMITTER_EMAIL: test@example.com

- name: 🧪 Run Shorebird shell_unittests
run: ./out/host_profile_unopt/shell_unittests '--gtest_filter=*Shorebird*'
working-directory: engine/src
Expand Down
48 changes: 48 additions & 0 deletions .github/workflows/shorebird_sbom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Tests shorebird/sbom, which _build_engine runs to write the CycloneDX SBOMs
# published beside the engine artifacts built into apps. Also checks that the
# SBOM specs in shorebird/ci name real artifacts and that the matcher-to-SPDX
# table names real licenses_cpp matchers.
name: shorebird_sbom

on:
pull_request:
paths:
- shorebird/sbom/**
- shorebird/ci/**
- engine/src/flutter/tools/licenses_cpp/data/licenses/**
- DEPS
- .github/workflows/shorebird_sbom.yml
push:
branches:
- shorebird/dev
paths:
- shorebird/sbom/**
- shorebird/ci/**
- engine/src/flutter/tools/licenses_cpp/data/licenses/**
- DEPS
- .github/workflows/shorebird_sbom.yml

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: 📚 Git Checkout
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
sparse-checkout: |
DEPS
shorebird
engine/src/flutter/tools/licenses_cpp/data
engine/src/flutter/shell/platform/android
engine/src/flutter/shell/platform/common/client_wrapper
engine/src/flutter/shell/platform/windows/client_wrapper
engine/src/flutter/lib/ui
engine/src/flutter/sky/packages/sky_engine
persist-credentials: false

- name: 🧪 Test shorebird/sbom
run: python3 shorebird/sbom/sbom_test.py
68 changes: 64 additions & 4 deletions engine/src/flutter/tools/licenses_cpp/src/license_checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "flutter/tools/licenses_cpp/src/license_checker.h"

#include <unistd.h>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iostream>
Expand All @@ -17,6 +18,7 @@
#include "flutter/tools/licenses_cpp/src/filter.h"
#include "flutter/tools/licenses_cpp/src/mmap_file.h"
#include "third_party/abseil-cpp/absl/container/btree_map.h"
#include "third_party/abseil-cpp/absl/container/btree_set.h"
#include "third_party/abseil-cpp/absl/container/flat_hash_set.h"
#include "third_party/abseil-cpp/absl/log/log.h"
#include "third_party/abseil-cpp/absl/log/vlog_is_on.h"
Expand Down Expand Up @@ -205,9 +207,38 @@ Package GetPackage(const Data& data,
return result;
}

// Shorebird: writes `value` as a JSON string.
void WriteJsonString(std::ostream& out, std::string_view value) {
out << '"';
for (char c : value) {
switch (c) {
case '"':
out << "\\\"";
break;
case '\\':
out << "\\\\";
break;
default:
if (static_cast<unsigned char>(c) < 0x20) {
char escaped[7];
std::snprintf(escaped, sizeof(escaped), "\\u%04x", c);
out << escaped;
} else {
out << c;
}
}
}
out << '"';
}

class LicenseMap {
public:
void Add(std::string_view package, std::string_view license) {
void Add(std::string_view package,
std::string_view license,
std::string_view matcher = "") {
if (!matcher.empty()) {
package_matchers_[std::string(package)].emplace(std::string(matcher));
}
auto package_emplace_result =
map_.try_emplace(license, absl::flat_hash_set<std::string>());
absl::flat_hash_set<std::string>& comment_set =
Expand All @@ -225,8 +256,29 @@ class LicenseMap {
}
}

// Shorebird: {"<package>": ["<matcher>", ...], ...}, sorted.
void WritePackageLicenses(std::ostream& out) {
out << "{";
bool first_package = true;
for (const auto& [package, matchers] : package_matchers_) {
out << (first_package ? "\n " : ",\n ");
first_package = false;
WriteJsonString(out, package);
out << ": [";
bool first_matcher = true;
for (const std::string& matcher : matchers) {
out << (first_matcher ? "" : ", ");
first_matcher = false;
WriteJsonString(out, matcher);
}
out << "]";
}
out << "\n}\n";
}

private:
absl::btree_map<std::string, absl::flat_hash_set<std::string>> map_;
absl::btree_map<std::string, absl::btree_set<std::string>> package_matchers_;
absl::flat_hash_set<std::string> license_files_;
};

Expand Down Expand Up @@ -255,7 +307,8 @@ absl::Status MatchLicenseFile(const fs::path& path,

if (matches.ok()) {
for (const Catalog::Match& match : matches.value()) {
license_map->Add(package.name, match.GetMatchedText());
license_map->Add(package.name, match.GetMatchedText(),
match.GetMatcher());
VLOG(1) << "OK: " << path << " : " << match.GetMatcher();
}
} else {
Expand Down Expand Up @@ -300,7 +353,8 @@ bool ProcessSourceCode(const fs::path& relative_path,
if (matches.ok()) {
did_find_copyright = true;
for (const Catalog::Match& match : matches.value()) {
license_map->Add(package.name, match.GetMatchedText());
license_map->Add(package.name, match.GetMatchedText(),
match.GetMatcher());
VLOG(1) << "OK: " << relative_path.lexically_normal() << " : "
<< match.GetMatcher();
}
Expand Down Expand Up @@ -379,7 +433,7 @@ bool ProcessNotices(const fs::path& relative_path,
if (matches.ok()) {
for (const Catalog::Match& match : matches.value()) {
for (std::string_view project : projects) {
license_map->Add(project, match.GetMatchedText());
license_map->Add(project, match.GetMatchedText(), match.GetMatcher());
}
VLOG(1) << "OK: " << relative_path.lexically_normal() << " : "
<< match.GetMatcher();
Expand Down Expand Up @@ -593,6 +647,9 @@ std::vector<absl::Status> LicenseChecker::Run(
}

state.license_map.Write(licenses);
if (flags.package_licenses != nullptr) {
state.license_map.WritePackageLicenses(*flags.package_licenses);
}
if (!VLOG_IS_ON(1) && IsStdoutTerminal()) {
PrintProgress(count++, git_repos.size());
std::cout << std::endl;
Expand Down Expand Up @@ -650,6 +707,9 @@ int LicenseChecker::FileRun(std::string_view working_dir,
}

state.license_map.Write(licenses);
if (flags.package_licenses != nullptr) {
state.license_map.WritePackageLicenses(*flags.package_licenses);
}
for (const absl::Status& status : state.errors) {
std::cerr << status << "\n";
}
Expand Down
4 changes: 4 additions & 0 deletions engine/src/flutter/tools/licenses_cpp/src/license_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class LicenseChecker {
struct Flags {
bool treat_unmatched_comments_as_errors = false;
std::optional<std::string> root_package_name;
// Shorebird: when set, also writes a JSON object mapping each package to
// the names of the catalog matchers that matched its licenses. Read by
// shorebird/sbom to give SBOM components SPDX ids.
std::ostream* package_licenses = nullptr;
};

static const char* kHeaderLicenseRegex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1005,3 +1005,40 @@ TEST_F(LicenseCheckerTest, OverrideRootPackageName) {
Copyright Test
)output");
}

// Shorebird: --package_licenses_path, read by shorebird/sbom.
TEST_F(LicenseCheckerTest, WritesPackageLicenses) {
absl::StatusOr<fs::path> temp_path = MakeTempDir();
ASSERT_TRUE(temp_path.ok());

absl::StatusOr<Data> data = MakeTestData();
ASSERT_TRUE(data.ok());

fs::current_path(*temp_path);
ASSERT_EQ(std::system("mkdir -p third_party/foobar"), 0);
ASSERT_TRUE(WriteFile(kHeader, *temp_path / "main.cc").ok());
ASSERT_TRUE(
WriteFile(kHeader, *temp_path / "third_party" / "foobar" / "foo.cc")
.ok());
ASSERT_TRUE(
WriteFile(kLicense, *temp_path / "third_party" / "foobar" / "LICENSE")
.ok());
Repo repo;
repo.Add("main.cc");
repo.Add("third_party/foobar/foo.cc");
repo.Add("third_party/foobar/LICENSE");
ASSERT_TRUE(repo.Commit().ok());

std::stringstream package_licenses;
LicenseChecker::Flags flags = {.package_licenses = &package_licenses};
std::stringstream ss;
std::vector<absl::Status> errors =
LicenseChecker::Run(temp_path->string(), ss, *data, flags);
EXPECT_EQ(errors.size(), 0u);

EXPECT_EQ(package_licenses.str(), R"output({
"engine": ["header"],
"foobar": ["header", "test"]
}
)output");
}
17 changes: 17 additions & 0 deletions engine/src/flutter/tools/licenses_cpp/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ ABSL_FLAG(std::optional<std::string>,
root_package,
std::nullopt,
"Name of the root package.");
ABSL_FLAG(std::optional<std::string>,
package_licenses_path,
std::nullopt,
"Shorebird: path to write each package's matched license names to, "
"as JSON.");

namespace {
int Run(std::string_view working_dir,
Expand Down Expand Up @@ -103,6 +108,18 @@ int main(int argc, char** argv) {
flags.treat_unmatched_comments_as_errors =
absl::GetFlag(FLAGS_treat_unmatched_comments_as_errors);
flags.root_package_name = absl::GetFlag(FLAGS_root_package);
std::ofstream package_licenses;
std::optional<std::string> package_licenses_path =
absl::GetFlag(FLAGS_package_licenses_path);
if (package_licenses_path.has_value()) {
package_licenses.open(package_licenses_path.value());
if (!package_licenses.good()) {
std::cerr << "Unable to write to '" << package_licenses_path.value()
<< "'.";
return 1;
}
flags.package_licenses = &package_licenses;
}
if (input.has_value()) {
if (include_filter.has_value()) {
std::cerr << "`--input_filter` not supported with `--input`"
Expand Down
4 changes: 2 additions & 2 deletions shorebird/ci/compose.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"--simulator-arm64-out-dir": "ci/ios_debug_sim_arm64"
},
"artifacts": [
{"src": "artifacts.zip", "dst": "flutter_infra_release/flutter/$engine/ios-release/artifacts.zip"},
{"src": "artifacts.zip", "dst": "flutter_infra_release/flutter/$engine/ios-release/artifacts.zip", "sbom": ["ios-release"]},
{"src": "Flutter.framework.dSYM.zip", "dst": "flutter_infra_release/flutter/$engine/ios-release/Flutter.framework.dSYM.zip"}
]
},
Expand All @@ -22,7 +22,7 @@
"--x64-out-dir": "ci/host_release"
},
"artifacts": [
{"src": "framework.zip", "dst": "flutter_infra_release/flutter/$engine/darwin-x64-release/framework.zip"},
{"src": "framework.zip", "dst": "flutter_infra_release/flutter/$engine/darwin-x64-release/framework.zip", "sbom": ["macos-arm64-release", "macos-x64-release"]},
{"src": "FlutterMacOS.framework.dSYM.zip", "dst": "flutter_infra_release/flutter/$engine/darwin-x64/FlutterMacOS.framework.dSYM.zip"}
]
},
Expand Down
Loading
Loading