Create nice dependency of tasks - one liners#

In previous example (Create nice dependency of tasks) we have shown how to create nice dependency of tasks in Makefile. However presented syntax can be not easy to read for some people and when dependencies will grow, it will be hard to maintain.

In this example we will show how to create one liners in Makefile and how to create dependencies which are easier to read.

Example scenario#

Lets reuse scenario from previous Example scenario.

Code#

Following example shows example of one liners in Makefile. By meaning one liners we are saying that we are defining dependencies and task in one line. However definition of dependencies are separate lines easy to read.

Code:

# ============================================================================
# Simple example of power from Makefile.
#
# Makefile can be written as one liners - easier to parse and proceed
# ============================================================================

# == Steps with dependencies ==
conf_sec_page_deps := conf_dns
conf_sec_page_deps += conf_web_srv
conf_sec_page_deps += conf_web_ssl
conf_sec_page: $(conf_sec_page_deps); @echo "STEP: Configure Secure Page"

conf_dns_deps := have_srv 
conf_dns_deps += inst_bind
conf_dns: $(conf_dns_deps); @echo "STEP: Configure DNS zones"

conf_web_srv_deps := inst_nginx
conf_web_srv: $(conf_web_srv_deps); @echo "STEP: Configure page in WEB server"

conf_web_ssl_deps := data_csr
conf_web_ssl_deps += conf_web_srv
conf_web_ssl: $(conf_web_ssl_deps); @echo "STEP: Configure CSR details (auto by LetsEncrypt)"

inst_nginx_deps := have_srv
inst_nginx: $(inst_nginx_deps); @echo "STEP: Install and start NGINX"

data_csr_deps := inst_nginx
data_csr: $(data_csr_deps); @echo "STEP: Use LestEncrypt"

# == Steps without dependencies ==
have_srv:; @echo "STEP: Setup server"
inst_bind:; @echo "STEP: Install and start BIND"

Result#

This section shows result when make is running without any arguments.

STEP: Setup server
STEP: Install and start BIND
STEP: Configure DNS zones
STEP: Install and start NGINX
STEP: Configure page in WEB server
STEP: Use LestEncrypt
STEP: Configure CSR details (auto by LetsEncrypt)
STEP: Configure Secure Page

Summary#

In this example we have shown how to create one liners in Makefile. By using one liners we can create dependencies which are easier to read and maintain.