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
137 changes: 137 additions & 0 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Copilot Instructions for mlibc Project

## Project Overview / 项目概述

**English:**
mlibc is a lightweight C library specifically designed for deeply embedded systems. The project focuses on implementing POSIX.1-2001 (PSE51) and related ANSI C APIs to provide essential system functionality for resource-constrained embedded environments. This library aims to offer a minimal yet complete implementation of standard C library functions while maintaining compatibility with various embedded architectures including RISC-V, ARM, and AArch64.

Key features:
- PSE51 (POSIX.1-2001) compliance
- ANSI C standard library implementation
- Multi-architecture support (RISC-V32/64, ARM, AArch64)
- Minimal memory footprint
- Embedded-first design philosophy

**中文:**
mlibc 是一个专门为深度嵌入式系统设计的轻量级 C 库。该项目专注于实现 POSIX.1-2001 (PSE51) 和相关的 ANSI C API,为资源受限的嵌入式环境提供基础系统功能。本库旨在提供标准 C 库函数的最小化但完整的实现,同时保持与多种嵌入式架构(包括 RISC-V、ARM 和 AArch64)的兼容性。

主要特性:
- 符合 PSE51 (POSIX.1-2001) 标准
- ANSI C 标准库实现
- 多架构支持 (RISC-V32/64, ARM, AArch64)
- 最小内存占用
- 嵌入式优先的设计理念

## Code Review Instructions / 代码审查指南

### Review Language / 审查语言
When performing code reviews, respond in Chinese and English.
进行代码审查时,请使用中英文双语回复。

### Review Focus Areas / 审查重点

**English:**
1. **Memory Safety**: Pay special attention to buffer overflows, memory leaks, and pointer safety
2. **Embedded Constraints**: Consider memory usage, stack usage, and performance implications
3. **Standard Compliance**: Ensure compliance with PSE51 and ANSI C standards
4. **Architecture Portability**: Verify code works across supported architectures
5. **Error Handling**: Check for proper error handling and edge cases
6. **Documentation**: Ensure code is well-documented for embedded developers

**中文:**
1. **内存安全**: 特别关注缓冲区溢出、内存泄漏和指针安全性
2. **嵌入式约束**: 考虑内存使用、栈使用和性能影响
3. **标准合规性**: 确保符合 PSE51 和 ANSI C 标准
4. **架构可移植性**: 验证代码在支持的架构间正常工作
5. **错误处理**: 检查适当的错误处理和边界情况
6. **文档说明**: 确保代码有良好的文档供嵌入式开发者使用

### Review Format / 审查格式

Please structure your reviews as follows:
请按以下格式组织您的审查:

```
## Code Review / 代码审查

### Summary / 总结
**English:** [Brief summary of changes and overall assessment]
**中文:** [变更简要总结和整体评估]

### Issues Found / 发现的问题
**English:**
- [Issue 1 description]
- [Issue 2 description]

**中文:**
- [问题1描述]
- [问题2描述]

### Recommendations / 建议
**English:**
- [Recommendation 1]
- [Recommendation 2]

**中文:**
- [建议1]
- [建议2]

### Positive Aspects / 积极方面
**English:**
- [Good practice 1]
- [Good practice 2]

**中文:**
- [良好实践1]
- [良好实践2]
```

### Specific Considerations for mlibc / mlibc 特定考虑事项

**English:**
- **PSE51 Compatibility**: Verify function signatures and behavior match PSE51 specifications
- **Minimal Dependencies**: Avoid introducing unnecessary dependencies
- **Cross-platform Build**: Consider SCons build system compatibility
- **Test Coverage**: Ensure adequate test coverage in testcases/ directory
- **Performance**: Optimize for embedded environments with limited resources

**中文:**
- **PSE51 兼容性**: 验证函数签名和行为符合 PSE51 规范
- **最小依赖**: 避免引入不必要的依赖
- **跨平台构建**: 考虑 SCons 构建系统兼容性
- **测试覆盖**: 确保 testcases/ 目录中有充分的测试覆盖
- **性能优化**: 针对资源有限的嵌入式环境进行优化

### Common Patterns to Look For / 需要关注的常见模式

**English:**
- Use of `__attribute__` for compiler-specific optimizations
- Proper errno handling according to POSIX standards
- Thread-safe implementations where required
- Minimal stack usage in recursive functions
- Proper header guard usage

**中文:**
- 使用 `__attribute__` 进行编译器特定优化
- 按照 POSIX 标准正确处理 errno
- 在需要时实现线程安全
- 递归函数中的最小栈使用
- 正确使用头文件保护

### Quality Gates / 质量门槛

**English:**
All code contributions should meet these criteria:
- Compiles without warnings on all supported architectures
- Passes existing test cases
- Includes appropriate test coverage for new functionality
- Maintains or improves performance benchmarks
- Follows project coding style

**中文:**
所有代码贡献都应满足以下标准:
- 在所有支持的架构上无警告编译
- 通过现有测试用例
- 为新功能包含适当的测试覆盖
- 维持或改善性能基准
- 遵循项目编码风格
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Build files
build/
build.log
test_results

toolchain/arm-linux-eabi_for_x86_64-pc-linux-gnu
toolchain/riscv64-unkwown-elf_for_x86_64-pc-linux-gnu
toolchain/binutils-*
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ endif

PROJECT_PATH := .

# Set default toolchain if MLIBC_TOOLCHAIN is not set
ifeq ($(MLIBC_TOOLCHAIN),)
MLIBC_TOOLCHAIN := $(PROJECT_PATH)/toolchain/arm-linux-eabi_for_x86_64-pc-linux-gnu/bin/arm-linux-eabi-
endif

CC := $(MLIBC_TOOLCHAIN)gcc
AR := $(MLIBC_TOOLCHAIN)ar

Expand Down
226 changes: 226 additions & 0 deletions testcases/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
# Master Makefile for mlibc PSE51 Test Suite
# Supports both PC and embedded test environments

# Test directories
TEST_DIRS := assert ctype fcntl locale signal stat stdio stdlib string time unistd utsname

# Colors for output
RED := \033[0;31m
GREEN := \033[0;32m
YELLOW := \033[1;33m
BLUE := \033[0;34m
NC := \033[0m

# Default target
.PHONY: all
all: help

# Help message
.PHONY: help
help:
@echo "$(BLUE)=== mlibc PSE51 Test Suite ===$(NC)"
@echo ""
@echo "$(YELLOW)PC Environment Tests:$(NC)"
@echo " make pc-build - Build all PC tests"
@echo " make pc-test - Run all PC tests"
@echo " make pc-clean - Clean PC test builds"
@echo ""
@echo "$(YELLOW)Embedded Environment Tests:$(NC)"
@echo " make embedded-build - Build all embedded tests"
@echo " make embedded-test - Run all embedded tests in QEMU"
@echo " make embedded-clean - Clean embedded test builds"
@echo ""
@echo "$(YELLOW)Combined Commands:$(NC)"
@echo " make build-all - Build both PC and embedded tests"
@echo " make test-all - Run both PC and embedded tests"
@echo " make clean-all - Clean all builds"
@echo ""
@echo "$(YELLOW)Quick Commands:$(NC)"
@echo " make test - Run all PC tests (quick)"
@echo " make embedded - Build and run embedded tests"
@echo ""
@echo "$(YELLOW)Individual Tests:$(NC)"
@echo " make pc-test-<dir> - Run specific PC test (e.g., pc-test-stdio)"
@echo " make embedded-test-<dir> - Run specific embedded test"

# PC Environment Targets
.PHONY: pc-build
pc-build:
@echo "$(BLUE)Building all PC tests...$(NC)"
@for dir in $(TEST_DIRS); do \
if [ -f $$dir/Makefile ]; then \
echo "$(YELLOW)Building $$dir...$(NC)"; \
$(MAKE) -C $$dir pc --no-print-directory || exit 1; \
fi; \
done
@echo "$(GREEN)All PC tests built successfully!$(NC)"

.PHONY: pc-test
pc-test: pc-build
@echo "$(BLUE)Running all PC tests...$(NC)"
@total=0; passed=0; failed=0; \
for dir in $(TEST_DIRS); do \
if [ -f $$dir/Makefile ]; then \
echo "$(YELLOW)\nTesting $$dir...$(NC)"; \
if $(MAKE) -C $$dir test --no-print-directory; then \
passed=$$((passed + 1)); \
else \
failed=$$((failed + 1)); \
echo "$(RED)$$dir tests failed!$(NC)"; \
fi; \
total=$$((total + 1)); \
fi; \
done; \
echo "\n$(BLUE)=== PC Test Summary ===$(NC)"; \
echo "Total: $$total, Passed: $(GREEN)$$passed$(NC), Failed: $(RED)$$failed$(NC)"

.PHONY: pc-clean
pc-clean:
@echo "$(BLUE)Cleaning PC test builds...$(NC)"
@for dir in $(TEST_DIRS); do \
if [ -f $$dir/Makefile ]; then \
$(MAKE) -C $$dir clean --no-print-directory; \
fi; \
done
@echo "$(GREEN)PC tests cleaned!$(NC)"

# Embedded Environment Targets
.PHONY: embedded-build
embedded-build: check-mlibc
@echo "$(BLUE)Building all embedded tests...$(NC)"
@for dir in $(TEST_DIRS); do \
if [ -f $$dir/Makefile.embedded ]; then \
echo "$(YELLOW)Building embedded $$dir...$(NC)"; \
$(MAKE) -C $$dir -f Makefile.embedded --no-print-directory || exit 1; \
fi; \
done
@echo "$(GREEN)All embedded tests built successfully!$(NC)"

.PHONY: embedded-test
embedded-test: embedded-build
@echo "$(BLUE)Running embedded tests...$(NC)"
@./test

.PHONY: embedded-clean
embedded-clean:
@echo "$(BLUE)Cleaning embedded test builds...$(NC)"
@for dir in $(TEST_DIRS); do \
if [ -f $$dir/Makefile.embedded ]; then \
$(MAKE) -C $$dir -f Makefile.embedded clean --no-print-directory; \
fi; \
done
@echo "$(GREEN)Embedded tests cleaned!$(NC)"

# Combined targets
.PHONY: build-all
build-all: pc-build embedded-build

.PHONY: test-all
test-all: pc-test embedded-test

.PHONY: clean-all
clean-all: pc-clean embedded-clean
@rm -f embedded_test_results.log embedded_test_summary.txt

# Quick shortcuts
.PHONY: test
test: pc-test

.PHONY: embedded
embedded: embedded-test

# Individual test targets
define INDIVIDUAL_TEST_RULES
.PHONY: pc-test-$(1)
pc-test-$(1):
@echo "$(BLUE)Testing $(1) (PC)...$(NC)"
@if [ -f $(1)/Makefile ]; then \
$(MAKE) -C $(1) test; \
else \
echo "$(RED)No PC tests for $(1)$(NC)"; \
fi

.PHONY: embedded-test-$(1)
embedded-test-$(1): check-mlibc
@echo "$(BLUE)Testing $(1) (embedded)...$(NC)"
@if [ -f $(1)/test ]; then \
cd $(1) && ./test; \
else \
echo "$(RED)No embedded tests for $(1)$(NC)"; \
fi
endef

$(foreach dir,$(TEST_DIRS),$(eval $(call INDIVIDUAL_TEST_RULES,$(dir))))

# Check if mlibc is built
.PHONY: check-mlibc
check-mlibc:
@ARCH=$${EMBEDDED_ARCH:-riscv32}; \
if [ ! -f ../build/$$ARCH/libmlibc.a ]; then \
if [ "$$ARCH" = "arm" ]; then \
echo "$(YELLOW)Building mlibc for ARM...$(NC)"; \
elif [ "$$ARCH" = "aarch64" ]; then \
echo "$(YELLOW)Building mlibc for AArch64...$(NC)"; \
elif [ "$$ARCH" = "riscv64" ]; then \
echo "$(YELLOW)Building mlibc for RISC-V 64-bit...$(NC)"; \
else \
echo "$(YELLOW)Building mlibc for RISC-V 32-bit...$(NC)"; \
fi; \
$(MAKE) -C .. ARCH=$$ARCH || { \
echo "$(RED)Failed to build mlibc!$(NC)"; \
echo "Please ensure you have the $$ARCH toolchain installed."; \
echo "Set MLIBC_TOOLCHAIN environment variable if needed."; \
exit 1; \
}; \
fi

# Clean everything including logs
.PHONY: distclean
distclean: clean-all
@rm -rf */build */test_results
@rm -f */*.o */*.log
@echo "$(GREEN)All test artifacts removed!$(NC)"

# Show test status
.PHONY: status
status:
@echo "$(BLUE)=== Test Suite Status ===$(NC)"
@echo "\n$(YELLOW)PC Tests:$(NC)"
@for dir in $(TEST_DIRS); do \
if [ -f $$dir/Makefile ]; then \
if [ -d $$dir/test_results ]; then \
echo " $$dir: $(GREEN)Built$(NC)"; \
else \
echo " $$dir: Not built"; \
fi; \
fi; \
done
@echo "\n$(YELLOW)Embedded Tests:$(NC)"
@for dir in $(TEST_DIRS); do \
if [ -f $$dir/Makefile.embedded ]; then \
if [ -d $$dir/build ]; then \
echo " $$dir: $(GREEN)Built$(NC)"; \
else \
echo " $$dir: Not built"; \
fi; \
fi; \
done

# Install dependencies (informational)
.PHONY: deps
deps:
@echo "$(BLUE)=== Dependencies ===$(NC)"
@echo ""
@echo "$(YELLOW)For PC tests:$(NC)"
@echo " - GCC or compatible C compiler"
@echo " - POSIX-compliant system"
@echo ""
@echo "$(YELLOW)For embedded tests:$(NC)"
@echo " - RISC-V 32-bit toolchain (riscv32-unknown-elf-gcc)"
@echo " - QEMU system emulator (qemu-system-riscv32)"
@echo ""
@echo "$(YELLOW)Ubuntu/Debian installation:$(NC)"
@echo " sudo apt-get install gcc-riscv64-unknown-elf qemu-system-misc"
@echo ""
@echo "$(YELLOW)Environment variables:$(NC)"
@echo " export MLIBC_TOOLCHAIN=riscv32-unknown-elf-"
Loading