Skip to content
Open
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
46 changes: 32 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ DEPS_DIR = deps
BUILD_DIR = build
TEST_DIR = tests

# Installation directories
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
DESTDIR ?=

# Compiler and Flags
CC = $(DEPS_DIR)/cosmocc/bin/cosmocc
CXX = $(DEPS_DIR)/cosmocc/bin/cosmoc++
Expand Down Expand Up @@ -74,7 +79,7 @@ TEST_TIKTOKEN_OBJ = $(patsubst %.c, $(BUILD_DIR)/test_%.o, $(TEST_TIKTOKEN_SRCS)
TIKTOKEN_TEST_DEPS = $(BUILD_DIR)/tiktoken.o $(BUILD_DIR)/stats.o $(BUILD_DIR)/tiktoken_cpp.o


.PHONY: all clean super_clean deps test help
.PHONY: all clean super_clean deps test install help

# Main build target depends on the final binary
all: $(BUILD_DIR)/dirdoc
Expand Down Expand Up @@ -186,46 +191,46 @@ $(DEPS_DIR)/$(COSMO_ZIP): ensure_dirs
fi

# Compile each source file into an object file.
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c deps
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | deps
$(CC) $(CFLAGS) -c $< -o $@

# Compile main C++ files, ensuring tiktoken_cpp.o depends on the generated header
$(BUILD_DIR)/tiktoken_cpp.o: $(SRC_DIR)/tiktoken_cpp.cpp $(TIKTOKEN_GENERATED_HEADER) deps
$(BUILD_DIR)/tiktoken_cpp.o: $(SRC_DIR)/tiktoken_cpp.cpp $(TIKTOKEN_GENERATED_HEADER) | deps
$(CXX) $(CXXFLAGS) -c $(SRC_DIR)/tiktoken_cpp.cpp -o $@

# Compile test source files into object files
$(BUILD_DIR)/test_%.o: $(TEST_DIR)/%.c deps
$(BUILD_DIR)/test_%.o: $(TEST_DIR)/%.c | deps
$(CC) $(CFLAGS) $(TEST_CFLAGS) -c $< -o $@

# Compile file deletion test separately
$(BUILD_DIR)/test_test_file_deletion.o: $(TEST_DIR)/test_file_deletion.c deps
$(BUILD_DIR)/test_test_file_deletion.o: $(TEST_DIR)/test_file_deletion.c | deps
$(CC) $(CFLAGS) $(TEST_CFLAGS) -c $< -o $@

# Compile file deletion test for standalone use
$(BUILD_DIR)/test_test_file_deletion_standalone.o: $(TEST_DIR)/test_file_deletion.c deps
$(BUILD_DIR)/test_test_file_deletion_standalone.o: $(TEST_DIR)/test_file_deletion.c | deps
$(CC) $(CFLAGS) $(TEST_CFLAGS) -DFILE_DELETION_STANDALONE -c $< -o $@

# Link all object files together for the main executable, ensuring dirdoc.o is last.
# Make sure the generated header exists before linking.
$(BUILD_DIR)/dirdoc: $(DIRDOC_LINK_OBJS) $(DIRDOC_OBJ) deps
$(BUILD_DIR)/dirdoc: $(DIRDOC_LINK_OBJS) $(DIRDOC_OBJ) | deps
@echo "⏳ Linking dirdoc..."
$(CXX) $(LDFLAGS) -o $@ $(DIRDOC_LINK_OBJS) $(DIRDOC_OBJ)
@echo "✅ Build complete"
@echo "✅ Build complete"

# Test-specific version of dirdoc.o that gets compiled with the UNIT_TEST define
$(BUILD_DIR)/dirdoc_test.o: $(SRC_DIR)/dirdoc.c deps
$(BUILD_DIR)/dirdoc_test.o: $(SRC_DIR)/dirdoc.c | deps
$(CC) $(CFLAGS) $(TEST_CFLAGS) -c $< -o $@

# Link test objects and application objects for the main test executable - using test-specific dirdoc_test.o
$(BUILD_DIR)/dirdoc_test: $(filter-out $(BUILD_DIR)/dirdoc.o, $(OBJECTS)) $(BUILD_DIR)/dirdoc_test.o $(MAIN_CPP_OBJECTS) $(TEST_OBJECTS) $(TIKTOKEN_GENERATED_HEADER) | deps
@echo "⏳ Linking test executable..."
$(CXX) $(LDFLAGS) -o $@ $(filter-out $(TIKTOKEN_GENERATED_HEADER), $(filter-out deps, $^))
$(BUILD_DIR)/dirdoc_test: $(filter-out $(BUILD_DIR)/dirdoc.o, $(OBJECTS)) $(BUILD_DIR)/dirdoc_test.o $(MAIN_CPP_OBJECTS) $(TEST_OBJECTS) $(TIKTOKEN_GENERATED_HEADER) | deps
@echo "⏳ Linking test executable..."
$(CXX) $(LDFLAGS) -o $@ $(filter-out $(TIKTOKEN_GENERATED_HEADER), $(filter-out deps, $^))
@echo "✅ Test link complete"

# Build file deletion test executable
$(BUILD_DIR)/test_file_deletion: $(BUILD_DIR)/test_test_file_deletion_standalone.o $(filter-out $(BUILD_DIR)/dirdoc.o, $(OBJECTS)) $(BUILD_DIR)/dirdoc_test.o $(MAIN_CPP_OBJECTS)
$(BUILD_DIR)/test_file_deletion: $(BUILD_DIR)/test_test_file_deletion_standalone.o $(filter-out $(BUILD_DIR)/dirdoc.o, $(OBJECTS)) $(BUILD_DIR)/dirdoc_test.o $(MAIN_CPP_OBJECTS) | deps
@echo "⏳ Linking file deletion test executable..."
$(CXX) $(LDFLAGS) -o $@ $(filter-out $(TIKTOKEN_GENERATED_HEADER), $(filter-out deps, $^))
$(CXX) $(LDFLAGS) -o $@ $(filter-out $(TIKTOKEN_GENERATED_HEADER), $(filter-out deps, $^))
@echo "✅ File deletion test link complete"

# Build and run the main tests - don't force 'all' to run, but ensure dependencies are available
Expand All @@ -238,6 +243,18 @@ test_file_deletion: deps $(BUILD_DIR)/test_file_deletion
@echo "🚀 Running file deletion tests..."
./$(BUILD_DIR)/test_file_deletion

# Install the dirdoc binary
install: $(BUILD_DIR)/dirdoc
@echo "⏳ Installing dirdoc to $(DESTDIR)$(BINDIR)..."
@mkdir -p $(DESTDIR)$(BINDIR)
@if [ ! -w "$(DESTDIR)$(BINDIR)" ]; then \
echo "❌ No write permission to $(DESTDIR)$(BINDIR)"; \
echo " Use 'sudo make install' or set PREFIX to a writable directory"; \
exit 1; \
fi
install -m 755 $(BUILD_DIR)/dirdoc $(DESTDIR)$(BINDIR)/dirdoc
@echo "✅ dirdoc installed to $(DESTDIR)$(BINDIR)/dirdoc"

clean:
@echo "⏳ Cleaning build artifacts..."
rm -rf $(BUILD_DIR)
Expand All @@ -255,4 +272,5 @@ help:
@echo " test - Build and run the test suite"
@echo " clean - Remove build artifacts (tools and generated files in build dir)"
@echo " super_clean - Remove build artifacts and dependencies (complete cleanup)"
@echo " install - Install dirdoc to $(PREFIX)/bin"
@echo " help - Show this help message"
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,21 @@ To conveniently run `dirdoc` from any folder on your system, choose one of the f
#### On Linux and macOS

- **Option 1:** Copy the compiled binary to a directory in your PATH (e.g., `/usr/local/bin`).
Ensure you have either run `make` (which places the binary in `build/dirdoc`) or downloaded the
release file and are currently in the folder that contains the `dirdoc` executable:
Or, if you've cloned the repository, simply run (you may need `sudo`):
sudo make install
```
To install without root privileges, specify a writable PREFIX, e.g.:
```bash
make install PREFIX="$HOME/.local"
```bash
sudo cp build/dirdoc /usr/local/bin/dirdoc # adjust the path if your binary is elsewhere
```
If you encounter a "Permission denied" error, rerun with `sudo` or choose a
PREFIX you can write to.
Or, if you've cloned the repository, simply run:
```bash
make install
```
- **Option 2:** Add the folder containing `dirdoc` to your PATH. For example, if you place it in `~/dirdoc`, add the following to your `~/.bashrc` or `~/.bash_profile`:
```bash
export PATH="$PATH:$HOME/dirdoc"
Expand Down
Loading