Makefile 產生器
為 C、C++、Python、Node.js、Go 或 Rust 產生含 build、test、run、clean 目標的 GNU Makefile。
makefile-generator本機處理
正在啟用本機工具…
設定
直接編譯來源目錄中的 *.c 檔,檔名限英數、句點、底線及連字號。test 目標使用專案的 test.sh。 GNU Make
結果
Makefile
# demo
CFLAGS ?= -O2 -Wall -Wextra
SRC_DIR := src
BUILD_DIR := build
OBJECT_DIR := $(BUILD_DIR)/.objects
TARGET := $(BUILD_DIR)/demo
SOURCES := $(shell export LC_ALL=C; for file in "$(SRC_DIR)"/*.c; do if [ -f "$$file" ]; then case "$$file" in (*[!A-Za-z0-9_./-]*) printf "%s\n" @invalid-source@ ;; (*) printf "%s\n" "$$file" ;; esac; fi; done)
ifneq ($(filter @invalid-source@,$(SOURCES)),)
$(error Source filenames must use letters, digits, dots, underscores and hyphens)
endif
OBJECTS := $(patsubst $(SRC_DIR)/%.c,$(OBJECT_DIR)/%.o,$(SOURCES))
UNSAFE_BUILD_PATH := $(shell for path in "$(BUILD_DIR)" "$(TARGET)" "$(OBJECT_DIR)" $(OBJECTS) $(OBJECTS:.o=.d); do if [ -L "$$path" ]; then printf yes; exit; fi; done; if [ -d "$(SRC_DIR)" ]; then src=$$(CDPATH= cd -P "$(SRC_DIR)" && pwd -P); root=$$(pwd -P); case "$$src/" in ("$$root/$(BUILD_DIR)/"*) printf yes ;; esac; fi)
ifneq ($(UNSAFE_BUILD_PATH),)
$(error Use non-symlink build paths and keep the physical source directory outside build)
endif
all: build
build: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o "$@"
$(OBJECT_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p "$(@D)"
$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -c "$<" -o "$@"
run: build
"./$(TARGET)"
test:
sh ./test.sh
clean:
rm -f "$(TARGET)" $(OBJECTS) $(OBJECTS:.o=.d)
-include $(OBJECTS:.o=.d)
.PHONY: all build run test clean
如何使用這個工具
為 C、C++、Python、Node.js、Go 或 Rust 產生含 build、test、run、clean 目標的 GNU Makefile。
不適合用在什麼情況
使用不含空白的專案相對路徑。使用 Makefile 前請檢查目標專案的工具鏈與測試指令。