dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
# Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
#
# This file is part of Ri.
# The Ri project belongs to the Dunimd Team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

################################################################################
# Ri Build System - Unified Makefile
################################################################################
#
# PURPOSE:
# This Makefile provides a unified build system for Ri project, supporting:
# - Local development builds
# - CI/CD pipeline builds (GitHub Actions)
# - Cross-platform compilation
# - Multi-language bindings (Rust, Python, Java, C)
#
# USAGE:
#   make <target> [VARIABLE=value]
#
# EXAMPLES:
#   make build                    # Build Rust library (default)
#   make build-cli               # Build CLI tool
#   make build-python            # Build Python wheel
#   make build-c                 # Build C static library
#   make build-java              # Build Java JAR
#   make test                    # Run tests
#   make clean                   # Clean build artifacts
#   make help                    # Show all available targets
#
# PLATFORM DETECTION:
#   The Makefile automatically detects the current platform and architecture.
#   Override with: make build PLATFORM=linux ARCH=arm64
#
# FEATURES:
#   - Automatic platform detection (Linux/Windows/macOS)
#   - Automatic architecture detection (x64/ARM64)
#   - Parallel builds support
#   - Incremental builds with dependency tracking
#   - Cross-compilation support
#   - Docker-based manylinux builds for Python
#
# REQUIREMENTS:
#   - Rust toolchain (stable)
#   - Python 3.8+ (for Python bindings)
#   - JDK 11+ (for Java bindings)
#   - CMake (for C dependencies)
#   - protoc (Protocol Buffers compiler)
#
# ENVIRONMENT VARIABLES:
#   PLATFORM     - Target platform (linux/windows/macos)
#   ARCH         - Target architecture (x64/arm64)
#   PYTHON_VER   - Python version for wheels (default: 3.11)
#   FEATURES     - Cargo features to enable
#   RELEASE      - Build in release mode (default: true)
#   TARGET       - Rust target triple (e.g., x86_64-unknown-linux-gnu)
#
################################################################################

# Detect operating system
UNAME_S := $(shell uname -s 2>/dev/null || echo "Windows")
ifeq ($(UNAME_S),Linux)
    PLATFORM ?= linux
endif
ifeq ($(UNAME_S),Darwin)
    PLATFORM ?= macos
endif
ifneq ($(or $(findstring MINGW,$(UNAME_S)),$(findstring MSYS,$(UNAME_S)),$(findstring CYGWIN,$(UNAME_S))),)
    PLATFORM ?= windows
endif
ifeq ($(UNAME_S),Windows)
    PLATFORM ?= windows
endif

# Detect architecture
UNAME_M := $(shell uname -m 2>/dev/null || echo "unknown")
ifeq ($(UNAME_M),x86_64)
    ARCH ?= x64
endif
ifeq ($(UNAME_M),aarch64)
    ARCH ?= arm64
endif
ifeq ($(UNAME_M),arm64)
    ARCH ?= arm64
endif

# Default values
RELEASE ?= true
PYTHON_VER ?= 3.11
FEATURES ?=
VERBOSE ?= false

# Windows features (excludes kafka — rdkafka configure script can't run on Windows)
WINDOWS_FEATURES := grpc,websocket,rabbitmq,cache,queue,gateway,service_mesh,auth,observability,postgres,mysql,sqlite,http_client,system_info,config_hot_reload,etcd
# Same features with ri/ prefix for passing to ric crate (which depends on ri)
RI_WINDOWS_FEATURES := ri/grpc,ri/websocket,ri/rabbitmq,ri/cache,ri/queue,ri/gateway,ri/service_mesh,ri/auth,ri/observability,ri/postgres,ri/mysql,ri/sqlite,ri/http_client,ri/system_info,ri/config_hot_reload,ri/etcd

# Build mode
ifeq ($(RELEASE),true)
    BUILD_MODE := --release
    BUILD_DIR := release
else
    BUILD_MODE :=
    BUILD_DIR := debug
endif

# Platform-specific configuration
ifeq ($(PLATFORM),linux)
    LIB_EXT := so
    LIB_PREFIX := lib
    STATIC_EXT := a
    TARGET ?= x86_64-unknown-linux-gnu
    ifeq ($(ARCH),arm64)
        TARGET := aarch64-unknown-linux-gnu
    endif
endif

ifeq ($(PLATFORM),windows)
    LIB_EXT := dll
    LIB_PREFIX := 
    STATIC_EXT := lib
    TARGET ?= x86_64-pc-windows-msvc
    ifeq ($(ARCH),arm64)
        TARGET := aarch64-pc-windows-msvc
endif
endif
export CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER := rust-lld
export CARGO_TARGET_AARCH64_PC_WINDOWS_MSVC_LINKER := rust-lld

ifeq ($(PLATFORM),macos)
    LIB_EXT := dylib
    LIB_PREFIX := lib
    STATIC_EXT := a
    TARGET ?= x86_64-apple-darwin
    ifeq ($(ARCH),arm64)
        TARGET := aarch64-apple-darwin
    endif
endif

# Output directories
DIST_DIR := dist
INCLUDE_DIR := include
TARGET_DIR := target/$(TARGET)/$(BUILD_DIR)

# Colors for output (if terminal supports it)
ifneq ($(TERM),)
    GREEN := \033[0;32m
    YELLOW := \033[0;33m
    RED := \033[0;31m
    NC := \033[0m
else
    GREEN := 
    YELLOW := 
    RED := 
    NC := 
endif

################################################################################
# Phony Targets
################################################################################
.PHONY: all build clean test help setup-env \
        build-rust build-cli build-python build-c build-java \
        build-all-archs build-all-platforms \
        install install-python install-java \
        doc doc-rust doc-python \
        lint fmt check \
        docker-clean docker-build \
        setup-protoc setup-cmake setup-rust setup-openssl setup-deps

# Default target
all: build

################################################################################
# Environment Setup Targets
################################################################################

# Setup all build dependencies
setup-env: setup-protoc setup-cmake setup-rust setup-openssl setup-deps
	@echo "$(GREEN)✓ Build environment setup complete$(NC)"

# Install Protocol Buffers compiler
setup-protoc:
	@echo "$(GREEN)Installing protoc...$(NC)"
ifeq ($(PLATFORM),linux)
	which protoc > /dev/null 2>&1 || (sudo apt-get update && sudo apt-get install -y protobuf-compiler)
else ifeq ($(PLATFORM),macos)
	which protoc > /dev/null 2>&1 || brew install protobuf
else ifeq ($(PLATFORM),windows)
	@echo "$(YELLOW)Note: protoc should be installed via GitHub Actions or manually on Windows$(NC)"
endif

# Install CMake
setup-cmake:
	@echo "$(GREEN)Installing CMake...$(NC)"
ifeq ($(PLATFORM),linux)
	which cmake > /dev/null 2>&1 || (sudo apt-get update && sudo apt-get install -y cmake)
else ifeq ($(PLATFORM),macos)
	which cmake > /dev/null 2>&1 || brew install cmake
else ifeq ($(PLATFORM),windows)
	@echo "$(YELLOW)Note: CMake should be installed via GitHub Actions or manually on Windows$(NC)"
endif

# Setup Rust toolchain
setup-rust:
	@echo "$(GREEN)Setting up Rust toolchain...$(NC)"
	@which rustup > /dev/null 2>&1 || (curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y)
	@rustup target add $(TARGET) 2>/dev/null || true

# Install OpenSSL (Windows only)
setup-openssl:
	@echo "$(GREEN)Installing OpenSSL...$(NC)"
ifeq ($(PLATFORM),windows)
ifeq ($(ARCH),x64)
	@echo "$(YELLOW)Installing OpenSSL for Windows x64 via vcpkg...$(NC)"
	@vcpkg install openssl:x64-windows 2>/dev/null || echo "vcpkg not available, skipping"
else ifeq ($(ARCH),arm64)
	@echo "$(YELLOW)Installing OpenSSL for Windows ARM64 via vcpkg...$(NC)"
	@vcpkg install openssl:arm64-windows 2>/dev/null || echo "vcpkg not available, skipping"
endif
else
	@echo "$(GREEN)OpenSSL will use system version$(NC)"
endif

# Install system dependencies
setup-deps:
	@echo "$(GREEN)Installing system dependencies...$(NC)"
	@if command -v apt-get >/dev/null 2>&1; then \
		echo "Using apt-get (Debian/Ubuntu)"; \
		echo 'deb [trusted=yes] https://pkgs.kquirk.com/apt all main' | sudo tee /etc/apt/sources.list.d/oqs.list > /dev/null; \
		sudo apt-get update; \
		sudo apt-get install -y libcurl4-openssl-dev libsasl2-dev build-essential pkg-config liboqs-dev cmake || \
		echo "liboqs-dev install failed, building from source..."; \
		if ! dpkg -s liboqs-dev >/dev/null 2>&1; then \
			echo "Building liboqs from source..."; \
			sudo apt-get install -y libcurl4-openssl-dev libsasl2-dev build-essential pkg-config cmake git ninja-build; \
			cd /tmp && rm -rf liboqs && git clone --depth 1 https://github.com/open-quantum-safe/liboqs.git && \
			cd liboqs && mkdir -p build && cd build && \
			cmake -GNinja -DCMAKE_INSTALL_PREFIX=/usr .. && \
			ninja && sudo ninja install && sudo ldconfig; \
		fi; \
	elif command -v yum >/dev/null 2>&1 || command -v dnf >/dev/null 2>&1; then \
		echo "Using yum/dnf (CentOS/RHEL/Fedora/manylinux)"; \
		if command -v dnf >/dev/null 2>&1; then PKGMGR=dnf; else PKGMGR=yum; fi; \
		$$PKGMGR install -y libcurl-devel openssl-devel openssl-libs cyrus-sasl-devel gcc gcc-c++ make cmake git ninja-build perl-core perl-IPC-Cmd perl-Data-Dumper; \
		if [ ! -f /usr/lib64/liboqs.so ] && [ ! -f /usr/local/lib64/liboqs.so ] && [ ! -f /usr/lib/x86_64-linux-gnu/liboqs.so ]; then \
			echo "Building liboqs from source..."; \
			cd /tmp && rm -rf liboqs && git clone --depth 1 https://github.com/open-quantum-safe/liboqs.git && \
			cd liboqs && mkdir -p build && cd build && \
			cmake -GNinja -DCMAKE_INSTALL_PREFIX=/usr .. && \
			ninja && ninja install && ldconfig; \
		else \
			echo "liboqs already installed, skipping build"; \
		fi; \
	elif command -v brew >/dev/null 2>&1; then \
		echo "Using brew (macOS)"; \
		brew update --quiet 2>/dev/null || true; \
		brew install --quiet cmake pkg-config openssl liboqs 2>/dev/null || true; \
		@mkdir -p .cargo; \
		echo '[target.x86_64-apple-darwin]\nrustflags = ["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]\n\n[target.aarch64-apple-darwin]\nrustflags = ["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]' > .cargo/config.toml; \
	else \
		echo "$(YELLOW)No supported package manager found, skipping system dependencies$(NC)"; \
	fi
ifeq ($(PLATFORM),windows)
	@echo "$(GREEN)Installing Windows dependencies via vcpkg...$(NC)"
	vcpkg install openssl:x64-windows librdkafka:x64-windows --classic 2>/dev/null || echo "Dependencies may already be installed"
	vcpkg integrate install 2>/dev/null || true
endif

################################################################################
# Help
################################################################################
help:
	@echo "$(GREEN)Ri Build System$(NC)"
	@echo ""
	@echo "$(YELLOW)Usage:$(NC)"
	@echo "  make <target> [VARIABLE=value]"
	@echo ""
	@echo "$(YELLOW)Build Targets:$(NC)"
	@echo "  build              Build Rust library (default)"
	@echo "  build-cli          Build CLI tool (ric)"
	@echo "  build-python       Build Python wheel"
	@echo "  build-c            Build C static library and headers"
	@echo "  build-java         Build Java JAR"
	@echo "  build-all          Build all components"
	@echo ""
	@echo "$(YELLOW)Platform-Specific Builds:$(NC)"
	@echo "  build-linux-x64        Build for Linux x64"
	@echo "  build-linux-arm64      Build for Linux ARM64"
	@echo "  build-windows-x64      Build for Windows x64"
	@echo "  build-windows-arm64    Build for Windows ARM64"
	@echo "  build-macos-x64        Build for macOS x64"
	@echo "  build-macos-arm64      Build for macOS ARM64"
	@echo ""
	@echo "$(YELLOW)Testing & Quality:$(NC)"
	@echo "  test               Run all tests"
	@echo "  test-unit          Run unit tests"
	@echo "  test-integration   Run integration tests"
	@echo "  lint               Run linter (clippy)"
	@echo "  fmt                Format code"
	@echo "  check              Check code without building"
	@echo ""
	@echo "$(YELLOW)Documentation:$(NC)"
	@echo "  doc                Build all documentation"
	@echo "  doc-rust           Build Rust documentation"
	@echo "  doc-python         Build Python documentation"
	@echo ""
	@echo "$(YELLOW)Installation:$(NC)"
	@echo "  install            Install Rust library"
	@echo "  install-python     Install Python package"
	@echo ""
	@echo "$(YELLOW)Cleaning:$(NC)"
	@echo "  clean              Clean build artifacts"
	@echo "  clean-all          Clean all generated files"
	@echo "  clean-python       Clean Python build artifacts"
	@echo "  clean-java         Clean Java build artifacts"
	@echo ""
	@echo "$(YELLOW)Docker:$(NC)"
	@echo "  docker-build       Build manylinux Python wheel"
	@echo "  docker-clean       Clean Docker artifacts"
	@echo ""
	@echo "$(YELLOW)Variables:$(NC)"
	@echo "  PLATFORM=$(PLATFORM)        Target platform (linux/windows/macos)"
	@echo "  ARCH=$(ARCH)                Target architecture (x64/arm64)"
	@echo "  PYTHON_VER=$(PYTHON_VER)    Python version (3.8-3.14)"
	@echo "  RELEASE=$(RELEASE)          Release mode (true/false)"
	@echo "  TARGET=$(TARGET)            Rust target triple"
	@echo "  FEATURES=...                Cargo features to enable"
	@echo ""
	@echo "$(YELLOW)Examples:$(NC)"
	@echo "  make build                           # Build for current platform"
	@echo "  make build-python PYTHON_VER=3.12    # Build Python 3.12 wheel"
	@echo "  make build-linux-arm64               # Cross-compile for Linux ARM64"
	@echo "  make build-c                         # Build C static library"
	@echo "  make test FEATURES=full              # Test with all features"

################################################################################
# Core Build Targets
################################################################################

# Build Rust library (default)
build:
	@echo "$(GREEN)Building Ri library for $(PLATFORM) $(ARCH)...$(NC)"
ifeq ($(PLATFORM),linux)
	@$(MAKE) setup-deps
endif
ifeq ($(PLATFORM)-$(ARCH),windows-arm64)
	cargo build $(BUILD_MODE) --target $(TARGET) --no-default-features $(if $(FEATURES),--features $(FEATURES),)
else ifeq ($(PLATFORM),windows)
	cargo build $(BUILD_MODE) --target $(TARGET) --no-default-features --features $(WINDOWS_FEATURES)
else
	cargo build $(BUILD_MODE) --target $(TARGET) $(if $(FEATURES),--features $(FEATURES),)
endif
	@echo "$(GREEN)✓ Build complete: $(TARGET_DIR)/$(LIB_PREFIX)ri.$(LIB_EXT)$(NC)"

# Build CLI tool
build-cli:
	@echo "$(GREEN)Building CLI tool for $(PLATFORM) $(ARCH)...$(NC)"
ifeq ($(PLATFORM),linux)
	@$(MAKE) setup-deps
endif
ifeq ($(PLATFORM)-$(ARCH),windows-arm64)
	cargo build $(BUILD_MODE) -p ric --target $(TARGET) --no-default-features --features $(RI_WINDOWS_FEATURES)
else ifeq ($(PLATFORM),windows)
	cargo build $(BUILD_MODE) -p ric --target $(TARGET) --no-default-features --features $(RI_WINDOWS_FEATURES)
else
	cargo build $(BUILD_MODE) -p ric --target $(TARGET)
endif
ifeq ($(PLATFORM),windows)
	@echo "$(GREEN)✓ Build complete: $(TARGET_DIR)/ric.exe$(NC)"
else
	@echo "$(GREEN)✓ Build complete: $(TARGET_DIR)/ric$(NC)"
endif

# Build Python wheel
build-python:
	@echo "$(GREEN)Building Python wheel for $(PLATFORM) $(ARCH) (Python $(PYTHON_VER))...$(NC)"
	@mkdir -p $(DIST_DIR)
ifeq ($(PLATFORM),linux)
ifneq ($(ARCH),arm64)
	@echo "$(YELLOW)Using manylinux container for Linux wheel...$(NC)"
	docker run --rm -v "$(PWD)":/io quay.io/pypa/manylinux_2_17_x86_64 \
		/bin/bash -c "cd /io && make setup-deps && \
			PYTHON_VER=$(PYTHON_VER) && \
			PYTHON_MAJOR=\$${PYTHON_VER%%.*} && \
			PYTHON_MINOR=\$${PYTHON_VER#*.} && \
			PYTHON_BIN=/opt/python/cp\$${PYTHON_MAJOR}\$${PYTHON_MINOR}-cp\$${PYTHON_MAJOR}\$${PYTHON_MINOR}/bin/python && \
			\$$PYTHON_BIN -m pip install maturin && \
			\$$PYTHON_BIN -m maturin build --release --target $(TARGET) -o /io/$(DIST_DIR) \
				--no-default-features \
			--features pyo3,grpc,websocket,rabbitmq,cache,queue,gateway,service_mesh,auth,observability,postgres,mysql,sqlite,http_client,system_info,config_hot_reload,protocol,kafka,etcd
else
	@echo "$(YELLOW)Building native Linux ARM64 wheel...$(NC)"
	$(MAKE) setup-deps
	PYTHON_VER=$(PYTHON_VER) && \
	PYTHON_BIN=python$${PYTHON_VER%.*} && \
	$$PYTHON_BIN -m pip install maturin && \
	$$PYTHON_BIN -m maturin build --release --target $(TARGET) -o $(DIST_DIR) \
		--no-default-features \
		--features pyo3,grpc,websocket,rabbitmq,cache,queue,gateway,service_mesh,auth,observability,postgres,mysql,sqlite,http_client,system_info,config_hot_reload,protocol,kafka,etcd
endif
else ifeq ($(PLATFORM),windows)
	@echo "$(YELLOW)Building Windows wheel...$(NC)"
	pip install maturin
ifeq ($(ARCH),arm64)
	maturin build --release --target $(TARGET) -o $(DIST_DIR) --no-default-features --features pyo3,grpc,websocket,rabbitmq,cache,queue,gateway,service_mesh,auth,observability,postgres,mysql,sqlite,http_client,system_info,config_hot_reload,etcd
else
	maturin build --release --target $(TARGET) -o $(DIST_DIR) --no-default-features --features pyo3,grpc,websocket,rabbitmq,cache,queue,gateway,service_mesh,auth,observability,postgres,mysql,sqlite,http_client,system_info,config_hot_reload,protocol,etcd
endif
else
	@echo "$(YELLOW)Building native wheel...$(NC)"
	pip install maturin
	OPENSSL_NO_VENDOR=1 maturin build --release --target $(TARGET) -o $(DIST_DIR) \
		--no-default-features \
		--features pyo3,grpc,websocket,rabbitmq,cache,queue,gateway,service_mesh,auth,observability,postgres,mysql,sqlite,http_client,system_info,config_hot_reload,protocol,kafka,etcd
endif
	@echo "$(GREEN)✓ Python wheel built: $(DIST_DIR)/$(NC)"
	@ls -lh $(DIST_DIR)/*.whl 2>/dev/null || echo "No wheels found"

# Build C static library and headers
build-c:
	@echo "$(GREEN)Building C static library for $(PLATFORM) $(ARCH)...$(NC)"
ifeq ($(PLATFORM),linux)
	@$(MAKE) setup-deps
endif
ifeq ($(PLATFORM),windows)
	cargo build $(BUILD_MODE) --target $(TARGET) --no-default-features --features c
else
	cargo build $(BUILD_MODE) --target $(TARGET) --no-default-features --features c
endif
	@echo "$(GREEN)Generating C headers...$(NC)"
	@mkdir -p $(INCLUDE_DIR)
	@if ! command -v cbindgen >/dev/null 2>&1; then cargo install cbindgen; fi
	cbindgen --crate ri -o $(INCLUDE_DIR)/ri.h
ifeq ($(PLATFORM),windows)
	@echo "$(GREEN)✓ C library built: $(TARGET_DIR)/ri.$(STATIC_EXT)$(NC)"
else
	@echo "$(GREEN)✓ C library built: $(TARGET_DIR)/$(LIB_PREFIX)ri.$(STATIC_EXT)$(NC)"
endif
	@echo "$(GREEN)✓ C header generated: $(INCLUDE_DIR)/ri.h$(NC)"

# Build Java JAR (requires all native libraries)
build-java:
	@echo "$(GREEN)Building Java JAR...$(NC)"
	@echo "$(YELLOW)Note: This requires pre-built native libraries for all platforms$(NC)"
	@mkdir -p java/src/main/resources/native
	@# Copy native libraries if they exist
	@for platform in linux-x64 linux-arm64 windows-x64 windows-arm64 macos-x64 macos-arm64; do \
		if [ -d "native/$$platform" ]; then \
			mkdir -p "java/src/main/resources/native/$$platform"; \
			cp -r native/$$platform/* java/src/main/resources/native/$$platform/ 2>/dev/null || true; \
		fi; \
	done
	cd java && mvn clean package -DskipTests
	@echo "$(GREEN)✓ Java JAR built: java/target/ri-*.jar$(NC)"

# Build all components
build-all: build build-cli build-python build-c
	@echo "$(GREEN)✓ All components built successfully$(NC)"

################################################################################
# Platform-Specific Build Targets
################################################################################

# Linux builds
build-linux-x64:
	@$(MAKE) build PLATFORM=linux ARCH=x64 TARGET=x86_64-unknown-linux-gnu

build-linux-arm64:
	@$(MAKE) build PLATFORM=linux ARCH=arm64 TARGET=aarch64-unknown-linux-gnu

# Windows builds
build-windows-x64:
	@$(MAKE) build PLATFORM=windows ARCH=x64 TARGET=x86_64-pc-windows-msvc

build-windows-arm64:
	cargo build $(BUILD_MODE) --target aarch64-pc-windows-msvc --no-default-features --features $(WINDOWS_FEATURES)
	@echo "$(GREEN)✓ Build complete: target/aarch64-pc-windows-msvc/release/ri.dll$(NC)"

# macOS builds
build-macos-x64:
	@$(MAKE) build PLATFORM=macos ARCH=x64 TARGET=x86_64-apple-darwin

build-macos-arm64:
	@$(MAKE) build PLATFORM=macos ARCH=arm64 TARGET=aarch64-apple-darwin

# CLI builds for all platforms
build-cli-linux-x64:
	@$(MAKE) build-cli PLATFORM=linux ARCH=x64 TARGET=x86_64-unknown-linux-gnu

build-cli-linux-arm64:
	@$(MAKE) build-cli PLATFORM=linux ARCH=arm64 TARGET=aarch64-unknown-linux-gnu

build-cli-windows-x64:
	@$(MAKE) build-cli PLATFORM=windows ARCH=x64 TARGET=x86_64-pc-windows-msvc

build-cli-windows-arm64:
	@echo "$(GREEN)Building CLI for Windows ARM64...$(NC)"
	cargo build $(BUILD_MODE) -p ric --target aarch64-pc-windows-msvc --no-default-features --features $(RI_WINDOWS_FEATURES)
	@echo "$(GREEN)✓ Build complete: target/aarch64-pc-windows-msvc/release/ric.exe$(NC)"

build-cli-macos-x64:
	@$(MAKE) build-cli PLATFORM=macos ARCH=x64 TARGET=x86_64-apple-darwin

build-cli-macos-arm64:
	@$(MAKE) build-cli PLATFORM=macos ARCH=arm64 TARGET=aarch64-apple-darwin

# C library builds for all platforms
build-c-linux-x64:
	@$(MAKE) build-c PLATFORM=linux ARCH=x64 TARGET=x86_64-unknown-linux-gnu

build-c-linux-arm64:
	@$(MAKE) build-c PLATFORM=linux ARCH=arm64 TARGET=aarch64-unknown-linux-gnu

build-c-windows-x64:
	@$(MAKE) build-c PLATFORM=windows ARCH=x64 TARGET=x86_64-pc-windows-msvc

build-c-windows-arm64:
	@$(MAKE) build-c PLATFORM=windows ARCH=arm64 TARGET=aarch64-pc-windows-msvc

build-c-macos-x64:
	@$(MAKE) build-c PLATFORM=macos ARCH=x64 TARGET=x86_64-apple-darwin

build-c-macos-arm64:
	@$(MAKE) build-c PLATFORM=macos ARCH=arm64 TARGET=aarch64-apple-darwin

################################################################################
# Testing
################################################################################

test:
	@echo "$(GREEN)Running tests...$(NC)"
	cargo test --all-features --target $(TARGET)
	@echo "$(GREEN)✓ Tests passed$(NC)"

test-unit:
	@echo "$(GREEN)Running unit tests...$(NC)"
	cargo test --lib --all-features --target $(TARGET)
	@echo "$(GREEN)✓ Unit tests passed$(NC)"

test-integration:
	@echo "$(GREEN)Running integration tests...$(NC)"
	cargo test --test '*' --all-features --target $(TARGET)
	@echo "$(GREEN)✓ Integration tests passed$(NC)"

################################################################################
# Code Quality
################################################################################

lint:
	@echo "$(GREEN)Running linter...$(NC)"
	cargo clippy --all-features --target $(TARGET) -- -D warnings
	@echo "$(GREEN)✓ Linting passed$(NC)"

fmt:
	@echo "$(GREEN)Formatting code...$(NC)"
	cargo fmt --all
	@echo "$(GREEN)✓ Code formatted$(NC)"

check:
	@echo "$(GREEN)Checking code...$(NC)"
	cargo check --all-features --target $(TARGET)
	@echo "$(GREEN)✓ Check passed$(NC)"

################################################################################
# Documentation
################################################################################

doc: doc-rust doc-python
	@echo "$(GREEN)✓ Documentation built$(NC)"

doc-rust:
	@echo "$(GREEN)Building Rust documentation...$(NC)"
	cargo doc --no-deps --all-features --target $(TARGET)
	@echo "$(GREEN)✓ Rust documentation: target/doc/ri/$(NC)"

doc-python:
	@echo "$(GREEN)Building Python documentation...$(NC)"
	@pip install pdoc 2>/dev/null || true
	pdoc -o target/doc/python ri
	@echo "$(GREEN)✓ Python documentation: target/doc/python/$(NC)"

################################################################################
# Installation
################################################################################

install:
	@echo "$(GREEN)Installing Ri library...$(NC)"
	cargo install --path .
	@echo "$(GREEN)✓ Installation complete$(NC)"

install-python:
	@echo "$(GREEN)Installing Python package...$(NC)"
	pip install maturin
	maturin develop --release --features pyo3
	@echo "$(GREEN)✓ Python package installed$(NC)"

################################################################################
# Cleaning
################################################################################

clean:
	@echo "$(YELLOW)Cleaning build artifacts...$(NC)"
	cargo clean
	@rm -rf $(DIST_DIR)
	@rm -rf $(INCLUDE_DIR)
	@echo "$(GREEN)✓ Clean complete$(NC)"

clean-all: clean
	@echo "$(YELLOW)Cleaning all generated files...$(NC)"
	@rm -rf target
	@rm -rf dist
	@rm -rf include
	@rm -rf java/target
	@rm -rf native
	@find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name "*.egg-info" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name ".mypy_cache" -exec rm -rf {} + 2>/dev/null || true
	@echo "$(GREEN)✓ All generated files cleaned$(NC)"

clean-python:
	@echo "$(YELLOW)Cleaning Python artifacts...$(NC)"
	@rm -rf dist
	@find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name "*.egg-info" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name ".mypy_cache" -exec rm -rf {} + 2>/dev/null || true
	@echo "$(GREEN)✓ Python artifacts cleaned$(NC)"

clean-java:
	@echo "$(YELLOW)Cleaning Java artifacts...$(NC)"
	@rm -rf java/target
	@echo "$(GREEN)✓ Java artifacts cleaned$(NC)"

################################################################################
# Docker Support
################################################################################

docker-build:
	@echo "$(GREEN)Building manylinux Python wheel in Docker...$(NC)"
	@echo "$(YELLOW)This requires Docker to be installed and running$(NC)"
	@$(MAKE) build-python PLATFORM=linux

docker-clean:
	@echo "$(YELLOW)Cleaning Docker artifacts...$(NC)"
	@docker system prune -f 2>/dev/null || true
	@echo "$(GREEN)✓ Docker artifacts cleaned$(NC)"

################################################################################
# CI/CD Helper Targets
################################################################################

# For GitHub Actions - builds all artifacts for a specific platform
ci-build-all: build build-cli build-python build-c
	@echo "$(GREEN)✓ CI build complete for $(PLATFORM) $(ARCH)$(NC)"

# For GitHub Actions - prepares artifacts for upload
ci-package:
	@echo "$(GREEN)Packaging artifacts...$(NC)"
	@mkdir -p artifacts
ifeq ($(PLATFORM),windows)
	@cp $(TARGET_DIR)/ric.exe artifacts/ric-$(PLATFORM)-$(ARCH).exe 2>/dev/null || true
	@cp $(TARGET_DIR)/ri.dll artifacts/ri-$(PLATFORM)-$(ARCH).dll 2>/dev/null || true
	@cp $(TARGET_DIR)/ri.lib artifacts/ri-$(PLATFORM)-$(ARCH).lib 2>/dev/null || true
else
	@cp $(TARGET_DIR)/ric artifacts/ric-$(PLATFORM)-$(ARCH) 2>/dev/null || true
	@cp $(TARGET_DIR)/$(LIB_PREFIX)ri.$(LIB_EXT) artifacts/ri-$(PLATFORM)-$(ARCH).$(LIB_EXT) 2>/dev/null || true
	@cp $(TARGET_DIR)/$(LIB_PREFIX)ri.$(STATIC_EXT) artifacts/ri-$(PLATFORM)-$(ARCH).$(STATIC_EXT) 2>/dev/null || true
endif
	@cp $(INCLUDE_DIR)/ri.h artifacts/ri.h 2>/dev/null || true
	@cp -r $(DIST_DIR)/*.whl artifacts/ 2>/dev/null || true
	@echo "$(GREEN)✓ Artifacts packaged in artifacts/$(NC)"

# Show build info
info:
	@echo "$(GREEN)Build Configuration:$(NC)"
	@echo "  Platform:     $(PLATFORM)"
	@echo "  Architecture: $(ARCH)"
	@echo "  Target:       $(TARGET)"
	@echo "  Build Mode:   $(if $(filter true,$(RELEASE)),Release,Debug)"
	@echo "  Python Ver:   $(PYTHON_VER)"
	@echo "  Features:     $(or $(FEATURES),default)"
	@echo ""
	@echo "$(GREEN)Paths:$(NC)"
	@echo "  Target Dir:   $(TARGET_DIR)"
	@echo "  Dist Dir:     $(DIST_DIR)"
	@echo "  Include Dir:  $(INCLUDE_DIR)"
	@echo ""
	@echo "$(GREEN)Library Names:$(NC)"
	@echo "  Dynamic:      $(LIB_PREFIX)ri.$(LIB_EXT)"
	@echo "  Static:       $(LIB_PREFIX)ri.$(STATIC_EXT)"

################################################################################
# Development Helpers
################################################################################

# Watch for changes and rebuild
watch:
	@echo "$(GREEN)Watching for changes...$(NC)"
	cargo watch -x "build $(BUILD_MODE) --target $(TARGET)"

# Run with debug logging
run:
	@echo "$(GREEN)Running Ri...$(NC)"
	cargo run $(BUILD_MODE) --target $(TARGET)

# Generate completion scripts for shells
completion:
	@echo "$(GREEN)Generating shell completion scripts...$(NC)"
	@mkdir -p completions
	@echo "Bash completion: completions/ric.bash"
	@echo "Zsh completion: completions/_ric"
	@echo "Fish completion: completions/ric.fish"
	@echo "$(GREEN)✓ Completion scripts generated$(NC)"