Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include ./scripts/test.mk
include ./scripts/proto.mk
include ./scripts/utils.mk
include ./scripts/run.mk
include ./tools/tools.mk

# Sets the default make target to `build`.
# Requires GNU Make >= v3.81.
Expand Down
90 changes: 90 additions & 0 deletions tools/tools.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# tools.mk - Build configuration for ev-node tools

# Tool names
TOOLS := da-debug blob-decoder cache-analyzer

# Build directory
TOOLS_BUILD_DIR := $(CURDIR)/build

# Version information (inherited from main build.mk if available)
VERSION ?= $(shell git describe --tags --abbrev=0 2>/dev/null || echo "dev")
GITSHA ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
LDFLAGS ?= \
-X main.Version=$(VERSION) \
-X main.GitSHA=$(GITSHA)

# Individual tool build targets
## build-tool-da-debug: Build da-debug tool
build-tool-da-debug:
@echo "--> Building da-debug tool"
@mkdir -p $(TOOLS_BUILD_DIR)
@cd tools/da-debug && go build -ldflags "$(LDFLAGS)" -o $(TOOLS_BUILD_DIR)/da-debug .
@echo "--> da-debug built: $(TOOLS_BUILD_DIR)/da-debug"
.PHONY: build-tool-da-debug

## build-tool-blob-decoder: Build blob-decoder tool
build-tool-blob-decoder:
@echo "--> Building blob-decoder tool"
@mkdir -p $(TOOLS_BUILD_DIR)
@cd tools/blob-decoder && go build -ldflags "$(LDFLAGS)" -o $(TOOLS_BUILD_DIR)/blob-decoder .
@echo "--> blob-decoder built: $(TOOLS_BUILD_DIR)/blob-decoder"
.PHONY: build-tool-blob-decoder

## build-tool-cache-analyzer: Build cache-analyzer tool
build-tool-cache-analyzer:
@echo "--> Building cache-analyzer tool"
@mkdir -p $(TOOLS_BUILD_DIR)
@cd tools/cache-analyzer && go build -ldflags "$(LDFLAGS)" -o $(TOOLS_BUILD_DIR)/cache-analyzer .
@echo "--> cache-analyzer built: $(TOOLS_BUILD_DIR)/cache-analyzer"
.PHONY: build-tool-cache-analyzer

# Build all tools
## build-tools: Build all tools
build-tools: $(addprefix build-tool-, $(TOOLS))
@echo "--> All tools built successfully!"
.PHONY: build-tools

# Install individual tools
## install-tool-da-debug: Install da-debug tool to Go bin
install-tool-da-debug:
@echo "--> Installing da-debug tool"
@cd tools/da-debug && go install -ldflags "$(LDFLAGS)" .
@echo "--> da-debug installed to Go bin"
.PHONY: install-tool-da-debug

## install-tool-blob-decoder: Install blob-decoder tool to Go bin
install-tool-blob-decoder:
@echo "--> Installing blob-decoder tool"
@cd tools/blob-decoder && go install -ldflags "$(LDFLAGS)" .
@echo "--> blob-decoder installed to Go bin"
.PHONY: install-tool-blob-decoder

## install-tool-cache-analyzer: Install cache-analyzer tool to Go bin
install-tool-cache-analyzer:
@echo "--> Installing cache-analyzer tool"
@cd tools/cache-analyzer && go install -ldflags "$(LDFLAGS)" .
@echo "--> cache-analyzer installed to Go bin"
.PHONY: install-tool-cache-analyzer

# Install all tools
## install-tools: Install all tools to Go bin
install-tools: $(addprefix install-tool-, $(TOOLS))
@echo "--> All tools installed successfully!"
.PHONY: install-tools
Comment on lines +12 to +73
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This new Makefile for tools has a couple of areas for improvement regarding maintainability and correctness.

  1. Code Duplication: The build-tool-* and install-tool-* targets are highly repetitive. This makes the file verbose and harder to maintain. Adding a new tool requires duplicating code blocks, which is error-prone.
  2. Incorrect Linker Flags: The LDFLAGS variable is defined with ?= and is being overridden by a definition in scripts/build.mk meant for a different application. This means the version information is not being correctly embedded into the tool binaries. Furthermore, the tool's main packages are missing the Version and GitSHA variables that the linker flags are trying to set.

I suggest a refactoring that addresses both issues. This involves:

  • Using define and foreach to generate the build/install targets from a template, adhering to the DRY (Don't Repeat Yourself) principle.
  • Using a dedicated variable TOOLS_LDFLAGS for the tools' linker flags to avoid conflicts.

Here is a suggested replacement that incorporates these improvements. Please note that you will still need to add the Version and GitSHA variables to the main package of each Go tool for the version injection to work.

TOOLS_LDFLAGS := \
	-X main.Version=$(VERSION) \
	-X main.GitSHA=$(GITSHA)

# --- Template for building a tool ---
define BUILD_TOOL_template
## build-tool-$(1): Build $(1) tool
build-tool-$(1):
	@echo "--> Building $(1) tool"
	@mkdir -p $$(TOOLS_BUILD_DIR)
	@cd tools/$(1) && go build -ldflags "$$(TOOLS_LDFLAGS)" -o $$(TOOLS_BUILD_DIR)/$(1) .
	@echo "--> $(1) built: $$(TOOLS_BUILD_DIR)/$(1)"
.PHONY: build-tool-$(1)
endef

# --- Template for installing a tool ---
define INSTALL_TOOL_template
## install-tool-$(1): Install $(1) tool to Go bin
install-tool-$(1):
	@echo "--> Installing $(1) tool"
	@cd tools/$(1) && go install -ldflags "$$(TOOLS_LDFLAGS)" .
	@echo "--> $(1) installed to Go bin"
.PHONY: install-tool-$(1)
endef

# --- Generate targets from templates ---
$(foreach tool,$(TOOLS),$(eval $(call BUILD_TOOL_template,$(tool))))
$(foreach tool,$(TOOLS),$(eval $(call INSTALL_TOOL_template,$(tool))))

# Build all tools
## build-tools: Build all tools
build-tools: $(addprefix build-tool-, $(TOOLS))
	@echo "--> All tools built successfully!"
.PHONY: build-tools

# Install all tools
## install-tools: Install all tools to Go bin
install-tools: $(addprefix install-tool-, $(TOOLS))
	@echo "--> All tools installed successfully!"
.PHONY: install-tools


# Clean built tools
## clean-tools: Remove built tool binaries
clean-tools:
@echo "--> Cleaning built tools"
@rm -f $(addprefix $(TOOLS_BUILD_DIR)/, $(TOOLS))
@echo "--> Tools cleaned"
.PHONY: clean-tools

# List available tools
## list-tools: List all available tools
list-tools:
@echo "Available tools:"
@for tool in $(TOOLS); do \
echo " - $$tool"; \
done
.PHONY: list-tools
Loading