Skip to content

Commit f25ad44

Browse files
committed
Format and fix ci
1 parent 93df2f5 commit f25ad44

20 files changed

Lines changed: 772 additions & 605 deletions

.clang-format

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: LLVM
4+
5+
# Indentation
6+
IndentWidth: 4
7+
TabWidth: 4
8+
UseTab: Never
9+
IndentCaseLabels: false
10+
NamespaceIndentation: None
11+
12+
# Braces
13+
BreakBeforeBraces: Attach
14+
AllowShortBlocksOnASingleLine: Empty
15+
AllowShortFunctionsOnASingleLine: Empty
16+
AllowShortIfStatementsOnASingleLine: Never
17+
AllowShortLoopsOnASingleLine: false
18+
19+
# Line length
20+
ColumnLimit: 100
21+
22+
# Spaces
23+
SpaceAfterCStyleCast: false
24+
SpaceAfterTemplateKeyword: true
25+
SpaceBeforeAssignmentOperators: true
26+
SpaceBeforeParens: ControlStatements
27+
SpacesInAngles: false
28+
SpacesInContainerLiterals: true
29+
SpacesInParentheses: false
30+
SpacesInSquareBrackets: false
31+
32+
# Alignment
33+
AlignAfterOpenBracket: Align
34+
AlignConsecutiveAssignments: false
35+
AlignConsecutiveDeclarations: false
36+
AlignEscapedNewlines: Left
37+
AlignOperands: true
38+
AlignTrailingComments: true
39+
40+
# Other
41+
AllowAllParametersOfDeclarationOnNextLine: true
42+
BinPackArguments: true
43+
BinPackParameters: true
44+
BreakConstructorInitializers: BeforeColon
45+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
46+
Cpp11BracedListStyle: true
47+
DerivePointerAlignment: false
48+
PointerAlignment: Left
49+
IncludeBlocks: Preserve
50+
SortIncludes: false
51+
ReflowComments: true
52+
53+
# Keep existing newlines in some cases
54+
MaxEmptyLinesToKeep: 1
55+
KeepEmptyLinesAtTheStartOfBlocks: false
56+
---
57+
Language: C
58+
BasedOnStyle: LLVM
59+
IndentWidth: 4
60+
ColumnLimit: 100
61+
PointerAlignment: Left
62+

.github/workflows/ci.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ on:
77
branches: [ main, master ]
88

99
jobs:
10+
format:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Check format
16+
run: make format-check
17+
1018
build-linux:
1119
runs-on: ubuntu-latest
1220
strategy:
@@ -118,7 +126,7 @@ jobs:
118126
submodules: recursive
119127

120128
- name: Build with C++${{ matrix.std }}
121-
run: make CXXFLAGS="-std=c++${{ matrix.std }} -Wall -Wextra -O2"
129+
run: make CXXFLAGS="-std=c++${{ matrix.std }} -Wall -Wextra -Werror -O2"
122130

123131
- name: Run tests
124132
run: make check

Makefile

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ CXX ?= g++
1616
AR ?= ar
1717

1818
# Base flags
19-
CXXFLAGS ?= -std=c++17 -Wall -Wextra -O2
19+
CXXFLAGS ?= -std=c++17 -Wall -Wextra -Werror -O2
2020
INCLUDES = -I./include -I./extern/json/single_include
2121

2222
# Platform-specific settings
@@ -94,9 +94,10 @@ examples: $(EXAMPLE_BINARIES) $(BINDIR)/c_api_example$(EXE_EXT)
9494
$(BINDIR)/%$(EXE_EXT): $(EXAMPLEDIR)/%.cpp $(STATIC_LIB)
9595
$(CXX) $(CXXFLAGS) $(INCLUDES) $< -L$(LIBDIR) -l$(LIB_NAME) $(LDFLAGS) -o $@
9696

97-
# C example needs special handling (link with C++ runtime)
97+
# C example - compile as C, link with C++ runtime
9898
$(BINDIR)/c_api_example$(EXE_EXT): $(EXAMPLEDIR)/c_api_example.c $(STATIC_LIB)
99-
$(CXX) $(CXXFLAGS) $(INCLUDES) $< -L$(LIBDIR) -l$(LIB_NAME) $(LDFLAGS) -o $@
99+
$(CC) -Wall -Wextra -Werror -O2 $(INCLUDES) -c $< -o $(OBJDIR)/c_api_example.o
100+
$(CXX) $(OBJDIR)/c_api_example.o -L$(LIBDIR) -l$(LIB_NAME) $(LDFLAGS) -o $@
100101

101102
# Build tests
102103
tests: $(TEST_BINARIES)
@@ -168,5 +169,20 @@ regenerate-data:
168169
@echo '#endif // ARCHSPEC_MICROARCHITECTURES_DATA_INC' >> $(SRCDIR)/microarchitectures_data.inc
169170
@echo "Done!"
170171

171-
.PHONY: all directories examples tests check run-examples clean install uninstall debug compile_commands regenerate-data
172+
# Source files for formatting
173+
FORMAT_SOURCES = $(shell find src include examples tests \( -name '*.cpp' -o -name '*.hpp' -o -name '*.h' -o -name '*.c' \) 2>/dev/null)
174+
175+
# Format source code
176+
format:
177+
@echo "Formatting source files..."
178+
@for f in $(FORMAT_SOURCES); do clang-format -i "$$f"; done
179+
@echo "Done!"
180+
181+
# Check formatting (for CI)
182+
format-check:
183+
@echo "Checking format..."
184+
@for f in $(FORMAT_SOURCES); do clang-format --dry-run --Werror "$$f" || exit 1; done
185+
@echo "Format OK!"
186+
187+
.PHONY: all directories examples tests check run-examples clean install uninstall debug compile_commands regenerate-data format format-check
172188

examples/c_api_example.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,45 @@
33
* Example demonstrating the C API for archspec
44
*/
55

6-
#include <stdio.h>
76
#include "archspec/archspec_c.h"
7+
#include <stdio.h>
88

99
int main(void) {
1010
printf("=== archspec C API Example ===\n\n");
11-
11+
1212
/* Host CPU information */
1313
const char* host_name = archspec_host_name();
1414
const char* host_vendor = archspec_host_vendor();
15-
15+
1616
printf("Host CPU:\n");
1717
printf(" Name: %s\n", host_name ? host_name : "(unknown)");
1818
printf(" Vendor: %s\n", host_vendor ? host_vendor : "(unknown)");
19-
19+
2020
char* host_features = archspec_host_features();
2121
if (host_features) {
2222
printf(" Features: %s\n", host_features);
2323
archspec_free(host_features);
2424
}
25-
25+
2626
/* Get optimization flags */
2727
char* gcc_flags = archspec_host_flags("gcc");
2828
if (gcc_flags) {
2929
printf(" GCC flags: %s\n", gcc_flags);
3030
archspec_free(gcc_flags);
3131
}
32-
32+
3333
char* clang_flags = archspec_host_flags("clang");
3434
if (clang_flags) {
3535
printf(" Clang flags: %s\n", clang_flags);
3636
archspec_free(clang_flags);
3737
}
38-
38+
3939
/* Check for specific features */
4040
printf("\nFeature checks:\n");
4141
printf(" Has SSE4.2: %s\n", archspec_host_has_feature("sse4_2") ? "yes" : "no");
4242
printf(" Has AVX2: %s\n", archspec_host_has_feature("avx2") ? "yes" : "no");
4343
printf(" Has NEON: %s\n", archspec_host_has_feature("neon") ? "yes" : "no");
44-
44+
4545
/* Query a specific target */
4646
printf("\nHaswell features:\n");
4747
char* haswell_features = archspec_get_features("haswell");
@@ -51,32 +51,33 @@ int main(void) {
5151
} else {
5252
printf(" (not found)\n");
5353
}
54-
54+
5555
char* haswell_flags = archspec_get_flags("haswell", "gcc");
5656
if (haswell_flags) {
5757
printf(" GCC flags: %s\n", haswell_flags);
5858
archspec_free(haswell_flags);
5959
}
60-
60+
6161
/* List all targets */
6262
printf("\nKnown targets (%zu total):\n ", archspec_target_count());
6363
size_t count = archspec_target_count();
6464
for (size_t i = 0; i < count && i < 10; i++) {
6565
const char* name = archspec_target_name(i);
6666
if (name) {
6767
printf("%s", name);
68-
if (i < 9 && i < count - 1) printf(", ");
68+
if (i < 9 && i < count - 1)
69+
printf(", ");
6970
}
7071
}
71-
if (count > 10) printf(", ... (%zu more)", count - 10);
72+
if (count > 10)
73+
printf(", ... (%zu more)", count - 10);
7274
printf("\n");
73-
75+
7476
/* Check if a target exists */
7577
printf("\nTarget existence:\n");
7678
printf(" 'skylake' exists: %s\n", archspec_target_exists("skylake") ? "yes" : "no");
7779
printf(" 'zen4' exists: %s\n", archspec_target_exists("zen4") ? "yes" : "no");
7880
printf(" 'foobar' exists: %s\n", archspec_target_exists("foobar") ? "yes" : "no");
79-
81+
8082
return 0;
8183
}
82-

examples/compiler_flags.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,46 @@
1010
int main(int argc, char* argv[]) {
1111
std::cout << "=== archspec_cpp Compiler Flags ===" << std::endl;
1212
std::cout << std::endl;
13-
13+
1414
// Targets to show flags for
1515
std::vector<std::string> targets;
16-
16+
1717
if (argc > 1) {
1818
// Use command line arguments
1919
for (int i = 1; i < argc; ++i) {
2020
targets.push_back(argv[i]);
2121
}
2222
} else {
2323
// Default targets
24-
targets = {
25-
"x86_64",
26-
"x86_64_v2",
27-
"x86_64_v3",
28-
"x86_64_v4",
29-
"haswell",
30-
"skylake",
31-
"skylake_avx512",
32-
"zen2",
33-
"zen3",
34-
"zen4",
35-
"aarch64",
36-
"neoverse_n1",
37-
"neoverse_v2",
38-
"m1",
39-
"m2"
40-
};
24+
targets = {"x86_64", "x86_64_v2", "x86_64_v3", "x86_64_v4", "haswell",
25+
"skylake", "skylake_avx512", "zen2", "zen3", "zen4",
26+
"aarch64", "neoverse_n1", "neoverse_v2", "m1", "m2"};
4127
}
42-
28+
4329
// Compilers and versions to test
4430
std::vector<std::pair<std::string, std::string>> compilers = {
4531
{"gcc", "10.0"},
4632
{"gcc", "12.0"},
4733
{"clang", "12.0"},
4834
{"clang", "15.0"},
4935
};
50-
36+
5137
for (const auto& target_name : targets) {
5238
const auto* target = archspec::get_target(target_name);
53-
39+
5440
if (!target) {
5541
std::cout << target_name << ": NOT FOUND" << std::endl;
5642
continue;
5743
}
58-
44+
5945
std::cout << "=== " << target_name << " ===" << std::endl;
6046
std::cout << " Vendor: " << target->vendor() << std::endl;
6147
std::cout << " Family: " << target->family() << std::endl;
6248
std::cout << std::endl;
63-
49+
6450
for (const auto& [compiler, version] : compilers) {
6551
std::string flags = target->optimization_flags(compiler, version);
66-
52+
6753
std::cout << " " << std::setw(12) << std::left << (compiler + " " + version + ":");
6854
if (flags.empty()) {
6955
std::cout << "(not supported)";
@@ -74,9 +60,8 @@ int main(int argc, char* argv[]) {
7460
}
7561
std::cout << std::endl;
7662
}
77-
63+
7864
std::cout << "Usage: " << argv[0] << " [target1] [target2] ..." << std::endl;
79-
65+
8066
return 0;
8167
}
82-

0 commit comments

Comments
 (0)