<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alvin Abad</title>
    <description>The latest articles on DEV Community by Alvin Abad (@alvinabad).</description>
    <link>https://dev.to/alvinabad</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1052156%2F6fd8a57c-df30-4961-a2fd-0670072c6f58.jpeg</url>
      <title>DEV Community: Alvin Abad</title>
      <link>https://dev.to/alvinabad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alvinabad"/>
    <language>en</language>
    <item>
      <title>I Love GNU Make</title>
      <dc:creator>Alvin Abad</dc:creator>
      <pubDate>Sat, 25 Mar 2023 14:29:18 +0000</pubDate>
      <link>https://dev.to/alvinabad/i-love-gnu-make-493a</link>
      <guid>https://dev.to/alvinabad/i-love-gnu-make-493a</guid>
      <description>&lt;p&gt;I love GNU Make and Makefiles. It is designed for compiling C/C++ programs but I use it for almost anything that needs to be scripted as long it makes sense. Like any tool, you use it when it's best suited for the job. My rule of thumb is if it can be done using GNU Make, I would try it first before using a more powerful scripting language like bash or Python. What makes it preferable over bash or Python, assuming it can accomplish the task at hand, is simplicity and readability of the code. That's mostly all about it. Of course, there are things, many things, it cannot do compared to Python or bash scripting. But I only switch to them if GNU Make starts becoming complicated.&lt;/p&gt;

&lt;p&gt;I use GNU Make to build Golang programs. Below is a sample Makefile to build a Golang project.&lt;/p&gt;

&lt;h4&gt;
  
  
  Makefile
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PROG        := helloworld
GOBIN       := $(CURDIR)/bin
PATH        := $(PATH):$(HOME)/go/bin

ifeq ($(DEBUG),true)
    DEBUG_OPT = -gcflags=all="-N -l"
endif

.PHONY: all \
        help \
        lint \
        gosec \
        fmt \
        gomodtidy \
        vet \
        build \
        install \
        test \
        clean

help:
    @echo "Usage:"
    @echo "    make clean all"
    @echo "    make clean build"
    @echo "    make DEBUG=true clean all"
    @echo "    make vet"
    @echo "    make lint"
    @echo "    make gosec"
    @echo "    make test"
    $(info GOBIN=$(GOBIN))

all: link gosec vet test install

build: gomodtidy vet
    go build -o $(PROG) cmd/main.go

install: gomodtidy vet
    @GOBIN="$(GOBIN)" go install $(DEBUG_OPT) ./...

lint:
    @PATH=$(PATH) golint ./...

gosec: gomodtidy
    @PATH=$(PATH) gosec ./...

fmt:
    go fmt ./...

vet: gomodtidy
    go vet ./...
    golangci-lint run

gomodtidy:
    @go mod tidy

test: gomodtidy
    @go test -bench= ./...

clean:
    @$(RM) -rf "$(GOBIN)"
    @$(RM) -f *.o
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, it's just a bunch of tasks that you want to run against your code. If you want to build the Golang code and produce the executable binary, you simply run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;make clean build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to run test,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;make test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to run a linter against your code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;make lint
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to check for security issues,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;make gosec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All these things can be accomplished using shell scripting. But I think GNU Make is more readable in this scenario. It's easy to spot the targets, which are the tasks intended for the project and how they can be executed. I can easily determine where the main program (package main) is located. I can easily see the name of the executable file and where it will be saved.&lt;/p&gt;

&lt;p&gt;If your build script is readable, then it becomes self-documenting. You save yourself of writing a separate documentation, like how to install or build the project.&lt;/p&gt;

&lt;p&gt;All my projects use Makefiles. By just looking at the Makefile, I can quickly see how the project is built and other tasks associated with it. To me, the quickest way to learn and understand a code base is if you can build and run it.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
