Repository: mesonbuild/meson

Issue:
WIP depdenencies/qt: Allow qt --no-framework on macos.
If we try to detect as a framework with qmake but doesn't find a
version of qt, allow falling through to finding as a non-framework
based build.

Fixes #5091
meson fails to find qt5 on macos when qt built with --no-framewok
Hello,

I've got a very strange failure to find qt5 on macos: it cannot find qt5 (modules: Core), even if it found qmake properly:
```
The Meson build system

Version: 0.49.2

Source dir: /Users/travis/build/bincrafters/conan-qt/test_package

Build dir: /Users/travis/build/bincrafters/conan-qt/test_package/build/048d7e866745e18def537ac6bfd40b46b6de3dd2/meson_folder

Build type: native build

Project name: test_package

Project version: undefined

Native C++ compiler: c++ (clang 10.0.0 "Apple LLVM version 10.0.0 (clang-1000.11.45.5)")

Build machine cpu family: x86_64

Build machine cpu: x86_64

WARNING: rcc dependencies will not work reliably until this upstream issue is fixed: https://bugreports.qt.io/browse/QTBUG-45460

Found pkg-config: /usr/local/bin/pkg-config (0.29.2)

Found qmake: /Users/travis/.conan/data/qt/5.12.2/bincrafters/testing/package/65406efd1da296857e7ee99e28c68c2399805be1/bin/qmake (5.12.2)

Dependency qt5 (modules: Core) found: NO (tried pkgconfig, qmake-qt5 and qmake)



meson.build:3:0: ERROR:  Dependency "qt5" not found, tried pkgconfig, qmake-qt5 and qmake
```
meson-log.txt says:
```
Build started at 2019-03-15T17:02:06.494677

Main binary: /Users/travis/.pyenv/versions/conan/bin/python3

Python system: Darwin

The Meson build system

Version: 0.49.2

Source dir: /Users/travis/build/bincrafters/conan-qt/test_package

Build dir: /Users/travis/build/bincrafters/conan-qt/test_package/build/048d7e866745e18def537ac6bfd40b46b6de3dd2/meson_folder

Build type: native build

Project name: test_package

Project version: undefined

Sanity testing C++ compiler: c++

Is cross compiler: False.

Sanity check compiler command line: c++ /Users/travis/build/bincrafters/conan-qt/test_package/build/048d7e866745e18def537ac6bfd40b46b6de3dd2/meson_folder/meson-private/sanitycheckcpp.cc -o /Users/travis/build/bincrafters/conan-qt/test_package/build/048d7e866745e18def537ac6bfd40b46b6de3dd2/meson_folder/meson-private/sanitycheckcpp.exe

Sanity check compile stdout:



-----

Sanity check compile stderr:



-----

Running test binary command: /Users/travis/build/bincrafters/conan-qt/test_package/build/048d7e866745e18def537ac6bfd40b46b6de3dd2/meson_folder/meson-private/sanitycheckcpp.exe

Native C++ compiler: c++ (clang 10.0.0 "Apple LLVM version 10.0.0 (clang-1000.11.45.5)")

Build machine cpu family: x86_64

Build machine cpu: x86_64

WARNING: rcc dependencies will not work reliably until this upstream issue is fixed: https://bugreports.qt.io/browse/QTBUG-45460

Found pkg-config: /usr/local/bin/pkg-config (0.29.2)

Determining dependency 'Qt5Core' with pkg-config executable '/usr/local/bin/pkg-config'

Called `/usr/local/bin/pkg-config --modversion Qt5Core` -> 1



Found qmake: /Users/travis/.conan/data/qt/5.12.2/bincrafters/testing/package/65406efd1da296857e7ee99e28c68c2399805be1/bin/qmake (5.12.2)

Building for macOS, looking for framework

Dependency qt5 (modules: Core) found: NO (tried pkgconfig, qmake-qt5 and qmake)



meson.build:3:0: ERROR:  Dependency "qt5" not found, tried pkgconfig, qmake-qt5 and qmake
```
meson 0.50.0 has the same output, but logfile has more details:

```

Main binary: /Users/travis/.pyenv/versions/conan/bin/python3

Python system: Darwin

The Meson build system

Version: 0.50.0

Source dir: /Users/travis/build/bincrafters/conan-qt/test_package

Build dir: /Users/travis/build/bincrafters/conan-qt/test_package/build/048d7e866745e18def537ac6bfd40b46b6de3dd2/meson_folder

Build type: native build

Project name: test_package

Project version: undefined

Sanity testing C++ compiler: c++

Is cross compiler: False.

Sanity check compiler command line: c++ /Users/travis/build/bincrafters/conan-qt/test_package/build/048d7e866745e18def537ac6bfd40b46b6de3dd2/meson_folder/meson-private/sanitycheckcpp.cc -o /Users/travis/build/bincrafters/conan-qt/test_package/build/048d7e866745e18def537ac6bfd40b46b6de3dd2/meson_folder/meson-private/sanitycheckcpp.exe

Sanity check compile stdout:



-----

Sanity check compile stderr:



-----

Running test binary command: /Users/travis/build/bincrafters/conan-qt/test_package/build/048d7e866745e18def537ac6bfd40b46b6de3dd2/meson_folder/meson-private/sanitycheckcpp.exe

Native C++ compiler: c++ (clang 10.0.0 "Apple LLVM version 10.0.0 (clang-1000.11.45.5)")

Build machine cpu family: x86_64

Build machine cpu: x86_64

WARNING: rcc dependencies will not work reliably until this upstream issue is fixed: https://bugreports.qt.io/browse/QTBUG-45460

Trying to find qt with pkg-config

Pkg-config binary for MachineChoice.HOST is not cached.

Pkg-config binary missing from cross or native file, or env var undefined.

Trying a default pkg-config fallback at pkg-config

Trying pkg-config binary pkg-config for machine MachineChoice.HOST at ['/usr/local/bin/pkg-config']

Found pkg-config: /usr/local/bin/pkg-config (0.29.2)

Determining dependency 'Qt5Core' with pkg-config executable '/usr/local/bin/pkg-config'

Called `/usr/local/bin/pkg-config --modversion Qt5Core` -> 1



Trying to find qt with qmake

Found qmake: /Users/travis/.conan/data/qt/5.12.2/bincrafters/testing/package/65406efd1da296857e7ee99e28c68c2399805be1/bin/qmake (5.12.2)

Building for macOS, looking for framework

Looking for qt framework QtCore

Finding framework path by running:  c++ -v -E - -pipe 



Looking for framework QtCore in /Users/travis/.conan/data/qt/5.12.2/bincrafters/testing/package/65406efd1da296857e7ee99e28c68c2399805be1/lib

Dependency qt5 (modules: Core) found: NO (tried pkgconfig, qmake-qt5 and qmake)



meson.build:3:0: ERROR: Dependency "qt5" not found, tried pkgconfig, qmake-qt5 and qmake
```

One more information, my Qt is built with `-no-framework` argument. Maybe meson supports only Qt as a framework on macos ?

when building Qt without `-no-framework` argument, meson successfully finds qt:
```

The Meson build system

Version: 0.50.0

Source dir: /Users/travis/build/bincrafters/conan-qt/test_package

Build dir: /Users/travis/build/bincrafters/conan-qt/test_package/build/048d7e866745e18def537ac6bfd40b46b6de3dd2/meson_folder

Build type: native build

Project name: test_package

Project version: undefined

Native C++ compiler: c++ (clang 10.0.0 "Apple LLVM version 10.0.0 (clang-1000.11.45.5)")

Build machine cpu family: x86_64

Build machine cpu: x86_64

WARNING: rcc dependencies will not work reliably until this upstream issue is fixed: https://bugreports.qt.io/browse/QTBUG-45460

Found pkg-config: /usr/local/bin/pkg-config (0.29.2)

Found qmake: /Users/travis/.conan/data/qt/5.12.2/bincrafters/testing/package/65406efd1da296857e7ee99e28c68c2399805be1/bin/qmake (5.12.2)

Dependency qt5 (modules: Core) found: YES 5.12.2 (qmake)

Detecting Qt5 tools

Found qmake: /Users/travis/.conan/data/qt/5.12.2/bincrafters/testing/package/65406efd1da296857e7ee99e28c68c2399805be1/bin/qmake (5.12.2)

 moc: YES (/Users/travis/.conan/data/qt/5.12.2/bincrafters/testing/package/65406efd1da296857e7ee99e28c68c2399805be1/bin/moc, 5.12.2)

 uic: YES (/Users/travis/.conan/data/qt/5.12.2/bincrafters/testing/package/65406efd1da296857e7ee99e28c68c2399805be1/bin/uic, 5.12.2)

 rcc: YES (/Users/travis/.conan/data/qt/5.12.2/bincrafters/testing/package/65406efd1da296857e7ee99e28c68c2399805be1/bin/rcc, 5.12.2)

 lrelease: NO

Build targets in project: 1
```


Discussion:
@ericLemanissier, could you test this patch and see if it works for you?
still does not work:

```
Build started at 2019-03-18T22:10:42.809196
Main binary: /Users/vsts/.pyenv/versions/conan/bin/python3
Python system: Darwin
The Meson build system
Version: 0.50.0
Source dir: /Users/vsts/agent/2.148.1/work/1/s/test_package
Build dir: /Users/vsts/agent/2.148.1/work/1/s/test_package/build/d7557b7867eb66bfbe96f4e499201a2138ce49c1/meson_folder
Build type: native build
Project name: test_package
Project version: undefined
Sanity testing C++ compiler: c++
Is cross compiler: False.
Sanity check compiler command line: c++ /Users/vsts/agent/2.148.1/work/1/s/test_package/build/d7557b7867eb66bfbe96f4e499201a2138ce49c1/meson_folder/meson-private/sanitycheckcpp.cc -o /Users/vsts/agent/2.148.1/work/1/s/test_package/build/d7557b7867eb66bfbe96f4e499201a2138ce49c1/meson_folder/meson-private/sanitycheckcpp.exe
Sanity check compile stdout:

-----
Sanity check compile stderr:

-----
Running test binary command: /Users/vsts/agent/2.148.1/work/1/s/test_package/build/d7557b7867eb66bfbe96f4e499201a2138ce49c1/meson_folder/meson-private/sanitycheckcpp.exe
Native C++ compiler: c++ (clang 10.0.0 "Apple LLVM version 10.0.0 (clang-1000.11.45.2)")
Build machine cpu family: x86_64
Build machine cpu: x86_64
WARNING: rcc dependencies will not work reliably until this upstream issue is fixed: https://bugreports.qt.io/browse/QTBUG-45460
Trying to find qt with pkg-config
Pkg-config binary for MachineChoice.HOST is not cached.
Pkg-config binary missing from cross or native file, or env var undefined.
Trying a default pkg-config fallback at pkg-config
Trying pkg-config binary pkg-config for machine MachineChoice.HOST at ['/usr/local/bin/pkg-config']
Found pkg-config: /usr/local/bin/pkg-config (0.29.2)
Determining dependency 'Qt5Core' with pkg-config executable '/usr/local/bin/pkg-config'
Called `/usr/local/bin/pkg-config --modversion Qt5Core` -> 1

Trying to find qt with qmake
Found qmake: /Users/vsts/.conan/data/qt/5.12.2/bincrafters/testing/package/4af5738ac0709a0b9c5e8a0af22beb6f466263c8/bin/qmake (5.12.2)
Building for macOS, looking for framework
Looking for qt framework QtCore
Finding framework path by running:  c++ -v -E - -pipe 

Looking for framework QtCore in /Users/vsts/.conan/data/qt/5.12.2/bincrafters/testing/package/4af5738ac0709a0b9c5e8a0af22beb6f466263c8/lib
Running compile:
Working directory:  /var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/tmpmmi47k00
Command line:  c++ /var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/tmpmmi47k00/testfile.cpp -pipe -o /var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/tmpmmi47k00/output.exe -O0 -fpermissive 

Code:
 #include<stdio.h>
        
        int main(int argc, char **argv) {
            printf("%ld\n", (long)(sizeof(void *)));
            return 0;
        };
Compiler stdout:
 
Compiler stderr:
 
Program stdout:

8

Program stderr:


Running compile:
Working directory:  /var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/tmpmg56qksd
Command line:  c++ /var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/tmpmg56qksd/testfile.cpp -pipe -c -o /var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/tmpmg56qksd/output.obj -O0 -fpermissive --print-search-dirs 

Code:
 
Compiler stdout:
 programs: =/Applications/Xcode_10.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
libraries: =/Applications/Xcode_10.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.0

Compiler stderr:
 
Dependency qt5 (modules: Core) found: NO (tried pkgconfig, qmake-qt5 and qmake)

meson.build:3:0: ERROR: Dependency "qt5" not found, tried pkgconfig, qmake-qt5 and qmake
```
EDIT: it does not work in all configurations. release build is failing on my azure pipeline, debug build is failing on travis-ci, but release build succeeds on travis-ci...
So should this be merged in its current form or should we stand by for an update that fixes everything?
I don't think anyone has ever tested qt with --no-framework on macos, when I've worked on it I used the version in homebrew. 
I've tested with the official Qt5 binaries which use a framework for macOS and static libraries (no framework) for iOS. Never tested macOS with --no-framework.
Indeed, it looks like if we're on macos then we assume that qmake is framework based. I have a patch that might fix this, but I'll don't really want to compile all of qt to test it :)
the problem with qt as a framework is that it is not compatible with debug only build: `debug-only framework builds are not supported. Configure with -no-framework if you want a pure debug build.` (this is a qt limitation)


Resolving patch:
diff --git a/mesonbuild/dependencies/ui.py b/mesonbuild/dependencies/ui.py
--- a/mesonbuild/dependencies/ui.py
+++ b/mesonbuild/dependencies/ui.py
@@ -360,7 +360,12 @@ def _qmake_detect(self, mods, kwargs):
         if self.env.machines.host.is_darwin() and not any(s in xspec for s in ['ios', 'tvos']):
             mlog.debug("Building for macOS, looking for framework")
             self._framework_detect(qvars, mods, kwargs)
-            return self.qmake.name
+            # Sometimes Qt is built not as a framework (for instance, when using conan pkg manager)
+            # skip and fall back to normal procedure then
+            if self.is_found:
+                return self.qmake.name
+            else:
+                mlog.debug("Building for macOS, couldn't find framework, falling back to library search")
         incdir = qvars['QT_INSTALL_HEADERS']
         self.compile_args.append('-I' + incdir)
         libdir = qvars['QT_INSTALL_LIBS']
@@ -410,6 +415,9 @@ def _get_modules_lib_suffix(self, is_debug):
                 suffix += 'd'
             if self.qtver == '4':
                 suffix += '4'
+        if self.env.machines[self.for_machine].is_darwin():
+            if is_debug:
+                suffix += '_debug'
         return suffix
 
     def _link_with_qtmain(self, is_debug, libdir):
@@ -432,8 +440,8 @@ def _framework_detect(self, qvars, modules, kwargs):
             fname = 'Qt' + m
             mlog.debug('Looking for qt framework ' + fname)
             fwdep = QtExtraFrameworkDependency(fname, self.env, fw_kwargs, language=self.language)
-            self.compile_args.append('-F' + libdir)
             if fwdep.found():
+                self.compile_args.append('-F' + libdir)
                 self.compile_args += fwdep.get_compile_args(with_private_headers=self.private_headers,
                                                             qt_version=self.version)
                 self.link_args += fwdep.get_link_args()
@@ -441,8 +449,8 @@ def _framework_detect(self, qvars, modules, kwargs):
                 break
         else:
             self.is_found = True
-        # Used by self.compilers_detect()
-        self.bindir = self.get_qmake_host_bins(qvars)
+            # Used by self.compilers_detect()
+            self.bindir = self.get_qmake_host_bins(qvars)
 
     def get_qmake_host_bins(self, qvars):
         # Prefer QT_HOST_BINS (qt5, correct for cross and native compiling)


Test patch: