# [PATCH] Don't optimize code in debug build

4 messages from 2011-02-13 to 2011-02-13. Participants: Piotr Krukowiecki, Johannes Sixt, Junio C Hamano.
Thread: https://gitlist.dev/t/26487

## Piotr Krukowiecki, 2011-02-13 08:32

Subject: [PATCH] Don't optimize code in debug build
Message-ID: <AANLkTinw7DJ3hP7ACcScHTYy-Dvizq-OxefP6e0dEBbR@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTinw7DJ3hP7ACcScHTYy-Dvizq-OxefP6e0dEBbR%40mail.gmail.com

```
Code optimization makes debugging harder.

Signed-off-by: Piotr Krukowiecki <piotr.krukowiecki.news@gmail.com>
---
My first patch to this list, so please be gentle ;)

Patch fixes most important problem. There are other improvement possible:
- "-g" is not needed in normal build IMO, I'd move it to debug
- I'd add -O0 -fno-inline to debug too, but maybe it's too gccish
(OTOH there's -g already)

 Makefile |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/Makefile b/Makefile
index ade7923..32d3a69 100644
--- a/Makefile
+++ b/Makefile
@@ -262,7 +262,10 @@ endif

 # CFLAGS and LDFLAGS are for the users to override from the command line.

-CFLAGS = -g -O2 -Wall
+CFLAGS = -g -Wall
+ifndef DEBUG
+CFLAGS += -O2
+endif
 LDFLAGS =
 ALL_CFLAGS = $(CPPFLAGS) $(CFLAGS)
 ALL_LDFLAGS = $(LDFLAGS)
-- 
1.7.1

-- 
Piotrek

```

## Johannes Sixt, 2011-02-13 09:36

Subject: Re: [PATCH] Don't optimize code in debug build
Message-ID: <201102131036.51311.j6t@kdbg.org>
URL: https://gitlist.dev/e/201102131036.51311.j6t%40kdbg.org
In-Reply-To: <AANLkTinw7DJ3hP7ACcScHTYy-Dvizq-OxefP6e0dEBbR@mail.gmail.com>

```
On Sonntag, 13. Februar 2011, Piotr Krukowiecki wrote:
> --- a/Makefile
> +++ b/Makefile
> @@ -262,7 +262,10 @@ endif
>
>  # CFLAGS and LDFLAGS are for the users to override from the command line.
>
> -CFLAGS = -g -O2 -Wall
> +CFLAGS = -g -Wall
> +ifndef DEBUG
> +CFLAGS += -O2
> +endif
>  LDFLAGS =
>  ALL_CFLAGS = $(CPPFLAGS) $(CFLAGS)
>  ALL_LDFLAGS = $(LDFLAGS)

Instead of this, you can just write

 CFLAGS = -g -Wall

in your config.mak. Is anything wrong with that?

-- Hannes

```

## Piotr Krukowiecki, 2011-02-13 16:26

Subject: Re: [PATCH] Don't optimize code in debug build
Message-ID: <AANLkTinECgZwFZ0157O-qvYUW1GpJgbsHDekR1WM6gPm@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTinECgZwFZ0157O-qvYUW1GpJgbsHDekR1WM6gPm%40mail.gmail.com
In-Reply-To: <AANLkTim6OHU8N7t5RQnt6S7x7ez48RSwp=b82McAC4Oq@mail.gmail.com>

```
(forgot about the list when replying)

> On Sun, Feb 13, 2011 at 10:36 AM, Johannes Sixt <j6t@kdbg.org> wrote:
>> On Sonntag, 13. Februar 2011, Piotr Krukowiecki wrote:
>>> --- a/Makefile
>>> +++ b/Makefile
>>> @@ -262,7 +262,10 @@ endif
>>>
>>>  # CFLAGS and LDFLAGS are for the users to override from the command line.
>>>
>>> -CFLAGS = -g -O2 -Wall
>>> +CFLAGS = -g -Wall
>>> +ifndef DEBUG
>>> +CFLAGS += -O2
>>> +endif
>>>  LDFLAGS =
>>>  ALL_CFLAGS = $(CPPFLAGS) $(CFLAGS)
>>>  ALL_LDFLAGS = $(LDFLAGS)
>>
>> Instead of this, you can just write
>>
>>  CFLAGS = -g -Wall
>>
>> in your config.mak. Is anything wrong with that?

Several things: I need to know I should remove -O2, I need to keep in
sync with other default options, current default is less useful, it
requires me to do extra steps.
DEBUG is kind of standard - Makefile already uses it in Windows
specific part. I suppose we want to keep consistent.

-- 
Piotrek

```

## Junio C Hamano, 2011-02-13 19:28

Subject: Re: [PATCH] Don't optimize code in debug build
Message-ID: <7vbp2fybs7.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vbp2fybs7.fsf%40alter.siamese.dyndns.org
In-Reply-To: <201102131036.51311.j6t@kdbg.org>

```
Johannes Sixt <j6t@kdbg.org> writes:

>> @@ -262,7 +262,10 @@ endif
>>
>>  # CFLAGS and LDFLAGS are for the users to override from the command line.
>>
>> -CFLAGS = -g -O2 -Wall
>> +CFLAGS = -g -Wall
>> +ifndef DEBUG
>> +CFLAGS += -O2
>> +endif
>>  LDFLAGS =
>>  ALL_CFLAGS = $(CPPFLAGS) $(CFLAGS)
>>  ALL_LDFLAGS = $(LDFLAGS)
>
> Instead of this, you can just write
>
>  CFLAGS = -g -Wall
>
> in your config.mak. Is anything wrong with that?

You then need to feed -O2 from the command line when you are not doing a
debugging build.  On the other hand, with the current Makefile, you need
to feed "-g -O0" from the command line when you are doing a debugging
build if you and your debugger get confused when seeing an optimized
binary.  So neither is a very good solution.

But having to feed DEBUG=Yes when running a debug build is not a good
solution either.  A single toggle is simply too coarse-grained; the next
temptation after applying this patch would be to add

	ifdef DEBUG
        CFLAGS += -DDEBUG
        endif

and from there everything goes downhill.  That is the last thing we would
want to see happen.

So I would say the current Makefile is just fine as is.

```
