|
| 1 | +#*************************************************************************** |
| 2 | +#* Autoversion makefile v.20170112.001402 (euler) * |
| 3 | +#* Copyright (C) 2014-2017 by Ruben Carlo Benante <rcb@beco.cc> * |
| 4 | +#* * |
| 5 | +#* This makefile sets BUILD and allows to set MAJOR.MINOR version, * |
| 6 | +#* DEBUG and OBJ to compile a range of different targets * |
| 7 | +#*************************************************************************** |
| 8 | +#* This program is free software; you can redistribute it and/or modify * |
| 9 | +#* it under the terms of the GNU General Public License as published by * |
| 10 | +#* the Free Software Foundation; version 2 of the License. * |
| 11 | +#* * |
| 12 | +#* This program is distributed in the hope that it will be useful, * |
| 13 | +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 | +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 | +#* GNU General Public License for more details. * |
| 16 | +#* * |
| 17 | +#* You should have received a copy of the GNU General Public License * |
| 18 | +#* along with this program; if not, write to the * |
| 19 | +#* Free Software Foundation, Inc., * |
| 20 | +#* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 21 | +#*************************************************************************** |
| 22 | +#* To contact the author, please write to: * |
| 23 | +#* Ruben Carlo Benante * |
| 24 | +#* Email: rcb@beco.cc * |
| 25 | +#* Webpage: http://drbeco.github.io/ * |
| 26 | +#*************************************************************************** |
| 27 | +# |
| 28 | +# Usage: |
| 29 | +# |
| 30 | +#* Options: |
| 31 | +#- DEBUG=[0|1] turn on/off debug MACRO |
| 32 | +#- Version is MAJOR.MINOR.DATE.TIME |
| 33 | +#+ MAJOR=N makes this major version N |
| 34 | +#+ MINOR=N makes this minor version N |
| 35 | +#- OBJ=someobjetc.o : compile and link with objetc or source |
| 36 | +#- You can force update with flag -B |
| 37 | +# |
| 38 | +# Examples: |
| 39 | +# |
| 40 | +# * From linux prompt: |
| 41 | +# - Normal C program (ex1.c) |
| 42 | +# $ make ex1.x |
| 43 | +# - Aspipo program (ex1.c) with object: |
| 44 | +# $ make ex1.x OBJ=libaspipo-ux64.o |
| 45 | +# - Multiple objects: |
| 46 | +# $ make ex1.x OBJ="mylib1.o mylib2.o" |
| 47 | +# - Multiple sources to generate a single binary ex1.x: |
| 48 | +# $ make ex1.x SRC="mysrc1.c mysrc2.c" |
| 49 | +# - Normal C program (ex2.c) version 2.3, no debug |
| 50 | +# $ make ex2.x MAJOR=2 MINOR=3 DEBUG=0 |
| 51 | +# - Brainforce: |
| 52 | +# $ make ex1.bf.x |
| 53 | +# - Portugol: |
| 54 | +# $ make ex1.gpt.x |
| 55 | +# |
| 56 | +# * Directly from vim editor command line: |
| 57 | +# - Normal C program (ex1.c) |
| 58 | +# :make ex1.x |
| 59 | +# - Aspipo program (ex1.c) |
| 60 | +# :make ex1.x OBJ=libaspipo-ux64.o |
| 61 | +# |
| 62 | +# * Copy from 'rascunhos' to 'trabalhos' |
| 63 | +# - $ make copy PRG=ex1 |
| 64 | +# It will try to copy every file matching patterns: |
| 65 | +#cp ex1.c # c source code |
| 66 | +#cp ex1.h # c library source code |
| 67 | +# cp ex1.x # binary from c source code |
| 68 | +# cp ex1.gpt # portugol source code |
| 69 | +# cp ex1.gpt.x # binary from portugol source code |
| 70 | +# cp ex1.bf # brainforce source code |
| 71 | +# cp ex1.bf.x # binary from brainforce source code |
| 72 | +# cp ex1.cpl.x # binary from c code with some prolog predicates linked to it |
| 73 | +# cp ex1.pl # prolog source code |
| 74 | +# cp ex1.pl.x # binary from prolog source code |
| 75 | +# cp ex1.so # shared library object from c source code |
| 76 | +# cp ex1.pl.so # c library object with some functions that may be called by a prolog program |
| 77 | +# to the '../trabalhos' folder |
| 78 | +# |
| 79 | +# * CTAGS |
| 80 | +# - Generate a 'tags' index file with all tags in all C source codes |
| 81 | +# $ make tags |
| 82 | +# - Use with vi: |
| 83 | +# :make tags |
| 84 | +# - To find a function or other tag, use $ vim -t tag |
| 85 | +# - Inside vi, use :ta tag |
| 86 | +# |
| 87 | +# |
| 88 | +# * clean |
| 89 | +# Clean temporary files *.o and errors.err |
| 90 | +# $ make clean |
| 91 | +# |
| 92 | +# * wipe |
| 93 | +#- WARNING!! Clean all created files: *.x *.so *.o errors.err and tags |
| 94 | +# $ make wipe |
| 95 | +# |
| 96 | +# Log: |
| 97 | +# 2017-03-03: |
| 98 | +# * added -fdiagnostics-color=always to colorize tee output |
| 99 | +# * added -pg -fprofile-arcs to allow gprof command on debug |
| 100 | +# 2017-03-05: |
| 101 | +# * added variable CCCOLOR to work with vi |
| 102 | +# - set makeprg=make\ o=%< CCCOLOR=never\ $* |
| 103 | +# - set makeprg=make\ %<.x CCCOLOR=never\ $* |
| 104 | +# - set makeprg=make\ CCCOLOR=never\ $* |
| 105 | +#* added CPPFLAGS += -D$(D) so we can pass values to |
| 106 | +#C-Pre-processor via make prog.x D=VARIABLE=VALUE |
| 107 | +# 2017-03-06: |
| 108 | +# * ifeq "$(DEBUG)" "1" to set CFLAGS |
| 109 | +# * added _XOPEN_SOURCE=700 to pair with -ansi |
| 110 | +# * added _FORTIFY_SOURCE=1 to help catch overflows |
| 111 | +# 2017-03-18: |
| 112 | +# * added -lcurl to link with libcurl |
| 113 | + |
| 114 | +# disable builtin rules with MAKEFLAGS and .SUFFIXES |
| 115 | +MAKEFLAGS += --no-builtin-rules |
| 116 | +#.SUFFIXES: |
| 117 | +.PHONY: clean wipe nomatch |
| 118 | +.PRECIOUS: %.o |
| 119 | +SHELL=/bin/bash -o pipefail |
| 120 | + |
| 121 | +# asure functions that return values are not ignored |
| 122 | +FORTIFY ?= 1 |
| 123 | +# turn on/off debug mode |
| 124 | +DEBUG ?= 1 |
| 125 | +# version major number |
| 126 | +MAJOR ?= 0 |
| 127 | +# version minor number |
| 128 | +MINOR ?= 1 |
| 129 | +# object files to compile with the source |
| 130 | +OBJ ?= |
| 131 | +# other source files to be compiled together |
| 132 | +SRC ?= |
| 133 | +# colorize output |
| 134 | +CCCOLOR ?= always |
| 135 | +# create a define |
| 136 | +D ?= D_ |
| 137 | +# project Euler exercise number |
| 138 | +N ?= 0 |
| 139 | +PENAME := $(shell printf '%-8s' pe$(N).c) |
| 140 | +# build date |
| 141 | +BUILD = $(shell date +"%g%m%d.%H%M%S") |
| 142 | +# build date inside binary code |
| 143 | +DEFSYM = $(subst .,_,$(BUILD)) |
| 144 | +# automatic version number |
| 145 | +VERSION = "\"$(MAJOR).$(MINOR).$(BUILD)\"" |
| 146 | +# c compiler |
| 147 | +CC = gcc |
| 148 | +# brainforce compiler |
| 149 | +BF = bf |
| 150 | +# gportugol compiler |
| 151 | +PT = gpt |
| 152 | +# prolog linker |
| 153 | +PLLD = swipl-ld |
| 154 | +# prolog compiler |
| 155 | +PL = swipl |
| 156 | +# c flags for the c compiler |
| 157 | +CFLAGS = -Wall -Wextra -std=gnu99 -fdiagnostics-color=$(CCCOLOR) |
| 158 | +#CFLAGS = -Wall -Wextra -g -O0 -std=gnu99 -pg -fprofile-arcs -fdiagnostics-color=$(CCCOLOR) |
| 159 | +ifeq "$(DEBUG)" "0" |
| 160 | +# not a debug, go fast |
| 161 | +CFLAGS += -Ofast |
| 162 | +else ifeq "$(DEBUG)" "1" |
| 163 | +# it is a debug, add symbols and no optimizations |
| 164 | +CFLAGS += -g -O0 |
| 165 | +else |
| 166 | +# exaustive debug |
| 167 | +CFLAGS += -g -O0 -pg -fprofile-arcs -ansi -Wpedantic |
| 168 | +endif |
| 169 | +#-pedantic-errors -Werror |
| 170 | +#-Ofast -c |
| 171 | +# pre-processor flags |
| 172 | +CPPFLAGS = -DVERSION=$(VERSION) -DBUILD="\"$(BUILD)\"" -DDEBUG=$(DEBUG) -D$(D) -D_FORTIFY_SOURCE=$(FORTIFY) |
| 173 | +ifeq "$(DEBUG)" "2" |
| 174 | +# POSIX extra stuff |
| 175 | +CPPFLAGS += -D_XOPEN_SOURCE=700 |
| 176 | +endif |
| 177 | +# libraries to link, options to the linker |
| 178 | +LDLIBS = -Wl,--defsym,BUILD_$(DEFSYM)=0 -lm -lpthread -lncurses -lcurl -lgmp |
| 179 | +# making a shared library |
| 180 | +CCSHARED = -shared -fPIC |
| 181 | +# brainforce options |
| 182 | +BFFLAGS = -i on -p both -r on -w on |
| 183 | +# making a prolog shared library |
| 184 | +PLLDSHARED = -shared |
| 185 | +# making a stand alone prolog program |
| 186 | +PLFLAGS = --goal=main --stand_alone=true |
| 187 | + |
| 188 | +# prevent built-in rules for %.o |
| 189 | +%.o : %.c |
| 190 | + |
| 191 | +# Programa BrainForce. |
| 192 | +%.bf.x : %.bf |
| 193 | +$(BF) $^ -o $@ $(BFFLAGS) 2>&1 | tee errors.err |
| 194 | + |
| 195 | +# Algoritmo em PORTUGOL. |
| 196 | +%.gpt.x : %.gpt |
| 197 | +$(PT) $^ -o $@ 2>&1 | tee errors.err |
| 198 | + |
| 199 | +# Compila um programa em PROLOG para binario individual. |
| 200 | +%.pl.x : %.pl $(SRC) |
| 201 | +$(PL) $(PLFLAGS) -o $@ -c $^ 2>&1 | tee errors.err |
| 202 | + |
| 203 | +# file.pl: regras de PROLOG que podem ser chamada por um programa em C. |
| 204 | +%.cpl.x : %.c %.pl |
| 205 | +$(PLLD) $^ -o $@ 2>&1 | tee errors.err |
| 206 | + |
| 207 | +# file.c: modulo C com funcoes que podem ser chamadas pelo PROLOG. |
| 208 | +%.plc.so : %.c |
| 209 | +$(PLLD) $(PLLDSHARED) $^ -o $@ 2>&1 | tee errors.err |
| 210 | + |
| 211 | +# Shared library |
| 212 | +%.so : %.c $(OBJ) $(SRC) |
| 213 | +-$(CC) $(CCSHARED) $(CFLAGS) $(CPPFLAGS) $(LDLIBS) $^ -o $@ 2>&1 | tee errors.err |
| 214 | +ifeq "$(CCCOLOR)" "always" |
| 215 | +@sed -i -r "s/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g" errors.err |
| 216 | +endif |
| 217 | + |
| 218 | +# Programa em C (incluindo bibliotecas como allegro ou libaspipo). |
| 219 | +# Inclui VERSION, data de BUILD e DEBUG (opcional). |
| 220 | +%.x : %.c $(OBJ) $(SRC) |
| 221 | +-$(CC) $(CFLAGS) $(CPPFLAGS) $(LDLIBS) $^ -o $@ 2>&1 | tee errors.err |
| 222 | +ifeq "$(CCCOLOR)" "always" |
| 223 | +@sed -i -r "s/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g" errors.err |
| 224 | +endif |
| 225 | +-@[ ! -s errors.err ] && echo $@ version $(VERSION) > VERSION |
| 226 | + |
| 227 | +# override built-in rules for mathing everything (exactly the same rule as %.x above) |
| 228 | +% : %.c $(OBJ) $(SRC) |
| 229 | +-$(CC) $(CFLAGS) $(CPPFLAGS) $(LDLIBS) $^ -o $@ 2>&1 | tee errors.err |
| 230 | +@echo $@ version $(VERSION) > VERSION |
| 231 | +ifeq "$(CCCOLOR)" "always" |
| 232 | +@sed -i -r "s/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g" errors.err |
| 233 | +endif |
| 234 | +-@[ ! -s errors.err ] && echo $@ version $(VERSION) > VERSION |
| 235 | + |
| 236 | +nomatch : |
| 237 | +@echo 'makefile error: no rules for the given goal(s)' $(warning nomatch) |
| 238 | + |
| 239 | +# CUIDADO! Apaga tudo que o makefile pode criar. |
| 240 | +wipe : |
| 241 | +rm -f *.x *.so *.o errors.err tags a.out |
| 242 | + |
| 243 | +# Apaga temporarios desnecessarios. |
| 244 | +clean : |
| 245 | +rm -f *.o errors.err |
| 246 | + |
| 247 | +copy : |
| 248 | +-cp $(PRG).c ../trabalhos # c source code |
| 249 | +-cp $(PRG).h ../trabalhos # c library source code |
| 250 | +-cp $(PRG).x ../trabalhos# binary from c source code |
| 251 | +-cp $(PRG).gpt ../trabalhos# portugol source code |
| 252 | +-cp $(PRG).gpt.x ../trabalhos# binary from portugol source code |
| 253 | +-cp $(PRG).bf ../trabalhos# brainforce source code |
| 254 | +-cp $(PRG).bf.x ../trabalhos# binary from brainforce source code |
| 255 | +-cp $(PRG).cpl.x ../trabalhos# binary from c code with some prolog predicates linked to it |
| 256 | +-cp $(PRG).pl ../trabalhos# prolog source code |
| 257 | +-cp $(PRG).pl.x ../trabalhos# binary from prolog source code |
| 258 | +-cp $(PRG).so ../trabalhos# shared library object from c source code |
| 259 | +-cp $(PRG).pl.so ../trabalhos# c library object with some functions that may be called by a prolog program |
| 260 | + |
| 261 | +# Gera arquivo de indice tags com funcoes de todos fontes em C |
| 262 | +tags : |
| 263 | +ctags -R |
| 264 | +ctags -R -x | less -F |
| 265 | + |
| 266 | +# Gera um novo peN |
| 267 | +euler : |
| 268 | +-cp peN.c pe$(N).c |
| 269 | +-sed -i 's/PEN.c /$(PENAME)/' pe$(N).c |
| 270 | +-sed -i 's/PEN pe ## N/PEN pe ## $(N)/' pe$(N).c |
| 271 | + |
| 272 | +#* ------------------------------------------------------------------- * |
| 273 | +#* makefile config for Vim modeline * |
| 274 | +#* vi: set ai noet ts=4 sw=4 tw=0 wm=0 fo=croqlt : * |
| 275 | +#* Template by Dr. Beco <rcb at beco dot cc> Version 20170506.191339 * |
| 276 | + |
0 commit comments