1- # include config.mk
1+ # ============================================================
2+ # Luma / Lux Build System (Cross-Platform)
3+ # Works on Linux, macOS, and Windows
4+ # ============================================================
25
3- # # LLVM configuration
4- # LLVM_CFLAGS := $(shell llvm-config --cflags)
5- # LLVM_LDFLAGS := $(shell llvm-config --ldflags --system-libs --libs core analysis bitwriter target)
6+ # Include default config
7+ include config.default.mk
68
7- # # Add LLVM flags to existing flags
8- # override CFLAGS += $(LLVM_CFLAGS)
9- # override LDFLAGS += $(LLVM_LDFLAGS)
9+ # Optional: include user config if present
10+ ifneq ($(wildcard config.mk) ,)
11+ include config.mk
12+ endif
1013
11- # # Detect platform and define commands
12- include config.default.mk
14+ # ------------------------------------------------------------
15+ # Paths & Files
16+ # ------------------------------------------------------------
1317
14- # Detect platform and define commands
18+ BIN := luma
19+
20+ # Detect OS and set appropriate commands
1521ifeq ($(OS ) ,Windows_NT)
16- SHELL ?= cmd.exe
17- MKDIR = if not exist "$(subst /,\,$1)" mkdir "$(subst /,\,$1)"
18- RMDIR = if exist "$(subst /,\,$1)" rmdir /s /q "$(subst /,\,$1)"
19- DEL = if exist "$(subst /,\,$1)" del /q "$(subst /,\,$1)"
20- EXE = .exe
21- PATHSEP = \\
22+ # Windows commands
23+ SHELL := cmd.exe
24+ MKDIR = if not exist "$(subst /,\,$(1 ) ) " mkdir "$(subst /,\,$(1 ) ) "
25+ RMDIR = if exist "$(subst /,\,$(1 ) ) " rmdir /s /q "$(subst /,\,$(1 ) ) "
26+ DEL = if exist "$(1 ) " del /q "$(1 ) "
27+ RM = del /q
28+ EXE = .exe
2229else
23- SHELL ?= /bin/sh
24- MKDIR = mkdir -p $1
25- RMDIR = rm -rf $1
26- DEL = rm -f $1
27- EXE =
28- PATHSEP = /
30+ # Unix commands
31+ SHELL := /bin/bash
32+ MKDIR = mkdir -p $(1 )
33+ RMDIR = rm -rf $(1 )
34+ DEL = rm -f $(1 )
35+ RM = rm -f
36+ EXE =
2937endif
3038
31- BIN := luma$(EXE )
39+ # ------------------------------------------------------------
40+ # Targets
41+ # ------------------------------------------------------------
3242
33- ifneq ($(wildcard config.mk) ,)
34- include config.mk
35- endif
43+ .PHONY : all clean debug test check llvm-test view-ir run-llvm compile-native help
3644
37- .PHONY : all clean debug test check llvm-test
45+ # Default target
46+ all : $(BIN )$(EXE )
3847
39- all : $(BIN )
40-
41- $(BIN ) : $(OBJ_FILES )
42- $(call MKDIR,$(@D ) )
48+ # Build binary
49+ $(BIN )$(EXE ) : $(OBJ_FILES )
50+ ifeq ($(OS ) ,Windows_NT)
51+ @if not exist "$(dir $@)" mkdir "$(subst /,\,$(dir $@))"
52+ else
53+ @mkdir -p $(dir $@)
54+ endif
4355 $(CC) -o $@ $^ $(LDFLAGS)
4456
57+ # Compile .c → .o
58+ ifeq ($(OS ) ,Windows_NT)
59+ $(OBJ_DIR ) /% .o : $(SRC_DIR ) /% .c
60+ @if not exist " $( subst /,\, $( dir $@ ) ) " mkdir " $( subst /,\, $( dir $@ ) ) "
61+ $(CC ) $(CFLAGS ) $(INCLUDES ) -c $< -o $@
62+ else
4563$(OBJ_DIR ) /% .o : $(SRC_DIR ) /% .c
46- $( call MKDIR, $(dir $@ ) )
64+ @mkdir -p $(dir $@ )
4765 $(CC ) $(CFLAGS ) $(INCLUDES ) -c $< -o $@
66+ endif
4867
68+ # Debug build
4969debug : CFLAGS += -g
5070debug : all
5171
52- # Test targets
53- test : $(BIN )
72+ # ------------------------------------------------------------
73+ # Testing & LLVM Targets
74+ # ------------------------------------------------------------
75+
76+ test : $(BIN )$(EXE )
5477 @echo " Running basic tests..."
55- ./$(BIN ) --help || true # non-zero exit code expected
78+ ifeq ($(OS ) ,Windows_NT)
79+ $(BIN)$(EXE) --help || exit 0
80+ else
81+ ./$(BIN) --help || true
82+ endif
5683
57- # Alias
5884check : test
5985
60- llvm-test : $(BIN )
86+ llvm-test : $(BIN )$( EXE )
6187 @echo " Testing LLVM IR generation..."
62- @echo " fn add(a: int, b: int) -> int { return a + b; }" > test_simple.lx
63- @echo " fn main() -> int { let x: int = 42; let y: int = 24; return add(x, y); }" >> test_simple.lx
88+ @echo fn add(a: int, b: int) -^> int { return a + b; } > test_simple.lx
89+ @echo fn main () -^> int { let x: int = 42; let y: int = 24; return add(x, y); } >> test_simple.lx
90+ ifeq ($(OS ) ,Windows_NT)
91+ $(BIN)$(EXE) test_simple.luma --save
92+ @dir *.bc *.ll 2>nul || echo No LLVM output files generated
93+ @del /q test_simple.lx 2>nul
94+ else
6495 ./$(BIN) test_simple.luma --save
65- @echo " Generated files:"
6696 @ls -la *.bc *.ll 2>/dev/null || echo "No LLVM output files generated"
67- @echo " Cleaning up test file..."
6897 @rm -f test_simple.lx
98+ endif
6999
70- # View generated LLVM IR
71100view-ir : output.ll
72- @echo " === Generated LLVM IR ==="
101+ @echo === Generated LLVM IR ===
102+ ifeq ($(OS ) ,Windows_NT)
103+ @type output.ll
104+ else
73105 @cat output.ll
106+ endif
74107
75- # Run the generated LLVM bitcode with lli (LLVM interpreter)
76108run-llvm : output.bc
77- @echo " Running with LLVM interpreter..."
109+ @echo Running with LLVM interpreter...
78110 lli output.bc
79111
80- # Compile LLVM IR to native executable
81112compile-native : output.ll
82- @echo " Compiling LLVM IR to native executable..."
113+ @echo Compiling LLVM IR to native executable...
83114 llc output.ll -o output.s
84115 gcc output.s -o program
85- @echo " Native executable created: ./program"
116+ @echo Native executable created: ./program
117+
118+ # ------------------------------------------------------------
119+ # Cleanup
120+ # ------------------------------------------------------------
86121
87122clean :
88- $(call RMDIR,$(OBJ_DIR ) )
89- $(call RMDIR,"output")
90- $(call DEL,$(BIN ) )
123+ @echo Cleaning build artifacts...
124+ ifeq ($(OS ) ,Windows_NT)
125+ @if exist "build" rmdir /s /q "build"
126+ @if exist "$(BIN).exe" del /q "$(BIN).exe"
127+ @if exist "$(BIN)" del /q "$(BIN)"
128+ @echo Cleaning LLVM output files...
129+ @if exist "output.bc" del /q "output.bc"
130+ @if exist "output.ll" del /q "output.ll"
131+ @if exist "output.s" del /q "output.s"
132+ @if exist "program.exe" del /q "program.exe"
133+ @if exist "program" del /q "program"
134+ else
135+ @rm -rf $(OBJ_DIR)
136+ @rm -f $(BIN)
91137 @echo "Cleaning LLVM output files..."
92- $(call DEL,output.bc)
93- $(call DEL,output.ll)
94- $(call DEL,output.s)
95- $(call DEL,program)
138+ @rm -f output.bc output.ll output.s program
139+ endif
140+
141+ # ------------------------------------------------------------
142+ # Help
143+ # ------------------------------------------------------------
96144
97- # Help target
98145help :
99- @echo " Available targets:"
100- @echo " all - Build the compiler"
101- @echo " debug - Build with debug symbols"
102- @echo " test - Run basic tests"
103- @echo " llvm-test - Test LLVM IR generation"
104- @echo " view-ir - View generated LLVM IR"
105- @echo " run-llvm - Run generated bitcode with lli"
106- @echo " compile-native - Compile LLVM IR to native executable"
107- @echo " clean - Remove all build artifacts and generated files "
108- @echo " help - Show this help"
146+ @echo Available targets:
147+ @echo all - Build the compiler
148+ @echo debug - Build with debug symbols
149+ @echo test - Run basic tests
150+ @echo llvm-test - Test LLVM IR generation
151+ @echo view-ir - View generated LLVM IR
152+ @echo run-llvm - Run generated bitcode with lli
153+ @echo compile-native - Compile LLVM IR to native executable
154+ @echo clean - Remove all build artifacts
155+ @echo help - Show this help
0 commit comments