casey / just

🤖 Just a command runner
https://just.systems
Creative Commons Zero v1.0 Universal
17.59k stars 399 forks source link

How to replicate Makefile's wildcard and patsubst #1993

Closed drskoolie closed 1 month ago

drskoolie commented 1 month ago

When compiling C code, you need to convert every .c to a .o. Unfortunately, gcc isn't smart enough to do that by itself. So you need to employ a make file that does it.

The code below is a simple example of using Makefile's wildcard and patsubst functions to do it.

The wildcard is easy, it's just globbing (src/*.c). The patsubst is the harder part for me. It replaces every string that it got from the wildcard to look like this ('obj/*.o').

After that, it runs gcc per each individual string that it got out. I've been looking through the docs of just, but I couldn't get it working.

# Define source directory
SRC_DIR := src

# Define object directory
OBJ_DIR := obj

# Define source files using wildcard function
SRC_FILES := $(wildcard $(SRC_DIR)/*.c)

# Define object files using patsubst function
OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC_FILES))

# Compiler
CC := gcc

# Compiler flags
CFLAGS := -Wall -Wextra -Iinclude

# Target executable
TARGET := my_program

# Default rule to build the executable
$(TARGET): $(OBJ_FILES)
    $(CC) $^ -o $@

# Rule to compile source files into object files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
    $(CC) $(CFLAGS) -c $< -o $@

# Clean rule
clean:
    rm -f $(OBJ_DIR)/*.o $(TARGET)
laniakea64 commented 1 month ago

For the patsubst, does this do what you're looking for? -

# Define source directory
SRC_DIR := 'src'

# Define object directory
OBJ_DIR := 'obj'

# Define source files using wildcard
SRC_FILES := `echo src/*.c`

# Define object files
OBJ_FILES := replace_regex(SRC_FILES, SRC_DIR / '([^/]+)\.c(\s|$)', OBJ_DIR / '${1}.o$2')
drskoolie commented 1 month ago

Yes, that's what I was looking for! Thanks. How do I know where the replace_regex function is in the docs @laniakea64?

laniakea64 commented 1 month ago

How do I know where the replace_regex function is in the docs

It's on the same page as other just functions are documented: https://just.systems/man/en/chapter_31.html#string-manipulation