|
1 | | -CC := gcc |
2 | | -CFLAGS := -Wall -Wextra -g |
3 | | -INCLUDE := -I../../include |
4 | | -LDFLAGS := -L../../target/debug/build/libbitcoinpqc-*/out/lib |
5 | | -LDLIBS := -lbitcoinpqc |
| 1 | +# libbitcoinpqc - Post-Quantum Cryptography for Bitcoin |
| 2 | +# Main Makefile for building, testing, and installing all components |
6 | 3 |
|
7 | | -.PHONY: all clean |
| 4 | +# User-configurable variables |
| 5 | +PREFIX ?= /usr/local |
| 6 | +DEBUG ?= 0 |
| 7 | +VERBOSE ?= 0 |
| 8 | +INSTALL_DEPS ?= 0 |
| 9 | +BUILD_DOCS ?= 0 |
| 10 | +NO_COLOR ?= 0 |
| 11 | +BUILD_BINDINGS ?= 0 |
8 | 12 |
|
9 | | -all: ml_dsa_test |
| 13 | +# Tool detection |
| 14 | +CMAKE := $(shell command -v cmake 2> /dev/null) |
| 15 | +CARGO := $(shell command -v cargo 2> /dev/null) |
| 16 | +PYTHON := $(shell command -v python3 2> /dev/null) |
| 17 | +NPM := $(shell command -v npm 2> /dev/null) |
10 | 18 |
|
11 | | -ml_dsa_test: ml_dsa_test.c |
12 | | - $(CC) $(CFLAGS) $(INCLUDE) -o $@ $< $(LDFLAGS) $(LDLIBS) |
| 19 | +# Build directories |
| 20 | +BUILD_DIR := build |
| 21 | +RELEASE_DIR := target/release |
| 22 | +DEBUG_DIR := target/debug |
13 | 23 |
|
14 | | -clean: |
15 | | - rm -f ml_dsa_test |
| 24 | +# Colors for terminal output |
| 25 | +ifeq ($(NO_COLOR), 0) |
| 26 | + GREEN := \033[0;32m |
| 27 | + YELLOW := \033[0;33m |
| 28 | + RED := \033[0;31m |
| 29 | + BLUE := \033[0;34m |
| 30 | + NC := \033[0m # No Color |
| 31 | +else |
| 32 | + GREEN := |
| 33 | + YELLOW := |
| 34 | + RED := |
| 35 | + BLUE := |
| 36 | + NC := |
| 37 | +endif |
| 38 | + |
| 39 | +# Default target |
| 40 | +.PHONY: all |
| 41 | +all: info c-lib rust-lib bindings |
| 42 | + |
| 43 | +.PHONY: everything |
| 44 | +everything: all examples tests docs |
| 45 | + |
| 46 | +# Bindings target |
| 47 | +.PHONY: bindings |
| 48 | +bindings: python nodejs |
| 49 | + |
| 50 | +# Print build information |
| 51 | +.PHONY: info |
| 52 | +info: |
| 53 | + @echo -e "${BLUE}Building libbitcoinpqc - Post-Quantum Cryptography for Bitcoin${NC}" |
| 54 | + @echo -e "${BLUE}------------------------------------------------------------${NC}" |
| 55 | + @if [ -n "$(CMAKE)" ]; then echo -e " [${GREEN}✓${NC}] CMake: $(CMAKE)"; else echo -e " [${RED}✗${NC}] CMake (required for C library)"; fi |
| 56 | + @if [ -n "$(CARGO)" ]; then echo -e " [${GREEN}✓${NC}] Cargo: $(CARGO)"; else echo -e " [${RED}✗${NC}] Cargo (required for Rust library)"; fi |
| 57 | + @if [ -n "$(PYTHON)" ]; then echo -e " [${GREEN}✓${NC}] Python: $(PYTHON)"; else echo -e " [${YELLOW}!${NC}] Python (optional for Python bindings)"; fi |
| 58 | + @if [ -n "$(NPM)" ]; then echo -e " [${GREEN}✓${NC}] NPM: $(NPM)"; else echo -e " [${YELLOW}!${NC}] NPM (optional for NodeJS bindings)"; fi |
| 59 | + @echo -e "${BLUE}------------------------------------------------------------${NC}" |
| 60 | + @echo -e "${YELLOW}This will build the core libraries (C and Rust) and language bindings.${NC}" |
| 61 | + @echo -e "${YELLOW}Available make targets:${NC}" |
| 62 | + @echo -e " ${GREEN}make c-lib${NC} - Build only the C library" |
| 63 | + @echo -e " ${GREEN}make rust-lib${NC} - Build only the Rust library" |
| 64 | + @echo -e " ${GREEN}make bindings${NC} - Build only Python and NodeJS bindings" |
| 65 | + @echo -e " ${GREEN}make examples${NC} - Build example programs" |
| 66 | + @echo -e " ${GREEN}make everything${NC} - Build all components (libraries, bindings, examples, tests)" |
| 67 | + @echo -e " ${GREEN}make help${NC} - Show all available targets" |
| 68 | + @echo -e "${BLUE}------------------------------------------------------------${NC}" |
| 69 | + |
| 70 | +# C library targets |
| 71 | +.PHONY: c-lib |
| 72 | +c-lib: cmake-configure cmake-build |
| 73 | + |
| 74 | +.PHONY: cmake-configure |
| 75 | +cmake-configure: |
| 76 | + @echo -e "${BLUE}Configuring C library with CMake...${NC}" |
| 77 | + @mkdir -p $(BUILD_DIR) |
| 78 | + @cd $(BUILD_DIR) && cmake .. -DCMAKE_BUILD_TYPE=$(if $(filter 1,$(DEBUG)),Debug,Release) -DBUILD_EXAMPLES=ON -DCMAKE_INSTALL_PREFIX=$(PREFIX) |
| 79 | + |
| 80 | +.PHONY: cmake-build |
| 81 | +cmake-build: |
| 82 | + @echo -e "${BLUE}Building C library...${NC}" |
| 83 | + @cmake --build $(BUILD_DIR) $(if $(filter 1,$(VERBOSE)),--verbose,) |
| 84 | + |
| 85 | +# Rust library targets |
| 86 | +.PHONY: rust-lib |
| 87 | +rust-lib: |
| 88 | + @echo -e "${BLUE}Building Rust library...${NC}" |
| 89 | + @$(CARGO) build $(if $(filter 0,$(DEBUG)),--release,) |
| 90 | + |
| 91 | +# Example targets |
| 92 | +.PHONY: examples |
| 93 | +examples: c-examples rust-examples |
| 94 | + |
| 95 | +.PHONY: c-examples |
| 96 | +c-examples: c-lib |
| 97 | + @echo -e "${BLUE}Building C examples...${NC}" |
| 98 | + @cmake --build $(BUILD_DIR) --target examples |
| 99 | + |
| 100 | +.PHONY: rust-examples |
| 101 | +rust-examples: |
| 102 | + @echo -e "${BLUE}Building and running Rust examples...${NC}" |
| 103 | + @$(CARGO) run --example basic $(if $(filter 0,$(DEBUG)),--release,) |
| 104 | + |
| 105 | +# Testing targets |
| 106 | +.PHONY: tests |
| 107 | +tests: test-c test-rust |
| 108 | + |
| 109 | +.PHONY: test-c |
| 110 | +test-c: c-lib |
| 111 | + @echo -e "${BLUE}Running C tests...${NC}" |
| 112 | + @cd $(BUILD_DIR) && ctest $(if $(filter 1,$(VERBOSE)),-V,) |
| 113 | + |
| 114 | +.PHONY: test-rust |
| 115 | +test-rust: |
| 116 | + @echo -e "${BLUE}Running Rust tests...${NC}" |
| 117 | + @$(CARGO) test $(if $(filter 0,$(DEBUG)),--release,) |
| 118 | + |
| 119 | +# Benchmark targets |
| 120 | +.PHONY: bench |
| 121 | +bench: |
| 122 | + @echo -e "${BLUE}Running benchmarks...${NC}" |
| 123 | + @$(CARGO) bench |
| 124 | + |
| 125 | +# Documentation targets |
| 126 | +.PHONY: docs |
| 127 | +docs: |
| 128 | +ifeq ($(BUILD_DOCS), 1) |
| 129 | + @echo -e "${BLUE}Building documentation...${NC}" |
| 130 | + @$(CARGO) doc --no-deps |
| 131 | + @echo -e "${GREEN}Documentation built in target/doc/bitcoinpqc/index.html${NC}" |
| 132 | +else |
| 133 | + @echo -e "${YELLOW}Skipping documentation build (use BUILD_DOCS=1 to enable)${NC}" |
| 134 | +endif |
| 135 | + |
| 136 | +# Language bindings |
| 137 | +.PHONY: python |
| 138 | +python: rust-lib |
| 139 | + @echo -e "${BLUE}Building Python bindings...${NC}" |
| 140 | + @if [ -n "$(PYTHON)" ]; then \ |
| 141 | + echo -e "${YELLOW}Creating a Python virtual environment...${NC}"; \ |
| 142 | + $(PYTHON) -m venv python/.venv || { echo -e "${RED}Failed to create virtual environment${NC}"; exit 1; }; \ |
| 143 | + echo -e "${GREEN}Virtual environment created at python/.venv${NC}"; \ |
| 144 | + echo -e "${YELLOW}Installing Python bindings in virtual environment...${NC}"; \ |
| 145 | + . python/.venv/bin/activate && cd python && $(PYTHON) -m pip install -e . && \ |
| 146 | + echo -e "${GREEN}Python bindings installed successfully in virtual environment${NC}"; \ |
| 147 | + echo -e "${YELLOW}To use the bindings, activate the virtual environment:${NC}"; \ |
| 148 | + echo -e " source python/.venv/bin/activate"; \ |
| 149 | + else \ |
| 150 | + echo -e "${RED}Python not found, skipping Python bindings${NC}"; \ |
| 151 | + fi |
| 152 | + |
| 153 | +.PHONY: nodejs |
| 154 | +nodejs: rust-lib |
| 155 | + @echo -e "${BLUE}Building NodeJS bindings...${NC}" |
| 156 | + @if [ -n "$(NPM)" ]; then \ |
| 157 | + cd nodejs && $(NPM) install && $(NPM) run build; \ |
| 158 | + else \ |
| 159 | + echo -e "${RED}NPM not found, skipping NodeJS bindings${NC}"; \ |
| 160 | + fi |
| 161 | + |
| 162 | +# Installation targets |
| 163 | +.PHONY: install |
| 164 | +install: install-c install-rust |
| 165 | + |
| 166 | +.PHONY: install-c |
| 167 | +install-c: c-lib |
| 168 | + @echo -e "${BLUE}Installing C library to $(PREFIX)...${NC}" |
| 169 | + @cd $(BUILD_DIR) && cmake --install . |
| 170 | + |
| 171 | +.PHONY: install-rust |
| 172 | +install-rust: rust-lib |
| 173 | + @echo -e "${BLUE}Installing Rust library...${NC}" |
| 174 | + @$(CARGO) install --path . |
| 175 | + |
| 176 | +# Clean targets |
| 177 | +.PHONY: clean |
| 178 | +clean: clean-c clean-rust clean-bindings |
| 179 | + |
| 180 | +.PHONY: clean-c |
| 181 | +clean-c: |
| 182 | + @echo -e "${BLUE}Cleaning C library build files...${NC}" |
| 183 | + @rm -rf $(BUILD_DIR) |
| 184 | + |
| 185 | +.PHONY: clean-rust |
| 186 | +clean-rust: |
| 187 | + @echo -e "${BLUE}Cleaning Rust library build files...${NC}" |
| 188 | + @$(CARGO) clean |
| 189 | + |
| 190 | +.PHONY: clean-bindings |
| 191 | +clean-bindings: |
| 192 | + @echo -e "${BLUE}Cleaning language bindings...${NC}" |
| 193 | + @if [ -d "python/build" ]; then rm -rf python/build; fi |
| 194 | + @if [ -d "nodejs/dist" ]; then rm -rf nodejs/dist; fi |
| 195 | + |
| 196 | +# Help target |
| 197 | +.PHONY: help |
| 198 | +help: |
| 199 | + @echo -e "${BLUE}libbitcoinpqc Makefile Help${NC}" |
| 200 | + @echo -e "${BLUE}-------------------------${NC}" |
| 201 | + @echo -e "Main targets:" |
| 202 | + @echo -e " ${GREEN}all${NC} - Build C and Rust libraries and language bindings (default)" |
| 203 | + @echo -e " ${GREEN}bindings${NC} - Build Python and NodeJS bindings" |
| 204 | + @echo -e " ${GREEN}everything${NC} - Build all components including examples and tests" |
| 205 | + @echo -e " ${GREEN}c-lib${NC} - Build only the C library" |
| 206 | + @echo -e " ${GREEN}rust-lib${NC} - Build only the Rust library" |
| 207 | + @echo -e " ${GREEN}python${NC} - Build Python bindings" |
| 208 | + @echo -e " ${GREEN}nodejs${NC} - Build NodeJS bindings" |
| 209 | + @echo -e " ${GREEN}examples${NC} - Build example programs" |
| 210 | + @echo -e " ${GREEN}tests${NC} - Run all tests" |
| 211 | + @echo -e " ${GREEN}bench${NC} - Run benchmarks" |
| 212 | + @echo -e " ${GREEN}docs${NC} - Build documentation" |
| 213 | + @echo -e " ${GREEN}install${NC} - Install libraries" |
| 214 | + @echo -e " ${GREEN}clean${NC} - Clean all build files" |
| 215 | + @echo -e " ${GREEN}help${NC} - Display this help message" |
| 216 | + @echo -e " ${GREEN}fix-warnings${NC} - Fix common build warnings" |
| 217 | + @echo -e " ${GREEN}troubleshoot${NC} - Display troubleshooting information" |
| 218 | + @echo -e "" |
| 219 | + @echo -e "Developer targets:" |
| 220 | + @echo -e " ${GREEN}dev${NC} - Run format, lint, and analyze" |
| 221 | + @echo -e " ${GREEN}format${NC} - Format C and Rust code" |
| 222 | + @echo -e " ${GREEN}lint${NC} - Lint C and Rust code" |
| 223 | + @echo -e " ${GREEN}analyze${NC} - Run static analysis on C code" |
| 224 | + @echo -e " ${GREEN}dev-deps${NC} - Install development dependencies" |
| 225 | + @echo -e "" |
| 226 | + @echo -e "Configuration options:" |
| 227 | + @echo -e " ${YELLOW}DEBUG=1${NC} - Build in debug mode (default: 0)" |
| 228 | + @echo -e " ${YELLOW}VERBOSE=1${NC} - Show verbose build output (default: 0)" |
| 229 | + @echo -e " ${YELLOW}PREFIX=/path${NC} - Installation prefix (default: /usr/local)" |
| 230 | + @echo -e " ${YELLOW}BUILD_DOCS=1${NC} - Build documentation (default: 0)" |
| 231 | + @echo -e " ${YELLOW}NO_COLOR=1${NC} - Disable colored output (default: 0)" |
| 232 | + |
| 233 | +# Fix warnings targets |
| 234 | +.PHONY: fix-warnings |
| 235 | +fix-warnings: |
| 236 | + @echo -e "${BLUE}Fixing CRYPTO_ALGNAME redefinition warnings...${NC}" |
| 237 | + @echo -e "${YELLOW}This functionality has been removed.${NC}" |
| 238 | + @echo -e "${GREEN}Please edit CMakeLists.txt manually if needed.${NC}" |
| 239 | + |
| 240 | +# Troubleshooting information |
| 241 | +.PHONY: troubleshoot |
| 242 | +troubleshoot: |
| 243 | + @echo -e "${BLUE}libbitcoinpqc Troubleshooting${NC}" |
| 244 | + @echo -e "${BLUE}---------------------------${NC}" |
| 245 | + @echo -e "Common issues and solutions:" |
| 246 | + @echo -e "" |
| 247 | + @echo -e " ${YELLOW}CRYPTO_ALGNAME redefinition warnings:${NC}" |
| 248 | + @echo -e " These are harmless but can be fixed by editing CMakeLists.txt manually" |
| 249 | + @echo -e " Change CRYPTO_ALGNAME to CRYPTO_ALGNAME_SPHINCS in the target_compile_definitions" |
| 250 | + @echo -e "" |
| 251 | + @echo -e " ${YELLOW}Example compilation errors:${NC}" |
| 252 | + @echo -e " If examples don't compile, ensure you're using the latest code." |
| 253 | + @echo -e " Run: make clean && make" |
| 254 | + @echo -e "" |
| 255 | + @echo -e " ${YELLOW}Missing tools:${NC}" |
| 256 | + @echo -e " Make sure you have all required development tools installed." |
| 257 | + @echo -e "" |
| 258 | + @echo -e " ${YELLOW}Python or NodeJS bindings issues:${NC}" |
| 259 | + @echo -e " If you experience Python or NodeJS binding problems:" |
| 260 | + @echo -e " - For Python: Check the virtual environment at python/.venv" |
| 261 | + @echo -e " - For NodeJS: Check the build logs in nodejs/build" |
| 262 | + @echo -e " You can build bindings separately with 'make python' or 'make nodejs'" |
| 263 | + @echo -e "" |
| 264 | + @echo -e " ${YELLOW}Terminal color issues:${NC}" |
| 265 | + @echo -e " If you see raw escape sequences (like \\033[0;32m), run with NO_COLOR=1" |
| 266 | + @echo -e "" |
| 267 | + @echo -e " ${YELLOW}For more detailed help:${NC}" |
| 268 | + @echo -e " Check the README.md or open an issue on GitHub." |
| 269 | + |
| 270 | +# Developer tools |
| 271 | +.PHONY: dev |
| 272 | +dev: format lint analyze |
| 273 | + |
| 274 | +.PHONY: format |
| 275 | +format: |
| 276 | + @echo -e "${BLUE}Formatting code...${NC}" |
| 277 | + @if command -v clang-format > /dev/null; then \ |
| 278 | + find src include examples -name "*.c" -o -name "*.h" | xargs clang-format -i -style=file; \ |
| 279 | + echo -e "${GREEN}C/C++ code formatted${NC}"; \ |
| 280 | + else \ |
| 281 | + echo -e "${YELLOW}clang-format not found, skipping C/C++ formatting${NC}"; \ |
| 282 | + fi |
| 283 | + @if [ -n "$(CARGO)" ]; then \ |
| 284 | + $(CARGO) fmt; \ |
| 285 | + echo -e "${GREEN}Rust code formatted${NC}"; \ |
| 286 | + else \ |
| 287 | + echo -e "${YELLOW}Cargo not found, skipping Rust formatting${NC}"; \ |
| 288 | + fi |
| 289 | + |
| 290 | +.PHONY: lint |
| 291 | +lint: |
| 292 | + @echo -e "${BLUE}Linting code...${NC}" |
| 293 | + @if command -v cppcheck > /dev/null; then \ |
| 294 | + cppcheck --enable=all --suppressions-list=.cppcheck-suppressions --error-exitcode=0 src include examples; \ |
| 295 | + echo -e "${GREEN}C/C++ code linted${NC}"; \ |
| 296 | + else \ |
| 297 | + echo -e "${YELLOW}cppcheck not found, skipping C/C++ linting${NC}"; \ |
| 298 | + fi |
| 299 | + @if [ -n "$(CARGO)" ]; then \ |
| 300 | + $(CARGO) clippy; \ |
| 301 | + echo -e "${GREEN}Rust code linted${NC}"; \ |
| 302 | + else \ |
| 303 | + echo -e "${YELLOW}Cargo not found, skipping Rust linting${NC}"; \ |
| 304 | + fi |
| 305 | + |
| 306 | +.PHONY: analyze |
| 307 | +analyze: |
| 308 | + @echo -e "${BLUE}Analyzing code...${NC}" |
| 309 | + @if command -v scan-build > /dev/null; then \ |
| 310 | + scan-build -o analysis-reports cmake --build $(BUILD_DIR); \ |
| 311 | + echo -e "${GREEN}Static analysis completed. See analysis-reports directory for results.${NC}"; \ |
| 312 | + else \ |
| 313 | + echo -e "${YELLOW}scan-build not found, skipping static analysis${NC}"; \ |
| 314 | + fi |
| 315 | + |
| 316 | +.PHONY: dev-deps |
| 317 | +dev-deps: |
| 318 | + @echo -e "${BLUE}Installing development dependencies...${NC}" |
| 319 | + @if command -v apt-get > /dev/null; then \ |
| 320 | + sudo apt-get update && sudo apt-get install -y clang-format cppcheck clang-tools llvm; \ |
| 321 | + elif command -v dnf > /dev/null; then \ |
| 322 | + sudo dnf install -y clang-tools-extra cppcheck; \ |
| 323 | + elif command -v pacman > /dev/null; then \ |
| 324 | + sudo pacman -S --needed clang cppcheck; \ |
| 325 | + elif command -v brew > /dev/null; then \ |
| 326 | + brew install llvm cppcheck; \ |
| 327 | + else \ |
| 328 | + echo -e "${YELLOW}Could not detect package manager, please install manually:${NC}"; \ |
| 329 | + echo "- clang-format (for code formatting)"; \ |
| 330 | + echo "- cppcheck (for static analysis)"; \ |
| 331 | + echo "- clang tools (for static analysis)"; \ |
| 332 | + fi |
| 333 | + @if [ -n "$(CARGO)" ]; then \ |
| 334 | + rustup component add clippy rustfmt; \ |
| 335 | + echo -e "${GREEN}Rust development tools installed${NC}"; \ |
| 336 | + fi |
0 commit comments