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
40 changes: 30 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
# cpu_features
[![Linux Status][linux_svg]][linux_link]
[![Macos Status][macos_svg]][macos_link]
[![Windows Status][windows_svg]][windows_link]

[linux_svg]: https://github.com/google/cpu_features/actions/workflows/amd64_linux.yml/badge.svg?branch=main
[linux_link]: https://github.com/google/cpu_features/actions/workflows/amd64_linux.yml
[macos_svg]: https://github.com/google/cpu_features/actions/workflows/amd64_macos.yml/badge.svg?branch=main
[macos_link]: https://github.com/google/cpu_features/actions/workflows/amd64_macos.yml
[windows_svg]: https://github.com/google/cpu_features/actions/workflows/amd64_windows.yml/badge.svg?branch=main
[windows_link]: https://github.com/google/cpu_features/actions/workflows/amd64_windows.yml

A cross-platform C library to retrieve CPU features (such as available
instructions) at runtime.

# GitHub-CI Status

[comment]: <> (The following lines are generated by "scripts/generate_badges.d" that you can run online https://run.dlang.io/)

| Os | amd64 | AArch64 | ARM | MIPS |
| :-- | --: | --: | --: | --: |
| FreeBSD | [![][i1a0]][l1a0]<br/>![][d1] | ![][d0]<br/>![][d1] | ![][d0]<br/>![][d1] | ![][d0]<br/>![][d1] |
| Linux | [![][i2a0]][l2a0]<br/>[![][i2a1]][l2a1] | [![][i2b0]][l2b0]<br/>![][d1] | [![][i2c0]][l2c0]<br/>![][d1] | [![][i2d0]][l2d0]<br/>![][d1] |
| MacOS | [![][i3a0]][l3a0]<br/>![][d1] | ![][d0]<br/>![][d1] | ![][d0]<br/>![][d1] | ![][d0]<br/>![][d1] |
| Windows | [![][i4a0]][l4a0]<br/>![][d1] | ![][d0]<br/>![][d1] | ![][d0]<br/>![][d1] | ![][d0]<br/>![][d1] |

[d0]: https://img.shields.io/badge/CMake-N%2FA-lightgrey
[d1]: https://img.shields.io/badge/Bazel-N%2FA-lightgrey
[i1a0]: https://img.shields.io/github/workflow/status/google/cpu_features/amd64%20FreeBSD%20CMake/main?label=CMake
[i2a0]: https://img.shields.io/github/workflow/status/google/cpu_features/amd64%20Linux%20CMake/main?label=CMake
[i2a1]: https://img.shields.io/github/workflow/status/google/cpu_features/amd64%20Linux%20Bazel/main?label=Bazel
[i2b0]: https://img.shields.io/github/workflow/status/google/cpu_features/AArch64%20Linux%20CMake/main?label=CMake
[i2c0]: https://img.shields.io/github/workflow/status/google/cpu_features/ARM%20Linux%20CMake/main?label=CMake
[i2d0]: https://img.shields.io/github/workflow/status/google/cpu_features/MIPS%20Linux%20CMake/main?label=CMake
[i3a0]: https://img.shields.io/github/workflow/status/google/cpu_features/amd64%20MacOS%20CMake/main?label=CMake
[i4a0]: https://img.shields.io/github/workflow/status/google/cpu_features/amd64%20Windows%20CMake/main?label=CMake
[l1a0]: https://github.com/google/cpu_features/actions/workflows/amd64_freebsd_cmake.yml
[l2a0]: https://github.com/google/cpu_features/actions/workflows/amd64_linux_cmake.yml
[l2a1]: https://github.com/google/cpu_features/actions/workflows/amd64_linux_bazel.yml
[l2b0]: https://github.com/google/cpu_features/actions/workflows/aarch64_linux_cmake.yml
[l2c0]: https://github.com/google/cpu_features/actions/workflows/arm_linux_cmake.yml
[l2d0]: https://github.com/google/cpu_features/actions/workflows/mips_linux_cmake.yml
[l3a0]: https://github.com/google/cpu_features/actions/workflows/amd64_macos_cmake.yml
[l4a0]: https://github.com/google/cpu_features/actions/workflows/amd64_windows_cmake.yml

## Table of Contents

- [Design Rationale](#rationale)
Expand Down
5 changes: 5 additions & 0 deletions bazel/ci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Usage
To build tests with bazel
```sh
bazel test -s --verbose_failures //...
```
26 changes: 1 addition & 25 deletions cmake/ci/README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
# GitHub-CI Status
| Os | amd64 | AArch64 | ARM | MIPS |
| :-- | --: | --: | --: | --: |
| FreeBSD | [![][img_1a]][lnk_1a] | ![][img_na] | ![][img_na] | ![][img_na] |
| Linux | [![][img_2a]][lnk_2a] | [![][img_2b]][lnk_2b] | [![][img_2c]][lnk_2c] | [![][img_2d]][lnk_2d] |
| MacOS | [![][img_3a]][lnk_3a] | ![][img_na] | ![][img_na] | ![][img_na] |
| Windows | [![][img_4a]][lnk_4a] | ![][img_na] | ![][img_na] | ![][img_na] |

[img_na]: https://img.shields.io/badge/build-N%2FA-lightgrey
[lnk_1a]: https://github.com/google/cpu_features/actions/workflows/amd64_freebsd_cmake.yml
[img_1a]: https://img.shields.io/github/workflow/status/google/cpu_features/amd64%20FreeBSD%20CMake/main
[lnk_2a]: https://github.com/google/cpu_features/actions/workflows/amd64_linux_cmake.yml
[img_2a]: https://img.shields.io/github/workflow/status/google/cpu_features/amd64%20Linux%20CMake/main
[lnk_3a]: https://github.com/google/cpu_features/actions/workflows/amd64_macos_cmake.yml
[img_3a]: https://img.shields.io/github/workflow/status/google/cpu_features/amd64%20MacOS%20CMake/main
[lnk_4a]: https://github.com/google/cpu_features/actions/workflows/amd64_windows_cmake.yml
[img_4a]: https://img.shields.io/github/workflow/status/google/cpu_features/amd64%20Windows%20CMake/main
[lnk_2b]: https://github.com/google/cpu_features/actions/workflows/aarch64_linux_cmake.yml
[img_2b]: https://img.shields.io/github/workflow/status/google/cpu_features/AArch64%20Linux%20CMake/main
[lnk_2c]: https://github.com/google/cpu_features/actions/workflows/arm_linux_cmake.yml
[img_2c]: https://img.shields.io/github/workflow/status/google/cpu_features/ARM%20Linux%20CMake/main
[lnk_2d]: https://github.com/google/cpu_features/actions/workflows/mips_linux_cmake.yml
[img_2d]: https://img.shields.io/github/workflow/status/google/cpu_features/MIPS%20Linux%20CMake/main

## Makefile/Docker testing
To test the build on various distro, we are using docker containers and a Makefile for orchestration.

Expand All @@ -42,7 +18,7 @@ make

note: you can also use from top directory
```sh
make --directory=ci
make --directory=cmake/ci
```

### Example
Expand Down
126 changes: 87 additions & 39 deletions scripts/generate_badges.d
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import std.algorithm : each, map, cartesianProduct, filter;
import std.algorithm : each, map, cartesianProduct, filter, joiner, sort, uniq;
import std.array : array;
import std.conv : to;
import std.format;
import std.range : chain, only;
import std.stdio;
import std.traits : EnumMembers;

enum BuildSystem
{
CMake,
Bazel
}

enum Cpu
{
amd64,
Expand All @@ -22,46 +28,73 @@ enum Os
Windows
}

struct Configuration
struct Badge
{
const:

Cpu cpu;
Os os;
BuildSystem build_system;

bool disabled()
string id()
{
import std.algorithm.comparison : among;

return os.among(Os.FreeBSD, Os.Windows, Os.MacOS) && cpu.among(Cpu.AArch64, Cpu.ARM, Cpu
.MIPS);
return format("%d%c%d", cast(uint)(os) + 1, cast(char)('a' + cpu), cast(uint)(build_system));
}

string cell()
string disabled_image_ref()
{
return format("%d%c", cast(uint)(os) + 1, cast(char)('a' + cpu));
return format("[d%d]", cast(uint)(build_system));
}

string link_ref()
{
return format("[lnk_%s]", cell());
return format("[l%s]", id());
}

string image_ref()
{
return format("[img_%s]", cell());
return format("[i%s]", id());
}

string cell_text()
bool enabled()
{
if (disabled())
return "![][img_na]";
return format("[![]%s]%s", image_ref, link_ref);
final switch (build_system)
{
case BuildSystem.CMake:
return os == Os.Linux || cpu == Cpu.amd64;
case BuildSystem.Bazel:
return os == Os.Linux && cpu == Cpu.amd64;
}
}

}
string text()
{
if (enabled())
return format("[![]%s]%s", image_ref, link_ref);
return format("![]%s", disabled_image_ref);
}

string disabled_image_link()
{
return format("%s: https://img.shields.io/badge/%s-N%%2FA-lightgrey", disabled_image_ref, build_system);
}

immutable allCpus = [EnumMembers!Cpu];
immutable allOses = [EnumMembers!Os];
string link_decl()
{
import std.uni : toLower;

const filename = toLower(format("%s_%s_%s.yml", cpu, os, build_system));
return format("%s: https://github.com/google/cpu_features/actions/workflows/%s", link_ref, filename);
}

string image_decl()
{
import std.uri : encode;

const worflow_name = encode(format("%s %s %s", cpu, os, build_system));
return format("%s: https://img.shields.io/github/workflow/status/google/cpu_features/%s/main?label=%s", image_ref, worflow_name, build_system);
}
}

auto tableHeader(in Cpu[] cpus)
{
Expand All @@ -73,40 +106,55 @@ auto tableAlignment(in Cpu[] cpus)
return chain(only(":--"), cpus.map!(v => "--:")).array;
}

auto tableRow(in Os os, in Cpu[] cpus)
auto tableCell(Range)(in Os os, in Cpu cpu, Range badges)
{
return chain(only(os.to!string), cpus.map!(cpu => Configuration(cpu, os).cell_text())).array;
return badges
.filter!(b => b.cpu == cpu && b.os == os)
.map!(b => b.text())
.joiner("<br/>")
.to!string;
}

auto tableRows(in Os[] oses, in Cpu[] cpus)
auto tableRow(Range)(in Os os, in Cpu[] cpus, Range badges)
{
return oses.map!(os => tableRow(os, cpus)).array;
return chain(only(os.to!string), cpus.map!(cpu => tableCell(os, cpu, badges))).array;
}

auto table(in Os[] oses, in Cpu[] cpus)
auto tableRows(Range)(in Os[] oses, in Cpu[] cpus, Range badges)
{
return chain(only(tableHeader(cpus)), only(tableAlignment(cpus)), tableRows(oses, cpus));
return oses.map!(os => tableRow(os, cpus, badges)).array;
}

auto table(Range)(in Os[] oses, in Cpu[] cpus, Range badges)
{
return chain(only(tableHeader(cpus)), only(tableAlignment(cpus)), tableRows(oses, cpus, badges));
}

void main()
{
immutable allCpus = [EnumMembers!Cpu];
immutable allOses = [EnumMembers!Os];
immutable allBuildSystems = [EnumMembers!BuildSystem];

auto badges = cartesianProduct(allCpus, allOses, allBuildSystems).map!(
t => Badge(t[0], t[1], t[2]));

writefln("%(|%-( %-21s |%) |\n%) |", table(allOses, allCpus));
writefln("%(|%-( %s |%) |\n%) |", table(allOses, allCpus, badges));
writeln();
writeln("[img_na]: https://img.shields.io/badge/build-N%2FA-lightgrey");
cartesianProduct(allCpus, allOses)
.map!(t => Configuration(t[0], t[1]))
.filter!(conf => !conf.disabled)
.each!((conf) {
import std.uni : toLower;
import std.uri : encode;

const filename = toLower(format("%s_%s_%s.yml", conf.cpu, conf.os, "cmake"));
writefln("%s: https://github.com/google/cpu_features/actions/workflows/%s", conf.link_ref, filename);

const worflow_name = encode(format("%s %s %s", conf.cpu, conf.os, "CMake"));
writefln("%s: https://img.shields.io/github/workflow/status/google/cpu_features/%s/main", conf.image_ref, worflow_name);
});
badges
.filter!(b => !b.enabled)
.map!(b => b.disabled_image_link())
.array
.sort
.uniq
.each!writeln;

badges
.filter!(b => b.enabled)
.map!(b => [b.link_decl(), b.image_decl()])
.joiner()
.array
.sort
.uniq
.each!writeln;
}