Giter Club home page Giter Club logo

makefile's Introduction

Makefile

The makefile I use for all of my C projects

Features

  • Help menu with make help
  • Debug and release build modes
  • Configurable variables
  • Automatic and recursive dependency resolution based on #includes (yes, you can embed source directories)
  • clean subcommand for fast binary removal
  • run subcommand for simple testing
  • all subcommand for quick and easy rebuilding
  • -r flag for building in release mode without editing the config (as per requested)

Source

# C makefile by Samuel B. Foster (github.com/smlbfstr)

# Config
compiler = gcc
debug_compile_flags = -Wall -O2 -g
debug_link_flags = 
release_compile_flags = -Wall -O2
release_link_flags = 
debug = true

build_directory = bin
source_directory = src

# Automatic variables (DO NOT TOUCH ANYTHING BELOW THIS POINT)
target = $(build_directory)/$(notdir $(shell pwd))
sources = $(shell find $(source_directory) -name '*.c')
objects = $(sources:%=$(build_directory)/%.o)
dependencies = $(objects:.o=.d)
include_directories = $(shell find $(source_directory) -type d)
prefix = [\x1b[33mmake\x1b[0m]
compile_flags =
link_flags =

ifeq ($(findstring r,$(firstword -$(MAKEFLAGS))),r)
	debug = false
endif

ifeq ($(debug), true)
	compile_flags += $(debug_compile_flags)
	link_flags += $(debug_link_flags)
else
	compile_flags += $(release_compile_flags)
	link_flags += $(release_link_flags)
endif

# Build rules
$(target): $(objects)
	@printf "$(prefix) \x1b[35mLinking objects\x1b[0m\n"
	@$(compiler) $(objects) -o $@ $(link_flags)

$(build_directory)/%.c.o: %.c
	@mkdir -p $(dir $@)
	@printf "$(prefix) \x1b[32mCompiling\x1b[0m %s\n" $<
	@$(compiler) $(compile_flags) $(addprefix -I,$(include_directories)) -MMD -MP $(CFLAGS) -c $< -o $@
	@chmod go-w $@

-include $(dependencies)

# Subcommands
.PHONY: clean run all

clean:
ifeq ($(wildcard $(build_directory)/.),)
	@printf "$(prefix) \x1b[31mNothing to clean\x1b[0m\n"
else
	@printf "$(prefix) \x1b[31mCleaning binaries\x1b[0m\n"
	@rm -r $(build_directory)
endif

run:
	@printf "$(prefix) \x1b[34mRunning\x1b[0m %s\n" $(notdir $(target))
	@./$(target)

all: clean $(target) run

help:
	@printf "┌─────────────────────────────────────────┐\n"
	@printf "│         \x1b[1mC Makefile by smlbfstr\x1b[0m          │\n"
	@printf "├─────────────┬───────────────────────────┤\n"
	@printf "│ \x1b[32mmake\x1b[0m        │ Compiles your code        │\n"
	@printf "├─────────────┼───────────────────────────┤\n"
	@printf "│ \x1b[2;35mmake\x1b[0;35m -r ...\x1b[0m │ Enables release mode      │\n"
	@printf "├─────────────┼───────────────────────────┤\n"
	@printf "│ \x1b[2;31mmake\x1b[0;31m clean\x1b[0m  │ Removes build directory   │\n"
	@printf "├─────────────┼───────────────────────────┤\n"
	@printf "│ \x1b[2;34mmake\x1b[0;34m run\x1b[0m    │ Runs the compiled binary  │\n"
	@printf "├─────────────┼───────────────────────────┤\n"
	@printf "│ \x1b[2;33mmake\x1b[0;33m all\x1b[0m    │ Does everything           │\n"
	@printf "└─────────────┴───────────────────────────┘\n"

makefile's People

Contributors

smlbfstr avatar

Stargazers

Aaron Sprouse avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.