Skip to content

Commit 625a61c

Browse files
committed
Fixed up the make file to work on windows!
1 parent 762c799 commit 625a61c

5 files changed

Lines changed: 237 additions & 137 deletions

File tree

Makefile

Lines changed: 113 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,155 @@
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
1521
ifeq ($(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
2229
else
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 =
2937
endif
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
4969
debug: CFLAGS += -g
5070
debug: 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
5884
check: 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
71100
view-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)
76108
run-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
81112
compile-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

87122
clean:
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
98145
help:
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

config.default.mk

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# config.default.mk
22

3-
CC ?= g++
4-
CFLAGS ?= -Wall -Wextra -std=c17 -Wno-unused-variable -O2 -x c # -x c tells g++ to treat files as C code
3+
CC ?= gcc
4+
CFLAGS ?= -Wall -Wextra -std=c17 -Wno-unused-variable -O2
55
LDFLAGS ?=
66
INCLUDES ?= -Isrc
77

@@ -10,20 +10,21 @@ LLVM_CFLAGS := $(shell llvm-config --cflags)
1010
LLVM_LDFLAGS := $(shell llvm-config --ldflags)
1111
LLVM_LIBS := $(shell llvm-config --libs --system-libs all)
1212

13-
# Alternative: specify exactly what you need
14-
# LLVM_LIBS := $(shell llvm-config --libs --system-libs core analysis bitwriter target irreader asmparser mcparser mc bitreader support demangle)
15-
1613
# Add LLVM flags to existing flags
1714
override CFLAGS += $(LLVM_CFLAGS)
18-
override LDFLAGS += $(LLVM_LDFLAGS) $(LLVM_LIBS)
15+
override LDFLAGS += $(LLVM_LDFLAGS) $(LLVM_LIBS) -lstdc++
1916

2017
SRC_DIR = src
2118
OBJ_DIR = build
2219

20+
# Recursive function to find all .c files
2321
define find_c_sources
2422
$(wildcard $(1)/*.c) \
2523
$(foreach d,$(wildcard $(1)/*),$(call find_c_sources,$(d)))
2624
endef
2725

26+
# Find all source files recursively
2827
SRC_FILES := $(call find_c_sources,$(SRC_DIR))
29-
OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC_FILES))
28+
29+
# Generate object file paths
30+
OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC_FILES))

tests/chess_engine/build_chess.mk

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ LUMA = ./../../luma
33
NAME = chess
44

55
MAIN = main.lx
6-
CHESS_SRCS = $(filter-out $(MAIN), $(wildcard $/*.lx))
6+
CHESS_SRCS = $(filter-out $(MAIN), $(wildcard *.lx))
77

88
STD_LIBS = ../../std/string.lx \
99
../../std/terminal.lx \
@@ -12,35 +12,56 @@ STD_LIBS = ../../std/string.lx \
1212

1313
ALL_SRCS = $(MAIN) $(CHESS_SRCS) $(STD_LIBS)
1414

15+
# Detect OS (Windows_NT is predefined on Windows)
16+
ifeq ($(OS),Windows_NT)
17+
EXE_EXT := .exe
18+
RM := del /Q
19+
MKDIR := if not exist build mkdir build
20+
SEP := &
21+
RUN_PREFIX :=
22+
else
23+
EXE_EXT :=
24+
RM := rm -f
25+
MKDIR := mkdir -p build
26+
SEP := ;
27+
RUN_PREFIX := ./
28+
endif
29+
30+
TARGET = $(NAME)$(EXE_EXT)
31+
1532
.PHONY: all
16-
all: $(NAME)
33+
all: $(TARGET)
1734

1835
# Build chess engine
19-
$(NAME): $(ALL_SRCS)
36+
$(TARGET): $(ALL_SRCS)
2037
@echo "Building chess engine..."
2138
$(LUMA) $(MAIN) -name $(NAME) -l $(CHESS_SRCS) $(STD_LIBS)
22-
@echo "Build complete: ./$(NAME)"
39+
@echo "Build complete: $(TARGET)"
2340

2441
.PHONY: run
25-
run: $(NAME)
42+
run: $(TARGET)
2643
@echo "Starting chess engine..."
27-
./$(NAME)
44+
$(RUN_PREFIX)$(TARGET)
2845

2946
.PHONY: valgrind
30-
valgrind: $(NAME)
47+
valgrind: $(TARGET)
48+
ifeq ($(OS),Windows_NT)
49+
@echo "Valgrind not supported on Windows natively."
50+
else
3151
@echo "Running chess engine with valgrind..."
32-
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME)
52+
valgrind --leak-check=full --show-leak-kinds=all ./$(TARGET)
53+
endif
3354

3455
.PHONY: clean
3556
clean:
3657
@echo "Cleaning build artifacts..."
37-
rm -f $(NAME)
38-
rm -f obj/*.o
58+
-$(RM) $(TARGET)
59+
-$(RM) obj\*.o 2>nul || true
60+
-$(RM) obj/*.o 2>/dev/null || true
3961

4062
.PHONY: rebuild
4163
rebuild: clean all
4264

43-
# Show what files will be compiled
4465
.PHONY: list
4566
list:
4667
@echo "Main file:"
@@ -52,7 +73,6 @@ list:
5273
@echo "Standard library:"
5374
@for lib in $(STD_LIBS); do echo " $$lib"; done
5475

55-
# Help target
5676
.PHONY: help
5777
help:
5878
@echo "Luma Chess Engine Build System"
@@ -63,13 +83,8 @@ help:
6383
@echo ""
6484
@echo "Run Targets:"
6585
@echo " run - Build and run"
66-
@echo " valgrind - Run with memory leak detection"
86+
@echo " valgrind - Run with memory leak detection (Linux only)"
6787
@echo ""
6888
@echo "Development:"
6989
@echo " list - Show all source files"
7090
@echo " clean - Remove build artifacts"
71-
@echo ""
72-
@echo "Examples:"
73-
@echo " make # Build"
74-
@echo " make run # Build and play"
75-
@echo " make list # Show what will be compiled"

0 commit comments

Comments
 (0)