Skip to content

Commit f8b09f7

Browse files
committed
First commit
0 parents  commit f8b09f7

25 files changed

Lines changed: 7714 additions & 0 deletions

.github/workflows/ci.yml

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
pull_request:
7+
branches: [ main, master ]
8+
9+
jobs:
10+
build-linux:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
compiler: [gcc, clang]
16+
include:
17+
- compiler: gcc
18+
cc: gcc
19+
cxx: g++
20+
- compiler: clang
21+
cc: clang
22+
cxx: clang++
23+
24+
steps:
25+
- uses: actions/checkout@v4
26+
with:
27+
submodules: recursive
28+
29+
- name: Build
30+
env:
31+
CC: ${{ matrix.cc }}
32+
CXX: ${{ matrix.cxx }}
33+
run: make
34+
35+
- name: Run tests
36+
run: make check
37+
38+
- name: Run examples
39+
run: make run-examples
40+
41+
build-macos:
42+
runs-on: macos-latest
43+
strategy:
44+
fail-fast: false
45+
matrix:
46+
compiler: [clang, gcc]
47+
include:
48+
- compiler: clang
49+
cc: clang
50+
cxx: clang++
51+
- compiler: gcc
52+
cc: gcc-13
53+
cxx: g++-13
54+
55+
steps:
56+
- uses: actions/checkout@v4
57+
with:
58+
submodules: recursive
59+
60+
- name: Build
61+
env:
62+
CC: ${{ matrix.cc }}
63+
CXX: ${{ matrix.cxx }}
64+
run: make
65+
66+
- name: Run tests
67+
run: make check
68+
69+
- name: Run examples
70+
run: make run-examples
71+
72+
build-windows:
73+
runs-on: windows-latest
74+
steps:
75+
- uses: actions/checkout@v4
76+
with:
77+
submodules: recursive
78+
79+
- name: Build (MinGW)
80+
shell: bash
81+
run: make CXX=g++ CC=gcc
82+
83+
- name: Run tests
84+
shell: bash
85+
run: make check
86+
87+
- name: Run examples
88+
shell: bash
89+
run: make run-examples
90+
91+
build-freebsd:
92+
runs-on: ubuntu-latest
93+
steps:
94+
- uses: actions/checkout@v4
95+
with:
96+
submodules: recursive
97+
98+
- name: Build and test on FreeBSD
99+
uses: vmactions/freebsd-vm@v1
100+
with:
101+
usesh: true
102+
prepare: |
103+
pkg install -y gmake
104+
run: |
105+
gmake CXX=clang++ CC=clang
106+
gmake check
107+
gmake run-examples
108+
109+
# Verify the library compiles with different C++ standards
110+
standards:
111+
runs-on: ubuntu-latest
112+
strategy:
113+
matrix:
114+
std: [17, 20, 23]
115+
steps:
116+
- uses: actions/checkout@v4
117+
with:
118+
submodules: recursive
119+
120+
- name: Build with C++${{ matrix.std }}
121+
run: make CXXFLAGS="-std=c++${{ matrix.std }} -Wall -Wextra -O2"
122+
123+
- name: Run tests
124+
run: make check
125+
126+
- name: Run examples
127+
run: make run-examples
128+

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Build output
2+
build/
3+
4+
# Editor files
5+
*.swp
6+
*.swo
7+
*~
8+
.vscode/
9+
.idea/
10+
11+
# OS files
12+
.DS_Store
13+
Thumbs.db
14+

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "extern/json"]
2+
path = extern/json
3+
url = https://github.com/nlohmann/json.git
4+
[submodule "extern/archspec"]
5+
path = extern/archspec
6+
url = https://github.com/archspec/archspec.git

LICENSE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Julia Contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
23+
---
24+
25+
This project uses data from the archspec project:
26+
https://github.com/archspec/archspec
27+
Copyright 2019-2024 Lawrence Livermore National Security, LLC and other
28+
Archspec Project Developers. (Apache-2.0 OR MIT)

Makefile

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# Copyright 2024 archspec_cpp contributors
2+
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
3+
#
4+
# Cross-platform Makefile for archspec_cpp
5+
# Supports: Linux, macOS, FreeBSD, Windows (with MinGW or MSVC via nmake)
6+
7+
# Detect OS
8+
ifeq ($(OS),Windows_NT)
9+
UNAME := Windows
10+
else
11+
UNAME := $(shell uname -s)
12+
endif
13+
14+
# Compiler settings
15+
CXX ?= g++
16+
AR ?= ar
17+
18+
# Base flags
19+
CXXFLAGS ?= -std=c++17 -Wall -Wextra -O2
20+
INCLUDES = -I./include -I./extern/json/single_include
21+
22+
# Platform-specific settings
23+
ifeq ($(UNAME),Darwin)
24+
# macOS
25+
LDFLAGS ?=
26+
SHARED_EXT = .dylib
27+
SHARED_FLAGS = -dynamiclib
28+
STATIC_EXT = .a
29+
else ifeq ($(UNAME),Linux)
30+
# Linux
31+
LDFLAGS ?= -ldl
32+
SHARED_EXT = .so
33+
SHARED_FLAGS = -shared -fPIC
34+
STATIC_EXT = .a
35+
else ifeq ($(UNAME),FreeBSD)
36+
# FreeBSD
37+
LDFLAGS ?=
38+
SHARED_EXT = .so
39+
SHARED_FLAGS = -shared -fPIC
40+
STATIC_EXT = .a
41+
else ifeq ($(UNAME),Windows)
42+
# Windows (MinGW)
43+
LDFLAGS ?=
44+
SHARED_EXT = .dll
45+
SHARED_FLAGS = -shared
46+
STATIC_EXT = .a
47+
EXE_EXT = .exe
48+
endif
49+
50+
# Directories
51+
SRCDIR = src
52+
INCDIR = include
53+
TESTDIR = tests
54+
EXAMPLEDIR = examples
55+
BUILDDIR = build
56+
OBJDIR = $(BUILDDIR)/obj
57+
LIBDIR = $(BUILDDIR)/lib
58+
BINDIR = $(BUILDDIR)/bin
59+
60+
# Source files
61+
SOURCES = $(SRCDIR)/cpuid.cpp $(SRCDIR)/microarchitecture.cpp $(SRCDIR)/detect.cpp $(SRCDIR)/archspec_c.cpp
62+
OBJECTS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SOURCES))
63+
64+
# Library names
65+
LIB_NAME = archspec
66+
STATIC_LIB = $(LIBDIR)/lib$(LIB_NAME)$(STATIC_EXT)
67+
68+
# Test sources
69+
TEST_SOURCES = $(wildcard $(TESTDIR)/*.cpp)
70+
TEST_BINARIES = $(patsubst $(TESTDIR)/%.cpp,$(BINDIR)/%$(EXE_EXT),$(TEST_SOURCES))
71+
72+
# Example sources
73+
EXAMPLE_SOURCES = $(wildcard $(EXAMPLEDIR)/*.cpp)
74+
EXAMPLE_BINARIES = $(patsubst $(EXAMPLEDIR)/%.cpp,$(BINDIR)/%$(EXE_EXT),$(EXAMPLE_SOURCES))
75+
76+
# Default target
77+
all: directories $(STATIC_LIB) examples tests
78+
79+
# Create directories
80+
directories:
81+
@mkdir -p $(OBJDIR) $(LIBDIR) $(BINDIR)
82+
83+
# Compile source files
84+
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
85+
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
86+
87+
# Build static library
88+
$(STATIC_LIB): $(OBJECTS)
89+
$(AR) rcs $@ $^
90+
91+
# Build examples
92+
examples: $(EXAMPLE_BINARIES) $(BINDIR)/c_api_example$(EXE_EXT)
93+
94+
$(BINDIR)/%$(EXE_EXT): $(EXAMPLEDIR)/%.cpp $(STATIC_LIB)
95+
$(CXX) $(CXXFLAGS) $(INCLUDES) $< -L$(LIBDIR) -l$(LIB_NAME) $(LDFLAGS) -o $@
96+
97+
# C example needs special handling (link with C++ runtime)
98+
$(BINDIR)/c_api_example$(EXE_EXT): $(EXAMPLEDIR)/c_api_example.c $(STATIC_LIB)
99+
$(CXX) $(CXXFLAGS) $(INCLUDES) $< -L$(LIBDIR) -l$(LIB_NAME) $(LDFLAGS) -o $@
100+
101+
# Build tests
102+
tests: $(TEST_BINARIES)
103+
104+
$(BINDIR)/%$(EXE_EXT): $(TESTDIR)/%.cpp $(STATIC_LIB)
105+
$(CXX) $(CXXFLAGS) $(INCLUDES) $< -L$(LIBDIR) -l$(LIB_NAME) $(LDFLAGS) -o $@
106+
107+
# Run tests
108+
check: tests
109+
@echo "Running tests..."
110+
@for test in $(TEST_BINARIES); do \
111+
echo "Running $$test..."; \
112+
$$test || exit 1; \
113+
done
114+
@echo "All tests passed!"
115+
116+
# Run examples
117+
run-examples: examples
118+
@echo "Running examples..."
119+
@for example in $(EXAMPLE_BINARIES); do \
120+
echo "=== Running $$example ==="; \
121+
$$example; \
122+
echo ""; \
123+
done
124+
125+
# Clean
126+
clean:
127+
rm -rf $(BUILDDIR)
128+
129+
# Install (optional, for system-wide installation)
130+
PREFIX ?= /usr/local
131+
install: $(STATIC_LIB)
132+
mkdir -p $(PREFIX)/lib $(PREFIX)/include/archspec
133+
cp $(STATIC_LIB) $(PREFIX)/lib/
134+
cp -r $(INCDIR)/archspec/* $(PREFIX)/include/archspec/
135+
136+
# Uninstall
137+
uninstall:
138+
rm -f $(PREFIX)/lib/lib$(LIB_NAME)$(STATIC_EXT)
139+
rm -rf $(PREFIX)/include/archspec
140+
141+
# Debug build
142+
debug: CXXFLAGS += -g -DDEBUG
143+
debug: all
144+
145+
# Generate compile_commands.json for IDE support
146+
compile_commands: clean
147+
@which bear > /dev/null || (echo "bear not installed, skipping" && exit 0)
148+
bear -- $(MAKE) all
149+
150+
# Path to archspec JSON data (from submodule)
151+
ARCHSPEC_JSON_DIR = extern/archspec/archspec/json/cpu
152+
153+
# Regenerate embedded JSON data (convenience target)
154+
regenerate-data:
155+
@echo "Regenerating embedded JSON data from archspec submodule..."
156+
@echo '// Auto-generated embedded JSON data - DO NOT EDIT' > $(SRCDIR)/microarchitectures_data.inc
157+
@echo '// Generated from $(ARCHSPEC_JSON_DIR)/microarchitectures.json' >> $(SRCDIR)/microarchitectures_data.inc
158+
@echo '//' >> $(SRCDIR)/microarchitectures_data.inc
159+
@echo '// To regenerate: make regenerate-data' >> $(SRCDIR)/microarchitectures_data.inc
160+
@echo '' >> $(SRCDIR)/microarchitectures_data.inc
161+
@echo '#ifndef ARCHSPEC_MICROARCHITECTURES_DATA_INC' >> $(SRCDIR)/microarchitectures_data.inc
162+
@echo '#define ARCHSPEC_MICROARCHITECTURES_DATA_INC' >> $(SRCDIR)/microarchitectures_data.inc
163+
@echo '' >> $(SRCDIR)/microarchitectures_data.inc
164+
@echo 'static const char* MICROARCHITECTURES_JSON = R"JSON_DATA(' >> $(SRCDIR)/microarchitectures_data.inc
165+
@cat $(ARCHSPEC_JSON_DIR)/microarchitectures.json >> $(SRCDIR)/microarchitectures_data.inc
166+
@echo ')JSON_DATA";' >> $(SRCDIR)/microarchitectures_data.inc
167+
@echo '' >> $(SRCDIR)/microarchitectures_data.inc
168+
@echo '#endif // ARCHSPEC_MICROARCHITECTURES_DATA_INC' >> $(SRCDIR)/microarchitectures_data.inc
169+
@echo "Done!"
170+
171+
.PHONY: all directories examples tests check run-examples clean install uninstall debug compile_commands regenerate-data
172+

0 commit comments

Comments
 (0)