1

I want to build two versions of my program, a "normal" version, and one in which the address sanitizer has been activated. As of now, I have this in my QSoas.pro

sanitizer {
  message("Activating the address sanitizer code")
  OBJECTS_DIR = build-snt
  QMAKE_CXXFLAGS += -fno-omit-frame-pointer -fsanitize=address
  LIBS += -fsanitize=address
  TARGET = $$join(TARGET,,,-snt)
}

This way, I can do:

~ qmake
~ make

to get the normal version, and

~ qmake CONFIG+=sanitizer
~ make

to get the version with the address sanitizer.

This is fine, but a little cumbersome, especially since I need in fact many other configuration options on the qmake command-line. Is there a way to have two targets, so that I could simply run

~ qmake
~ make
~ make my-sanitized-exe
Vincent Fourmond
  • 3,038
  • 1
  • 22
  • 24
  • Is [this](https://stackoverflow.com/a/2266285/3293508) what you are looking for? – Nguyen Cong Sep 06 '19 at 08:47
  • 1
    I am not sure it will work. Just an idea. You might have a set of makefiles, each of which specify its own configuration and call `make` with `-f` option to specify which makefile to use. – vahancho Sep 06 '19 at 09:03
  • @NguyenCong This is what I'm already doing. – Vincent Fourmond Sep 06 '19 at 09:06
  • @vahancho This is indeed a possibility... with the automatic rebuilding of the makefile by qmake, it could actually work. Would you make a full answer with that ? It looks like a hack, though :-)... – Vincent Fourmond Sep 06 '19 at 09:08

2 Answers2

1

This is my proposal on how you can achieve the desired behavior. The idea is using two (or more) different Makefiles that will tell the make tool what to do. To create a set of Makefiles I would do this (roughly):

~ qmake -o ./normal/Makefile

to create a normal version, and:

~ qmake CONFIG+=sanitizer -o ./sanitizer/Makefile

to create a Makefile for sanitizer. Now, if I want to build a normal version I call:

~ make -f ./normal/Makefile

and

~ make -f ./sanitizer/Makefile

to build an alternative version. Hopefully this will work.

vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Yeah, that does the job. I'm not accepting yet since this is not exactly what I asked, but I'll accept that in a couple of days if no other answer comes up. Thanks ! – Vincent Fourmond Sep 06 '19 at 09:32
1

The most natural way, IMO, is an out-of-source build. That is, create a subdirectory called "sanitizer", go into it, and build your Makefile(s) the same way you do it with cmake, meson etc.:

mkdir sanitizer
cd sanitizer
qmake CONFIG+=sanitizer ../Qsoas.pro

QMake natively supports out-of-source builds, so everything should be fine. However, if you need to distiguish between the source and build directories, you can use the variables $$PWD and $$OUT_PWD.

See also qmake's manual for shadowed() function to translate the paths automatically.

Matt
  • 13,674
  • 1
  • 18
  • 27