From 3b7a74f1af544996e624cbfd2d0b103c477ba637 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilges Date: Thu, 12 Nov 2020 17:44:44 -0800 Subject: [PATCH] Enable `pkg-config`-driven pycairo cflags Allow pkg-config to detect flags for either: - `py3cairo`, or - `pycairo` The previous method of always using `sys.exec_prefix` to locate pycairo headers is insufficient on systems where Python and pycairo have different install prefixes (e.g. `/usr` vs. `/usr/local`). For backward compatibility, the previous method is still the default behavior when `pkg-config` cannot detect appropriate flags at runtime. Resolves issue #234 --- setup.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index 9985da5a2..99213fb69 100755 --- a/setup.py +++ b/setup.py @@ -228,14 +228,30 @@ def run(self): extra_comp_args = list(filter(lambda arg: arg != "-fvisibility=hidden", extra_comp_args)) if os.environ.get("PYCAIRO", "false") == "true": - try: - extra_comp_args.append('-DHAVE_PYCAIRO') - print("-I%s/include/pycairo".format(sys.exec_prefix)) - extra_comp_args.append("-I{0}/include/pycairo".format(sys.exec_prefix)) - #extra_comp_args.extend(check_output(["pkg-config", '--cflags', 'pycairo']).strip().split(' ')) - #linkflags.extend(check_output(["pkg-config", '--libs', 'pycairo']).strip().split(' ')) - except: - raise Exception("Failed to find compiler options for pycairo") + pycairo_library_name = None + pycairo_default_cflags = "-I{0}/include/pycairo".format(sys.exec_prefix) + for name in ("py3cairo", "pycairo"): + try: + check_output(["pkg-config", "--exists", name]) + except subprocess.CalledProcessError: + continue + else: + pycairo_library_name = name + + pycairo_flags_set = False + if pycairo_library_name: + try: + extra_comp_args.extend(check_output(["pkg-config", "--cflags", pycairo_library_name]).strip().split(" ")) + linkflags.extend(check_output(["pkg-config", "--libs", pycairo_library_name]).strip().split(" ")) + pycairo_flags_set = True + except subprocess.CalledProcessError: + pass + + if not pycairo_flags_set: + extra_comp_args.append(pycairo_default_cflags) + print("Failed to find compiler options for pycairo; using default CFLAGS:", pycairo_default_cflags) + + extra_comp_args.append('-DHAVE_PYCAIRO') if sys.platform == 'darwin': extra_comp_args.append('-mmacosx-version-min=10.11')