189 lines
6.1 KiB
Makefile
Executable file
189 lines
6.1 KiB
Makefile
Executable file
# Generic Makefile for NetBeans for C/C++ projects
|
|
# Copyright (C) 2025 Benny Lyons
|
|
#
|
|
# This program is free software; you can redistribute it and/or
|
|
# modify it under the terms of the GNU General Public License
|
|
# as published by the Free Software Foundation; either version 2
|
|
# of the License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, write to the Free Software
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
# Platforms: Linux OpenIndiana
|
|
# Works from command line (use gmake on OpenIndiana) and from NetBeans
|
|
# Targets
|
|
# debug
|
|
# release
|
|
# check1 --> uses cppcheck (ensure it is installed to use)
|
|
# check2 --> uses clang-tidy (currently not available on OpenIndiana)
|
|
|
|
# Uses the variable NETBEANSBUILD which is added to _both_ the compile flags
|
|
# and the link process. Without this option Netbeans will not recognise the
|
|
# binary, so it is not possible to debug the binary in NetBeans.
|
|
# If you obtain the NetBeans error 'Not an ELF Binary', or something similar,
|
|
# then using $(NETBEANSBUILD) will most likely fix this.
|
|
#
|
|
|
|
|
|
# Change this to the name of your application, programme
|
|
# or the name of the binary this makefile produces
|
|
APP = myapp
|
|
|
|
# C++ compiler
|
|
# CXX:=clang++
|
|
CXX := g++
|
|
|
|
|
|
#
|
|
# NETBEANSBUILD
|
|
#
|
|
# Netbeans does not recognise an executable to debug out of the box.
|
|
# You must instruct the compiler to produce Platform Independent Code by
|
|
# using -no-pie. PIE is required to use Address Space Layout Randomization
|
|
# (ASLR), which is a security feature, which Netbeans cannot currently use.
|
|
#
|
|
# To fix, add the following option to CFLAGS and LDFLAGS
|
|
# -no-pie
|
|
NETBEANSBUILD := -no-pie
|
|
|
|
ifeq ($(MAKECMDGOALS),debug)
|
|
DBG_REL :=-ggdb3
|
|
else
|
|
DBG_REL :=
|
|
endif
|
|
|
|
#
|
|
# OS dependent flags
|
|
#
|
|
OS := $(shell uname -s)
|
|
|
|
#
|
|
# Targets:
|
|
# check1 uses cppcheck
|
|
# check2 uses clang-tidy
|
|
#
|
|
ifeq ($(MAKECMDGOALS),check1)
|
|
ifeq ($(OS),Linux)
|
|
# installed by system and in PATH
|
|
CPPCHECK := cppcheck
|
|
CHECKFLAGS :=
|
|
endif
|
|
ifeq ($(OS),SunOS)
|
|
# local installation
|
|
CPPCHECK := $(HOME)/devl/cppcheck/bin/cppcheck
|
|
endif
|
|
# Uncomment one of the following to select an output format
|
|
#CHECKFLAGS = --template="{file},{line},{severity},{id},{message}"
|
|
#CHECKFLAGS = --platform=unix64 --quiet --enable=all --error-exitcode=1 --check-level=exhaustive -I./include
|
|
CHECKFLAGS = --enable=all --error-exitcode=1 --suppress=missingIncludeSystem
|
|
endif
|
|
|
|
ifeq ($(MAKECMDGOALS),check2)
|
|
CPPCHECK := clang-tidy
|
|
CHECKFLAGS := --checks=*
|
|
#CHECKFLAGS := --checks=clang-analyzer-*
|
|
#CHECKFLAGS := --checks=cppcoreguidelines-*, clang-analyzer-*
|
|
endif
|
|
|
|
|
|
|
|
SRC_DIR := src
|
|
OBJ_DIR := obj
|
|
SRC := $(wildcard $(SRC_DIR)/*.cpp)
|
|
|
|
OBJS := $(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC))
|
|
|
|
##
|
|
## Add checks and issue warnings
|
|
##
|
|
# -Wall -Wextra # turn on all warnings.
|
|
# -Wconversion -Wsign-conversio # warn on unsign/sign conversions.
|
|
# -Wformat-security # warn onformat functions that might be a security issue
|
|
# -Werror # deactivate this initially? convert all warnings into errors.
|
|
# -march=x86-64 # take max advantage of address space (important for ASLR;
|
|
# # more virtual address space to chose from when randomising layout).
|
|
# -fstack-protector-all #
|
|
# -Wstack-protector #
|
|
# --param ssp-buffer-size=4 #
|
|
# -ftrapv # generate traps for signed overflow (currently bugged in gcc)
|
|
# -D_FORTIFY_SOURCE=2 -O2 # buffer overflow check. -D_FORTIFY_SOURCE=1 also possible
|
|
# -Wfloat-equal # testing floating-point numbers for equality is usually bad
|
|
# -Wpointer-arith # warn if anything depends upon the size of a function or of void
|
|
##_CXXFLAGS=-Wall -Wextra \
|
|
## -Wconversion -Wsign-conversion \
|
|
## -march=x86-64 \
|
|
## -Wformat-security \
|
|
## -fstack-protector-all -Wstack-protector --param ssp-buffer-size=4 \
|
|
## -ftrapv \
|
|
## -D_FORTIFY_SOURCE=2 -O2 \
|
|
## -Wfloat-equal \
|
|
## -Wpointer-arith
|
|
_CXXFLAGS=-Wall -Weffc++ -pedantic \
|
|
-pedantic-errors -Waggregate-return -Wcast-align \
|
|
-Wcast-qual -Wchar-subscripts -Wcomment \
|
|
-Wdisabled-optimization \
|
|
-Wformat -Wformat=2 \
|
|
-Wformat-nonliteral -Wformat-security \
|
|
-Wformat-y2k \
|
|
-Wimport -Winit-self -Winline \
|
|
-Winvalid-pch \
|
|
-Wlong-long -Wmissing-braces \
|
|
-Wmissing-format-attribute \
|
|
-Wmissing-include-dirs -Wmissing-noreturn \
|
|
-Wpacked -Wparentheses -Wpointer-arith \
|
|
-Wredundant-decls -Wreturn-type \
|
|
-Wsequence-point -Wshadow -Wsign-compare -fstack-protector-all -Wstack-protector \
|
|
-Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default \
|
|
-Wswitch-enum -Wtrigraphs \
|
|
-Wunknown-pragmas -Wunreachable-code \
|
|
-Wvariadic-macros \
|
|
-Wvolatile-register-var -Wwrite-strings\
|
|
-Werror=return-type \
|
|
-Werror=narrowing \
|
|
-ftrapv -Wfloat-equal -Wpointer-arith \
|
|
-D_FORTIFY_SOURCE=2 -O2
|
|
|
|
# app specific includes
|
|
_APP_CXXFLAGS =
|
|
CXXFLAGS = -std=c++17 $(NETBEANSBUILD) $(DBG_REL) -I./include/ $(_CXXFLAGS) $(_APP_CXXFLAGS)
|
|
|
|
# app specific libs
|
|
_APP_LIBS =
|
|
LIBS = $(NETBEANSBUILD) $(_APP_LIBS)
|
|
|
|
|
|
debug release: DIRS $(APP)
|
|
.PHONY: check1 check2
|
|
check1 check2: check
|
|
|
|
|
|
|
|
$(APP): $(OBJS)
|
|
$(CXX) -o bin/$@ $^ $(LIBS)
|
|
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
|
|
$(CXX) $(CXXFLAGS) -o $@ -c $<
|
|
|
|
.PHONEY: DIRS
|
|
DIRS:
|
|
mkdir -p obj bin
|
|
|
|
check: $(SRC)
|
|
$(info output to cppcheck.out)
|
|
$(info Check using: : $(CPPCHECK))
|
|
$(CPPCHECK) $(CHECKFLAGS) $^
|
|
|
|
|
|
.PHONY: clean
|
|
clean:
|
|
$(info cleaning up)
|
|
@rm -f $(APP) $(OBJ_DIR)/* ./bin/$(APP) cppcheck.out
|
|
@if [ -d "./bin" ]; then rmdir ./bin ; fi
|
|
@if [ -d "./obj" ]; then rmdir ./obj ; fi
|
|
|