Revision 6e7a5a62abc622d3a7771b4cff3d3cc7103ae7f7 authored by Rok Češnovar on 03 June 2021, 17:32:38 UTC, committed by GitHub on 03 June 2021, 17:32:38 UTC
2 parent s fdbfd0f + b70a727
Raw File
compiler_flags
# Find files subroutine for different operating system
# This is recursive version of the makefiles wildcard function
findfiles = $(strip $(subst \n,,$(foreach d,$(wildcard $(addsuffix *,$(1))),$(call findfiles,$(d)/,$(2)) $(wildcard $(d)/$(2)))))

ifneq ($(OS),Windows_NT)
GENERATE_DISTRIBUTION_TESTS=true
endif

# Detect operating system, set default compiler, and detect compiler type
# The defaults are configured for the compilers we test:
# https://github.com/stan-dev/stan/wiki/Supported-C---Compilers-and-Language-Features

## Detect operating system
ifneq ($(OS),Windows_NT)
  OS := $(shell uname -s)
endif

## Set OS specific library filename extensions
ifeq ($(OS),Windows_NT)
  LIBRARY_SUFFIX ?= .dll
endif

ifeq ($(OS),Darwin)
  LIBRARY_SUFFIX ?= .dylib
endif

ifeq ($(OS),Linux)
  LIBRARY_SUFFIX ?= .so
endif

## Set default compiler
ifeq (default,$(origin CXX))
  ifeq ($(OS),Darwin)  ## Darwin is Mac OS X
    CXX := clang++
  endif
  ifeq ($(OS),Linux)
    CXX := g++
  endif
  ifeq ($(OS),Windows_NT)
    CXX := g++
  endif
endif

# Detect compiler type
# - CXX_TYPE: {gcc, clang, mingw32-gcc, other}
# - CXX_MAJOR: major version of CXX
# - CXX_MINOR: minor version of CXX
ifneq (,$(findstring clang,$(CXX)))
  CXX_TYPE ?= clang
endif
ifneq (,$(findstring mingw32-g,$(CXX)))
  CXX_TYPE ?= mingw32-gcc
endif
ifneq (,$(findstring gcc,$(CXX)))
  CXX_TYPE ?= gcc
endif
ifneq (,$(findstring g++,$(CXX)))
  CXX_TYPE ?= gcc
endif
CXX_TYPE ?= other
CXX_VERSION :=  $(shell $(CXX) -dumpversion 2>&1)
CXX_MAJOR := $(word 1,$(subst ., ,$(CXX_VERSION)))
CXX_MINOR := $(word 2,$(subst ., ,$(CXX_VERSION)))


################################################################################
# Set optional compiler flags for performance
#
# These flags are compiler and compiler version specific optimization flags.
#
# Because clang-6 and clang-7's -dumpversion reports version 4.2.1
# and clang-6 needs either the llvm-ar or ar gold plugin to use link time
# optimization (flto), flto is only turned on for clang versions greater than 8.
# For gcc all versions after 4.9 can use the system `ar` and other utilities
# when compiling with flto.
#
# FLAGS:
# CXXFLAGS_OPTIM: Additional flags to CXXFLAGS
# CPPFLAGS_OPTIM: Additional flags to CXXFLAGS
# CXXFLAGS_OPTIM_TBB: Additional flags to TBB_CXXFLAGS
# CXXFLAGS_OPTIM_SUNDIALS: Additional flags to CXXFLAGS_SUNDIALS
# CPPFLAGS_OPTIM_SUNDIALS: Additional flags to CPPFLAGS_SUNDIALS
# CXXFLAGS_FLTO: Additional flags for compiling with FLTO
# CPPFLAGS_FLTO: Additional flags for compiling with FLTO
# CXXFLAGS_FLTO_TBB: Additional flags for compiling tbb with flto
# CXXFLAGS_FLTO_SUNDIALS: Additional flags for compiling c++ sundials with flto
# CPPFLAGS_FLTO_SUNDIALS: Additional flags for compiling c sundials with flto
# LDFLAGS_OPTIM: Adding optimization flags to LDFLAGS
# LDFLAGS_FLTO: Adding flto options to LDFLAGS
# LDFLAGS_MPI_FLTO: For adding flto options to MPI build
##

ifdef STAN_NO_RANGE_CHECKS
  CXXFLAGS_THREADS ?= -DSTAN_NO_RANGE_CHECKS
endif

################################################################################
# Set default compiler flags
#
# The options that have been commented are things that the user can set.
# They are commented because in make, undefined is different than empty;
# we can test for that everywhere, but it's a lot easier using the set if
# unassigned operator (`?=`) when it's not set.
##
O ?= 3

## setup includes
INC ?= $(INC_FIRST) -I $(if $(MATH),$(MATH),.) -I $(EIGEN) -I $(BOOST) $(INC_SUNDIALS)
INC_SUNDIALS ?= -I $(SUNDIALS)/include
INC_GTEST ?= -I $(GTEST)/include -I $(GTEST)

## setup precompiler options
CPPFLAGS_BOOST ?= -DBOOST_DISABLE_ASSERTS
CPPFLAGS_SUNDIALS ?= -DNO_FPRINTF_OUTPUT $(CPPFLAGS_OPTIM_SUNDIALS) $(CXXFLAGS_FLTO_SUNDIALS)
#CPPFLAGS_GTEST ?=


## setup compiler flags
CXXFLAGS_LANG ?= -std=c++1y
#CXXFLAGS_BOOST ?=
CXXFLAGS_SUNDIALS ?= -pipe $(CXXFLAGS_OPTIM_SUNDIALS) $(CPPFLAGS_FLTO_SUNDIALS)
#CXXFLAGS_GTEST


## These are the other compiler flags that can be set.
##
## They are listed here for documentation and are intentionally
## commented out.

# CXXFLAGS_OS
# CXXFLAGS_WARNINGS
# CXXFLAGS_EIGEN

## OPENCL specific flags
# CXXFLAGS_OPENCL
# LDFLAGS_OPENCL
# LDLIBS_OPENCL

## TBB specific flags
# CXXFLAGS_TBB
# LDFLAGS_TBB
# LDLIBS_TBB

## MPI specific flags
# CXXFLAGS_MPI
# LDFLAGS_MPI
# LDLIBS_MPI

################################################################################
# Update compiler flags with operating system specific modifications
##
ifeq ($(OS),Windows_NT)
  CXXFLAGS_WARNINGS ?= -Wall -Wno-unused-function -Wno-uninitialized -Wno-unused-but-set-variable -Wno-unused-variable -Wno-sign-compare -Wno-unused-local-typedefs -Wno-int-in-bool-context -Wno-attributes
  CPPFLAGS_GTEST ?= -DGTEST_HAS_PTHREAD=0
  CPPFLAGS_OS ?= -D_USE_MATH_DEFINES
  ## Detecting Process Bitness:
  ## http://blogs.msdn.com/b/david.wang/archive/2006/03/26/howto-detect-process-bitness.aspx
  ifeq ($(PROCESSOR_ARCHITECTURE)$(PROCESSOR_ARCHITEW6432),x86)
    CXXFLAGS_OS ?= -m32
  else
    CXXFLAGS_OS ?= -m64
  endif

  ifneq (gcc,$(CXX_TYPE))
    LDLIBS_OS ?= -static-libgcc
  else
    LDLIBS_OS ?= -static-libgcc -static-libstdc++
  endif

  EXE := .exe
endif

ifeq ($(OS),Darwin)
  ifeq (clang,$(CXX_TYPE))
    CXXFLAGS_OS ?= -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare
  endif
endif

ifeq ($(OS),Linux)
  CPPFLAGS_GTEST ?= -DGTEST_HAS_PTHREAD=0
  CXXFLAGS_WARNINGS ?= -Wno-sign-compare
  ifeq (gcc,$(CXX_TYPE))
    CXXFLAGS_OS ?= -pthread
  endif
  ifeq (mingw32-gcc,$(CXX_TYPE))
    LDLIBS_OS ?= -static -lwinpthread
    EXE := .exe
    WINE ?= wine
  endif
  ifeq (clang,$(CXX_TYPE))
    LDLIBS_OS ?= -lpthread
  endif
endif

## makes reentrant version lgamma_r available from cmath
CXXFLAGS_OS += -D_REENTRANT

## silence warnings occuring due to the TBB and Eigen libraries
CXXFLAGS_WARNINGS += -Wno-ignored-attributes

################################################################################
# Setup OpenCL
#
# Adds the following to CXXFLAGS_OPENCL
# OPENCL_DEVICE_ID - The ID of the OpenCL device (default: 0)
# OPENCL_PLATFORM_ID The ID of the OpenCL platform (default: 0)
# Both IDs can be found through installing and calling clinfo

ifdef STAN_OPENCL
  ifeq ($(OS),Darwin)  # Mac OS X
    LDLIBS_OPENCL ?= -framework OpenCL
  else
    LDLIBS_OPENCL ?= -lOpenCL
  endif
  OPENCL_DEVICE_ID ?= 0
  OPENCL_PLATFORM_ID ?= 0

  CPPFLAGS_OPENCL ?= -DSTAN_OPENCL -DOPENCL_DEVICE_ID=$(OPENCL_DEVICE_ID) -DOPENCL_PLATFORM_ID=$(OPENCL_PLATFORM_ID)
  CPPFLAGS_OPENCL += -DCL_HPP_TARGET_OPENCL_VERSION=120 -DCL_HPP_MINIMUM_OPENCL_VERSION=120
  CPPFLAGS_OPENCL += -DCL_HPP_ENABLE_EXCEPTIONS -Wno-ignored-attributes
  CXXFLAGS_OPENCL ?= -I $(OPENCL)
endif

################################################################################
# Setup Intel TBB
#
# Sets up TBB CXXFLAGS_TBB and LDFLAGS_TBB to compile and link to TBB
#
# The tbbmalloc and tbbmalloc_proxy libraries are optionally included
# as targets. By default these are included on MacOS only. This behavior
# can be altered by explicitly setting the TBB_LIBRARIES variable which
# should contain "tbb" or "tbb tbbmalloc tbbmalloc_proxy". Setting the
# TBB_LIBRARIES variable overrides the default.

TBB_BIN ?= $(MATH)lib/tbb
TBB_RELATIVE_PATH ?= ../$(notdir $(TBB))
TBB_BIN_ABSOLUTE_PATH = $(abspath $(TBB_BIN))
TBB_ABSOLUTE_PATH = $(abspath $(TBB))

ifeq ($(OS),Darwin)
  TBB_LIBRARIES ?= tbb tbbmalloc tbbmalloc_proxy
else
  TBB_LIBRARIES ?= tbb
endif

ifdef TBB_LIB

ifdef TBB_INC
CXXFLAGS_TBB ?= -I $(TBB_INC)
else
CXXFLAGS_TBB ?= -I $(TBB)/include
endif

LDFLAGS_TBB ?= -Wl,-L,"$(TBB_LIB)" -Wl,-rpath,"$(TBB_LIB)" -ltbb
LDLIBS_TBB ?= $(LDFLAGS_TBB)

else

ifeq ($(OS),Windows_NT)
  TBB_TARGETS ?= $(addprefix $(TBB_BIN)/,$(addsuffix $(LIBRARY_SUFFIX),$(TBB_LIBRARIES)))
endif
ifeq ($(OS),Darwin)
  TBB_TARGETS ?= $(addprefix $(TBB_BIN)/lib,$(addsuffix $(LIBRARY_SUFFIX), $(TBB_LIBRARIES)))
endif
ifeq ($(OS),Linux)
  # Update the suffix with the internal TBB source code!
  # The new version of TBB library is 12+ (e.g., libtbb.so.12.1 not libtbb.so.2)
  TBB_TARGETS ?= $(addprefix $(TBB_BIN)/lib,$(addsuffix $(LIBRARY_SUFFIX).2,$(TBB_LIBRARIES)))
endif

CXXFLAGS_TBB ?= -I $(TBB)/include
LDFLAGS_TBB ?= -Wl,-L,"$(TBB_BIN_ABSOLUTE_PATH)" -Wl,-rpath,"$(TBB_BIN_ABSOLUTE_PATH)" $(LDFLAGS_FLTO_FLTO) $(LDFLAGS_OPTIM_TBB)
LDLIBS_TBB ?= $(LDFLAGS_TBB)

endif

ifdef TBB_INTERFACE_NEW
CPPFLAGS_TBB ?= -DTBB_INTERFACE_NEW
endif

################################################################################
# Setup STAN_THREADS
#
# Sets up CXXFLAGS_THREADS to use threading

ifdef STAN_THREADS
  CXXFLAGS_THREADS ?= -DSTAN_THREADS
endif

################################################################################
# Setup MPI
#
# Sets up MPI CXXFLAGS_MPI and LDLIBS_MPI to compile and link to MPI

ifdef STAN_MPI
  ifeq ($(OS),Windows_NT)
    $(error MPI is not supported on Windows.)
  endif

  MPI_TARGETS ?= $(addsuffix $(LIBRARY_SUFFIX),$(BOOST)/stage/lib/libboost_serialization $(BOOST)/stage/lib/libboost_mpi) $(MPI_TEMPLATE_INSTANTIATION)
  CPPFLAGS_MPI ?= -DSTAN_MPI

  BOOST_LIBRARY_ABSOLUTE_PATH = $(abspath $(BOOST)/stage/lib)

  LDFLAGS_MPI ?= -Wl,-L,"$(BOOST_LIBRARY_ABSOLUTE_PATH)" -Wl,-rpath,"$(BOOST_LIBRARY_ABSOLUTE_PATH)" $(LDFLAGS_MPI_FLTO) $(MPI_OPTIM)
  CXXFLAGS_MPI ?= -Wno-delete-non-virtual-dtor
endif

CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) $(CXXFLAGS_FLTO) $(CXXFLAGS_OPTIM) -O$(O) $(INC)
CPPFLAGS += $(CPPFLAGS_LANG) $(CPPFLAGS_OS) $(CPPFLAGS_WARNINGS) $(CPPFLAGS_BOOST) $(CPPFLAGS_EIGEN) $(CPPFLAGS_OPENCL) $(CPPFLAGS_TBB) $(CPPFLAGS_MPI) $(CPPFLAGS_TBB) $(CPPFLAGS_FLTO) $(CPPFLAGS_OPTIM)
LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(LDFLAGS_WARNINGS) $(LDFLAGS_BOOST) $(LDFLAGS_EIGEN) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_TBB) $(LDFLAGS_FLTO) $(LDFLAGS_OPTIM)
LDLIBS += $(LDLIBS_LANG) $(LDLIBS_OS) $(LDLIBS_WARNINGS) $(LDLIBS_BOOST) $(LDLIBS_EIGEN) $(LDLIBS_OPENCL) $(LDLIBS_MPI) $(LDLIBS_TBB)

.PHONY: print-compiler-flags
print-compiler-flags:
	@echo '  Current configuration:'
	@echo '  - OS (Operating System):      ' $(OS)
	@echo '  - CXX (Compiler):             ' $(CXX)
	@echo '  - CXX_TYPE                    ' $(CXX_TYPE)
	@echo '  - Compiler version:           ' $(CXX_MAJOR).$(CXX_MINOR)
	@echo '  - O (Optimization Level):     ' $(O)
	@echo '  Library configuration:'
	@echo '  - EIGEN                       ' $(EIGEN)
	@echo '  - BOOST                       ' $(BOOST)
	@echo '  - SUNDIALS                    ' $(SUNDIALS)
	@echo '  - TBB                         ' $(TBB)
	@echo '  - GTEST                       ' $(GTEST)
	@echo '  - STAN_THREADS                ' $(STAN_THREADS)
	@echo '  - STAN_OPENCL                 ' $(STAN_OPENCL)
	@echo '  - STAN_MPI                    ' $(STAN_MPI)
	@echo '  Compiler flags (each can be overriden separately):'
	@echo '  - CXXFLAGS_LANG               ' $(CXXFLAGS_LANG)
	@echo '  - CXXFLAGS_WARNINGS           ' $(CXXFLAGS_WARNINGS)
	@echo '  - CXXFLAGS_OPTIM              ' $(CXXFLAGS_OPTIM)
	@echo '  - CXXFLAGS_FLTO               ' $(CXXFLAGS_FLTO)
	@echo '  - CXXFLAGS_BOOST              ' $(CXXFLAGS_BOOST)
	@echo '  - CXXFLAGS_EIGEN              ' $(CXXFLAGS_EIGEN)
	@echo '  - CXXFLAGS_OS                 ' $(CXXFLAGS_OS)
	@echo '  - CXXFLAGS_GTEST              ' $(CXXFLAGS_GTEST)
	@echo '  - CXXFLAGS_THREADS            ' $(CXXFLAGS_THREADS)
	@echo '  - CXXFLAGS_OPENCL             ' $(CXXFLAGS_OPENCL)
	@echo '  - CXXFLAGS_TBB                ' $(CXXFLAGS_TBB)
	@echo '  - CXXFLAGS_OPTIM_TBB          ' $(CXXFLAGS_OPTIM_TBB)
	@echo '  - CXXFLAGS_MPI                ' $(CXXFLAGS_MPI)
	@echo '  - CFLAGS_SUNDIALS             ' $(CFLAGS_SUNDIALS)
	@echo '  - CXXFLAGS_SUNDIALS           ' $(CXXFLAGS_SUNDIALS)
	@echo '  - CXXFLAGS_OPTIM_SUNDIALS     ' $(CXXFLAGS_OPTIM_SUNDIALS)
	@echo '  LDLIBS:'
	@echo '  - LDLIBS_LANG                 ' $(LDLIBS_LANG)
	@echo '  - LDLIBS_WARNINGS             ' $(LDLIBS_WARNINGS)
	@echo '  - LDLIBS_BOOST                ' $(LDLIBS_BOOST)
	@echo '  - LDLIBS_EIGEN                ' $(LDLIBS_EIGEN)
	@echo '  - LDLIBS_OS                   ' $(LDLIBS_OS)
	@echo '  - LDLIBS_GTEST                ' $(LDLIBS_GTEST)
	@echo '  - LDLIBS_OPENCL               ' $(LDLIBS_OPENCL)
	@echo '  - LDLIBS_TBB                  ' $(LDLIBS_TBB)
	@echo '  - LDLIBS_MPI                  ' $(LDLIBS_MPI)
	@echo '  LDFLAGS:'
	@echo '  - LDFLAGS_LANG                ' $(LDFLAGS_LANG)
	@echo '  - LDFLAGS_WARNINGS            ' $(LDFLAGS_WARNINGS)
	@echo '  - LDFLAGS_BOOST               ' $(LDFLAGS_BOOST)
	@echo '  - LDFLAGS_EIGEN               ' $(LDFLAGS_EIGEN)
	@echo '  - LDFLAGS_OS                  ' $(LDFLAGS_OS)
	@echo '  - LDFLAGS_GTEST               ' $(LDFLAGS_GTEST)
	@echo '  - LDFLAGS_OPENCL              ' $(LDFLAGS_OPENCL)
	@echo '  - LDFLAGS_TBB                 ' $(LDFLAGS_TBB)
	@echo '  - LDFLAGS_MPI                 ' $(LDFLAGS_MPI)
	@echo ''
back to top