Skip to content

Commit 6c33bda

Browse files
authored
Fix Makefile handling of user CXXFLAGS & also add a DEBUG flag (#990)
The way `CXXFLAGS` and other flags were being set meant that users had to add the default values (`-std=c++17 -fopenmp -O3` etc) if they set the flags at all, or else lose the defaults. This was error-prone and suboptimal. The change here makes it so that the user values are appended at the end of the defaults, so that they can override values. This PR also adds a DEBUG flag that switches between using `-g -O0` and the regular `-O3` option. Developers can invoke it using, e.g., `make DEBUG=1`.
1 parent fca5a12 commit 6c33bda

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

Makefile

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
EIGEN_COMMIT = "b66188b5dfd147265bfa9ec47595ca0db72d21f5"
1717
EIGEN_URL = "https://gitlab.com/libeigen/eigen/-/archive/"
1818

19-
# Default build targets. Additional may be added conditionally below.
19+
# Default build targets. Additional ones are added conditionally below.
2020
TARGETS = qsim
2121
TESTS = run-cxx-tests
2222

2323
# By default, we also build the pybind11-based Python interface.
24+
# Can be overriden via env variables or command-line flags
2425
PYBIND11 ?= true
2526

2627
ifeq ($(PYBIND11), true)
@@ -31,14 +32,36 @@ endif
3132
# Default options for Pytest (only used if the pybind interface is built).
3233
PYTESTFLAGS ?= -v
3334

34-
# Default C++ compilers and compiler flags. Can be overriden via env variables.
35+
# Default compilers and compiler flags.
36+
# Can be overriden via env variables or command-line flags.
3537
CXX ?= g++
3638
NVCC ?= nvcc
3739
HIPCC ?= hipcc
3840

39-
CXXFLAGS ?= -O3 -std=c++17 -fopenmp -flto=auto
40-
NVCCFLAGS ?= -O3 --std c++17 -Wno-deprecated-gpu-targets
41-
HIPCCFLAGS ?= -O3
41+
BASE_CXXFLAGS := -std=c++17 -fopenmp
42+
BASE_NVCCFLAGS := -std c++17 -Wno-deprecated-gpu-targets
43+
BASE_HIPCCFLAGS :=
44+
45+
CXXFLAGS := $(BASE_CXXFLAGS) $(CXXFLAGS)
46+
NVCCFLAGS := $(BASE_NVCCFLAGS) $(NVCCFLAGS)
47+
HIPCCFLAGS := $(BASE_HIPCCFLAGS) $(HIPCCFLAGS)
48+
49+
LTO_FLAGS := -flto=auto
50+
USING_CLANG := $(shell $(CXX) --version | grep -isq clang && echo "true")
51+
ifeq ($(USING_CLANG),"true")
52+
LTO_FLAGS := -flto
53+
endif
54+
55+
ifdef DEBUG
56+
DEBUG_FLAGS := -g -O0
57+
CXXFLAGS += $(DEBUG_FLAGS)
58+
NVCCFLAGS += $(DEBUG_FLAGS)
59+
HIPCCFLAGS += $(DEBUG_FLAGS)
60+
else
61+
CXXFLAGS += -O3 $(LTO_FLAGS)
62+
NVCCFLAGS += -O3
63+
HIPCCFLAGS += -O3
64+
endif
4265

4366
# For compatibility with CMake, if $CUDAARCHS is set, use it to set the
4467
# architecture options to nvcc. Otherwise, default to the "native" option,

0 commit comments

Comments
 (0)