Skip to content

Commit

Permalink
gn: fix CXX detection
Browse files Browse the repository at this point in the history
Based on boost recipe.
  • Loading branch information
valgur committed Nov 3, 2023
1 parent d8874ba commit 68101d5
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions recipes/gn/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import os
import shutil
import sys
import textwrap
import time

from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.apple import is_apple_os
from conan.tools.apple import is_apple_os, XCRun
from conan.tools.build import check_min_cppstd
from conan.tools.env import VirtualBuildEnv, Environment
from conan.tools.env import VirtualBuildEnv
from conan.tools.files import chdir, copy, get, load, save, replace_in_file
from conan.tools.layout import basic_layout
from conan.tools.microsoft import is_msvc, VCVars
Expand Down Expand Up @@ -88,6 +89,22 @@ def generate(self):
configure_args.append("-d")
save(self, os.path.join(self.source_folder, "configure_args"), " ".join(configure_args))

@property
def _cxx(self):
compilers_by_conf = self.conf.get("tools.build:compiler_executables", default={}, check_type=dict)
cxx = compilers_by_conf.get("cpp") or VirtualBuildEnv(self).vars().get("CXX")
if cxx:
return cxx
if self.settings.compiler == "apple-clang":
return XCRun(self).cxx
compiler_version = self.settings.compiler.version
major = Version(compiler_version).major
if self.settings.compiler == "gcc":
return shutil.which(f"g++-{compiler_version}") or shutil.which(f"g++-{major}") or shutil.which("g++") or ""
if self.settings.compiler == "clang":
return shutil.which(f"clang++-{compiler_version}") or shutil.which(f"clang++-{major}") or shutil.which("clang++") or ""
return ""

def build(self):
with chdir(self, self.source_folder):
# Generate dummy header to be able to run `build/gen.py` with `--no-last-commit-position`.
Expand All @@ -107,8 +124,7 @@ def build(self):

# Make sure CXX env var is set, otherwise gn defaults it to clang++
# https://gn.googlesource.com/gn/+/refs/heads/main/build/gen.py#386
compilers_by_conf = self.conf.get("tools.build:compiler_executables", default={}, check_type=dict)
os.environ["CXX"] = compilers_by_conf.get("cpp") or VirtualBuildEnv(self).vars().get("CXX")
os.environ["CXX"] = self._cxx

self.run(f"{sys.executable} build/gen.py " + load(self, "configure_args"))

Expand Down

0 comments on commit 68101d5

Please sign in to comment.